[clang] [Driver][MSVC] Add lto-sample-profile option for lld-link (PR #127442)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 18 19:13:15 PST 2025
https://github.com/tianleliu updated https://github.com/llvm/llvm-project/pull/127442
>From ad4d2857220e5e5b31cd66a79eac7d8dca438864 Mon Sep 17 00:00:00 2001
From: tianleli <tianle.l.liu at intel.com>
Date: Mon, 17 Feb 2025 11:24:10 +0800
Subject: [PATCH 1/3] [SPGO][Driver] Add lto-sample-profile option for lld-link
SPGO in lto mode, linker needs -lto-sample-profile option to set
sample profile file. Linux adds this option by transfering
fprofile-sample-use but lld-link on Windows misses the transfering.
So add it now.
---
clang/lib/Driver/ToolChains/MSVC.cpp | 3 +++
clang/test/Driver/cl-link.c | 3 +++
2 files changed, 6 insertions(+)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index bae41fc06c036..1c6854e3ef775 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -232,6 +232,9 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
}
+ if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ))
+ CmdArgs.push_back(Args.MakeArgString(std::string("-lto-sample-profile:") +
+ A->getValue()));
Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
// Control Flow Guard checks
diff --git a/clang/test/Driver/cl-link.c b/clang/test/Driver/cl-link.c
index 9bf8a8137926d..f1097ad0be9bf 100644
--- a/clang/test/Driver/cl-link.c
+++ b/clang/test/Driver/cl-link.c
@@ -71,3 +71,6 @@
// RUN: %clang_cl -m32 -arch:IA32 --target=i386-pc-win32 /Tc%s -fuse-ld=lld -### -fsanitize=address 2>&1 | FileCheck --check-prefix=INFER-LLD %s
// INFER-LLD: lld-link
// INFER-LLD-NOT: INFERASANLIBS
+
+// RUN: %clang_cl --target=x86_64-unknown-windows-msvc -fuse-ld=lld -### -S -fprofile-sample-use=%S/Inputs/file.prof %s 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s
+// CHECK-SAMPLE-PROFILE: "-lto-sample-profile:{{.*}}/file.prof"
>From 1a61846294298a6800be225f8fe6c8ebacc0a9d1 Mon Sep 17 00:00:00 2001
From: tianleli <tianle.l.liu at intel.com>
Date: Mon, 17 Feb 2025 15:50:51 +0800
Subject: [PATCH 2/3] Add flto condition for the option.
---
clang/lib/Driver/ToolChains/MSVC.cpp | 8 +++++---
clang/test/Driver/cl-link.c | 2 +-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 1c6854e3ef775..18b1b0330bcf7 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -232,9 +232,11 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
}
- if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ))
- CmdArgs.push_back(Args.MakeArgString(std::string("-lto-sample-profile:") +
- A->getValue()));
+ if (C.getDriver().isUsingLTO()) {
+ if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ))
+ CmdArgs.push_back(Args.MakeArgString(std::string("-lto-sample-profile:") +
+ A->getValue()));
+ }
Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
// Control Flow Guard checks
diff --git a/clang/test/Driver/cl-link.c b/clang/test/Driver/cl-link.c
index f1097ad0be9bf..3fd7dccec2267 100644
--- a/clang/test/Driver/cl-link.c
+++ b/clang/test/Driver/cl-link.c
@@ -72,5 +72,5 @@
// INFER-LLD: lld-link
// INFER-LLD-NOT: INFERASANLIBS
-// RUN: %clang_cl --target=x86_64-unknown-windows-msvc -fuse-ld=lld -### -S -fprofile-sample-use=%S/Inputs/file.prof %s 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s
+// RUN: %clang_cl --target=x86_64-unknown-windows-msvc -flto -fuse-ld=lld -### -S -fprofile-sample-use=%S/Inputs/file.prof %s 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s
// CHECK-SAMPLE-PROFILE: "-lto-sample-profile:{{.*}}/file.prof"
>From 54e5be4b4327f66359d6aa41aee8ae1005b8612b Mon Sep 17 00:00:00 2001
From: tianleli <tianle.l.liu at intel.com>
Date: Wed, 19 Feb 2025 11:11:49 +0800
Subject: [PATCH 3/3] Use getLastProfileSampleUseArg instead and remove -S in
test.
---
clang/lib/Driver/ToolChains/MSVC.cpp | 2 +-
clang/test/Driver/cl-link.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 18b1b0330bcf7..d5a7fc7e85230 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -233,7 +233,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
if (C.getDriver().isUsingLTO()) {
- if (Arg *A = Args.getLastArg(options::OPT_fprofile_sample_use_EQ))
+ if (Arg *A = tools::getLastProfileSampleUseArg(Args))
CmdArgs.push_back(Args.MakeArgString(std::string("-lto-sample-profile:") +
A->getValue()));
}
diff --git a/clang/test/Driver/cl-link.c b/clang/test/Driver/cl-link.c
index 3fd7dccec2267..726bc26a64edd 100644
--- a/clang/test/Driver/cl-link.c
+++ b/clang/test/Driver/cl-link.c
@@ -72,5 +72,5 @@
// INFER-LLD: lld-link
// INFER-LLD-NOT: INFERASANLIBS
-// RUN: %clang_cl --target=x86_64-unknown-windows-msvc -flto -fuse-ld=lld -### -S -fprofile-sample-use=%S/Inputs/file.prof %s 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s
+// RUN: %clang_cl --target=x86_64-unknown-windows-msvc /Tc%s -flto -fuse-ld=lld -### -fprofile-sample-use=%S/Inputs/file.prof 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s
// CHECK-SAMPLE-PROFILE: "-lto-sample-profile:{{.*}}/file.prof"
More information about the cfe-commits
mailing list