101e04c3fSmrgNew IR, or NIR, is an IR for Mesa intended to sit below GLSL IR and Mesa IR. 201e04c3fSmrgIts design inherits from the various IRs that Mesa has used in the past, as 301e04c3fSmrgwell as Direct3D assembly, and it includes a few new ideas as well. It is a 401e04c3fSmrgflat (in terms of using instructions instead of expressions), typeless IR, 501e04c3fSmrgsimilar to TGSI and Mesa IR. It also supports SSA (although it doesn't require 601e04c3fSmrgit). 701e04c3fSmrg 801e04c3fSmrgVariables 901e04c3fSmrg========= 1001e04c3fSmrg 1101e04c3fSmrgNIR includes support for source-level GLSL variables through a structure mostly 1201e04c3fSmrgcopied from GLSL IR. These will be used for linking and conversion from GLSL IR 1301e04c3fSmrg(and later, from an AST), but for the most part, they will be lowered to 1401e04c3fSmrgregisters (see below) and loads/stores. 1501e04c3fSmrg 1601e04c3fSmrgRegisters 1701e04c3fSmrg========= 1801e04c3fSmrg 1901e04c3fSmrgRegisters are light-weight; they consist of a structure that only contains its 2001e04c3fSmrgsize, its index for liveness analysis, and an optional name for debugging. In 2101e04c3fSmrgaddition, registers can be local to a function or global to the entire shader; 2201e04c3fSmrgthe latter will be used in ARB_shader_subroutine for passing parameters and 2301e04c3fSmrggetting return values from subroutines. Registers can also be an array, in which 2401e04c3fSmrgcase they can be accessed indirectly. Each ALU instruction (add, subtract, etc.) 2501e04c3fSmrgworks directly with registers or SSA values (see below). 2601e04c3fSmrg 2701e04c3fSmrgSSA 2801e04c3fSmrg======== 2901e04c3fSmrg 3001e04c3fSmrgEverywhere a register can be loaded/stored, an SSA value can be used instead. 3101e04c3fSmrgThe only exception is that arrays/indirect addressing are not supported with 3201e04c3fSmrgSSA; although research has been done on extensions of SSA to arrays before, it's 3301e04c3fSmrgusually for the purpose of parallelization (which we're not interested in), and 3401e04c3fSmrgadds some overhead in the form of adding copies or extra arrays (which is much 3501e04c3fSmrgmore expensive than introducing copies between non-array registers). SSA uses 3601e04c3fSmrgpoint directly to their corresponding definition, which in turn points to the 3701e04c3fSmrginstruction it is part of. This creates an implicit use-def chain and avoids the 3801e04c3fSmrgneed for an external structure for each SSA register. 3901e04c3fSmrg 4001e04c3fSmrgFunctions 4101e04c3fSmrg========= 4201e04c3fSmrg 4301e04c3fSmrgSupport for function calls is mostly similar to GLSL IR. Each shader contains a 4401e04c3fSmrglist of functions, and each function has a list of overloads. Each overload 4501e04c3fSmrgcontains a list of parameters, and may contain an implementation which specifies 4601e04c3fSmrgthe variables that correspond to the parameters and return value. Inlining a 4701e04c3fSmrgfunction, assuming it has a single return point, is as simple as copying its 4801e04c3fSmrginstructions, registers, and local variables into the target function and then 4901e04c3fSmrginserting copies to and from the new parameters as appropriate. After functions 5001e04c3fSmrgare inlined and any non-subroutine functions are deleted, parameters and return 5101e04c3fSmrgvariables will be converted to global variables and then global registers. We 5201e04c3fSmrgdon't do this lowering earlier (i.e. the fortranizer idea) for a few reasons: 5301e04c3fSmrg 5401e04c3fSmrg- If we want to do optimizations before link time, we need to have the function 5501e04c3fSmrgsignature available during link-time. 5601e04c3fSmrg 5701e04c3fSmrg- If we do any inlining before link time, then we might wind up with the 5801e04c3fSmrginlined function and the non-inlined function using the same global 5901e04c3fSmrgvariables/registers which would preclude optimization. 6001e04c3fSmrg 6101e04c3fSmrgIntrinsics 6201e04c3fSmrg========= 6301e04c3fSmrg 6401e04c3fSmrgAny operation (other than function calls and textures) which touches a variable 6501e04c3fSmrgor is not referentially transparent is represented by an intrinsic. Intrinsics 6601e04c3fSmrgare similar to the idea of a "builtin function," i.e. a function declaration 6701e04c3fSmrgwhose implementation is provided by the backend, except they are more powerful 6801e04c3fSmrgin the following ways: 6901e04c3fSmrg 7001e04c3fSmrg- They can also load and store registers when appropriate, which limits the 7101e04c3fSmrgnumber of variables needed in later stages of the IR while obviating the need 7201e04c3fSmrgfor a separate load/store variable instruction. 7301e04c3fSmrg 7401e04c3fSmrg- Intrinsics can be marked as side-effect free, which permits them to be 7501e04c3fSmrgtreated like any other instruction when it comes to optimizations. This allows 7601e04c3fSmrgload intrinsics to be represented as intrinsics while still being optimized 7701e04c3fSmrgaway by dead code elimination, common subexpression elimination, etc. 7801e04c3fSmrg 7901e04c3fSmrgIntrinsics are used for: 8001e04c3fSmrg 8101e04c3fSmrg- Atomic operations 8201e04c3fSmrg- Memory barriers 8301e04c3fSmrg- Subroutine calls 8401e04c3fSmrg- Geometry shader emitVertex and endPrimitive 8501e04c3fSmrg- Loading and storing variables (before lowering) 8601e04c3fSmrg- Loading and storing uniforms, shader inputs and outputs, etc (after lowering) 8701e04c3fSmrg- Copying variables (cases where in GLSL the destination is a structure or 8801e04c3fSmrgarray) 8901e04c3fSmrg- The kitchen sink 9001e04c3fSmrg- ... 9101e04c3fSmrg 9201e04c3fSmrgTextures 9301e04c3fSmrg========= 9401e04c3fSmrg 9501e04c3fSmrgUnfortunately, there are far too many texture operations to represent each one 9601e04c3fSmrgof them with an intrinsic, so there's a special texture instruction similar to 9701e04c3fSmrgthe GLSL IR one. The biggest difference is that, while the texture instruction 9801e04c3fSmrghas a sampler dereference field used just like in GLSL IR, this gets lowered to 9901e04c3fSmrga texture unit index (with a possible indirect offset) while the type 10001e04c3fSmrginformation of the original sampler is kept around for backends. Also, all the 10101e04c3fSmrgnon-constant sources are stored in a single array to make it easier for 10201e04c3fSmrgoptimization passes to iterate over all the sources. 10301e04c3fSmrg 10401e04c3fSmrgControl Flow 10501e04c3fSmrg========= 10601e04c3fSmrg 10701e04c3fSmrgLike in GLSL IR, control flow consists of a tree of "control flow nodes", which 10801e04c3fSmrginclude if statements and loops, and jump instructions (break, continue, and 10901e04c3fSmrgreturn). Unlike GLSL IR, though, the leaves of the tree aren't statements but 11001e04c3fSmrgbasic blocks. Each basic block also keeps track of its successors and 11101e04c3fSmrgpredecessors, and function implementations keep track of the beginning basic 11201e04c3fSmrgblock (the first basic block of the function) and the ending basic block (a fake 11301e04c3fSmrgbasic block that every return statement points to). Together, these elements 11401e04c3fSmrgmake up the control flow graph, in this case a redundant piece of information on 11501e04c3fSmrgtop of the control flow tree that will be used by almost all the optimizations. 11601e04c3fSmrgThere are helper functions to add and remove control flow nodes that also update 11701e04c3fSmrgthe control flow graph, and so usually it doesn't need to be touched by passes 11801e04c3fSmrgthat modify control flow nodes. 119