1b8e80941SmrgWelcome to Mesa's GLSL compiler. A brief overview of how things flow: 2b8e80941Smrg 3b8e80941Smrg1) lex and yacc-based preprocessor takes the incoming shader string 4b8e80941Smrgand produces a new string containing the preprocessed shader. This 5b8e80941Smrgtakes care of things like #if, #ifdef, #define, and preprocessor macro 6b8e80941Smrginvocations. Note that #version, #extension, and some others are 7b8e80941Smrgpassed straight through. See glcpp/* 8b8e80941Smrg 9b8e80941Smrg2) lex and yacc-based parser takes the preprocessed string and 10b8e80941Smrggenerates the AST (abstract syntax tree). Almost no checking is 11b8e80941Smrgperformed in this stage. See glsl_lexer.ll and glsl_parser.yy. 12b8e80941Smrg 13b8e80941Smrg3) The AST is converted to "HIR". This is the intermediate 14b8e80941Smrgrepresentation of the compiler. Constructors are generated, function 15b8e80941Smrgcalls are resolved to particular function signatures, and all the 16b8e80941Smrgsemantic checking is performed. See ast_*.cpp for the conversion, and 17b8e80941Smrgir.h for the IR structures. 18b8e80941Smrg 19b8e80941Smrg4) The driver (Mesa, or main.cpp for the standalone binary) performs 20b8e80941Smrgoptimizations. These include copy propagation, dead code elimination, 21b8e80941Smrgconstant folding, and others. Generally the driver will call 22b8e80941Smrgoptimizations in a loop, as each may open up opportunities for other 23b8e80941Smrgoptimizations to do additional work. See most files called ir_*.cpp 24b8e80941Smrg 25b8e80941Smrg5) linking is performed. This does checking to ensure that the 26b8e80941Smrgoutputs of the vertex shader match the inputs of the fragment shader, 27b8e80941Smrgand assigns locations to uniforms, attributes, and varyings. See 28b8e80941Smrglinker.cpp. 29b8e80941Smrg 30b8e80941Smrg6) The driver may perform additional optimization at this point, as 31b8e80941Smrgfor example dead code elimination previously couldn't remove functions 32b8e80941Smrgor global variable usage when we didn't know what other code would be 33b8e80941Smrglinked in. 34b8e80941Smrg 35b8e80941Smrg7) The driver performs code generation out of the IR, taking a linked 36b8e80941Smrgshader program and producing a compiled program for each stage. See 37b8e80941Smrg../mesa/program/ir_to_mesa.cpp for Mesa IR code generation. 38b8e80941Smrg 39b8e80941SmrgFAQ: 40b8e80941Smrg 41b8e80941SmrgQ: What is HIR versus IR versus LIR? 42b8e80941Smrg 43b8e80941SmrgA: The idea behind the naming was that ast_to_hir would produce a 44b8e80941Smrghigh-level IR ("HIR"), with things like matrix operations, structure 45b8e80941Smrgassignments, etc., present. A series of lowering passes would occur 46b8e80941Smrgthat do things like break matrix multiplication into a series of dot 47b8e80941Smrgproducts/MADs, make structure assignment be a series of assignment of 48b8e80941Smrgcomponents, flatten if statements into conditional moves, and such, 49b8e80941Smrgproducing a low level IR ("LIR"). 50b8e80941Smrg 51b8e80941SmrgHowever, it now appears that each driver will have different 52b8e80941Smrgrequirements from a LIR. A 915-generation chipset wants all functions 53b8e80941Smrginlined, all loops unrolled, all ifs flattened, no variable array 54b8e80941Smrgaccesses, and matrix multiplication broken down. The Mesa IR backend 55b8e80941Smrgfor swrast would like matrices and structure assignment broken down, 56b8e80941Smrgbut it can support function calls and dynamic branching. A 965 vertex 57b8e80941Smrgshader IR backend could potentially even handle some matrix operations 58b8e80941Smrgwithout breaking them down, but the 965 fragment shader IR backend 59b8e80941Smrgwould want to break to have (almost) all operations down channel-wise 60b8e80941Smrgand perform optimization on that. As a result, there's no single 61b8e80941Smrglow-level IR that will make everyone happy. So that usage has fallen 62b8e80941Smrgout of favor, and each driver will perform a series of lowering passes 63b8e80941Smrgto take the HIR down to whatever restrictions it wants to impose 64b8e80941Smrgbefore doing codegen. 65b8e80941Smrg 66b8e80941SmrgQ: How is the IR structured? 67b8e80941Smrg 68b8e80941SmrgA: The best way to get started seeing it would be to run the 69b8e80941Smrgstandalone compiler against a shader: 70b8e80941Smrg 71b8e80941Smrg./glsl_compiler --dump-lir \ 72b8e80941Smrg ~/src/piglit/tests/shaders/glsl-orangebook-ch06-bump.frag 73b8e80941Smrg 74b8e80941SmrgSo for example one of the ir_instructions in main() contains: 75b8e80941Smrg 76b8e80941Smrg(assign (constant bool (1)) (var_ref litColor) (expression vec3 * (var_ref Surf 77b8e80941SmrgaceColor) (var_ref __retval) ) ) 78b8e80941Smrg 79b8e80941SmrgOr more visually: 80b8e80941Smrg (assign) 81b8e80941Smrg / | \ 82b8e80941Smrg (var_ref) (expression *) (constant bool 1) 83b8e80941Smrg / / \ 84b8e80941Smrg(litColor) (var_ref) (var_ref) 85b8e80941Smrg / \ 86b8e80941Smrg (SurfaceColor) (__retval) 87b8e80941Smrg 88b8e80941Smrgwhich came from: 89b8e80941Smrg 90b8e80941SmrglitColor = SurfaceColor * max(dot(normDelta, LightDir), 0.0); 91b8e80941Smrg 92b8e80941Smrg(the max call is not represented in this expression tree, as it was a 93b8e80941Smrgfunction call that got inlined but not brought into this expression 94b8e80941Smrgtree) 95b8e80941Smrg 96b8e80941SmrgEach of those nodes is a subclass of ir_instruction. A particular 97b8e80941Smrgir_instruction instance may only appear once in the whole IR tree with 98b8e80941Smrgthe exception of ir_variables, which appear once as variable 99b8e80941Smrgdeclarations: 100b8e80941Smrg 101b8e80941Smrg(declare () vec3 normDelta) 102b8e80941Smrg 103b8e80941Smrgand multiple times as the targets of variable dereferences: 104b8e80941Smrg... 105b8e80941Smrg(assign (constant bool (1)) (var_ref __retval) (expression float dot 106b8e80941Smrg (var_ref normDelta) (var_ref LightDir) ) ) 107b8e80941Smrg... 108b8e80941Smrg(assign (constant bool (1)) (var_ref __retval) (expression vec3 - 109b8e80941Smrg (var_ref LightDir) (expression vec3 * (constant float (2.000000)) 110b8e80941Smrg (expression vec3 * (expression float dot (var_ref normDelta) (var_ref 111b8e80941Smrg LightDir) ) (var_ref normDelta) ) ) ) ) 112b8e80941Smrg... 113b8e80941Smrg 114b8e80941SmrgEach node has a type. Expressions may involve several different types: 115b8e80941Smrg(declare (uniform ) mat4 gl_ModelViewMatrix) 116b8e80941Smrg((assign (constant bool (1)) (var_ref constructor_tmp) (expression 117b8e80941Smrg vec4 * (var_ref gl_ModelViewMatrix) (var_ref gl_Vertex) ) ) 118b8e80941Smrg 119b8e80941SmrgAn expression tree can be arbitrarily deep, and the compiler tries to 120b8e80941Smrgkeep them structured like that so that things like algebraic 121b8e80941Smrgoptimizations ((color * 1.0 == color) and ((mat1 * mat2) * vec == mat1 122b8e80941Smrg* (mat2 * vec))) or recognizing operation patterns for code generation 123b8e80941Smrg(vec1 * vec2 + vec3 == mad(vec1, vec2, vec3)) are easier. This comes 124b8e80941Smrgat the expense of additional trickery in implementing some 125b8e80941Smrgoptimizations like CSE where one must navigate an expression tree. 126b8e80941Smrg 127b8e80941SmrgQ: Why no SSA representation? 128b8e80941Smrg 129b8e80941SmrgA: Converting an IR tree to SSA form makes dead code elimination, 130b8e80941Smrgcommon subexpression elimination, and many other optimizations much 131b8e80941Smrgeasier. However, in our primarily vector-based language, there's some 132b8e80941Smrgmajor questions as to how it would work. Do we do SSA on the scalar 133b8e80941Smrgor vector level? If we do it at the vector level, we're going to end 134b8e80941Smrgup with many different versions of the variable when encountering code 135b8e80941Smrglike: 136b8e80941Smrg 137b8e80941Smrg(assign (constant bool (1)) (swiz x (var_ref __retval) ) (var_ref a) ) 138b8e80941Smrg(assign (constant bool (1)) (swiz y (var_ref __retval) ) (var_ref b) ) 139b8e80941Smrg(assign (constant bool (1)) (swiz z (var_ref __retval) ) (var_ref c) ) 140b8e80941Smrg 141b8e80941SmrgIf every masked update of a component relies on the previous value of 142b8e80941Smrgthe variable, then we're probably going to be quite limited in our 143b8e80941Smrgdead code elimination wins, and recognizing common expressions may 144b8e80941Smrgjust not happen. On the other hand, if we operate channel-wise, then 145b8e80941Smrgwe'll be prone to optimizing the operation on one of the channels at 146b8e80941Smrgthe expense of making its instruction flow different from the other 147b8e80941Smrgchannels, and a vector-based GPU would end up with worse code than if 148b8e80941Smrgwe didn't optimize operations on that channel! 149b8e80941Smrg 150b8e80941SmrgOnce again, it appears that our optimization requirements are driven 151b8e80941Smrgsignificantly by the target architecture. For now, targeting the Mesa 152b8e80941SmrgIR backend, SSA does not appear to be that important to producing 153b8e80941Smrgexcellent code, but we do expect to do some SSA-based optimizations 154b8e80941Smrgfor the 965 fragment shader backend when that is developed. 155b8e80941Smrg 156b8e80941SmrgQ: How should I expand instructions that take multiple backend instructions? 157b8e80941Smrg 158b8e80941SmrgSometimes you'll have to do the expansion in your code generation -- 159b8e80941Smrgsee, for example, ir_to_mesa.cpp's handling of ir_unop_sqrt. However, 160b8e80941Smrgin many cases you'll want to do a pass over the IR to convert 161b8e80941Smrgnon-native instructions to a series of native instructions. For 162b8e80941Smrgexample, for the Mesa backend we have ir_div_to_mul_rcp.cpp because 163b8e80941SmrgMesa IR (and many hardware backends) only have a reciprocal 164b8e80941Smrginstruction, not a divide. Implementing non-native instructions this 165b8e80941Smrgway gives the chance for constant folding to occur, so (a / 2.0) 166b8e80941Smrgbecomes (a * 0.5) after codegen instead of (a * (1.0 / 2.0)) 167b8e80941Smrg 168b8e80941SmrgQ: How shoud I handle my special hardware instructions with respect to IR? 169b8e80941Smrg 170b8e80941SmrgOur current theory is that if multiple targets have an instruction for 171b8e80941Smrgsome operation, then we should probably be able to represent that in 172b8e80941Smrgthe IR. Generally this is in the form of an ir_{bin,un}op expression 173b8e80941Smrgtype. For example, we initially implemented fract() using (a - 174b8e80941Smrgfloor(a)), but both 945 and 965 have instructions to give that result, 175b8e80941Smrgand it would also simplify the implementation of mod(), so 176b8e80941Smrgir_unop_fract was added. The following areas need updating to add a 177b8e80941Smrgnew expression type: 178b8e80941Smrg 179b8e80941Smrgir.h (new enum) 180b8e80941Smrgir.cpp:operator_strs (used for ir_reader) 181b8e80941Smrgir_constant_expression.cpp (you probably want to be able to constant fold) 182b8e80941Smrgir_validate.cpp (check users have the right types) 183b8e80941Smrg 184b8e80941SmrgYou may also need to update the backends if they will see the new expr type: 185b8e80941Smrg 186b8e80941Smrg../mesa/program/ir_to_mesa.cpp 187b8e80941Smrg 188b8e80941SmrgYou can then use the new expression from builtins (if all backends 189b8e80941Smrgwould rather see it), or scan the IR and convert to use your new 190b8e80941Smrgexpression type (see ir_mod_to_floor, for example). 191b8e80941Smrg 192b8e80941SmrgQ: How is memory management handled in the compiler? 193b8e80941Smrg 194b8e80941SmrgThe hierarchical memory allocator "talloc" developed for the Samba 195b8e80941Smrgproject is used, so that things like optimization passes don't have to 196b8e80941Smrgworry about their garbage collection so much. It has a few nice 197b8e80941Smrgfeatures, including low performance overhead and good debugging 198b8e80941Smrgsupport that's trivially available. 199b8e80941Smrg 200b8e80941SmrgGenerally, each stage of the compile creates a talloc context and 201b8e80941Smrgallocates its memory out of that or children of it. At the end of the 202b8e80941Smrgstage, the pieces still live are stolen to a new context and the old 203b8e80941Smrgone freed, or the whole context is kept for use by the next stage. 204b8e80941Smrg 205b8e80941SmrgFor IR transformations, a temporary context is used, then at the end 206b8e80941Smrgof all transformations, reparent_ir reparents all live nodes under the 207b8e80941Smrgshader's IR list, and the old context full of dead nodes is freed. 208b8e80941SmrgWhen developing a single IR transformation pass, this means that you 209b8e80941Smrgwant to allocate instruction nodes out of the temporary context, so if 210b8e80941Smrgit becomes dead it doesn't live on as the child of a live node. At 211b8e80941Smrgthe moment, optimization passes aren't passed that temporary context, 212b8e80941Smrgso they find it by calling talloc_parent() on a nearby IR node. The 213b8e80941Smrgtalloc_parent() call is expensive, so many passes will cache the 214b8e80941Smrgresult of the first talloc_parent(). Cleaning up all the optimization 215b8e80941Smrgpasses to take a context argument and not call talloc_parent() is left 216b8e80941Smrgas an exercise. 217b8e80941Smrg 218b8e80941SmrgQ: What is the file naming convention in this directory? 219b8e80941Smrg 220b8e80941SmrgInitially, there really wasn't one. We have since adopted one: 221b8e80941Smrg 222b8e80941Smrg - Files that implement code lowering passes should be named lower_* 223b8e80941Smrg (e.g., lower_noise.cpp). 224b8e80941Smrg - Files that implement optimization passes should be named opt_*. 225b8e80941Smrg - Files that implement a class that is used throught the code should 226b8e80941Smrg take the name of that class (e.g., ir_hierarchical_visitor.cpp). 227b8e80941Smrg - Files that contain code not fitting in one of the previous 228b8e80941Smrg categories should have a sensible name (e.g., glsl_parser.yy). 229