Name Date Size #Lines LOC

..--

glcpp/H25-Apr-2025-7,1294,389

tests/H25-Apr-2025-6,2124,733

CrossPlatformSettings_piece_all.glslH A D25-Apr-20253.7 KiB9980

READMEH A D25-Apr-202510.5 KiB228182

TODOH A D25-Apr-2025689 1310

ast.hH A D25-Apr-202537.1 KiB1,423735

ast_array_index.cppH A D25-Apr-202515.2 KiB365207

ast_expr.cppH A D25-Apr-20252.2 KiB9663

ast_function.cppH A D25-Apr-202598.8 KiB2,6261,751

ast_to_hir.cppH A D25-Apr-2025352.7 KiB9,2425,849

ast_type.cppH A D25-Apr-202533.7 KiB1,069849

astc_decoder.glslH A D25-Apr-202541.2 KiB1,3831,210

bc1.glslH A D25-Apr-202516.6 KiB545458

bc4.glslH A D25-Apr-20255.6 KiB188153

builtin_functions.cppH A D25-Apr-2025509.3 KiB9,5897,770

builtin_functions.hH A D25-Apr-20252.3 KiB7936

builtin_types.cppH A D25-Apr-202519.4 KiB461363

builtin_variables.cppH A D25-Apr-202565.6 KiB1,7181,311

etc2_rgba_stitch.glslH A D25-Apr-20251.9 KiB4739

float64.glslH A D25-Apr-202555.3 KiB1,7501,586

gl_nir.hH A D25-Apr-20252.8 KiB7437

gl_nir_detect_function_recursion.cH A D25-Apr-202510.8 KiB312135

gl_nir_link_atomics.cH A D25-Apr-202513 KiB376265

gl_nir_link_interface_blocks.cH A D25-Apr-202518.6 KiB493267

gl_nir_link_uniform_blocks.cH A D25-Apr-202549.2 KiB1,334853

gl_nir_link_uniform_initializers.cH A D25-Apr-202510.8 KiB310245

gl_nir_link_uniforms.cH A D25-Apr-202571.3 KiB1,9421,337

gl_nir_link_varyings.cH A D25-Apr-2025177.8 KiB4,5932,886

gl_nir_link_varyings.hH A D25-Apr-20256.8 KiB24369

gl_nir_link_xfb.cH A D25-Apr-20258.6 KiB229122

gl_nir_linker.cH A D25-Apr-2025114.1 KiB3,1072,055

gl_nir_linker.hH A D25-Apr-20256.3 KiB157100

gl_nir_lower_atomics.cH A D25-Apr-20255.8 KiB185117

gl_nir_lower_blend_equation_advanced.cH A D25-Apr-202520.5 KiB596390

gl_nir_lower_buffers.cH A D25-Apr-202513.6 KiB355231

gl_nir_lower_discard_flow.cH A D25-Apr-20255.9 KiB16099

gl_nir_lower_images.cH A D25-Apr-20253.6 KiB11568

gl_nir_lower_named_interface_blocks.cH A D25-Apr-202513.3 KiB373245

gl_nir_lower_packed_varyings.cH A D25-Apr-202538.9 KiB1,050609

gl_nir_lower_samplers.cH A D25-Apr-20251.7 KiB409

gl_nir_lower_samplers_as_deref.cH A D25-Apr-202513.6 KiB397236

gl_nir_lower_xfb_varying.cH A D25-Apr-20257.1 KiB208144

gl_nir_opt_dead_builtin_varyings.cH A D25-Apr-202517.7 KiB539362

glsl_lexer.llH A D25-Apr-202544.2 KiB824747

glsl_parser.yyH A D25-Apr-202595.8 KiB3,1252,867

glsl_parser_extras.cppH A D25-Apr-202585.4 KiB2,5911,899

glsl_parser_extras.hH A D25-Apr-202537.1 KiB1,149744

glsl_symbol_table.cppH A D25-Apr-20258.8 KiB287215

glsl_symbol_table.hH A D25-Apr-20253.6 KiB11238

glsl_to_nir.cppH A D25-Apr-202592.4 KiB2,8182,279

glsl_to_nir.hH A D25-Apr-20251.8 KiB5419

hir_field_selection.cppH A D25-Apr-20253.1 KiB8141

ir.cppH A D25-Apr-202566.2 KiB2,3361,836

ir.hH A D25-Apr-202571.7 KiB2,5551,154

ir_array_refcount.cppH A D25-Apr-20256 KiB208119

ir_array_refcount.hH A D25-Apr-20253.7 KiB13046

ir_basic_block.cppH A D25-Apr-20253.3 KiB10039

ir_basic_block.hH A D25-Apr-20251.4 KiB348

ir_builder.cppH A D25-Apr-202510.9 KiB642503

ir_builder.hH A D25-Apr-20256.8 KiB235160

ir_clone.cppH A D25-Apr-202512.5 KiB441318

ir_constant_expression.cppH A D25-Apr-202535 KiB1,242767

