mesa/src/compiler/glsl
Eric Anholt ee4cee6dbd android: Disable trying to read/write to the disk cache.
We need the disk cache enabled in Android to get EGL_ANDROID_blob_cache's
callbacks called, but we don't actually want to store anything on disk.
Fixes "Failed to create //.cache for shader cache (Read-only file
system)---disabling." spam on init.

Reviewed-by: Tapani Pälli <tapani.palli@intel.com>
Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6762>
2020-09-22 20:41:25 +00:00
..
glcpp glsl/test: Don't run whitespace tests in parallel 2020-08-17 17:44:26 +00:00
tests android: Disable trying to read/write to the disk cache. 2020-09-22 20:41:25 +00:00
ast.h glsl: add NV_viewport_array2 support 2020-04-15 20:12:00 -04:00
ast_array_index.cpp
ast_expr.cpp
ast_function.cpp glsl: only allow 32 bits atomic operations on images 2020-07-09 09:58:01 +02:00
ast_to_hir.cpp compiler/glsl: Initialize local variable to zero to fix MSVC RTC error 2020-08-17 09:31:37 +00:00
ast_type.cpp
builtin_functions.cpp glsl: take EXT_gpu_shader4 in to account when adding round 2020-09-18 05:49:51 +00:00
builtin_functions.h
builtin_int64.h
builtin_types.cpp glsl: define gl_LightSource members in ARB_vertex_program order 2020-06-30 01:29:43 +00:00
builtin_variables.cpp glsl: fix per_vertex_accumulator::fields size 2020-09-14 15:24:32 +02:00
float64.glsl nir: rename nir_op_fne to nir_op_fneu 2020-08-21 17:26:21 +00:00
generate_ir.cpp
gl_nir.h
gl_nir_link_atomics.c nir: Add a nir_foreach_uniform_variable helper 2020-07-29 17:38:58 +00:00
gl_nir_link_uniform_blocks.c nir: Use a single list for all shader variables 2020-07-29 17:38:58 +00:00
gl_nir_link_uniform_initializers.c nir: Add a nir_foreach_gl_uniform_variable helper for GL linking 2020-07-29 17:38:58 +00:00
gl_nir_link_uniforms.c nir: Add a nir_foreach_gl_uniform_variable helper for GL linking 2020-07-29 17:38:58 +00:00
gl_nir_link_xfb.c
gl_nir_linker.c nir/gl_nir_linker: Call add_vars_with_modes once for GL_PROGRAM_INPUT 2020-07-29 17:38:58 +00:00
gl_nir_linker.h nir: Use a single list for all shader variables 2020-07-29 17:38:58 +00:00
gl_nir_lower_atomics.c
gl_nir_lower_buffers.c
gl_nir_lower_images.c nir/lower_images: handle dec and inc 2020-06-18 15:15:17 +00:00
gl_nir_lower_samplers.c
gl_nir_lower_samplers_as_deref.c nir: Properly preserve metadata in more cases 2020-06-11 05:08:12 +00:00
glsl_lexer.ll
glsl_parser.yy mesa: do not use bitfields for advanced-blend state 2020-07-17 06:19:16 +00:00
glsl_parser_extras.cpp glsl: don't lower to mediump for desktop OpenGL 2020-08-05 22:04:47 +00:00
glsl_parser_extras.h glsl,driconf: add allow_glsl_120_subset_in_110 for SPECviewperf13 2020-06-23 09:25:24 +00:00
glsl_symbol_table.cpp
glsl_symbol_table.h
glsl_to_nir.cpp nir: Rename get_buffer_size to get_ssbo_size 2020-09-22 13:34:12 +00:00
glsl_to_nir.h
hir_field_selection.cpp
int64.glsl
ir.cpp glsl: Initialize ir_constant member const_elements in all constructors. 2020-09-21 18:29:46 -07:00
ir.h compiler: add glsl_print_type 2020-08-17 11:06:49 +00:00
ir_array_refcount.cpp glsl: pull mark_array_elements_referenced() out into common helper 2020-04-18 11:50:44 +00:00
ir_array_refcount.h glsl: pull mark_array_elements_referenced() out into common helper 2020-04-18 11:50:44 +00:00
ir_basic_block.cpp
ir_basic_block.h
ir_builder.cpp
ir_builder.h
ir_builder_print_visitor.cpp
ir_builder_print_visitor.h
ir_clone.cpp
ir_constant_expression.cpp glsl: fix crashes on out of bound matrix access using constant index 2020-08-18 11:34:54 +00:00
ir_equals.cpp
ir_expression_flattening.cpp
ir_expression_flattening.h
ir_expression_operation.py glsl: fix constant expression evaluation for 16-bit types 2020-08-05 22:04:47 +00:00
ir_function.cpp glsl: subroutine signatures must match exactly 2020-05-24 23:55:44 +00:00
ir_function_can_inline.cpp
ir_function_detect_recursion.cpp
ir_function_inlining.h
ir_hierarchical_visitor.cpp
ir_hierarchical_visitor.h
ir_hv_accept.cpp
ir_optimization.h glsl: remove the return type from lower_precision 2020-07-07 22:02:06 -04:00
ir_print_visitor.cpp compiler: add INTERP_MODE_COLOR for radeonsi 2020-09-02 23:03:00 -04:00
ir_print_visitor.h
ir_reader.cpp
ir_reader.h
ir_rvalue_visitor.cpp
ir_rvalue_visitor.h
ir_set_program_inouts.cpp
ir_uniform.h
ir_validate.cpp glsl: don't validate array types in ir_dereference_variable 2020-07-08 23:22:17 +00:00
ir_variable_refcount.cpp
ir_variable_refcount.h
ir_visitor.h
link_atomics.cpp
link_functions.cpp
link_interface_blocks.cpp
link_uniform_block_active_visitor.cpp
link_uniform_block_active_visitor.h
link_uniform_blocks.cpp
link_uniform_initializers.cpp
link_uniforms.cpp
link_varyings.cpp
link_varyings.h
linker.cpp st/mesa: Replace UsesStreams by ActiveStreamMask for GS 2020-08-18 11:17:26 +00:00
linker.h
linker_util.cpp glsl: pull mark_array_elements_referenced() out into common helper 2020-04-18 11:50:44 +00:00
linker_util.h glsl: pull mark_array_elements_referenced() out into common helper 2020-04-18 11:50:44 +00:00
list.h glsl/list: Fix undefined behaviour of foreach_* macros 2020-04-14 19:29:38 +00:00
loop_analysis.cpp glsl: Handle 16-bit types in loop analysis 2020-07-07 22:02:06 -04:00
loop_analysis.h
loop_unroll.cpp
lower_blend_equation_advanced.cpp mesa: do not use bitfields for advanced-blend state 2020-07-17 06:19:16 +00:00
lower_buffer_access.cpp
lower_buffer_access.h
lower_builtins.cpp glsl: lower the precision of imageLoad 2020-06-02 20:01:18 +00:00
lower_const_arrays_to_uniforms.cpp
lower_cs_derived.cpp
lower_discard.cpp
lower_discard_flow.cpp
lower_distance.cpp
lower_if_to_cond_assign.cpp
lower_instructions.cpp
lower_int64.cpp
lower_jumps.cpp
lower_mat_op_to_vec.cpp
lower_named_interface_blocks.cpp
lower_offset_array.cpp
lower_output_reads.cpp
lower_packed_varyings.cpp
lower_packing_builtins.cpp
lower_precision.cpp glsl: improve precision determination for calls 2020-08-05 22:04:47 +00:00
lower_shared_reference.cpp
lower_subroutine.cpp
lower_tess_level.cpp
lower_texture_projection.cpp
lower_ubo_reference.cpp
lower_variable_index_to_cond_assign.cpp
lower_vec_index_to_cond_assign.cpp
lower_vec_index_to_swizzle.cpp
lower_vector.cpp
lower_vector_derefs.cpp glsl: Eliminate assigments to out-of-bounds elements of vector 2020-08-21 15:00:51 +00:00
lower_vector_insert.cpp glsl: Eliminate out-of-bounds triop_vector_insert 2020-08-21 15:00:51 +00:00
lower_vertex_id.cpp
lower_xfb_varying.cpp
main.cpp
meson.build meson: use gnu_symbol_visibility argument 2020-06-01 18:59:18 +00:00
opt_add_neg_to_sub.h
opt_algebraic.cpp glsl: Don't replace lrp pattern with lrp if arguments are not floats 2020-06-03 09:06:25 +00:00
opt_array_splitting.cpp
opt_conditional_discard.cpp
opt_constant_folding.cpp
opt_constant_propagation.cpp glsl: handle int16 and uint16 types and add instructions for mediump 2020-06-02 20:01:18 +00:00
opt_constant_variable.cpp glsl: fix incorrect optimisation in opt_constant_variable() 2020-06-12 08:51:54 +00:00
opt_copy_propagation_elements.cpp
opt_dead_builtin_variables.cpp
opt_dead_builtin_varyings.cpp
opt_dead_code.cpp
opt_dead_code_local.cpp
opt_dead_functions.cpp
opt_flatten_nested_if_blocks.cpp
opt_flip_matrices.cpp
opt_function_inlining.cpp
opt_if_simplification.cpp
opt_minmax.cpp glsl: handle int16 and uint16 types and add instructions for mediump 2020-06-02 20:01:18 +00:00
opt_rebalance_tree.cpp
opt_redundant_jumps.cpp
opt_structure_splitting.cpp
opt_swizzle.cpp
opt_tree_grafting.cpp
opt_vectorize.cpp
program.h
propagate_invariance.cpp
README glsl: Hard-code noise to zero in builtin_functions.cpp 2020-04-21 06:16:13 +00:00
s_expression.cpp
s_expression.h
serialize.cpp compiler/glsl: explicitly store NumUniformBlocks 2020-05-05 09:17:51 +00:00
serialize.h
shader_cache.cpp
shader_cache.h
standalone.cpp glsl: remove LowerPrecisionTemporaries 2020-08-05 21:00:44 +00:00
standalone.h
standalone_scaffolding.cpp
standalone_scaffolding.h
string_to_uint_map.cpp
string_to_uint_map.h
test.cpp
test_optpass.cpp glsl: Hard-code noise to zero in builtin_functions.cpp 2020-04-21 06:16:13 +00:00
test_optpass.h
TODO
xxd.py

