[LLVMdev] Prevent instruction elimination

Frits van Bommel fvbommel at gmail.com
Wed Oct 27 00:47:42 PDT 2010


On Tue, Oct 26, 2010 at 7:21 PM, Devang Patel <dpatel at apple.com> wrote:
>
> On Oct 26, 2010, at 9:23 AM, Xinfinity wrote:
>> Another idea would be to create a boolean as part of the IRBuilder and set
>> it from CodeGen. And for each instruction inserted by the IRBuilder to check
>> the boolean and attach metadata if necessary. But I think this is too
>> specialized to my particular needs and it would not be a good solution.
>> Is there a cleaner method?
>
> Setting a bit in IRBuilder is simpler. Why not use special purpose IRBuilder based on standard IRBuilder ?
> Another alternative is to take two step approach:
>
> step 1: insert special intrinsics to mark begin and end of your special scopes.
> step 2: immediately after clang finishes generating IR, run your special pass to remove your special intrinsics and update all instructions in the scope appropriately.
>
> This  way, you'll be able to localize your changes.

There's yet another way to implement this.
Currently, IRBuilder keeps track of a DebugLoc to set debug
information on instructions generated. You could generalize this to
keeping a SmallVector<std::pair<unsigned /*Kind*/, MDNode* /*Data*/>,
N> (or SmallSet/DenseMap/whatever) of metadata to set on generated
instructions. Debug information is then just a pair consisting of
LLVMContext::MD_dbg and debugloc.getAsMDNode(Context)).

Once you have this, you'd just need to call something like
Builder.addMetaData(MyKind, MyData) when entering your pragma'd block
and Builder.removeMetaData(MyKind) when exiting it.

Some other points:
 - This could be useful for other people too, so it might be accepted
as a patch.
 - Kind can also be a const char* if you prefer, but the unsigned ID
is probably more efficient when replacing or deleting Data of a
pre-existing Kind. Helper methods that accept StringRefs can easily be
added by calling Context.getMDKindID(name) before delegating to the
unsigned version.
 - This would probably make IRBuilder a bit more expensive to copy if
there's an additional allocation (i.e. if the SmallVector/SmallSet got
overfull, or if you're using a DenseMap).
 - I'm not sure how efficient it is to convert DebugLoc back and forth
to MDNode* (adding "dbg" metadata as an MDNode* automatically converts
it back because Instruction stores a DebugLoc) so maybe it should
still be handled specially. Benchmarking to see if it makes any
difference should be easy enough, just time compilation of something
large with clang -g...




More information about the llvm-dev mailing list