How to get the variable mapping between the source and llvm bytecode

Hi,

I want to know the variable mapping between the source and llvm bytecode. It seems that current llvm debugging information cannot provide this mapping directly.

Here is my examples (llvm 2.7). In this exmaple, I want to know the mapping: tmp<—>%4. But current llvm’s debugging information can only provide that the instruction “%4 = add nsw i32 %3, 2” is at line 3 in the C source. But since there’re two “+” at line 3, I don’t have a direct way to know which “+” is mapped to this “add”. Is there any way I can derive the mapping based on current ddg infor? Thanks.

Hi,

I want to know the variable mapping between the source and llvm bytecode. It
seems that current llvm debugging information cannot provide this mapping
directly.

Here is my examples (llvm 2.7). In this exmaple, I want to know the mapping:
tmp<--->%4. But current llvm's debugging information can only provide that
the instruction "%4 = add nsw i32 %3, 2" is at line 3 in the C source. But
since there're two "+" at line 3, I don't have a direct way to know which
"+" is mapped to this "add". Is there any way I can derive the mapping based
on current ddg infor? Thanks.

You want to map the "+" in the source to a specific "add" instruction?
clang does provide column number info, though, which might help a
bit. In general, though, debug info isn't rich enough to provide that
sort of mapping. What are you trying to do?

-Eli

Hi,

I want to get the mapping between C source’s variables and bytecode variables. It seems that llvm doesn’t provide this mapping, so I think a walk-around method is to get the instruction’s mapping. I have to use llvm-gcc, not clang. Any advice? Thanks.

Best,

Hi,

I want to get the mapping between C source's variables and bytecode
variables. It seems that llvm doesn't provide this mapping, so I think a
walk-around method is to get the instruction's mapping. I have to use
llvm-gcc, not clang. Any advice? Thanks.

"int tmp;" corresponds to "%tmp = alloca i32" in the IR. You can pull
that mapping out of the appropriate llvm.dbg.declare call. To find
assignments to the variable, look for store instructions which store
into "%tmp". I fail to see what the addition has to do with anything
in this context.

-Eli

Eli,

Thanks very much for your reply. That’s what I want to know. It works for this example. But I usually have to deal with the optimized bytecode with “-O”.
See the example:

The current situation is that LLVM's optimizations don't go out of
their way to preserve debug info, so you are only going to be able to
create the variable mapping with unoptimized IR.

Reid

Now the mapping will be tmp <---->%1. We can't derive the mapping based on
"alloca/store" instruction.

If you try llvm-gcc from svn trunk then you'll see llvm.dbg.value
intrinsic replacing llvm.dbg.declare intrinsic when alloca is lowered.
However, as others have said, the optimizer does not guarantee that it
will always be able to preserve variable info accurately.

The real question is, why do you want to map tmp to %1 ?