Accessing metadata & creating DIVariable

Hi,

I am wondering if someone can guide me in adding metadata to IR which
already contains some metadata. I am trying to add dbg.declare inst
for a local variable I added to a function. I used the DIBuilder to
build a DIVariable. When I try to compile llc fails with following
message.

  llc: MCAsmStreamer.cpp:273: virtual
void<unnamed>::MCAsmStreamer::EmitLabel(llvm::MCSymbol*): Assertion
`Symbol->isUndefined() && "Cannot define a symbol twice!"' failed.

I can't figure out what is wrong, there are no obvious duplicate
entries. The original CU entry got replaced by the CU I created with
DIBuilder. Not sure, if changing the CU is the cause of error.

What is the right way to get a handle on the MDNode of the CompileUnit
in a Module ?

bye,
Vyas

What is the right way to get a handle on the MDNode of the CompileUnit
in a Module ?

The CompileUnit is an instance of DICompileUnit, which is just a wrapper around a MDNode. See DebugInfo.h

I can’t figure out what is wrong, there are no obvious duplicate
entries. The original CU entry got replaced by the CU I created with
DIBuilder. Not sure, if changing the CU is the cause of error.

How did you create a CU ?

I am adding local var to existing IR with debug info. I am not using
the Pass infrastructure.

void InsertDbg(AllocaInst *i, StringRef varname, Instruction, inserbefore)
{

DIBuilder di(*module);
cu = di.createCU / * How do I get the MDNode of already in the
IR . Instead of recreating it, using filename and directory I knew out
of band */

DIBuilder.createCompileUnit() does not return anything.

file= di.createFile

You don't need CU here.

type= di.createBasicType //long

same, you don't need CU here.

If I don't have a di.createCompileUnit. di.createFile fails with this message.

DIBuilder.cpp:59: llvm::DIFile
llvm::DIBuilder::createFile(llvm::StringRef, llvm::StringRef):
Assertion `TheCU && "Unable to create DW_TAG_file_type without
CompileUnit"' failed.

Which had me scratching my head, If DIFile needs a CU it would be an
argument to DIBuilder::.createFile
Also type.Verify() thought createBasicType succeeds.

I meant type.Verify() fails, though the call creatBasicType succeeds.

You need to call di.createCompileUnit() once for your translation unit in the beginning. You don't need to keep track of CU yourself. DIBuilder will take care of it. After words, you can call di.createBasicType(..) and it will work.

Hi,

You need to call di.createCompileUnit() once for your translation unit in the beginning. You don't need to keep track of CU yourself. DIBuilder will take care of it. After words, you can call di.createBasicType(..) and it will work.

Actually it doesn't. But if I call di.createCompileUnit(file, dir,
producer, ...) with file or dir different from that of CU already in
the file, then it works. It isn't sufficient if the producer is
different. That fixes my problem, so many thanks.

If DIBuiler constructor would pick existing CU from the Module, we
can retain original CU entry. But, I do no have enough knowledge of
DWARF or LLVM, to know if it is valid/reasonable thing to do.

thanks,
Vyas

Either you have not answered my earlier question OR I missed your reply. In any case, how did you get (or create) your pre existing CU from the Module ? What did you do to construct it ?

I get the filename and directory from DISubprogram from reading
llvm.dbg.sp and use
that to DIBuiler::.createCompileUnit. This leads to "duplicate
symbol" error from llc.

If the llvm::Module already has llvm.dbg.sp then you don't need to use DIBuilder::createCompileUnit(). The point, I am missing is how did you get llvm.dbg.sp in module ?

It seems you already have generated debug info in llvm::Module using DIBuilder. If that is true then you don't need to use createCompileUnit() for the same translation unit.

If you reading a llvm::Module which already has a debug info and you are trying to add more debug info in the module than DIBuilder is not the tool for you.

Have you looked at how clang generated debug info ?

I get the filename and directory from DISubprogram from reading
llvm.dbg.sp and use
that to DIBuiler::.createCompileUnit. This leads to "duplicate
symbol" error from llc.

If the llvm::Module already has llvm.dbg.sp then you don't need to use DIBuilder::createCompileUnit(). The point, I am missing is how did you get llvm.dbg.sp in module ?

It seems you already have generated debug info in llvm::Module using DIBuilder. If that is true then you don't need to use createCompileUnit() for the same translation unit.

Yes, that was what I said in the first line of my original mail :slight_smile: I
am adding instrumentation code/variables to IR, and I want to add
debuginfo for the new variables and calls I introduced.

If you reading a llvm::Module which already has a debug info and you are trying to add more debug info in the module than DIBuilder is not the tool for you.

:frowning:

Have you looked at how clang generated debug info ?

No experience with clang, But where do I have to look ? Is what I
am looking for even possible. I thought since it is said optimization
passes update the metadata, it should be simple.

Vyas