text_to_binary.barrier_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Assembler tests for instructions in the "Barrier Instructions" section
  15. // of the SPIR-V spec.
  16. #include <string>
  17. #include "gmock/gmock.h"
  18. #include "test/test_fixture.h"
  19. #include "test/unit_spirv.h"
  20. namespace spvtools {
  21. namespace {
  22. using spvtest::MakeInstruction;
  23. using spvtest::TextToBinaryTest;
  24. using ::testing::_;
  25. using ::testing::ElementsAre;
  26. using ::testing::Eq;
  27. // Test OpMemoryBarrier
  28. using OpMemoryBarrier = spvtest::TextToBinaryTest;
  29. TEST_F(OpMemoryBarrier, Good) {
  30. const std::string input = "OpMemoryBarrier %1 %2\n";
  31. EXPECT_THAT(CompiledInstructions(input),
  32. Eq(MakeInstruction(spv::Op::OpMemoryBarrier, {1, 2})));
  33. EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
  34. }
  35. TEST_F(OpMemoryBarrier, BadMissingScopeId) {
  36. const std::string input = "OpMemoryBarrier\n";
  37. EXPECT_THAT(CompileFailure(input),
  38. Eq("Expected operand for OpMemoryBarrier instruction, but found "
  39. "the end of the stream."));
  40. }
  41. TEST_F(OpMemoryBarrier, BadInvalidScopeId) {
  42. const std::string input = "OpMemoryBarrier 99\n";
  43. EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
  44. }
  45. TEST_F(OpMemoryBarrier, BadMissingMemorySemanticsId) {
  46. const std::string input = "OpMemoryBarrier %scope\n";
  47. EXPECT_THAT(CompileFailure(input),
  48. Eq("Expected operand for OpMemoryBarrier instruction, but found "
  49. "the end of the stream."));
  50. }
  51. TEST_F(OpMemoryBarrier, BadInvalidMemorySemanticsId) {
  52. const std::string input = "OpMemoryBarrier %scope 14\n";
  53. EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
  54. }
  55. // TODO(dneto): OpControlBarrier
  56. // TODO(dneto): OpGroupAsyncCopy
  57. // TODO(dneto): OpGroupWaitEvents
  58. // TODO(dneto): OpGroupAll
  59. // TODO(dneto): OpGroupAny
  60. // TODO(dneto): OpGroupBroadcast
  61. // TODO(dneto): OpGroupIAdd
  62. // TODO(dneto): OpGroupFAdd
  63. // TODO(dneto): OpGroupFMin
  64. // TODO(dneto): OpGroupUMin
  65. // TODO(dneto): OpGroupSMin
  66. // TODO(dneto): OpGroupFMax
  67. // TODO(dneto): OpGroupUMax
  68. // TODO(dneto): OpGroupSMax
  69. using NamedMemoryBarrierTest = spvtest::TextToBinaryTest;
  70. // OpMemoryNamedBarrier is not in 1.0, but it is enabled by a capability.
  71. // We should be able to assemble it. Validation checks are in another test
  72. // file.
  73. TEST_F(NamedMemoryBarrierTest, OpcodeAssemblesInV10) {
  74. EXPECT_THAT(
  75. CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
  76. SPV_ENV_UNIVERSAL_1_0),
  77. ElementsAre(spvOpcodeMake(4, spv::Op::OpMemoryNamedBarrier), _, _, _));
  78. }
  79. TEST_F(NamedMemoryBarrierTest, ArgumentCount) {
  80. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  81. Eq("Expected operand for OpMemoryNamedBarrier instruction, but "
  82. "found the end of the stream."));
  83. EXPECT_THAT(
  84. CompileFailure("OpMemoryNamedBarrier %bar", SPV_ENV_UNIVERSAL_1_1),
  85. Eq("Expected operand for OpMemoryNamedBarrier instruction, but found the "
  86. "end of the stream."));
  87. EXPECT_THAT(
  88. CompileFailure("OpMemoryNamedBarrier %bar %scope", SPV_ENV_UNIVERSAL_1_1),
  89. Eq("Expected operand for OpMemoryNamedBarrier instruction, but found the "
  90. "end of the stream."));
  91. EXPECT_THAT(
  92. CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
  93. SPV_ENV_UNIVERSAL_1_1),
  94. ElementsAre(spvOpcodeMake(4, spv::Op::OpMemoryNamedBarrier), _, _, _));
  95. EXPECT_THAT(
  96. CompileFailure("OpMemoryNamedBarrier %bar %scope %semantics %extra",
  97. SPV_ENV_UNIVERSAL_1_1),
  98. Eq("Expected '=', found end of stream."));
  99. }
  100. TEST_F(NamedMemoryBarrierTest, ArgumentTypes) {
  101. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier 123 %scope %semantics",
  102. SPV_ENV_UNIVERSAL_1_1),
  103. Eq("Expected id to start with %."));
  104. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier %bar %scope \"semantics\"",
  105. SPV_ENV_UNIVERSAL_1_1),
  106. Eq("Expected id to start with %."));
  107. }
  108. using TypeNamedBarrierTest = spvtest::TextToBinaryTest;
  109. TEST_F(TypeNamedBarrierTest, OpcodeAssemblesInV10) {
  110. EXPECT_THAT(
  111. CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_0),
  112. ElementsAre(spvOpcodeMake(2, spv::Op::OpTypeNamedBarrier), _));
  113. }
  114. TEST_F(TypeNamedBarrierTest, ArgumentCount) {
  115. EXPECT_THAT(CompileFailure("OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  116. Eq("Expected <result-id> at the beginning of an instruction, "
  117. "found 'OpTypeNamedBarrier'."));
  118. EXPECT_THAT(
  119. CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  120. ElementsAre(spvOpcodeMake(2, spv::Op::OpTypeNamedBarrier), _));
  121. EXPECT_THAT(
  122. CompileFailure("%t = OpTypeNamedBarrier 1 2 3", SPV_ENV_UNIVERSAL_1_1),
  123. Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
  124. "found '1'."));
  125. }
  126. using NamedBarrierInitializeTest = spvtest::TextToBinaryTest;
  127. TEST_F(NamedBarrierInitializeTest, OpcodeAssemblesInV10) {
  128. EXPECT_THAT(
  129. CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
  130. SPV_ENV_UNIVERSAL_1_0),
  131. ElementsAre(spvOpcodeMake(4, spv::Op::OpNamedBarrierInitialize), _, _,
  132. _));
  133. }
  134. TEST_F(NamedBarrierInitializeTest, ArgumentCount) {
  135. EXPECT_THAT(
  136. CompileFailure("%bar = OpNamedBarrierInitialize", SPV_ENV_UNIVERSAL_1_1),
  137. Eq("Expected operand for OpNamedBarrierInitialize instruction, but found "
  138. "the end of the stream."));
  139. EXPECT_THAT(CompileFailure("%bar = OpNamedBarrierInitialize %ype",
  140. SPV_ENV_UNIVERSAL_1_1),
  141. Eq("Expected operand for OpNamedBarrierInitialize instruction, "
  142. "but found the end of the stream."));
  143. EXPECT_THAT(
  144. CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
  145. SPV_ENV_UNIVERSAL_1_1),
  146. ElementsAre(spvOpcodeMake(4, spv::Op::OpNamedBarrierInitialize), _, _,
  147. _));
  148. EXPECT_THAT(
  149. CompileFailure("%bar = OpNamedBarrierInitialize %type %count \"extra\"",
  150. SPV_ENV_UNIVERSAL_1_1),
  151. Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
  152. "found '\"extra\"'."));
  153. }
  154. } // namespace
  155. } // namespace spvtools