binary_version_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2017 Pierre Moreau
  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. #include <string>
  15. #include "gmock/gmock.h"
  16. #include "test/link/linker_fixture.h"
  17. namespace spvtools {
  18. namespace {
  19. using ::testing::HasSubstr;
  20. using BinaryVersion = spvtest::LinkerTest;
  21. spvtest::Binary CreateBinary(uint32_t version) {
  22. return {
  23. // clang-format off
  24. // Header
  25. static_cast<uint32_t>(spv::MagicNumber),
  26. version,
  27. SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
  28. 1u, // NOTE: Bound
  29. 0u, // NOTE: Schema; reserved
  30. // OpCapability Shader
  31. static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
  32. static_cast<uint32_t>(spv::Capability::Shader),
  33. // OpMemoryModel Logical Simple
  34. static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
  35. static_cast<uint32_t>(spv::AddressingModel::Logical),
  36. static_cast<uint32_t>(spv::MemoryModel::Simple)
  37. // clang-format on
  38. };
  39. }
  40. TEST_F(BinaryVersion, Match) {
  41. // clang-format off
  42. spvtest::Binaries binaries = {
  43. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
  44. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
  45. };
  46. // clang-format on
  47. spvtest::Binary linked_binary;
  48. ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
  49. EXPECT_THAT(GetErrorMessage(), std::string());
  50. EXPECT_EQ(SPV_SPIRV_VERSION_WORD(1, 3), linked_binary[1]);
  51. }
  52. TEST_F(BinaryVersion, Mismatch) {
  53. // clang-format off
  54. spvtest::Binaries binaries = {
  55. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
  56. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 5)),
  57. };
  58. // clang-format on
  59. spvtest::Binary linked_binary;
  60. ASSERT_EQ(SPV_ERROR_INTERNAL, Link(binaries, &linked_binary))
  61. << GetErrorMessage();
  62. EXPECT_THAT(GetErrorMessage(),
  63. HasSubstr("Conflicting SPIR-V versions: 1.3 (input modules 1 "
  64. "through 1) vs 1.5 (input module 2)."));
  65. }
  66. TEST_F(BinaryVersion, UseHighest) {
  67. // clang-format off
  68. spvtest::Binaries binaries = {
  69. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 3)),
  70. CreateBinary(SPV_SPIRV_VERSION_WORD(1, 5)),
  71. };
  72. // clang-format on
  73. LinkerOptions options;
  74. options.SetUseHighestVersion(true);
  75. spvtest::Binary linked_binary;
  76. ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary, options))
  77. << GetErrorMessage();
  78. EXPECT_THAT(GetErrorMessage(), std::string());
  79. EXPECT_EQ(SPV_SPIRV_VERSION_WORD(1, 5), linked_binary[1]);
  80. }
  81. } // namespace
  82. } // namespace spvtools