Keeping variable names for LLVM IR with swift compiler frontend

Viewed 107

I am very new to compiler development. For a project I need to find a way to keep the naming of the variables in my swift code for the LLVM IR. This is because I want to run an analyzer pass on the IR that makes use of those names.

I have tried this with self compiled Clang and found a flag "-fno-discard-value-names" that achieves this purpose. Is there anything that matches this functionality for Swiftc?

1 Answers

A possible solution is to rely on debug info, by compiling with the -g flag.

The IR would have intrinsic calls, such as the one below,

%91 = extractvalue { i64, i1 } %90, 0, !dbg !257
...
call void @llvm.dbg.value(metadata i64 %89, metadata !245, metadata !DIExpression()), !dbg !247

that map values in the ir (%91) to high level debug information. Then it's easier to map that info to the source code, such as:

!258 = !DILocalVariable(name: "e", scope: !256, file: !19, line: 35, type: !246)

Be aware that there are other dbg intrinsics that may appear !

Related