Support SPV_KHR_linkonce_odr, SPV_KHR_expect_assume (#4161)

* SPV_KHR_linkonce_odr

Covers:
- Assembler
- Disassembler
- Validator

* Support SPV_KHR_expect_assume

Covers:
- assembler
- disassembler
- validation
This commit is contained in:
David Neto 2021-03-16 09:51:59 -04:00 коммит произвёл GitHub
Родитель 478754c005
Коммит 4100477e76
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 484 добавлений и 0 удалений

Просмотреть файл

@ -72,6 +72,37 @@ spv_result_t ValidateShaderClock(ValidationState_t& _,
return SPV_SUCCESS;
}
spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
const auto operand_type_id = _.GetOperandTypeId(inst, 0);
if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Value operand of OpAssumeTrueKHR must be a boolean scalar";
}
return SPV_SUCCESS;
}
spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
const auto result_type = inst->type_id();
if (!_.IsBoolScalarOrVectorType(result_type) &&
!_.IsIntScalarOrVectorType(result_type)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Result of OpExpectKHR must be a scalar or vector of integer "
"type or boolean type";
}
if (_.GetOperandTypeId(inst, 2) != result_type) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Type of Value operand of OpExpectKHR does not match the result "
"type ";
}
if (_.GetOperandTypeId(inst, 3) != result_type) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Type of ExpectedValue operand of OpExpectKHR does not match the "
"result type ";
}
return SPV_SUCCESS;
}
} // namespace
spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
@ -152,6 +183,16 @@ spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
return error;
}
break;
case SpvOpAssumeTrueKHR:
if (auto error = ValidateAssumeTrue(_, inst)) {
return error;
}
break;
case SpvOpExpectKHR:
if (auto error = ValidateExpect(_, inst)) {
return error;
}
break;
default:
break;
}

Просмотреть файл

@ -905,5 +905,36 @@ INSTANTIATE_TEST_SUITE_P(
MakeInstruction(SpvOpDecorate, {1, 5300})},
})));
// SPV_KHR_linkonce_odr
INSTANTIATE_TEST_SUITE_P(
SPV_KHR_linkonce_odr, ExtensionRoundTripTest,
Combine(
Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_3, SPV_ENV_VULKAN_1_0,
SPV_ENV_VULKAN_1_1, SPV_ENV_VULKAN_1_2),
ValuesIn(std::vector<AssemblyCase>{
{"OpExtension \"SPV_KHR_linkonce_odr\"\n",
MakeInstruction(SpvOpExtension,
MakeVector("SPV_KHR_linkonce_odr"))},
{"OpDecorate %1 LinkageAttributes \"foobar\" LinkOnceODR\n",
MakeInstruction(SpvOpDecorate,
Concatenate({{1, SpvDecorationLinkageAttributes},
MakeVector("foobar"),
{SpvLinkageTypeLinkOnceODR}}))},
})));
// SPV_KHR_expect_assume
INSTANTIATE_TEST_SUITE_P(
SPV_KHR_expect_assume, ExtensionRoundTripTest,
Combine(Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_3,
SPV_ENV_VULKAN_1_0, SPV_ENV_VULKAN_1_1, SPV_ENV_VULKAN_1_2),
ValuesIn(std::vector<AssemblyCase>{
{"OpExtension \"SPV_KHR_expect_assume\"\n",
MakeInstruction(SpvOpExtension,
MakeVector("SPV_KHR_expect_assume"))},
{"OpAssumeTrueKHR %1\n",
MakeInstruction(SpvOpAssumeTrueKHR, {1})}})));
} // namespace
} // namespace spvtools

Просмотреть файл

