Add a Feature struct to validation state.
authorDavid Neto <dneto@google.com>
Wed, 22 Feb 2017 22:53:08 +0000 (17:53 -0500)
committerDavid Neto <dneto@google.com>
Mon, 27 Feb 2017 18:12:59 +0000 (13:12 -0500)
For now, it is used only for checks of 16bit int and float types.

source/val/validation_state.cpp
source/val/validation_state.h
source/validate_datarules.cpp

index 4f1dd7a..5b01b15 100644 (file)
@@ -332,6 +332,18 @@ void ValidationState_t::RegisterCapability(SpvCapability cap) {
     desc->capabilities.ForEach(
         [this](SpvCapability c) { RegisterCapability(c); });
   }
+
+  switch (cap) {
+    case SpvCapabilityInt16:
+      features_.declare_int16_type = true;
+      break;
+    case SpvCapabilityFloat16:
+    case SpvCapabilityFloat16Buffer:
+      features_.declare_float16_type = true;
+      break;
+    default:
+      break;
+  }
 }
 
 bool ValidationState_t::HasAnyOf(const CapabilitySet& capabilities) const {
index 3900487..97f4e7f 100644 (file)
@@ -54,6 +54,12 @@ enum ModuleLayoutSection {
 /// This class manages the state of the SPIR-V validation as it is being parsed.
 class ValidationState_t {
  public:
+  // Features that can optionally be turned on by a capability.
+  struct Feature {
+    bool declare_int16_type = false;    // Allow OpTypeInt with 16 bit width?
+    bool declare_float16_type = false;  // Allow OpTypeFloat with 16 bit width?
+  };
+
   ValidationState_t(const spv_const_context context);
 
   /// Returns the context
@@ -290,6 +296,10 @@ class ValidationState_t {
   bool IsStructTypeWithBuiltInMember(uint32_t id) const {
     return (builtin_structs_.find(id) != builtin_structs_.end());
   }
+
+  // Returns the state of optional features.
+  const Feature& features() const { return features_; }
+
  private:
   ValidationState_t(const ValidationState_t&);
 
@@ -361,6 +371,10 @@ class ValidationState_t {
 
   /// NOTE: See correspoding getter functions
   bool in_function_;
+
+  // The state of optional features.  These are determined by capabilities
+  // declared by the module.
+  Feature features_;
 };
 
 }  /// namespace libspirv
index 3d50328..3927ab5 100644 (file)
@@ -69,8 +69,7 @@ spv_result_t ValidateFloatSize(ValidationState_t& _,
     return SPV_SUCCESS;
   }
   if (num_bits == 16) {
-    if (_.HasCapability(SpvCapabilityFloat16) ||
-        _.HasCapability(SpvCapabilityFloat16Buffer)) {
+    if (_.features().declare_float16_type) {
       return SPV_SUCCESS;
     }
     return _.diag(SPV_ERROR_INVALID_DATA)
@@ -108,7 +107,7 @@ spv_result_t ValidateIntSize(ValidationState_t& _,
            << "Using an 8-bit integer type requires the Int8 capability.";
   }
   if (num_bits == 16) {
-    if (_.HasCapability(SpvCapabilityInt16)) {
+    if (_.features().declare_int16_type) {
       return SPV_SUCCESS;
     }
     return _.diag(SPV_ERROR_INVALID_DATA)