diff options
Diffstat (limited to '3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp')
-rw-r--r-- | 3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp b/3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp index c4f450ea958..406da0939f9 100644 --- a/3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp +++ b/3rdparty/bgfx/3rdparty/spirv-tools/test/opt/module_test.cpp @@ -21,6 +21,7 @@ #include "gtest/gtest.h" #include "source/opt/build_module.h" #include "source/opt/module.h" +#include "source/opt/pass.h" #include "spirv-tools/libspirv.hpp" #include "test/opt/module_utils.h" @@ -139,6 +140,161 @@ OpFunctionEnd)"; EXPECT_EQ(text, str.str()); } +TEST(ModuleTest, IdBoundTestAtLimit) { + const std::string text = R"( +OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%1 = OpTypeVoid +%2 = OpTypeFunction %1 +%3 = OpFunction %1 None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd)"; + + std::unique_ptr<IRContext> context = BuildModule(text); + uint32_t current_bound = context->module()->id_bound(); + context->set_max_id_bound(current_bound); + uint32_t next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, 0); + EXPECT_EQ(current_bound, context->module()->id_bound()); + next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, 0); +} + +TEST(ModuleTest, IdBoundTestBelowLimit) { + const std::string text = R"( +OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%1 = OpTypeVoid +%2 = OpTypeFunction %1 +%3 = OpFunction %1 None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd)"; + + std::unique_ptr<IRContext> context = BuildModule(text); + uint32_t current_bound = context->module()->id_bound(); + context->set_max_id_bound(current_bound + 100); + uint32_t next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, current_bound); + EXPECT_EQ(current_bound + 1, context->module()->id_bound()); + next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, current_bound + 1); +} + +TEST(ModuleTest, IdBoundTestNearLimit) { + const std::string text = R"( +OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%1 = OpTypeVoid +%2 = OpTypeFunction %1 +%3 = OpFunction %1 None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd)"; + + std::unique_ptr<IRContext> context = BuildModule(text); + uint32_t current_bound = context->module()->id_bound(); + context->set_max_id_bound(current_bound + 1); + uint32_t next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, current_bound); + EXPECT_EQ(current_bound + 1, context->module()->id_bound()); + next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, 0); +} + +TEST(ModuleTest, IdBoundTestUIntMax) { + const std::string text = R"( +OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%1 = OpTypeVoid +%2 = OpTypeFunction %1 +%3 = OpFunction %1 None %2 +%4294967294 = OpLabel ; ID is UINT_MAX-1 +OpReturn +OpFunctionEnd)"; + + std::unique_ptr<IRContext> context = BuildModule(text); + uint32_t current_bound = context->module()->id_bound(); + + // Expecting |BuildModule| to preserve the numeric ids. + EXPECT_EQ(current_bound, std::numeric_limits<uint32_t>::max()); + + context->set_max_id_bound(current_bound); + uint32_t next_id_bound = context->module()->TakeNextIdBound(); + EXPECT_EQ(next_id_bound, 0); + EXPECT_EQ(current_bound, context->module()->id_bound()); +} + +// Tests that "text" does not change when it is assembled, converted into a +// module, converted back to a binary, and then disassembled. +void AssembleAndDisassemble(const std::string& text) { + std::unique_ptr<IRContext> context = BuildModule(text); + std::vector<uint32_t> binary; + + context->module()->ToBinary(&binary, false); + + SpirvTools tools(SPV_ENV_UNIVERSAL_1_1); + std::string s; + tools.Disassemble(binary, &s); + EXPECT_EQ(s, text); +} + +TEST(ModuleTest, TrailingOpLine) { + const std::string text = R"(OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%5 = OpString "file.ext" +%void = OpTypeVoid +%2 = OpTypeFunction %void +%3 = OpFunction %void None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd +OpLine %5 1 0 +)"; + + AssembleAndDisassemble(text); +} + +TEST(ModuleTest, TrailingOpNoLine) { + const std::string text = R"(OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%void = OpTypeVoid +%2 = OpTypeFunction %void +%3 = OpFunction %void None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd +OpNoLine +)"; + + AssembleAndDisassemble(text); +} + +TEST(ModuleTest, MulitpleTrailingOpLine) { + const std::string text = R"(OpCapability Shader +OpCapability Linkage +OpMemoryModel Logical GLSL450 +%5 = OpString "file.ext" +%void = OpTypeVoid +%2 = OpTypeFunction %void +%3 = OpFunction %void None %2 +%4 = OpLabel +OpReturn +OpFunctionEnd +OpLine %5 1 0 +OpNoLine +OpLine %5 1 1 +)"; + + AssembleAndDisassemble(text); +} } // namespace } // namespace opt } // namespace spvtools |