spirv: Add better type validation to OpTypeImage
[mesa.git] / src / compiler / spirv / spirv_to_nir.c
index 297db87d57598ee22ccc22a876b05258552959af..0d474957dc95f430e5c91f20c6981d3b39753230 100644 (file)
@@ -178,10 +178,13 @@ vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
    switch (glsl_get_base_type(type)) {
    case GLSL_TYPE_INT:
    case GLSL_TYPE_UINT:
+   case GLSL_TYPE_INT16:
+   case GLSL_TYPE_UINT16:
    case GLSL_TYPE_INT64:
    case GLSL_TYPE_UINT64:
    case GLSL_TYPE_BOOL:
    case GLSL_TYPE_FLOAT:
+   case GLSL_TYPE_FLOAT16:
    case GLSL_TYPE_DOUBLE: {
       int bit_size = glsl_get_bit_size(type);
       if (glsl_type_is_vector_or_scalar(type)) {
@@ -239,7 +242,7 @@ vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
    }
 
    default:
-      unreachable("bad constant type");
+      vtn_fail("bad constant type");
    }
 
    return val;
@@ -254,7 +257,7 @@ vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
       return vtn_undef_ssa_value(b, val->type->type);
 
    case vtn_value_type_constant:
-      return vtn_const_ssa_value(b, val->constant, val->const_type);
+      return vtn_const_ssa_value(b, val->constant, val->type->type);
 
    case vtn_value_type_ssa:
       return val->ssa;
@@ -267,7 +270,7 @@ vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
       return ssa;
 
    default:
-      unreachable("Invalid type for an SSA value");
+      vtn_fail("Invalid type for an SSA value");
    }
 }
 
@@ -344,7 +347,7 @@ vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
       if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
          val->ext_handler = vtn_handle_glsl450_instruction;
       } else {
-         unreachable("Unsupported extension");
+         vtn_fail("Unsupported extension");
       }
       break;
    }
@@ -357,7 +360,7 @@ vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
    }
 
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 }
 
@@ -446,7 +449,7 @@ vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
          dec->scope = VTN_DEC_EXECUTION_MODE;
          break;
       default:
-         unreachable("Invalid decoration opcode");
+         vtn_fail("Invalid decoration opcode");
       }
       dec->decoration = *(w++);
       dec->literals = w;
@@ -481,7 +484,7 @@ vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
    }
 
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 }
 
@@ -508,6 +511,7 @@ vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
    case vtn_base_type_pointer:
    case vtn_base_type_image:
    case vtn_base_type_sampler:
+   case vtn_base_type_sampled_image:
       /* Nothing more to do */
       break;
 
@@ -645,7 +649,7 @@ struct_member_decoration_cb(struct vtn_builder *b,
       break;
 
    default:
-      unreachable("Unhandled decoration");
+      vtn_fail("Unhandled decoration");
    }
 }
 
@@ -756,12 +760,12 @@ type_decoration_cb(struct vtn_builder *b,
       break;
 
    default:
-      unreachable("Unhandled decoration");
+      vtn_fail("Unhandled decoration");
    }
 }
 
 static unsigned
-translate_image_format(SpvImageFormat format)
+translate_image_format(struct vtn_builder *b, SpvImageFormat format)
 {
    switch (format) {
    case SpvImageFormatUnknown:      return 0;      /* GL_NONE */
@@ -805,8 +809,65 @@ translate_image_format(SpvImageFormat format)
    case SpvImageFormatR16ui:        return 0x8234; /* GL_R16UI */
    case SpvImageFormatR8ui:         return 0x8232; /* GL_R8UI */
    default:
-      unreachable("Invalid image format");
-      return 0;
+      vtn_fail("Invalid image format");
+   }
+}
+
+static struct vtn_type *
+vtn_type_layout_std430(struct vtn_builder *b, struct vtn_type *type,
+                       uint32_t *size_out, uint32_t *align_out)
+{
+   switch (type->base_type) {
+   case vtn_base_type_scalar: {
+      uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
+      *size_out = comp_size;
+      *align_out = comp_size;
+      return type;
+   }
+
+   case vtn_base_type_vector: {
+      uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
+      assert(type->length > 0 && type->length <= 4);
+      unsigned align_comps = type->length == 3 ? 4 : type->length;
+      *size_out = comp_size * type->length,
+      *align_out = comp_size * align_comps;
+      return type;
+   }
+
+   case vtn_base_type_matrix:
+   case vtn_base_type_array: {
+      /* We're going to add an array stride */
+      type = vtn_type_copy(b, type);
+      uint32_t elem_size, elem_align;
+      type->array_element = vtn_type_layout_std430(b, type->array_element,
+                                                   &elem_size, &elem_align);
+      type->stride = vtn_align_u32(elem_size, elem_align);
+      *size_out = type->stride * type->length;
+      *align_out = elem_align;
+      return type;
+   }
+
+   case vtn_base_type_struct: {
+      /* We're going to add member offsets */
+      type = vtn_type_copy(b, type);
+      uint32_t offset = 0;
+      uint32_t align = 0;
+      for (unsigned i = 0; i < type->length; i++) {
+         uint32_t mem_size, mem_align;
+         type->members[i] = vtn_type_layout_std430(b, type->members[i],
+                                                   &mem_size, &mem_align);
+         offset = vtn_align_u32(offset, mem_align);
+         type->offsets[i] = offset;
+         offset += mem_size;
+         align = MAX2(align, mem_align);
+      }
+      *size_out = offset;
+      *align_out = align;
+      return type;
+   }
+
+   default:
+      unreachable("Invalid SPIR-V type for std430");
    }
 }
 
@@ -827,21 +888,46 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
    case SpvOpTypeBool:
       val->type->base_type = vtn_base_type_scalar;
       val->type->type = glsl_bool_type();
