Skip to content

Commit f2dac2f

Browse files
spirv-val: Add AllowVulkan32BitBitwise option (KhronosGroup#6001)
* Allows some bitwise instructions to use non-32-bit base operands
1 parent a80d3b5 commit f2dac2f

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

include/spirv-tools/libspirv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetAllowLocalSizeId(
740740
SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetAllowOffsetTextureOperand(
741741
spv_validator_options options, bool val);
742742

743+
// Allow base operands of some bit operations to be non-32-bit wide.
744+
SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetAllowVulkan32BitBitwise(
745+
spv_validator_options options, bool val);
746+
743747
// Whether friendly names should be used in validation error messages.
744748
SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetFriendlyNames(
745749
spv_validator_options options, bool val);

include/spirv-tools/libspirv.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ class SPIRV_TOOLS_EXPORT ValidatorOptions {
132132
spvValidatorOptionsSetAllowOffsetTextureOperand(options_, val);
133133
}
134134

135+
// Allow base operands of some bit operations to be non-32-bit wide.
136+
void SetAllowVulkan32BitBitwise(bool val) {
137+
spvValidatorOptionsSetAllowVulkan32BitBitwise(options_, val);
138+
}
139+
135140
// Records whether or not the validator should relax the rules on pointer
136141
// usage in logical addressing mode.
137142
//

source/spirv_validator_options.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ void spvValidatorOptionsSetAllowOffsetTextureOperand(
131131
options->allow_offset_texture_operand = val;
132132
}
133133

134+
void spvValidatorOptionsSetAllowVulkan32BitBitwise(
135+
spv_validator_options options, bool val) {
136+
options->allow_vulkan_32_bit_bitwise = val;
137+
}
138+
134139
void spvValidatorOptionsSetFriendlyNames(spv_validator_options options,
135140
bool val) {
136141
options->use_friendly_names = val;

source/spirv_validator_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct spv_validator_options_t {
4949
skip_block_layout(false),
5050
allow_localsizeid(false),
5151
allow_offset_texture_operand(false),
52+
allow_vulkan_32_bit_bitwise(false),
5253
before_hlsl_legalization(false),
5354
use_friendly_names(true) {}
5455

@@ -62,6 +63,7 @@ struct spv_validator_options_t {
6263
bool skip_block_layout;
6364
bool allow_localsizeid;
6465
bool allow_offset_texture_operand;
66+
bool allow_vulkan_32_bit_bitwise;
6567
bool before_hlsl_legalization;
6668
bool use_friendly_names;
6769
};

source/val/validate_bitwise.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ spv_result_t ValidateBaseType(ValidationState_t& _, const Instruction* inst,
3030

3131
if (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type)) {
3232
return _.diag(SPV_ERROR_INVALID_DATA, inst)
33-
<< _.VkErrorID(4781)
3433
<< "Expected int scalar or vector type for Base operand: "
3534
<< spvOpcodeString(opcode);
3635
}
3736

3837
// Vulkan has a restriction to 32 bit for base
3938
if (spvIsVulkanEnv(_.context()->target_env)) {
40-
if (_.GetBitWidth(base_type) != 32) {
39+
if (_.GetBitWidth(base_type) != 32 &&
40+
!_.options()->allow_vulkan_32_bit_bitwise) {
4141
return _.diag(SPV_ERROR_INVALID_DATA, inst)
4242
<< _.VkErrorID(4781)
4343
<< "Expected 32-bit int type for Base operand: "

test/val/val_bitwise_test.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ TEST_F(ValidateBitwise, OpBitFieldInsertNot32Vulkan) {
427427
HasSubstr("Expected 32-bit int type for Base operand: BitFieldInsert"));
428428
}
429429

430+
TEST_F(ValidateBitwise, OpBitFieldInsertNot32Allow) {
431+
const std::string body = R"(
432+
%val1 = OpBitFieldInsert %u64 %u64_1 %u64_2 %s32_1 %s32_2
433+
)";
434+
435+
CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_VULKAN_1_0);
436+
spvValidatorOptionsSetAllowVulkan32BitBitwise(getValidatorOptions(), true);
437+
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
438+
}
439+
430440
TEST_F(ValidateBitwise, OpBitFieldSExtractSuccess) {
431441
const std::string body = R"(
432442
%val1 = OpBitFieldSExtract %u64 %u64_1 %s32_1 %s32_2
@@ -607,10 +617,8 @@ TEST_F(ValidateBitwise, OpBitCountBaseNotInt) {
607617
%val1 = OpBitCount %u32 %f64_1
608618
)";
609619

610-
CompileSuccessfully(GenerateShaderCode(body).c_str(), SPV_ENV_VULKAN_1_0);
611-
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
612-
EXPECT_THAT(getDiagnosticString(),
613-
AnyVUID("VUID-StandaloneSpirv-Base-04781"));
620+
CompileSuccessfully(GenerateShaderCode(body).c_str());
621+
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
614622
EXPECT_THAT(
615623
getDiagnosticString(),
616624
HasSubstr(

tools/val/val.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ NOTE: The validator is a work in progress.
6868
be allowed by the target environment.
6969
--allow-offset-texture-operand Allow use of the Offset texture operands where it would otherwise not
7070
be allowed by the target environment.
71+
--allow-vulkan-32-bit-bitwise Allow use of non-32 bit for the Base operand where it would otherwise
72+
not be allowed by the target environment.
7173
--before-hlsl-legalization Allows code patterns that are intended to be
7274
fixed by spirv-opt's legalization passes.
7375
--version Display validator version information.
@@ -165,6 +167,8 @@ int main(int argc, char** argv) {
165167
options.SetAllowLocalSizeId(true);
166168
} else if (0 == strcmp(cur_arg, "--allow-offset-texture-operand")) {
167169
options.SetAllowOffsetTextureOperand(true);
170+
} else if (0 == strcmp(cur_arg, "--allow-vulkan-32-bit-bitwise")) {
171+
options.SetAllowVulkan32BitBitwise(true);
168172
} else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
169173
options.SetRelaxStructStore(true);
170174
} else if (0 == cur_arg[1]) {

0 commit comments

Comments
 (0)