From 3d27da4bc7780da2538c12368362a2ffdf342760 Mon Sep 17 00:00:00 2001 From: Dejan Mircevski Date: Thu, 31 Mar 2016 12:16:51 -0400 Subject: [PATCH] Add a target environment to spvContextCreate(). Run clang-format. --- include/spirv-tools/libspirv.h | 14 ++++++++++++-- source/table.cpp | 12 +++++++++++- test/BinaryToText.cpp | 16 ++++++++-------- test/ExtInstGLSLstd450.cpp | 9 +++++---- test/TestFixture.h | 10 +++++----- test/TextDestroy.cpp | 2 +- test/TextToBinary.cpp | 33 +++++++++++++++++++++++++++------ test/ValidateFixtures.cpp | 6 ++++-- test/ValidateID.cpp | 10 +++++----- test/ValidationState.cpp | 3 ++- tools/as/as.cpp | 2 +- tools/dis/dis.cpp | 2 +- tools/val/val.cpp | 7 +++++-- 13 files changed, 87 insertions(+), 39 deletions(-) diff --git a/include/spirv-tools/libspirv.h b/include/spirv-tools/libspirv.h index 9bd8a3b..7de0750 100644 --- a/include/spirv-tools/libspirv.h +++ b/include/spirv-tools/libspirv.h @@ -343,8 +343,18 @@ typedef spv_context_t* spv_context; // Platform API -// Creates a context object. -spv_context spvContextCreate(); +// Certain target environments impose additional restrictions on SPIR-V, so it's +// often necessary to specify which one applies. SPV_ENV_UNIVERSAL means +// environment-agnostic SPIR-V. +typedef enum { + SPV_ENV_UNIVERSAL_1_0, // SPIR-V 1.0 any revision, no other restrictions. + SPV_ENV_UNIVERSAL_1_0_4, // SPIR-V 1.0 revision 4, no other restrictions. + SPV_ENV_VULKAN_1_0, // Vulkan 1.0 any revision. + SPV_ENV_VULKAN_1_0_7 // Vulkan 1.0 revision 7. +} spv_target_env; + +// Creates a context object. Returns null if env is invalid. +spv_context spvContextCreate(spv_target_env env); // Destroys the given context object. void spvContextDestroy(spv_context context); diff --git a/source/table.cpp b/source/table.cpp index 1a32f2d..9488b1c 100644 --- a/source/table.cpp +++ b/source/table.cpp @@ -28,7 +28,17 @@ #include -spv_context spvContextCreate() { +spv_context spvContextCreate(spv_target_env env) { + switch (env) { + case SPV_ENV_UNIVERSAL_1_0_4: + case SPV_ENV_UNIVERSAL_1_0: + case SPV_ENV_VULKAN_1_0_7: + case SPV_ENV_VULKAN_1_0: + break; + default: + return nullptr; + } + spv_opcode_table opcode_table; spv_operand_table operand_table; spv_ext_inst_table ext_inst_table; diff --git a/test/BinaryToText.cpp b/test/BinaryToText.cpp index cb4f6ad..8503705 100644 --- a/test/BinaryToText.cpp +++ b/test/BinaryToText.cpp @@ -30,8 +30,8 @@ #include "gmock/gmock.h" -#include "source/spirv_constant.h" #include "TestFixture.h" +#include "source/spirv_constant.h" using ::testing::Eq; using ::testing::HasSubstr; @@ -41,7 +41,7 @@ using spvtest::TextToBinaryTest; namespace { class BinaryToText : public ::testing::Test { public: - BinaryToText() : context(spvContextCreate()) {} + BinaryToText() : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)) {} ~BinaryToText() { spvContextDestroy(context); } virtual void SetUp() { @@ -203,7 +203,7 @@ INSTANTIATE_TEST_CASE_P( "%2 = OpTypeVector %1 4", spvtest::MakeInstruction(SpvOpConstant, {2, 3, 999}), "Type Id 2 is not a scalar numeric type"}, - }),); + }), ); INSTANTIATE_TEST_CASE_P( InvalidIdsCheckedDuringLiteralCaseParsing, BinaryToTextFail, @@ -219,7 +219,7 @@ INSTANTIATE_TEST_CASE_P( {"%1 = OpTypeFloat 32\n%2 = OpConstant %1 1.5", spvtest::MakeInstruction(SpvOpSwitch, {2, 3, 4, 5}), "Invalid OpSwitch: selector id 2 is not a scalar integer"}, - }),); + }), ); TEST_F(TextToBinaryTest, OneInstruction) { const std::string input = "OpSource OpenCL_C 12\n"; @@ -275,7 +275,7 @@ INSTANTIATE_TEST_CASE_P( "OpDecorate %1 FPFastMathMode NotNaN|NotInf\n", "OpDecorate %1 FPFastMathMode NSZ|AllowRecip\n", "OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n", - }),); + }), ); INSTANTIATE_TEST_CASE_P(LoopControlMasks, RoundTripInstructionsTest, ::testing::ValuesIn(std::vector{ @@ -283,7 +283,7 @@ INSTANTIATE_TEST_CASE_P(LoopControlMasks, RoundTripInstructionsTest, "OpLoopMerge %1 %2 Unroll\n", "OpLoopMerge %1 %2 DontUnroll\n", "OpLoopMerge %1 %2 Unroll|DontUnroll\n", - }),); + }), ); INSTANTIATE_TEST_CASE_P(SelectionControlMasks, RoundTripInstructionsTest, ::testing::ValuesIn(std::vector{ @@ -291,7 +291,7 @@ INSTANTIATE_TEST_CASE_P(SelectionControlMasks, RoundTripInstructionsTest, "OpSelectionMerge %1 Flatten\n", "OpSelectionMerge %1 DontFlatten\n", "OpSelectionMerge %1 Flatten|DontFlatten\n", - }),); + }), ); // clang-format off INSTANTIATE_TEST_CASE_P( @@ -474,6 +474,6 @@ INSTANTIATE_TEST_CASE_P(GeneratorStrings, GeneratorStringTest, "Khronos Glslang Reference Front End; 1"}, {9, 18, "Unknown(9); 18"}, {65535, 32767, "Unknown(65535); 32767"}, - }),); + }), ); } // anonymous namespace diff --git a/test/ExtInstGLSLstd450.cpp b/test/ExtInstGLSLstd450.cpp index 2035854..9934e59 100644 --- a/test/ExtInstGLSLstd450.cpp +++ b/test/ExtInstGLSLstd450.cpp @@ -49,7 +49,7 @@ struct ExtInstContext { using ExtInstGLSLstd450RoundTripTest = ::testing::TestWithParam; TEST_P(ExtInstGLSLstd450RoundTripTest, ParameterizedExtInst) { - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); const std::string spirv = R"( OpCapability Shader %1 = OpExtInstImport "GLSL.std.450" @@ -94,8 +94,9 @@ OpFunctionEnd EXPECT_NE(binary->code + binary->wordCount, std::search(binary->code, binary->code + binary->wordCount, expected_contains.begin(), expected_contains.end())) - << "Cannot find\n" << spvtest::WordVector(expected_contains).str() - << "in\n" << spvtest::WordVector(*binary).str(); + << "Cannot find\n" + << spvtest::WordVector(expected_contains).str() << "in\n" + << spvtest::WordVector(*binary).str(); // Check round trip gives the same text. spv_text output_text = nullptr; @@ -205,6 +206,6 @@ INSTANTIATE_TEST_CASE_P( {"NMin", "%5 %5", 79, 7, {5, 5}}, {"NMax", "%5 %5", 80, 7, {5, 5}}, {"NClamp", "%5 %5 %5", 81, 8, {5, 5, 5}}, - })),); + })), ); } // anonymous namespace diff --git a/test/TestFixture.h b/test/TestFixture.h index 77f51a2..c3edd32 100644 --- a/test/TestFixture.h +++ b/test/TestFixture.h @@ -43,7 +43,7 @@ class TextToBinaryTestBase : public T { const SpirvVector::size_type kFirstInstruction = 5; TextToBinaryTestBase() - : context(spvContextCreate()), + : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), diagnostic(nullptr), text(), binary(nullptr) { @@ -66,8 +66,8 @@ class TextToBinaryTestBase : public T { // Compiles SPIR-V text in the given assembly syntax format, asserting // compilation success. Returns the compiled code. SpirvVector CompileSuccessfully(const std::string& txt) { - spv_result_t status = spvTextToBinary(context, txt.c_str(), txt.size(), - &binary, &diagnostic); + spv_result_t status = + spvTextToBinary(context, txt.c_str(), txt.size(), &binary, &diagnostic); EXPECT_EQ(SPV_SUCCESS, status) << txt; SpirvVector code_copy; if (status == SPV_SUCCESS) { @@ -100,8 +100,8 @@ class TextToBinaryTestBase : public T { std::string EncodeAndDecodeSuccessfully(const std::string& txt, uint32_t disassemble_options) { DestroyBinary(); - spv_result_t error = spvTextToBinary(context, txt.c_str(), txt.size(), - &binary, &diagnostic); + spv_result_t error = + spvTextToBinary(context, txt.c_str(), txt.size(), &binary, &diagnostic); if (error) { spvDiagnosticPrint(diagnostic); spvDiagnosticDestroy(diagnostic); diff --git a/test/TextDestroy.cpp b/test/TextDestroy.cpp index ef789ad..c721fdc 100644 --- a/test/TextDestroy.cpp +++ b/test/TextDestroy.cpp @@ -31,7 +31,7 @@ namespace { TEST(TextDestroy, DestroyNull) { spvBinaryDestroy(nullptr); } TEST(TextDestroy, Default) { - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); char textStr[] = R"( OpSource OpenCL_C 12 OpMemoryModel Physical64 OpenCL diff --git a/test/TextToBinary.cpp b/test/TextToBinary.cpp index a2e490a..2ea1be6 100644 --- a/test/TextToBinary.cpp +++ b/test/TextToBinary.cpp @@ -25,6 +25,7 @@ // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. #include +#include #include #include @@ -45,6 +46,8 @@ using spvtest::Concatenate; using spvtest::MakeInstruction; using spvtest::TextToBinaryTest; using testing::Eq; +using testing::IsNull; +using testing::NotNull; // An mask parsing test case. struct MaskCase { @@ -56,7 +59,7 @@ struct MaskCase { using GoodMaskParseTest = ::testing::TestWithParam; TEST_P(GoodMaskParseTest, GoodMaskExpressions) { - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); uint32_t value; EXPECT_EQ(SPV_SUCCESS, @@ -96,12 +99,12 @@ INSTANTIATE_TEST_CASE_P( {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 4, "Pure"}, {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 8, "Const"}, {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0xd, "Inline|Const|Pure"}, - }),); + }), ); using BadFPFastMathMaskParseTest = ::testing::TestWithParam; TEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) { - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); uint32_t value; EXPECT_NE(SPV_SUCCESS, @@ -116,7 +119,7 @@ INSTANTIATE_TEST_CASE_P(ParseMask, BadFPFastMathMaskParseTest, nullptr, "", "NotValidEnum", "|", "NotInf|", "|NotInf", "NotInf||NotNaN", "Unroll" // A good word, but for the wrong enum - }),); + }), ); TEST_F(TextToBinaryTest, InvalidText) { ASSERT_EQ(SPV_ERROR_INVALID_TEXT, @@ -220,7 +223,7 @@ INSTANTIATE_TEST_CASE_P( {"-2.5", 0xc0200000}, {"!0xff800000", 0xff800000}, // -inf {"!0xff800001", 0xff800001}, // NaN - }),); + }), ); using TextToBinaryHalfValueTest = spvtest::TextToBinaryTestBase< ::testing::TestWithParam>>; @@ -252,7 +255,7 @@ INSTANTIATE_TEST_CASE_P( {"0x1.8p4", 0x00004e00}, {"0x1.801p4", 0x00004e00}, {"0x1.804p4", 0x00004e01}, - }),); + }), ); TEST(AssemblyContextParseNarrowSignedIntegers, Sample) { AssemblyContext context(AutoText(""), nullptr); @@ -538,4 +541,22 @@ TEST(AssemblyContextParseMessages, Errors) { spvDiagnosticDestroy(diag); } +TEST(CreateContext, InvalidEnvironment) { + spv_target_env env; + std::memset(&env, 99, sizeof(env)); + EXPECT_THAT(spvContextCreate(env), IsNull()); +} + +TEST(CreateContext, UniversalEnvironment) { + auto c = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); + EXPECT_THAT(c, NotNull()); + spvContextDestroy(c); +} + +TEST(CreateContext, VulkanEnvironment) { + auto c = spvContextCreate(SPV_ENV_VULKAN_1_0); + EXPECT_THAT(c, NotNull()); + spvContextDestroy(c); +} + } // anonymous namespace diff --git a/test/ValidateFixtures.cpp b/test/ValidateFixtures.cpp index e51a580..1830895 100644 --- a/test/ValidateFixtures.cpp +++ b/test/ValidateFixtures.cpp @@ -26,8 +26,8 @@ // Common validation fixtures for unit tests -#include "UnitSPIRV.h" #include "ValidateFixtures.h" +#include "UnitSPIRV.h" #include #include @@ -37,7 +37,9 @@ namespace spvtest { template ValidateBase::ValidateBase() - : context_(spvContextCreate()), binary_(), diagnostic_() {} + : context_(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), + binary_(), + diagnostic_() {} template ValidateBase::~ValidateBase() { diff --git a/test/ValidateID.cpp b/test/ValidateID.cpp index 171ec97..e29672f 100644 --- a/test/ValidateID.cpp +++ b/test/ValidateID.cpp @@ -71,7 +71,7 @@ const char kOpenCLMemoryModel64[] = R"( #define CHECK(str, expected) \ spv_diagnostic diagnostic; \ - spv_context context = spvContextCreate(); \ + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); \ std::string shader = std::string(kGLSL450MemoryModel) + str; \ spv_result_t error = spvTextToBinary(context, shader.c_str(), shader.size(), \ &binary, &diagnostic); \ @@ -91,10 +91,10 @@ const char kOpenCLMemoryModel64[] = R"( #define CHECK_KERNEL(str, expected, bitness) \ ASSERT_TRUE(bitness == 32 || bitness == 64); \ spv_diagnostic diagnostic; \ - spv_context context = spvContextCreate(); \ - std::string kernel = std::string(bitness == 32 ? \ - kOpenCLMemoryModel32 : \ - kOpenCLMemoryModel64) + str; \ + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); \ + std::string kernel = std::string(bitness == 32 ? kOpenCLMemoryModel32 \ + : kOpenCLMemoryModel64) + \ + str; \ spv_result_t error = spvTextToBinary(context, kernel.c_str(), kernel.size(), \ &binary, &diagnostic); \ if (error) { \ diff --git a/test/ValidationState.cpp b/test/ValidationState.cpp index e38516d..bbc4f60 100644 --- a/test/ValidationState.cpp +++ b/test/ValidationState.cpp @@ -42,7 +42,8 @@ using std::vector; class ValidationStateTest : public testing::Test { public: ValidationStateTest() - : context_(spvContextCreate()), state_(&diag_, context_) {} + : context_(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), + state_(&diag_, context_) {} protected: spv_diagnostic diag_; diff --git a/tools/as/as.cpp b/tools/as/as.cpp index 8166d76..b297610 100644 --- a/tools/as/as.cpp +++ b/tools/as/as.cpp @@ -129,7 +129,7 @@ int main(int argc, char** argv) { spv_binary binary; spv_diagnostic diagnostic = nullptr; - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0_4); spv_result_t error = spvTextToBinary(context, contents.data(), contents.size(), &binary, &diagnostic); spvContextDestroy(context); diff --git a/tools/dis/dis.cpp b/tools/dis/dis.cpp index 1f12f50..9fbaed5 100644 --- a/tools/dis/dis.cpp +++ b/tools/dis/dis.cpp @@ -179,7 +179,7 @@ int main(int argc, char** argv) { spv_text text; spv_text* textOrNull = print_to_stdout ? nullptr : &text; spv_diagnostic diagnostic = nullptr; - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0_4); spv_result_t error = spvBinaryToText(context, contents.data(), contents.size(), options, textOrNull, &diagnostic); diff --git a/tools/val/val.cpp b/tools/val/val.cpp index 76220fa..d3ebfb7 100644 --- a/tools/val/val.cpp +++ b/tools/val/val.cpp @@ -52,10 +52,11 @@ Options: const char kBuildVersion[] = #include "build-version.inc" -; + ; int main(int argc, char** argv) { const char* inFile = nullptr; + spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0_4; for (int argi = 1; argi < argc; ++argi) { const char* cur_arg = argv[argi]; @@ -68,6 +69,8 @@ int main(int argc, char** argv) { } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) { print_usage(argv[0]); return 0; + } else if (0 == strcmp(cur_arg, "--vulkan")) { + target_env = SPV_ENV_VULKAN_1_0_7; } else if (0 == cur_arg[1]) { // Setting a filename of "-" to indicate stdin. if (!inFile) { @@ -108,7 +111,7 @@ int main(int argc, char** argv) { spv_const_binary_t binary = {contents.data(), contents.size()}; spv_diagnostic diagnostic = nullptr; - spv_context context = spvContextCreate(); + spv_context context = spvContextCreate(target_env); spv_result_t error = spvValidate(context, &binary, &diagnostic); spvContextDestroy(context); if (error) { -- 2.7.4