+      val->type->length = 1;
       break;
    case SpvOpTypeInt: {
       int bit_size = w[2];
       const bool signedness = w[3];
       val->type->base_type = vtn_base_type_scalar;
-      if (bit_size == 64)
+      switch (bit_size) {
+      case 64:
          val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
-      else
+         break;
+      case 32:
          val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
+         break;
+      case 16:
+         val->type->type = (signedness ? glsl_int16_t_type() : glsl_uint16_t_type());
+         break;
+      default:
+         vtn_fail("Invalid int bit size");
+      }
+      val->type->length = 1;
       break;
    }
+
    case SpvOpTypeFloat: {
       int bit_size = w[2];
       val->type->base_type = vtn_base_type_scalar;
-      val->type->type = bit_size == 64 ? glsl_double_type() : glsl_float_type();
+      switch (bit_size) {
+      case 16:
+         val->type->type = glsl_float16_t_type();
+         break;
+      case 32:
+         val->type->type = glsl_float_type();
+         break;
+      case 64:
+         val->type->type = glsl_double_type();
+         break;
+      default:
+         vtn_fail("Invalid float bit size");
+      }
+      val->type->length = 1;
       break;
    }
 
@@ -849,9 +935,14 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
       struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
       unsigned elems = w[3];
 
-      vtn_assert(glsl_type_is_scalar(base->type));
+      vtn_fail_if(base->base_type != vtn_base_type_scalar,
+                  "Base type for OpTypeVector must be a scalar");
+      vtn_fail_if(elems < 2 || elems > 4,
+                  "Invalid component count for OpTypeVector");
+
       val->type->base_type = vtn_base_type_vector;
       val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
+      val->type->length = elems;
       val->type->stride = glsl_get_bit_size(base->type) / 8;
       val->type->array_element = base;
       break;
@@ -861,12 +952,18 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
       struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
       unsigned columns = w[3];
 
-      vtn_assert(glsl_type_is_vector(base->type));
+      vtn_fail_if(base->base_type != vtn_base_type_vector,
+                  "Base type for OpTypeMatrix must be a vector");
+      vtn_fail_if(columns < 2 || columns > 4,
+                  "Invalid column count for OpTypeMatrix");
+
       val->type->base_type = vtn_base_type_matrix;
       val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
                                          glsl_get_vector_elements(base->type),
                                          columns);
-      vtn_assert(!glsl_type_is_error(val->type->type));
+      vtn_fail_if(glsl_type_is_error(val->type->type),
+                  "Unsupported base type for OpTypeMatrix");
+      assert(!glsl_type_is_error(val->type->type));
       val->type->length = columns;
       val->type->array_element = base;
       val->type->row_major = false;
@@ -959,16 +1056,31 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
           */
          val->type->type = glsl_vector_type(GLSL_TYPE_UINT, 2);
       }
+
+      if (storage_class == SpvStorageClassWorkgroup &&
+          b->options->lower_workgroup_access_to_offsets) {
+         uint32_t size, align;
+         val->type->deref = vtn_type_layout_std430(b, val->type->deref,
+                                                   &size, &align);
+         val->type->length = size;
+         val->type->align = align;
+         /* These can actually be stored to nir_variables and used as SSA
+          * values so they need a real glsl_type.
+          */
+         val->type->type = glsl_uint_type();
+      }
       break;
    }
 
    case SpvOpTypeImage: {
       val->type->base_type = vtn_base_type_image;
 
-      const struct glsl_type *sampled_type =
-         vtn_value(b, w[2], vtn_value_type_type)->type->type;
+      const struct vtn_type *sampled_type =
+         vtn_value(b, w[2], vtn_value_type_type)->type;
 
-      vtn_assert(glsl_type_is_vector_or_scalar(sampled_type));
+      vtn_fail_if(sampled_type->base_type != vtn_base_type_scalar ||
+                  glsl_get_bit_size(sampled_type->type) != 32,
+                  "Sampled type of OpTypeImage must be a 32-bit scalar");
 
       enum glsl_sampler_dim dim;
       switch ((SpvDim)w[3]) {
@@ -980,7 +1092,7 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
       case SpvDimBuffer:   dim = GLSL_SAMPLER_DIM_BUF;   break;
       case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
       default:
-         unreachable("Invalid SPIR-V Sampler dimension");
+         vtn_fail("Invalid SPIR-V image dimensionality");
       }
 
       bool is_shadow = w[4];
@@ -1000,28 +1112,31 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
          else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
             dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
          else
-            unreachable("Unsupported multisampled image type");
+            vtn_fail("Unsupported multisampled image type");
       }
 
-      val->type->image_format = translate_image_format(format);
+      val->type->image_format = translate_image_format(b, format);
 
+      enum glsl_base_type sampled_base_type =
+         glsl_get_base_type(sampled_type->type);
       if (sampled == 1) {
          val->type->sampled = true;
          val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
-                                             glsl_get_base_type(sampled_type));
+                                             sampled_base_type);
       } else if (sampled == 2) {
          vtn_assert(!is_shadow);
          val->type->sampled = false;
-         val->type->type = glsl_image_type(dim, is_array,
-                                           glsl_get_base_type(sampled_type));
+         val->type->type = glsl_image_type(dim, is_array, sampled_base_type);
       } else {
-         unreachable("We need to know if the image will be sampled");
+         vtn_fail("We need to know if the image will be sampled");
       }
       break;
    }
 
    case SpvOpTypeSampledImage:
-      val->type = vtn_value(b, w[2], vtn_value_type_type)->type;
+      val->type->base_type = vtn_base_type_sampled_image;
+      val->type->image = vtn_value(b, w[2], vtn_value_type_type)->type;
+      val->type->type = val->type->image->type;
       break;
 
    case SpvOpTypeSampler:
@@ -1041,7 +1156,7 @@ vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
    case SpvOpTypeQueue:
    case SpvOpTypePipe:
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 
    vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
