xref: /aosp_15_r20/external/mesa3d/docs/nir/alu.rst (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard WorkerNIR ALU Instructions
2*61046927SAndroid Build Coastguard Worker====================
3*61046927SAndroid Build Coastguard Worker
4*61046927SAndroid Build Coastguard WorkerALU instructions represent simple operations, such as addition, multiplication,
5*61046927SAndroid Build Coastguard Workercomparison, etc., that take a certain number of arguments and return a result
6*61046927SAndroid Build Coastguard Workerthat only depends on the arguments.  ALU instructions in NIR must be pure in
7*61046927SAndroid Build Coastguard Workerthe sense that they have no side effect and that identical inputs yields an
8*61046927SAndroid Build Coastguard Workeridentical output.  A good rule of thumb is that only things which can be
9*61046927SAndroid Build Coastguard Workerconstant folded should be ALU operations.  If it can't be constant folded, then
10*61046927SAndroid Build Coastguard Workerit should probably be an intrinsic instead.
11*61046927SAndroid Build Coastguard Worker
12*61046927SAndroid Build Coastguard WorkerEach ALU instruction has an opcode, which is a member of the :c:enum:`nir_op`
13*61046927SAndroid Build Coastguard Workerenum, that describes what it does as well as how many arguments it takes.
14*61046927SAndroid Build Coastguard WorkerAssociated with each opcode is an metadata structure,
15*61046927SAndroid Build Coastguard Worker:c:struct:`nir_op_info`, which shows how many arguments the opcode takes,
16*61046927SAndroid Build Coastguard Workerinformation about data types, and algebraic properties such as associativity
17*61046927SAndroid Build Coastguard Workerand commutativity. The info structure for each opcode may be accessed through
18*61046927SAndroid Build Coastguard Workera global :c:var:`nir_op_infos` array that’s indexed by the opcode.
19*61046927SAndroid Build Coastguard Worker
20*61046927SAndroid Build Coastguard WorkerALU operations are typeless, meaning that they're only defined to convert
21*61046927SAndroid Build Coastguard Workera certain bit-pattern input to another bit-pattern output.  The only concrete
22*61046927SAndroid Build Coastguard Workernotion of types for a NIR SSA value or register is that each value has a number
23*61046927SAndroid Build Coastguard Workerof vector components and a bit-size.  How that data is interpreted is entirely
24*61046927SAndroid Build Coastguard Workercontrolled by the opcode.  NIR doesn't have opcodes for ``intBitsToFloat()``
25*61046927SAndroid Build Coastguard Workerand friends because they are implicit.
26*61046927SAndroid Build Coastguard Worker
27*61046927SAndroid Build Coastguard WorkerEven though ALU operations are typeless, each opcode also has an "ALU type"
28*61046927SAndroid Build Coastguard Workermetadata for each of the sources and the destination which can be
29*61046927SAndroid Build Coastguard Workerfloating-point, boolean, integer, or unsigned integer.  The ALU type mainly
30*61046927SAndroid Build Coastguard Workerhelps back-ends which want to handle all conversion instructions, for instance,
31*61046927SAndroid Build Coastguard Workerin a single switch case.  They're also important when a back-end requests the
32*61046927SAndroid Build Coastguard Workerabsolute value, negate, and saturate modifiers (not used by core NIR).  In that
33*61046927SAndroid Build Coastguard Workercase, modifiers are interpreted with respect to the ALU type on the source or
34*61046927SAndroid Build Coastguard Workerdestination of the instruction.  In addition, if an operation takes a boolean
35*61046927SAndroid Build Coastguard Workerargument, then the argument may be assumed to be either ``0`` for false or
36*61046927SAndroid Build Coastguard Worker``~0`` (a.k.a ``-1``) for true even if it is not a 1-bit value.  If an
37*61046927SAndroid Build Coastguard Workeroperation’s result has a boolean type, then it may only produce only ``0`` or ``~0``.
38*61046927SAndroid Build Coastguard Worker
39*61046927SAndroid Build Coastguard WorkerMost of the common ALU ops in NIR operate per-component, meaning that the
40*61046927SAndroid Build Coastguard Workeroperation is defined by what it does on a single scalar value and, when
41*61046927SAndroid Build Coastguard Workerperformed on vectors, it performs the same operation on each component.  Things
42*61046927SAndroid Build Coastguard Workerlike add, multiply, etc. fall into this category.  Per-component operations
43*61046927SAndroid Build Coastguard Workernaturally scale to as many components as necessary.  Non-per-component ALU ops
44*61046927SAndroid Build Coastguard Workerare things like :nir:alu-op:`vec4` or :nir:alu-op:`pack_64_2x32` where any
45*61046927SAndroid Build Coastguard Workergiven component in the result value may be a combination of any component in
46*61046927SAndroid Build Coastguard Workerany source.  These ops have a number of destination components and a number of
47*61046927SAndroid Build Coastguard Workercomponents required by each source which is fixed by the opcode.
48*61046927SAndroid Build Coastguard Worker
49*61046927SAndroid Build Coastguard WorkerWhile most instruction types in NIR require vector sizes to perfectly match on
50*61046927SAndroid Build Coastguard Workerinputs and outputs, ALU instruction sources have an additional
51*61046927SAndroid Build Coastguard Worker:c:member:`nir_alu_src.swizzle` field which allows them to act on vectors
52*61046927SAndroid Build Coastguard Workerwhich are not the native vector size of the instruction.  This is ideal for
53*61046927SAndroid Build Coastguard Workerhardware with a native data type of :nir:alu-op:`vec4` but also means that ALU
54*61046927SAndroid Build Coastguard Workerinstructions are often used (and required) for packing/unpacking vectors for
55*61046927SAndroid Build Coastguard Workeruse in other instruction types like intrinsics or texture ops.
56*61046927SAndroid Build Coastguard Worker
57*61046927SAndroid Build Coastguard Worker.. c:autostruct:: nir_op_info
58*61046927SAndroid Build Coastguard Worker   :file: src/compiler/nir/nir.h
59*61046927SAndroid Build Coastguard Worker   :members:
60*61046927SAndroid Build Coastguard Worker
61*61046927SAndroid Build Coastguard Worker.. c:autovar:: nir_op_infos
62*61046927SAndroid Build Coastguard Worker
63*61046927SAndroid Build Coastguard Worker.. c:autostruct:: nir_alu_instr
64*61046927SAndroid Build Coastguard Worker   :members:
65*61046927SAndroid Build Coastguard Worker
66*61046927SAndroid Build Coastguard Worker.. c:autostruct:: nir_alu_src
67*61046927SAndroid Build Coastguard Worker   :members:
68*61046927SAndroid Build Coastguard Worker
69*61046927SAndroid Build Coastguard WorkerNIR ALU Opcode Reference:
70*61046927SAndroid Build Coastguard Worker-------------------------
71*61046927SAndroid Build Coastguard Worker
72*61046927SAndroid Build Coastguard Worker.. nir:alu-opcodes::
73