@ -38,6 +38,8 @@ add_spvtools_unittest(TARGET val_abcde
val_entry_point.cpp
val_explicit_reserved_test.cpp
val_extensions_test.cpp
val_extension_spv_khr_expect_assume.cpp
val_extension_spv_khr_linkonce_odr.cpp
val_extension_spv_khr_terminate_invocation.cpp
val_ext_inst_test.cpp
${VAL_TEST_COMMON_SRCS}

Просмотреть файл

@ -0,0 +1,310 @@
// Copyright (c) 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Tests for OpExtension validator rules.
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "source/enum_string_mapping.h"
#include "source/extensions.h"
#include "source/spirv_target_env.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
#include "test/val/val_fixtures.h"
namespace spvtools {
namespace val {
namespace {
using ::testing::HasSubstr;
using ::testing::Values;
using ::testing::ValuesIn;
using ValidateSpvExpectAssumeKHR = spvtest::ValidateBase<bool>;
TEST_F(ValidateSpvExpectAssumeKHR, Valid) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%bool = OpTypeBool
%true = OpConstantTrue %bool
%undef = OpUndef %bool
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%v2bool = OpTypeVector %bool 2
%v2uint = OpTypeVector %uint 2
%null_v2bool = OpConstantNull %v2bool
%null_v2uint = OpConstantNull %v2uint
%main = OpFunction %void None %voidfn
%entry = OpLabel
OpAssumeTrueKHR %true
OpAssumeTrueKHR %undef ; probably undefined behaviour
%bool_val = OpExpectKHR %bool %true %true
%uint_val = OpExpectKHR %uint %uint_1 %uint_2 ; a bad expectation
%v2bool_val = OpExpectKHR %v2bool %null_v2bool %null_v2bool
%v2uint_val = OpExpectKHR %v2uint %null_v2uint %null_v2uint
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateSpvExpectAssumeKHR, RequiresExtension) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%bool = OpTypeBool
%true = OpConstantTrue %bool
%undef = OpUndef %bool
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%main = OpFunction %void None %voidfn
%entry = OpLabel
OpAssumeTrueKHR %true
OpAssumeTrueKHR %undef ; probably undefined behaviour
%val = OpExpectKHR %uint %uint_1 %uint_2 ; a bad expectation
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Capability: operand ExpectAssumeKHR(5629) requires "
"one of these extensions: SPV_KHR_expect_assume"));
}
TEST_F(ValidateSpvExpectAssumeKHR,
AssumeTrueKHR_RequiresExpectAssumeCapability) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%bool = OpTypeBool
%true = OpConstantTrue %bool
%undef = OpUndef %bool
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%main = OpFunction %void None %voidfn
%entry = OpLabel
OpAssumeTrueKHR %true
OpAssumeTrueKHR %undef ; probably undefined behaviour
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Opcode AssumeTrueKHR requires one of these "
"capabilities: ExpectAssumeKHR \n"
" OpAssumeTrueKHR %true\n"));
}
TEST_F(ValidateSpvExpectAssumeKHR, AssumeTrueKHR_OperandMustBeBool) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%bool = OpTypeBool
%true = OpConstantTrue %bool
%undef = OpUndef %bool
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%main = OpFunction %void None %voidfn
%entry = OpLabel
OpAssumeTrueKHR %uint_1 ; bad type
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(
getDiagnosticString(),
HasSubstr("Value operand of OpAssumeTrueKHR must be a boolean scalar\n"
" OpAssumeTrueKHR %uint_1\n"));
}
TEST_F(ValidateSpvExpectAssumeKHR, ExpectKHR_RequiresExpectAssumeCapability) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%bool = OpTypeBool
%true = OpConstantTrue %bool
%undef = OpUndef %bool
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%uint_2 = OpConstant %uint 2
%main = OpFunction %void None %voidfn
%entry = OpLabel
%val = OpExpectKHR %uint %uint_1 %uint_2 ; a bad expectation
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Opcode ExpectKHR requires one of these capabilities: "
"ExpectAssumeKHR \n"
" %11 = OpExpectKHR %uint %uint_1 %uint_2\n"));
}
TEST_F(ValidateSpvExpectAssumeKHR, ExpectKHR_ResultMustBeBoolOrIntScalar) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%float = OpTypeFloat 32
%float_0 = OpConstant %float 0
%main = OpFunction %void None %voidfn
%entry = OpLabel
%val = OpExpectKHR %float %float_0 %float_0
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Result of OpExpectKHR must be a scalar or vector of "
"integer type or boolean type\n"
" %7 = OpExpectKHR %float %float_0 %float_0\n"));
}
TEST_F(ValidateSpvExpectAssumeKHR, ExpectKHR_Value0MustMatchResultType) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%uint = OpTypeInt 32 0
%float = OpTypeFloat 32
%float_0 = OpConstant %float 0
%main = OpFunction %void None %voidfn
%entry = OpLabel
%val = OpExpectKHR %uint %float_0 %float_0
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Type of Value operand of OpExpectKHR does not match "
"the result type \n"
" %8 = OpExpectKHR %uint %float_0 %float_0\n"));
}
TEST_F(ValidateSpvExpectAssumeKHR, ExpectKHR_Value1MustMatchResultType) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpCapability ExpectAssumeKHR
OpExtension "SPV_KHR_expect_assume"
OpMemoryModel Physical32 OpenCL
%void = OpTypeVoid
%voidfn = OpTypeFunction %void
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%float = OpTypeFloat 32
%float_0 = OpConstant %float 0
%main = OpFunction %void None %voidfn
%entry = OpLabel
%val = OpExpectKHR %uint %uint_0 %float_0
OpReturn
OpFunctionEnd
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Type of ExpectedValue operand of OpExpectKHR does not "
"match the result type \n"
" %9 = OpExpectKHR %uint %uint_0 %float_0\n"));
}
} // namespace
} // namespace val
} // namespace spvtools

