<div dir="ltr">I doubt the debug info metadata schema can cope with what you really want here - I imagine it's pretty clear about dbg.declare/value things referencing local variables in the function, not global variables.<br><br>Might take some pretty major finagling to make that generalize/allowable.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 14, 2016 at 6:10 PM, Jonathan Roelofs via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">jroelofs created this revision.<br>
jroelofs added reviewers: aprantl, llvm-commits.<br>
<br>
Note this doesn't actually work yet... and I need help fixing it.<br>
<br>
With this patch I get:<br>
Assertion failed: (cast<DISubprogram>(Scope)->describes(MF->getFunction())), function getOrCreateRegularScope, file /Users/jroelofs/workdir/mento/llvm/lib/CodeGen/LexicalScopes.cpp, line 160.<br>
0  llc                      0x000000010cd6f6ee llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 46<br>
1  llc                      0x000000010cd71389 PrintStackTraceSignalHandler(void*) + 25<br>
2  llc                      0x000000010cd6d989 llvm::sys::RunSignalHandlers() + 425<br>
3  llc                      0x000000010cd716c9 SignalHandler(int) + 345<br>
4  libsystem_platform.dylib 0x00007fff9585f52a _sigtramp + 26<br>
5  llc                      0x000000010de14046 llvm::initializeUnifyFunctionExitNodesPass(llvm::PassRegistry&)::initialized + 84870<br>
6  llc                      0x000000010cd713ab raise + 27<br>
7  llc                      0x000000010cd71462 abort + 18<br>
8  llc                      0x000000010cd71441 __assert_rtn + 129<br>
9  llc                      0x000000010bf4a9ca llvm::LexicalScopes::getOrCreateRegularScope(llvm::DILocalScope const*) + 1930<br>
10 llc                      0x000000010bf49212 llvm::LexicalScopes::getOrCreateLexicalScope(llvm::DILocalScope const*, llvm::DILocation const*) + 98<br>
11 llc                      0x000000010bf4b3bc llvm::LexicalScopes::getOrCreateLexicalScope(llvm::DILocation const*) + 76<br>
12 llc                      0x000000010bf4ab0e llvm::LexicalScopes::getMachineBasicBlocks(llvm::DILocation const*, llvm::SmallPtrSetImpl<llvm::MachineBasicBlock const*>&) + 62<br>
13 llc                      0x000000010bf5859a (anonymous namespace)::UserValueScopes::dominates(llvm::MachineBasicBlock*) + 106<br>
14 llc                      0x000000010bf57744 (anonymous namespace)::UserValue::extendDef(llvm::SlotIndex, unsigned int, llvm::LiveRange*, llvm::VNInfo const*, llvm::SmallVectorImpl<llvm::SlotIndex>*, llvm::LiveIntervals&, llvm::MachineDominatorTree&, (anonymous namespace)::UserValueScopes&) + 1300<br>
15 llc                      0x000000010bf56a8c (anonymous namespace)::UserValue::computeIntervals(llvm::MachineRegisterInfo&, llvm::TargetRegisterInfo const&, llvm::LiveIntervals&, llvm::MachineDominatorTree&, (anonymous namespace)::UserValueScopes&) + 1404<br>
16 llc                      0x000000010bf540a6 (anonymous namespace)::LDVImpl::computeIntervals() + 742<br>
17 llc                      0x000000010bf525b4 (anonymous namespace)::LDVImpl::runOnMachineFunction(llvm::MachineFunction&) + 292<br>
18 llc                      0x000000010bf5230b llvm::LiveDebugVariables::runOnMachineFunction(llvm::MachineFunction&) + 187<br>
19 llc                      0x000000010c02472e llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 110<br>
20 llc                      0x000000010c59fdad llvm::FPPassManager::runOnFunction(llvm::Function&) + 413<br>
21 llc                      0x000000010c5a00e5 llvm::FPPassManager::runOnModule(llvm::Module&) + 117<br>
22 llc                      0x000000010c5a0e3a (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) + 2010<br>
23 llc                      0x000000010c5a03cb llvm::legacy::PassManagerImpl::run(llvm::Module&) + 347<br>
24 llc                      0x000000010c5a1a81 llvm::legacy::PassManager::run(llvm::Module&) + 33<br>
25 llc                      0x000000010a8b9107 compileModule(char**, llvm::LLVMContext&) + 19239<br>
26 llc                      0x000000010a8b4576 main + 230<br>
27 libdyld.dylib            0x00007fff96b215ad start + 1<br>
28 libdyld.dylib            0x0000000000000012 start + 1766713958<br>
<br>
But I don't know how to get ahold of the DIScope that it wants.<br>
<br>
<br>
Also this feels really awkward... so if there's a better way to do this, please let me know.<br>
<br>
<a href="http://reviews.llvm.org/D16209" rel="noreferrer" target="_blank">http://reviews.llvm.org/D16209</a><br>
<br>
Files:<br>
  tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp<br>
