From 9f0abea3d966598ff0d933679e0ad45e00e0a0e5 Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Tue, 10 May 2022 14:05:03 -0700 Subject: [PATCH] [Autolink Extract] Keep a set of linker flags instead of vector Otherwise we can duplicate linker flags across input binaries, which can result in very large linkerr invocation commands. Resolves https://github.com/apple/swift/issues/58380 --- lib/DriverTool/autolink_extract_main.cpp | 13 +++++++------ test/AutolinkExtract/import.swift | 8 +++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/DriverTool/autolink_extract_main.cpp b/lib/DriverTool/autolink_extract_main.cpp index 8d00c791589cb..cc4fcb8429754 100644 --- a/lib/DriverTool/autolink_extract_main.cpp +++ b/lib/DriverTool/autolink_extract_main.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "swift/AST/DiagnosticsFrontend.h" #include "swift/Frontend/Frontend.h" @@ -112,7 +113,7 @@ class AutolinkExtractInvocation { /// Return 'true' if there was an error, and 'false' otherwise. static bool extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, - std::vector &LinkerFlags, + llvm::SetVector &LinkerFlags, CompilerInstance &Instance) { // Search for the section we hold autolink entries in for (auto &Section : ObjectFile->sections()) { @@ -141,7 +142,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1, /*KeepEmpty=*/false); for (const auto &Flag : SplitFlags) - LinkerFlags.push_back(Flag.str()); + LinkerFlags.insert(Flag.str()); } } return false; @@ -152,7 +153,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, /// 'true' if there was an error, and 'false' otherwise. static bool extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, - std::vector &LinkerFlags, + llvm::SetVector &LinkerFlags, CompilerInstance &Instance) { // Search for the data segment we hold autolink entries in for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) { @@ -165,7 +166,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1, /*KeepEmpty=*/false); for (const auto &Flag : SplitFlags) - LinkerFlags.push_back(Flag.str()); + LinkerFlags.insert(Flag.str()); } } return false; @@ -178,7 +179,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, static bool extractLinkerFlags(const llvm::object::Binary *Bin, CompilerInstance &Instance, StringRef BinaryFileName, - std::vector &LinkerFlags) { + llvm::SetVector &LinkerFlags) { if (auto *ObjectFile = llvm::dyn_cast(Bin)) { return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); } else if (auto *ObjectFile = @@ -227,7 +228,7 @@ int autolink_extract_main(ArrayRef Args, const char *Argv0, return 1; } - std::vector LinkerFlags; + llvm::SetVector LinkerFlags; // Extract the linker flags from the objects. for (const auto &BinaryFileName : Invocation.getInputFilenames()) { diff --git a/test/AutolinkExtract/import.swift b/test/AutolinkExtract/import.swift index ae8b929c7e132..b5a7fcc119568 100644 --- a/test/AutolinkExtract/import.swift +++ b/test/AutolinkExtract/import.swift @@ -1,10 +1,16 @@ // RUN: %empty-directory(%t) // RUN: %target-swiftc_driver -emit-module -emit-module-path %t/empty.swiftmodule -module-name empty -module-link-name empty %S/empty.swift // RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental.o -// RUN: %target-swift-autolink-extract %t/import_experimental.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s +// RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental_again.o +// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s + +// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix UNIQUE %s // REQUIRES: autolink-extract +// UNIQUE-COUNT-1: -lswiftCore +// UNIQUE-COUNT-1: -lempty + // CHECK-elf-DAG: -lswiftCore // CHECK-elf-DAG: -lempty