ir_equals.cppH A D25-Apr-20255.5 KiB218151

ir_expression_flattening.cppH A D25-Apr-20252.6 KiB8237

ir_expression_flattening.hH A D25-Apr-20251.8 KiB445

ir_expression_operation.pyH A D25-Apr-202546.1 KiB866615

ir_function.cppH A D25-Apr-202514.8 KiB433258

ir_function_detect_recursion.cppH A D25-Apr-202510.5 KiB320134

ir_hierarchical_visitor.cppH A D25-Apr-20259 KiB404298

ir_hierarchical_visitor.hH A D25-Apr-20259.1 KiB21767

ir_hv_accept.cppH A D25-Apr-202511.9 KiB458320

ir_optimization.hH A D25-Apr-20253.3 KiB7538

ir_print_visitor.cppH A D25-Apr-202517.2 KiB686544

ir_print_visitor.hH A D25-Apr-20253.2 KiB9743

ir_reader.cppH A D25-Apr-202535.6 KiB1,2151,005

ir_reader.hH A D25-Apr-20251.4 KiB346

ir_rvalue_visitor.cppH A D25-Apr-20256.7 KiB316239

ir_rvalue_visitor.hH A D25-Apr-20253.8 KiB8949

ir_uniform.hH A D25-Apr-20256.6 KiB23460

ir_validate.cppH A D25-Apr-202541 KiB1,2651,045

ir_variable_refcount.cppH A D25-Apr-20254.6 KiB16091

ir_variable_refcount.hH A D25-Apr-20253.1 KiB9936

ir_visitor.hH A D25-Apr-20253.6 KiB9550

link_functions.cppH A D25-Apr-202512.7 KiB358196

link_interface_blocks.cppH A D25-Apr-202510.9 KiB318175

linker.cppH A D25-Apr-202572.1 KiB1,9601,260

linker.hH A D25-Apr-20252.2 KiB5927

linker_util.cppH A D25-Apr-202516.3 KiB462271

linker_util.hH A D25-Apr-20254.7 KiB15071

list.hH A D25-Apr-202521.8 KiB776513

lower_builtins.cppH A D25-Apr-20251.9 KiB6827

lower_instructions.cppH A D25-Apr-202518.9 KiB526304

lower_jumps.cppH A D25-Apr-202535 KiB939446

lower_mat_op_to_vec.cppH A D25-Apr-202514.2 KiB442297

lower_packing_builtins.cppH A D25-Apr-202547.9 KiB1,356507

lower_precision.cppH A D25-Apr-202543.8 KiB1,357897

lower_subroutine.cppH A D25-Apr-20253.7 KiB12577

lower_vec_index_to_cond_assign.cppH A D25-Apr-20255.7 KiB18297

lower_vector_derefs.cppH A D25-Apr-20257.6 KiB204123

main.cppH A D25-Apr-20253.3 KiB10758

meson.buildH A D25-Apr-20257.9 KiB283260

opt_algebraic.cppH A D25-Apr-202512.2 KiB422272

opt_dead_builtin_variables.cppH A D25-Apr-20253.4 KiB8429

opt_dead_code.cppH A D25-Apr-20256.1 KiB18484

opt_dead_code_local.cppH A D25-Apr-202510 KiB354234

opt_flatten_nested_if_blocks.cppH A D25-Apr-20252.7 KiB10442

opt_flip_matrices.cppH A D25-Apr-20253.9 KiB12470

opt_function_inlining.cppH A D25-Apr-202512.2 KiB384219

opt_if_simplification.cppH A D25-Apr-20253.7 KiB12855

opt_minmax.cppH A D25-Apr-202516 KiB534362

opt_rebalance_tree.cppH A D25-Apr-20259.4 KiB338195

opt_tree_grafting.cppH A D25-Apr-202511.4 KiB424279

program.hH A D25-Apr-20251.7 KiB4817

propagate_invariance.cppH A D25-Apr-20253.7 KiB13070

s_expression.cppH A D25-Apr-20256 KiB221147

s_expression.hH A D25-Apr-20254.6 KiB17993

serialize.cppH A D25-Apr-202549 KiB1,3621,088

serialize.hH A D25-Apr-20251.6 KiB5120

shader_cache.cppH A D25-Apr-20259.4 KiB266145

shader_cache.hH A D25-Apr-20251.5 KiB4112

standalone.cppH A D25-Apr-202516.7 KiB459361

standalone.hH A D25-Apr-20251.7 KiB5524

standalone_scaffolding.cppH A D25-Apr-202510.9 KiB347254

standalone_scaffolding.hH A D25-Apr-20254.1 KiB12064

string_to_uint_map.cppH A D25-Apr-20251.9 KiB6127

string_to_uint_map.hH A D25-Apr-20255.4 KiB18899

test.cppH A D25-Apr-20252.4 KiB7933

test_optpass.cppH A D25-Apr-20257.8 KiB239171

test_optpass.hH A D25-Apr-20251.2 KiB304

README

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