aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/glsl
diff options
context:
space:
mode:
Diffstat (limited to 'mesalib/src/glsl')
-rw-r--r--mesalib/src/glsl/Android.gen.mk66
-rw-r--r--mesalib/src/glsl/Android.mk13
-rw-r--r--mesalib/src/glsl/ast_function.cpp2
-rw-r--r--mesalib/src/glsl/ast_to_hir.cpp4
-rw-r--r--mesalib/src/glsl/glcpp/glcpp-parse.y2
-rw-r--r--mesalib/src/glsl/glsl_types.cpp23
-rw-r--r--mesalib/src/glsl/glsl_types.h7
-rw-r--r--mesalib/src/glsl/link_uniforms.cpp2
-rw-r--r--mesalib/src/glsl/linker.cpp3
-rw-r--r--mesalib/src/glsl/lower_packed_varyings.cpp8
-rw-r--r--mesalib/src/glsl/lower_packing_builtins.cpp8
-rw-r--r--mesalib/src/glsl/nir/nir.c112
-rw-r--r--mesalib/src/glsl/nir/nir.h17
-rw-r--r--mesalib/src/glsl/nir/nir_array.h96
-rw-r--r--mesalib/src/glsl/nir/nir_from_ssa.c2
-rw-r--r--mesalib/src/glsl/nir/nir_lower_locals_to_regs.c148
-rw-r--r--mesalib/src/glsl/nir/nir_lower_to_source_mods.c7
-rw-r--r--mesalib/src/glsl/nir/nir_lower_var_copies.c24
-rw-r--r--mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c120
-rw-r--r--mesalib/src/glsl/nir/nir_opt_algebraic.py1
-rw-r--r--mesalib/src/glsl/nir/nir_print.c2
-rw-r--r--mesalib/src/glsl/nir/nir_types.cpp2
-rw-r--r--mesalib/src/glsl/opt_algebraic.cpp20
-rw-r--r--mesalib/src/glsl/opt_dead_builtin_varyings.cpp8
-rw-r--r--mesalib/src/glsl/opt_function_inlining.cpp2
-rw-r--r--mesalib/src/glsl/standalone_scaffolding.cpp3
26 files changed, 485 insertions, 217 deletions
diff --git a/mesalib/src/glsl/Android.gen.mk b/mesalib/src/glsl/Android.gen.mk
index 7ec56d4f2..0835871f9 100644
--- a/mesalib/src/glsl/Android.gen.mk
+++ b/mesalib/src/glsl/Android.gen.mk
@@ -27,17 +27,29 @@ ifeq ($(LOCAL_MODULE_CLASS),)
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
endif
-intermediates := $(call local-intermediates-dir)
+intermediates := $(call local-generated-sources-dir)
sources := \
glsl_lexer.cpp \
glsl_parser.cpp \
glcpp/glcpp-lex.c \
- glcpp/glcpp-parse.c
+ glcpp/glcpp-parse.c \
+ nir/nir_builder_opcodes.h \
+ nir/nir_constant_expressions.c \
+ nir/nir_opcodes.c \
+ nir/nir_opcodes.h \
+ nir/nir_opt_algebraic.c
LOCAL_SRC_FILES := $(filter-out $(sources), $(LOCAL_SRC_FILES))
-LOCAL_C_INCLUDES += $(intermediates) $(intermediates)/glcpp $(MESA_TOP)/src/glsl/glcpp
+LOCAL_C_INCLUDES += \
+ $(intermediates)/glcpp \
+ $(intermediates)/nir \
+ $(MESA_TOP)/src/glsl/glcpp \
+ $(MESA_TOP)/src/glsl/nir
+
+LOCAL_EXPORT_C_INCLUDE_DIRS += \
+ $(intermediates)/nir
sources := $(addprefix $(intermediates)/, $(sources))
LOCAL_GENERATED_SOURCES += $(sources)
@@ -77,3 +89,51 @@ $(intermediates)/glcpp/glcpp-lex.c: $(LOCAL_PATH)/glcpp/glcpp-lex.l
$(intermediates)/glcpp/glcpp-parse.c: $(LOCAL_PATH)/glcpp/glcpp-parse.y
$(call glsl_local-y-to-c-and-h)
+
+nir_builder_opcodes_gen := $(LOCAL_PATH)/nir/nir_builder_opcodes_h.py
+nir_builder_opcodes_deps := \
+ $(LOCAL_PATH)/nir/nir_opcodes.py \
+ $(LOCAL_PATH)/nir/nir_builder_opcodes_h.py
+
+$(intermediates)/nir/nir_builder_opcodes.h: $(nir_builder_opcodes_deps)
+ @mkdir -p $(dir $@)
+ $(hide) $(MESA_PYTHON2) $(nir_builder_opcodes_gen) $< > $@
+
+nir_constant_expressions_gen := $(LOCAL_PATH)/nir/nir_constant_expressions.py
+nir_constant_expressions_deps := \
+ $(LOCAL_PATH)/nir/nir_opcodes.py \
+ $(LOCAL_PATH)/nir/nir_constant_expressions.py \
+ $(LOCAL_PATH)/nir/nir_constant_expressions.h
+
+$(intermediates)/nir/nir_constant_expressions.c: $(nir_constant_expressions_deps)
+ @mkdir -p $(dir $@)
+ $(hide) $(MESA_PYTHON2) $(nir_constant_expressions_gen) $< > $@
+
+nir_opcodes_h_gen := $(LOCAL_PATH)/nir/nir_opcodes_h.py
+nir_opcodes_h_deps := \
+ $(LOCAL_PATH)/nir/nir_opcodes.py \
+ $(LOCAL_PATH)/nir/nir_opcodes_h.py
+
+$(intermediates)/nir/nir_opcodes.h: $(nir_opcodes_h_deps)
+ @mkdir -p $(dir $@)
+ $(hide) $(MESA_PYTHON2) $(nir_opcodes_h_gen) $< > $@
+
+$(LOCAL_PATH)/nir/nir.h: $(intermediates)/nir/nir_opcodes.h
+
+nir_opcodes_c_gen := $(LOCAL_PATH)/nir/nir_opcodes_c.py
+nir_opcodes_c_deps := \
+ $(LOCAL_PATH)/nir/nir_opcodes.py \
+ $(LOCAL_PATH)/nir/nir_opcodes_c.py
+
+$(intermediates)/nir/nir_opcodes.c: $(nir_opcodes_c_deps)
+ @mkdir -p $(dir $@)
+ $(hide) $(MESA_PYTHON2) $(nir_opcodes_c_gen) $< > $@
+
+nir_opt_algebraic_gen := $(LOCAL_PATH)/nir/nir_opt_algebraic.py
+nir_opt_algebraic_deps := \
+ $(LOCAL_PATH)/nir/nir_opt_algebraic.py \
+ $(LOCAL_PATH)/nir/nir_algebraic.py
+
+$(intermediates)/nir/nir_opt_algebraic.c: $(nir_opt_algebraic_deps)
+ @mkdir -p $(dir $@)
+ $(hide) $(MESA_PYTHON2) $(nir_opt_algebraic_gen) $< > $@
diff --git a/mesalib/src/glsl/Android.mk b/mesalib/src/glsl/Android.mk
index 38c2087a4..f20741e0d 100644
--- a/mesalib/src/glsl/Android.mk
+++ b/mesalib/src/glsl/Android.mk
@@ -35,12 +35,14 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
$(LIBGLCPP_FILES) \
- $(LIBGLSL_FILES)
+ $(LIBGLSL_FILES) \
+ $(NIR_FILES)
LOCAL_C_INCLUDES := \
- $(MESA_TOP)/src \
$(MESA_TOP)/src/mapi \
- $(MESA_TOP)/src/mesa
+ $(MESA_TOP)/src/mesa \
+ $(MESA_TOP)/src/gallium/include \
+ $(MESA_TOP)/src/gallium/auxiliary
LOCAL_MODULE := libmesa_glsl
@@ -59,9 +61,10 @@ LOCAL_SRC_FILES := \
$(GLSL_COMPILER_CXX_FILES)
LOCAL_C_INCLUDES := \
- $(MESA_TOP)/src \
$(MESA_TOP)/src/mapi \
- $(MESA_TOP)/src/mesa
+ $(MESA_TOP)/src/mesa \
+ $(MESA_TOP)/src/gallium/include \
+ $(MESA_TOP)/src/gallium/auxiliary
LOCAL_STATIC_LIBRARIES := libmesa_glsl libmesa_glsl_utils libmesa_util
diff --git a/mesalib/src/glsl/ast_function.cpp b/mesalib/src/glsl/ast_function.cpp
index 87df93e68..758361324 100644
--- a/mesalib/src/glsl/ast_function.cpp
+++ b/mesalib/src/glsl/ast_function.cpp
@@ -1191,7 +1191,7 @@ emit_inline_matrix_constructor(const glsl_type *type,
*
* - Construct a matrix from an arbirary combination of vectors and
* scalars. The components of the constructor parameters are assigned
- * to the matrix in colum-major order until the matrix is full.
+ * to the matrix in column-major order until the matrix is full.
*
* - Construct a matrix from a single matrix. The source matrix is copied
* to the upper left portion of the constructed matrix, and the remaining
diff --git a/mesalib/src/glsl/ast_to_hir.cpp b/mesalib/src/glsl/ast_to_hir.cpp
index 78369360f..18b82e3be 100644
--- a/mesalib/src/glsl/ast_to_hir.cpp
+++ b/mesalib/src/glsl/ast_to_hir.cpp
@@ -3532,9 +3532,7 @@ ast_declarator_list::hir(exec_list *instructions,
* vectors. Vertex shader inputs cannot be arrays or
* structures."
*/
- const glsl_type *check_type = var->type;
- while (check_type->is_array())
- check_type = check_type->element_type();
+ const glsl_type *check_type = var->type->without_array();
switch (check_type->base_type) {
case GLSL_TYPE_FLOAT:
diff --git a/mesalib/src/glsl/glcpp/glcpp-parse.y b/mesalib/src/glsl/glcpp/glcpp-parse.y
index c2f5223a9..cfceca66b 100644
--- a/mesalib/src/glsl/glcpp/glcpp-parse.y
+++ b/mesalib/src/glsl/glcpp/glcpp-parse.y
@@ -1463,7 +1463,7 @@ _arguments_parse (argument_list_t *arguments,
else {
if (argument->head == NULL) {
/* Don't treat initial whitespace as
- * part of the arguement. */
+ * part of the argument. */
if (node->token->type == SPACE)
continue;
}
diff --git a/mesalib/src/glsl/glsl_types.cpp b/mesalib/src/glsl/glsl_types.cpp
index 4aa36a794..9c9b7efcb 100644
--- a/mesalib/src/glsl/glsl_types.cpp
+++ b/mesalib/src/glsl/glsl_types.cpp
@@ -738,24 +738,27 @@ glsl_type::record_key_compare(const void *a, const void *b)
}
+/**
+ * Generate an integer hash value for a glsl_type structure type.
+ */
unsigned
glsl_type::record_key_hash(const void *a)
{
const glsl_type *const key = (glsl_type *) a;
- char hash_key[128];
- unsigned size = 0;
-
- size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
+ uintptr_t hash = key->length;
+ unsigned retval;
for (unsigned i = 0; i < key->length; i++) {
- if (size >= sizeof(hash_key))
- break;
-
- size += snprintf(& hash_key[size], sizeof(hash_key) - size,
- "%p", (void *) key->fields.structure[i].type);
+ /* casting pointer to uintptr_t */
+ hash = (hash * 13 ) + (uintptr_t) key->fields.structure[i].type;
}
- return hash_table_string_hash(& hash_key);
+ if (sizeof(hash) == 8)
+ retval = (hash & 0xffffffff) ^ ((uint64_t) hash >> 32);
+ else
+ retval = hash;
+
+ return retval;
}
diff --git a/mesalib/src/glsl/glsl_types.h b/mesalib/src/glsl/glsl_types.h
index d383dd5be..5645dcd50 100644
--- a/mesalib/src/glsl/glsl_types.h
+++ b/mesalib/src/glsl/glsl_types.h
@@ -540,7 +540,12 @@ struct glsl_type {
*/
const glsl_type *without_array() const
{
- return this->is_array() ? this->fields.array : this;
+ const glsl_type *t = this;
+
+ while (t->is_array())
+ t = t->fields.array;
+
+ return t;
}
/**
diff --git a/mesalib/src/glsl/link_uniforms.cpp b/mesalib/src/glsl/link_uniforms.cpp
index 51b939098..9a297299d 100644
--- a/mesalib/src/glsl/link_uniforms.cpp
+++ b/mesalib/src/glsl/link_uniforms.cpp
@@ -274,7 +274,7 @@ namespace {
*
* As uniforms are added to the active set the number of active uniforms and
* the storage requirements for those uniforms are accumulated. The active
- * uniforms are added the the hash table supplied to the constructor.
+ * uniforms are added to the hash table supplied to the constructor.
*
* If the same uniform is added multiple times (i.e., once for each shader
* target), it will only be accounted once.
diff --git a/mesalib/src/glsl/linker.cpp b/mesalib/src/glsl/linker.cpp
index b6baa5d36..21fde9444 100644
--- a/mesalib/src/glsl/linker.cpp
+++ b/mesalib/src/glsl/linker.cpp
@@ -2570,7 +2570,8 @@ add_interface_variables(struct gl_shader_program *shProg,
if (var->data.location != SYSTEM_VALUE_VERTEX_ID &&
var->data.location != SYSTEM_VALUE_VERTEX_ID_ZERO_BASE &&
var->data.location != SYSTEM_VALUE_INSTANCE_ID)
- continue;
+ continue;
+ /* FALLTHROUGH */
case ir_var_shader_in:
if (programInterface != GL_PROGRAM_INPUT)
continue;
diff --git a/mesalib/src/glsl/lower_packed_varyings.cpp b/mesalib/src/glsl/lower_packed_varyings.cpp
index 2c9a1c495..d8bebb522 100644
--- a/mesalib/src/glsl/lower_packed_varyings.cpp
+++ b/mesalib/src/glsl/lower_packed_varyings.cpp
@@ -645,13 +645,7 @@ lower_packed_varyings_visitor::needs_lowering(ir_variable *var)
if (var->data.explicit_location)
return false;
- const glsl_type *type = var->type;
- if (this->gs_input_vertices != 0) {
- assert(type->is_array());
- type = type->element_type();
- }
- if (type->is_array())
- type = type->fields.array;
+ const glsl_type *type = var->type->without_array();
if (type->vector_elements == 4 && !type->is_double())
return false;
return true;
diff --git a/mesalib/src/glsl/lower_packing_builtins.cpp b/mesalib/src/glsl/lower_packing_builtins.cpp
index db73c7b0f..a6fb8a883 100644
--- a/mesalib/src/glsl/lower_packing_builtins.cpp
+++ b/mesalib/src/glsl/lower_packing_builtins.cpp
@@ -569,8 +569,8 @@ private:
* return pack_uvec2_to_uint(uvec2(
* round(clamp(VEC2_RVALUE, 0.0f, 1.0f) * 65535.0f)));
*
- * Here it is safe to directly convert the vec2 to uvec2 because the the
- * vec2 has been clamped to a non-negative range.
+ * Here it is safe to directly convert the vec2 to uvec2 because the vec2
+ * has been clamped to a non-negative range.
*/
assert(vec2_rval->type == glsl_type::vec2_type);
@@ -613,8 +613,8 @@ private:
* return pack_uvec4_to_uint(uvec4(
* round(clamp(VEC2_RVALUE, 0.0f, 1.0f) * 255.0f)));
*
- * Here it is safe to directly convert the vec4 to uvec4 because the the
- * vec4 has been clamped to a non-negative range.
+ * Here it is safe to directly convert the vec4 to uvec4 because the vec4
+ * has been clamped to a non-negative range.
*/
assert(vec4_rval->type == glsl_type::vec4_type);
diff --git a/mesalib/src/glsl/nir/nir.c b/mesalib/src/glsl/nir/nir.c
index c6e53612b..4cc074b80 100644
--- a/mesalib/src/glsl/nir/nir.c
+++ b/mesalib/src/glsl/nir/nir.c
@@ -589,6 +589,66 @@ nir_copy_deref(void *mem_ctx, nir_deref *deref)
return NULL;
}
+/* Returns a load_const instruction that represents the constant
+ * initializer for the given deref chain. The caller is responsible for
+ * ensuring that there actually is a constant initializer.
+ */
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
+{
+ nir_constant *constant = deref->var->constant_initializer;
+ assert(constant);
+
+ const nir_deref *tail = &deref->deref;
+ unsigned matrix_offset = 0;
+ while (tail->child) {
+ switch (tail->child->deref_type) {
+ case nir_deref_type_array: {
+ nir_deref_array *arr = nir_deref_as_array(tail->child);
+ assert(arr->deref_array_type == nir_deref_array_type_direct);
+ if (glsl_type_is_matrix(tail->type)) {
+ assert(arr->deref.child == NULL);
+ matrix_offset = arr->base_offset;
+ } else {
+ constant = constant->elements[arr->base_offset];
+ }
+ break;
+ }
+
+ case nir_deref_type_struct: {
+ constant = constant->elements[nir_deref_as_struct(tail->child)->index];
+ break;
+ }
+
+ default:
+ unreachable("Invalid deref child type");
+ }
+
+ tail = tail->child;
+ }
+
+ nir_load_const_instr *load =
+ nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type));
+
+ matrix_offset *= load->def.num_components;
+ for (unsigned i = 0; i < load->def.num_components; i++) {
+ switch (glsl_get_base_type(tail->type)) {
+ case GLSL_TYPE_FLOAT:
+ case GLSL_TYPE_INT:
+ case GLSL_TYPE_UINT:
+ load->value.u[i] = constant->value.u[matrix_offset + i];
+ break;
+ case GLSL_TYPE_BOOL:
+ load->value.u[i] = constant->value.b[matrix_offset + i] ?
+ NIR_TRUE : NIR_FALSE;
+ break;
+ default:
+ unreachable("Invalid immediate type");
+ }
+ }
+
+ return load;
+}
/**
* \name Control flow modification
@@ -1800,33 +1860,37 @@ src_does_not_use_reg(nir_src *src, void *void_reg)
void
nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
{
- if (src->is_ssa) {
- nir_ssa_def *old_ssa = src->ssa;
- *src = new_src;
- if (old_ssa && nir_foreach_src(instr, src_does_not_use_def, old_ssa)) {
- struct set_entry *entry = _mesa_set_search(old_ssa->uses, instr);
- assert(entry);
- _mesa_set_remove(old_ssa->uses, entry);
- }
- } else {
- if (src->reg.indirect)
- nir_instr_rewrite_src(instr, src->reg.indirect, new_src);
-
- nir_register *old_reg = src->reg.reg;
- *src = new_src;
- if (old_reg && nir_foreach_src(instr, src_does_not_use_reg, old_reg)) {
- struct set_entry *entry = _mesa_set_search(old_reg->uses, instr);
- assert(entry);
- _mesa_set_remove(old_reg->uses, entry);
+ nir_src old_src = *src;
+ *src = new_src;
+
+ for (nir_src *iter_src = &old_src; iter_src;
+ iter_src = iter_src->is_ssa ? NULL : iter_src->reg.indirect) {
+ if (iter_src->is_ssa) {
+ nir_ssa_def *ssa = iter_src->ssa;
+ if (ssa && nir_foreach_src(instr, src_does_not_use_def, ssa)) {
+ struct set_entry *entry = _mesa_set_search(ssa->uses, instr);
+ assert(entry);
+ _mesa_set_remove(ssa->uses, entry);
+ }
+ } else {
+ nir_register *reg = iter_src->reg.reg;
+ if (reg && nir_foreach_src(instr, src_does_not_use_reg, reg)) {
+ struct set_entry *entry = _mesa_set_search(reg->uses, instr);
+ assert(entry);
+ _mesa_set_remove(reg->uses, entry);
+ }
}
}
- if (new_src.is_ssa) {
- if (new_src.ssa)
- _mesa_set_add(new_src.ssa->uses, instr);
- } else {
- if (new_src.reg.reg)
- _mesa_set_add(new_src.reg.reg->uses, instr);
+ for (nir_src *iter_src = &new_src; iter_src;
+ iter_src = iter_src->is_ssa ? NULL : iter_src->reg.indirect) {
+ if (iter_src->is_ssa) {
+ if (iter_src->ssa)
+ _mesa_set_add(iter_src->ssa->uses, instr);
+ } else {
+ if (iter_src->reg.reg)
+ _mesa_set_add(iter_src->reg.reg->uses, instr);
+ }
}
}
diff --git a/mesalib/src/glsl/nir/nir.h b/mesalib/src/glsl/nir/nir.h
index 74772c798..98b0ec328 100644
--- a/mesalib/src/glsl/nir/nir.h
+++ b/mesalib/src/glsl/nir/nir.h
@@ -960,7 +960,8 @@ typedef struct {
static inline unsigned
nir_tex_instr_dest_size(nir_tex_instr *instr)
{
- if (instr->op == nir_texop_txs) {
+ switch (instr->op) {
+ case nir_texop_txs: {
unsigned ret;
switch (instr->sampler_dim) {
case GLSL_SAMPLER_DIM_1D:
@@ -985,13 +986,18 @@ nir_tex_instr_dest_size(nir_tex_instr *instr)
return ret;
}
- if (instr->op == nir_texop_query_levels)
+ case nir_texop_lod:
return 2;
- if (instr->is_shadow && instr->is_new_style_shadow)
+ case nir_texop_query_levels:
return 1;
- return 4;
+ default:
+ if (instr->is_shadow && instr->is_new_style_shadow)
+ return 1;
+
+ return 4;
+ }
}
static inline unsigned
@@ -1514,6 +1520,9 @@ nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);
+
void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
diff --git a/mesalib/src/glsl/nir/nir_array.h b/mesalib/src/glsl/nir/nir_array.h
new file mode 100644
index 000000000..1db4e8cea
--- /dev/null
+++ b/mesalib/src/glsl/nir/nir_array.h
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2015 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.
+ *
+ * Authors:
+ * Jason Ekstrand (jason@jlekstrand.net)
+ *
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ void *mem_ctx;
+ size_t size;
+ size_t alloc;
+ void *data;
+} nir_array;
+
+static inline void
+nir_array_init(nir_array *arr, void *mem_ctx)
+{
+ arr->mem_ctx = mem_ctx;
+ arr->size = 0;
+ arr->alloc = 0;
+ arr->data = NULL;
+}
+
+static inline void
+nir_array_fini(nir_array *arr)
+{
+ if (arr->mem_ctx)
+ ralloc_free(arr->data);
+ else
+ free(arr->data);
+}
+
+#define NIR_ARRAY_INITIAL_SIZE 64
+
+/* Increments the size of the array by the given ammount and returns a
+ * pointer to the beginning of the newly added space.
+ */
+static inline void *
+nir_array_grow(nir_array *arr, size_t additional)
+{
+ size_t new_size = arr->size + additional;
+ if (new_size > arr->alloc) {
+ if (arr->alloc == 0)
+ arr->alloc = NIR_ARRAY_INITIAL_SIZE;
+
+ while (new_size > arr->alloc)
+ arr->alloc *= 2;
+
+ if (arr->mem_ctx)
+ arr->data = reralloc_size(arr->mem_ctx, arr->data, arr->alloc);
+ else
+ arr->data = realloc(arr->data, arr->alloc);
+ }
+
+ void *ptr = (void *)((char *)arr->data + arr->size);
+ arr->size = new_size;
+
+ return ptr;
+}
+
+#define nir_array_add(arr, type, elem) \
+ *(type *)nir_array_grow(arr, sizeof(type)) = (elem)
+
+#define nir_array_foreach(arr, type, elem) \
+ for (type *elem = (type *)(arr)->data; \
+ elem < (type *)((char *)(arr)->data + (arr)->size); elem++)
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
diff --git a/mesalib/src/glsl/nir/nir_from_ssa.c b/mesalib/src/glsl/nir/nir_from_ssa.c
index 184698abd..6a3b141bd 100644
--- a/mesalib/src/glsl/nir/nir_from_ssa.c
+++ b/mesalib/src/glsl/nir/nir_from_ssa.c
@@ -642,7 +642,7 @@ emit_copy(nir_parallel_copy_instr *pcopy, nir_src src, nir_src dest_src,
*
* The algorithm works by playing this little shell game with the values.
* We start by recording where every source value is and which source value
- * each destination value should recieve. We then grab any copy whose
+ * each destination value should receive. We then grab any copy whose
* destination is "empty", i.e. not used as a source, and do the following:
* - Find where its source value currently lives
* - Emit the move instruction
diff --git a/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c b/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
index 8c5df7be6..bc6a3d320 100644
--- a/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
+++ b/mesalib/src/glsl/nir/nir_lower_locals_to_regs.c
@@ -26,13 +26,20 @@
*/
#include "nir.h"
+#include "nir_array.h"
struct locals_to_regs_state {
- void *mem_ctx;
+ nir_shader *shader;
nir_function_impl *impl;
/* A hash table mapping derefs to registers */
struct hash_table *regs_table;
+
+ /* A growing array of derefs that we have encountered. There is exactly
+ * one element of this array per element in the hash table. This is
+ * used to make adding register initialization code deterministic.
+ */
+ nir_array derefs_array;
};
/* The following two functions implement a hash and equality check for
@@ -100,15 +107,8 @@ get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
unsigned array_size = 1;
nir_deref *tail = &deref->deref;
while (tail->child) {
- if (tail->child->deref_type == nir_deref_type_array) {
- /* Multiply by the parent's type. */
- if (glsl_type_is_matrix(tail->type)) {
- array_size *= glsl_get_matrix_columns(tail->type);
- } else {
- assert(glsl_get_length(tail->type) > 0);
- array_size *= glsl_get_length(tail->type);
- }
- }
+ if (tail->child->deref_type == nir_deref_type_array)
+ array_size *= glsl_get_length(tail->type);
tail = tail->child;
}
@@ -119,6 +119,7 @@ get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
reg->num_array_elems = array_size > 1 ? array_size : 0;
_mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
+ nir_array_add(&state->derefs_array, nir_deref_var *, deref);
return reg;
}
@@ -134,6 +135,14 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
src.reg.base_offset = 0;
src.reg.indirect = NULL;
+ /* It is possible for a user to create a shader that has an array with a
+ * single element and then proceed to access it indirectly. Indirectly
+ * accessing a non-array register is not allowed in NIR. In order to
+ * handle this case we just convert it to a direct reference.
+ */
+ if (src.reg.reg->num_array_elems == 0)
+ return src;
+
nir_deref *tail = &deref->deref;
while (tail->child != NULL) {
const struct glsl_type *parent_type = tail->type;
@@ -149,11 +158,11 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
if (src.reg.indirect) {
nir_load_const_instr *load_const =
- nir_load_const_instr_create(state->mem_ctx, 1);
+ nir_load_const_instr_create(state->shader, 1);
load_const->value.u[0] = glsl_get_length(parent_type);
nir_instr_insert_before(instr, &load_const->instr);
- nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx, nir_op_imul);
+ nir_alu_instr *mul = nir_alu_instr_create(state->shader, nir_op_imul);
mul->src[0].src = *src.reg.indirect;
mul->src[1].src.is_ssa = true;
mul->src[1].src.ssa = &load_const->def;
@@ -167,15 +176,15 @@ get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
if (src.reg.indirect == NULL) {
- src.reg.indirect = ralloc(state->mem_ctx, nir_src);
+ src.reg.indirect = ralloc(state->shader, nir_src);
nir_src_copy(src.reg.indirect, &deref_array->indirect,
- state->mem_ctx);
+ state->shader);
} else {
- nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
+ nir_alu_instr *add = nir_alu_instr_create(state->shader,
nir_op_iadd);
add->src[0].src = *src.reg.indirect;
nir_src_copy(&add->src[1].src, &deref_array->indirect,
- state->mem_ctx);
+ state->shader);
add->dest.write_mask = 1;
nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
nir_instr_insert_before(instr, &add->instr);
@@ -205,7 +214,7 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
if (intrin->variables[0]->var->data.mode != nir_var_local)
continue;
- nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
+ nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
mov->src[0].src = get_deref_reg_src(intrin->variables[0],
&intrin->instr, state);
mov->dest.write_mask = (1 << intrin->num_components) - 1;
@@ -214,9 +223,9 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
intrin->num_components, NULL);
nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
nir_src_for_ssa(&mov->dest.dest.ssa),
- state->mem_ctx);
+ state->shader);
} else {
- nir_dest_copy(&mov->dest.dest, &intrin->dest, state->mem_ctx);
+ nir_dest_copy(&mov->dest.dest, &intrin->dest, state->shader);
}
nir_instr_insert_before(&intrin->instr, &mov->instr);
@@ -231,8 +240,8 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
nir_src reg_src = get_deref_reg_src(intrin->variables[0],
&intrin->instr, state);
- nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
- nir_src_copy(&mov->src[0].src, &intrin->src[0], state->mem_ctx);
+ nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
+ nir_src_copy(&mov->src[0].src, &intrin->src[0], state->shader);
mov->dest.write_mask = (1 << intrin->num_components) - 1;
mov->dest.dest.is_ssa = false;
mov->dest.dest.reg.reg = reg_src.reg.reg;
@@ -257,20 +266,115 @@ lower_locals_to_regs_block(nir_block *block, void *void_state)
return true;
}
+static nir_block *
+compute_reg_usedef_lca(nir_register *reg)
+{
+ struct set_entry *entry;
+ nir_block *lca = NULL;
+
+ set_foreach(reg->defs, entry)
+ lca = nir_dominance_lca(lca, ((nir_instr *)entry->key)->block);
+
+ set_foreach(reg->uses, entry)
+ lca = nir_dominance_lca(lca, ((nir_instr *)entry->key)->block);
+
+ set_foreach(reg->if_uses, entry) {
+ nir_if *if_stmt = (nir_if *)entry->key;
+ nir_cf_node *prev_node = nir_cf_node_prev(&if_stmt->cf_node);
+ assert(prev_node->type == nir_cf_node_block);
+ lca = nir_dominance_lca(lca, nir_cf_node_as_block(prev_node));
+ }
+
+ return lca;
+}
+
+static void
+insert_constant_initializer(nir_deref_var *deref_head, nir_deref *deref_tail,
+ nir_block *block,
+ struct locals_to_regs_state *state)
+{
+ if (deref_tail->child) {
+ switch (deref_tail->child->deref_type) {
+ case nir_deref_type_array: {
+ unsigned array_elems = glsl_get_length(deref_tail->type);
+
+ nir_deref_array arr_deref;
+ arr_deref.deref = *deref_tail->child;
+ arr_deref.deref_array_type = nir_deref_array_type_direct;
+
+ nir_deref *old_child = deref_tail->child;
+ deref_tail->child = &arr_deref.deref;
+ for (unsigned i = 0; i < array_elems; i++) {
+ arr_deref.base_offset = i;
+ insert_constant_initializer(deref_head, &arr_deref.deref,
+ block, state);
+ }
+ deref_tail->child = old_child;
+ return;
+ }
+
+ case nir_deref_type_struct:
+ insert_constant_initializer(deref_head, deref_tail->child,
+ block, state);
+ return;
+
+ default:
+ unreachable("Invalid deref child type");
+ }
+ }
+
+ assert(deref_tail->child == NULL);
+
+ nir_load_const_instr *load =
+ nir_deref_get_const_initializer_load(state->shader, deref_head);
+ nir_instr_insert_before_block(block, &load->instr);
+
+ nir_src reg_src = get_deref_reg_src(deref_head, &load->instr, state);
+
+ nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
+ mov->src[0].src = nir_src_for_ssa(&load->def);
+ mov->dest.write_mask = (1 << load->def.num_components) - 1;
+ mov->dest.dest.is_ssa = false;
+ mov->dest.dest.reg.reg = reg_src.reg.reg;
+ mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
+ mov->dest.dest.reg.indirect = reg_src.reg.indirect;
+
+ nir_instr_insert_after(&load->instr, &mov->instr);
+}
+
static void
nir_lower_locals_to_regs_impl(nir_function_impl *impl)
{
struct locals_to_regs_state state;
- state.mem_ctx = ralloc_parent(impl);
+ state.shader = impl->overload->function->shader;
state.impl = impl;
state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
+ nir_array_init(&state.derefs_array, NULL);
+
+ nir_metadata_require(impl, nir_metadata_dominance);
nir_foreach_block(impl, lower_locals_to_regs_block, &state);
+ nir_array_foreach(&state.derefs_array, nir_deref_var *, deref_ptr) {
+ nir_deref_var *deref = *deref_ptr;
+ struct hash_entry *deref_entry =
+ _mesa_hash_table_search(state.regs_table, deref);
+ assert(deref_entry && deref_entry->key == deref);
+ nir_register *reg = (nir_register *)deref_entry->data;
+
+ if (deref->var->constant_initializer == NULL)
+ continue;
+
+ nir_block *usedef_lca = compute_reg_usedef_lca(reg);
+
+ insert_constant_initializer(deref, &deref->deref, usedef_lca, &state);
+ }
+
nir_metadata_preserve(impl, nir_metadata_block_index |
nir_metadata_dominance);
+ nir_array_fini(&state.derefs_array);
_mesa_hash_table_destroy(state.regs_table, NULL);
}
diff --git a/mesalib/src/glsl/nir/nir_lower_to_source_mods.c b/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
index d6bf77f17..7b4a0f657 100644
--- a/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
+++ b/mesalib/src/glsl/nir/nir_lower_to_source_mods.c
@@ -67,6 +67,13 @@ nir_lower_to_source_mods_block(nir_block *block, void *state)
continue;
}
+ /* We can only do a rewrite if the source we are copying is SSA.
+ * Otherwise, moving the read might invalidly reorder reads/writes
+ * on a register.
+ */
+ if (!parent->src[0].src.is_ssa)
+ continue;
+
nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
if (alu->src[i].abs) {
/* abs trumps both neg and abs, do nothing */
diff --git a/mesalib/src/glsl/nir/nir_lower_var_copies.c b/mesalib/src/glsl/nir/nir_lower_var_copies.c
index 58389a7c7..21672901f 100644
--- a/mesalib/src/glsl/nir/nir_lower_var_copies.c
+++ b/mesalib/src/glsl/nir/nir_lower_var_copies.c
@@ -64,26 +64,6 @@ get_deref_tail(nir_deref *deref)
return deref;
}
-static int
-type_get_length(const struct glsl_type *type)
-{
- switch (glsl_get_base_type(type)) {
- case GLSL_TYPE_STRUCT:
- case GLSL_TYPE_ARRAY:
- return glsl_get_length(type);
- case GLSL_TYPE_FLOAT:
- case GLSL_TYPE_INT:
- case GLSL_TYPE_UINT:
- case GLSL_TYPE_BOOL:
- if (glsl_type_is_matrix(type))
- return glsl_get_matrix_columns(type);
- else
- return glsl_get_vector_elements(type);
- default:
- unreachable("Invalid deref base type");
- }
-}
-
/* This function recursively walks the given deref chain and replaces the
* given copy instruction with an equivalent sequence load/store
* operations.
@@ -121,9 +101,9 @@ emit_copy_load_store(nir_intrinsic_instr *copy_instr,
nir_deref_array *src_arr = nir_deref_as_array(src_arr_parent->child);
nir_deref_array *dest_arr = nir_deref_as_array(dest_arr_parent->child);
- unsigned length = type_get_length(src_arr_parent->type);
+ unsigned length = glsl_get_length(src_arr_parent->type);
/* The wildcards should represent the same number of elements */
- assert(length == type_get_length(dest_arr_parent->type));
+ assert(length == glsl_get_length(dest_arr_parent->type));
assert(length > 0);
/* Walk over all of the elements that this wildcard refers to and
diff --git a/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c b/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
index 2ca74d71b..bb60f4601 100644
--- a/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
+++ b/mesalib/src/glsl/nir/nir_lower_vars_to_ssa.c
@@ -55,7 +55,7 @@ struct deref_node {
};
struct lower_variables_state {
- void *mem_ctx;
+ nir_shader *shader;
void *dead_ctx;
nir_function_impl *impl;
@@ -90,34 +90,14 @@ struct lower_variables_state {
struct hash_table *phi_table;
};
-static int
-type_get_length(const struct glsl_type *type)
-{
- switch (glsl_get_base_type(type)) {
- case GLSL_TYPE_STRUCT:
- case GLSL_TYPE_ARRAY:
- return glsl_get_length(type);
- case GLSL_TYPE_FLOAT:
- case GLSL_TYPE_INT:
- case GLSL_TYPE_UINT:
- case GLSL_TYPE_BOOL:
- if (glsl_type_is_matrix(type))
- return glsl_get_matrix_columns(type);
- else
- return glsl_get_vector_elements(type);
- default:
- unreachable("Invalid deref base type");
- }
-}
-
static struct deref_node *
deref_node_create(struct deref_node *parent,
- const struct glsl_type *type, void *mem_ctx)
+ const struct glsl_type *type, nir_shader *shader)
{
size_t size = sizeof(struct deref_node) +
- type_get_length(type) * sizeof(struct deref_node *);
+ glsl_get_length(type) * sizeof(struct deref_node *);
- struct deref_node *node = rzalloc_size(mem_ctx, size);
+ struct deref_node *node = rzalloc_size(shader, size);
node->type = type;
node->parent = parent;
node->deref = NULL;
@@ -165,7 +145,7 @@ get_deref_node(nir_deref_var *deref, struct lower_variables_state *state)
case nir_deref_type_struct: {
nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
- assert(deref_struct->index < type_get_length(node->type));
+ assert(deref_struct->index < glsl_get_length(node->type));
if (node->children[deref_struct->index] == NULL)
node->children[deref_struct->index] =
@@ -184,7 +164,7 @@ get_deref_node(nir_deref_var *deref, struct lower_variables_state *state)
* out-of-bounds offset. We need to handle this at least
* somewhat gracefully.
*/
- if (arr->base_offset >= type_get_length(node->type))
+ if (arr->base_offset >= glsl_get_length(node->type))
return NULL;
if (node->children[arr->base_offset] == NULL)
@@ -317,6 +297,10 @@ deref_may_be_aliased_node(struct deref_node *node, nir_deref *deref,
if (arr->deref_array_type == nir_deref_array_type_indirect)
return true;
+ /* If there is an indirect at this level, we're aliased. */
+ if (node->indirect)
+ return true;
+
assert(arr->deref_array_type == nir_deref_array_type_direct);
if (node->children[arr->base_offset] &&
@@ -465,7 +449,7 @@ lower_copies_to_load_store(struct deref_node *node,
set_foreach(node->copies, copy_entry) {
nir_intrinsic_instr *copy = (void *)copy_entry->key;
- nir_lower_var_copy_instr(copy, state->mem_ctx);
+ nir_lower_var_copy_instr(copy, state->shader);
for (unsigned i = 0; i < 2; ++i) {
struct deref_node *arg_node =
@@ -485,67 +469,6 @@ lower_copies_to_load_store(struct deref_node *node,
return true;
}
-/* Returns a load_const instruction that represents the constant
- * initializer for the given deref chain. The caller is responsible for
- * ensuring that there actually is a constant initializer.
- */
-static nir_load_const_instr *
-get_const_initializer_load(const nir_deref_var *deref,
- struct lower_variables_state *state)
-{
- nir_constant *constant = deref->var->constant_initializer;
- const nir_deref *tail = &deref->deref;
- unsigned matrix_offset = 0;
- while (tail->child) {
- switch (tail->child->deref_type) {
- case nir_deref_type_array: {
- nir_deref_array *arr = nir_deref_as_array(tail->child);
- assert(arr->deref_array_type == nir_deref_array_type_direct);
- if (glsl_type_is_matrix(tail->type)) {
- assert(arr->deref.child == NULL);
- matrix_offset = arr->base_offset;
- } else {
- constant = constant->elements[arr->base_offset];
- }
- break;
- }
-
- case nir_deref_type_struct: {
- constant = constant->elements[nir_deref_as_struct(tail->child)->index];
- break;
- }
-
- default:
- unreachable("Invalid deref child type");
- }
-
- tail = tail->child;
- }
-
- nir_load_const_instr *load =
- nir_load_const_instr_create(state->mem_ctx,
- glsl_get_vector_elements(tail->type));
-
- matrix_offset *= load->def.num_components;
- for (unsigned i = 0; i < load->def.num_components; i++) {
- switch (glsl_get_base_type(tail->type)) {
- case GLSL_TYPE_FLOAT:
- case GLSL_TYPE_INT:
- case GLSL_TYPE_UINT:
- load->value.u[i] = constant->value.u[matrix_offset + i];
- break;
- case GLSL_TYPE_BOOL:
- load->value.u[i] = constant->value.b[matrix_offset + i] ?
- NIR_TRUE : NIR_FALSE;
- break;
- default:
- unreachable("Invalid immediate type");
- }
- }
-
- return load;
-}
-
/** Pushes an SSA def onto the def stack for the given node
*
* Each node is potentially associated with a stack of SSA definitions.
@@ -614,7 +537,7 @@ get_ssa_def_for_block(struct deref_node *node, nir_block *block,
* given block. This means that we need to add an undef and use that.
*/
nir_ssa_undef_instr *undef =
- nir_ssa_undef_instr_create(state->mem_ctx,
+ nir_ssa_undef_instr_create(state->shader,
glsl_get_vector_elements(node->type));
nir_instr_insert_before_cf_list(&state->impl->body, &undef->instr);
def_stack_push(node, &undef->def, state);
@@ -694,7 +617,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
* should result in an undefined value.
*/
nir_ssa_undef_instr *undef =
- nir_ssa_undef_instr_create(state->mem_ctx,
+ nir_ssa_undef_instr_create(state->shader,
intrin->num_components);
nir_instr_insert_before(&intrin->instr, &undef->instr);
@@ -702,14 +625,14 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
nir_src_for_ssa(&undef->def),
- state->mem_ctx);
+ state->shader);
continue;
}
if (!node->lower_to_ssa)
continue;
- nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,
+ nir_alu_instr *mov = nir_alu_instr_create(state->shader,
nir_op_imov);
mov->src[0].src.is_ssa = true;
mov->src[0].src.ssa = get_ssa_def_for_block(node, block, state);
@@ -727,7 +650,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
nir_src_for_ssa(&mov->dest.dest.ssa),
- state->mem_ctx);
+ state->shader);
break;
}
@@ -750,7 +673,7 @@ rename_variables_block(nir_block *block, struct lower_variables_state *state)
assert(intrin->src[0].is_ssa);
- nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx,
+ nir_alu_instr *mov = nir_alu_instr_create(state->shader,
nir_op_imov);
mov->src[0].src.is_ssa = true;
mov->src[0].src.ssa = intrin->src[0].ssa;
@@ -887,7 +810,7 @@ insert_phi_nodes(struct lower_variables_state *state)
continue;
if (has_already[next->index] < iter_count) {
- nir_phi_instr *phi = nir_phi_instr_create(state->mem_ctx);
+ nir_phi_instr *phi = nir_phi_instr_create(state->shader);
nir_ssa_dest_init(&phi->instr, &phi->dest,
glsl_get_vector_elements(node->type), NULL);
nir_instr_insert_before_block(next, &phi->instr);
@@ -938,8 +861,8 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
{
struct lower_variables_state state;
- state.mem_ctx = ralloc_parent(impl);
- state.dead_ctx = ralloc_context(state.mem_ctx);
+ state.shader = impl->overload->function->shader;
+ state.dead_ctx = ralloc_context(state.shader);
state.impl = impl;
state.deref_var_nodes = _mesa_hash_table_create(state.dead_ctx,
@@ -983,7 +906,8 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
progress = true;
if (deref->var->constant_initializer) {
- nir_load_const_instr *load = get_const_initializer_load(deref, &state);
+ nir_load_const_instr *load =
+ nir_deref_get_const_initializer_load(state.shader, deref);
nir_ssa_def_init(&load->instr, &load->def,
glsl_get_vector_elements(node->type), NULL);
nir_instr_insert_before_cf_list(&impl->body, &load->instr);
diff --git a/mesalib/src/glsl/nir/nir_opt_algebraic.py b/mesalib/src/glsl/nir/nir_opt_algebraic.py
index cdb19241c..2a2b9561e 100644
--- a/mesalib/src/glsl/nir/nir_opt_algebraic.py
+++ b/mesalib/src/glsl/nir/nir_opt_algebraic.py
@@ -144,6 +144,7 @@ optimizations = [
(('fexp', ('fmul', ('flog', a), b)), ('fpow', a, b), '!options->lower_fpow'), # e^(ln(a)*b) = a^b
(('fpow', a, 1.0), a),
(('fpow', a, 2.0), ('fmul', a, a)),
+ (('fpow', a, 4.0), ('fmul', ('fmul', a, a), ('fmul', a, a))),
(('fpow', 2.0, a), ('fexp2', a)),
(('fsqrt', ('fexp2', a)), ('fexp2', ('fmul', 0.5, a))),
(('fsqrt', ('fexp', a)), ('fexp', ('fmul', 0.5, a))),
diff --git a/mesalib/src/glsl/nir/nir_print.c b/mesalib/src/glsl/nir/nir_print.c
index fb8c9344c..eb4045cec 100644
--- a/mesalib/src/glsl/nir/nir_print.c
+++ b/mesalib/src/glsl/nir/nir_print.c
@@ -533,6 +533,8 @@ print_load_const_instr(nir_load_const_instr *instr, unsigned tabs, FILE *fp)
fprintf(fp, "0x%08x /* %f */", instr->value.u[i], instr->value.f[i]);
}
+
+ fprintf(fp, ")");
}
static void
diff --git a/mesalib/src/glsl/nir/nir_types.cpp b/mesalib/src/glsl/nir/nir_types.cpp
index f0d0b46d2..62176f508 100644
--- a/mesalib/src/glsl/nir/nir_types.cpp
+++ b/mesalib/src/glsl/nir/nir_types.cpp
@@ -103,7 +103,7 @@ glsl_get_matrix_columns(const struct glsl_type *type)
unsigned
glsl_get_length(const struct glsl_type *type)
{
- return type->length;
+ return type->is_matrix() ? type->matrix_columns : type->length;
}
const char *
diff --git a/mesalib/src/glsl/opt_algebraic.cpp b/mesalib/src/glsl/opt_algebraic.cpp
index 3d2f2ca0b..fa5db70f2 100644
--- a/mesalib/src/glsl/opt_algebraic.cpp
+++ b/mesalib/src/glsl/opt_algebraic.cpp
@@ -99,6 +99,12 @@ is_vec_two(ir_constant *ir)
}
static inline bool
+is_vec_four(ir_constant *ir)
+{
+ return (ir == NULL) ? false : ir->is_value(4.0, 4);
+}
+
+static inline bool
is_vec_negative_one(ir_constant *ir)
{
return (ir == NULL) ? false : ir->is_negative_one();
@@ -774,6 +780,20 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
return mul(x, x);
}
+ if (is_vec_four(op_const[1])) {
+ ir_variable *x = new(ir) ir_variable(ir->operands[1]->type, "x",
+ ir_var_temporary);
+ base_ir->insert_before(x);
+ base_ir->insert_before(assign(x, ir->operands[0]));
+
+ ir_variable *squared = new(ir) ir_variable(ir->operands[1]->type,
+ "squared",
+ ir_var_temporary);
+ base_ir->insert_before(squared);
+ base_ir->insert_before(assign(squared, mul(x, x)));
+ return mul(squared, squared);
+ }
+
break;
case ir_binop_min:
diff --git a/mesalib/src/glsl/opt_dead_builtin_varyings.cpp b/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
index 50c8aa763..92f20c71d 100644
--- a/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
+++ b/mesalib/src/glsl/opt_dead_builtin_varyings.cpp
@@ -35,7 +35,7 @@
* the built-in varyings have pre-assigned locations. Also, the elimination
* of unused gl_TexCoord elements requires its own lowering pass anyway.
*
- * It's implemented by replacing all occurences of dead varyings with
+ * It's implemented by replacing all occurrences of dead varyings with
* temporary variables, which creates dead code. It is recommended to run
* a dead-code elimination pass after this.
*
@@ -280,7 +280,7 @@ public:
*
* We're going to break down the gl_TexCoord array into separate
* variables. First, add declarations of the new variables all
- * occurences of gl_TexCoord will be replaced with.
+ * occurrences of gl_TexCoord will be replaced with.
*/
if (info->lower_texcoord_array) {
prepare_array(ir, this->new_texcoord, ARRAY_SIZE(this->new_texcoord),
@@ -411,7 +411,7 @@ public:
* variable dereference representing gl_TexCoord[i].
*/
if (this->info->lower_texcoord_array) {
- /* gl_TexCoord[i] occurence */
+ /* gl_TexCoord[i] occurrence */
ir_dereference_array *const da = (*rvalue)->as_dereference_array();
if (da && da->variable_referenced() ==
@@ -425,7 +425,7 @@ public:
/* Same for gl_FragData. */
if (this->info->lower_fragdata_array) {
- /* gl_FragData[i] occurence */
+ /* gl_FragData[i] occurrence */
ir_dereference_array *const da = (*rvalue)->as_dereference_array();
if (da && da->variable_referenced() == this->info->fragdata_array) {
diff --git a/mesalib/src/glsl/opt_function_inlining.cpp b/mesalib/src/glsl/opt_function_inlining.cpp
index 64b4907ba..84a9e4fa0 100644
--- a/mesalib/src/glsl/opt_function_inlining.cpp
+++ b/mesalib/src/glsl/opt_function_inlining.cpp
@@ -130,7 +130,7 @@ ir_call::generate_inline(ir_instruction *next_ir)
parameters[i] = sig_param->clone(ctx, ht);
parameters[i]->data.mode = ir_var_auto;
- /* Remove the read-only decoration becuase we're going to write
+ /* Remove the read-only decoration because we're going to write
* directly to this variable. If the cloned variable is left
* read-only and the inlined function is inside a loop, the loop
* analysis code will get confused.
diff --git a/mesalib/src/glsl/standalone_scaffolding.cpp b/mesalib/src/glsl/standalone_scaffolding.cpp
index 6f5a27f85..a109c4e92 100644
--- a/mesalib/src/glsl/standalone_scaffolding.cpp
+++ b/mesalib/src/glsl/standalone_scaffolding.cpp
@@ -189,9 +189,6 @@ void initialize_context_to_defaults(struct gl_context *ctx, gl_api api)
options.MaxUnrollIterations = 32;
options.MaxIfDepth = UINT_MAX;
- /* Default pragma settings */
- options.DefaultPragmas.Optimize = true;
-
for (int sh = 0; sh < MESA_SHADER_STAGES; ++sh)
memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
}