Debian Codegraph @

docs/drivers/amd/function-calls.rst

raw
1 :orphan:
2
3 .. _aco-fn-calls:
4
5 Function call support in RADV/ACO
6 =================================
7
8 ACO supports function calls inside shaders - given a function signature and ABI, shaders can call
9 an arbitrary function, even via only a function pointer (i.e. with an unknown function definition).
10
11 This function call support is useful for implementing ray tracing pipelines (by representing individual RT shaders
12 as callable functions), but it also has potential use cases in GPGPU/Compute workloads.
13
14 This page serves to document the concepts involved in implementing function calls as well as an overview of the
15 implementation components.
16
17 Function call representation
18 ----------------------------
19
20 In NIR, function calls are represented by a `nir_call_instr`. The instruction takes a `nir_function` representing
21 the function being called, as well as SSA defs for each call parameter.
22 NIR can also represent "indirect calls", i.e. calls where the function being called is
23 unknown - instead, the instruction takes an SSA def containing a function pointer to the callee. In this case, the
24 `nir_function` only serves to provide information about the function signature, i.e. how many and which parameters
25 the function takes.
26
27 Call instructions do not have return values - instead, return values are represented by so-called "return parameters".
28 Instead of an SSA value, these parameters are derefs, and the return value is written into the deref when the callee
29 returns. Return parameters can double as input parameters, too - the callee can read the previous value of the deref
30 before (potentially) overwriting it with a new value.
31
32 ACO's representation of function calls follows this very closely. Calls are described by the `p_call` pseudo-instruction.
33 The operands to this instruction are a function pointer (i.e. the address of the callee), followed by the call
34 parameters. Return parameters are handled differently, though: While the initial value of the return parameter is passed
35 as an operand, the call instruction produces new definitions that refer to the SSA values of the return parameters after
36 the function call returns. There is a special NIR intrinsic ``load_return_param_amd`` that can be used to access these
37 new definitions when lowering return parameter derefs to SSA form.
38
39 .. _div-calls:
40
41 Divergent calls
42 ---------------
43
44 On CPUs, a call instruction will only ever jump to a single address. However, GPUs are SIMT, and the value of a function
45 pointer may be divergent, i.e. different threads try calling different functions within the same call instruction. AMD
46 hardware executes one instruction for all threads in lockstep, so the multiple callees have to be executed one after
47 the other.
48
49 This is handled by RADV in ``radv_nir_lower_call_abi``. In addition to the (non-divergent) function pointer to jump to,
50 ``radv_nir_lower_call_abi`` prepends another parameter representing the (potentially divergent) function pointer for all
51 lanes. For callable functions, ``radv_nir_lower_call_abi`` wraps the function body in a condition that verifies that the
52 current thread's (divergent) pointer matches the (non-divergent) pointer that is currently being executed. This serves
53 to "mask off" all threads that wanted to jump to a different function than what is currently executing. At the very end,
54 ``radv_nir_lower_call_abi`` inserts some code deciding whether to jump to the next callee or to return.
55
56 .. _stack:
57
58 Stack
59 -----
60
61 Supporting arbitrary function calls also means supporting recursion, and recursive functions need a stack.
62 AMD hardware provides instructions for accessing a per-thread scratch memory area in VRAM, and ACO uses this per-thread
63 scratch memory to set up its stack.
64
65 The stack frame for a function consists of all scratch memory allocated for this function in NIR, as well as space to
66 spill VGPRs if that is required. ACO adds a stack pointer as a parameter to every function - this stack pointer is added
67 to the offset inside the scratch space for all scratch loads/stores to make sure they don't overwrite stack frames of
68 caller functions.
69
70 ACO's call instructions take two stack-related operands: The current (caller) stack pointer and the caller's stack size.
71 When converting the call instruction to hardware instructions, ACO will add the caller stack size to the stack pointer
72 for the duration of the call (and subtract it again afterwards). This allows us to re-use the same stack pointer after
73 the call.
74
75 Implicit/System Parameters
76 --------------------------
77
78 In addition to parameters defined by the function signature, both RADV and ACO will insert additional parameters while
79 lowering calls. This is an overview of which lowering passes add which parameters.
80
81 Parameters added by ``radv_nir_lower_call_abi`` (see :ref:`Divergent calls <div-calls>`):
82 - "Uniform"/Non-divergent callee pointer
83 - Divergent function pointer
84
85 Parameters added by ACO: (see :ref:`Stack <stack>`)
86 - Stack pointer (uniform)
87
88 ABI Definition
89 --------------
90
91 The ABI (Application Binary Interface) defines specifics about the interaction between the function caller and the
92 callee (e.g. assignment of registers to parameters or register preservation). In ACO, the primary purpose of the ABI is
93 to define which register ranges are "preserved" (i.e. never overwritten by the callee) or "clobbered" (i.e. potentially
94 overwritten by the callee).
95
96 The caller can use preserved register ranges to store temporaries that are live across a call, and the callee can use
97 clobbered register ranges to store its own temporaries. If the callee wants to use registers from a preserved range,
98 then it needs to back up the value contained in the preserved register beforehand, and restore it when it's done using
99 the preserved register. Similarly, if there are not enough preserved registers for the caller to store all its
100 temporaries, the caller will need to spill excess temporaries to the stack.
101
102 ACO has to cater to different needs when defining ABIs: On one side, ray tracing traversal shaders demand to free up
103 the entire register file for the callee (Ray traversal is a really hot loop, so we don't want to spill anything at all).
104 Besides some parameters like the invocation ID, these shaders should be able to overwrite almost anything. On the other
105 side, RT traversal shaders should not be required to free up the register file when calling any-hit/intersection shaders
106 as this would also cause spilling during traversal. GPGPU compute workloads could fall anywhere between these extremes,
107 so a middle-ground solution is desirable for these.
108
109 ACO's way of defining an ABI divides the register file into "blocks" (``struct aco::ABI::RegisterBlock``). Each block
110 consists of a fixed number of preserved and clobbered registers, and a boolean determining whether the preserved or
111 clobbered registers come first in the block. Preserved and clobbered register ranges are defined by
112 repeating these blocks for as long as there are unassigned registers.
113
114 Some examples of preserved/clobbered register ranges using this approach::
115
116 For all examples, there are 108 SGPRs and 128 VGPRs to assign.
117
118 RegisterBlock:
119 clobbered_size: {16 sgpr, 16 vgpr}
120 preserved_size: {16 sgpr, 16 vgpr}
121 clobbered_first: false
122 results in:
123 v0-v15: preserved
124 v16-v31: clobbered
125 v32-v47: preserved
126 v48-v63: clobbered
127 v64-v79: preserved
128 v80-v95: clobbered
129 v96-v111: preserved
130 v112-v127: clobbered
131
132 s0-s15: preserved
133 s16-s31: clobbered
134 s32-s47: preserved
135 s48-s63: clobbered
136 s64-s79: preserved
137 s80-s95: clobbered
138 s96-s108: preserved
139
140 RegisterBlock:
141 clobbered_size: {128 sgpr, 256 vgpr}
142 preserved_size: {80 sgpr, 80 vgpr}
143 clobbered_first: false
144 results in:
145 v0-v79: preserved
146 v80-v127: clobbered
147
148 s0-s79: preserved
149 s80-s108: clobbered
150
151 An alternating preserved-clobbered-preserved pattern is useful for generic compute workloads, because the ratio of
152 preserved to clobbered registers is roughly the same, no matter how many registers are used by the shaders.
153
154 The latter example where the lower part of the register file is preserved and only some registers high up in the
155 register file are clobbered is suitable for any-hit/intersection shaders - traversal shader temporaries can live in the
156 preserved part low in the register file.
157
158 This block assignment is optional - if no ``RegisterBlock`` is given, the ABI defines the entire register range as
159 clobbered-by-default, although parameters that are not marked as clobbered via ``ACO_NIR_PARAM_ATTRIB_DISCARDABLE``
160 will continue being preserved.
161
162 Parameter Register Assignment
163 -----------------------------
164
165 If a ``RegisterBlock`` defines preserved and clobbered ranges, then parameters are assigned registers from either range
166 depending on ``ACO_NIR_PARAM_ATTRIB_DISCARDABLE`` - if parameters are marked as clobbered with this attribute, then they
167 are assigned a register in a clobbered range, otherwise they are assigned in a register in a preserved range. The order
168 of the parameters in the register file is not necessarily the same order as in the function signature - they may get
169 reordered if it's beneficial to fill gaps or for alignment.
170
171 If there is no ``RegisterBlock``, then registers will be assigned based on alignment only.
172
173 If there is no more space for a parameter in any of its corresponding register ranges, it will be moved to the stack.