xref: /aosp_15_r20/external/mesa3d/src/amd/compiler/README.md (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker# Welcome to ACO
2*61046927SAndroid Build Coastguard Worker
3*61046927SAndroid Build Coastguard WorkerACO (short for *AMD compiler*) is a back-end compiler for AMD GCN / RDNA GPUs, based on the NIR compiler infrastructure.
4*61046927SAndroid Build Coastguard WorkerSimply put, ACO translates shader programs from the NIR intermediate representation into a GCN / RDNA binary which the GPU can execute.
5*61046927SAndroid Build Coastguard Worker
6*61046927SAndroid Build Coastguard Worker## Motivation
7*61046927SAndroid Build Coastguard Worker
8*61046927SAndroid Build Coastguard WorkerWhy did we choose to develop a new compiler backend?
9*61046927SAndroid Build Coastguard Worker
10*61046927SAndroid Build Coastguard Worker1. We'd like to give gamers a fluid, stutter-free experience, so we prioritize compilation speed.
11*61046927SAndroid Build Coastguard Worker2. Good divergence analysis allows us to better optimize runtime performance.
12*61046927SAndroid Build Coastguard Worker3. Issues can be fixed within mesa releases, independently of the schedule of other projects.
13*61046927SAndroid Build Coastguard Worker
14*61046927SAndroid Build Coastguard Worker## Control flow
15*61046927SAndroid Build Coastguard Worker
16*61046927SAndroid Build Coastguard WorkerModern GPUs are SIMD machines that execute the shader in parallel.
17*61046927SAndroid Build Coastguard WorkerIn case of GCN / RDNA the parallelism is achieved by executing the shader on several waves, and each wave has several lanes (32 or 64).
18*61046927SAndroid Build Coastguard WorkerWhen every lane executes exactly the same instructions, and takes the same path, it's uniform control flow;
19*61046927SAndroid Build Coastguard Workerotherwise when some lanes take one path while other lanes take a different path, it's divergent.
20*61046927SAndroid Build Coastguard Worker
21*61046927SAndroid Build Coastguard WorkerEach hardware lane corresponds to a shader invocation from a software perspective.
22*61046927SAndroid Build Coastguard Worker
23*61046927SAndroid Build Coastguard WorkerThe hardware doesn't directly support divergence,
24*61046927SAndroid Build Coastguard Workerso in case of divergent control flow, the GPU must execute both code paths, each with some lanes disabled.
25*61046927SAndroid Build Coastguard WorkerThis is why divergence is a performance concern in shader programming.
26*61046927SAndroid Build Coastguard Worker
27*61046927SAndroid Build Coastguard WorkerACO deals with divergent control flow by maintaining two control flow graphs (CFG):
28*61046927SAndroid Build Coastguard Worker
29*61046927SAndroid Build Coastguard Worker* logical CFG - directly translated from NIR and shows the intended control flow of the program.
30*61046927SAndroid Build Coastguard Worker* linear CFG - created according to Whole-Function Vectorization by Ralf Karrenberg and Sebastian Hack.
31*61046927SAndroid Build Coastguard Worker  The linear CFG represents how the program is physically executed on GPU and may contain additional blocks for control flow handling and to avoid critical edges.
32*61046927SAndroid Build Coastguard Worker  Note that all nodes of the logical CFG also participate in the linear CFG, but not vice versa.
33*61046927SAndroid Build Coastguard Worker
34*61046927SAndroid Build Coastguard Worker## Compilation phases
35*61046927SAndroid Build Coastguard Worker
36*61046927SAndroid Build Coastguard Worker#### Instruction Selection
37*61046927SAndroid Build Coastguard Worker
38*61046927SAndroid Build Coastguard WorkerThe instruction selection is based around the divergence analysis and works in 3 passes on the NIR shader.
39*61046927SAndroid Build Coastguard Worker
40*61046927SAndroid Build Coastguard Worker1. The divergence analysis pass calculates for each SSA definition if its value is guaranteed to be uniform across all threads of the workgroup.
41*61046927SAndroid Build Coastguard Worker2. We determine the register class for each SSA definition.
42*61046927SAndroid Build Coastguard Worker3. Actual instruction selection. The advanced divergence analysis allows for better usage of the scalar unit, scalar memory loads and the scalar register file.
43*61046927SAndroid Build Coastguard Worker
44*61046927SAndroid Build Coastguard WorkerWe have two types of instructions:
45*61046927SAndroid Build Coastguard Worker
46*61046927SAndroid Build Coastguard Worker* Hardware instructions as specified by the GCN / RDNA instruction set architecture manuals.
47*61046927SAndroid Build Coastguard Worker* Pseudo instructions which are helpers that encapsulate more complex functionality.
48*61046927SAndroid Build Coastguard Worker  They eventually get lowered to real hardware instructions.
49*61046927SAndroid Build Coastguard Worker
50*61046927SAndroid Build Coastguard WorkerEach instruction can have operands (temporaries that it reads), and definitions (temporaries that it writes).
51*61046927SAndroid Build Coastguard WorkerTemporaries can be fixed to a specific register, or just specify a register class (either a single register, or a vector of several registers).
52*61046927SAndroid Build Coastguard Worker
53*61046927SAndroid Build Coastguard Worker#### Value Numbering
54*61046927SAndroid Build Coastguard Worker
55*61046927SAndroid Build Coastguard WorkerThe value numbering pass is necessary for two reasons: the lack of descriptor load representation in NIR,
56*61046927SAndroid Build Coastguard Workerand every NIR instruction that gets emitted as multiple ACO instructions also has potential for CSE.
57*61046927SAndroid Build Coastguard WorkerThis pass does dominator-tree value numbering.
58*61046927SAndroid Build Coastguard Worker
59*61046927SAndroid Build Coastguard Worker#### Optimization
60*61046927SAndroid Build Coastguard Worker
61*61046927SAndroid Build Coastguard WorkerIn this phase, simpler instructions are combined into more complex instructions (like the different versions of multiply-add as well as neg, abs, clamp, and output modifiers) and constants are inlined, moves are eliminated, etc.
62*61046927SAndroid Build Coastguard WorkerExactly which optimizations are performed depends on the hardware for which the shader is being compiled.
63*61046927SAndroid Build Coastguard Worker
64*61046927SAndroid Build Coastguard Worker#### Setup of reduction temporaries
65*61046927SAndroid Build Coastguard Worker
66*61046927SAndroid Build Coastguard WorkerThis pass is responsible for making sure that register allocation is correct for reductions, by adding pseudo instructions that utilize linear VGPRs.
67*61046927SAndroid Build Coastguard WorkerWhen a temporary has a linear VGPR register class, this means that the variable is considered *live* in the linear control flow graph.
68*61046927SAndroid Build Coastguard Worker
69*61046927SAndroid Build Coastguard Worker#### Insert exec mask
70*61046927SAndroid Build Coastguard Worker
71*61046927SAndroid Build Coastguard WorkerIn the GCN/RDNA architecture, there is a special register called `exec` which is used for manually controlling which VALU threads (aka. *lanes*) are active. The value of `exec` has to change in divergent branches, loops, etc. and it needs to be restored after the branch or loop is complete. This pass ensures that the correct lanes are active in every branch.
72*61046927SAndroid Build Coastguard Worker
73*61046927SAndroid Build Coastguard Worker#### Live-Variable Analysis
74*61046927SAndroid Build Coastguard Worker
75*61046927SAndroid Build Coastguard WorkerA live-variable analysis is used to calculate the register need of the shader.
76*61046927SAndroid Build Coastguard WorkerThis information is used for spilling and scheduling before register allocation.
77*61046927SAndroid Build Coastguard Worker
78*61046927SAndroid Build Coastguard Worker#### Spilling
79*61046927SAndroid Build Coastguard Worker
80*61046927SAndroid Build Coastguard WorkerFirst, we lower the shader program to CSSA form.
81*61046927SAndroid Build Coastguard WorkerThen, if the register demand exceeds the global limit, this pass lowers register usage by temporarily storing excess scalar values in free vector registers, or excess vector values in scratch memory, and reloading them when needed. It is based on the paper "Register Spilling and Live-Range Splitting for SSA-Form Programs".
82*61046927SAndroid Build Coastguard Worker
83*61046927SAndroid Build Coastguard Worker#### Instruction Scheduling
84*61046927SAndroid Build Coastguard Worker
85*61046927SAndroid Build Coastguard WorkerScheduling is another NP-complete problem where basically all known heuristics suffer from unpredictable change in register pressure. For that reason, the implemented scheduler does not completely re-schedule all instructions, but only aims to move up memory loads as far as possible without exceeding the maximum register limit for the pre-calculated wave count. The reason this works is that ILP is very limited on GCN. This approach looks promising so far.
86*61046927SAndroid Build Coastguard Worker
87*61046927SAndroid Build Coastguard Worker#### Register Allocation
88*61046927SAndroid Build Coastguard Worker
89*61046927SAndroid Build Coastguard WorkerThe register allocator works on SSA (as opposed to LLVM's which works on virtual registers). The SSA properties guarantee that there are always as many registers available as needed. The problem is that some instructions require a vector of neighboring registers to be available, but the free regs might be scattered. In this case, the register allocator inserts shuffle code (moving some temporaries to other registers) to make space for the variable. The assumption is that it is (almost) always better to have a few more moves than to sacrifice a wave. The RA does SSA-reconstruction on the fly, which makes its runtime linear.
90*61046927SAndroid Build Coastguard Worker
91*61046927SAndroid Build Coastguard Worker#### SSA Elimination
92*61046927SAndroid Build Coastguard Worker
93*61046927SAndroid Build Coastguard WorkerThe next step is a pass out of SSA by inserting parallelcopies at the end of blocks to match the phi nodes' semantics.
94*61046927SAndroid Build Coastguard Worker
95*61046927SAndroid Build Coastguard Worker#### Lower to HW instructions
96*61046927SAndroid Build Coastguard Worker
97*61046927SAndroid Build Coastguard WorkerMost pseudo instructions are lowered to actual machine instructions.
98*61046927SAndroid Build Coastguard WorkerThese are mostly parallel copy instructions created by instruction selection or register allocation and spill/reload code.
99*61046927SAndroid Build Coastguard Worker
100*61046927SAndroid Build Coastguard Worker#### ILP Scheduling
101*61046927SAndroid Build Coastguard Worker
102*61046927SAndroid Build Coastguard WorkerThis second scheduler works on registers rather than SSA-values to determine dependencies. It implements a forward list scheduling algorithm using a partial dependency graph of few instructions at a time and aims to create larger memory clauses and improve ILP.
103*61046927SAndroid Build Coastguard Worker
104*61046927SAndroid Build Coastguard Worker#### Insert wait states
105*61046927SAndroid Build Coastguard Worker
106*61046927SAndroid Build Coastguard WorkerGCN requires some wait states to be manually inserted in order to ensure correct behavior on memory instructions and some register dependencies.
107*61046927SAndroid Build Coastguard WorkerThis means that we need to insert `s_waitcnt` instructions (and its variants) so that the shader program waits until the eg. a memory operation is complete.
108*61046927SAndroid Build Coastguard Worker
109*61046927SAndroid Build Coastguard Worker#### Resolve hazards and insert NOPs
110*61046927SAndroid Build Coastguard Worker
111*61046927SAndroid Build Coastguard WorkerSome instructions require wait states or other instructions to resolve hazards which are not handled by the hardware.
112*61046927SAndroid Build Coastguard WorkerThis pass makes sure that no known hazards occur.
113*61046927SAndroid Build Coastguard Worker
114*61046927SAndroid Build Coastguard Worker#### Emit program - Assembler
115*61046927SAndroid Build Coastguard Worker
116*61046927SAndroid Build Coastguard WorkerThe assembler emits the actual binary that will be sent to the hardware for execution. ACO's assembler is straight-forward because all instructions have their format, opcode, registers and potential fields already available, so it only needs to cater to the some differences between each hardware generation.
117*61046927SAndroid Build Coastguard Worker
118*61046927SAndroid Build Coastguard Worker## Supported shader stages
119*61046927SAndroid Build Coastguard Worker
120*61046927SAndroid Build Coastguard WorkerHardware stages (as executed on the chip) don't exactly match software stages (as defined in OpenGL / Vulkan).
121*61046927SAndroid Build Coastguard WorkerWhich software stage gets executed on which hardware stage depends on what kind of software stages are present in the current pipeline.
122*61046927SAndroid Build Coastguard Worker
123*61046927SAndroid Build Coastguard WorkerAn important difference is that VS is always the first stage to run in SW models,
124*61046927SAndroid Build Coastguard Workerwhereas HW VS refers to the last HW stage before fragment shading in GCN/RDNA terminology.
125*61046927SAndroid Build Coastguard WorkerThat's why, among other things, the HW VS is no longer used to execute the SW VS when tessellation or geometry shading are used.
126*61046927SAndroid Build Coastguard Worker
127*61046927SAndroid Build Coastguard Worker#### Glossary of software stages
128*61046927SAndroid Build Coastguard Worker
129*61046927SAndroid Build Coastguard Worker* VS = Vertex Shader
130*61046927SAndroid Build Coastguard Worker* TCS = Tessellation Control Shader, equivalent to D3D HS = Hull Shader
131*61046927SAndroid Build Coastguard Worker* TES = Tessellation Evaluation Shader, equivalent to D3D DS = Domain Shader
132*61046927SAndroid Build Coastguard Worker* GS = Geometry Shader
133*61046927SAndroid Build Coastguard Worker* FS = Fragment Shader, equivalent to D3D PS = Pixel Shader
134*61046927SAndroid Build Coastguard Worker* CS = Compute Shader
135*61046927SAndroid Build Coastguard Worker* TS = Task Shader
136*61046927SAndroid Build Coastguard Worker* MS = Mesh Shader
137*61046927SAndroid Build Coastguard Worker
138*61046927SAndroid Build Coastguard Worker#### Glossary of hardware stages
139*61046927SAndroid Build Coastguard Worker
140*61046927SAndroid Build Coastguard Worker* LS = Local Shader (merged into HS on GFX9+), only runs SW VS when tessellation is used
141*61046927SAndroid Build Coastguard Worker* HS = Hull Shader, the HW equivalent of a Tessellation Control Shader, runs before the fixed function hardware performs tessellation
142*61046927SAndroid Build Coastguard Worker* ES = Export Shader (merged into GS on GFX9+), if there is a GS in the SW pipeline, the preceding stage (ie. SW VS or SW TES) always has to run on this HW stage
143*61046927SAndroid Build Coastguard Worker* GS = Geometry Shader, also known as legacy GS
144*61046927SAndroid Build Coastguard Worker* VS = Vertex Shader, **not equivalent to SW VS**: when there is a GS in the SW pipeline this stage runs a "GS copy" shader, otherwise it always runs the SW stage before FS
145*61046927SAndroid Build Coastguard Worker* NGG = Next Generation Geometry, a new hardware stage that replaces legacy HW GS and HW VS on RDNA GPUs
146*61046927SAndroid Build Coastguard Worker* PS = Pixel Shader, the HW equivalent to SW FS
147*61046927SAndroid Build Coastguard Worker* CS = Compute Shader
148*61046927SAndroid Build Coastguard Worker
149*61046927SAndroid Build Coastguard Worker##### Notes about HW VS and the "GS copy" shader
150*61046927SAndroid Build Coastguard Worker
151*61046927SAndroid Build Coastguard WorkerHW PS reads its inputs from a special ring buffer called Parameter Cache (PC) that only HW VS can write to, using export instructions.
152*61046927SAndroid Build Coastguard WorkerHowever, legacy GS store their output in VRAM (before GFX10/NGG).
153*61046927SAndroid Build Coastguard WorkerSo in order for HW PS to be able to read the GS outputs, we must run something on the VS stage which reads the GS outputs
154*61046927SAndroid Build Coastguard Workerfrom VRAM and exports them to the PC. This is what we call a "GS copy" shader.
155*61046927SAndroid Build Coastguard WorkerFrom a HW perspective the "GS copy" shader is in fact VS (it runs on the HW VS stage),
156*61046927SAndroid Build Coastguard Workerbut from a SW perspective it's not part of the traditional pipeline,
157*61046927SAndroid Build Coastguard Workerit's just some "glue code" that we need for outputs to play nicely.
158*61046927SAndroid Build Coastguard Worker
159*61046927SAndroid Build Coastguard WorkerOn GFX10/NGG this limitation no longer exists, because NGG can export directly to the PC.
160*61046927SAndroid Build Coastguard Worker
161*61046927SAndroid Build Coastguard Worker##### Notes about merged shaders
162*61046927SAndroid Build Coastguard Worker
163*61046927SAndroid Build Coastguard WorkerThe merged stages on GFX9 (and GFX10/legacy) are: LSHS and ESGS. On GFX10/NGG the ESGS is merged with HW VS into NGG.
164*61046927SAndroid Build Coastguard Worker
165*61046927SAndroid Build Coastguard WorkerThis might be confusing due to a mismatch between the number of invocations of these shaders.
166*61046927SAndroid Build Coastguard WorkerFor example, ES is per-vertex, but GS is per-primitive.
167*61046927SAndroid Build Coastguard WorkerThis is why merged shaders get an argument called `merged_wave_info` which tells how many invocations each part needs,
168*61046927SAndroid Build Coastguard Workerand there is some code at the beginning of each part to ensure the correct number of invocations by disabling some threads.
169*61046927SAndroid Build Coastguard WorkerSo, think about these as two independent shader programs slapped together.
170*61046927SAndroid Build Coastguard Worker
171*61046927SAndroid Build Coastguard Worker### Which software stage runs on which hardware stage?
172*61046927SAndroid Build Coastguard Worker
173*61046927SAndroid Build Coastguard Worker#### Graphics Pipeline
174*61046927SAndroid Build Coastguard Worker
175*61046927SAndroid Build Coastguard Worker##### GFX6-8:
176*61046927SAndroid Build Coastguard Worker
177*61046927SAndroid Build Coastguard Worker* Each SW stage has its own HW stage
178*61046927SAndroid Build Coastguard Worker* LS and HS share the same LDS space, so LS can store its output to LDS, where HS can read it
179*61046927SAndroid Build Coastguard Worker* HS, ES, GS outputs are stored in VRAM, next stage reads these from VRAM
180*61046927SAndroid Build Coastguard Worker* GS outputs got to VRAM, so they have to be copied by a GS copy shader running on the HW VS stage
181*61046927SAndroid Build Coastguard Worker
182*61046927SAndroid Build Coastguard Worker| GFX6-8 HW stages:       | LS  | HS  | ES  | GS  | VS     | PS | ACO terminology |
183*61046927SAndroid Build Coastguard Worker| -----------------------:|:----|:----|:----|:----|:-------|:---|:----------------|
184*61046927SAndroid Build Coastguard Worker| SW stages: only VS+PS:  |     |     |     |     | VS     | FS | `vertex_vs`, `fragment_fs` |
185*61046927SAndroid Build Coastguard Worker|            with tess:   | VS  | TCS |     |     | TES    | FS | `vertex_ls`, `tess_control_hs`, `tess_eval_vs`, `fragment_fs` |
186*61046927SAndroid Build Coastguard Worker|            with GS:     |     |     | VS  | GS  | GS copy| FS | `vertex_es`, `geometry_gs`, `gs_copy_vs`, `fragment_fs` |
187*61046927SAndroid Build Coastguard Worker|            with both:   | VS  | TCS | TES | GS  | GS copy| FS | `vertex_ls`, `tess_control_hs`, `tess_eval_es`, `geometry_gs`, `gs_copy_vs`, `fragment_fs` |
188*61046927SAndroid Build Coastguard Worker
189*61046927SAndroid Build Coastguard Worker##### GFX9+ (including GFX10/legacy):
190*61046927SAndroid Build Coastguard Worker
191*61046927SAndroid Build Coastguard Worker* HW LS and HS stages are merged, and the merged shader still uses LDS in the same way as before
192*61046927SAndroid Build Coastguard Worker* HW ES and GS stages are merged, so ES outputs can go to LDS instead of VRAM
193*61046927SAndroid Build Coastguard Worker* LSHS outputs and ESGS outputs are still stored in VRAM, so a GS copy shader is still necessary
194*61046927SAndroid Build Coastguard Worker
195*61046927SAndroid Build Coastguard Worker| GFX9+ HW stages:        | LSHS      | ESGS      | VS     | PS | ACO terminology |
196*61046927SAndroid Build Coastguard Worker| -----------------------:|:----------|:----------|:-------|:---|:----------------|
197*61046927SAndroid Build Coastguard Worker| SW stages: only VS+PS:  |           |           | VS     | FS | `vertex_vs`, `fragment_fs` |
198*61046927SAndroid Build Coastguard Worker|            with tess:   | VS + TCS  |           | TES    | FS | `vertex_tess_control_hs`, `tess_eval_vs`, `fragment_fs` |
199*61046927SAndroid Build Coastguard Worker|            with GS:     |           | VS + GS   | GS copy| FS | `vertex_geometry_gs`, `gs_copy_vs`, `fragment_fs` |
200*61046927SAndroid Build Coastguard Worker|            with both:   | VS + TCS  | TES + GS  | GS copy| FS | `vertex_tess_control_hs`, `tess_eval_geometry_gs`, `gs_copy_vs`, `fragment_fs` |
201*61046927SAndroid Build Coastguard Worker
202*61046927SAndroid Build Coastguard Worker##### NGG (GFX10+ only):
203*61046927SAndroid Build Coastguard Worker
204*61046927SAndroid Build Coastguard Worker * HW GS and VS stages are now merged, and NGG can export directly to PC
205*61046927SAndroid Build Coastguard Worker * GS copy shaders are no longer needed
206*61046927SAndroid Build Coastguard Worker
207*61046927SAndroid Build Coastguard Worker| GFX10/NGG HW stages:    | LSHS      | NGG                | PS | ACO terminology |
208*61046927SAndroid Build Coastguard Worker| -----------------------:|:----------|:-------------------|:---|:----------------|
209*61046927SAndroid Build Coastguard Worker| SW stages: only VS+PS:  |           | VS                 | FS | `vertex_ngg`, `fragment_fs` |
210*61046927SAndroid Build Coastguard Worker|            with tess:   | VS + TCS  | TES                | FS | `vertex_tess_control_hs`, `tess_eval_ngg`, `fragment_fs` |
211*61046927SAndroid Build Coastguard Worker|            with GS:     |           | VS + GS            | FS | `vertex_geometry_ngg`, `fragment_fs` |
212*61046927SAndroid Build Coastguard Worker|            with both:   | VS + TCS  | TES + GS           | FS | `vertex_tess_control_hs`, `tess_eval_geometry_ngg`, `fragment_fs` |
213*61046927SAndroid Build Coastguard Worker
214*61046927SAndroid Build Coastguard Worker#### Mesh Shading Graphics Pipeline
215*61046927SAndroid Build Coastguard Worker
216*61046927SAndroid Build Coastguard WorkerGFX10.3+:
217*61046927SAndroid Build Coastguard Worker
218*61046927SAndroid Build Coastguard Worker* TS will run as a CS and stores its output payload to VRAM
219*61046927SAndroid Build Coastguard Worker* MS runs on NGG, loads its inputs from VRAM and stores outputs to LDS, then PC
220*61046927SAndroid Build Coastguard Worker* Pixel Shaders work the same way as before
221*61046927SAndroid Build Coastguard Worker
222*61046927SAndroid Build Coastguard Worker| GFX10.3+ HW stages      | CS    | NGG   | PS | ACO terminology |
223*61046927SAndroid Build Coastguard Worker| -----------------------:|:------|:------|:---|:----------------|
224*61046927SAndroid Build Coastguard Worker| SW stages: only MS+PS:  |       | MS    | FS | `mesh_ngg`, `fragment_fs` |
225*61046927SAndroid Build Coastguard Worker|            with task:   | TS    | MS    | FS | `task_cs`, `mesh_ngg`, `fragment_fs` |
226*61046927SAndroid Build Coastguard Worker
227*61046927SAndroid Build Coastguard Worker#### Compute pipeline
228*61046927SAndroid Build Coastguard Worker
229*61046927SAndroid Build Coastguard WorkerGFX6-10:
230*61046927SAndroid Build Coastguard Worker
231*61046927SAndroid Build Coastguard Worker* Note that the SW CS always runs on the HW CS stage on all HW generations.
232*61046927SAndroid Build Coastguard Worker
233*61046927SAndroid Build Coastguard Worker| GFX6-10 HW stage        | CS   | ACO terminology |
234*61046927SAndroid Build Coastguard Worker| -----------------------:|:-----|:----------------|
235*61046927SAndroid Build Coastguard Worker| SW stage                | CS   | `compute_cs`    |
236*61046927SAndroid Build Coastguard Worker
237*61046927SAndroid Build Coastguard Worker
238*61046927SAndroid Build Coastguard Worker## How to debug
239*61046927SAndroid Build Coastguard Worker
240*61046927SAndroid Build Coastguard WorkerHandy `RADV_DEBUG` options that help with ACO debugging:
241*61046927SAndroid Build Coastguard Worker
242*61046927SAndroid Build Coastguard Worker* `nocache` - you always want to use this when debugging, otherwise you risk using a broken shader from the cache.
243*61046927SAndroid Build Coastguard Worker* `shaders` - makes ACO print the IR after register allocation, as well as the disassembled shader binary.
244*61046927SAndroid Build Coastguard Worker* `metashaders` - does the same thing as `shaders` but for built-in RADV shaders.
245*61046927SAndroid Build Coastguard Worker* `preoptir` - makes ACO print the final NIR shader before instruction selection, as well as the ACO IR after instruction selection.
246*61046927SAndroid Build Coastguard Worker* `nongg` - disables NGG support
247*61046927SAndroid Build Coastguard Worker
248*61046927SAndroid Build Coastguard WorkerWe also have `ACO_DEBUG` options:
249*61046927SAndroid Build Coastguard Worker
250*61046927SAndroid Build Coastguard Worker* `validateir` - Validate the ACO IR between compilation stages. By default, enabled in debug builds and disabled in release builds.
251*61046927SAndroid Build Coastguard Worker* `validatera` - Perform a RA (register allocation) validation.
252*61046927SAndroid Build Coastguard Worker* `force-waitcnt` - Forces ACO to emit a wait state after each instruction when there is something to wait for. Harms performance.
253*61046927SAndroid Build Coastguard Worker* `novn` - Disables the ACO value numbering stage.
254*61046927SAndroid Build Coastguard Worker* `noopt` - Disables the ACO optimizer.
255*61046927SAndroid Build Coastguard Worker* `nosched` - Disables the ACO pre-RA and post-RA scheduler.
256*61046927SAndroid Build Coastguard Worker* `nosched-ilp` - Disables the ACO post-RA ILP scheduler.
257*61046927SAndroid Build Coastguard Worker
258*61046927SAndroid Build Coastguard WorkerNote that you need to **combine these options into a comma-separated list**, for example: `RADV_DEBUG=nocache,shaders` otherwise only the last one will take effect. (This is how all environment variables work, yet this is an often made mistake.) Example:
259*61046927SAndroid Build Coastguard Worker
260*61046927SAndroid Build Coastguard Worker```
261*61046927SAndroid Build Coastguard WorkerRADV_DEBUG=nocache,shaders ACO_DEBUG=validateir,validatera vkcube
262*61046927SAndroid Build Coastguard Worker```
263*61046927SAndroid Build Coastguard Worker
264*61046927SAndroid Build Coastguard Worker### Using GCC sanitizers
265*61046927SAndroid Build Coastguard Worker
266*61046927SAndroid Build Coastguard WorkerGCC has several sanitizers which can help figure out hard to diagnose issues. To use these, you need to pass
267*61046927SAndroid Build Coastguard Workerthe `-Dbsanitize` flag to `meson` when building mesa. For example `-Dbsanitize=undefined` will add support for
268*61046927SAndroid Build Coastguard Workerthe undefined behavior sanitizer.
269*61046927SAndroid Build Coastguard Worker
270*61046927SAndroid Build Coastguard Worker### Hardened builds and glibc++ assertions
271*61046927SAndroid Build Coastguard Worker
272*61046927SAndroid Build Coastguard WorkerSeveral Linux distributions use "hardened" builds meaning several special compiler flags are added by
273*61046927SAndroid Build Coastguard Workerdownstream packaging which are not used in mesa builds by default. These may be responsible for
274*61046927SAndroid Build Coastguard Workersome bug reports of inexplicable crashes with assertion failures you can't reproduce.
275*61046927SAndroid Build Coastguard Worker
276*61046927SAndroid Build Coastguard WorkerMost notable are the glibc++ debug flags, which you can use by adding the `-D_GLIBCXX_ASSERTIONS=1` and
277*61046927SAndroid Build Coastguard Worker`-D_GLIBCXX_DEBUG=1` flags.
278*61046927SAndroid Build Coastguard Worker
279*61046927SAndroid Build Coastguard WorkerTo see the full list of downstream compiler flags, you can use eg. `rpm --eval "%optflags"`
280*61046927SAndroid Build Coastguard Workeron Red Hat based distros like Fedora.
281*61046927SAndroid Build Coastguard Worker
282*61046927SAndroid Build Coastguard Worker### Good practices
283*61046927SAndroid Build Coastguard Worker
284*61046927SAndroid Build Coastguard WorkerHere are some good practices we learned while debugging visual corruption and hangs.
285*61046927SAndroid Build Coastguard Worker
286*61046927SAndroid Build Coastguard Worker1. Bisecting shaders:
287*61046927SAndroid Build Coastguard Worker    * Use renderdoc when examining shaders. This is deterministic while real games often use multi-threading or change the order in which shaders get compiled.
288*61046927SAndroid Build Coastguard Worker    * Edit `radv_shader.c` or `radv_pipeline.c` to change if they are compiled with LLVM or ACO.
289*61046927SAndroid Build Coastguard Worker2. Things to check early:
290*61046927SAndroid Build Coastguard Worker    * Disable value_numbering, optimizer and/or scheduler.
291*61046927SAndroid Build Coastguard Worker      Note that if any of these change the output, it does not necessarily mean that the error is there, as register assignment does also change.
292*61046927SAndroid Build Coastguard Worker3. Finding the instruction causing a hang:
293*61046927SAndroid Build Coastguard Worker    * The ability to directly manipulate the binaries gives us an easy way to find the exact instruction which causes the hang.
294*61046927SAndroid Build Coastguard Worker      Use NULL exports (for FS and VS) and `s_endpgm` to end the shader early to find the problematic instruction.
295*61046927SAndroid Build Coastguard Worker4. Other faulty instructions:
296*61046927SAndroid Build Coastguard Worker    * Use print_asm and check for illegal instructions.
297*61046927SAndroid Build Coastguard Worker    * Compare to the ACO IR to see if the assembly matches what we want (this can take a while).
298*61046927SAndroid Build Coastguard Worker      Typical issues might be a wrong instruction format leading to a wrong opcode or an sgpr used for vgpr field.
299*61046927SAndroid Build Coastguard Worker5. Comparing to the LLVM backend:
300*61046927SAndroid Build Coastguard Worker   * If everything else didn't help, we probably just do something wrong. The LLVM backend is quite mature, so its output might help find differences, but this can be a long road.
301