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 15 #include <string> 16 17 #include "gmock/gmock.h" 18 #include "test/link/linker_fixture.h" 19 20 namespace spvtools { 21 namespace { 22 23 using ::testing::HasSubstr; 24 using PartialLinkage = spvtest::LinkerTest; 25 26 TEST_F(PartialLinkage, Allowed) { 27 const std::string body1 = R"( 28 OpCapability Linkage 29 OpDecorate %1 LinkageAttributes "foo" Import 30 OpDecorate %2 LinkageAttributes "bar" Import 31 %3 = OpTypeFloat 32 32 %1 = OpVariable %3 Uniform 33 %2 = OpVariable %3 Uniform 34 )"; 35 const std::string body2 = R"( 36 OpCapability Linkage 37 OpDecorate %1 LinkageAttributes "bar" Export 38 %2 = OpTypeFloat 32 39 %3 = OpConstant %2 3.1415 40 %1 = OpVariable %2 Uniform %3 41 )"; 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 49 const std::string expected_res = R"(OpCapability Linkage 50 OpModuleProcessed "Linked by SPIR-V Tools Linker" 51 OpDecorate %1 LinkageAttributes "foo" Import 52 %2 = OpTypeFloat 32 53 %1 = OpVariable %2 Uniform 54 %3 = OpConstant %2 3.1415 55 %4 = OpVariable %2 Uniform %3 56 )"; 57 std::string res_body; 58 SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER); 59 ASSERT_EQ(SPV_SUCCESS, Disassemble(linked_binary, &res_body)) 60 << GetErrorMessage(); 61 EXPECT_EQ(expected_res, res_body); 62 } 63 64 TEST_F(PartialLinkage, Disallowed) { 65 const std::string body1 = R"( 66 OpCapability Linkage 67 OpDecorate %1 LinkageAttributes "foo" Import 68 OpDecorate %2 LinkageAttributes "bar" Import 69 %3 = OpTypeFloat 32 70 %1 = OpVariable %3 Uniform 71 %2 = OpVariable %3 Uniform 72 )"; 73 const std::string body2 = R"( 74 OpCapability Linkage 75 OpDecorate %1 LinkageAttributes "bar" Export 76 %2 = OpTypeFloat 32 77 %3 = OpConstant %2 3.1415 78 %1 = OpVariable %2 Uniform %3 79 )"; 80 81 spvtest::Binary linked_binary; 82 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, 83 AssembleAndLink({body1, body2}, &linked_binary)); 84 EXPECT_THAT(GetErrorMessage(), 85 HasSubstr("Unresolved external reference to \"foo\".")); 86 } 87 88 } // namespace 89 } // namespace spvtools 90