@@ -1061,10 +1176,13 @@ vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
    switch (glsl_get_base_type(type)) {
    case GLSL_TYPE_INT:
    case GLSL_TYPE_UINT:
+   case GLSL_TYPE_INT16:
+   case GLSL_TYPE_UINT16:
    case GLSL_TYPE_INT64:
    case GLSL_TYPE_UINT64:
    case GLSL_TYPE_BOOL:
    case GLSL_TYPE_FLOAT:
+   case GLSL_TYPE_FLOAT16:
    case GLSL_TYPE_DOUBLE:
       /* Nothing to do here.  It's already initialized to zero */
       break;
@@ -1089,7 +1207,7 @@ vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
       break;
 
    default:
-      unreachable("Invalid type for null constant");
+      vtn_fail("Invalid type for null constant");
    }
 
    return c;
@@ -1151,7 +1269,7 @@ handle_workgroup_size_decoration_cb(struct vtn_builder *b,
        dec->literals[0] != SpvBuiltInWorkgroupSize)
       return;
 
-   vtn_assert(val->const_type == glsl_vector_type(GLSL_TYPE_UINT, 3));
+   vtn_assert(val->type->type == glsl_vector_type(GLSL_TYPE_UINT, 3));
 
    b->shader->info.cs.local_size[0] = val->constant->values[0].u32[0];
    b->shader->info.cs.local_size[1] = val->constant->values[0].u32[1];
