[LLVMdev] Ideas for annotating types?

Talin viridia at gmail.com
Sun Jul 10 17:26:32 PDT 2011

So based on your suggestion, I've sketched out the following interface:

class TypeAnnotationMap {
  /** Create a new type annotation map with the specified name. */
  static TypeAnnotation * create(StringRef name);

  /** Load a type annotation map from a module. */
  static TypeAnnotation * load(Module * m, StringRef name);

  /** Serialize this annotation map as a global variable in module 'm'.
      This will replace any pre-existing variable of the same name. */
  void store(Module * m);

  /** Return the name of this type annotation map. */
  StringRef name() const;

  /** Set the value of the annotation for the input type 'ty'. This replaces
      pre-existing value associated with 'ty'. */
  void put(const Type * ty, const Constant * val);

  /** Get the value of the annotation for type 'ty'. */
  const Constant * get(const Type * ty) const;

  /** Remove any value associated with type 'ty'. */
  void remove(const Type * ty);

An example use would be as follows:

// Create a new map
TypeAnnotationMap * aMap = TypeAnnotationMap::create("llvm.gc.root");

// Add an entry to the map.
aMap->put(ty, data);

// Store as a global variable in a module

Internally the class has a map of Type pointers to Constant pointers. To
serialize the map, it converts the internal map to a list of types (which
are represented as members of a struct), and a list of constants. It then
creates a tuple whose first member is a NULL pointer to the struct type, and
whose second member is a pointer to the list of constants. It creates a
global variable in the module whose name is the name of the map, and sets
it's initializer to be that tuple.

A module can have an arbitrary number of such maps, each with a different
map name.

Does that sound about right?

On Sun, Jul 10, 2011 at 11:59 AM, Renato Golin <rengolin at systemcall.org>wrote:

> On 10 July 2011 19:47, Talin <viridia at gmail.com> wrote:
> > However, I would imagine other uses for this facility. What this
> effectively
> > is "metadata for types", although unlike metadata nodes this data would
> be
> > immutable.
> Hi Talin,
> The new type system allows you to name types and, if I got it rght,
> that's unique and immutable.
> You could use that fact, plus some metadata, to group your values and
> use one annotation per group.
> Should be easy to iterate through all values in a given function
> testing for a given (unique) type.
> cheers,
> --renato

-- Talin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110710/a57f5c86/attachment.html>

More information about the llvm-dev mailing list