Hi, I wonder how the metadata is preserved during the transformation? For example, we need location information during debug process, how do we preserve the metadata during (may be a constant-fold or something else) a transformation. Should we write some code maually to do so or the IR can “automaticlly” do?
Metadata in LLVM is preserved on a best-effort basis. This means that metadata in LLVM needs to have its semantics defined in such a way that it is always correct to drop metadata if a transformation does not understand how to preserve it.
Now, just because metadata can be dropped doesn’t mean that transformations should drop it. Indeed, there are a number of utilities in LLVM that help to preserve metadata. Since you mentioned debug information, I’ll focus on that.
The IRBuilder
helper class will propagate known debug location metadata to all instructions it constructs. All you need to do to preserve this metadata if you use IRBuilder
is tell it the instruction to grab the metadata from. The SetInsertPoint
–if you tell it to insert at a specific instruction–will be sufficient to set the debug location. If a transformation is generally working along the lines of “expand an instruction into multiple ones”, then use of IRBuilder
would be highly recommended for getting this stuff correct.
If your goal is code motion, then moving an instruction anywhere within a function will preserve its metadata. Similarly, cloning an instruction (via Instruction::clone
) will also preserve existing metadata. Note that in many cases, however, it would still be necessary to drop unknown metadata, since you can’t guarantee that, for example, arbitrary metadata can be safely duplicated [1]. For this purpose, there are functions on Instruction that will drop all metadata except debug locations.
[1] An example of such a case: imagine a metadata that indicated that the pointer created by the instruction was the only pointer that could point to that location–duplicating the instruction means that said metadata is now inaccurate.