• Home
Name Date Size #Lines LOC

..--

glcpp/03-May-2024-7,1764,439

tests/03-May-2024-8,7226,479

READMED03-May-202410.5 KiB229183

TODOD03-May-2024689 1310

ast.hD03-May-202436.5 KiB1,402727

ast_array_index.cppD03-May-202415 KiB365207

ast_expr.cppD03-May-20242.2 KiB9663

ast_function.cppD03-May-202494.9 KiB2,5541,687

ast_to_hir.cppD03-May-2024340.2 KiB8,9705,681

ast_type.cppD03-May-202435.2 KiB1,013802

builtin_functions.cppD03-May-2024373 KiB7,8306,280

builtin_functions.hD03-May-20242.5 KiB8540

builtin_int64.hD03-May-202443 KiB1,197690

builtin_types.cppD03-May-202419.5 KiB475368

builtin_variables.cppD03-May-202462.7 KiB1,6301,232

float64.glslD03-May-202454.6 KiB1,7251,564

generate_ir.cppD03-May-20241.3 KiB347

gl_nir.hD03-May-20241.9 KiB5221

gl_nir_link_atomics.cD03-May-202412.9 KiB374263

gl_nir_link_uniform_blocks.cD03-May-202423.4 KiB648338

gl_nir_link_uniform_initializers.cD03-May-202410.7 KiB308243

gl_nir_link_uniforms.cD03-May-202468.9 KiB1,8751,293

gl_nir_link_xfb.cD03-May-20247.3 KiB19493

gl_nir_linker.cD03-May-202425.6 KiB693451

gl_nir_linker.hD03-May-20242.9 KiB7738

gl_nir_lower_atomics.cD03-May-20245.7 KiB179111

gl_nir_lower_buffers.cD03-May-202412.9 KiB335217

gl_nir_lower_images.cD03-May-20244.3 KiB13787

gl_nir_lower_samplers.cD03-May-20241.7 KiB409

gl_nir_lower_samplers_as_deref.cD03-May-202412.4 KiB369215

glsl_lexer.llD03-May-202441.8 KiB794719

glsl_parser.yyD03-May-202494.8 KiB3,1112,855

glsl_parser_extras.cppD03-May-202479.2 KiB2,4201,755

glsl_parser_extras.hD03-May-202433.9 KiB1,062695

glsl_symbol_table.cppD03-May-20249 KiB295222

glsl_symbol_table.hD03-May-20243.6 KiB11437

glsl_to_nir.cppD03-May-202490.7 KiB2,7222,205

glsl_to_nir.hD03-May-20241.8 KiB5217

hir_field_selection.cppD03-May-20243.1 KiB8141

int64.glslD03-May-20242.6 KiB122100

ir.cppD03-May-202466.7 KiB2,4141,898

ir.hD03-May-202472.6 KiB2,5521,122

ir_array_refcount.cppD03-May-20246 KiB208119

ir_array_refcount.hD03-May-20243.6 KiB12844

ir_basic_block.cppD03-May-20243.3 KiB10039

ir_basic_block.hD03-May-20241.4 KiB348

ir_builder.cppD03-May-202411.2 KiB649509

ir_builder.hD03-May-20247.1 KiB244169

ir_builder_print_visitor.cppD03-May-202423.3 KiB779608

ir_builder_print_visitor.hD03-May-20241.3 KiB325

ir_clone.cppD03-May-202412.5 KiB445321

ir_constant_expression.cppD03-May-202434.7 KiB1,246770

ir_equals.cppD03-May-20245.3 KiB212147

ir_expression_flattening.cppD03-May-20242.6 KiB8539

ir_expression_flattening.hD03-May-20241.8 KiB445

ir_expression_operation.pyD03-May-202443.2 KiB824575

ir_function.cppD03-May-202413.7 KiB411233

ir_function_can_inline.cppD03-May-20242.4 KiB7630

ir_function_detect_recursion.cppD03-May-202411.5 KiB361157

ir_function_inlining.hD03-May-20241.4 KiB364

ir_hierarchical_visitor.cppD03-May-20249 KiB404298

ir_hierarchical_visitor.hD03-May-20249.1 KiB21767

ir_hv_accept.cppD03-May-202411.8 KiB455317

ir_optimization.hD03-May-20248.7 KiB195140

ir_print_visitor.cppD03-May-202416.4 KiB662522

ir_print_visitor.hD03-May-20243.1 KiB9541

ir_reader.cppD03-May-202434 KiB1,170964

ir_reader.hD03-May-20241.4 KiB346

ir_rvalue_visitor.cppD03-May-20246.8 KiB317239

ir_rvalue_visitor.hD03-May-20243.8 KiB8949

ir_set_program_inouts.cppD03-May-202415 KiB442260

ir_uniform.hD03-May-20246.2 KiB22251

