简体   繁体   中英

What are "retainedNodes" in LLVMs debug metadata?

Using the LLVM 8.0.1 library, I try to create the debug info for a function with the following code:

DIFile *Unit = DebugBuilder->createFile(CompileUnit->getFilename(), CompileUnit->getDirectory());
DIScope *FContext(Unit);

DISubprogram *SP = DebugBuilder->createFunction(
    FContext, def->Name, def->Name, Unit, LineNo,
    CreateFunctionType(ft, CompileUnit->getFile()), 0);

func->setSubprogram(SP);

This, however, results in IR like the following:

define i32 @main(i32 %y) !dbg !3 {
entry:
  ret i32 2
}
    ; ...
!3 = !DISubprogram(name: "main", linkageName: "main", scope: !2, file: !2, type: !4, spFlags: 0, retainedNodes: !7)
    ; ...
!7 = <temporary!> !{}

Which, upon calling DebugBuilder->finalize() , throws Assertion failed: !N->isTemporary() && "Expected all forward declarations to be resolved"

I have not found a description of the retainedNodes field in the official reference nor other tutorials, and web searches only lead to uncommented sections of the LLVM source. What is the meaning or purpose of this field? How is a temporary node created there?

I found this solved by adding, before generating the DebugBuilder ,

TheModule->addModuleFlag(llvm::Module::Warning, "CodeView", 1);

... as explained apparently nowhere in the official documentation.

I had the same problem with the LLVM C API (using inkwell-rs as a wrapper). I fixed this problem by invoking LLVMDIBuilderCreateFunction with IsDefinition = true and IsLocalToUnit = true . This keeps the retainedNodes metadata node, but its value is empty metadata ( !{} ) instead of a temporary.

I solved a similar problem by finalizing the subprogram explicitly with finalizeSubprogram.

DebugBuilder->finalizeSubprogram(SP);

This seems to resolve the temporary, but I still got some warnings, when compiling the generated IR.

If you make the function a definition by adding the DISubprogram::SPFlagDefinition flag to DebugBuilder->createFunction call, retainedNodes will be set to an empty node instead of a temporary.

要添加其他答案,可以在此处找到 RetainedNodes 的描述: https : //llvm.org/docs/LangRef.html#dissubprogram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM