Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions source/val/validation_state.cpp
Expand Up @@ -15,6 +15,7 @@
#include "val/validation_state.h"

#include <cassert>
#include <map>

#include "opcode.h"
#include "val/basic_block.h"
Expand Down Expand Up @@ -425,6 +426,22 @@ bool ValidationState_t::RegisterUniqueTypeDeclaration(
key.insert(key.end(), inst.words + words_begin, inst.words + words_end);
}

if (inst.opcode == SpvOpTypePointer &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to have a comment describing the rule you're enforcing.

Copy link
Author

Choose a reason for hiding this comment

The 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) {
const auto result = decoration_params.emplace(decoration.dec_type(),
&decoration.params());
(void)result;
assert(result.second);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should assert here? There's nothing wrong with redundant decorations, except that it wastes space.
I don't think the storage behind id_decorations has unique'd it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth saying you're assuming that a single decoration enum can only be set once per target ID.
It think that's true currently, and is likely to be true always. But I don't recall a SPIR-V validation rule about that. (Perhaps an oversight.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will assume that decorations are not unique, but only use the last one.

}
for (const auto& kv : decoration_params) {
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
2 changes: 1 addition & 1 deletion source/validate_type_unique.cpp
Expand Up @@ -49,7 +49,7 @@ spv_result_t TypeUniquePass(ValidationState_t& _,
// return _.diag(SPV_ERROR_INVALID_DATA)
return _.diag(SPV_SUCCESS)
<< "Duplicate non-aggregate type declarations are not allowed."
<< " Opcode: " << inst->opcode;
<< " Opcode: " << spvOpcodeString(SpvOp(inst->opcode));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an improvement. But in actual practice I've found having the ID generated by this instruction is easier to work with. It pinpoints the exact instruction at fault.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}

Expand Down
55 changes: 54 additions & 1 deletion test/val/val_type_unique_test.cpp
Expand Up @@ -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) {
Expand Down Expand Up @@ -238,4 +238,57 @@ OpMemoryModel Physical32 OpenCL
Not(HasSubstr(GetErrorString(SpvOpTypeVoid))));
}

TEST_F(ValidateTypeUnique, PointerTypesSameArrayStrideNoExtension) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name is misleading to me: The array strides here are different, but the test name is ....SameArrayStride...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopyPaste

string str = R"(
OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical GLSL450
OpDecorate %ptr1 ArrayStride 4
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr(GetErrorString(SpvOpTypePointer)));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr(GetErrorString(SpvOpTypePointer)));
}

} // anonymous namespace