text_to_binary.memory_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "Memory Instructions" section of
  15. // the SPIR-V spec.
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include "gmock/gmock.h"
  20. #include "test/test_fixture.h"
  21. #include "test/unit_spirv.h"
  22. namespace spvtools {
  23. namespace {
  24. using spvtest::EnumCase;
  25. using spvtest::MakeInstruction;
  26. using spvtest::TextToBinaryTest;
  27. using ::testing::Eq;
  28. // Test assembly of Memory Access masks
  29. using MemoryAccessTest = spvtest::TextToBinaryTestBase<
  30. ::testing::TestWithParam<EnumCase<SpvMemoryAccessMask>>>;
  31. TEST_P(MemoryAccessTest, AnySingleMemoryAccessMask) {
  32. std::stringstream input;
  33. input << "OpStore %ptr %value " << GetParam().name();
  34. for (auto operand : GetParam().operands()) input << " " << operand;
  35. EXPECT_THAT(CompiledInstructions(input.str()),
  36. Eq(MakeInstruction(SpvOpStore, {1, 2, GetParam().value()},
  37. GetParam().operands())));
  38. }
  39. INSTANTIATE_TEST_SUITE_P(
  40. TextToBinaryMemoryAccessTest, MemoryAccessTest,
  41. ::testing::ValuesIn(std::vector<EnumCase<SpvMemoryAccessMask>>{
  42. {SpvMemoryAccessMaskNone, "None", {}},
  43. {SpvMemoryAccessVolatileMask, "Volatile", {}},
  44. {SpvMemoryAccessAlignedMask, "Aligned", {16}},
  45. {SpvMemoryAccessNontemporalMask, "Nontemporal", {}},
  46. }));
  47. TEST_F(TextToBinaryTest, CombinedMemoryAccessMask) {
  48. const std::string input = "OpStore %ptr %value Volatile|Aligned 16";
  49. const uint32_t expected_mask =
  50. SpvMemoryAccessVolatileMask | SpvMemoryAccessAlignedMask;
  51. EXPECT_THAT(expected_mask, Eq(3u));
  52. EXPECT_THAT(CompiledInstructions(input),
  53. Eq(MakeInstruction(SpvOpStore, {1, 2, expected_mask, 16})));
  54. }
  55. // Test Storage Class enum values
  56. using StorageClassTest = spvtest::TextToBinaryTestBase<
  57. ::testing::TestWithParam<EnumCase<SpvStorageClass>>>;
  58. TEST_P(StorageClassTest, AnyStorageClass) {
  59. const std::string input = "%1 = OpVariable %2 " + GetParam().name();
  60. EXPECT_THAT(CompiledInstructions(input),
  61. Eq(MakeInstruction(SpvOpVariable, {1, 2, GetParam().value()})));
  62. }
  63. // clang-format off
  64. #define CASE(NAME) { SpvStorageClass##NAME, #NAME, {} }
  65. INSTANTIATE_TEST_SUITE_P(
  66. TextToBinaryStorageClassTest, StorageClassTest,
  67. ::testing::ValuesIn(std::vector<EnumCase<SpvStorageClass>>{
  68. CASE(UniformConstant),
  69. CASE(Input),
  70. CASE(Uniform),
  71. CASE(Output),
  72. CASE(Workgroup),
  73. CASE(CrossWorkgroup),
  74. CASE(Private),
  75. CASE(Function),
  76. CASE(Generic),
  77. CASE(PushConstant),
  78. CASE(AtomicCounter),
  79. CASE(Image),
  80. }));
  81. #undef CASE
  82. // clang-format on
  83. // TODO(dneto): OpVariable with initializers
  84. // TODO(dneto): OpImageTexelPointer
  85. // TODO(dneto): OpLoad
  86. // TODO(dneto): OpStore
  87. // TODO(dneto): OpCopyMemory
  88. // TODO(dneto): OpCopyMemorySized
  89. // TODO(dneto): OpAccessChain
  90. // TODO(dneto): OpInBoundsAccessChain
  91. // TODO(dneto): OpPtrAccessChain
  92. // TODO(dneto): OpArrayLength
  93. // TODO(dneto): OpGenercPtrMemSemantics
  94. } // namespace
  95. } // namespace spvtools