Skip to content
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

[LLD] [COFF] Port -lto-sample-profile to COFF version of LLD #85701

Merged
merged 2 commits into from
Mar 20, 2024

Conversation

chrulski-intel
Copy link
Contributor

Following the commit of #83972 which added COFF support for SPGO,
this patch ports the support of the option -lto-sample-profile that was only
available in the ELF variant of LLD to the COFF variant to enable running the
SPGO passes in the LTO/thinLTO pipelines.

Following the commit of llvm#83972 which
added COFF support for SPGO, this patch ports the support of the option
-lto-sample-profile that was only available in the ELF variant of LLD to the
COFF variant to enable SPGO passes of the LTO/thinLTO pipelines.
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 18, 2024

@llvm/pr-subscribers-lld-coff
@llvm/pr-subscribers-lld

@llvm/pr-subscribers-platform-windows

Author: None (chrulski-intel)

Changes

Following the commit of #83972 which added COFF support for SPGO,
this patch ports the support of the option -lto-sample-profile that was only
available in the ELF variant of LLD to the COFF variant to enable running the
SPGO passes in the LTO/thinLTO pipelines.


Full diff: https://github.com/llvm/llvm-project/pull/85701.diff

6 Files Affected:

  • (modified) lld/COFF/Config.h (+3)
  • (modified) lld/COFF/Driver.cpp (+1)
  • (modified) lld/COFF/LTO.cpp (+1)
  • (modified) lld/COFF/Options.td (+1)
  • (added) lld/test/COFF/Inputs/sample-profile.prof (+1)
  • (added) lld/test/COFF/sample-profile.ll (+23)
diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 018f03b211e422..8f85929f1bea7f 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -263,6 +263,9 @@ struct Configuration {
   // Used for /lto-pgo-warn-mismatch:
   bool ltoPGOWarnMismatch = true;
 
+  // Used for /lto-sample-profile:
+  llvm::StringRef ltoSampleProfileName;
+
   // Used for /call-graph-ordering-file:
   llvm::MapVector<std::pair<const SectionChunk *, const SectionChunk *>,
                   uint64_t>
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 22ee2f133be98a..1b075389325a91 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2028,6 +2028,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   config->ltoObjPath = args.getLastArgValue(OPT_lto_obj_path);
   config->ltoCSProfileGenerate = args.hasArg(OPT_lto_cs_profile_generate);
   config->ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file);
+  config->ltoSampleProfileName = args.getLastArgValue(OPT_lto_sample_profile);
   // Handle miscellaneous boolean flags.
   config->ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch,
                                             OPT_lto_pgo_warn_mismatch_no, true);
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 7df93191121367..be49aa6e8bb3da 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -86,6 +86,7 @@ lto::Config BitcodeCompiler::createConfig() {
   c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile);
   c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;
   c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;
+  c.SampleProfile = ctx.config.ltoSampleProfileName;
   c.TimeTraceEnabled = ctx.config.timeTraceEnabled;
   c.TimeTraceGranularity = ctx.config.timeTraceGranularity;
 
diff --git a/lld/COFF/Options.td b/lld/COFF/Options.td
index 4dab4a20717392..1e78a560bca864 100644
--- a/lld/COFF/Options.td
+++ b/lld/COFF/Options.td
@@ -79,6 +79,7 @@ def lldltocachepolicy : P<"lldltocachepolicy",
     "Pruning policy for the ThinLTO cache">;
 def lldsavetemps : F<"lldsavetemps">,
     HelpText<"Save intermediate LTO compilation results">;
+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">;
 def mllvm   : P<"mllvm", "Options to pass to LLVM">;
