mesa/src/compiler/glsl
Ian Romanick cb518df775 glsl: Add built-in functions for INTEL_shader_integer_functions2
Reviewed-by: Caio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/767>
2020-01-23 00:18:57 +00:00
..
glcpp mesa: add support cursor support for relative path shader includes 2019-11-20 05:05:56 +00:00
tests glsl/tests: Use splitlines() instead of strip() 2019-12-05 12:31:17 +01:00
ast.h glsl: add ARB_shading_language_include support to #line 2019-11-20 05:05:55 +00:00
ast_array_index.cpp glsl/types: Rename is_integer to is_integer_32 2019-06-19 20:28:52 +00:00
ast_expr.cpp glsl: remove tabs from ast_expr.cpp 2016-09-29 11:06:12 +10:00
ast_function.cpp mesa/compiler: rework tear down of builtin/types 2019-08-21 09:44:10 +02:00
ast_to_hir.cpp glsl: Add varyings to "zero-init of uninitialized vars" workaround 2019-11-22 15:25:56 +00:00
ast_type.cpp tree-wide: replace MAYBE_UNUSED with ASSERTED 2019-07-31 09:41:05 +01:00
builtin_functions.cpp glsl: Add built-in functions for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
builtin_functions.h mesa/compiler: rework tear down of builtin/types 2019-08-21 09:44:10 +02:00
builtin_int64.h glsl/int64: Fix a typo in imod64 2017-03-01 15:31:44 -08:00
builtin_types.cpp glsl/builtin types: Set the precision on the depth range params 2019-07-11 08:04:54 +02:00
builtin_variables.cpp glsl: Set .flat for gl_FrontFacing 2019-12-30 19:54:50 +00:00
float64.glsl glsl: Fix software 64-bit integer to 32-bit float conversions. 2020-01-10 10:51:58 -08:00
generate_ir.cpp glsl: Add "built-in" functions to do 64x64 => 64 multiplication 2017-01-20 15:41:23 -08:00
gl_nir.h nir: move gl_nir_opt_access from glsl directory 2019-10-10 15:49:18 -04:00
gl_nir_link_atomics.c glsl: add gl_nir_link_check_atomic_counter_resources() 2020-01-07 08:38:52 +11:00
gl_nir_link_uniform_blocks.c nir/linker: add gl_nir_link_uniform_blocks.c 2019-06-30 16:58:26 -05:00
gl_nir_link_uniform_initializers.c nir/linker: Initialize UniformDataDefaults when using SPIR-V 2019-07-12 23:42:41 +02:00
gl_nir_link_uniforms.c glsl: fix potential bug in nir uniform linker 2020-01-12 11:02:20 +11:00
gl_nir_link_xfb.c nir/xfb: move varyings info out of nir_xfb_info 2019-03-15 11:59:32 +01:00
gl_nir_linker.c glsl: call calculate_subroutine_compat() from the nir linker 2020-01-10 00:41:20 +00:00
gl_nir_linker.h glsl: add new gl_nir_link_glsl() helper 2020-01-07 08:39:16 +11:00
gl_nir_lower_atomics.c nir: rename nir_var_shared to nir_var_mem_shared 2019-01-19 20:01:41 +01:00
gl_nir_lower_bindless_images.c nir: Add explicit signs to image min/max intrinsics 2019-08-21 17:19:55 +00:00
gl_nir_lower_buffers.c glsl/nir: do not change an element index to have correct block name 2020-01-06 13:01:19 -08:00
gl_nir_lower_samplers.c nir: Carve out nir_lower_samplers from GLSL code. 2019-09-06 12:20:20 +03:00
gl_nir_lower_samplers_as_deref.c nir/samplers: don't zero samplers_used/txf. 2019-12-02 09:15:55 +10:00
glsl_lexer.ll glsl: error if #include used while extension is disabled 2019-11-20 05:05:55 +00:00
glsl_parser.yy glsl: add ARB_shading_language_include support to #line 2019-11-20 05:05:55 +00:00
glsl_parser_extras.cpp mesa: Extension boilerplate for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
glsl_parser_extras.h mesa: Extension boilerplate for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
glsl_symbol_table.cpp glsl: use the linear allocator in glsl_symbol_table 2016-10-31 11:53:38 +01:00
glsl_symbol_table.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
glsl_to_nir.cpp nir: Rename nir_intrinsic_barrier to control_barrier 2020-01-13 17:23:47 +00:00
glsl_to_nir.h glsl: use NIR function inlining for drivers that use glsl_to_nir() 2019-03-06 23:05:20 +00:00
hir_field_selection.cpp glsl: rename is_record() -> is_struct() 2019-03-06 13:10:02 +11:00
int64.glsl glsl: Update build instructions for int64.glsl 2017-06-20 17:45:49 -07:00
ir.cpp glsl: Add new expressions for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
ir.h compiler: make variable::data::binding unsigned 2019-11-04 16:49:46 -05:00
ir_array_refcount.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
ir_array_refcount.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_basic_block.cpp
ir_basic_block.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_builder.cpp glsl: Remove ir_binop_greater and ir_binop_lequal expressions 2017-10-30 09:27:09 -07:00
ir_builder.h glsl: Add missing include guards 2018-10-30 06:19:09 -06:00
ir_builder_print_visitor.cpp util: use standard name for snprintf() 2019-07-19 22:39:38 +01:00
ir_builder_print_visitor.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_clone.cpp glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_constant_expression.cpp glsl: Add new expressions for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
ir_equals.cpp glsl: calculate number of operands in an expression once 2017-08-11 10:43:12 +10:00
ir_expression_flattening.cpp glsl: Don't pass NULL to ir_assignment constructor when not necessary 2017-10-02 14:46:02 -07:00
ir_expression_flattening.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_expression_operation.py glsl: Add new expressions for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
ir_function.cpp mesa/glsl: add support for EXT_shader_implicit_conversions 2018-11-02 11:10:36 +01:00
ir_function_can_inline.cpp
ir_function_detect_recursion.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
ir_function_inlining.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_hierarchical_visitor.cpp glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_hierarchical_visitor.h glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_hv_accept.cpp glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_optimization.h glsl: handle max uniform limits with lower_const_arrays_to_uniforms 2019-11-28 14:11:46 +02:00
ir_print_visitor.cpp glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_print_visitor.h glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
ir_reader.cpp glsl: Cross validate variable's invariance by explicit invariance only 2019-03-21 23:28:08 -07:00
ir_reader.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_rvalue_visitor.cpp glsl: calculate number of operands in an expression once 2017-08-11 10:43:12 +10:00
ir_rvalue_visitor.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_set_program_inouts.cpp compiler: Move double_inputs to gl_program::DualSlotInputs 2018-09-06 16:07:50 -05:00
ir_uniform.h mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
ir_validate.cpp glsl: Add new expressions for INTEL_shader_integer_functions2 2020-01-23 00:18:57 +00:00
ir_variable_refcount.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
ir_variable_refcount.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
ir_visitor.h glsl: Add ir_demote 2019-09-30 12:44:30 -07:00
link_atomics.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
link_functions.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
link_interface_blocks.cpp glsl: additional interface redeclaration check for SSO programs 2019-12-04 15:27:41 +00:00
link_uniform_block_active_visitor.cpp glsl: fix a binding points assignment for ssbo/ubo arrays 2020-01-06 13:01:19 -08:00
link_uniform_block_active_visitor.h glsl: fix a binding points assignment for ssbo/ubo arrays 2020-01-06 13:01:19 -08:00
link_uniform_blocks.cpp glsl: fix a binding points assignment for ssbo/ubo arrays 2020-01-06 13:01:19 -08:00
link_uniform_initializers.cpp glsl: fix crash compiling bindless samplers inside unnamed UBOs 2019-10-12 22:04:23 +11:00
link_uniforms.cpp glsl: don't set uniform block as used when its not 2019-12-05 13:18:23 +11:00
link_varyings.cpp glsl/link_varyings: Fix hash table leak 2019-07-12 11:07:08 +03:00
link_varyings.h glsl/linker: check for xfb_offset aliasing 2019-04-29 12:13:29 +02:00
linker.cpp glsl: call calculate_subroutine_compat() from the nir linker 2020-01-10 00:41:20 +00:00
linker.h glsl/linker: Make precision matching optional in intrastage_match 2019-06-14 09:29:53 +02:00
linker_util.cpp glsl: move calculate_subroutine_compat() to shared linker code 2020-01-10 00:41:20 +00:00
linker_util.h glsl: move calculate_subroutine_compat() to shared linker code 2020-01-10 00:41:20 +00:00
list.h glsl/list: Add an exec_list_is_singular() helper. 2019-04-28 22:35:42 -07:00
loop_analysis.cpp glsl: Fix unroll of do{} while(false) like loops 2019-09-06 10:27:33 +00:00
loop_analysis.h src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
loop_unroll.cpp glsl: Fix unroll of do{} while(false) like loops 2019-09-06 10:27:33 +00:00
lower_blend_equation_advanced.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
lower_buffer_access.cpp tree-wide: replace MAYBE_UNUSED with ASSERTED 2019-07-31 09:41:05 +01:00
lower_buffer_access.h glsl: Use link_calculate_matrix_stride in lower_buffer_access and friends 2017-11-08 18:37:29 -08:00
lower_const_arrays_to_uniforms.cpp glsl: handle max uniform limits with lower_const_arrays_to_uniforms 2019-11-28 14:11:46 +02:00
lower_cs_derived.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
lower_discard.cpp glsl: Don't pass NULL to ir_assignment constructor when not necessary 2017-10-02 14:46:02 -07:00
lower_discard_flow.cpp mesa: Remove unused hash table includes 2016-09-12 10:48:35 +10:00
lower_distance.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
lower_if_to_cond_assign.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
lower_instructions.cpp glsl/types: Rename is_integer to is_integer_32 2019-06-19 20:28:52 +00:00
lower_int64.cpp glsl: Remove unused private fields 2017-08-21 14:45:44 -07:00
lower_jumps.cpp glsl: Don't pass NULL to ir_assignment constructor when not necessary 2017-10-02 14:46:02 -07:00
lower_mat_op_to_vec.cpp glsl: calculate number of operands in an expression once 2017-08-11 10:43:12 +10:00
lower_named_interface_blocks.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
lower_noise.cpp
lower_offset_array.cpp
lower_output_reads.cpp glsl/lower_output_reads: set invariant and precise flags on temporaries 2019-01-21 00:16:50 +01:00
lower_packed_varyings.cpp glsl: rename is_record() -> is_struct() 2019-03-06 13:10:02 +11:00
lower_packing_builtins.cpp
lower_shared_reference.cpp glsl/types: Rename is_integer to is_integer_32 2019-06-19 20:28:52 +00:00
lower_subroutine.cpp
lower_tess_level.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
lower_texture_projection.cpp glsl: Don't pass NULL to ir_assignment constructor when not necessary 2017-10-02 14:46:02 -07:00
lower_ubo_reference.cpp glsl/types: Rename is_integer to is_integer_32 2019-06-19 20:28:52 +00:00
lower_variable_index_to_cond_assign.cpp glsl/types: Rename is_integer to is_integer_32 2019-06-19 20:28:52 +00:00
lower_vec_index_to_cond_assign.cpp glsl: fix interpolateAtXxx(some_vec[idx], ...) with dynamic idx 2017-11-03 14:30:08 +01:00
lower_vec_index_to_swizzle.cpp glsl: pass mem_ctx to constant_expression_value(...) and friends 2017-08-11 15:44:08 +10:00
lower_vector.cpp glsl: Mark functions static 2017-08-21 14:45:44 -07:00
lower_vector_derefs.cpp glsl: Don't lower vector derefs for SSBOs, UBOs, and shared 2019-03-15 01:02:19 +00:00
lower_vector_insert.cpp glsl: pass mem_ctx to constant_expression_value(...) and friends 2017-08-11 15:44:08 +10:00
lower_vertex_id.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
main.cpp glsl/freedreno/panfrost: pass gl_context to the standalone compiler 2019-03-06 23:05:20 +00:00
meson.build meson: Add idep_getopt for tests 2019-10-10 16:33:04 -07:00
opt_add_neg_to_sub.h glsl/standalone: Optimize add-of-neg to subtract 2016-11-10 14:30:49 -08:00
opt_algebraic.cpp glsl: replace 'x + (-x)' with constant 0 2019-08-29 17:48:49 -04:00
opt_array_splitting.cpp glsl: Silence unused parameter warnings 2017-09-19 12:02:43 -05:00
opt_conditional_discard.cpp glsl: fix optimization of discard nested multiple levels 2016-07-28 10:47:04 +01:00
opt_constant_folding.cpp glsl: pass mem_ctx to constant_expression_value(...) and friends 2017-08-11 15:44:08 +10:00
opt_constant_propagation.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
opt_constant_variable.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
opt_copy_propagation_elements.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
opt_dead_builtin_variables.cpp glsl: fix derived cs variables 2017-10-23 08:34:56 -04:00
opt_dead_builtin_varyings.cpp glsl: disable lower_fragdata_array() for NIR drivers 2019-11-01 11:33:54 +11:00
opt_dead_code.cpp glsl: don't set uniform block as used when its not 2019-12-05 13:18:23 +11:00
opt_dead_code_local.cpp glsl: Move 'foo = foo;' optimization to opt_dead_code_local 2017-10-02 14:46:11 -07:00
opt_dead_functions.cpp
opt_flatten_nested_if_blocks.cpp glsl: Separate overlapping sentinel nodes in exec_list. 2016-07-26 12:12:27 -07:00
opt_flip_matrices.cpp
opt_function_inlining.cpp src/compiler: use new hash table and set creation helpers 2019-01-14 10:49:28 -08:00
opt_if_simplification.cpp glsl: pass mem_ctx to constant_expression_value(...) and friends 2017-08-11 15:44:08 +10:00
opt_minmax.cpp glsl: fix opt_minmax redundancy checks against baserange 2017-01-07 21:46:36 +11:00
opt_rebalance_tree.cpp
opt_redundant_jumps.cpp
opt_structure_splitting.cpp glsl: rename is_record() -> is_struct() 2019-03-06 13:10:02 +11:00
opt_swizzle.cpp glsl: Combine nop-swizzle optimization with swizzle-swizzle optimization 2017-11-08 18:37:29 -08:00
opt_tree_grafting.cpp glsl: calculate number of operands in an expression once 2017-08-11 10:43:12 +10:00
opt_vectorize.cpp
program.h st/glsl_to_nir: use nir based program resource list builder 2019-12-13 00:07:19 +00:00
propagate_invariance.cpp glsl: Silence unused parameter warning in propagate_invariance.cpp 2016-12-19 15:55:44 -08:00
README
s_expression.cpp mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
s_expression.h mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
serialize.cpp glsl/serialize: optimize for equal offsets in uniform remap tables 2019-10-25 17:01:26 -04:00
serialize.h glsl: Split out shader program serialization 2017-12-08 16:44:35 +11:00
shader_cache.cpp glsl/shader_cache: handle SPIR-V shaders 2019-07-12 23:42:41 +02:00
shader_cache.h mesa: include mtypes.h less 2018-04-12 19:31:30 -04:00
standalone.cpp mesa/compiler: rework tear down of builtin/types 2019-08-21 09:44:10 +02:00
standalone.h glsl/freedreno/panfrost: pass gl_context to the standalone compiler 2019-03-06 23:05:20 +00:00
standalone_scaffolding.cpp glsl: add scaffolding for EXT_gpu_shader4 2019-04-24 20:45:15 -04:00
standalone_scaffolding.h glsl/nir: Add a shared helper for building float64 shaders 2019-03-06 17:24:57 +00:00
string_to_uint_map.cpp util: move string_to_uint_map to glsl 2017-08-29 13:40:44 +01:00
string_to_uint_map.h util: move string_to_uint_map to glsl 2017-08-29 13:40:44 +01:00
test.cpp
test_optpass.cpp glsl/test: Fix use after free in test_optpass. 2018-11-12 07:42:58 +02:00
test_optpass.h glsl: consistently use ifndef guards over pragma once 2017-03-22 16:55:22 +00:00
TODO
xxd.py glsl: Add utility to convert text files to C strings 2019-01-09 16:42:40 -08:00

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_noise.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).