More metadata questions

Hi,

I hope I'm not too annoying with my newbie questions.
I've read the new (great!) wiki about the DI* classes, and I still can't
figure out how to insert metadata into the LLVM IR.

Let's say that I want to have two passes, one that creates some metadata
string and inserts it into the IR, and the second one that reads it and acts
according to what it finds (or doesn't find).

Are the DI* classes the right way to go?
Or should I use MDString, etc?

Either way, I've found lots of ways to define them, but not the right way to
actually get them to be where I want them in the IR.

Thanks a lot,
Guy

Let's say that I want to have two passes, one that creates some metadata
string and inserts it into the IR, and the second one that reads it and acts
according to what it finds (or doesn't find).

Hi Guy,

I apologise, but when you said "C++" and "metadata" I assumed you
wanted Debug Metadata, which from your description, is clearly not the
case. :wink:

Are the DI* classes the right way to go?

Nope. Sorry about that.

Or should I use MDString, etc?

Yes. If the use would be interesting to others (langs, opts,
back-ends), you could create a library that would do that.

Either way, I've found lots of ways to define them, but not the right way to
actually get them to be where I want them in the IR.

Well, simply put, metadata has to be associated with a real Value* to
be kept in the IR. So, if you create an MDNode that points to a Value*
and has an MDString on it, you basically attached a metadata string to
a Value that will remain in the IR throughout the optimizations and
validations.

I don't know if you can read all metadata associated to a Value*
(back-reference), but if not, you can read all metadata and create the
back-ref map yourself. That would allow you to get any metadata from a
Value during your optimization passes.

Finally, always remember that metadata cannot ever be required for
code correctness. You can rely on it for optimization but your
optimization pass has to stop running and continue if it doesn't find
the metadata you're looking for.

hope that helps.

cheers,
--renato

Hi,

I hope I'm not too annoying with my newbie questions.
I've read the new (great!) wiki about the DI* classes, and I still can't
figure out how to insert metadata into the LLVM IR.

Let's say that I want to have two passes, one that creates some metadata
string and inserts it into the IR, and the second one that reads it and acts
according to what it finds (or doesn't find).

Are the DI* classes the right way to go?
Or should I use MDString, etc?

DI* classes are only for debug info processing. You should use MDString, MDNode etc... directly from metadata.h

Either way, I've found lots of ways to define them, but not the right way to
actually get them to be where I want them in the IR.

Right now, there are three ways:

1) Create independent MDNodes (just like Constants).
2) Use as an operands in intrinsics
3) Attached to an instruction

Extensible Metadata in LLVM IR - The LLVM Project Blog blog post covers these three using debugging information as an example.

How do you intend to use metadata ?