From 1d84cb60a8ad47d93d48c2e5e097b68239faf5ff Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 12 Aug 2019 09:19:16 -0700 Subject: [PATCH] Driver: loosen restrictions for `-force-autolink-symbol` On targets which support COMDAT (PE/COFF, ELF), use that to relax the constraints on the emission of `-force-autolink-symbol` so that it is possible to use that with `-incremental`. --- lib/Driver/Driver.cpp | 13 +++++++++---- lib/IRGen/IRGenModule.cpp | 5 ++++- test/Driver/autolink-force-load-comdat.swift | 11 +++++++++++ test/Driver/autolink-force-load-no-comdat.swift | 8 ++++++++ test/Driver/options.swift | 6 +----- 5 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 test/Driver/autolink-force-load-comdat.swift create mode 100644 test/Driver/autolink-force-load-no-comdat.swift diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index b7a30945334a0..9dd28b9075367 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -206,7 +206,8 @@ static void validateSearchPathArgs(DiagnosticEngine &diags, } static void validateAutolinkingArgs(DiagnosticEngine &diags, - const ArgList &args) { + const ArgList &args, + const llvm::Triple &T) { auto *forceLoadArg = args.getLastArg(options::OPT_autolink_force_load); if (!forceLoadArg) return; @@ -214,6 +215,9 @@ static void validateAutolinkingArgs(DiagnosticEngine &diags, if (!incrementalArg) return; + if (T.supportsCOMDAT()) + return; + // Note: -incremental can itself be overridden by other arguments later // on, but since -autolink-force-load is a rare and not-really-recommended // option it's not worth modeling that complexity here (or moving the @@ -223,14 +227,15 @@ static void validateAutolinkingArgs(DiagnosticEngine &diags, } /// Perform miscellaneous early validation of arguments. -static void validateArgs(DiagnosticEngine &diags, const ArgList &args) { +static void validateArgs(DiagnosticEngine &diags, const ArgList &args, + const llvm::Triple &T) { validateBridgingHeaderArgs(diags, args); validateWarningControlArgs(diags, args); validateProfilingArgs(diags, args); validateDebugInfoArgs(diags, args); validateCompilationConditionArgs(diags, args); validateSearchPathArgs(diags, args); - validateAutolinkingArgs(diags, args); + validateAutolinkingArgs(diags, args, T); } std::unique_ptr @@ -783,7 +788,7 @@ Driver::buildCompilation(const ToolChain &TC, std::unique_ptr TranslatedArgList( translateInputAndPathArgs(*ArgList, workingDirectory)); - validateArgs(Diags, *TranslatedArgList); + validateArgs(Diags, *TranslatedArgList, TC.getTriple()); // Perform toolchain specific args validation. TC.validateArguments(Diags, *TranslatedArgList); diff --git a/lib/IRGen/IRGenModule.cpp b/lib/IRGen/IRGenModule.cpp index 7d616c2396657..ebe565371ee50 100644 --- a/lib/IRGen/IRGenModule.cpp +++ b/lib/IRGen/IRGenModule.cpp @@ -1184,7 +1184,7 @@ void IRGenModule::emitAutolinkInfo() { } if (!IRGen.Opts.ForceLoadSymbolName.empty() && - isFirstObjectFileInModule(*this)) { + (Triple.supportsCOMDAT() || isFirstObjectFileInModule(*this))) { llvm::SmallString<64> buf; encodeForceLoadSymbolName(buf, IRGen.Opts.ForceLoadSymbolName); auto ForceImportThunk = @@ -1192,6 +1192,9 @@ void IRGenModule::emitAutolinkInfo() { llvm::GlobalValue::ExternalLinkage, buf, &Module); ApplyIRLinkage(IRLinkage::ExternalExport).to(ForceImportThunk); + if (Triple.supportsCOMDAT()) + if (auto *GO = cast(ForceImportThunk)) + GO->setComdat(Module.getOrInsertComdat(ForceImportThunk->getName())); auto BB = llvm::BasicBlock::Create(getLLVMContext(), "", ForceImportThunk); llvm::IRBuilder<> IRB(BB); diff --git a/test/Driver/autolink-force-load-comdat.swift b/test/Driver/autolink-force-load-comdat.swift new file mode 100644 index 0000000000000..4870a5f12a132 --- /dev/null +++ b/test/Driver/autolink-force-load-comdat.swift @@ -0,0 +1,11 @@ +// RUN: %swiftc_driver -incremental -autolink-force-load %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s +// RUN: %swiftc_driver -autolink-force-load -incremental %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s + +// MACHO targets do not support COMDAT +// UNSUPPORTED: OS=macosx +// UNSUPPORTED: OS=tvos +// UNSUPPORTED: OS=watchos +// UNSUPPORTED: OS=ios + +// AUTOLINK_FORCE_LOAD-NOT: error: '-autolink-force-load' is not supported with '-incremental' + diff --git a/test/Driver/autolink-force-load-no-comdat.swift b/test/Driver/autolink-force-load-no-comdat.swift new file mode 100644 index 0000000000000..c4dc53a473410 --- /dev/null +++ b/test/Driver/autolink-force-load-no-comdat.swift @@ -0,0 +1,8 @@ +// RUN: not %swiftc_driver -incremental -autolink-force-load %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s +// RUN: not %swiftc_driver -autolink-force-load -incremental %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s + +// MACHO targets do not support COMDAT +// REQUIRES-ANY: OS=macosx, OS=tvos, OS=watchos, OS=ios + +// AUTOLINK_FORCE_LOAD: error: '-autolink-force-load' is not supported with '-incremental' + diff --git a/test/Driver/options.swift b/test/Driver/options.swift index f86a9b2d166fa..c884e3ab9edeb 100644 --- a/test/Driver/options.swift +++ b/test/Driver/options.swift @@ -101,10 +101,6 @@ // ASSUME_SINGLE_THREADED: swift // ASSUME_SINGLE_THREADED: -frontend {{.*}} -assume-single-threaded -// RUN: not %swiftc_driver -incremental -autolink-force-load %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s -// RUN: not %swiftc_driver -autolink-force-load -incremental %s 2>&1 | %FileCheck -check-prefix=AUTOLINK_FORCE_LOAD %s -// AUTOLINK_FORCE_LOAD: error: '-autolink-force-load' is not supported with '-incremental' - // RUN: %swift_driver -### -g -debug-info-format=codeview %s | %FileCheck -check-prefix DEBUG_INFO_FORMAT_CODEVIEW %s // RUN: %swift_driver -### -g -debug-info-format=dwarf %s | %FileCheck -check-prefix DEBUG_INFO_FORMAT_DWARF %s // RUN: %swiftc_driver -### -g -debug-info-format=codeview %s | %FileCheck -check-prefix DEBUG_INFO_FORMAT_CODEVIEW %s @@ -132,4 +128,4 @@ // RUN: %swiftc_driver -F %t/test.framework/ %s 2>&1 | %FileCheck -check-prefix SEARCH_PATH_INCLUDES_FRAMEWORK_EXTENSION %s // RUN: %swift_driver -Fsystem %t/test.framework/ %s 2>&1 | %FileCheck -check-prefix SEARCH_PATH_INCLUDES_FRAMEWORK_EXTENSION %s // RUN: %swiftc_driver -Fsystem %t/test.framework/ %s 2>&1 | %FileCheck -check-prefix SEARCH_PATH_INCLUDES_FRAMEWORK_EXTENSION %s -// SEARCH_PATH_INCLUDES_FRAMEWORK_EXTENSION: warning: framework search path ends in ".framework" \ No newline at end of file +// SEARCH_PATH_INCLUDES_FRAMEWORK_EXTENSION: warning: framework search path ends in ".framework"