This is an archive of the discontinued LLVM Phabricator instance.

Teach GlobalOpt not to drop DebugInfo on the floor when it promotes globals to allocas
AbandonedPublic

Authored by jroelofs on Jan 14 2016, 6:10 PM.

Details

Summary

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.

Diff Detail

Event Timeline

jroelofs updated this revision to Diff 44953.Jan 14 2016, 6:10 PM
jroelofs retitled this revision from to Teach GlobalOpt not to drop DebugInfo on the floor when it promotes globals to allocas.
jroelofs updated this object.
jroelofs added reviewers: aprantl, llvm-commits.
jroelofs added inline comments.Jan 14 2016, 6:14 PM
tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
1793

What happens in insertDelcare when this ^ is nullptr?

1796

This ^ line in particular feels weird to me. I thought I should use D->getType().resolve(), but couldn't figure out how to get ahold of the parameter that it wants.

1798

Apparently this ^ isn't the scope that DILocation wants... not sure how to get the one it's looking for.

1806

Do I also need to insert the value one too?

3092

The false here is copy-pasta. I don't actually know if that's the value I want.

loladiro added inline comments.
tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
1796

Hmm, yeah, that's awkward. I don't think there's a good reason why the DIBuilder can't also take a type ref. In any case, this doesn't work because it could be an indirect reference.

1798

F->getSubprogram(), where F is the function you're inserting into probably makes the most sense.

1806

No, but this will segfault if Store (the argument indicates which instruction to insert before) is null. I suppose ideally this would be the instruction after either the store (if not null) or the alloca (if null). I wonder if DIBuilder should grow some new APIs to just create the instruction, and then insert it with an IRBuilder.

3092

It probably doesn't matter, but false is the conservative choice here.

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.

Might take some pretty major finagling to make that generalize/allowable.

@dblaikie Not sure if my explanation was clear: I'm trying to convert the DI metadata from the kind that references a global, to the kind that references a local. To do that, I /think/ I need to add the dbg.declare/value to the alloca that gets's created, and delete the one that references the soon-to-be-dead global. Does /that/ seem to you to be something out of the range of things that the DI metadata scheme can cope with?

If not, do you know how to delete the DIGlobalVariable here? I started down this rabbit hole because I've got a bug where the debug info emitted into the asm references the global... which no longer exists, causing the assembler to barf on it.

jroelofs added inline comments.Jan 15 2016, 7:00 AM
tools/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp
1796

How do I follow the indirection in that case?

1806

Is that the only thing it needs the store for? If so, maybe I can finagle another API to make it insertAfter the alloca when the Store arg is null.

3092

ok

jroelofs abandoned this revision.Jan 18 2016, 1:00 PM

I don't have the time/experience with debug-info stuffs to follow through with this, given how complicated it turned out to be... so I've filed llvm.org/PR26200