diff options
author | Ian Romanick <ian.d.romanick@intel.com> | 2017-10-09 16:55:54 -0700 |
---|---|---|
committer | Ian Romanick <ian.d.romanick@intel.com> | 2018-03-29 14:16:13 -0700 |
commit | e34eb6142b780a0b66a27ab5f80961099b251ab6 (patch) | |
tree | 05d72a3173d198e0abbc4e3b6fc6852b93240045 | |
parent | f54b50da6d9e86df1caba92f366af0294e0e5ea9 (diff) |
WIP: glsl: Add pass to replace vector-scalar operations with vector-vector operations
-rw-r--r-- | src/compiler/Makefile.sources | 1 | ||||
-rw-r--r-- | src/compiler/glsl/ir_optimization.h | 2 | ||||
-rw-r--r-- | src/compiler/glsl/lower_arithmetic_width.cpp | 100 | ||||
-rw-r--r-- | src/compiler/glsl/meson.build | 1 |
4 files changed, 104 insertions, 0 deletions
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index f5193f6e2ec..7380170f1d1 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -83,6 +83,7 @@ LIBGLSL_FILES = \ glsl/loop_analysis.cpp \ glsl/loop_analysis.h \ glsl/loop_unroll.cpp \ + glsl/lower_arithmetic_width.cpp \ glsl/lower_blend_equation_advanced.cpp \ glsl/lower_buffer_access.cpp \ glsl/lower_buffer_access.h \ diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h index 81049a479e8..bd0c643b903 100644 --- a/src/compiler/glsl/ir_optimization.h +++ b/src/compiler/glsl/ir_optimization.h @@ -180,4 +180,6 @@ ir_variable *compare_index_block(ir_builder::ir_factory &body, bool lower_64bit_integer_instructions(exec_list *instructions, unsigned what_to_lower); +void lower_arithmetic_width(exec_list *instructions); + #endif /* GLSL_IR_OPTIMIZATION_H */ diff --git a/src/compiler/glsl/lower_arithmetic_width.cpp b/src/compiler/glsl/lower_arithmetic_width.cpp new file mode 100644 index 00000000000..5d2028d0ba0 --- /dev/null +++ b/src/compiler/glsl/lower_arithmetic_width.cpp @@ -0,0 +1,100 @@ +/* + * Copyright © 2017 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/** + * \name lower_arithmetic_width.cpp + * + * GLSL IR allows mixing of scalars and either vectors or matrices for some + * arithmetic operations. Vector architectures implement these operations by + * swizzling the scalar to a vector of the correct size. In addition, SPIR-V + * does not allow mixed scalar and vector operations for anything except + * multiplication. + * + * This pass replaces mixed scalar-vector operations with vector-vector + * operations by applying a swizzle to the scalar operand to create a vector + * of matching size. + * + * If the reduce_multiply option is specified, multiplications with a vector + * operand that is effectively a scalar will be converted to a scalar (e.g., + * foo.yyyy becomes foo.y). If the operand is a scalar, the swizzle is + * removed (e.g., scalar_foo.xxx becomes scalar_foo). + */ + +#include "ir.h" +#include "ir_hierarchical_visitor.h" + +class arithmetic_width_visitor : public ir_hierarchical_visitor { +public: + arithmetic_width_visitor() + { + /* empty */ + } + + ir_visitor_status visit_leave(ir_expression *); +}; + +ir_visitor_status +arithmetic_width_visitor::visit_leave(ir_expression *ir) +{ + /* If there's only one operand, there's nothing to mix. */ + if (ir->num_operands == 1) + return visit_continue; + + /* All operations where GLSL IR allows mixing are operations that result in + * a vector. + */ + if (!ir->type->is_vector()) + return visit_continue; + + if (ir->operation == ir_binop_mul && + (ir->type->is_float() || ir->type->is_double())) { + return visit_continue; + } + + const unsigned vector_size = ir->type->vector_elements; + const struct ir_swizzle_mask swiz_mask = { 0, 0, 0, 0, vector_size, true }; + void *const mem_ctx = ralloc_parent(ir); + + for (unsigned i = 0; i < ir->num_operands; ++i) { + ir_rvalue *const src = ir->operands[i]; + + assert(!src->type->is_vector() || + vector_size == src->type->vector_elements); + + if (src->type->is_scalar()) { + ir_swizzle *const swiz = new(mem_ctx) ir_swizzle(src, swiz_mask); + + ir->operands[i] = swiz; + } + } + + return visit_continue; +} + +void +lower_arithmetic_width(exec_list *instructions) +{ + arithmetic_width_visitor v; + + v.run(instructions); +} diff --git a/src/compiler/glsl/meson.build b/src/compiler/glsl/meson.build index 25300c77935..6fb3dcbec1d 100644 --- a/src/compiler/glsl/meson.build +++ b/src/compiler/glsl/meson.build @@ -124,6 +124,7 @@ files_libglsl = files( 'loop_analysis.cpp', 'loop_analysis.h', 'loop_unroll.cpp', + 'lower_arithmetic_width.cpp', 'lower_blend_equation_advanced.cpp', 'lower_buffer_access.cpp', 'lower_buffer_access.h', |