text_to_binary.barrier_test.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(SpvOpMemoryBarrier, {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, found end of stream."));
  39. }
  40. TEST_F(OpMemoryBarrier, BadInvalidScopeId) {
  41. const std::string input = "OpMemoryBarrier 99\n";
  42. EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
  43. }
  44. TEST_F(OpMemoryBarrier, BadMissingMemorySemanticsId) {
  45. const std::string input = "OpMemoryBarrier %scope\n";
  46. EXPECT_THAT(CompileFailure(input),
  47. Eq("Expected operand, found end of stream."));
  48. }
  49. TEST_F(OpMemoryBarrier, BadInvalidMemorySemanticsId) {
  50. const std::string input = "OpMemoryBarrier %scope 14\n";
  51. EXPECT_THAT(CompileFailure(input), Eq("Expected id to start with %."));
  52. }
  53. // TODO(dneto): OpControlBarrier
  54. // TODO(dneto): OpGroupAsyncCopy
  55. // TODO(dneto): OpGroupWaitEvents
  56. // TODO(dneto): OpGroupAll
  57. // TODO(dneto): OpGroupAny
  58. // TODO(dneto): OpGroupBroadcast
  59. // TODO(dneto): OpGroupIAdd
  60. // TODO(dneto): OpGroupFAdd
  61. // TODO(dneto): OpGroupFMin
  62. // TODO(dneto): OpGroupUMin
  63. // TODO(dneto): OpGroupSMin
  64. // TODO(dneto): OpGroupFMax
  65. // TODO(dneto): OpGroupUMax
  66. // TODO(dneto): OpGroupSMax
  67. using NamedMemoryBarrierTest = spvtest::TextToBinaryTest;
  68. // OpMemoryNamedBarrier is not in 1.0, but it is enabled by a capability.
  69. // We should be able to assemble it. Validation checks are in another test
  70. // file.
  71. TEST_F(NamedMemoryBarrierTest, OpcodeAssemblesInV10) {
  72. EXPECT_THAT(
  73. CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
  74. SPV_ENV_UNIVERSAL_1_0),
  75. ElementsAre(spvOpcodeMake(4, SpvOpMemoryNamedBarrier), _, _, _));
  76. }
  77. TEST_F(NamedMemoryBarrierTest, ArgumentCount) {
  78. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  79. Eq("Expected operand, found end of stream."));
  80. EXPECT_THAT(
  81. CompileFailure("OpMemoryNamedBarrier %bar", SPV_ENV_UNIVERSAL_1_1),
  82. Eq("Expected operand, found end of stream."));
  83. EXPECT_THAT(
  84. CompileFailure("OpMemoryNamedBarrier %bar %scope", SPV_ENV_UNIVERSAL_1_1),
  85. Eq("Expected operand, found end of stream."));
  86. EXPECT_THAT(
  87. CompiledInstructions("OpMemoryNamedBarrier %bar %scope %semantics",
  88. SPV_ENV_UNIVERSAL_1_1),
  89. ElementsAre(spvOpcodeMake(4, SpvOpMemoryNamedBarrier), _, _, _));
  90. EXPECT_THAT(
  91. CompileFailure("OpMemoryNamedBarrier %bar %scope %semantics %extra",
  92. SPV_ENV_UNIVERSAL_1_1),
  93. Eq("Expected '=', found end of stream."));
  94. }
  95. TEST_F(NamedMemoryBarrierTest, ArgumentTypes) {
  96. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier 123 %scope %semantics",
  97. SPV_ENV_UNIVERSAL_1_1),
  98. Eq("Expected id to start with %."));
  99. EXPECT_THAT(CompileFailure("OpMemoryNamedBarrier %bar %scope \"semantics\"",
  100. SPV_ENV_UNIVERSAL_1_1),
  101. Eq("Expected id to start with %."));
  102. }
  103. using TypeNamedBarrierTest = spvtest::TextToBinaryTest;
  104. TEST_F(TypeNamedBarrierTest, OpcodeAssemblesInV10) {
  105. EXPECT_THAT(
  106. CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_0),
  107. ElementsAre(spvOpcodeMake(2, SpvOpTypeNamedBarrier), _));
  108. }
  109. TEST_F(TypeNamedBarrierTest, ArgumentCount) {
  110. EXPECT_THAT(CompileFailure("OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  111. Eq("Expected <result-id> at the beginning of an instruction, "
  112. "found 'OpTypeNamedBarrier'."));
  113. EXPECT_THAT(
  114. CompiledInstructions("%t = OpTypeNamedBarrier", SPV_ENV_UNIVERSAL_1_1),
  115. ElementsAre(spvOpcodeMake(2, SpvOpTypeNamedBarrier), _));
  116. EXPECT_THAT(
  117. CompileFailure("%t = OpTypeNamedBarrier 1 2 3", SPV_ENV_UNIVERSAL_1_1),
  118. Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
  119. "found '1'."));
  120. }
  121. using NamedBarrierInitializeTest = spvtest::TextToBinaryTest;
  122. TEST_F(NamedBarrierInitializeTest, OpcodeAssemblesInV10) {
  123. EXPECT_THAT(
  124. CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
  125. SPV_ENV_UNIVERSAL_1_0),
  126. ElementsAre(spvOpcodeMake(4, SpvOpNamedBarrierInitialize), _, _, _));
  127. }
  128. TEST_F(NamedBarrierInitializeTest, ArgumentCount) {
  129. EXPECT_THAT(
  130. CompileFailure("%bar = OpNamedBarrierInitialize", SPV_ENV_UNIVERSAL_1_1),
  131. Eq("Expected operand, found end of stream."));
  132. EXPECT_THAT(CompileFailure("%bar = OpNamedBarrierInitialize %ype",
  133. SPV_ENV_UNIVERSAL_1_1),
  134. Eq("Expected operand, found end of stream."));
  135. EXPECT_THAT(
  136. CompiledInstructions("%bar = OpNamedBarrierInitialize %type %count",
  137. SPV_ENV_UNIVERSAL_1_1),
  138. ElementsAre(spvOpcodeMake(4, SpvOpNamedBarrierInitialize), _, _, _));
  139. EXPECT_THAT(
  140. CompileFailure("%bar = OpNamedBarrierInitialize %type %count \"extra\"",
  141. SPV_ENV_UNIVERSAL_1_1),
  142. Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
  143. "found '\"extra\"'."));
  144. }
  145. } // namespace
  146. } // namespace spvtools