Просмотреть файл

@ -0,0 +1,100 @@
// Copyright (c) 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Tests for OpExtension validator rules.
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "source/enum_string_mapping.h"
#include "source/extensions.h"
#include "source/spirv_target_env.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
#include "test/val/val_fixtures.h"
namespace spvtools {
namespace val {
namespace {
using ::testing::HasSubstr;
using ::testing::Values;
using ::testing::ValuesIn;
using ValidateSpvKHRLinkOnceODR = spvtest::ValidateBase<bool>;
TEST_F(ValidateSpvKHRLinkOnceODR, Valid) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpExtension "SPV_KHR_linkonce_odr"
OpMemoryModel Physical32 OpenCL
OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
%uint = OpTypeInt 32 0
%ptr = OpTypePointer CrossWorkgroup %uint
%var = OpVariable %ptr CrossWorkgroup
)";
CompileSuccessfully(str.c_str());
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
}
TEST_F(ValidateSpvKHRLinkOnceODR, RequiresExtension) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpCapability Linkage
OpMemoryModel Physical32 OpenCL
OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
%uint = OpTypeInt 32 0
%ptr = OpTypePointer CrossWorkgroup %uint
%var = OpVariable %ptr CrossWorkgroup
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(
getDiagnosticString(),
HasSubstr("4th operand of Decorate: operand LinkOnceODR(2) requires one "
"of these extensions: SPV_KHR_linkonce_odr \n"
" OpDecorate %1 LinkageAttributes \"foobar\" LinkOnceODR\n"));
}
TEST_F(ValidateSpvKHRLinkOnceODR, RequiresLinkageCapability) {
const std::string str = R"(
OpCapability Kernel
OpCapability Addresses
OpExtension "SPV_KHR_linkonce_odr"
OpMemoryModel Physical32 OpenCL
OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
%uint = OpTypeInt 32 0
%ptr = OpTypePointer CrossWorkgroup %uint
%var = OpVariable %ptr CrossWorkgroup
)";
CompileSuccessfully(str.c_str());
EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
EXPECT_THAT(
getDiagnosticString(),
HasSubstr(
"Operand 2 of Decorate requires one of these capabilities: Linkage \n"
" OpDecorate %1 LinkageAttributes \"foobar\" LinkOnceODR"));
}
} // namespace
} // namespace val
} // namespace spvtools