entry_points_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. class EntryPoints : public spvtest::LinkerTest {};
  21. TEST_F(EntryPoints, SameModelDifferentName) {
  22. const std::string body1 = R"(
  23. OpCapability Shader
  24. OpMemoryModel Logical GLSL450
  25. OpEntryPoint GLCompute %3 "foo"
  26. %1 = OpTypeVoid
  27. %2 = OpTypeFunction %1
  28. %3 = OpFunction %1 None %2
  29. OpFunctionEnd
  30. )";
  31. const std::string body2 = R"(
  32. OpCapability Shader
  33. OpMemoryModel Logical GLSL450
  34. OpEntryPoint GLCompute %3 "bar"
  35. %1 = OpTypeVoid
  36. %2 = OpTypeFunction %1
  37. %3 = OpFunction %1 None %2
  38. OpFunctionEnd
  39. )";
  40. spvtest::Binary linked_binary;
  41. ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
  42. << GetErrorMessage();
  43. EXPECT_THAT(GetErrorMessage(), std::string());
  44. }
  45. TEST_F(EntryPoints, DifferentModelSameName) {
  46. const std::string body1 = R"(
  47. OpCapability Shader
  48. OpMemoryModel Logical GLSL450
  49. OpEntryPoint GLCompute %3 "foo"
  50. %1 = OpTypeVoid
  51. %2 = OpTypeFunction %1
  52. %3 = OpFunction %1 None %2
  53. OpFunctionEnd
  54. )";
  55. const std::string body2 = R"(
  56. OpCapability Shader
  57. OpMemoryModel Logical GLSL450
  58. OpEntryPoint Vertex %3 "foo"
  59. %1 = OpTypeVoid
  60. %2 = OpTypeFunction %1
  61. %3 = OpFunction %1 None %2
  62. OpFunctionEnd
  63. )";
  64. spvtest::Binary linked_binary;
  65. ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary))
  66. << GetErrorMessage();
  67. EXPECT_THAT(GetErrorMessage(), std::string());
  68. }
  69. TEST_F(EntryPoints, SameModelAndName) {
  70. const std::string body1 = R"(
  71. OpCapability Shader
  72. OpMemoryModel Logical GLSL450
  73. OpEntryPoint GLCompute %3 "foo"
  74. %1 = OpTypeVoid
  75. %2 = OpTypeFunction %1
  76. %3 = OpFunction %1 None %2
  77. OpFunctionEnd
  78. )";
  79. const std::string body2 = R"(
  80. OpCapability Shader
  81. OpMemoryModel Logical GLSL450
  82. OpEntryPoint GLCompute %3 "foo"
  83. %1 = OpTypeVoid
  84. %2 = OpTypeFunction %1
  85. %3 = OpFunction %1 None %2
  86. OpFunctionEnd
  87. )";
  88. spvtest::Binary linked_binary;
  89. EXPECT_EQ(SPV_ERROR_INTERNAL,
  90. AssembleAndLink({body1, body2}, &linked_binary));
  91. EXPECT_THAT(GetErrorMessage(),
  92. HasSubstr("The entry point \"foo\", with execution model "
  93. "GLCompute, was already defined."));
  94. }
  95. TEST_F(EntryPoints, LinkedVariables) {
  96. const std::string body1 = R"(
  97. OpCapability Addresses
  98. OpCapability Linkage
  99. OpCapability Kernel
  100. OpMemoryModel Physical64 OpenCL
  101. OpDecorate %7 LinkageAttributes "foo" Export
  102. %1 = OpTypeInt 32 0
  103. %2 = OpTypeVector %1 3
  104. %3 = OpTypePointer Input %2
  105. %4 = OpVariable %3 Input
  106. %5 = OpTypeVoid
  107. %6 = OpTypeFunction %5
  108. %7 = OpFunction %5 None %6
  109. %8 = OpLabel
  110. %9 = OpLoad %2 %4 Aligned 32
  111. OpReturn
  112. OpFunctionEnd
  113. )";
  114. const std::string body2 = R"(
  115. OpCapability Linkage
  116. OpCapability Kernel
  117. OpMemoryModel Physical64 OpenCL
  118. OpEntryPoint Kernel %4 "bar"
  119. OpDecorate %3 LinkageAttributes "foo" Import
  120. %1 = OpTypeVoid
  121. %2 = OpTypeFunction %1
  122. %3 = OpFunction %1 None %2
  123. OpFunctionEnd
  124. %4 = OpFunction %1 None %2
  125. %5 = OpLabel
  126. %6 = OpFunctionCall %1 %3
  127. OpReturn
  128. OpFunctionEnd
  129. )";
  130. spvtest::Binary linked_binary;
  131. EXPECT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary));
  132. EXPECT_THAT(GetErrorMessage(), std::string());
  133. EXPECT_TRUE(Validate(linked_binary));
  134. EXPECT_THAT(GetErrorMessage(), std::string());
  135. }
  136. } // namespace
  137. } // namespace spvtools