123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- // Copyright (c) 2016 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.
- #include <algorithm>
- #include <memory>
- #include <string>
- #include <unordered_set>
- #include <utility>
- #include <vector>
- #include "gtest/gtest.h"
- #include "source/opt/build_module.h"
- #include "source/opt/ir_context.h"
- #include "spirv-tools/libspirv.hpp"
- namespace spvtools {
- namespace opt {
- namespace {
- void DoRoundTripCheck(const std::string& text) {
- SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
- ASSERT_NE(nullptr, context) << "Failed to assemble\n" << text;
- std::vector<uint32_t> binary;
- context->module()->ToBinary(&binary, /* skip_nop = */ false);
- std::string disassembled_text;
- EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
- EXPECT_EQ(text, disassembled_text);
- }
- TEST(IrBuilder, RoundTrip) {
- // #version 310 es
- // int add(int a, int b) { return a + b; }
- // void main() { add(1, 2); }
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "%1 = OpExtInstImport \"GLSL.std.450\"\n"
- "OpMemoryModel Logical GLSL450\n"
- "OpEntryPoint Vertex %main \"main\"\n"
- "OpSource ESSL 310\n"
- "OpSourceExtension \"GL_GOOGLE_cpp_style_line_directive\"\n"
- "OpSourceExtension \"GL_GOOGLE_include_directive\"\n"
- "OpName %main \"main\"\n"
- "OpName %add_i1_i1_ \"add(i1;i1;\"\n"
- "OpName %a \"a\"\n"
- "OpName %b \"b\"\n"
- "OpName %param \"param\"\n"
- "OpName %param_0 \"param\"\n"
- "%void = OpTypeVoid\n"
- "%9 = OpTypeFunction %void\n"
- "%int = OpTypeInt 32 1\n"
- "%_ptr_Function_int = OpTypePointer Function %int\n"
- "%12 = OpTypeFunction %int %_ptr_Function_int %_ptr_Function_int\n"
- "%int_1 = OpConstant %int 1\n"
- "%int_2 = OpConstant %int 2\n"
- "%main = OpFunction %void None %9\n"
- "%15 = OpLabel\n"
- "%param = OpVariable %_ptr_Function_int Function\n"
- "%param_0 = OpVariable %_ptr_Function_int Function\n"
- "OpStore %param %int_1\n"
- "OpStore %param_0 %int_2\n"
- "%16 = OpFunctionCall %int %add_i1_i1_ %param %param_0\n"
- "OpReturn\n"
- "OpFunctionEnd\n"
- "%add_i1_i1_ = OpFunction %int None %12\n"
- "%a = OpFunctionParameter %_ptr_Function_int\n"
- "%b = OpFunctionParameter %_ptr_Function_int\n"
- "%17 = OpLabel\n"
- "%18 = OpLoad %int %a\n"
- "%19 = OpLoad %int %b\n"
- "%20 = OpIAdd %int %18 %19\n"
- "OpReturnValue %20\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, RoundTripIncompleteBasicBlock) {
- DoRoundTripCheck(
- "%2 = OpFunction %1 None %3\n"
- "%4 = OpLabel\n"
- "OpNop\n");
- }
- TEST(IrBuilder, RoundTripIncompleteFunction) {
- DoRoundTripCheck("%2 = OpFunction %1 None %3\n");
- }
- TEST(IrBuilder, KeepLineDebugInfo) {
- // #version 310 es
- // void main() {}
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "%1 = OpExtInstImport \"GLSL.std.450\"\n"
- "OpMemoryModel Logical GLSL450\n"
- "OpEntryPoint Vertex %main \"main\"\n"
- "%3 = OpString \"minimal.vert\"\n"
- "OpSource ESSL 310\n"
- "OpName %main \"main\"\n"
- "OpLine %3 10 10\n"
- "%void = OpTypeVoid\n"
- "OpLine %3 100 100\n"
- "%5 = OpTypeFunction %void\n"
- "%main = OpFunction %void None %5\n"
- "OpLine %3 1 1\n"
- "OpNoLine\n"
- "OpLine %3 2 2\n"
- "OpLine %3 3 3\n"
- "%6 = OpLabel\n"
- "OpLine %3 4 4\n"
- "OpNoLine\n"
- "OpReturn\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, LocalGlobalVariables) {
- // #version 310 es
- //
- // float gv1 = 10.;
- // float gv2 = 100.;
- //
- // float f() {
- // float lv1 = gv1 + gv2;
- // float lv2 = gv1 * gv2;
- // return lv1 / lv2;
- // }
- //
- // void main() {
- // float lv1 = gv1 - gv2;
- // }
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "%1 = OpExtInstImport \"GLSL.std.450\"\n"
- "OpMemoryModel Logical GLSL450\n"
- "OpEntryPoint Vertex %main \"main\"\n"
- "OpSource ESSL 310\n"
- "OpName %main \"main\"\n"
- "OpName %f_ \"f(\"\n"
- "OpName %gv1 \"gv1\"\n"
- "OpName %gv2 \"gv2\"\n"
- "OpName %lv1 \"lv1\"\n"
- "OpName %lv2 \"lv2\"\n"
- "OpName %lv1_0 \"lv1\"\n"
- "%void = OpTypeVoid\n"
- "%10 = OpTypeFunction %void\n"
- "%float = OpTypeFloat 32\n"
- "%12 = OpTypeFunction %float\n"
- "%_ptr_Private_float = OpTypePointer Private %float\n"
- "%gv1 = OpVariable %_ptr_Private_float Private\n"
- "%float_10 = OpConstant %float 10\n"
- "%gv2 = OpVariable %_ptr_Private_float Private\n"
- "%float_100 = OpConstant %float 100\n"
- "%_ptr_Function_float = OpTypePointer Function %float\n"
- "%main = OpFunction %void None %10\n"
- "%17 = OpLabel\n"
- "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
- "OpStore %gv1 %float_10\n"
- "OpStore %gv2 %float_100\n"
- "%18 = OpLoad %float %gv1\n"
- "%19 = OpLoad %float %gv2\n"
- "%20 = OpFSub %float %18 %19\n"
- "OpStore %lv1_0 %20\n"
- "OpReturn\n"
- "OpFunctionEnd\n"
- "%f_ = OpFunction %float None %12\n"
- "%21 = OpLabel\n"
- "%lv1 = OpVariable %_ptr_Function_float Function\n"
- "%lv2 = OpVariable %_ptr_Function_float Function\n"
- "%22 = OpLoad %float %gv1\n"
- "%23 = OpLoad %float %gv2\n"
- "%24 = OpFAdd %float %22 %23\n"
- "OpStore %lv1 %24\n"
- "%25 = OpLoad %float %gv1\n"
- "%26 = OpLoad %float %gv2\n"
- "%27 = OpFMul %float %25 %26\n"
- "OpStore %lv2 %27\n"
- "%28 = OpLoad %float %lv1\n"
- "%29 = OpLoad %float %lv2\n"
- "%30 = OpFDiv %float %28 %29\n"
- "OpReturnValue %30\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, OpUndefOutsideFunction) {
- // #version 310 es
- // void main() {}
- const std::string text =
- // clang-format off
- "OpMemoryModel Logical GLSL450\n"
- "%int = OpTypeInt 32 1\n"
- "%uint = OpTypeInt 32 0\n"
- "%float = OpTypeFloat 32\n"
- "%4 = OpUndef %int\n"
- "%int_10 = OpConstant %int 10\n"
- "%6 = OpUndef %uint\n"
- "%bool = OpTypeBool\n"
- "%8 = OpUndef %float\n"
- "%double = OpTypeFloat 64\n";
- // clang-format on
- SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
- ASSERT_NE(nullptr, context);
- const auto opundef_count = std::count_if(
- context->module()->types_values_begin(),
- context->module()->types_values_end(),
- [](const Instruction& inst) { return inst.opcode() == SpvOpUndef; });
- EXPECT_EQ(3, opundef_count);
- std::vector<uint32_t> binary;
- context->module()->ToBinary(&binary, /* skip_nop = */ false);
- std::string disassembled_text;
- EXPECT_TRUE(t.Disassemble(binary, &disassembled_text));
- EXPECT_EQ(text, disassembled_text);
- }
- TEST(IrBuilder, OpUndefInBasicBlock) {
- DoRoundTripCheck(
- // clang-format off
- "OpMemoryModel Logical GLSL450\n"
- "OpName %main \"main\"\n"
- "%void = OpTypeVoid\n"
- "%uint = OpTypeInt 32 0\n"
- "%double = OpTypeFloat 64\n"
- "%5 = OpTypeFunction %void\n"
- "%main = OpFunction %void None %5\n"
- "%6 = OpLabel\n"
- "%7 = OpUndef %uint\n"
- "%8 = OpUndef %double\n"
- "OpReturn\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, KeepLineDebugInfoBeforeType) {
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "OpMemoryModel Logical GLSL450\n"
- "%1 = OpString \"minimal.vert\"\n"
- "OpLine %1 1 1\n"
- "OpNoLine\n"
- "%void = OpTypeVoid\n"
- "OpLine %1 2 2\n"
- "%3 = OpTypeFunction %void\n");
- // clang-format on
- }
- TEST(IrBuilder, KeepLineDebugInfoBeforeLabel) {
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "OpMemoryModel Logical GLSL450\n"
- "%1 = OpString \"minimal.vert\"\n"
- "%void = OpTypeVoid\n"
- "%3 = OpTypeFunction %void\n"
- "%4 = OpFunction %void None %3\n"
- "%5 = OpLabel\n"
- "OpBranch %6\n"
- "OpLine %1 1 1\n"
- "OpLine %1 2 2\n"
- "%6 = OpLabel\n"
- "OpBranch %7\n"
- "OpLine %1 100 100\n"
- "%7 = OpLabel\n"
- "OpReturn\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, KeepLineDebugInfoBeforeFunctionEnd) {
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "OpMemoryModel Logical GLSL450\n"
- "%1 = OpString \"minimal.vert\"\n"
- "%void = OpTypeVoid\n"
- "%3 = OpTypeFunction %void\n"
- "%4 = OpFunction %void None %3\n"
- "OpLine %1 1 1\n"
- "OpLine %1 2 2\n"
- "OpFunctionEnd\n");
- // clang-format on
- }
- TEST(IrBuilder, KeepModuleProcessedInRightPlace) {
- DoRoundTripCheck(
- // clang-format off
- "OpCapability Shader\n"
- "OpMemoryModel Logical GLSL450\n"
- "%1 = OpString \"minimal.vert\"\n"
- "OpName %void \"void\"\n"
- "OpModuleProcessed \"Made it faster\"\n"
- "OpModuleProcessed \".. and smaller\"\n"
- "%void = OpTypeVoid\n");
- // clang-format on
- }
- // Checks the given |error_message| is reported when trying to build a module
- // from the given |assembly|.
- void DoErrorMessageCheck(const std::string& assembly,
- const std::string& error_message, uint32_t line_num) {
- auto consumer = [error_message, line_num](spv_message_level_t, const char*,
- const spv_position_t& position,
- const char* m) {
- EXPECT_EQ(error_message, m);
- EXPECT_EQ(line_num, position.line);
- };
- SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, std::move(consumer), assembly);
- EXPECT_EQ(nullptr, context);
- }
- TEST(IrBuilder, FunctionInsideFunction) {
- DoErrorMessageCheck("%2 = OpFunction %1 None %3\n%5 = OpFunction %4 None %6",
- "function inside function", 2);
- }
- TEST(IrBuilder, MismatchOpFunctionEnd) {
- DoErrorMessageCheck("OpFunctionEnd",
- "OpFunctionEnd without corresponding OpFunction", 1);
- }
- TEST(IrBuilder, OpFunctionEndInsideBasicBlock) {
- DoErrorMessageCheck(
- "%2 = OpFunction %1 None %3\n"
- "%4 = OpLabel\n"
- "OpFunctionEnd",
- "OpFunctionEnd inside basic block", 3);
- }
- TEST(IrBuilder, BasicBlockOutsideFunction) {
- DoErrorMessageCheck("OpCapability Shader\n%1 = OpLabel",
- "OpLabel outside function", 2);
- }
- TEST(IrBuilder, OpLabelInsideBasicBlock) {
- DoErrorMessageCheck(
- "%2 = OpFunction %1 None %3\n"
- "%4 = OpLabel\n"
- "%5 = OpLabel",
- "OpLabel inside basic block", 3);
- }
- TEST(IrBuilder, TerminatorOutsideFunction) {
- DoErrorMessageCheck("OpReturn", "terminator instruction outside function", 1);
- }
- TEST(IrBuilder, TerminatorOutsideBasicBlock) {
- DoErrorMessageCheck("%2 = OpFunction %1 None %3\nOpReturn",
- "terminator instruction outside basic block", 2);
- }
- TEST(IrBuilder, NotAllowedInstAppearingInFunction) {
- DoErrorMessageCheck("%2 = OpFunction %1 None %3\n%5 = OpVariable %4 Function",
- "Non-OpFunctionParameter (opcode: 59) found inside "
- "function but outside basic block",
- 2);
- }
- TEST(IrBuilder, UniqueIds) {
- const std::string text =
- // clang-format off
- "OpCapability Shader\n"
- "%1 = OpExtInstImport \"GLSL.std.450\"\n"
- "OpMemoryModel Logical GLSL450\n"
- "OpEntryPoint Vertex %main \"main\"\n"
- "OpSource ESSL 310\n"
- "OpName %main \"main\"\n"
- "OpName %f_ \"f(\"\n"
- "OpName %gv1 \"gv1\"\n"
- "OpName %gv2 \"gv2\"\n"
- "OpName %lv1 \"lv1\"\n"
- "OpName %lv2 \"lv2\"\n"
- "OpName %lv1_0 \"lv1\"\n"
- "%void = OpTypeVoid\n"
- "%10 = OpTypeFunction %void\n"
- "%float = OpTypeFloat 32\n"
- "%12 = OpTypeFunction %float\n"
- "%_ptr_Private_float = OpTypePointer Private %float\n"
- "%gv1 = OpVariable %_ptr_Private_float Private\n"
- "%float_10 = OpConstant %float 10\n"
- "%gv2 = OpVariable %_ptr_Private_float Private\n"
- "%float_100 = OpConstant %float 100\n"
- "%_ptr_Function_float = OpTypePointer Function %float\n"
- "%main = OpFunction %void None %10\n"
- "%17 = OpLabel\n"
- "%lv1_0 = OpVariable %_ptr_Function_float Function\n"
- "OpStore %gv1 %float_10\n"
- "OpStore %gv2 %float_100\n"
- "%18 = OpLoad %float %gv1\n"
- "%19 = OpLoad %float %gv2\n"
- "%20 = OpFSub %float %18 %19\n"
- "OpStore %lv1_0 %20\n"
- "OpReturn\n"
- "OpFunctionEnd\n"
- "%f_ = OpFunction %float None %12\n"
- "%21 = OpLabel\n"
- "%lv1 = OpVariable %_ptr_Function_float Function\n"
- "%lv2 = OpVariable %_ptr_Function_float Function\n"
- "%22 = OpLoad %float %gv1\n"
- "%23 = OpLoad %float %gv2\n"
- "%24 = OpFAdd %float %22 %23\n"
- "OpStore %lv1 %24\n"
- "%25 = OpLoad %float %gv1\n"
- "%26 = OpLoad %float %gv2\n"
- "%27 = OpFMul %float %25 %26\n"
- "OpStore %lv2 %27\n"
- "%28 = OpLoad %float %lv1\n"
- "%29 = OpLoad %float %lv2\n"
- "%30 = OpFDiv %float %28 %29\n"
- "OpReturnValue %30\n"
- "OpFunctionEnd\n";
- // clang-format on
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
- ASSERT_NE(nullptr, context);
- std::unordered_set<uint32_t> ids;
- context->module()->ForEachInst([&ids](const Instruction* inst) {
- EXPECT_TRUE(ids.insert(inst->unique_id()).second);
- });
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|