Skip to content
Open
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
1 change: 1 addition & 0 deletions lld/MachO/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ struct Configuration {
std::vector<SectionAlign> sectionAlignments;
std::vector<SegmentProtection> segmentProtections;
bool ltoDebugPassManager = false;
bool emitLLVM = false;
llvm::StringRef codegenDataGeneratePath;
bool csProfileGenerate = false;
llvm::StringRef csProfilePath;
Expand Down
9 changes: 5 additions & 4 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
config->ignoreAutoLinkOptions.insert(arg->getValue());
config->strictAutoLink = args.hasArg(OPT_strict_auto_link);
config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
config->emitLLVM = args.hasArg(OPT_lto_emit_llvm);
config->codegenDataGeneratePath =
args.getLastArgValue(OPT_codegen_data_generate_path);
config->csProfileGenerate = args.hasArg(OPT_cs_profile_generate);
Expand Down Expand Up @@ -2344,10 +2345,10 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,

resolveLCLinkerOptions();

// If --thinlto-index-only is given, we should create only "index
// files" and not object files. Index file creation is already done
// in compileBitcodeFiles, so we are done if that's the case.
if (config->thinLTOIndexOnly)
// If either --thinlto-index-only or --lto-emit-llvm is given, we should
// not create object files. Index file creation is already done in
// compileBitcodeFiles, so we are done if that's the case.
if (config->thinLTOIndexOnly || config->emitLLVM)
return errorCount() == 0;

// LTO may emit a non-hidden (extern) object file symbol even if the
Expand Down
10 changes: 10 additions & 0 deletions lld/MachO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ static lto::Config createConfig() {
if (config->saveTemps)
checkError(c.addSaveTemps(config->outputFile.str() + ".",
/*UseInputModulePath=*/true));

if (config->emitLLVM) {
llvm::StringRef outputFile = config->outputFile;
c.PreCodeGenModuleHook = [outputFile](size_t task, const Module &m) {
if (std::unique_ptr<raw_fd_ostream> os = openLTOOutputFile(outputFile))
WriteBitcodeToFile(m, *os, false);
return false;
};
}

return c;
}

Expand Down
2 changes: 2 additions & 0 deletions lld/MachO/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def lto_debug_pass_manager: Flag<["--"], "lto-debug-pass-manager">,
HelpText<"Debug new pass manager">, Group<grp_lld>;
def lto_newpm_passes: Joined<["--"], "lto-newpm-passes=">,
HelpText<"Passes to run during LTO">, Group<grp_lld>;
def lto_emit_llvm: Flag<["--"], "lto-emit-llvm">,
HelpText<"Emit LLVM-IR bitcode">, Group<grp_lld>;
def load_pass_plugins : Separate<["--"], "load-pass-plugin">, Group<grp_lld>;
def load_pass_plugins_eq : Joined<["--"], "load-pass-plugin=">,
Alias<!cast<Separate>(load_pass_plugins)>,
Expand Down
18 changes: 18 additions & 0 deletions lld/test/MachO/lto-emit-llvm.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; REQUIRES: x86
;
; Check that the --lto-emit-llvm option is handled correctly.
;
; RUN: opt %s -o %t.o
; RUN: ld.lld --lto-emit-llvm %t.o -o %t.out.o
; RUN: llvm-dis < %t.out.o -o - | FileCheck %s
;
; CHECK: define hidden void @main()

target triple = "x86_64-apple-darwin"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

@llvm.compiler.used = appending global [1 x ptr] [ptr @main], section "llvm.metadata"

define hidden void @main() {
ret void
}
Loading