spirv: Add a vtn_constant_int helper
authorJason Ekstrand <jason@jlekstrand.net>
Thu, 6 Jun 2019 14:53:27 +0000 (09:53 -0500)
committerJason Ekstrand <jason@jlekstrand.net>
Wed, 19 Jun 2019 21:05:54 +0000 (21:05 +0000)
Reviewed-by: Karol Herbst <kherbst@redhat.com>
src/compiler/spirv/vtn_private.h
src/compiler/spirv/vtn_variables.c

index 1b2be93621f4e2432c0c3137d07efe422e0f9fab..58b9553c1b8e08260aef977d5cc44c77f21af9b8 100644 (file)
@@ -716,6 +716,24 @@ vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
    }
 }
 
+static inline int64_t
+vtn_constant_int(struct vtn_builder *b, uint32_t value_id)
+{
+   struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
+
+   vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
+               !glsl_type_is_integer(val->type->type),
+               "Expected id %u to be an integer constant", value_id);
+
+   switch (glsl_get_bit_size(val->type->type)) {
+   case 8:  return val->constant->values[0][0].i8;
+   case 16: return val->constant->values[0][0].i16;
+   case 32: return val->constant->values[0][0].i32;
+   case 64: return val->constant->values[0][0].i64;
+   default: unreachable("Invalid bit size");
+   }
+}
+
 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
 
 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
index 925d2549d4c47893d2035236b89b00ed1e00cfdc..1a9ca0745fa5e04b295406efb253fa5c853bc558 100644 (file)
@@ -2403,23 +2403,7 @@ vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
          struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
          if (link_val->value_type == vtn_value_type_constant) {
             chain->link[idx].mode = vtn_access_mode_literal;
-            const unsigned bit_size = glsl_get_bit_size(link_val->type->type);
-            switch (bit_size) {
-            case 8:
-               chain->link[idx].id = link_val->constant->values[0][0].i8;
-               break;
-            case 16:
-               chain->link[idx].id = link_val->constant->values[0][0].i16;
-               break;
-            case 32:
-               chain->link[idx].id = link_val->constant->values[0][0].i32;
-               break;
-            case 64:
-               chain->link[idx].id = link_val->constant->values[0][0].i64;
-               break;
-            default:
-               vtn_fail("Invalid bit size: %u", bit_size);
-            }
+            chain->link[idx].id = vtn_constant_int(b, w[i]);
          } else {
             chain->link[idx].mode = vtn_access_mode_id;
             chain->link[idx].id = w[i];