ir_validate.cppD03-May-202437.7 KiB1,217997

ir_variable_refcount.cppD03-May-20244.5 KiB15386

ir_variable_refcount.hD03-May-20242.9 KiB9232

ir_visitor.hD03-May-20243.6 KiB9550

link_atomics.cppD03-May-202412.4 KiB354241

link_functions.cppD03-May-202411.7 KiB340180

link_interface_blocks.cppD03-May-202419.6 KiB539290

link_uniform_block_active_visitor.cppD03-May-202410.2 KiB295174

link_uniform_block_active_visitor.hD03-May-20242.7 KiB8337

link_uniform_blocks.cppD03-May-202420.6 KiB574387

link_uniform_initializers.cppD03-May-202411.1 KiB312227

link_uniforms.cppD03-May-202462.7 KiB1,7681,136

link_varyings.cppD03-May-2024121.8 KiB3,2202,001

link_varyings.hD03-May-20248.2 KiB299119

linker.cppD03-May-2024179.6 KiB4,9883,109

linker.hD03-May-20248.6 KiB21989

linker_util.cppD03-May-202413.5 KiB377231

linker_util.hD03-May-20243.7 KiB11353

list.hD03-May-202421.9 KiB778513

loop_analysis.cppD03-May-202423.8 KiB853516

loop_analysis.hD03-May-20246.4 KiB24593

loop_unroll.cppD03-May-202419 KiB592329

lower_blend_equation_advanced.cppD03-May-202418.4 KiB571366

lower_buffer_access.cppD03-May-202416.9 KiB448276

lower_buffer_access.hD03-May-20242.7 KiB7128

lower_builtins.cppD03-May-20241.9 KiB6827

lower_const_arrays_to_uniforms.cppD03-May-20244.7 KiB15888

lower_cs_derived.cppD03-May-20247.5 KiB236154

lower_discard.cppD03-May-20244.7 KiB20267

lower_discard_flow.cppD03-May-20244.6 KiB15582

lower_distance.cppD03-May-202424.2 KiB686386

lower_if_to_cond_assign.cppD03-May-202410.9 KiB340202

lower_instructions.cppD03-May-202466.9 KiB1,9151,156

lower_int64.cppD03-May-202411.6 KiB392234

lower_jumps.cppD03-May-202438.8 KiB1,037499

lower_mat_op_to_vec.cppD03-May-202412.4 KiB442297

lower_named_interface_blocks.cppD03-May-202410.8 KiB319201

lower_offset_array.cppD03-May-20242.7 KiB9243

lower_output_reads.cppD03-May-20246 KiB18399

lower_packed_varyings.cppD03-May-202436 KiB945533

lower_packing_builtins.cppD03-May-202446.3 KiB1,312473

lower_precision.cppD03-May-202443.5 KiB1,363914

lower_shared_reference.cppD03-May-202417.3 KiB520360

lower_subroutine.cppD03-May-20243.7 KiB12577

lower_tess_level.cppD03-May-202415.8 KiB462237

lower_texture_projection.cppD03-May-20243.1 KiB10451

lower_ubo_reference.cppD03-May-202438 KiB1,145830

lower_variable_index_to_cond_assign.cppD03-May-202418.4 KiB568333

lower_vec_index_to_cond_assign.cppD03-May-20248 KiB241132

lower_vec_index_to_swizzle.cppD03-May-20243.3 KiB10342

lower_vector.cppD03-May-20246.1 KiB229120

lower_vector_derefs.cppD03-May-20247.7 KiB205126

lower_vector_insert.cppD03-May-20245.7 KiB17996

lower_vertex_id.cppD03-May-20244.7 KiB14789

lower_xfb_varying.cppD03-May-20247.2 KiB245164

main.cppD03-May-20243.4 KiB10859

meson.buildD03-May-20248.3 KiB281264

opt_add_neg_to_sub.hD03-May-20242 KiB6227

opt_algebraic.cppD03-May-202432.5 KiB1,062753

opt_array_splitting.cppD03-May-202414.5 KiB506294

opt_conditional_discard.cppD03-May-20242.7 KiB8941

opt_constant_folding.cppD03-May-20246.1 KiB213125

opt_constant_propagation.cppD03-May-202415.2 KiB534354

opt_constant_variable.cppD03-May-20246.9 KiB236138

opt_copy_propagation_elements.cppD03-May-202420.6 KiB746507

opt_dead_builtin_variables.cppD03-May-20243.3 KiB8227

opt_dead_builtin_varyings.cppD03-May-202420.6 KiB621412

opt_dead_code.cppD03-May-20247.1 KiB20486

opt_dead_code_local.cppD03-May-20249.5 KiB359239

opt_dead_functions.cppD03-May-20243.9 KiB15384

opt_flatten_nested_if_blocks.cppD03-May-20242.7 KiB10442

opt_flip_matrices.cppD03-May-20243.9 KiB12470

