Skip to content

[LLD][COFF] allow saving intermediate files with /lldsavetemps #115131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion lld/COFF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct Configuration {
Symbol *delayLoadHelper = nullptr;
Symbol *arm64ECIcallHelper = nullptr;

bool saveTemps = false;
llvm::DenseSet<llvm::StringRef> saveTempsArgs;

// /guard:cf
int guardCF = GuardCFLevel::Off;
Expand Down
18 changes: 16 additions & 2 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,10 @@ getVFS(const opt::InputArgList &args) {
return nullptr;
}

constexpr const char *lldsaveTempsValues[] = {
"resolution", "preopt", "promote", "internalize", "import",
"opt", "precodegen", "prelink", "combinedindex"};

void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
ScopedTimer rootTimer(ctx.rootTimer);
Configuration *config = &ctx.config;
Expand Down Expand Up @@ -2012,8 +2016,18 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
config->ltoDebugPassManager = ltoDebugPM;

// Handle /lldsavetemps
if (args.hasArg(OPT_lldsavetemps))
config->saveTemps = true;
if (args.hasArg(OPT_lldsavetemps)) {
for (const char *s : lldsaveTempsValues)
config->saveTempsArgs.insert(s);
} else {
for (auto *arg : args.filtered(OPT_lldsavetemps_colon)) {
StringRef s = arg->getValue();
if (llvm::is_contained(lldsaveTempsValues, s))
config->saveTempsArgs.insert(s);
else
error("unknown /lldsavetemps value: " + s);
}
}

// Handle /lldemit
if (auto *arg = args.getLastArg(OPT_lldemit)) {
Expand Down
7 changes: 4 additions & 3 deletions lld/COFF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ lto::Config BitcodeCompiler::createConfig() {
c.Options.MCOptions.AsmVerbose = true;
}

if (ctx.config.saveTemps)
if (!ctx.config.saveTempsArgs.empty())
checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".",
/*UseInputModulePath*/ true));
/*UseInputModulePath*/ true,
ctx.config.saveTempsArgs));
return c;
}

Expand Down Expand Up @@ -255,7 +256,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
sys::path::remove_dots(path, true);
ltoObjName = saver().save(path.str());
}
if (ctx.config.saveTemps || emitASM)
if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM)
saveBuffer(buf[i].second, ltoObjName);
if (!emitASM)
ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName)));
Expand Down
3 changes: 3 additions & 0 deletions lld/COFF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def lldltocachepolicy : P<"lldltocachepolicy",
"Pruning policy for the ThinLTO cache">;
def lldsavetemps : F<"lldsavetemps">,
HelpText<"Save intermediate LTO compilation results">;
def lldsavetemps_colon : Joined<["/", "-", "/?", "-?"], "lldsavetemps:">,
HelpText<"Save select intermediate LTO compilation results">,
Values<"resolution,preopt,promote,internalize,import,opt,precodegen,prelink,combinedindex">;
def lto_sample_profile: P<"lto-sample-profile", "Sample profile file path">;
def machine : P<"machine", "Specify target platform">;
def merge : P<"merge", "Combine sections">;
Expand Down
1 change: 1 addition & 0 deletions lld/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Breaking changes
COFF Improvements
-----------------
* ``/includeglob`` has been implemented to match the behavior of ``--undefined-glob`` available for ELF.
* ``/lldsavetemps`` allows saving select intermediate LTO compilation results (e.g. resolution, preopt, promote, internalize, import, opt, precodegen, prelink, combinedindex).

MinGW Improvements
------------------
Expand Down
64 changes: 64 additions & 0 deletions lld/test/COFF/savetemps-colon.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
; REQUIRES: x86
; RUN: rm -fr %T/savetemps-colon
; RUN: mkdir %T/savetemps-colon
; RUN: opt -thinlto-bc -o %T/savetemps-colon/savetemps.obj %s
; RUN: opt -thinlto-bc -o %T/savetemps-colon/thin1.obj %S/Inputs/thinlto.ll

;; Check preopt
; RUN: lld-link /lldsavetemps:preopt /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.obj.*.preopt.bc | count 2

;; Check promote
; RUN: lld-link /lldsavetemps:promote /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.obj.*.promote.bc | count 2

;; Check internalize
; RUN: lld-link /lldsavetemps:internalize /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.obj.*.internalize.bc | count 2

;; Check import
; RUN: lld-link /lldsavetemps:import /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.obj.*.import.bc | count 2

;; Check opt
;; Not supported on Windows due to difficulty with escaping "opt" across platforms.

;; Check precodegen
; RUN: lld-link /lldsavetemps:precodegen /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.obj.*.precodegen.bc | count 2

;; Check combinedindex
; RUN: lld-link /lldsavetemps:combinedindex /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.exe.index.bc | count 1

;; Check prelink
; RUN: lld-link /lldsavetemps:prelink /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.exe.lto.*.obj | count 2

;; Check resolution
; RUN: lld-link /lldsavetemps:resolution /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj
; RUN: ls %T/savetemps-colon/*.resolution.txt | count 1

;; Check error message
; RUN: not lld-link /lldsavetemps:notastage /out:%T/savetemps-colon/savetemps.exe /entry:main \
; RUN: /subsystem:console %T/savetemps-colon/savetemps.obj %T/savetemps-colon/thin1.obj 2>&1 \
; RUN: | FileCheck %s
; CHECK: unknown /lldsavetemps value: notastage

target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc"

declare void @g()

define i32 @main() {
call void @g()
ret i32 0
}
Loading