Welcome to Mesa's GLSL compiler.  A brief overview of how things flow:

1) lex and yacc-based preprocessor takes the incoming shader string
and produces a new string containing the preprocessed shader.  This
takes care of things like #if, #ifdef, #define, and preprocessor macro
invocations.  Note that #version, #extension, and some others are
passed straight through.  See glcpp/*

2) lex and yacc-based parser takes the preprocessed string and
generates the AST (abstract syntax tree).  Almost no checking is
performed in this stage.  See glsl_lexer.ll and glsl_parser.yy.

3) The AST is converted to "HIR".  This is the intermediate
representation of the compiler.  Constructors are generated, function
calls are resolved to particular function signatures, and all the
semantic checking is performed.  See ast_*.cpp for the conversion, and
ir.h for the IR structures.

4) The driver (Mesa, or main.cpp for the standalone binary) performs
optimizations.  These include copy propagation, dead code elimination,
constant folding, and others.  Generally the driver will call
optimizations in a loop, as each may open up opportunities for other
optimizations to do additional work.  See most files called ir_*.cpp

5) linking is performed.  This does checking to ensure that the
outputs of the vertex shader match the inputs of the fragment shader,
and assigns locations to uniforms, attributes, and varyings.  See
linker.cpp.

6) The driver may perform additional optimization at this point, as
for example dead code elimination previously couldn't remove functions
or global variable usage when we didn't know what other code would be
linked in.

7) The driver performs code generation out of the IR, taking a linked
shader program and producing a compiled program for each stage.  See
../mesa/program/ir_to_mesa.cpp for Mesa IR code generation.

FAQ:

Q: What is HIR versus IR versus LIR?

A: The idea behind the naming was that ast_to_hir would produce a
high-level IR ("HIR"), with things like matrix operations, structure
assignments, etc., present.  A series of lowering passes would occur
that do things like break matrix multiplication into a series of dot
products/MADs, make structure assignment be a series of assignment of
components, flatten if statements into conditional moves, and such,
producing a low level IR ("LIR").

However, it now appears that each driver will have different
requirements from a LIR.  A 915-generation chipset wants all functions
inlined, all loops unrolled, all ifs flattened, no variable array
accesses, and matrix multiplication broken down.  The Mesa IR backend
for swrast would like matrices and structure assignment broken down,
but it can support function calls and dynamic branching.  A 965 vertex
shader IR backend could potentially even handle some matrix operations
without breaking them down, but the 965 fragment shader IR backend
would want to break to have (almost) all operations down channel-wise
and perform optimization on that.  As a result, there's no single
low-level IR that will make everyone happy.  So that usage has fallen
out of favor, and each driver will perform a series of lowering passes
to take the HIR down to whatever restrictions it wants to impose
before doing codegen.

Q: How is the IR structured?

A: The best way to get started seeing it would be to run the
standalone compiler against a shader:

./glsl_compiler --dump-lir \
	~/src/piglit/tests/shaders/glsl-orangebook-ch06-bump.frag

So for example one of the ir_instructions in main() contains:

(assign (constant bool (1)) (var_ref litColor)  (expression vec3 * (var_ref Surf
aceColor) (var_ref __retval) ) )

Or more visually:
                     (assign)
                 /       |        \
        (var_ref)  (expression *)  (constant bool 1)
         /          /           \
(litColor)      (var_ref)    (var_ref)
                  /                  \
           (SurfaceColor)          (__retval)

which came from:

litColor = SurfaceColor * max(dot(normDelta, LightDir), 0.0);

(the max call is not represented in this expression tree, as it was a
function call that got inlined but not brought into this expression
tree)

Each of those nodes is a subclass of ir_instruction.  A particular
ir_instruction instance may only appear once in the whole IR tree with
the exception of ir_variables, which appear once as variable
declarations:

(declare () vec3 normDelta)

and multiple times as the targets of variable dereferences:
...
(assign (constant bool (1)) (var_ref __retval) (expression float dot
 (var_ref normDelta) (var_ref LightDir) ) )
...
(assign (constant bool (1)) (var_ref __retval) (expression vec3 -
 (var_ref LightDir) (expression vec3 * (constant float (2.000000))
 (expression vec3 * (expression float dot (var_ref normDelta) (var_ref
 LightDir) ) (var_ref normDelta) ) ) ) )
...

Each node has a type.  Expressions may involve several different types:
(declare (uniform ) mat4 gl_ModelViewMatrix)
((assign (constant bool (1)) (var_ref constructor_tmp) (expression
 vec4 * (var_ref gl_ModelViewMatrix) (var_ref gl_Vertex) ) )

An expression tree can be arbitrarily deep, and the compiler tries to
keep them structured like that so that things like algebraic
optimizations ((color * 1.0 == color) and ((mat1 * mat2) * vec == mat1
* (mat2 * vec))) or recognizing operation patterns for code generation
(vec1 * vec2 + vec3 == mad(vec1, vec2, vec3)) are easier.  This comes
at the expense of additional trickery in implementing some
optimizations like CSE where one must navigate an expression tree.

Q: Why no SSA representation?

A: Converting an IR tree to SSA form makes dead code elimination,
common subexpression elimination, and many other optimizations much
easier.  However, in our primarily vector-based language, there's some
major questions as to how it would work.  Do we do SSA on the scalar
or vector level?  If we do it at the vector level, we're going to end
up with many different versions of the variable when encountering code
like:

(assign (constant bool (1)) (swiz x (var_ref __retval) ) (var_ref a) )
(assign (constant bool (1)) (swiz y (var_ref __retval) ) (var_ref b) )
(assign (constant bool (1)) (swiz z (var_ref __retval) ) (var_ref c) )

If every masked update of a component relies on the previous value of
the variable, then we're probably going to be quite limited in our
dead code elimination wins, and recognizing common expressions may
just not happen.  On the other hand, if we operate channel-wise, then
we'll be prone to optimizing the operation on one of the channels at
the expense of making its instruction flow different from the other
channels, and a vector-based GPU would end up with worse code than if
we didn't optimize operations on that channel!

Once again, it appears that our optimization requirements are driven
significantly by the target architecture.  For now, targeting the Mesa
IR backend, SSA does not appear to be that important to producing
excellent code, but we do expect to do some SSA-based optimizations
for the 965 fragment shader backend when that is developed.

Q: How should I expand instructions that take multiple backend instructions?

Sometimes you'll have to do the expansion in your code generation --
see, for example, ir_to_mesa.cpp's handling of ir_unop_sqrt.  However,
in many cases you'll want to do a pass over the IR to convert
non-native instructions to a series of native instructions.  For
example, for the Mesa backend we have ir_div_to_mul_rcp.cpp because
Mesa IR (and many hardware backends) only have a reciprocal
instruction, not a divide.  Implementing non-native instructions this
way gives the chance for constant folding to occur, so (a / 2.0)
becomes (a * 0.5) after codegen instead of (a * (1.0 / 2.0))

Q: How shoud I handle my special hardware instructions with respect to IR?

Our current theory is that if multiple targets have an instruction for
some operation, then we should probably be able to represent that in
the IR.  Generally this is in the form of an ir_{bin,un}op expression
type.  For example, we initially implemented fract() using (a -
floor(a)), but both 945 and 965 have instructions to give that result,
and it would also simplify the implementation of mod(), so
ir_unop_fract was added.  The following areas need updating to add a
new expression type:

ir.h (new enum)
ir.cpp:operator_strs (used for ir_reader)
ir_constant_expression.cpp (you probably want to be able to constant fold)
ir_validate.cpp (check users have the right types)

You may also need to update the backends if they will see the new expr type:

../mesa/program/ir_to_mesa.cpp

You can then use the new expression from builtins (if all backends
would rather see it), or scan the IR and convert to use your new
expression type (see ir_mod_to_floor, for example).

Q: How is memory management handled in the compiler?

The hierarchical memory allocator "talloc" developed for the Samba
project is used, so that things like optimization passes don't have to
worry about their garbage collection so much.  It has a few nice
features, including low performance overhead and good debugging
support that's trivially available.

Generally, each stage of the compile creates a talloc context and
allocates its memory out of that or children of it.  At the end of the
stage, the pieces still live are stolen to a new context and the old
one freed, or the whole context is kept for use by the next stage.

For IR transformations, a temporary context is used, then at the end
of all transformations, reparent_ir reparents all live nodes under the
shader's IR list, and the old context full of dead nodes is freed.
When developing a single IR transformation pass, this means that you
want to allocate instruction nodes out of the temporary context, so if
it becomes dead it doesn't live on as the child of a live node.  At
the moment, optimization passes aren't passed that temporary context,
so they find it by calling talloc_parent() on a nearby IR node.  The
talloc_parent() call is expensive, so many passes will cache the
result of the first talloc_parent().  Cleaning up all the optimization
passes to take a context argument and not call talloc_parent() is left
as an exercise.

Q: What is the file naming convention in this directory?

Initially, there really wasn't one.  We have since adopted one:

 - Files that implement code lowering passes should be named lower_*
   (e.g., lower_builtins.cpp).
 - Files that implement optimization passes should be named opt_*.
 - Files that implement a class that is used throught the code should
   take the name of that class (e.g., ir_hierarchical_visitor.cpp).
 - Files that contain code not fitting in one of the previous
   categories should have a sensible name (e.g., glsl_parser.yy).