partial_linkage_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2018 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 PartialLinkage = spvtest::LinkerTest;
  21. TEST_F(PartialLinkage, Allowed) {
  22. const std::string body1 = R"(
  23. OpCapability Linkage
  24. OpCapability Addresses
  25. OpCapability Kernel
  26. OpMemoryModel Physical64 OpenCL
  27. OpDecorate %1 LinkageAttributes "foo" Import
  28. OpDecorate %2 LinkageAttributes "bar" Import
  29. %3 = OpTypeFloat 32
  30. %1 = OpVariable %3 Uniform
  31. %2 = OpVariable %3 Uniform
  32. )";
  33. const std::string body2 = R"(
  34. OpCapability Linkage
  35. OpCapability Addresses
  36. OpCapability Kernel
  37. OpMemoryModel Physical64 OpenCL
  38. OpDecorate %1 LinkageAttributes "bar" Export
  39. %2 = OpTypeFloat 32
  40. %3 = OpConstant %2 3.1415
  41. %1 = OpVariable %2 Uniform %3
  42. )";
  43. spvtest::Binary linked_binary;
  44. LinkerOptions linker_options;
  45. linker_options.SetAllowPartialLinkage(true);
  46. ASSERT_EQ(SPV_SUCCESS,
  47. AssembleAndLink({body1, body2}, &linked_binary, linker_options))
  48. << GetErrorMessage();
  49. const std::string expected_res = R"(OpCapability Linkage
  50. OpCapability Addresses
  51. OpCapability Kernel
  52. OpMemoryModel Physical64 OpenCL
  53. OpModuleProcessed "Linked by SPIR-V Tools Linker"
  54. OpDecorate %1 LinkageAttributes "foo" Import
  55. %2 = OpTypeFloat 32
  56. %1 = OpVariable %2 Uniform
  57. %3 = OpConstant %2 3.1415
  58. %4 = OpVariable %2 Uniform %3
  59. )";
  60. std::string res_body;
  61. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  62. ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body))
  63. << GetErrorMessage();
  64. EXPECT_EQ(expected_res, res_body);
  65. }
  66. TEST_F(PartialLinkage, Disallowed) {
  67. const std::string body1 = R"(
  68. OpCapability Linkage
  69. OpCapability Addresses
  70. OpCapability Kernel
  71. OpMemoryModel Physical64 OpenCL
  72. OpDecorate %1 LinkageAttributes "foo" Import
  73. OpDecorate %2 LinkageAttributes "bar" Import
  74. %3 = OpTypeFloat 32
  75. %1 = OpVariable %3 Uniform
  76. %2 = OpVariable %3 Uniform
  77. )";
  78. const std::string body2 = R"(
  79. OpCapability Linkage
  80. OpCapability Addresses
  81. OpCapability Kernel
  82. OpMemoryModel Physical64 OpenCL
  83. OpDecorate %1 LinkageAttributes "bar" Export
  84. %2 = OpTypeFloat 32
  85. %3 = OpConstant %2 3.1415
  86. %1 = OpVariable %2 Uniform %3
  87. )";
  88. spvtest::Binary linked_binary;
  89. EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
  90. AssembleAndLink({body1, body2}, &linked_binary));
  91. EXPECT_THAT(GetErrorMessage(),
  92. HasSubstr("Unresolved external reference to \"foo\"."));
  93. }
  94. } // namespace
  95. } // namespace spvtools