[PATCH] D16209: Teach GlobalOpt not to drop DebugInfo on the floor when it promotes globals to allocas

Jonathan Roelofs via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 14 18:10:20 PST 2016


jroelofs created this revision.
jroelofs added reviewers: aprantl, llvm-commits.

Note this doesn't actually work yet... and I need help fixing it.

With this patch I get:
Assertion failed: (cast<DISubprogram>(Scope)->describes(MF->getFunction())), function getOrCreateRegularScope, file /Users/jroelofs/workdir/mento/llvm/lib/CodeGen/LexicalScopes.cpp, line 160.
0  llc                      0x000000010cd6f6ee llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 46
1  llc                      0x000000010cd71389 PrintStackTraceSignalHandler(void*) + 25
2  llc                      0x000000010cd6d989 llvm::sys::RunSignalHandlers() + 425
3  llc                      0x000000010cd716c9 SignalHandler(int) + 345
4  libsystem_platform.dylib 0x00007fff9585f52a _sigtramp + 26
5  llc                      0x000000010de14046 llvm::initializeUnifyFunctionExitNodesPass(llvm::PassRegistry&)::initialized + 84870
6  llc                      0x000000010cd713ab raise + 27
7  llc                      0x000000010cd71462 abort + 18
8  llc                      0x000000010cd71441 __assert_rtn + 129
9  llc                      0x000000010bf4a9ca llvm::LexicalScopes::getOrCreateRegularScope(llvm::DILocalScope const*) + 1930
10 llc                      0x000000010bf49212 llvm::LexicalScopes::getOrCreateLexicalScope(llvm::DILocalScope const*, llvm::DILocation const*) + 98
11 llc                      0x000000010bf4b3bc llvm::LexicalScopes::getOrCreateLexicalScope(llvm::DILocation const*) + 76
12 llc                      0x000000010bf4ab0e llvm::LexicalScopes::getMachineBasicBlocks(llvm::DILocation const*, llvm::SmallPtrSetImpl<llvm::MachineBasicBlock const*>&) + 62
13 llc                      0x000000010bf5859a (anonymous namespace)::UserValueScopes::dominates(llvm::MachineBasicBlock*) + 106
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
15 llc                      0x000000010bf56a8c (anonymous namespace)::UserValue::computeIntervals(llvm::MachineRegisterInfo&, llvm::TargetRegisterInfo const&, llvm::LiveIntervals&, llvm::MachineDominatorTree&, (anonymous namespace)::UserValueScopes&) + 1404
16 llc                      0x000000010bf540a6 (anonymous namespace)::LDVImpl::computeIntervals() + 742
17 llc                      0x000000010bf525b4 (anonymous namespace)::LDVImpl::runOnMachineFunction(llvm::MachineFunction&) + 292
18 llc                      0x000000010bf5230b llvm::LiveDebugVariables::runOnMachineFunction(llvm::MachineFunction&) + 187
19 llc                      0x000000010c02472e llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 110
20 llc                      0x000000010c59fdad llvm::FPPassManager::runOnFunction(llvm::Function&) + 413
21 llc                      0x000000010c5a00e5 llvm::FPPassManager::runOnModule(llvm::Module&) + 117
22 llc                      0x000000010c5a0e3a (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) + 2010
23 llc                      0x000000010c5a03cb llvm::legacy::PassManagerImpl::run(llvm::Module&) + 347
24 llc                      0x000000010c5a1a81 llvm::legacy::PassManager::run(llvm::Module&) + 33
25 llc                      0x000000010a8b9107 compileModule(char**, llvm::LLVMContext&) + 19239
26 llc                      0x000000010a8b4576 main + 230
27 libdyld.dylib            0x00007fff96b215ad start + 1
28 libdyld.dylib            0x0000000000000012 start + 1766713958

But I don't know how to get ahold of the DIScope that it wants.


Also this feels really awkward... so if there's a better way to do this, please let me know.

http://reviews.llvm.org/D16209

Files:
  tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp

Index: tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -28,6 +28,7 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/DIBuilder.h"
 #include "llvm/IR/GetElementPtrTypeIterator.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
@@ -86,6 +87,7 @@
                                const GlobalStatus &GS);
     bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
 
+    std::unique_ptr<DIBuilder> DIB;
     TargetLibraryInfo *TLI;
     SmallSet<const Comdat *, 8> NotDiscardableComdats;
   };
@@ -1749,6 +1751,16 @@
   }
 }
 
+static DIGlobalVariable *FindDebugInfo(GlobalVariable *GV) {
+  DebugInfoFinder F;
+  F.processModule(*GV->getParent());
+  for (DICompileUnit *DIC : F.compile_units())
+    for (DIGlobalVariable *DIG : DIC->getGlobalVariables())
+      if (DIG->getVariable() == GV)
+          return DIG;
+  return nullptr;
+}
+
 /// Analyze the specified global variable and optimize
 /// it if possible.  If we make a change, return true.
 bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
@@ -1776,11 +1788,26 @@
     // FIXME: Pass Global's alignment when globals have alignment
     AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
                                         GV->getName(), &FirstI);
+    StoreInst *Store = nullptr;
     if (!isa<UndefValue>(GV->getInitializer()))
-      new StoreInst(GV->getInitializer(), Alloca, &FirstI);
+      Store = new StoreInst(GV->getInitializer(), Alloca, &FirstI);
+
+    if (DIGlobalVariable *D = FindDebugInfo(GV)) {
+      if (DIType *T = dyn_cast<DIType>(D->getRawType())) {
+        DEBUG(dbgs() << "                   ... and transferring its DebugInfo\n");
+        auto *Scope = D->getScope();
+        auto *Variable = DIB->createAutoVariable(Scope,
+                                                 D->getLinkageName(),
+                                                 D->getFile(), D->getLine(),
+                                                 T);
+        auto *E = DIB->createExpression();
+        auto *DL = DILocation::get(GV->getContext(), D->getLine(), 0,
+                                   Scope);
+        DIB->insertDeclare(Alloca, Variable, E, DL, Store);
+      }
 
     makeAllConstantUsesInstructions(GV);
-    
+
     GV->replaceAllUsesWith(Alloca);
     GV->eraseFromParent();
     ++NumLocalized;
@@ -3062,6 +3089,7 @@
 }
 
 bool GlobalOpt::runOnModule(Module &M) {
+  DIB = llvm::make_unique<DIBuilder>(M, /*AllowUnresolved=*/ false);
   bool Changed = false;
 
   auto &DL = M.getDataLayout();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16209.44953.patch
Type: text/x-patch
Size: 2799 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160115/727bde61/attachment.bin>


More information about the llvm-commits mailing list