<br>
Index: tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp<br>
===================================================================<br>
--- tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp<br>
+++ tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp<br>
@@ -28,6 +28,7 @@<br>
 #include "llvm/IR/Constants.h"<br>
 #include "llvm/IR/DataLayout.h"<br>
 #include "llvm/IR/DerivedTypes.h"<br>
+#include "llvm/IR/DIBuilder.h"<br>
 #include "llvm/IR/GetElementPtrTypeIterator.h"<br>
 #include "llvm/IR/Instructions.h"<br>
 #include "llvm/IR/IntrinsicInst.h"<br>
@@ -86,6 +87,7 @@<br>
                                const GlobalStatus &GS);<br>
     bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);<br>
<br>
+    std::unique_ptr<DIBuilder> DIB;<br>
     TargetLibraryInfo *TLI;<br>
     SmallSet<const Comdat *, 8> NotDiscardableComdats;<br>
   };<br>
@@ -1749,6 +1751,16 @@<br>
   }<br>
 }<br>
<br>
+static DIGlobalVariable *FindDebugInfo(GlobalVariable *GV) {<br>
+  DebugInfoFinder F;<br>
+  F.processModule(*GV->getParent());<br>
+  for (DICompileUnit *DIC : F.compile_units())<br>
+    for (DIGlobalVariable *DIG : DIC->getGlobalVariables())<br>
+      if (DIG->getVariable() == GV)<br>
+          return DIG;<br>
+  return nullptr;<br>
+}<br>
+<br>
 /// Analyze the specified global variable and optimize<br>
 /// it if possible.  If we make a change, return true.<br>
 bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,<br>
@@ -1776,11 +1788,26 @@<br>
     // FIXME: Pass Global's alignment when globals have alignment<br>
     AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,<br>
                                         GV->getName(), &FirstI);<br>
+    StoreInst *Store = nullptr;<br>
     if (!isa<UndefValue>(GV->getInitializer()))<br>
-      new StoreInst(GV->getInitializer(), Alloca, &FirstI);<br>
+      Store = new StoreInst(GV->getInitializer(), Alloca, &FirstI);<br>
+<br>
+    if (DIGlobalVariable *D = FindDebugInfo(GV)) {<br>
+      if (DIType *T = dyn_cast<DIType>(D->getRawType())) {<br>
+        DEBUG(dbgs() << "                   ... and transferring its DebugInfo\n");<br>
+        auto *Scope = D->getScope();<br>
+        auto *Variable = DIB->createAutoVariable(Scope,<br>
+                                                 D->getLinkageName(),<br>
+                                                 D->getFile(), D->getLine(),<br>
+                                                 T);<br>
+        auto *E = DIB->createExpression();<br>
+        auto *DL = DILocation::get(GV->getContext(), D->getLine(), 0,<br>
+                                   Scope);<br>
+        DIB->insertDeclare(Alloca, Variable, E, DL, Store);<br>
+      }<br>
<br>
     makeAllConstantUsesInstructions(GV);<br>
-<br>
+<br>
     GV->replaceAllUsesWith(Alloca);<br>
     GV->eraseFromParent();<br>
     ++NumLocalized;<br>
@@ -3062,6 +3089,7 @@<br>
 }<br>
<br>
 bool GlobalOpt::runOnModule(Module &M) {<br>
+  DIB = llvm::make_unique<DIBuilder>(M, /*AllowUnresolved=*/ false);<br>
   bool Changed = false;<br>
<br>
   auto &DL = M.getDataLayout();<br>
<br>
<br>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div>