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

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.

GitOrigin-RevId: a9fe23cde3ee554f4bd6118edcc2e747f3a8d8d5
diff --git a/COFF/Config.h b/COFF/Config.h
index 018f03b..8f85929 100644
--- a/COFF/Config.h
+++ b/COFF/Config.h
@@ -263,6 +263,9 @@
   // 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/COFF/Driver.cpp b/COFF/Driver.cpp
index 22ee2f1..1b07538 100644
--- a/COFF/Driver.cpp
+++ b/COFF/Driver.cpp
@@ -2028,6 +2028,7 @@
   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/COFF/LTO.cpp b/COFF/LTO.cpp
index 7df9319..be49aa6 100644
--- a/COFF/LTO.cpp
+++ b/COFF/LTO.cpp
@@ -86,6 +86,7 @@
   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/COFF/Options.td b/COFF/Options.td
index 4dab4a2..1e78a56 100644
--- a/COFF/Options.td
+++ b/COFF/Options.td
@@ -79,6 +79,7 @@
     "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/test/COFF/Inputs/lto-sample-profile.prof b/test/COFF/Inputs/lto-sample-profile.prof
new file mode 100644
index 0000000..0ccd747
--- /dev/null
+++ b/test/COFF/Inputs/lto-sample-profile.prof
@@ -0,0 +1 @@
+f:0:0
diff --git a/test/COFF/lto-sample-profile.ll b/test/COFF/lto-sample-profile.ll
new file mode 100644
index 0000000..64d4c8a
--- /dev/null
+++ b/test/COFF/lto-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/lto-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
+}