Flags<[DriverOption, CC1Option]>,
HelpText<"Disable generation of linker directives for automatic library linking">;
+def fembed_bitcode : Flag<["-"], "fembed-bitcode">, Group<f_Group>,
+ Flags<[CC1Option, CC1AsOption]>,
+ HelpText<"Embed LLVM IR bitcode as data">;
+def fembed_bitcode_marker : Flag<["-"], "fembed-bitcode-marker">,
+ Group<f_Group>, Flags<[CC1Option]>,
+ HelpText<"Embed placeholder LLVM IR data as a marker">;
def fgnu_inline_asm : Flag<["-"], "fgnu-inline-asm">, Group<f_Group>, Flags<[DriverOption]>;
def fno_gnu_inline_asm : Flag<["-"], "fno-gnu-inline-asm">, Group<f_Group>,
Flags<[DriverOption, CC1Option]>,
DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<vfs::FileSystem> VFS)
: Opts(createDriverOptTable()), Diags(Diags), VFS(VFS), Mode(GCCMode),
- SaveTemps(SaveTempsNone), LTOMode(LTOK_None),
+ SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), LTOMode(LTOK_None),
ClangExecutable(ClangExecutable),
SysRoot(DEFAULT_SYSROOT), UseStdLib(true),
DefaultTargetTriple(DefaultTargetTriple),
.Default(SaveTempsCwd);
}
+ // Ignore -fembed-bitcode options with LTO
+ // since the output will be bitcode anyway.
+ if (!Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false)) {
+ if (Args.hasArg(options::OPT_fembed_bitcode))
+ BitcodeEmbed = EmbedBitcode;
+ else if (Args.hasArg(options::OPT_fembed_bitcode_marker))
+ BitcodeEmbed = EmbedMarker;
+ } else {
+ // claim the bitcode option under LTO so no warning is issued.
+ Args.ClaimAllArgs(options::OPT_fembed_bitcode);
+ Args.ClaimAllArgs(options::OPT_fembed_bitcode_marker);
+ }
+
setLTOMode(Args);
std::unique_ptr<llvm::opt::InputArgList> UArgs =
// CudaHostAction, updates CollapsedCHA with the pointer to it so the
// caller can deal with extra handling such action requires.
static const Tool *selectToolForJob(Compilation &C, bool SaveTemps,
- const ToolChain *TC, const JobAction *JA,
+ bool EmbedBitcode, const ToolChain *TC,
+ const JobAction *JA,
const ActionList *&Inputs,
const CudaHostAction *&CollapsedCHA) {
const Tool *ToolForJob = nullptr;
!C.getArgs().hasArg(options::OPT__SLASH_Fa) &&
isa<AssembleJobAction>(JA) && Inputs->size() == 1 &&
isa<BackendJobAction>(*Inputs->begin())) {
- // A BackendJob is always preceded by a CompileJob, and without
- // -save-temps they will always get combined together, so instead of
- // checking the backend tool, check if the tool for the CompileJob
- // has an integrated assembler.
+ // A BackendJob is always preceded by a CompileJob, and without -save-temps
+ // or -fembed-bitcode, they will always get combined together, so instead of
+ // checking the backend tool, check if the tool for the CompileJob has an
+ // integrated assembler. For -fembed-bitcode, CompileJob is still used to
+ // look up tools for BackendJob, but they need to match before we can split
+ // them.
const ActionList *BackendInputs = &(*Inputs)[0]->getInputs();
// Compile job may be wrapped in CudaHostAction, extract it if
// that's the case and update CollapsedCHA if we combine phases.
const Tool *Compiler = TC->SelectTool(*CompileJA);
if (!Compiler)
return nullptr;
+ // When using -fembed-bitcode, it is required to have the same tool (clang)
+ // for both CompilerJA and BackendJA. Otherwise, combine two stages.
+ if (EmbedBitcode) {
+ JobAction *InputJA = cast<JobAction>(*Inputs->begin());
+ const Tool *BackendTool = TC->SelectTool(*InputJA);
+ if (BackendTool == Compiler)
+ CompileJA = InputJA;
+ }
if (Compiler->hasIntegratedAssembler()) {
Inputs = &CompileJA->getInputs();
ToolForJob = Compiler;
}
// A backend job should always be combined with the preceding compile job
- // unless OPT_save_temps is enabled and the compiler is capable of emitting
- // LLVM IR as an intermediate output.
+ // unless OPT_save_temps or OPT_fembed_bitcode is enabled and the compiler is
+ // capable of emitting LLVM IR as an intermediate output.
if (isa<BackendJobAction>(JA)) {
// Check if the compiler supports emitting LLVM IR.
assert(Inputs->size() == 1);
const Tool *Compiler = TC->SelectTool(*CompileJA);
if (!Compiler)
return nullptr;
- if (!Compiler->canEmitIR() || !SaveTemps) {
+ if (!Compiler->canEmitIR() ||
+ (!SaveTemps && !EmbedBitcode)) {
Inputs = &CompileJA->getInputs();
ToolForJob = Compiler;
CollapsedCHA = CHA;
const JobAction *JA = cast<JobAction>(A);
const CudaHostAction *CollapsedCHA = nullptr;
const Tool *T =
- selectToolForJob(C, isSaveTempsEnabled(), TC, JA, Inputs, CollapsedCHA);
+ selectToolForJob(C, isSaveTempsEnabled(), embedBitcodeEnabled(), TC, JA,
+ Inputs, CollapsedCHA);
if (!T)
return InputInfo();
Args.AddLastArg(CmdArgs, options::OPT_fthinlto_index_EQ);
}
+ // Embed-bitcode option.
+ if (C.getDriver().embedBitcodeEnabled() &&
+ (isa<BackendJobAction>(JA) || isa<AssembleJobAction>(JA))) {
+ // Add flags implied by -fembed-bitcode.
+ CmdArgs.push_back("-fembed-bitcode");
+ // Disable all llvm IR level optimizations.
+ CmdArgs.push_back("-disable-llvm-optzns");
+ }
+ if (C.getDriver().embedBitcodeMarkerOnly())
+ CmdArgs.push_back("-fembed-bitcode-marker");
+
// We normally speed up the clang process a bit by skipping destructors at
// exit, but when we're generating diagnostics we can rely on some of the
// cleanup.
else
CmdArgs.push_back("-no_pie");
}
+ // for embed-bitcode, use -bitcode_bundle in linker command
+ if (C.getDriver().embedBitcodeEnabled() ||
+ C.getDriver().embedBitcodeMarkerOnly()) {
+ // Check if the toolchain supports bitcode build flow.
+ if (MachOTC.SupportsEmbeddedBitcode())
+ CmdArgs.push_back("-bitcode_bundle");
+ else
+ D.Diag(diag::err_drv_bitcode_unsupported_on_toolchain);
+ }
Args.AddLastArg(CmdArgs, options::OPT_prebind);
Args.AddLastArg(CmdArgs, options::OPT_noprebind);
--- /dev/null
+// RUN: %clang -ccc-print-bindings -c %s -fembed-bitcode 2>&1 | FileCheck %s
+// CHECK: clang
+// CHECK: clang
+
+// RUN: %clang %s -fembed-bitcode 2>&1 -### | FileCheck %s -check-prefix=CHECK-CC
+// CHECK-CC: -cc1
+// CHECK-CC: -emit-llvm-bc
+// CHECK-CC: -cc1
+// CHECK-CC: -emit-obj
+// CHECK-CC: -fembed-bitcode
+// CHECK-CC: ld
+// CHECK-CC: -bitcode_bundle
+
+// RUN: %clang %s -save-temps -fembed-bitcode 2>&1 -### | FileCheck %s -check-prefix=CHECK-SAVE-TEMP
+// CHECK-SAVE-TEMP: -cc1
+// CHECK-SAVE-TEMP: -E
+// CHECK-SAVE-TEMP: -cc1
+// CHECK-SAVE-TEMP: -emit-llvm-bc
+// CHECK-SAVE-TEMP: -cc1
+// CHECK-SAVE-TEMP: -S
+// CHECK-SAVE-TEMP: -fembed-bitcode
+// CHECK-SAVE-TEMP: -cc1as
+// CHECK-SAVE-TEMP: ld
+// CHECK-SAVE-TEMP: -bitcode_bundle
+
+// RUN: %clang -c %s -flto -fembed-bitcode 2>&1 -### | FileCheck %s -check-prefix=CHECK-LTO
+// CHECK-LTO: -cc1
+// CHECK-LTO: -emit-llvm-bc
+// CHECK-LTO-NOT: warning: argument unused during compilation: '-fembed-bitcode'
+// CHECK-LTO-NOT: -cc1
+// CHECK-LTO-NOT: -fembed-bitcode
+
+// RUN: %clang -c %s -fembed-bitcode-marker 2>&1 -### | FileCheck %s -check-prefix=CHECK-MARKER
+// CHECK-MARKER: -cc1
+// CHECK-MARKER: -emit-obj
+// CHECK-MARKER: -fembed-bitcode-marker
+// CHECK-MARKER-NOT: -cc1
+