@@ -1163,94 +1281,119 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
                     const uint32_t *w, unsigned count)
 {
    struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
-   val->const_type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
    val->constant = rzalloc(b, nir_constant);
    switch (opcode) {
    case SpvOpConstantTrue:
-      vtn_assert(val->const_type == glsl_bool_type());
-      val->constant->values[0].u32[0] = NIR_TRUE;
-      break;
    case SpvOpConstantFalse:
-      vtn_assert(val->const_type == glsl_bool_type());
-      val->constant->values[0].u32[0] = NIR_FALSE;
-      break;
-
    case SpvOpSpecConstantTrue:
    case SpvOpSpecConstantFalse: {
-      vtn_assert(val->const_type == glsl_bool_type());
-      uint32_t int_val =
-         get_specialization(b, val, (opcode == SpvOpSpecConstantTrue));
+      vtn_fail_if(val->type->type != glsl_bool_type(),
+                  "Result type of %s must be OpTypeBool",
+                  spirv_op_to_string(opcode));
+
+      uint32_t int_val = (opcode == SpvOpConstantTrue ||
+                          opcode == SpvOpSpecConstantTrue);
+
+      if (opcode == SpvOpSpecConstantTrue ||
+          opcode == SpvOpSpecConstantFalse)
+         int_val = get_specialization(b, val, int_val);
+
       val->constant->values[0].u32[0] = int_val ? NIR_TRUE : NIR_FALSE;
       break;
    }
 
    case SpvOpConstant: {
-      vtn_assert(glsl_type_is_scalar(val->const_type));
-      int bit_size = glsl_get_bit_size(val->const_type);
-      if (bit_size == 64) {
-         val->constant->values->u32[0] = w[3];
-         val->constant->values->u32[1] = w[4];
-      } else {
-         vtn_assert(bit_size == 32);
+      vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
+                  "Result type of %s must be a scalar",
+                  spirv_op_to_string(opcode));
+      int bit_size = glsl_get_bit_size(val->type->type);
+      switch (bit_size) {
+      case 64:
+         val->constant->values->u64[0] = vtn_u64_literal(&w[3]);
+         break;
+      case 32:
          val->constant->values->u32[0] = w[3];
+         break;
+      case 16:
+         val->constant->values->u16[0] = w[3];
+         break;
+      default:
+         vtn_fail("Unsupported SpvOpConstant bit size");
       }
       break;
    }
+
    case SpvOpSpecConstant: {
-      vtn_assert(glsl_type_is_scalar(val->const_type));
-      val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
-      int bit_size = glsl_get_bit_size(val->const_type);
-      if (bit_size == 64)
+      vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
+                  "Result type of %s must be a scalar",
+                  spirv_op_to_string(opcode));
+      int bit_size = glsl_get_bit_size(val->type->type);
+      switch (bit_size) {
+      case 64:
          val->constant->values[0].u64[0] =
             get_specialization64(b, val, vtn_u64_literal(&w[3]));
-      else
+         break;
+      case 32:
          val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
+         break;
+      case 16:
+         val->constant->values[0].u16[0] = get_specialization(b, val, w[3]);
+         break;
+      default:
+         vtn_fail("Unsupported SpvOpSpecConstant bit size");
+      }
       break;
    }
+
    case SpvOpSpecConstantComposite:
    case SpvOpConstantComposite: {
       unsigned elem_count = count - 3;
+      vtn_fail_if(elem_count != val->type->length,
+                  "%s has %u constituents, expected %u",
+                  spirv_op_to_string(opcode), elem_count, val->type->length);
+
       nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
       for (unsigned i = 0; i < elem_count; i++)
          elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
 
-      switch (glsl_get_base_type(val->const_type)) {
-      case GLSL_TYPE_UINT:
-      case GLSL_TYPE_INT:
-      case GLSL_TYPE_UINT64:
-      case GLSL_TYPE_INT64:
-      case GLSL_TYPE_FLOAT:
-      case GLSL_TYPE_BOOL:
-      case GLSL_TYPE_DOUBLE: {
-         int bit_size = glsl_get_bit_size(val->const_type);
-         if (glsl_type_is_matrix(val->const_type)) {
-            vtn_assert(glsl_get_matrix_columns(val->const_type) == elem_count);
-            for (unsigned i = 0; i < elem_count; i++)
-               val->constant->values[i] = elems[i]->values[0];
-         } else {
-            vtn_assert(glsl_type_is_vector(val->const_type));
-            vtn_assert(glsl_get_vector_elements(val->const_type) == elem_count);
-            for (unsigned i = 0; i < elem_count; i++) {
-               if (bit_size == 64) {
-                  val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
-               } else {
-                  vtn_assert(bit_size == 32);
-                  val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
-               }
+      switch (val->type->base_type) {
+      case vtn_base_type_vector: {
+         assert(glsl_type_is_vector(val->type->type));
+         int bit_size = glsl_get_bit_size(val->type->type);
+         for (unsigned i = 0; i < elem_count; i++) {
+            switch (bit_size) {
+            case 64:
+               val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
+               break;
+            case 32:
+               val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
+               break;
+            case 16:
+               val->constant->values[0].u16[i] = elems[i]->values[0].u16[0];
+               break;
+            default:
+               vtn_fail("Invalid SpvOpConstantComposite bit size");
             }
          }
-         ralloc_free(elems);
          break;
       }
-      case GLSL_TYPE_STRUCT:
-      case GLSL_TYPE_ARRAY:
+
+      case vtn_base_type_matrix:
+         assert(glsl_type_is_matrix(val->type->type));
+         for (unsigned i = 0; i < elem_count; i++)
+            val->constant->values[i] = elems[i]->values[0];
+         break;
+
+      case vtn_base_type_struct:
+      case vtn_base_type_array:
          ralloc_steal(val->constant, elems);
          val->constant->num_elements = elem_count;
          val->constant->elements = elems;
          break;
 
       default:
-         unreachable("Unsupported type for constants");
+         vtn_fail("Result type of %s must be a composite type",
+                  spirv_op_to_string(opcode));
       }
       break;
    }
@@ -1267,22 +1410,14 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
          vtn_assert(v1->value_type == vtn_value_type_constant ||
                     v1->value_type == vtn_value_type_undef);
 
-         unsigned len0 = v0->value_type == vtn_value_type_constant ?
-                         glsl_get_vector_elements(v0->const_type) :
-                         glsl_get_vector_elements(v0->type->type);
-         unsigned len1 = v1->value_type == vtn_value_type_constant ?
-                         glsl_get_vector_elements(v1->const_type) :
-                         glsl_get_vector_elements(v1->type->type);
+         unsigned len0 = glsl_get_vector_elements(v0->type->type);
+         unsigned len1 = glsl_get_vector_elements(v1->type->type);
 
          vtn_assert(len0 + len1 < 16);
 
-         unsigned bit_size = glsl_get_bit_size(val->const_type);
-         unsigned bit_size0 = v0->value_type == vtn_value_type_constant ?
-                              glsl_get_bit_size(v0->const_type) :
-                              glsl_get_bit_size(v0->type->type);
-         unsigned bit_size1 = v1->value_type == vtn_value_type_constant ?
-                              glsl_get_bit_size(v1->const_type) :
-                              glsl_get_bit_size(v1->type->type);
+         unsigned bit_size = glsl_get_bit_size(val->type->type);
+         unsigned bit_size0 = glsl_get_bit_size(v0->type->type);
+         unsigned bit_size1 = glsl_get_bit_size(v1->type->type);
 
          vtn_assert(bit_size == bit_size0 && bit_size == bit_size1);
          (void)bit_size0; (void)bit_size1;
@@ -1309,6 +1444,7 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
                   val->constant->values[0].u64[j] = u64[comp];
             }
          } else {
+            /* This is for both 32-bit and 16-bit values */
             uint32_t u32[8];
             if (v0->value_type == vtn_value_type_constant) {
                for (unsigned i = 0; i < len0; i++)
@@ -1352,41 +1488,39 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
 
          int elem = -1;
          int col = 0;
-         const struct glsl_type *type = comp->const_type;
+         const struct vtn_type *type = comp->type;
          for (unsigned i = deref_start; i < count; i++) {
-            switch (glsl_get_base_type(type)) {
-            case GLSL_TYPE_UINT:
-            case GLSL_TYPE_INT:
-            case GLSL_TYPE_UINT64:
-            case GLSL_TYPE_INT64:
-            case GLSL_TYPE_FLOAT:
-            case GLSL_TYPE_DOUBLE:
-            case GLSL_TYPE_BOOL:
-               /* If we hit this granularity, we're picking off an element */
-               if (glsl_type_is_matrix(type)) {
-                  vtn_assert(col == 0 && elem == -1);
-                  col = w[i];
-                  elem = 0;
-                  type = glsl_get_column_type(type);
-               } else {
-                  vtn_assert(elem <= 0 && glsl_type_is_vector(type));
-                  elem = w[i];
-                  type = glsl_scalar_type(glsl_get_base_type(type));
-               }
-               continue;
-
-            case GLSL_TYPE_ARRAY:
+            vtn_fail_if(w[i] > type->length,
+                        "%uth index of %s is %u but the type has only "
+                        "%u elements", i - deref_start,
+                        spirv_op_to_string(opcode), w[i], type->length);
+
+            switch (type->base_type) {
+            case vtn_base_type_vector:
+               elem = w[i];
+               type = type->array_element;
+               break;
+
+            case vtn_base_type_matrix:
+               assert(col == 0 && elem == -1);
+               col = w[i];
+               elem = 0;
+               type = type->array_element;
+               break;
+
+            case vtn_base_type_array:
                c = &(*c)->elements[w[i]];
-               type = glsl_get_array_element(type);
-               continue;
+               type = type->array_element;
+               break;
 
-            case GLSL_TYPE_STRUCT:
+            case vtn_base_type_struct:
                c = &(*c)->elements[w[i]];
-               type = glsl_get_struct_field(type, w[i]);
-               continue;
+               type = type->members[w[i]];
+               break;
 
             default:
-               unreachable("Invalid constant type");
+               vtn_fail("%s must only index into composite types",
+                        spirv_op_to_string(opcode));
             }
          }
 
@@ -1394,31 +1528,45 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
             if (elem == -1) {
                val->constant = *c;
             } else {
-               unsigned num_components = glsl_get_vector_elements(type);
-               unsigned bit_size = glsl_get_bit_size(type);
+               unsigned num_components = type->length;
+               unsigned bit_size = glsl_get_bit_size(type->type);
                for (unsigned i = 0; i < num_components; i++)
-                  if (bit_size == 64) {
+                  switch(bit_size) {
+                  case 64:
                      val->constant->values[0].u64[i] = (*c)->values[col].u64[elem + i];
-                  } else {
-                     vtn_assert(bit_size == 32);
+                     break;
+                  case 32:
                      val->constant->values[0].u32[i] = (*c)->values[col].u32[elem + i];
+                     break;
+                  case 16:
+                     val->constant->values[0].u16[i] = (*c)->values[col].u16[elem + i];
+                     break;
+                  default:
+                     vtn_fail("Invalid SpvOpCompositeExtract bit size");
                   }
             }
          } else {
             struct vtn_value *insert =
                vtn_value(b, w[4], vtn_value_type_constant);
-            vtn_assert(insert->const_type == type);
+            vtn_assert(insert->type == type);
             if (elem == -1) {
                *c = insert->constant;
             } else {
-               unsigned num_components = glsl_get_vector_elements(type);
-               unsigned bit_size = glsl_get_bit_size(type);
+               unsigned num_components = type->length;
+               unsigned bit_size = glsl_get_bit_size(type->type);
                for (unsigned i = 0; i < num_components; i++)
-                  if (bit_size == 64) {
+                  switch (bit_size) {
+                  case 64:
                      (*c)->values[col].u64[elem + i] = insert->constant->values[0].u64[i];
-                  } else {
-                     vtn_assert(bit_size == 32);
+                     break;
+                  case 32:
                      (*c)->values[col].u32[elem + i] = insert->constant->values[0].u32[i];
+                     break;
+                  case 16:
+                     (*c)->values[col].u16[elem + i] = insert->constant->values[0].u16[i];
+                     break;
+                  default:
+                     vtn_fail("Invalid SpvOpCompositeInsert bit size");
                   }
             }
          }
@@ -1427,22 +1575,38 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
 
       default: {
          bool swap;
-         nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->const_type);
+         nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->type->type);
          nir_alu_type src_alu_type = dst_alu_type;
-         nir_op op = vtn_nir_alu_op_for_spirv_opcode(opcode, &swap, src_alu_type, dst_alu_type);
+         unsigned num_components = glsl_get_vector_elements(val->type->type);
+         unsigned bit_size;
+
+         vtn_assert(count <= 7);
 
-         unsigned num_components = glsl_get_vector_elements(val->const_type);
-         unsigned bit_size =
-            glsl_get_bit_size(val->const_type);
+         switch (opcode) {
+         case SpvOpSConvert:
+         case SpvOpFConvert:
+            /* We have a source in a conversion */
+            src_alu_type =
+               nir_get_nir_type_for_glsl_type(
+                  vtn_value(b, w[4], vtn_value_type_constant)->type->type);
+            /* We use the bitsize of the conversion source to evaluate the opcode later */
+            bit_size = glsl_get_bit_size(
+               vtn_value(b, w[4], vtn_value_type_constant)->type->type);
+            break;
+         default:
+            bit_size = glsl_get_bit_size(val->type->type);
+         };
 
+         nir_op op = vtn_nir_alu_op_for_spirv_opcode(b, opcode, &swap,
+                                                     src_alu_type,
+                                                     dst_alu_type);
          nir_const_value src[4];
-         vtn_assert(count <= 7);
+
          for (unsigned i = 0; i < count - 4; i++) {
             nir_constant *c =
                vtn_value(b, w[4 + i], vtn_value_type_constant)->constant;
 
             unsigned j = swap ? 1 - i : i;
-            vtn_assert(bit_size == 32);
             src[j] = c->values[0];
          }
 
@@ -1455,15 +1619,15 @@ vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
    }
 
    case SpvOpConstantNull:
-      val->constant = vtn_null_constant(b, val->const_type);
+      val->constant = vtn_null_constant(b, val->type->type);
       break;
 
    case SpvOpConstantSampler:
-      unreachable("OpConstantSampler requires Kernel Capability");
+      vtn_fail("OpConstantSampler requires Kernel Capability");
       break;
 
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 
    /* Now that we have the value, update the workgroup size if needed */
@@ -1533,10 +1697,13 @@ vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
          switch (glsl_get_base_type(type)) {
          case GLSL_TYPE_INT:
          case GLSL_TYPE_UINT:
+         case GLSL_TYPE_INT16:
+         case GLSL_TYPE_UINT16:
          case GLSL_TYPE_INT64:
          case GLSL_TYPE_UINT64:
          case GLSL_TYPE_BOOL:
          case GLSL_TYPE_FLOAT:
+         case GLSL_TYPE_FLOAT16:
          case GLSL_TYPE_DOUBLE:
             child_type = glsl_get_column_type(type);
             break;
@@ -1547,7 +1714,7 @@ vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
             child_type = glsl_get_struct_field(type, i);
             break;
          default:
-            unreachable("unkown base type");
+            vtn_fail("unkown base type");
          }
 
          val->elems[i] = vtn_create_ssa_value(b, child_type);
@@ -1660,7 +1827,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 
    nir_tex_src srcs[8]; /* 8 should be enough */
@@ -1699,7 +1866,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
          coord_components = 3;
          break;
       default:
-         unreachable("Invalid sampler type");
+         vtn_fail("Invalid sampler type");
       }
 
       if (is_array && texop != nir_texop_lod)
@@ -1820,7 +1987,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
    case GLSL_TYPE_UINT:    instr->dest_type = nir_type_uint;  break;
    case GLSL_TYPE_BOOL:    instr->dest_type = nir_type_bool;      break;
    default:
-      unreachable("Invalid base type for sampler result");
+      vtn_fail("Invalid base type for sampler result");
    }
 
    nir_deref_var *sampler = vtn_pointer_to_deref(b, sampled.sampler);
@@ -1854,7 +2021,7 @@ vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
       instr->sampler = NULL;
       break;
    case nir_texop_txf_ms_mcs:
-      unreachable("unexpected nir_texop_txf_ms_mcs");
+      vtn_fail("unexpected nir_texop_txf_ms_mcs");
    }
 
    nir_ssa_dest_init(&instr->instr, &instr->dest,
@@ -1960,7 +2127,7 @@ fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Invalid SPIR-V atomic");
+      vtn_fail("Invalid SPIR-V atomic");
    }
 }
 
@@ -2052,7 +2219,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Invalid image opcode");
+      vtn_fail("Invalid image opcode");
    }
 
    nir_intrinsic_op op;
