Attributes (nounwind, align 8, "frame-pointer"="all",
byval(%T) and so on) are recorded in two blocks. The PARAMATTR_GROUP block (10)
defines groups, each a set of attributes for one position (return value, one
parameter, or the function). The PARAMATTR block (9) defines attribute lists, each a
list of group IDs. A function, call or global variable names a list by its 1-based
number; 0 means none. Operand order and checks come from
parseAttributeBlock and parseAttributeGroupBlock in
BitcodeReader.cpp (lines 2078–2138 and 2406–2638) and from the writer
(BitcodeWriter.cpp lines 1083–1172). The kind numbers are in
LLVMBitCodes.h, lines 726–840.
The two blocks
| Block | Code | Record | Operands | Reader’s rules |
|---|---|---|---|---|
| 9 | 1 | ENTRY_OLD | [paramidx, attrbits, paramidx, attrbits, …] | An odd number of operands is Invalid parameter attribute record. Each pair is one position and a bit mask; the record makes one list. Written by LLVM 3.2 and earlier. |
| 9 | 2 | ENTRY | [group ID, group ID, …] | One list. The group IDs are those of the GRP_ENTRY records. The reader does not check each ID against the groups it has read. The writer writes one record per list, the groups in position order, leaving out positions without attributes. |
| 10 | 3 | GRP_CODE_ENTRY | [group ID, position, attribute…] | Fewer than 3 operands: Invalid grp record. The attributes are described below. |
The position is 0xFFFFFFFF for the function, 0 for the return value and
1 upwards for the parameters (the first parameter is 1). Each block may appear once:
a second PARAMATTR or PARAMATTR_GROUP block is Invalid multiple blocks.
Records of other codes in either block are skipped. A stream error inside the block
is Malformed block. The group block comes before the list block in the
files LLVM writes, and a list refers to groups by ID, not by position.
A reference to a list is the operand paramattr of FUNCTION, the attribute operand of GLOBALVAR, and the first operand of CALL, INVOKE and CALLBR records. Number n means the n-th ENTRY record, counting from 1; a number past the end, and 0, give an empty list.
Attribute encodings in a group
After the group ID and the position, the record is a sequence of attributes. Each one starts with a form number that says how it is encoded. Where the table says a kind, it is a number from the kind table.
| Form | Operands that follow | Meaning |
|---|---|---|
0 | kind | An enum attribute: a flag with no value. The kind must be an enum attribute, or the reader reports Not an enum attribute. Three exceptions: uwtable (33) is accepted here and means the default unwind table kind, dead_on_return (103) with default information, and the typed kinds below give an attribute whose type is filled in later. |
1 | kind, value | An integer attribute. The kind must be an integer attribute (Not an int attribute). The value is interpreted as below. |
3 | key chars…, 0 | A string attribute with no value, such as "no-jump-tables". |
4 | key chars…, 0, value chars…, 0 | A string attribute with a value, such as "frame-pointer"="all". The writer uses form 3 when the value is empty, so "a"="" reads back as "a". The reader only asserts that each string ends with a zero. |
5 | kind | A type attribute with no type (an old file, or a type that is not known). The kind must be a type attribute (Not a type attribute). |
6 | kind, type ID | A type attribute with its type, such as byval(%struct.s) or sret(i32). The ID is looked up in the type table. |
7 | kind, bit width, lower, upper | A constant-range attribute (range). See ranges. The kind must be one (Not a ConstantRange attribute). |
8 | kind, count, bit width, range… | A list of constant ranges (initializes). Fewer than 2 operands left: Too few records for constant range list. Ranges that are unordered or overlap: Invalid (unordered or overlapping) range list. The kind must be a range-list attribute (Not a constant range list attribute). |
A constant range (forms 7 and 8) is read by readConstantRange
(BitcodeReader.cpp lines 872 to 902): fewer than 2 operands left for the
lower and upper bounds, or, for a width above 64 bits, fewer operands left than the
two active-word counts in the first word announce, is Too few records for
range (lines 876, 881); a missing bit-width operand in front of a range is the
same message (line 900).
Any other form number is Invalid attribute group entry. A kind number that is
not in the table, or is one of the old kinds listed below outside the place where it is
accepted, is Unknown attribute kind (n). After the loop the reader
runs LLVM’s attribute upgrade on the group.
Integer attribute values
| Kind | Attribute | Value |
|---|---|---|
| 1 | align | The alignment in bytes (a power of two). Not a logarithm, unlike the alignment operands of other records. |
| 25 | alignstack | The stack alignment in bytes (getWithStackAlignment stores A.value(), Attributes.cpp line 226). |
| 41, 42 | dereferenceable, dereferenceable_or_null | The number of bytes. |
| 51 | allocsize | (element size argument << 32) | number-of-elements argument; a low half of 0xFFFFFFFF means the second argument is absent (Attributes.cpp lines 61–79). |
| 74 | vscale_range | (min << 32) | max; a max of 0 means there is no maximum (lines 81–92). |
| 33 | uwtable | 0 none, 1 sync, 2 async (the default; CodeGen.h UWTableKind). |
| 82 | allockind | A bit set: 1 alloc, 2 realloc, 4 free, 8 uninitialized, 16 zeroed, 32 aligned (Attributes.h lines 55–64). |
| 86 | memory | A memory effects value with a version in the top byte. |
| 102 | captures | (components captured other than by return << 4) | (components captured by return). Each part is a bit set of 4 bits: 1 address is null only, 2 address (implies 1), 4 read provenance, 8 provenance (implies 4) (ModRef.h lines 365–371, 485–493). |
| 87 | nofpclass | A mask of floating-point classes; the reader keeps only the defined bits (fcAllFlags). |
| 103 | dead_on_return | Built with DeadOnReturnInfo::createFromIntValue. |
| 106 | denormal_fpenv | Built with DenormalFPEnv::createFromIntValue. |
The memory attribute
The value has a version in bits 56 to 63 (the writer writes 2, line 1107) and the effects
in the bits below. Each of the locations has 2 bits: 0 no access, 1 read, 2 write, 3 read
and write (ModRef.h lines 28–35). The locations, in the order of
IRMemLocation (lines 60–72):
| Bits | Location |
|---|---|
| 0–1 | argument memory |
| 2–3 | inaccessible memory |
| 4–5 | errno memory |
| 6–7 | other memory |
| 8–9 | target memory 0 |
| 10–11 | target memory 1 |
Version 0 had only three locations: argument memory in bits 0–1, inaccessible memory in bits 2–3 and everything else in bits 4–5; the reader maps the last to both errno and other memory. When the target triple is AArch64, the reader also copies the inaccessible-memory access to the two target locations, for versions 0 and 1 (lines 2508–2545).
Constant ranges
A range is two bounds. With a bit width of 64 or less they are two operands, each a
sign-rotated number (the value shifted left one bit, with the low bit set for a negative
value; the number 1 stands for the minimum integer). With a width over 64 the encoding
is: one operand holding the number of 64-bit words of the lower bound in its low 32 bits
and of the upper bound in its high 32 bits, then the lower words, then the upper words,
each word sign-rotated (readConstantRange, lines 872–895; the writer is
emitConstantRange, lines 1067–1081). Form 7 puts the bit width before the
range; form 8 gives the count and the bit width once, then the ranges without widths.
Attribute kinds
The kind number is the code in LLVMBitCodes.h (code 0 is unused). The name
is the textual name from Attributes.td. The class says which forms can carry
it: E enum (form 0), I integer (form 1),
T type (forms 5 and 6), R constant range (form 7),
L range list (form 8). Kinds 11, 45, 49 and 50 are not defined in
current LLVM; the reader accepts them only as described under the table.
| Kind | Name | Class | Kind | Name | Class |
|---|---|---|---|---|---|
| 1 | align | I | 57 | optforfuzzing | E |
| 2 | alwaysinline | E | 58 | shadowcallstack | E |
| 3 | byval | T | 59 | speculative_load_hardening | E |
| 4 | inlinehint | E | 60 | immarg | E |
| 5 | inreg | E | 61 | willreturn | E |
| 6 | minsize | E | 62 | nofree | E |
| 7 | naked | E | 63 | nosync | E |
| 8 | nest | E | 64 | sanitize_memtag | E |
| 9 | noalias | E | 65 | preallocated | T |
| 10 | nobuiltin | E | 66 | nomerge | E |
| 11 | old nocapture | — | 67 | null_pointer_is_valid | E |
| 12 | noduplicate | E | 68 | noundef | E |
| 13 | noimplicitfloat | E | 69 | byref | T |
| 14 | noinline | E | 70 | mustprogress | E |
| 15 | nonlazybind | E | 71 | nocallback | E |
| 16 | noredzone | E | 72 | hot | E |
| 17 | noreturn | E | 73 | noprofile | E |
| 18 | nounwind | E | 74 | vscale_range | I |
| 19 | optsize | E | 75 | swiftasync | E |
| 20 | readnone | E | 76 | nosanitize_coverage | E |
| 21 | readonly | E | 77 | elementtype | T |
| 22 | returned | E | 78 | disable_sanitizer_instrumentation | E |
| 23 | returns_twice | E | 79 | nosanitize_bounds | E |
| 24 | signext | E | 80 | allocalign | E |
| 25 | alignstack | I | 81 | allocptr | E |
| 26 | ssp | E | 82 | allockind | I |
| 27 | sspreq | E | 83 | presplitcoroutine | E |
| 28 | sspstrong | E | 84 | fn_ret_thunk_extern | E |
| 29 | sret | T | 85 | skipprofile | E |
| 30 | sanitize_address | E | 86 | memory | I |
| 31 | sanitize_thread | E | 87 | nofpclass | I |
| 32 | sanitize_memory | E | 88 | optdebug | E |
| 33 | uwtable | I (E accepted) | 89 | writable | E |
| 34 | zeroext | E | 90 | coro_only_destroy_when_complete | E |
| 35 | builtin | E | 91 | dead_on_unwind | E |
| 36 | cold | E | 92 | range | R |
| 37 | optnone | E | 93 | sanitize_numerical_stability | E |
| 38 | inalloca | T | 94 | initializes | L |
| 39 | nonnull | E | 95 | hybrid_patchable | E |
| 40 | jumptable | E | 96 | sanitize_realtime | E |
| 41 | dereferenceable | I | 97 | sanitize_realtime_blocking | E |
| 42 | dereferenceable_or_null | I | 98 | coro_elide_safe | E |
| 43 | convergent | E | 99 | noext | E |
| 44 | safestack | E | 100 | nodivergencesource | E |
| 45 | old argmemonly | — | 101 | sanitize_type | E |
| 46 | swiftself | E | 102 | captures | I |
| 47 | swifterror | E | 103 | dead_on_return | I (E accepted) |
| 48 | norecurse | E | 104 | sanitize_alloc_token | E |
| 49 | old inaccessiblememonly | — | 105 | nocreateundeforpoison | E |
| 50 | old inaccessiblemem_or_argmemonly | — | 106 | denormal_fpenv | I |
| 51 | allocsize | I | 107 | nooutline | E |
| 52 | writeonly | E | 108 | flatten | E |
| 53 | speculatable | E | 109 | noipa | E |
| 54 | strictfp | E | 110 | nofreeobj | E |
| 55 | sanitize_hwaddress | E | 111 | sanitize_concurrency | E |
| 56 | nocf_check | E | 112 | likely_module_local | E |
Old kinds and upgrades (reader lines 2381–2404 and 2441–2480):
- In a group for the function position, an enum attribute of kind 20
(
readnone), 21 (readonly), 52 (writeonly), 45, 49 or 50 is turned into amemoryattribute (memory(none),memory(read),memory(write),memory(argmem: readwrite),memory(inaccessiblemem: readwrite), and argument or inaccessible memory read and write). Several are combined into one. Kinds 45, 49 and 50 outside the function position giveUnknown attribute kind. - An enum attribute of kind 11 (
nocapture) becomescaptures(none)wherever it is. - Kinds 3 (
byval), 29 (sret) and 38 (inalloca) in form 0 become typeless attributes; when the group is put on a function the reader adds the type from the parameter’s pointee type (old typed-pointer files), andMissing param element type for attribute upgradeis the error if it has none.
ENTRY_OLD bit masks
In an ENTRY_OLD record each attribute set is one 64-bit mask
(decodeLLVMAttributesForBitcode, lines 2022–2076, with the bit table in
getRawAttributeMask, lines 1924–2000). The reader first takes the alignment
from bits 16 to 31; it then rebuilds the attribute mask from bits 0 to 15 and from bits
32 to 51 (moved down by 11), so bits 52 and up are not read.
| Bit | Attribute | Bit | Attribute |
|---|---|---|---|
| 0 | zeroext | 32 | nocapture (becomes captures(none)) |
| 1 | signext | 33 | noredzone |
| 2 | noreturn | 34 | noimplicitfloat |
| 3 | inreg | 35 | naked |
| 4 | sret | 36 | inlinehint |
| 5 | nounwind | 37–39 | alignstack: log₂ of the alignment plus 1 |
| 6 | noalias | 40 | returns_twice |
| 7 | byval | 41 | uwtable |
| 8 | nest | 42 | nonlazybind |
| 9 | readnone | 43 | sanitize_address |
| 10 | readonly | 44 | minsize |
| 11 | noinline | 45 | noduplicate |
| 12 | alwaysinline | 46 | sspstrong |
| 13 | optsize | 47 | sanitize_thread |
| 14 | ssp | 48 | sanitize_memory |
| 15 | sspreq | 49 | nobuiltin |
| 16–31 | align: the alignment in bytes | 50 | returned |
| 51 | cold |
For the function position, bits 9 and 10 become memory(none) and
memory(read). A byval, sret or inalloca bit gives a typeless attribute that is
completed as for form 0.
Sources
- llvm/lib/Bitcode/Reader/BitcodeReader.cpp – lines 872–903 (constant ranges), 1924–2138, 2141–2404, 2406–2638
- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp – lines 1056–1172
- llvm/include/llvm/Bitcode/LLVMBitCodes.h –
AttributeKindCodes(726–840) andAttributeCodes(130–137) - llvm/include/llvm/IR/Attributes.td – names and classes
- llvm/include/llvm/IR/Attributes.h, llvm/lib/IR/Attributes.cpp, llvm/include/llvm/Support/ModRef.h, llvm/include/llvm/Support/CodeGen.h – the packed value layouts