How To Use Attributes
Introduction
Attributes in LLVM have changed in some fundamental ways. It was necessary todo this to support expanding the attributes to encompass more than a handful ofattributes — e.g. command line options. The old way of handling attributesconsisted of representing them as a bit mask of values. This bit mask wasstored in a “list” structure that was reference counted. The advantage of thiswas that attributes could be manipulated with ‘or’s and ‘and’s. Thedisadvantage of this was that there was limited room for expansion, andvirtually no support for attribute-value pairs other than alignment.
In the new scheme, an Attribute
object represents a single attribute that’suniqued. You use the Attribute::get
methods to create a new Attribute
object. An attribute can be a single “enum” value (the enum being theAttribute::AttrKind
enum), a string representing a target-dependentattribute, or an attribute-value pair. Some examples:
- Target-independent:
noinline
,zext
- Target-dependent:
"no-sse"
,"thumb2"
- Attribute-value pair:
"cpu" = "cortex-a8"
,align = 4
Note: for an attribute value pair, we expect a target-dependent attribute tohave a string for the value.
Attribute
An Attribute
object is designed to be passed around by value.
Because attributes are no longer represented as a bit mask, you will need toconvert any code which does treat them as a bit mask to use the new querymethods on the Attribute class.
AttributeList
The AttributeList
stores a collection of Attribute objects for each kind ofobject that may have an attribute associated with it: the function as a whole,the return type, or the function’s parameters. A function’s attributes are atindex AttributeList::FunctionIndex
; the return type’s attributes are atindex AttributeList::ReturnIndex
; and the function’s parameters’ attributesare at indices 1, …, n (where ‘n’ is the number of parameters). Most methodson the AttributeList
class take an index parameter.
An AttributeList
is also a uniqued and immutable object. You create anAttributeList
through the AttributeList::get
methods. You can add andremove attributes, which result in the creation of a new AttributeList
.
An AttributeList
object is designed to be passed around by value.
Note: It is advised that you do not use the AttributeList
“introspection”methods (e.g. Raw
, getRawPointer
, etc.). These methods breakencapsulation, and may be removed in a future release (i.e. LLVM 4.0).
AttrBuilder
Lastly, we have a “builder” class to help create the AttributeList
objectwithout having to create several different intermediate uniquedAttributeList
objects. The AttrBuilder
class allows you to add andremove attributes at will. The attributes won’t be uniqued until you call theappropriate AttributeList::get
method.
An AttrBuilder
object is not designed to be passed around by value. Itshould be passed by reference.
Note: It is advised that you do not use the AttrBuilder::addRawValue()
method or the AttrBuilder(uint64_t Val)
constructor. These are forbackwards compatibility and may be removed in a future release (i.e. LLVM 4.0).
And that’s basically it! A lot of functionality is hidden behind these classes,but the interfaces are pretty straight forward.