@@ -2078,7 +2245,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
    OP(AtomicXor,              atomic_xor)
 #undef OP
    default:
-      unreachable("Invalid image opcode");
+      vtn_fail("Invalid image opcode");
    }
 
    nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
@@ -2128,7 +2295,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Invalid image opcode");
+      vtn_fail("Invalid image opcode");
    }
 
    if (opcode != SpvOpImageWrite) {
@@ -2155,7 +2322,7 @@ vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
 }
 
 static nir_intrinsic_op
-get_ssbo_nir_atomic_op(SpvOp opcode)
+get_ssbo_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
 {
    switch (opcode) {
    case SpvOpAtomicLoad:      return nir_intrinsic_load_ssbo;
@@ -2176,12 +2343,38 @@ get_ssbo_nir_atomic_op(SpvOp opcode)
    OP(AtomicXor,              atomic_xor)
 #undef OP
    default:
-      unreachable("Invalid SSBO atomic");
+      vtn_fail("Invalid SSBO atomic");
    }
 }
 
 static nir_intrinsic_op
-get_shared_nir_atomic_op(SpvOp opcode)
+get_shared_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
+{
+   switch (opcode) {
+   case SpvOpAtomicLoad:      return nir_intrinsic_load_shared;
+   case SpvOpAtomicStore:     return nir_intrinsic_store_shared;
+#define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
+   OP(AtomicExchange,         atomic_exchange)
+   OP(AtomicCompareExchange,  atomic_comp_swap)
+   OP(AtomicIIncrement,       atomic_add)
+   OP(AtomicIDecrement,       atomic_add)
+   OP(AtomicIAdd,             atomic_add)
+   OP(AtomicISub,             atomic_add)
+   OP(AtomicSMin,             atomic_imin)
+   OP(AtomicUMin,             atomic_umin)
+   OP(AtomicSMax,             atomic_imax)
+   OP(AtomicUMax,             atomic_umax)
+   OP(AtomicAnd,              atomic_and)
+   OP(AtomicOr,               atomic_or)
+   OP(AtomicXor,              atomic_xor)
+#undef OP
+   default:
+      vtn_fail("Invalid shared atomic");
+   }
+}
+
+static nir_intrinsic_op
+get_var_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
 {
    switch (opcode) {
    case SpvOpAtomicLoad:      return nir_intrinsic_load_var;
@@ -2202,7 +2395,7 @@ get_shared_nir_atomic_op(SpvOp opcode)
    OP(AtomicXor,              atomic_xor)
 #undef OP
    default:
-      unreachable("Invalid shared atomic");
+      vtn_fail("Invalid shared atomic");
    }
 }
 
