Home | History | Annotate | Line # | Download | only in sb
      1      1.1  mrg r600-sb
      2      1.1  mrg =======
      3      1.1  mrg 
      4      1.1  mrg * * * * *
      5      1.1  mrg 
      6      1.1  mrg Debugging
      7      1.1  mrg ---------
      8      1.1  mrg 
      9      1.1  mrg ### Environment variables
     10      1.1  mrg 
     11      1.1  mrg -   **R600\_DEBUG**
     12      1.1  mrg 
     13      1.1  mrg     There are new flags:
     14      1.1  mrg 
     15  1.1.1.2  mrg     -   **nosb** - Disable sb backend for graphics shaders
     16      1.1  mrg     -   **sbcl** - Enable optimization of compute shaders (experimental)
     17  1.1.1.2  mrg     -   **sbdry** - Dry run, optimize but use source bytecode -
     18  1.1.1.2  mrg         useful if you only want to check shader dumps
     19      1.1  mrg         without the risk of lockups and other problems
     20      1.1  mrg     -   **sbstat** - Print optimization statistics (only time so far)
     21      1.1  mrg     -   **sbdump** - Print IR after some passes.
     22  1.1.1.2  mrg     -   **sbnofallback** - Abort on errors instead of fallback
     23  1.1.1.2  mrg     -   **sbdisasm** - Use sb disassembler for shader dumps
     24  1.1.1.2  mrg     -   **sbsafemath** - Disable unsafe math optimizations
     25      1.1  mrg 
     26      1.1  mrg ### Regression debugging
     27      1.1  mrg 
     28      1.1  mrg If there are any regressions as compared to the default backend
     29      1.1  mrg (R600\_SB=0), it's possible to use the following environment variables
     30      1.1  mrg to find the incorrectly optimized shader that causes the regression.
     31      1.1  mrg 
     32      1.1  mrg -   **R600\_SB\_DSKIP\_MODE** - allows to skip optimization for some
     33      1.1  mrg     shaders
     34      1.1  mrg     -   0 - disabled (default)
     35      1.1  mrg     -   1 - skip optimization for the shaders in the range
     36      1.1  mrg         [R600\_SB\_DSKIP\_START; R600\_SB\_DSKIP\_END], that is,
     37      1.1  mrg         optimize only the shaders that are not in this range
     38      1.1  mrg     -   2 - optimize only the shaders in the range
     39      1.1  mrg         [R600\_SB\_DSKIP\_START; R600\_SB\_DSKIP\_END]
     40      1.1  mrg 
     41      1.1  mrg -   **R600\_SB\_DSKIP\_START** - start of the range (1-based)
     42      1.1  mrg 
     43      1.1  mrg -   **R600\_SB\_DSKIP\_END** - end of the range (1-based)
     44      1.1  mrg 
     45      1.1  mrg Example - optimize only the shaders 5, 6, and 7:
     46      1.1  mrg 
     47      1.1  mrg     R600_SB_DSKIP_START=5 R600_SB_DSKIP_END=7 R600_SB_DSKIP_MODE=2
     48      1.1  mrg 
     49      1.1  mrg All shaders compiled by the application are numbered starting from 1,
     50      1.1  mrg the number of shaders used by the application may be obtained by running
     51      1.1  mrg it with "R600_DEBUG=sb,sbstat" - it will print "sb: shader \#index\#"
     52      1.1  mrg for each compiled shader.
     53      1.1  mrg 
     54      1.1  mrg After figuring out the total number of shaders used by the application,
     55      1.1  mrg the variables above allow to use bisection to find the shader that is
     56      1.1  mrg the cause of regression. E.g. if the application uses 100 shaders, we
     57      1.1  mrg can divide the range [1; 100] and run the application with the
     58      1.1  mrg optimization enabled only for the first half of the shaders:
     59      1.1  mrg 
     60      1.1  mrg     R600_SB_DSKIP_START=1 R600_SB_DSKIP_END=50 R600_SB_DSKIP_MODE=2 <app>
     61      1.1  mrg 
     62      1.1  mrg If the regression is reproduced with these parameters, then the failing
     63      1.1  mrg shader is in the range [1; 50], if it's not reproduced - then it's in
     64      1.1  mrg the range [51; 100]. Then we can divide the new range again and repeat
     65      1.1  mrg the testing, until we'll reduce the range to a single failing shader.
     66      1.1  mrg 
     67      1.1  mrg *NOTE: This method relies on the assumption that the application
     68      1.1  mrg produces the same sequence of the shaders on each run. It's not always
     69      1.1  mrg true - some applications may produce different sequences of the shaders,
     70      1.1  mrg in such cases the tools like apitrace may be used to record the trace
     71      1.1  mrg with the application, then this method may be applied when replaying the
     72      1.1  mrg trace - also this may be faster and/or more convenient than testing the
     73      1.1  mrg application itself.*
     74      1.1  mrg 
     75      1.1  mrg * * * * *
     76      1.1  mrg 
     77      1.1  mrg Intermediate Representation
     78      1.1  mrg ---------------------------
     79      1.1  mrg 
     80      1.1  mrg ### Values
     81      1.1  mrg 
     82      1.1  mrg All kinds of the operands (literal constants, references to kcache
     83      1.1  mrg constants, references to GPRs, etc) are currently represented by the
     84      1.1  mrg **value** class (possibly it makes sense to switch to hierarchy of
     85      1.1  mrg classes derived from **value** instead, to save some memory).
     86      1.1  mrg 
     87      1.1  mrg All values (except some pseudo values like the exec\_mask or predicate
     88      1.1  mrg register) represent 32bit scalar values - there are no vector values,
     89      1.1  mrg CF/FETCH instructions use groups of 4 values for src and dst operands.
     90      1.1  mrg 
     91      1.1  mrg ### Nodes
     92      1.1  mrg 
     93      1.1  mrg Shader programs are represented using the tree data structure, some
     94      1.1  mrg nodes contain a list of subnodes.
     95      1.1  mrg 
     96      1.1  mrg #### Control flow nodes
     97      1.1  mrg 
     98      1.1  mrg Control flow information is represented using four special node types
     99      1.1  mrg (based on the ideas from [[1]](#references) )
    100      1.1  mrg 
    101      1.1  mrg -   **region\_node** - single-entry, single-exit region.
    102      1.1  mrg 
    103      1.1  mrg     All loops and if's in the program are enclosed in region nodes.
    104      1.1  mrg     Region nodes have two containers for phi nodes -
    105      1.1  mrg     region\_node::loop\_phi contains the phi expressions to be executed
    106      1.1  mrg     at the region entry, region\_node::phi contains the phi expressions
    107      1.1  mrg     to be executed at the region exit. It's the only type of the node
    108      1.1  mrg     that contains associated phi expressions.
    109      1.1  mrg 
    110      1.1  mrg -   **depart\_node** - "depart region \$id after { ... }"
    111      1.1  mrg 
    112      1.1  mrg     Depart target region (jump to exit point) after executing contained
    113      1.1  mrg     code.
    114      1.1  mrg 
    115      1.1  mrg -   **repeat\_node** - "repeat region \$id after { ... }"
    116      1.1  mrg 
    117      1.1  mrg     Repeat target region (jump to entry point) after executing contained
    118      1.1  mrg     code.
    119      1.1  mrg 
    120      1.1  mrg -   **if\_node** - "if (cond) { ... }"
    121      1.1  mrg 
    122      1.1  mrg     Execute contained code if condition is true. The difference from
    123      1.1  mrg     [[1]](#references) is that we don't have associated phi expressions
    124      1.1  mrg     for the **if\_node**, we enclose **if\_node** in the
    125      1.1  mrg     **region\_node** and store corresponding phi's in the
    126      1.1  mrg     **region\_node**, this allows more uniform handling.
    127      1.1  mrg 
    128      1.1  mrg The target region of depart and repeat nodes is always the region where
    129      1.1  mrg they are located (possibly in the nested region), there are no arbitrary
    130      1.1  mrg jumps/goto's - control flow in the program is always structured.
    131      1.1  mrg 
    132      1.1  mrg Typical control flow constructs can be represented as in the following
    133      1.1  mrg examples:
    134      1.1  mrg 
    135      1.1  mrg GLSL:
    136      1.1  mrg 
    137      1.1  mrg     if (cond) {
    138      1.1  mrg         < 1 >
    139      1.1  mrg     } else {
    140      1.1  mrg         < 2 >
    141      1.1  mrg     }
    142      1.1  mrg 
    143      1.1  mrg IR:
    144      1.1  mrg 
    145      1.1  mrg     region #0 {
    146      1.1  mrg         depart region #0 after {
    147      1.1  mrg             if (cond) {
    148      1.1  mrg                 depart region #0 after {
    149      1.1  mrg                     < 1 >
    150      1.1  mrg                 }
    151      1.1  mrg             }
    152      1.1  mrg             < 2 >
    153      1.1  mrg         }
    154      1.1  mrg         <region #0 phi nodes >
    155      1.1  mrg     }
    156      1.1  mrg 
    157      1.1  mrg GLSL:
    158      1.1  mrg 
    159      1.1  mrg     while (cond) {
    160      1.1  mrg         < 1 >
    161      1.1  mrg     }
    162      1.1  mrg 
    163      1.1  mrg IR:
    164      1.1  mrg 
    165      1.1  mrg     region #0 {
    166      1.1  mrg         <region #0 loop_phi nodes>
    167      1.1  mrg         repeat region #0 after {
    168      1.1  mrg             region #1 {
    169      1.1  mrg                 depart region #1 after {
    170      1.1  mrg                     if (!cond) {
    171      1.1  mrg                         depart region #0
    172      1.1  mrg                     }
    173      1.1  mrg                 }
    174      1.1  mrg             }
    175      1.1  mrg             < 1 >
    176      1.1  mrg         }
    177      1.1  mrg         <region #0 phi nodes>
    178      1.1  mrg     }
    179      1.1  mrg 
    180      1.1  mrg 'Break' and 'continue' inside the loops are directly translated to the
    181      1.1  mrg depart and repeat nodes for the corresponding loop region.
    182      1.1  mrg 
    183      1.1  mrg This may look a bit too complicated, but in fact this allows more simple
    184      1.1  mrg and uniform handling of the control flow.
    185      1.1  mrg 
    186      1.1  mrg All loop\_phi and phi nodes for some region always have the same number
    187      1.1  mrg of source operands. The number of source operands for
    188      1.1  mrg region\_node::loop\_phi nodes is 1 + number of repeat nodes that
    189      1.1  mrg reference this region as a target. The number of source operands for
    190      1.1  mrg region\_node::phi nodes is equal to the number of depart nodes that
    191      1.1  mrg reference this region as a target. All depart/repeat nodes for the
    192      1.1  mrg region have unique indices equal to the index of source operand for
    193      1.1  mrg phi/loop\_phi nodes.
    194      1.1  mrg 
    195      1.1  mrg First source operand for region\_node::loop\_phi nodes (src[0]) is an
    196      1.1  mrg incoming value that enters the region from the outside. Each remaining
    197      1.1  mrg source operand comes from the corresponding repeat node.
    198      1.1  mrg 
    199      1.1  mrg More complex example:
    200      1.1  mrg 
    201      1.1  mrg GLSL:
    202      1.1  mrg 
    203      1.1  mrg     a = 1;
    204      1.1  mrg     while (a < 5) {
    205      1.1  mrg         a = a * 2;
    206      1.1  mrg         if (b == 3) {
    207      1.1  mrg             continue;
    208      1.1  mrg         } else {
    209      1.1  mrg             a = 6;
    210      1.1  mrg         }
    211      1.1  mrg         if (c == 4)
    212      1.1  mrg             break;
    213      1.1  mrg         a = a + 1;
    214      1.1  mrg     }
    215      1.1  mrg 
    216      1.1  mrg IR with SSA form:
    217      1.1  mrg 
    218      1.1  mrg     a.1 = 1;
    219      1.1  mrg     region #0 {
    220      1.1  mrg         // loop phi values: src[0] - incoming, src[1] - from repeat_1, src[2] - from repeat_2
    221      1.1  mrg         region#0 loop_phi: a.2 = phi a.1, a.6, a.3
    222      1.1  mrg 
    223      1.1  mrg         repeat_1 region #0 after {
    224      1.1  mrg             a.3 = a.2 * 2;
    225      1.1  mrg             cond1 = (b == 3);
    226      1.1  mrg             region #1 {
    227      1.1  mrg                 depart_0 region #1 after {
    228      1.1  mrg                     if (cond1) {
    229      1.1  mrg                         repeat_2 region #0;
    230      1.1  mrg                     }
    231      1.1  mrg                 }
    232      1.1  mrg                 a.4 = 6;
    233      1.1  mrg 
    234      1.1  mrg                 region #1 phi: a.5 = phi a.4; // src[0] - from depart_0
    235      1.1  mrg             }
    236      1.1  mrg             cond2 = (c == 4);
    237      1.1  mrg             region #2 {
    238      1.1  mrg                 depart_0 region #2 after {
    239      1.1  mrg                     if (cond2) {
    240      1.1  mrg                         depart_0 region #0;
    241      1.1  mrg                     }
    242      1.1  mrg                 }
    243      1.1  mrg             }
    244      1.1  mrg             a.6 = a.5 + 1;
    245      1.1  mrg         }
    246      1.1  mrg 
    247      1.1  mrg         region #0 phi: a.7 = phi a.5 // src[0] from depart_0
    248      1.1  mrg     }
    249      1.1  mrg 
    250      1.1  mrg Phi nodes with single source operand are just copies, they are not
    251      1.1  mrg really necessary, but this allows to handle all **depart\_node**s in the
    252      1.1  mrg uniform way.
    253      1.1  mrg 
    254      1.1  mrg #### Instruction nodes
    255      1.1  mrg 
    256      1.1  mrg Instruction nodes represent different kinds of instructions -
    257      1.1  mrg **alu\_node**, **cf\_node**, **fetch\_node**, etc. Each of them contains
    258      1.1  mrg the "bc" structure where all fields of the bytecode are stored (the type
    259      1.1  mrg is **bc\_alu** for **alu\_node**, etc). The operands are represented
    260      1.1  mrg using the vectors of pointers to **value** class (node::src, node::dst)
    261      1.1  mrg 
    262      1.1  mrg #### SSA-specific nodes
    263      1.1  mrg 
    264      1.1  mrg Phi nodes currently don't have special node class, they are stored as
    265      1.1  mrg **node**. Destination vector contains a single destination value, source
    266      1.1  mrg vector contains 1 or more source values.
    267      1.1  mrg 
    268      1.1  mrg Psi nodes [[5], [6]](#references) also don't have a special node class
    269      1.1  mrg and stored as **node**. Source vector contains 3 values for each source
    270      1.1  mrg operand - the **value** of predicate, **value** of corresponding
    271      1.1  mrg PRED\_SEL field, and the source **value** itself.
    272      1.1  mrg 
    273      1.1  mrg ### Indirect addressing
    274      1.1  mrg 
    275      1.1  mrg Special kind of values (VLK\_RELREG) is used to represent indirect
    276      1.1  mrg operands. These values don't have SSA versions. The representation is
    277      1.1  mrg mostly based on the [[2]](#references). Indirect operand contains the
    278      1.1  mrg "offset/address" value (value::rel), (e.g. some SSA version of the AR
    279      1.1  mrg register value, though after some passes it may be any value - constant,
    280      1.1  mrg register, etc), also it contains the maydef and mayuse vectors of
    281      1.1  mrg pointers to **value**s (similar to dst/src vectors in the **node**) to
    282      1.1  mrg represent the effects of aliasing in the SSA form.
    283      1.1  mrg 
    284      1.1  mrg E.g. if we have the array R5.x ... R8.x and the following instruction :
    285      1.1  mrg 
    286      1.1  mrg     MOV R0.x, R[5 + AR].x
    287      1.1  mrg 
    288      1.1  mrg then source indirect operand is represented with the VLK\_RELREG value,
    289      1.1  mrg value::rel is AR, value::maydef is empty (in fact it always contain the
    290      1.1  mrg same number of elements as mayuse to simplify the handling, but they are
    291      1.1  mrg NULLs), value::mayuse contains [R5.x, R6.x, R7.x, R8.x] (or the
    292      1.1  mrg corresponding SSA versions after ssa\_rename).
    293      1.1  mrg 
    294      1.1  mrg Additional "virtual variables" as in [HSSA [2]](#references) are not
    295      1.1  mrg used, also there is no special handling for "zero versions". Typical
    296      1.1  mrg programs in our case are small, indirect addressing is rare, array sizes
    297      1.1  mrg are limited by max gpr number, so we don't really need to use special
    298      1.1  mrg tricks to avoid the explosion of value versions. Also this allows more
    299      1.1  mrg precise liveness computation for array elements without modifications to
    300      1.1  mrg the algorithms.
    301      1.1  mrg 
    302      1.1  mrg With the following instruction:
    303      1.1  mrg 
    304      1.1  mrg     MOV R[5+AR].x, R0.x
    305      1.1  mrg 
    306      1.1  mrg we'll have both maydef and mayuse vectors for dst operand filled with
    307      1.1  mrg array values initially: [R5.x, R6.x, R7.x, R8.x]. After the ssa\_rename
    308      1.1  mrg pass mayuse will contain previous versions, maydef will contain new
    309      1.1  mrg potentially-defined versions.
    310      1.1  mrg 
    311      1.1  mrg * * * * *
    312      1.1  mrg 
    313      1.1  mrg Passes
    314      1.1  mrg ------
    315      1.1  mrg 
    316      1.1  mrg -   **bc\_parser** - creates the IR from the source bytecode,
    317      1.1  mrg     initializes src and dst value vectors for instruction nodes. Most
    318      1.1  mrg     ALU nodes have one dst operand and the number of source operands is
    319      1.1  mrg     equal to the number of source operands for the ISA instruction.
    320      1.1  mrg     Nodes for PREDSETxx instructions have 3 dst operands - dst[0] is dst
    321      1.1  mrg     gpr as in the original instruction, other two are pseudo-operands
    322      1.1  mrg     that represent possibly updated predicate and exec\_mask. Predicate
    323      1.1  mrg     values are used in the predicated alu instructions (node::pred),
    324      1.1  mrg     exec\_mask values are used in the if\_nodes (if\_node::cond). Each
    325      1.1  mrg     vector operand in the CF/TEX/VTX instructions is represented with 4
    326      1.1  mrg     values - components of the vector.
    327      1.1  mrg 
    328      1.1  mrg -   **ssa\_prepare** - creates phi expressions.
    329      1.1  mrg 
    330      1.1  mrg -   **ssa\_rename** - renames the values (assigns versions).
    331      1.1  mrg 
    332      1.1  mrg -   **liveness** - liveness computation, sets 'dead' flag for unused
    333      1.1  mrg     nodes and values, optionally computes interference information for
    334      1.1  mrg     the values.
    335      1.1  mrg 
    336      1.1  mrg -   **dce\_cleanup** - eliminates 'dead' nodes, also removes some
    337      1.1  mrg     unnecessary nodes created by bc\_parser, e.g. the nodes for the JUMP
    338      1.1  mrg     instructions in the source, containers for ALU groups (they were
    339      1.1  mrg     only needed for the ssa\_rename pass)
    340      1.1  mrg 
    341      1.1  mrg -   **if\_conversion** - converts control flow with if\_nodes to the
    342      1.1  mrg     data flow in cases where it can improve performance (small alu-only
    343      1.1  mrg     branches). Both branches are executed speculatively and the phi
    344      1.1  mrg     expressions are replaced with conditional moves (CNDxx) to select
    345      1.1  mrg     the final value using the same condition predicate as was used by
    346      1.1  mrg     the original if\_node. E.g. **if\_node** used dst[2] from PREDSETxx
    347      1.1  mrg     instruction, CNDxx now uses dst[0] from the same PREDSETxx
    348      1.1  mrg     instruction.
    349      1.1  mrg 
    350      1.1  mrg -   **peephole** - peephole optimizations
    351      1.1  mrg 
    352      1.1  mrg -   **gvn** - Global Value Numbering [[2]](#references),
    353      1.1  mrg     [[3]](#references)
    354      1.1  mrg 
    355      1.1  mrg -   **gcm** - Global Code Motion [[3]](#references). Also performs
    356      1.1  mrg     grouping of the instructions of the same kind (CF/FETCH/ALU).
    357      1.1  mrg 
    358      1.1  mrg -   register allocation passes, some ideas are used from
    359      1.1  mrg     [[4]](#references), but implementation is simplified to make it more
    360      1.1  mrg     efficient in terms of the compilation speed (e.g. no recursive
    361      1.1  mrg     recoloring) while achieving good enough results.
    362      1.1  mrg 
    363      1.1  mrg     -   **ra\_split** - prepares the program to register allocation.
    364      1.1  mrg         Splits live ranges for constrained values by inserting the
    365      1.1  mrg         copies to/from temporary values, so that the live range of the
    366      1.1  mrg         constrained values becomes minimal.
    367      1.1  mrg 
    368      1.1  mrg     -   **ra\_coalesce** - performs global allocation on registers used
    369      1.1  mrg         in CF/FETCH instructions. It's performed first to make sure they
    370      1.1  mrg         end up in the same GPR. Also tries to allocate all values
    371      1.1  mrg         involved in copies (inserted by the ra\_split pass) to the same
    372      1.1  mrg         register, so that the copies may be eliminated.
    373      1.1  mrg 
    374      1.1  mrg     -   **ra\_init** - allocates gpr arrays (if indirect addressing is
    375      1.1  mrg         used), and remaining values.
    376      1.1  mrg 
    377      1.1  mrg -   **post\_scheduler** - ALU scheduler, handles VLIW packing and
    378      1.1  mrg     performs the final register allocation for local values inside ALU
    379      1.1  mrg     clauses. Eliminates all coalesced copies (if src and dst of the copy
    380      1.1  mrg     are allocated to the same register).
    381      1.1  mrg 
    382      1.1  mrg -   **ra\_checker** - optional debugging pass that tries to catch basic
    383      1.1  mrg     errors of the scheduler or regalloc,
    384      1.1  mrg 
    385      1.1  mrg -   **bc\_finalize** - propagates the regalloc information from values
    386      1.1  mrg     in node::src and node::dst vectors to the bytecode fields, converts
    387      1.1  mrg     control flow structure (region/depart/repeat) to the target
    388      1.1  mrg     instructions (JUMP/ELSE/POP,
    389      1.1  mrg     LOOP\_START/LOOP\_END/LOOP\_CONTINUE/LOOP\_BREAK).
    390      1.1  mrg 
    391      1.1  mrg -   **bc\_builder** - builds final bytecode,
    392      1.1  mrg 
    393      1.1  mrg * * * * *
    394      1.1  mrg 
    395      1.1  mrg References
    396      1.1  mrg ----------
    397      1.1  mrg 
    398      1.1  mrg [1] ["Tree-Based Code Optimization. A Thesis Proposal", Carl
    399      1.1  mrg McConnell](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.38.4210&rep=rep1&type=pdf)
    400      1.1  mrg 
    401      1.1  mrg [2] ["Effective Representation of Aliases and Indirect Memory Operations
    402      1.1  mrg in SSA Form", Fred Chow, Sun Chan, Shin-Ming Liu, Raymond Lo, Mark
    403      1.1  mrg Streich](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.33.6974&rep=rep1&type=pdf)
    404      1.1  mrg 
    405      1.1  mrg [3] ["Global Code Motion. Global Value Numbering.", Cliff
    406      1.1  mrg Click](http://www.cs.washington.edu/education/courses/cse501/06wi/reading/click-pldi95.pdf)
    407      1.1  mrg 
    408      1.1  mrg [4] ["Register Allocation for Programs in SSA Form", Sebastian
    409      1.1  mrg Hack](http://digbib.ubka.uni-karlsruhe.de/volltexte/documents/6532)
    410      1.1  mrg 
    411      1.1  mrg [5] ["An extension to the SSA representation for predicated code",
    412      1.1  mrg Francois de
    413      1.1  mrg Ferriere](http://www.cdl.uni-saarland.de/ssasem/talks/Francois.de.Ferriere.pdf)
    414      1.1  mrg 
    415      1.1  mrg [6] ["Improvements to the Psi-SSA Representation", F. de
    416      1.1  mrg Ferriere](http://www.scopesconf.org/scopes-07/presentations/3_Presentation.pdf)
    417