LLVM Discussion Forums

Associate insructions with source code

Is it possible to associate instructions with the relevant source code statement/range?.
For my use case i am only interested in memory operations and compiling with -O0.
The goal is, after some analyses at the IR level, to be able to highlight the “interesting” reads/writes. Is there some straightforward way to achieve this?

Thanks in advance

In general there is not mapping, however you can use debug information to get an approximation. Depending on the use-case it may or may not be suitable.

Thank you for the response. I am aware of DebugInfo. This is what i am using at the moment. So far it seems that after running mem2reg i get the correct line/col for the remaining Store and Load instructions. At least in the inputs i’ve tried (Cuda kernels).

I was wondering whether its possible to do better than that. For instance not only get a start line/col but also end line/col for a given Load/Store. Or even just an end line only. I’ve thought of looking up the lines of subsequent instructions and pick the closest one to the line of the load/store and use that value (-1) as the end line. However i think this is not robust enough as (modulo any other complications) multiple LoadInst might belong to the same read statement and the stament might span multiple lines, plus there may be whitespaces between that statement and the next.

I’ve thought of looking up libTooling as well but then again, im still not sure how to associate IR values with their AST statement.