@@ -2237,7 +2430,7 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Invalid SPIR-V atomic");
+      vtn_fail("Invalid SPIR-V atomic");
    }
 
    /*
@@ -2245,10 +2438,11 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
    SpvMemorySemanticsMask semantics = w[5];
    */
 
-   if (ptr->mode == vtn_variable_mode_workgroup) {
+   if (ptr->mode == vtn_variable_mode_workgroup &&
+       !b->options->lower_workgroup_access_to_offsets) {
       nir_deref_var *deref = vtn_pointer_to_deref(b, ptr);
       const struct glsl_type *deref_type = nir_deref_tail(&deref->deref)->type;
-      nir_intrinsic_op op = get_shared_nir_atomic_op(opcode);
+      nir_intrinsic_op op = get_var_nir_atomic_op(b, opcode);
       atomic = nir_intrinsic_instr_create(b->nb.shader, op);
       atomic->variables[0] = nir_deref_var_clone(deref, atomic);
 
@@ -2281,31 +2475,40 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
          break;
 
       default:
-         unreachable("Invalid SPIR-V atomic");
+         vtn_fail("Invalid SPIR-V atomic");
 
       }
    } else {
-      vtn_assert(ptr->mode == vtn_variable_mode_ssbo);
       nir_ssa_def *offset, *index;
       offset = vtn_pointer_to_offset(b, ptr, &index, NULL);
 
-      nir_intrinsic_op op = get_ssbo_nir_atomic_op(opcode);
+      nir_intrinsic_op op;
+      if (ptr->mode == vtn_variable_mode_ssbo) {
+         op = get_ssbo_nir_atomic_op(b, opcode);
+      } else {
+         vtn_assert(ptr->mode == vtn_variable_mode_workgroup &&
+                    b->options->lower_workgroup_access_to_offsets);
+         op = get_shared_nir_atomic_op(b, opcode);
+      }
 
       atomic = nir_intrinsic_instr_create(b->nb.shader, op);
 
+      int src = 0;
       switch (opcode) {
       case SpvOpAtomicLoad:
          atomic->num_components = glsl_get_vector_elements(ptr->type->type);
-         atomic->src[0] = nir_src_for_ssa(index);
-         atomic->src[1] = nir_src_for_ssa(offset);
+         if (ptr->mode == vtn_variable_mode_ssbo)
+            atomic->src[src++] = nir_src_for_ssa(index);
+         atomic->src[src++] = nir_src_for_ssa(offset);
          break;
 
       case SpvOpAtomicStore:
          atomic->num_components = glsl_get_vector_elements(ptr->type->type);
          nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
-         atomic->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
-         atomic->src[1] = nir_src_for_ssa(index);
-         atomic->src[2] = nir_src_for_ssa(offset);
+         atomic->src[src++] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
+         if (ptr->mode == vtn_variable_mode_ssbo)
+            atomic->src[src++] = nir_src_for_ssa(index);
+         atomic->src[src++] = nir_src_for_ssa(offset);
          break;
 
       case SpvOpAtomicExchange:
@@ -2322,13 +2525,14 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
       case SpvOpAtomicAnd:
       case SpvOpAtomicOr:
       case SpvOpAtomicXor:
-         atomic->src[0] = nir_src_for_ssa(index);
-         atomic->src[1] = nir_src_for_ssa(offset);
-         fill_common_atomic_sources(b, opcode, w, &atomic->src[2]);
+         if (ptr->mode == vtn_variable_mode_ssbo)
+            atomic->src[src++] = nir_src_for_ssa(index);
+         atomic->src[src++] = nir_src_for_ssa(offset);
+         fill_common_atomic_sources(b, opcode, w, &atomic->src[src]);
          break;
 
       default:
-         unreachable("Invalid SPIR-V atomic");
+         vtn_fail("Invalid SPIR-V atomic");
       }
    }
 
