partial_linkage_test.cpp 2.6 KB

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