llvm::Type as llvm::Value in metadata

Hi all,

I've a question about something that's bothering me for quite some time now: why is a llvm::Function seen as a llvm::Value (in a sense that llvm::Function is a subclass of llvm::Value), but llvm::Type is not? Should they not - technically seen - be equal?

And to ask a concrete question: is there any way to store a reference to a llvm::Type within the LLVM metadata? As far as I know, the MD nodes take only llvm::Values, so I see no way of storing a reference to a type; it would be a nice thing to be able to store such informations, e.g. when using the metadata to store some more user-specific informations about the module.

Hope someone has a nice idea about this!

Greetings

Marcus

llvm::Function is a subclass of llvm::Value because you can use a
llvm::Function as a value (of type pointer-to-function). The same is
not true for llvm::Type.

-Eli

Yeah, I know that (in fact, I hoped to express exactly that :wink: ). The question is, why is that so? And how can you store references to llvm::Types in the metadata section of a module?

Greetings,

Marcus

Hi all,

I've a question about something that's bothering me for quite some time
now: why is a llvm::Function seen as a llvm::Value (in a sense that
llvm::Function is a subclass of llvm::Value), but llvm::Type is not? Should
they not - technically seen - be equal?

llvm::Function is a subclass of llvm::Value because you can use a
llvm::Function as a value (of type pointer-to-function). The same is
not true for llvm::Type.

Yeah, I know that (in fact, I hoped to express exactly that :wink: ). The
question is, why is that so?

Types are purely a compile-time concept in LLVM IR, just like in C;
there isn't any meta-type or reflection data associated with an LLVM
type.

And how can you store references to llvm::Types
in the metadata section of a module?

Essentially, you don't; you can store generic data in a string, though.

-Eli

Hi Marcus,

And to ask a concrete question: is there any way to store a reference to a llvm::Type within the LLVM metadata?

Not directly, but you can store a null value (Constant::getNullValue()) of the respective type.

Cheers,
Clemens

That's quite a pity, since unnamed types cannot be referenced by their name nor by their index or any other thing, but only by the pointer you get from llvm::StructType::get/llvm::ArrayType::get/..., am I right? Furthermore, one has to know the exact meaning of each string in the metadata (though it would probably be possible to do some name trickery to encode type names). However thanks for the answer.

Still, and just for the sake of interest, it would be nice to know about the reasons for this design decision (that means, why are llvm::Types not considered llvm::Values while llvm::Functions are).

Greetings

Marcus

And to ask a concrete question: is there any way to store a reference to a llvm::Type within the LLVM metadata?

Not directly, but you can store a null value (Constant::getNullValue())
of the respective type.

Cheers,
Clemens

Hi Clemens,

I thought about this already, yes. It would still require me to know that a specific information in the metadata refers to a type (which originally I wanted to avoid), but I guess it can't be helped and I have to implement it that way.

So long,

Marcus