@@ -2349,7 +2553,7 @@ vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
 }
 
 static nir_alu_instr *
-create_vec(nir_shader *shader, unsigned num_components, unsigned bit_size)
+create_vec(struct vtn_builder *b, unsigned num_components, unsigned bit_size)
 {
    nir_op op;
    switch (num_components) {
@@ -2357,10 +2561,10 @@ create_vec(nir_shader *shader, unsigned num_components, unsigned bit_size)
    case 2: op = nir_op_vec2; break;
    case 3: op = nir_op_vec3; break;
    case 4: op = nir_op_vec4; break;
-   default: unreachable("bad vector size");
+   default: vtn_fail("bad vector size");
    }
 
-   nir_alu_instr *vec = nir_alu_instr_create(shader, op);
+   nir_alu_instr *vec = nir_alu_instr_create(b->shader, op);
    nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
                      bit_size, NULL);
    vec->dest.write_mask = (1 << num_components) - 1;
@@ -2378,9 +2582,8 @@ vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
       vtn_create_ssa_value(b, glsl_transposed_type(src->type));
 
    for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
-      nir_alu_instr *vec = create_vec(b->shader,
-                                      glsl_get_matrix_columns(src->type),
-                                      glsl_get_bit_size(src->type));
+      nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type),
+                                         glsl_get_bit_size(src->type));
       if (glsl_type_is_vector_or_scalar(src->type)) {
           vec->src[0].src = nir_src_for_ssa(src->def);
           vec->src[0].swizzle[0] = i;
@@ -2410,7 +2613,7 @@ nir_ssa_def *
 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
                   unsigned index)
 {
-   nir_alu_instr *vec = create_vec(b->shader, src->num_components,
+   nir_alu_instr *vec = create_vec(b, src->num_components,
                                    src->bit_size);
 
    for (unsigned i = 0; i < src->num_components; i++) {
@@ -2456,7 +2659,7 @@ vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
                    nir_ssa_def *src0, nir_ssa_def *src1,
                    const uint32_t *indices)
 {
-   nir_alu_instr *vec = create_vec(b->shader, num_components, src0->bit_size);
+   nir_alu_instr *vec = create_vec(b, num_components, src0->bit_size);
 
    for (unsigned i = 0; i < num_components; i++) {
       uint32_t index = indices[i];
@@ -2484,8 +2687,7 @@ static nir_ssa_def *
 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
                      unsigned num_srcs, nir_ssa_def **srcs)
 {
-   nir_alu_instr *vec = create_vec(b->shader, num_components,
-                                   srcs[0]->bit_size);
+   nir_alu_instr *vec = create_vec(b, num_components, srcs[0]->bit_size);
 
    /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
     *
@@ -2648,7 +2850,7 @@ vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("unknown composite operation");
+      vtn_fail("unknown composite operation");
    }
 }
 
@@ -2673,7 +2875,7 @@ vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
       intrinsic_op = nir_intrinsic_barrier;
       break;
    default:
-      unreachable("unknown barrier instruction");
+      vtn_fail("unknown barrier instruction");
    }
 
    nir_intrinsic_instr *intrin =
@@ -2686,7 +2888,8 @@ vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
 }
 
 static unsigned
-gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
+gl_primitive_from_spv_execution_mode(struct vtn_builder *b,
+                                     SpvExecutionMode mode)
 {
    switch (mode) {
    case SpvExecutionModeInputPoints:
@@ -2709,13 +2912,13 @@ gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
    case SpvExecutionModeOutputTriangleStrip:
       return 5; /* GL_TRIANGLE_STRIP */
    default:
-      unreachable("Invalid primitive type");
-      return 4;
+      vtn_fail("Invalid primitive type");
    }
 }
 
 static unsigned
-vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
+vertices_in_from_spv_execution_mode(struct vtn_builder *b,
+                                    SpvExecutionMode mode)
 {
    switch (mode) {
    case SpvExecutionModeInputPoints:
@@ -2729,13 +2932,12 @@ vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
    case SpvExecutionModeInputTrianglesAdjacency:
       return 6;
    default:
-      unreachable("Invalid GS input mode");
-      return 0;
+      vtn_fail("Invalid GS input mode");
    }
 }
 
 static gl_shader_stage
-stage_for_execution_model(SpvExecutionModel model)
+stage_for_execution_model(struct vtn_builder *b, SpvExecutionModel model)
 {
    switch (model) {
    case SpvExecutionModelVertex:
@@ -2751,7 +2953,7 @@ stage_for_execution_model(SpvExecutionModel model)
    case SpvExecutionModelGLCompute:
       return MESA_SHADER_COMPUTE;
    default:
-      unreachable("Unsupported execution model");
+      vtn_fail("Unsupported execution model");
    }
 }
 
@@ -2892,8 +3094,15 @@ vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
          spv_check_supported(variable_pointers, cap);
          break;
 
