From 844ab6a0126a6379712e1db5c3c3a93653022f99 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Wed, 16 Nov 2016 06:06:44 +0000 Subject: [PATCH] [Driver] Infer the correct option to ld64 for -fembed-bitcode Summary: -fembed-bitcode infers -bitcode_bundle to ld64 but it is not correctly passed when using LTO. LTO is a special case of -fembed-bitcode which it doesn't require embed the bitcode in a special section in the object file but it requires linker to save that as part of the final executable. rdar://problem/29274226 Reviewers: mehdi_amini Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26690 llvm-svn: 287084 --- clang/include/clang/Driver/Driver.h | 10 ++++++-- clang/lib/Driver/Driver.cpp | 36 ++++++++++++----------------- clang/lib/Driver/Tools.cpp | 8 +++---- clang/test/Driver/embed-bitcode.c | 12 ++++++++++ 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h index 2e938fdda831..9104658485ba 100644 --- a/clang/include/clang/Driver/Driver.h +++ b/clang/include/clang/Driver/Driver.h @@ -289,8 +289,14 @@ public: bool isSaveTempsEnabled() const { return SaveTemps != SaveTempsNone; } bool isSaveTempsObj() const { return SaveTemps == SaveTempsObj; } - bool embedBitcodeEnabled() const { return BitcodeEmbed == EmbedBitcode; } - bool embedBitcodeMarkerOnly() const { return BitcodeEmbed == EmbedMarker; } + bool embedBitcodeEnabled() const { return BitcodeEmbed != EmbedNone; } + bool embedBitcodeInObject() const { + // LTO has no object file output so ignore embed bitcode option in LTO. + return (BitcodeEmbed == EmbedBitcode) && !isUsingLTO(); + } + bool embedBitcodeMarkerOnly() const { + return (BitcodeEmbed == EmbedMarker) && !isUsingLTO(); + } /// Compute the desired OpenMP runtime from the flags provided. OpenMPRuntimeKind getOpenMPRuntime(const llvm::opt::ArgList &Args) const; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 61b277df1529..bdf1c08b7304 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -626,26 +626,20 @@ Compilation *Driver::BuildCompilation(ArrayRef ArgList) { setLTOMode(Args); - // Ignore -fembed-bitcode options with LTO - // since the output will be bitcode anyway. - if (getLTOMode() == LTOK_None) { - if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { - StringRef Name = A->getValue(); - unsigned Model = llvm::StringSwitch(Name) - .Case("off", EmbedNone) - .Case("all", EmbedBitcode) - .Case("bitcode", EmbedBitcode) - .Case("marker", EmbedMarker) - .Default(~0U); - if (Model == ~0U) { - Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) - << Name; - } else - BitcodeEmbed = static_cast(Model); - } - } else { - // claim the bitcode option under LTO so no warning is issued. - Args.ClaimAllArgs(options::OPT_fembed_bitcode_EQ); + // Process -fembed-bitcode= flags. + if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { + StringRef Name = A->getValue(); + unsigned Model = llvm::StringSwitch(Name) + .Case("off", EmbedNone) + .Case("all", EmbedBitcode) + .Case("bitcode", EmbedBitcode) + .Case("marker", EmbedMarker) + .Default(~0U); + if (Model == ~0U) { + Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) + << Name; + } else + BitcodeEmbed = static_cast(Model); } std::unique_ptr UArgs = @@ -3059,7 +3053,7 @@ InputInfo Driver::BuildJobsForActionNoCache( const JobAction *JA = cast(A); ActionList CollapsedOffloadActions; - ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), embedBitcodeEnabled()); + ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), embedBitcodeInObject()); const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions); if (!T) diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 45ba6996520c..3662010037e8 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -4162,7 +4162,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, } // Embed-bitcode option. - if (C.getDriver().embedBitcodeEnabled() && + if (C.getDriver().embedBitcodeInObject() && (isa(JA) || isa(JA))) { // Add flags implied by -fembed-bitcode. Args.AddLastArg(CmdArgs, options::OPT_fembed_bitcode_EQ); @@ -6352,7 +6352,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // pristine IR generated by the frontend. Ideally, a new compile action should // be added so both IR can be captured. if (C.getDriver().isSaveTempsEnabled() && - !C.getDriver().embedBitcodeEnabled() && isa(JA)) + !C.getDriver().embedBitcodeInObject() && isa(JA)) CmdArgs.push_back("-disable-llvm-passes"); if (Output.getType() == types::TY_Dependencies) { @@ -8321,9 +8321,9 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args, else CmdArgs.push_back("-no_pie"); } + // for embed-bitcode, use -bitcode_bundle in linker command - if (C.getDriver().embedBitcodeEnabled() || - C.getDriver().embedBitcodeMarkerOnly()) { + if (C.getDriver().embedBitcodeEnabled()) { // Check if the toolchain supports bitcode build flow. if (MachOTC.SupportsEmbeddedBitcode()) CmdArgs.push_back("-bitcode_bundle"); diff --git a/clang/test/Driver/embed-bitcode.c b/clang/test/Driver/embed-bitcode.c index da60da3fff7b..36314e664093 100644 --- a/clang/test/Driver/embed-bitcode.c +++ b/clang/test/Driver/embed-bitcode.c @@ -41,3 +41,15 @@ // CHECK-MARKER: -fembed-bitcode=marker // CHECK-MARKER-NOT: -cc1 +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=6.0 %s -fembed-bitcode=all -fintegrated-as 2>&1 -### | FileCheck %s -check-prefix=CHECK-LINKER +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=6.0 %s -fembed-bitcode=marker -fintegrated-as 2>&1 -### | FileCheck %s -check-prefix=CHECK-LINKER +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=6.0 %s -flto=full -fembed-bitcode=bitcode -fintegrated-as 2>&1 -### | FileCheck %s -check-prefix=CHECK-LINKER +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=6.0 %s -flto=thin -fembed-bitcode=bitcode -fintegrated-as 2>&1 -### | FileCheck %s -check-prefix=CHECK-LINKER +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=6.0 %s -fembed-bitcode=off -fintegrated-as 2>&1 -### | FileCheck %s -check-prefix=CHECK-NO-LINKER +// CHECK-LINKER: ld +// CHECK-LINKER: -bitcode_bundle +// CHECK-NO-LINKER-NOT: -bitcode_bundle + +// RUN: %clang -target armv7-apple-darwin -miphoneos-version-min=5.0 %s -fembed-bitcode -### 2>&1 | \ +// RUN: FileCheck %s -check-prefix=CHECK-PLATFORM-UNSUPPORTED +// CHECK-PLATFORM-UNSUPPORTED: -fembed-bitcode is not supported on versions of iOS prior to 6.0