glsl: Add an option to clamp block indices when lowering UBO/SSBOs
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 19 May 2016 03:28:07 +0000 (20:28 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Tue, 24 May 2016 02:12:34 +0000 (19:12 -0700)
This prevents array overflow when the block is actually an array of UBOs or
SSBOs.  On some hardware such as i965, such overflows can cause GPU hangs.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/compiler/glsl/ir_optimization.h
src/compiler/glsl/linker.cpp
src/compiler/glsl/lower_ubo_reference.cpp
src/mesa/drivers/dri/i965/brw_compiler.c
src/mesa/main/mtypes.h

index 71b10e4d643b26ac1423bb47b8aabde1dfb4030a..ba14e34b71e0f3df16c20823d2980ff1d186e04b 100644 (file)
@@ -123,7 +123,7 @@ bool lower_clip_cull_distance(struct gl_shader_program *prog, gl_shader *shader)
 void lower_output_reads(unsigned stage, exec_list *instructions);
 bool lower_packing_builtins(exec_list *instructions, int op_mask);
 void lower_shared_reference(struct gl_shader *shader, unsigned *shared_size);
-void lower_ubo_reference(struct gl_shader *shader);
+void lower_ubo_reference(struct gl_shader *shader, bool clamp_block_indices);
 void lower_packed_varyings(void *mem_ctx,
                            unsigned locations_used, ir_variable_mode mode,
                            unsigned gs_input_vertices, gl_shader *shader,
index 89cd9f795cc3d0d6aa44e7fdef8d5409248762fb..3d95540256c49d92f4bf842f30aee917b2b8e8d0 100644 (file)
@@ -4906,7 +4906,8 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
          &ctx->Const.ShaderCompilerOptions[i];
 
       if (options->LowerBufferInterfaceBlocks)
-         lower_ubo_reference(prog->_LinkedShaders[i]);
+         lower_ubo_reference(prog->_LinkedShaders[i],
+                             options->ClampBlockIndicesToArrayBounds);
 
       if (options->LowerShaderSharedVariables)
          lower_shared_reference(prog->_LinkedShaders[i],
index 1a0140fad152a225d78d6c47102a8b3da361497b..749deedcd1c92519067cf4ba6cff1deb4031e74a 100644 (file)
@@ -44,8 +44,10 @@ namespace {
 class lower_ubo_reference_visitor :
       public lower_buffer_access::lower_buffer_access {
 public:
-   lower_ubo_reference_visitor(struct gl_shader *shader)
-   : shader(shader), struct_field(NULL), variable(NULL)
+   lower_ubo_reference_visitor(struct gl_shader *shader,
+                               bool clamp_block_indices)
+   : shader(shader), clamp_block_indices(clamp_block_indices),
+     struct_field(NULL), variable(NULL)
    {
    }
 
@@ -104,6 +106,7 @@ public:
    ir_visitor_status visit_enter(ir_call *ir);
 
    struct gl_shader *shader;
+   bool clamp_block_indices;
    struct gl_uniform_buffer_variable *ubo_var;
    const struct glsl_struct_field *struct_field;
    ir_variable *variable;
@@ -242,6 +245,26 @@ interface_field_name(void *mem_ctx, char *base_name, ir_rvalue *d,
    return NULL;
 }
 
+static ir_rvalue *
+clamp_to_array_bounds(void *mem_ctx, ir_rvalue *index, const glsl_type *type)
+{
+   assert(type->is_array());
+
+   const unsigned array_size = type->arrays_of_arrays_size();
+
+   ir_constant *max_index = new(mem_ctx) ir_constant(array_size - 1);
+   max_index->type = index->type;
+
+   ir_constant *zero = new(mem_ctx) ir_constant(0);
+   zero->type = index->type;
+
+   if (index->type->base_type == GLSL_TYPE_INT)
+      index = max2(index, zero);
+   index = min2(index, max_index);
+
+   return index;
+}
+
 void
 lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
                                                      ir_variable *var,
@@ -258,6 +281,11 @@ lower_ubo_reference_visitor::setup_for_load_or_store(void *mem_ctx,
       interface_field_name(mem_ctx, (char *) var->get_interface_type()->name,
                            deref, &nonconst_block_index);
 
+   if (nonconst_block_index && clamp_block_indices) {
+      nonconst_block_index =
+         clamp_to_array_bounds(mem_ctx, nonconst_block_index, var->type);
+   }
+
    /* Locate the block by interface name */
    unsigned num_blocks;
    struct gl_uniform_block **blocks;
@@ -1062,9 +1090,9 @@ lower_ubo_reference_visitor::visit_enter(ir_call *ir)
 } /* unnamed namespace */
 
 void
-lower_ubo_reference(struct gl_shader *shader)
+lower_ubo_reference(struct gl_shader *shader, bool clamp_block_indices)
 {
-   lower_ubo_reference_visitor v(shader);
+   lower_ubo_reference_visitor v(shader, clamp_block_indices);
 
    /* Loop over the instructions lowering references, because we take
     * a deref of a UBO array using a UBO dereference as the index will
index 82131db9a8f2535a0cb25853663dc4176b06a8fb..3f1758922864afe3f6bc2940ab0732ac0c3dfba5 100644 (file)
@@ -188,6 +188,7 @@ brw_compiler_create(void *mem_ctx, const struct brw_device_info *devinfo)
       }
 
       compiler->glsl_compiler_options[i].LowerBufferInterfaceBlocks = true;
+      compiler->glsl_compiler_options[i].ClampBlockIndicesToArrayBounds = true;
    }
 
    compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].EmitNoIndirectInput = false;
index 6cd30e8c10c67a19e06b70b0b5ee711c536ac4a4..b7b3ede57f15ccf0250f6555895dcba7a081901c 100644 (file)
@@ -2951,6 +2951,9 @@ struct gl_shader_compiler_options
 
    GLboolean LowerBufferInterfaceBlocks; /**< Lower UBO and SSBO access to intrinsics. */
 
+   /** Clamp UBO and SSBO block indices so they don't go out-of-bounds. */
+   GLboolean ClampBlockIndicesToArrayBounds;
+
    GLboolean LowerShaderSharedVariables; /**< Lower compute shader shared
                                           *   variable access to intrinsics. */