+      case SpvCapabilityStorageUniformBufferBlock16:
+      case SpvCapabilityStorageUniform16:
+      case SpvCapabilityStoragePushConstant16:
+      case SpvCapabilityStorageInputOutput16:
+         spv_check_supported(storage_16bit, cap);
+         break;
+
       default:
-         unreachable("Unhandled capability");
+         vtn_fail("Unhandled capability");
       }
       break;
    }
@@ -2915,7 +3124,7 @@ vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
       entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
 
       if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
-          stage_for_execution_model(w[1]) != b->entry_point_stage)
+          stage_for_execution_model(b, w[1]) != b->entry_point_stage)
          break;
 
       vtn_assert(b->entry_point == NULL);
@@ -3021,11 +3230,11 @@ vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
       if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
           b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
          b->shader->info.tess.primitive_mode =
-            gl_primitive_from_spv_execution_mode(mode->exec_mode);
+            gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
       } else {
          vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
          b->shader->info.gs.vertices_in =
-            vertices_in_from_spv_execution_mode(mode->exec_mode);
+            vertices_in_from_spv_execution_mode(b, mode->exec_mode);
       }
       break;
 
@@ -3034,7 +3243,7 @@ vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
    case SpvExecutionModeOutputTriangleStrip:
       vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
       b->shader->info.gs.output_primitive =
-         gl_primitive_from_spv_execution_mode(mode->exec_mode);
+         gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
       break;
 
    case SpvExecutionModeSpacingEqual:
@@ -3073,7 +3282,7 @@ vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
       break;
 
    case SpvExecutionModeXfb:
-      unreachable("Unhandled execution mode");
+      vtn_fail("Unhandled execution mode");
       break;
 
    case SpvExecutionModeVecTypeHint:
@@ -3081,7 +3290,7 @@ vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
       break; /* OpenCL */
 
    default:
-      unreachable("Unhandled execution mode");
+      vtn_fail("Unhandled execution mode");
    }
 }
 
@@ -3089,6 +3298,8 @@ static bool
 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
                                         const uint32_t *w, unsigned count)
 {
+   vtn_set_instruction_result_type(b, opcode, w, count);
+
    switch (opcode) {
    case SpvOpSource:
    case SpvOpSourceContinued:
@@ -3107,7 +3318,7 @@ vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
    case SpvOpMemberDecorate:
    case SpvOpGroupDecorate:
    case SpvOpGroupMemberDecorate:
-      unreachable("Invalid opcode types and variables section");
+      vtn_fail("Invalid opcode types and variables section");
       break;
 
    case SpvOpTypeVoid:
@@ -3276,6 +3487,55 @@ vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
       /* Handle OpSelect up-front here because it needs to be able to handle
        * pointers and not just regular vectors and scalars.
        */
+      struct vtn_value *res_val = vtn_untyped_value(b, w[2]);
+      struct vtn_value *sel_val = vtn_untyped_value(b, w[3]);
+      struct vtn_value *obj1_val = vtn_untyped_value(b, w[4]);
+      struct vtn_value *obj2_val = vtn_untyped_value(b, w[5]);
+
+      const struct glsl_type *sel_type;
+      switch (res_val->type->base_type) {
+      case vtn_base_type_scalar:
+         sel_type = glsl_bool_type();
+         break;
+      case vtn_base_type_vector:
+         sel_type = glsl_vector_type(GLSL_TYPE_BOOL, res_val->type->length);
+         break;
+      case vtn_base_type_pointer:
+         /* We need to have actual storage for pointer types */
+         vtn_fail_if(res_val->type->type == NULL,
+                     "Invalid pointer result type for OpSelect");
+         sel_type = glsl_bool_type();
+         break;
+      default:
+         vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
+      }
+
+      if (unlikely(sel_val->type->type != sel_type)) {
+         if (sel_val->type->type == glsl_bool_type()) {
+            /* This case is illegal but some older versions of GLSLang produce
+             * it.  The GLSLang issue was fixed on March 30, 2017:
+             *
+             * https://github.com/KhronosGroup/glslang/issues/809
+             *
+             * Unfortunately, there are applications in the wild which are
+             * shipping with this bug so it isn't nice to fail on them so we
+             * throw a warning instead.  It's not actually a problem for us as
+             * nir_builder will just splat the condition out which is most
+             * likely what the client wanted anyway.
+             */
+            vtn_warn("Condition type of OpSelect must have the same number "
+                     "of components as Result Type");
+         } else {
+            vtn_fail("Condition type of OpSelect must be a scalar or vector "
+                     "of Boolean type. It must have the same number of "
+                     "components as Result Type");
+         }
+      }
+
+      vtn_fail_if(obj1_val->type != res_val->type ||
+                  obj2_val->type != res_val->type,
+                  "Object types must match the result type in OpSelect");
+
       struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
       struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, res_type->type);
       ssa->def = nir_bcsel(&b->nb, vtn_ssa_value(b, w[3])->def,
@@ -3407,7 +3667,7 @@ vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
       break;
 
    default:
-      unreachable("Unhandled opcode");
+      vtn_fail("Unhandled opcode");
    }
 
    return true;
@@ -3458,7 +3718,7 @@ spirv_to_nir(const uint32_t *words, size_t word_count,
                                    vtn_handle_preamble_instruction);
 
    if (b->entry_point == NULL) {
-      assert(!"Entry point not found");
+      vtn_fail("Entry point not found");
       ralloc_free(b);
       return NULL;
    }
@@ -3479,6 +3739,9 @@ spirv_to_nir(const uint32_t *words, size_t word_count,
    words = vtn_foreach_instruction(b, words, word_end,
                                    vtn_handle_variable_or_type_instruction);
 
+   /* Set types on all vtn_values */
+   vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_type);
+
    vtn_build_cfg(b, words, word_end);
 
    assert(b->entry_point->value_type == vtn_value_type_function);