New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension allows multiple same OpTypePointer types #783
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
#include "val/validation_state.h" | ||
|
||
#include <cassert> | ||
#include <map> | ||
|
||
#include "opcode.h" | ||
#include "val/basic_block.h" | ||
|
@@ -425,6 +426,25 @@ bool ValidationState_t::RegisterUniqueTypeDeclaration( | |
key.insert(key.end(), inst.words + words_begin, inst.words + words_end); | ||
} | ||
|
||
// If module has extension SPV_KHR_variable_pointers, then OpTypePointer | ||
// is allowed to create identical types so long as their decorations differ. | ||
if (inst.opcode == SpvOpTypePointer && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to have a comment describing the rule you're enforcing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
HasExtension(Extension::kSPV_KHR_variable_pointers)) { | ||
const std::vector<Decoration>& decorations = id_decorations(inst.result_id); | ||
std::map<SpvDecoration, const std::vector<uint32_t>*> decoration_params; | ||
for (const auto& decoration : decorations) { | ||
decoration_params[decoration.dec_type()] = &decoration.params(); | ||
} | ||
for (const auto& kv : decoration_params) { | ||
// Append decoration data to the key, sorted by decoration type. | ||
// Use magic number to separate unrelated chunks of data. | ||
const uint32_t kMagicSeparator = 0xffff9296; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low level comment: This magic number mechanism is fragile. Better to push a kv.second.size() then its contents. (But really we should not care about duplication of decorations.) |
||
key.push_back(kMagicSeparator); | ||
key.push_back(static_cast<uint32_t>(kv.first)); | ||
key.insert(key.end(), kv.second->begin(), kv.second->end()); | ||
} | ||
} | ||
|
||
return unique_type_declarations_.insert(std::move(key)).second; | ||
} | ||
} /// namespace libspirv |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,7 +95,7 @@ OpFunctionEnd | |
// declaration. | ||
string GetErrorString(SpvOp opcode) { | ||
return "Duplicate non-aggregate type declarations are not allowed. Opcode: " | ||
+ std::to_string(opcode); | ||
+ std::string(spvOpcodeString(opcode)); | ||
} | ||
|
||
TEST_F(ValidateTypeUnique, success) { | ||
|
@@ -238,4 +238,57 @@ OpMemoryModel Physical32 OpenCL | |
Not(HasSubstr(GetErrorString(SpvOpTypeVoid)))); | ||
} | ||
|
||
TEST_F(ValidateTypeUnique, PointerTypesDifferentArrayStrideNoExtension) { | ||
string str = R"( | ||
OpCapability Shader | ||
OpCapability Linkage | ||
OpMemoryModel Logical GLSL450 | ||
OpDecorate %ptr1 ArrayStride 4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe you just have a typo here? |
||
OpDecorate %ptr2 ArrayStride 8 | ||
%u32 = OpTypeInt 32 0 | ||
%ptr1 = OpTypePointer Input %u32 | ||
%ptr2 = OpTypePointer Input %u32 | ||
)"; | ||
CompileSuccessfully(str.c_str()); | ||
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); | ||
EXPECT_THAT(getDiagnosticString(), | ||
HasSubstr(GetErrorString(SpvOpTypePointer))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused. The validation succeeds, but generates an error string? Does this test even pass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It logs a warning but returns SPV_SUCCESS. |
||
} | ||
|
||
TEST_F(ValidateTypeUnique, PointerTypesDifferentArrayStride) { | ||
string str = R"( | ||
OpCapability Shader | ||
OpCapability Linkage | ||
OpExtension "SPV_KHR_variable_pointers" | ||
OpMemoryModel Logical GLSL450 | ||
OpDecorate %ptr1 ArrayStride 4 | ||
OpDecorate %ptr2 ArrayStride 8 | ||
%u32 = OpTypeInt 32 0 | ||
%ptr1 = OpTypePointer Input %u32 | ||
%ptr2 = OpTypePointer Input %u32 | ||
)"; | ||
CompileSuccessfully(str.c_str()); | ||
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); | ||
EXPECT_THAT(getDiagnosticString(), | ||
Not(HasSubstr(GetErrorString(SpvOpTypePointer)))); | ||
} | ||
|
||
TEST_F(ValidateTypeUnique, PointerTypesSameArrayStride) { | ||
string str = R"( | ||
OpCapability Shader | ||
OpCapability Linkage | ||
OpExtension "SPV_KHR_variable_pointers" | ||
OpMemoryModel Logical GLSL450 | ||
OpDecorate %ptr1 ArrayStride 4 | ||
OpDecorate %ptr2 ArrayStride 4 | ||
%u32 = OpTypeInt 32 0 | ||
%ptr1 = OpTypePointer Input %u32 | ||
%ptr2 = OpTypePointer Input %u32 | ||
)"; | ||
CompileSuccessfully(str.c_str()); | ||
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); | ||
EXPECT_THAT(getDiagnosticString(), | ||
HasSubstr(GetErrorString(SpvOpTypePointer))); | ||
} | ||
|
||
} // anonymous namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking again, this is not the rule.
The SPV_KHR_variable_pointers extension says
From 2.8 in the SPIR-V spec:
That means the validator should allow distinct OpTypePointer with the same operands, and with the same decorations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I think all this new code boils down to:
Even better: put the check up in the callee, along with the aggregate types. That is, modify TypeUniquePass to do the pointer-and-has-extension check.