Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions source/link/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<opt::Function> 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<opt::Function> cloned_func(func.Clone(linked_context));
linked_module->AddFunction(std::move(cloned_func));
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions test/link/partial_linkage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "test/link/linker_fixture.h"
Expand Down Expand Up @@ -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<std::string> bodies =
reverse ? std::vector<std::string>{body2, body1}
: std::vector<std::string>{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
Expand Down
Loading