opt_function_inlining.cppD03-May-202413.3 KiB467292

opt_if_simplification.cppD03-May-20243.7 KiB12855

opt_minmax.cppD03-May-202416 KiB534362

opt_rebalance_tree.cppD03-May-20249.4 KiB338195

opt_redundant_jumps.cppD03-May-20243.6 KiB12562

opt_structure_splitting.cppD03-May-202410.8 KiB378246

opt_swizzle.cppD03-May-20243.3 KiB12067

opt_tree_grafting.cppD03-May-202411.3 KiB420275

opt_vectorize.cppD03-May-202412.4 KiB408216

program.hD03-May-20242 KiB5724

propagate_invariance.cppD03-May-20243.6 KiB12667

s_expression.cppD03-May-20246 KiB221147

s_expression.hD03-May-20244.6 KiB17993

serialize.cppD03-May-202447.8 KiB1,3411,072

serialize.hD03-May-20241.6 KiB5120

shader_cache.cppD03-May-20249.4 KiB265145

shader_cache.hD03-May-20241.5 KiB4112

standalone.cppD03-May-202421.8 KiB624484

standalone.hD03-May-20241.7 KiB5625

standalone_scaffolding.cppD03-May-20248.7 KiB276199

standalone_scaffolding.hD03-May-20243.8 KiB11559

string_to_uint_map.cppD03-May-20241.5 KiB4311

string_to_uint_map.hD03-May-20245.1 KiB17891

test.cppD03-May-20242.4 KiB7933

test_optpass.cppD03-May-20249.8 KiB274208

test_optpass.hD03-May-20241.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 --
159see, for example, ir_to_mesa.cpp's handling of ir_unop_sqrt.  However,
160in many cases you'll want to do a pass over the IR to convert
161non-native instructions to a series of native instructions.  For
162example, for the Mesa backend we have ir_div_to_mul_rcp.cpp because
163Mesa IR (and many hardware backends) only have a reciprocal
164instruction, not a divide.  Implementing non-native instructions this
165way gives the chance for constant folding to occur, so (a / 2.0)
166becomes (a * 0.5) after codegen instead of (a * (1.0 / 2.0))
167
168Q: How shoud I handle my special hardware instructions with respect to IR?
169
170Our current theory is that if multiple targets have an instruction for
171some operation, then we should probably be able to represent that in
172the IR.  Generally this is in the form of an ir_{bin,un}op expression
173type.  For example, we initially implemented fract() using (a -
174floor(a)), but both 945 and 965 have instructions to give that result,
175and it would also simplify the implementation of mod(), so
176ir_unop_fract was added.  The following areas need updating to add a
177new expression type:
178
179ir.h (new enum)
180ir.cpp:operator_strs (used for ir_reader)
181ir_constant_expression.cpp (you probably want to be able to constant fold)
182ir_validate.cpp (check users have the right types)
183
184You may also need to update the backends if they will see the new expr type:
185
186../mesa/program/ir_to_mesa.cpp
187
188You can then use the new expression from builtins (if all backends
189would rather see it), or scan the IR and convert to use your new
190expression type (see ir_mod_to_floor, for example).
191
192Q: How is memory management handled in the compiler?
193
194The hierarchical memory allocator "talloc" developed for the Samba
195project is used, so that things like optimization passes don't have to
196worry about their garbage collection so much.  It has a few nice
197features, including low performance overhead and good debugging
198support that's trivially available.
199
200Generally, each stage of the compile creates a talloc context and
201allocates its memory out of that or children of it.  At the end of the
202stage, the pieces still live are stolen to a new context and the old
203one freed, or the whole context is kept for use by the next stage.
204
205For IR transformations, a temporary context is used, then at the end
206of all transformations, reparent_ir reparents all live nodes under the
207shader's IR list, and the old context full of dead nodes is freed.
208When developing a single IR transformation pass, this means that you
209want to allocate instruction nodes out of the temporary context, so if
210it becomes dead it doesn't live on as the child of a live node.  At
211the moment, optimization passes aren't passed that temporary context,
212so they find it by calling talloc_parent() on a nearby IR node.  The
213talloc_parent() call is expensive, so many passes will cache the
214result of the first talloc_parent().  Cleaning up all the optimization
215passes to take a context argument and not call talloc_parent() is left
216as an exercise.
217
218Q: What is the file naming convention in this directory?
219
220Initially, there really wasn't one.  We have since adopted one:
221
222 - Files that implement code lowering passes should be named lower_*
223   (e.g., lower_builtins.cpp).
224 - Files that implement optimization passes should be named opt_*.
225 - Files that implement a class that is used throught the code should
226   take the name of that class (e.g., ir_hierarchical_visitor.cpp).
227 - Files that contain code not fitting in one of the previous
228   categories should have a sensible name (e.g., glsl_parser.yy).
229