text_to_binary.function_test.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "Function" section of the
  15. // SPIR-V spec.
  16. #include <string>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "test/test_fixture.h"
  20. #include "test/unit_spirv.h"
  21. namespace spvtools {
  22. namespace {
  23. using spvtest::EnumCase;
  24. using spvtest::MakeInstruction;
  25. using spvtest::TextToBinaryTest;
  26. using ::testing::Eq;
  27. // Test OpFunction
  28. using OpFunctionControlTest = spvtest::TextToBinaryTestBase<
  29. ::testing::TestWithParam<EnumCase<spv::FunctionControlMask>>>;
  30. TEST_P(OpFunctionControlTest, AnySingleFunctionControlMask) {
  31. const std::string input = "%result_id = OpFunction %result_type " +
  32. GetParam().name() + " %function_type ";
  33. EXPECT_THAT(CompiledInstructions(input),
  34. Eq(MakeInstruction(spv::Op::OpFunction,
  35. {1, 2, (uint32_t)GetParam().value(), 3})));
  36. }
  37. // clang-format off
  38. #define CASE(VALUE,NAME) { spv::FunctionControlMask::VALUE, NAME }
  39. INSTANTIATE_TEST_SUITE_P(TextToBinaryFunctionTest, OpFunctionControlTest,
  40. ::testing::ValuesIn(std::vector<EnumCase<spv::FunctionControlMask>>{
  41. CASE(MaskNone, "None"),
  42. CASE(Inline, "Inline"),
  43. CASE(DontInline, "DontInline"),
  44. CASE(Pure, "Pure"),
  45. CASE(Const, "Const"),
  46. }));
  47. #undef CASE
  48. // clang-format on
  49. TEST_F(OpFunctionControlTest, CombinedFunctionControlMask) {
  50. // Sample a single combination. This ensures we've integrated
  51. // the instruction parsing logic with spvTextParseMask.
  52. const std::string input =
  53. "%result_id = OpFunction %result_type Inline|Pure|Const %function_type";
  54. const uint32_t expected_mask = uint32_t(spv::FunctionControlMask::Inline |
  55. spv::FunctionControlMask::Pure |
  56. spv::FunctionControlMask::Const);
  57. EXPECT_THAT(
  58. CompiledInstructions(input),
  59. Eq(MakeInstruction(spv::Op::OpFunction, {1, 2, expected_mask, 3})));
  60. }
  61. TEST_F(OpFunctionControlTest, WrongFunctionControl) {
  62. EXPECT_THAT(CompileFailure("%r = OpFunction %t Inline|Unroll %ft"),
  63. Eq("Invalid function control operand 'Inline|Unroll'."));
  64. }
  65. // TODO(dneto): OpFunctionParameter
  66. // TODO(dneto): OpFunctionEnd
  67. // TODO(dneto): OpFunctionCall
  68. } // namespace
  69. } // namespace spvtools