From 9232b7b5c681da620928d206e79bb66b07c6b04c Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Mon, 23 Nov 2026 00:28:09 -0800 Subject: [PATCH] linker: Move function declarations before definitions Partial linkage can leave unresolved imports from later modules after function definitions, which is invalid SPIR-V. --- source/link/linker.cpp | 14 +++++--- test/link/partial_linkage_test.cpp | 51 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/source/link/linker.cpp b/source/link/linker.cpp index 8c055475b1..870fb770d2 100644 --- a/source/link/linker.cpp +++ b/source/link/linker.cpp @@ -401,11 +401,15 @@ spv_result_t MergeModules(const MessageConsumer& consumer, } } - // Process functions and their basic blocks - for (const auto& module : input_modules) { - for (const auto& func : *module) { - std::unique_ptr cloned_func(func.Clone(linked_context)); - linked_module->AddFunction(std::move(cloned_func)); + // Declarations must precede definitions, including imports kept by partial + // linkage from later input modules. + for (bool declarations : {true, false}) { + for (const auto& module : input_modules) { + for (const auto& func : *module) { + if (func.IsDeclaration() != declarations) continue; + std::unique_ptr cloned_func(func.Clone(linked_context)); + linked_module->AddFunction(std::move(cloned_func)); + } } } diff --git a/test/link/partial_linkage_test.cpp b/test/link/partial_linkage_test.cpp index bf4b508b6d..e50c037765 100644 --- a/test/link/partial_linkage_test.cpp +++ b/test/link/partial_linkage_test.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "gmock/gmock.h" #include "test/link/linker_fixture.h" @@ -71,6 +72,56 @@ OpDecorate %1 LinkageAttributes "foo" Import EXPECT_EQ(expected_res, res_body); } +TEST_F(PartialLinkage, FunctionDeclarationsBeforeDefinitions) { + const std::string body1 = R"( +OpCapability Addresses +OpCapability Kernel +OpCapability Linkage +OpMemoryModel Physical64 OpenCL +OpDecorate %first LinkageAttributes "first" Export +%void = OpTypeVoid +%fn = OpTypeFunction %void +%first = OpFunction %void None %fn +%entry = OpLabel +OpReturn +OpFunctionEnd +)"; + const std::string body2 = R"( +OpCapability Addresses +OpCapability Kernel +OpCapability Linkage +OpMemoryModel Physical64 OpenCL +OpDecorate %external LinkageAttributes "external" Import +OpDecorate %second LinkageAttributes "second" Export +%void = OpTypeVoid +%fn = OpTypeFunction %void +%external = OpFunction %void None %fn +OpFunctionEnd +%second = OpFunction %void None %fn +%entry = OpLabel +%call = OpFunctionCall %void %external +OpReturn +OpFunctionEnd +)"; + + for (bool create_library : {false, true}) { + SCOPED_TRACE(create_library); + LinkerOptions options; + options.SetAllowPartialLinkage(true); + options.SetCreateLibrary(create_library); + for (bool reverse : {false, true}) { + SCOPED_TRACE(reverse); + const std::vector bodies = + reverse ? std::vector{body2, body1} + : std::vector{body1, body2}; + spvtest::Binary linked_binary; + ASSERT_EQ(SPV_SUCCESS, AssembleAndLink(bodies, &linked_binary, options)) + << GetErrorMessage(); + EXPECT_TRUE(Validate(linked_binary)) << GetErrorMessage(); + } + } +} + TEST_F(PartialLinkage, Disallowed) { const std::string body1 = R"( OpCapability Linkage