Replacement for findStopPoint() in LLVM 2.7

Dear LLVMers,

I'm updating some code to use the new LLVM 2.7 API. One piece of this code uses the findStopPoint() function to find the source filename and line number information of an instruction.

What is the best way to do this under LLVM 2.7 now that the stop point intrinsic has been removed? It appears that the debug information is attached as metadata, but what is the easiest way to extract the filename and line number information out of this metadata?

Thanks in advance.

-- John T.

Something like this (you can of course cache TheMetadata and MDDbgKind)

llvm::MetadataContext *TheMetadata = M->getContext().getMetadata();
MDDbgKind = TheMetadata->getMDKind("dbg");
if (MDDbgKind) {
  if (MDNode *Dbg = TheMetadata->getMD(MDDbgKind, I)) {
        DILocation Loc(Dbg);
  ...
  Loc.getDirectory()
  Loc.getFilename()
  Loc.getLineNumber()
        Loc.getColumnNumber()
  ....
  }
}

Best regards,
--Edwin

Török Edwin wrote:

  

Dear LLVMers,

I'm updating some code to use the new LLVM 2.7 API. One piece of this code uses the findStopPoint() function to find the source filename and line number information of an instruction.

What is the best way to do this under LLVM 2.7 now that the stop point intrinsic has been removed? It appears that the debug information is attached as metadata, but what is the easiest way to extract the filename and line number information out of this metadata?

Something like this (you can of course cache TheMetadata and MDDbgKind)

llvm::MetadataContext *TheMetadata = M->getContext().getMetadata();
MDDbgKind = TheMetadata->getMDKind("dbg");
if (MDDbgKind) {
  if (MDNode *Dbg = TheMetadata->getMD(MDDbgKind, I)) {
        DILocation Loc(Dbg);
  ...
  Loc.getDirectory()
  Loc.getFilename()
  Loc.getLineNumber()
        Loc.getColumnNumber()
  ....
  }
  
A grep through my LLVM 2.7 source tree does not find MetadataContext anywhere. Is this something that was added to LLVM mainline after the LLVM 2.7 branch was created?

I'm using the release_27 branch of LLVM, so I'm limited to facilities within that branch.

-- John T.

Török Edwin wrote:

Dear LLVMers,

I'm updating some code to use the new LLVM 2.7 API. One piece of
this code uses the findStopPoint() function to find the source
filename and line number information of an instruction.

What is the best way to do this under LLVM 2.7 now that the stop
point intrinsic has been removed? It appears that the debug
information is attached as metadata, but what is the easiest way to
extract the filename and line number information out of this metadata?

Something like this (you can of course cache TheMetadata and MDDbgKind)

llvm::MetadataContext *TheMetadata = M->getContext().getMetadata();
MDDbgKind = TheMetadata->getMDKind("dbg");
if (MDDbgKind) {
  if (MDNode *Dbg = TheMetadata->getMD(MDDbgKind, I)) {
        DILocation Loc(Dbg);
    ...
    Loc.getDirectory()
    Loc.getFilename()
    Loc.getLineNumber()
        Loc.getColumnNumber()
    ....
  }
  
A grep through my LLVM 2.7 source tree does not find MetadataContext
anywhere. Is this something that was added to LLVM mainline after the
LLVM 2.7 branch was created?

It should be there, include/llvm/Metadata.h.

I'm using the release_27 branch of LLVM, so I'm limited to facilities
within that branch.

The example I've given is from code way older than the 2.7 branch (Dec
2009 or so).

Best regards,
--Edwin

Török Edwin wrote:

[snip]

Something like this (you can of course cache TheMetadata and MDDbgKind)

llvm::MetadataContext *TheMetadata = M->getContext().getMetadata();
MDDbgKind = TheMetadata->getMDKind("dbg");
if (MDDbgKind) {
  if (MDNode *Dbg = TheMetadata->getMD(MDDbgKind, I)) {
        DILocation Loc(Dbg);
    ...
    Loc.getDirectory()
    Loc.getFilename()
    Loc.getLineNumber()
        Loc.getColumnNumber()
    ....
  }
  

A grep through my LLVM 2.7 source tree does not find MetadataContext
anywhere. Is this something that was added to LLVM mainline after the
LLVM 2.7 branch was created?
    
It should be there, include/llvm/Metadata.h.
  
Odd. I just looked in there, and MetadataContext is not defined in that file.
  

I'm using the release_27 branch of LLVM, so I'm limited to facilities
within that branch.
    
The example I've given is from code way older than the 2.7 branch (Dec
2009 or so).
  
Is is possible that someone removed it?

-- John T.

Ah, the method got moved to the instruction itself!

dbgKind = Context->getMDKindID("dbg");
if (MDNode *Dbg = I->getMetadata(dbgKind)) {
...

Török Edwin wrote:

[snip]

Ah, the method got moved to the instruction itself!

dbgKind = Context->getMDKindID("dbg");
if (MDNode *Dbg = I->getMetadata(dbgKind)) {
...
  
Thanks! This appears to work.

I also have code that looks up debug information for GlobalVariables and regular LLVM Value *'s. For the former, I think I can look up their debug information by using the DebugInfoFinder class and iterating through all the MDNodes for global variables (using the global_variable_begin()/global_variable_end() methods). Is this the best way to do it, or is there a better way?

For LLVM Value *s, I'm not sure how to get the debug information if they come from alloca instructions. Is the best option to look for a use of the Value in a llvm.dbg.declare() call and then grab the debug metadata from that?

-- John T.

Török Edwin wrote:

[snip]

Ah, the method got moved to the instruction itself!

dbgKind = Context->getMDKindID("dbg");
if (MDNode *Dbg = I->getMetadata(dbgKind)) {
...
  
Thanks! This appears to work.

Ok.

I also have code that looks up debug information for GlobalVariables and
regular LLVM Value *'s. For the former, I think I can look up their
debug information by using the DebugInfoFinder class and iterating
through all the MDNodes for global variables (using the
global_variable_begin()/global_variable_end() methods). Is this the
best way to do it, or is there a better way?

For LLVM Value *s, I'm not sure how to get the debug information if they
come from alloca instructions. Is the best option to look for a use of
the Value in a llvm.dbg.declare() call and then grab the debug metadata
from that?

I wasn't following the dbg.declare changes, but I think there is a
llvm.dbg.value now that survives mem2reg (or rather it is dbg.declare
initially and becomes dbg.value later).

Best regards,
--Edwin

This should all be described at
http://llvm.org/docs/SourceLevelDebugging.html. If something's missing
there, please ask Devang to add it.

Devang, I don't think there's any documentation about how to _read_
debug info (for example, pointing to Instruction::getMetadata() and
include/llvm/Analysis/DebugInfo.h). Could you add some?

Thanks,
Jeffrey

For LLVM Value *s, I'm not sure how to get the debug information if they
come from alloca instructions. Is the best option to look for a use of
the Value in a llvm.dbg.declare() call and then grab the debug metadata
from that?

Now llvm.dbg.declare() uses metadata, so it won't show up in uses list
for the Value. This is done to ensure that debug info does not
influence optimizer. You need to check all llvm.dbg.declare
intrinsics' operands' elements.

I wasn't following the dbg.declare changes, but I think there is a
llvm.dbg.value now that survives mem2reg (or rather it is dbg.declare
initially and becomes dbg.value later).

lllvm.dbg.declare is transformed into llvm.dbg.value when alloca disappears.

That'll work, but may not be as efficient as it could be, if you're
only interested in global variables' debug info. You can try following

  NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
  if (!NMD)
    return;

  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
    DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
    ...
   }