Allow relaxing validation of pointers in logical addressing mode
authorLei Zhang <antiagainst@google.com>
Sun, 7 Jan 2018 15:50:01 +0000 (10:50 -0500)
committerDavid Neto <dneto@google.com>
Mon, 8 Jan 2018 15:36:23 +0000 (10:36 -0500)
In HLSL structured buffer legalization, pointer to pointer types
are emitted to indicate a structured buffer variable should be
treated as an alias of some other variable. We need an option to
relax the check of pointer types in logical addressing mode to
catch other validation errors.

include/spirv-tools/libspirv.h
include/spirv-tools/libspirv.hpp
source/spirv_validator_options.cpp
source/spirv_validator_options.h
source/validate_id.cpp
test/val/val_id_test.cpp
tools/val/val.cpp

index ed7b820..e8ff4a5 100644 (file)
@@ -441,6 +441,15 @@ void spvValidatorOptionsSetUniversalLimit(spv_validator_options options,
 void spvValidatorOptionsSetRelaxStoreStruct(spv_validator_options options,
                                             bool val);
 
+// Records whether or not the validator should relax the rules on pointer usage
+// in logical addressing mode.
+//
+// When relaxed, it will allow the following usage cases of pointers:
+// 1) OpVariable allocating an object whose type is a pointer type
+// 2) OpReturnValue returning a pointer value
+void spvValidatorOptionsSetRelaxLogicalPointer(spv_validator_options options,
+                                               bool val);
+
 // Encodes the given SPIR-V assembly text to its binary representation. The
 // length parameter specifies the number of bytes for text. Encoded binary will
 // be stored into *binary. Any error will be written into *diagnostic if
index 6b9ad27..2e4aa62 100644 (file)
@@ -81,6 +81,16 @@ class ValidatorOptions {
     spvValidatorOptionsSetRelaxStoreStruct(options_, val);
   }
 
+  // Records whether or not the validator should relax the rules on pointer
+  // usage in logical addressing mode.
+  //
+  // When relaxed, it will allow the following usage cases of pointers:
+  // 1) OpVariable allocating an object whose type is a pointer type
+  // 2) OpReturnValue returning a pointer value
+  void SetRelaxLogicalPointer(bool val) {
+    spvValidatorOptionsSetRelaxLogicalPointer(options_, val);
+  }
+
  private:
   spv_validator_options options_;
 };
index e676c5e..144a168 100644 (file)
@@ -81,3 +81,8 @@ void spvValidatorOptionsSetRelaxStoreStruct(spv_validator_options options,
                                             bool val) {
   options->relax_struct_store = val;
 }
+
+void spvValidatorOptionsSetRelaxLogicalPointer(spv_validator_options options,
+                                               bool val) {
+  options->relax_logcial_pointer = val;
+}
index 4a67812..d15b63b 100644 (file)
@@ -37,10 +37,14 @@ struct validator_universal_limits_t {
 // Manages command line options passed to the SPIR-V Validator. New struct
 // members may be added for any new option.
 struct spv_validator_options_t {
-  spv_validator_options_t() : universal_limits_(), relax_struct_store(false) {}
+  spv_validator_options_t()
+      : universal_limits_(),
+        relax_struct_store(false),
+        relax_logcial_pointer(false) {}
 
   validator_universal_limits_t universal_limits_;
   bool relax_struct_store;
+  bool relax_logcial_pointer;
 };
 
 #endif  // LIBSPIRV_SPIRV_VALIDATOR_OPTIONS_H_
index 8b2eee0..2267f38 100644 (file)
@@ -1879,16 +1879,20 @@ bool idUsage::isValid<SpvOpReturnValue>(const spv_instruction_t* inst,
                      << "' is missing or void.";
     return false;
   }
+
   const bool uses_variable_pointer =
       module_.features().variable_pointers ||
       module_.features().variable_pointers_storage_buffer;
+
   if (addressingModel == SpvAddressingModelLogical &&
-      SpvOpTypePointer == valueType->opcode() && !uses_variable_pointer) {
+      SpvOpTypePointer == valueType->opcode() && !uses_variable_pointer &&
+      !module_.options()->relax_logcial_pointer) {
     DIAG(valueIndex)
         << "OpReturnValue value's type <id> '" << value->type_id()
         << "' is a pointer, which is invalid in the Logical addressing model.";
     return false;
   }
+
   // NOTE: Find OpFunction
   const spv_instruction_t* function = inst - 1;
   while (firstInst != function) {
index 3bf11a4..8278b16 100644 (file)
@@ -2426,6 +2426,48 @@ TEST_F(ValidateIdWithMessage, OpStoreTypeBadRelaxedStruct2) {
                 "<id> '16's layout."));
 }
 
+TEST_F(ValidateIdWithMessage, OpStoreTypeRelaxedLogicalPointerReturnPointer) {
+  const string spirv = R"(
+     OpCapability Shader
+     OpCapability Linkage
+     OpMemoryModel Logical GLSL450
+%1 = OpTypeInt 32 1
+%2 = OpTypePointer Function %1
+%3 = OpTypeFunction %2 %2
+%4 = OpFunction %2 None %3
+%5 = OpFunctionParameter %2
+%6 = OpLabel
+     OpReturnValue %5
+     OpFunctionEnd)";
+
+  spvValidatorOptionsSetRelaxLogicalPointer(options_, true);
+  CompileSuccessfully(spirv.c_str());
+  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
+}
+
+TEST_F(ValidateIdWithMessage, OpStoreTypeRelaxedLogicalPointerAllocPointer) {
+  const string spirv = R"(
+      OpCapability Shader
+      OpCapability Linkage
+      OpMemoryModel Logical GLSL450
+ %1 = OpTypeVoid
+ %2 = OpTypeInt 32 1
+ %3 = OpTypeFunction %1          ; void(void)
+ %4 = OpTypePointer Uniform %2   ; int*
+ %5 = OpTypePointer Private %4   ; int** (Private)
+ %6 = OpTypePointer Function %4  ; int** (Function)
+ %7 = OpVariable %5 Private
+ %8 = OpFunction %1 None %3
+ %9 = OpLabel
+%10 = OpVariable %6 Function
+      OpReturn
+      OpFunctionEnd)";
+
+  spvValidatorOptionsSetRelaxLogicalPointer(options_, true);
+  CompileSuccessfully(spirv.c_str());
+  EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
+}
+
 TEST_F(ValidateIdWithMessage, OpStoreVoid) {
   string spirv = kGLSL450MemoryModel + R"(
 %1 = OpTypeVoid
index 050e7d6..633d2f6 100644 (file)
@@ -44,6 +44,8 @@ Options:
   --max-function-args              <maximum number arguments allowed per function>
   --max-control-flow-nesting-depth <maximum Control Flow nesting depth allowed>
   --max-access-chain-indexes       <maximum number of indexes allowed to use for Access Chain instructions>
+  --relax-logcial-pointer          Allow allocating an object of a pointer type and returning
+                                   a pointer value from a function in logical addressing mode
   --relax-struct-store             Allow store from one struct type to a
                                    different type with compatible layout and
                                    members.
@@ -112,6 +114,8 @@ int main(int argc, char** argv) {
           continue_processing = false;
           return_code = 1;
         }
+      } else if (0 == strcmp(cur_arg, "--relax-logical-pointer")) {
+        options.SetRelaxLogicalPointer(true);
       } else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
         options.SetRelaxStructStore(true);
       } else if (0 == cur_arg[1]) {