diff --git a/lld/test/COFF/Inputs/sample-profile.prof b/lld/test/COFF/Inputs/sample-profile.prof
new file mode 100644
index 00000000000000..0ccd747bd376f8
--- /dev/null
+++ b/lld/test/COFF/Inputs/sample-profile.prof
@@ -0,0 +1 @@
+f:0:0
diff --git a/lld/test/COFF/sample-profile.ll b/lld/test/COFF/sample-profile.ll
new file mode 100644
index 00000000000000..4cf0b55e3688d0
--- /dev/null
+++ b/lld/test/COFF/sample-profile.ll
@@ -0,0 +1,23 @@
+; REQUIRES: x86
+; RUN: opt -module-summary %s -o %t1.o
+; RUN: opt -module-summary %p/Inputs/thinlto.ll -o %t2.o
+
+; RUN: rm -f %t1.o.4.opt.bc
+; RUN: lld-link /lto-sample-profile:%p/Inputs/sample-profile.prof /lldsavetemps /entry:f /subsystem:console %t1.o %t2.o /out:%t3.exe
+; RUN: opt -S %t1.o.4.opt.bc | FileCheck %s
+
+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-msvc19.0.24215"
+
+; CHECK: ![[#]] = !{i32 1, !"ProfileSummary", ![[#]]}
+declare void @g(...)
+
+define void @h() {
+  ret void
+}
+define void @f() {
+entry:
+  call void (...) @g()
+  call void (...) @h()
+  ret void
+}

@HaohaiWen
Copy link
Contributor

HaohaiWen commented Mar 19, 2024

Rename sample-profile.ll/.prof to lto-sample-profile.ll/.prof?
Those files for ELF are in lto folder. LTO tests for COFF normally starts with lto- prefix.

@chrulski-intel
Copy link
Contributor Author

Rename sample-profile.ll/.prof to lto-sample-profile.ll/.prof? Those files for ELF are in lto folder. LTO tests for COFF normally starts with lto- prefix.

Done

@mstorsjo
Copy link
Member

FYI, it'd be nice to support the same new LTO options in the mingw frontend as well. The mingw frontend uses the same options syntax as the ELF frontend, so in most cases it's just case of copying the new options definitions from lld/ELF/Options.td into lld/MinGW/Options.td, adding a mapping from the ELF style options to the lld-link style option in lld/MinGW/Driver.cpp, and adding a test case in lld/test/MinGW/driver.test.

This is clearly not a requirement for this PR in itself, but if you'd have a bit of extra time to invest in it, it'd be appreciated. Otherwise I can try to look into it later. (I'm trying to keep the MinGW driver up to date in supporting all the LTO options as the ELF driver, intersected with which features actually are supported in the COFF driver.)

chrulski-intel added a commit to chrulski-intel/llvm-project that referenced this pull request Mar 19, 2024
This has been a supported option for ELF and is added to the COFF Linker
in llvm#85701
@chrulski-intel
Copy link
Contributor Author

FYI, it'd be nice to support the same new LTO options in the mingw frontend as well. The mingw frontend uses the same options syntax as the ELF frontend, so in most cases it's just case of copying the new options definitions from lld/ELF/Options.td into lld/MinGW/Options.td, adding a mapping from the ELF style options to the lld-link style option in lld/MinGW/Driver.cpp, and adding a test case in lld/test/MinGW/driver.test.

This is clearly not a requirement for this PR in itself, but if you'd have a bit of extra time to invest in it, it'd be appreciated. Otherwise I can try to look into it later. (I'm trying to keep the MinGW driver up to date in supporting all the LTO options as the ELF driver, intersected with which features actually are supported in the COFF driver.)

@mstorsjo I gave it a go in #85841, please have a look when you can.

@HaohaiWen HaohaiWen merged commit a9fe23c into llvm:main Mar 20, 2024
4 checks passed
chrulski-intel added a commit to chrulski-intel/llvm-project that referenced this pull request Mar 20, 2024
This has been a supported option for ELF and is added to the COFF Linker
in llvm#85701
@chrulski-intel chrulski-intel deleted the COFF-lto-sample-profile branch March 20, 2024 18:01
HaohaiWen pushed a commit that referenced this pull request Mar 21, 2024
This has been a supported option for ELF and is added to the COFF Linker
in #85701
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
)

Following the commit of llvm#83972 which added COFF support for SPGO, this
patch ports the support of the option -lto-sample-profile that was only
available in the ELF variant of LLD to the COFF variant to enable
running the SPGO passes in the LTO/thinLTO pipelines.
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
This has been a supported option for ELF and is added to the COFF Linker
in llvm#85701
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants