xref: /aosp_15_r20/external/llvm/docs/HowToUseAttributes.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker=====================
2*9880d681SAndroid Build Coastguard WorkerHow To Use Attributes
3*9880d681SAndroid Build Coastguard Worker=====================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker  :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerIntroduction
9*9880d681SAndroid Build Coastguard Worker============
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerAttributes in LLVM have changed in some fundamental ways.  It was necessary to
12*9880d681SAndroid Build Coastguard Workerdo this to support expanding the attributes to encompass more than a handful of
13*9880d681SAndroid Build Coastguard Workerattributes --- e.g. command line options.  The old way of handling attributes
14*9880d681SAndroid Build Coastguard Workerconsisted of representing them as a bit mask of values.  This bit mask was
15*9880d681SAndroid Build Coastguard Workerstored in a "list" structure that was reference counted.  The advantage of this
16*9880d681SAndroid Build Coastguard Workerwas that attributes could be manipulated with 'or's and 'and's.  The
17*9880d681SAndroid Build Coastguard Workerdisadvantage of this was that there was limited room for expansion, and
18*9880d681SAndroid Build Coastguard Workervirtually no support for attribute-value pairs other than alignment.
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard WorkerIn the new scheme, an ``Attribute`` object represents a single attribute that's
21*9880d681SAndroid Build Coastguard Workeruniqued.  You use the ``Attribute::get`` methods to create a new ``Attribute``
22*9880d681SAndroid Build Coastguard Workerobject.  An attribute can be a single "enum" value (the enum being the
23*9880d681SAndroid Build Coastguard Worker``Attribute::AttrKind`` enum), a string representing a target-dependent
24*9880d681SAndroid Build Coastguard Workerattribute, or an attribute-value pair.  Some examples:
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker* Target-independent: ``noinline``, ``zext``
27*9880d681SAndroid Build Coastguard Worker* Target-dependent: ``"no-sse"``, ``"thumb2"``
28*9880d681SAndroid Build Coastguard Worker* Attribute-value pair: ``"cpu" = "cortex-a8"``, ``align = 4``
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard WorkerNote: for an attribute value pair, we expect a target-dependent attribute to
31*9880d681SAndroid Build Coastguard Workerhave a string for the value.
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker``Attribute``
34*9880d681SAndroid Build Coastguard Worker=============
35*9880d681SAndroid Build Coastguard WorkerAn ``Attribute`` object is designed to be passed around by value.
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard WorkerBecause attributes are no longer represented as a bit mask, you will need to
38*9880d681SAndroid Build Coastguard Workerconvert any code which does treat them as a bit mask to use the new query
39*9880d681SAndroid Build Coastguard Workermethods on the Attribute class.
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker``AttributeSet``
42*9880d681SAndroid Build Coastguard Worker================
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard WorkerThe ``AttributeSet`` class replaces the old ``AttributeList`` class.  The
45*9880d681SAndroid Build Coastguard Worker``AttributeSet`` stores a collection of Attribute objects for each kind of
46*9880d681SAndroid Build Coastguard Workerobject that may have an attribute associated with it: the function as a
47*9880d681SAndroid Build Coastguard Workerwhole, the return type, or the function's parameters.  A function's attributes
48*9880d681SAndroid Build Coastguard Workerare at index ``AttributeSet::FunctionIndex``; the return type's attributes are
49*9880d681SAndroid Build Coastguard Workerat index ``AttributeSet::ReturnIndex``; and the function's parameters'
50*9880d681SAndroid Build Coastguard Workerattributes are at indices 1, ..., n (where 'n' is the number of parameters).
51*9880d681SAndroid Build Coastguard WorkerMost methods on the ``AttributeSet`` class take an index parameter.
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard WorkerAn ``AttributeSet`` is also a uniqued and immutable object.  You create an
54*9880d681SAndroid Build Coastguard Worker``AttributeSet`` through the ``AttributeSet::get`` methods.  You can add and
55*9880d681SAndroid Build Coastguard Workerremove attributes, which result in the creation of a new ``AttributeSet``.
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard WorkerAn ``AttributeSet`` object is designed to be passed around by value.
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard WorkerNote: It is advised that you do *not* use the ``AttributeSet`` "introspection"
60*9880d681SAndroid Build Coastguard Workermethods (e.g. ``Raw``, ``getRawPointer``, etc.).  These methods break
61*9880d681SAndroid Build Coastguard Workerencapsulation, and may be removed in a future release (i.e. LLVM 4.0).
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker``AttrBuilder``
64*9880d681SAndroid Build Coastguard Worker===============
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard WorkerLastly, we have a "builder" class to help create the ``AttributeSet`` object
67*9880d681SAndroid Build Coastguard Workerwithout having to create several different intermediate uniqued
68*9880d681SAndroid Build Coastguard Worker``AttributeSet`` objects.  The ``AttrBuilder`` class allows you to add and
69*9880d681SAndroid Build Coastguard Workerremove attributes at will.  The attributes won't be uniqued until you call the
70*9880d681SAndroid Build Coastguard Workerappropriate ``AttributeSet::get`` method.
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard WorkerAn ``AttrBuilder`` object is *not* designed to be passed around by value.  It
73*9880d681SAndroid Build Coastguard Workershould be passed by reference.
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard WorkerNote: It is advised that you do *not* use the ``AttrBuilder::addRawValue()``
76*9880d681SAndroid Build Coastguard Workermethod or the ``AttrBuilder(uint64_t Val)`` constructor.  These are for
77*9880d681SAndroid Build Coastguard Workerbackwards compatibility and may be removed in a future release (i.e. LLVM 4.0).
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard WorkerAnd that's basically it! A lot of functionality is hidden behind these classes,
80*9880d681SAndroid Build Coastguard Workerbut the interfaces are pretty straight forward.
81*9880d681SAndroid Build Coastguard Worker
82