[flang-commits] [clang] [flang] [Flang][Driver]Add support for option '-fpseudo-probe-for-profiling' in flang (PR #205046)
Kaviya Rajendiran via flang-commits
flang-commits at lists.llvm.org
Wed Jun 24 04:03:55 PDT 2026
https://github.com/kaviya2510 updated https://github.com/llvm/llvm-project/pull/205046
>From 7ea5ca3e85b14eddd996c2a5aa1ebf22c4925dea Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Mon, 22 Jun 2026 12:43:58 +0530
Subject: [PATCH 1/3] [Flang][Driver]Add support for option
'-fpseudo-probe-for-profiling' in flang
---
clang/include/clang/Options/Options.td | 2 +-
clang/lib/Driver/ToolChains/Flang.cpp | 5 ++++
.../include/flang/Frontend/CodeGenOptions.def | 1 +
flang/lib/Frontend/CompilerInvocation.cpp | 4 ++++
flang/lib/Frontend/FrontendActions.cpp | 8 ++++++-
.../Driver/fpseudo-probe-for-profiling.f90 | 15 ++++++++++++
.../pseudo-probe-for-profiling.f90 | 24 +++++++++++++++++++
7 files changed, 57 insertions(+), 2 deletions(-)
create mode 100644 flang/test/Driver/fpseudo-probe-for-profiling.f90
create mode 100644 flang/test/Integration/pseudo-probe-for-profiling.f90
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 4fc9f4d4c3472..3c2091013d152 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1944,7 +1944,7 @@ defm pseudo_probe_for_profiling
CodeGenOpts<"PseudoProbeForProfiling">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption], "Emit">,
NegFlag<SetFalse, [], [ClangOption], "Do not emit">,
- BothFlags<[], [ClangOption, CC1Option, CLOption],
+ BothFlags<[], [ClangOption, CC1Option, CLOption, FlangOption, FC1Option],
" pseudo probes for sample profiling">>;
def fprofile_list_EQ : Joined<["-"], "fprofile-list=">,
Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 1d74a34583311..ea4df1db38ec8 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1022,6 +1022,11 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, const JobAction &JA,
A->render(Args, CmdArgs);
}
}
+
+ //-fpseudo-probe-for-profiling
+ if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling,
+ options::OPT_fno_pseudo_probe_for_profiling, false))
+ CmdArgs.push_back("-fpseudo-probe-for-profiling");
}
void Flang::ConstructJob(Compilation &C, const JobAction &JA,
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index 37931c0ffecc1..a5907b6edbd97 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -58,6 +58,7 @@ CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling
CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass
CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version
CODEGENOPT(DebugInfoForProfiling, 1, 0) ///< Emit extra debug info to make sample profile more accurate.
+CODEGENOPT(PseudoProbeForProfiling, 1, 0) ///< Emit pseudo probes for sample profiling.
CODEGENOPT(Underscoring, 1, 1)
ENUM_CODEGENOPT(FPMaxminBehavior, Fortran::common::FPMaxminBehavior, 2, Fortran::common::FPMaxminBehavior::Legacy)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index ab961416441fe..3144e13ae8c81 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -488,6 +488,10 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
opts.SampleProfileFile =
args.getLastArgValue(clang::options::OPT_fprofile_sample_use_EQ);
+ if (args.hasFlag(clang::options::OPT_fpseudo_probe_for_profiling,
+ clang::options::OPT_fno_pseudo_probe_for_profiling, false))
+ opts.PseudoProbeForProfiling = 1;
+
// -mcmodel option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::options::OPT_mcmodel_EQ)) {
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 426dea4869001..f1de4fd2b3fd2 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -995,7 +995,13 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
opts.SampleProfileFile, "", opts.ProfileRemappingFile,
opts.MemoryProfileUsePath, llvm::PGOOptions::SampleUse,
llvm::PGOOptions::NoCSAction, llvm::PGOOptions::ColdFuncOpt::Default,
- opts.DebugInfoForProfiling, /*PseudoProbeForProfiling=*/false);
+ opts.DebugInfoForProfiling, opts.PseudoProbeForProfiling);
+ } else if (opts.PseudoProbeForProfiling) {
+ // -fpseudo-probe-for-profiling
+ pgoOpt = llvm::PGOOptions(
+ "", "", "", /*MemoryProfile=*/"", llvm::PGOOptions::NoAction,
+ llvm::PGOOptions::NoCSAction, llvm::PGOOptions::ColdFuncOpt::Default,
+ opts.DebugInfoForProfiling, true);
}
llvm::StandardInstrumentations si(llvmModule->getContext(),
diff --git a/flang/test/Driver/fpseudo-probe-for-profiling.f90 b/flang/test/Driver/fpseudo-probe-for-profiling.f90
new file mode 100644
index 0000000000000..8d5fc72d314be
--- /dev/null
+++ b/flang/test/Driver/fpseudo-probe-for-profiling.f90
@@ -0,0 +1,15 @@
+! Test to check the option "-fpseudo-probe-for-profiling".
+
+! RUN: %flang -### %s 2>&1 | FileCheck %s --check-prefix=NO-PROBE
+! RUN: %flang -### -fpseudo-probe-for-profiling %s 2>&1 | FileCheck %s --check-prefix=PROBE
+! RUN: %flang -### -fno-pseudo-probe-for-profiling %s 2>&1 | FileCheck %s --check-prefix=NO-PROBE
+! RUN: %flang -### -fpseudo-probe-for-profiling -fno-pseudo-probe-for-profiling %s 2>&1 | FileCheck %s --check-prefix=NO-PROBE
+! RUN: %flang -### -fpseudo-probe-for-profiling -fno-pseudo-probe-for-profiling -fpseudo-probe-for-profiling %s 2>&1 | FileCheck %s --check-prefix=PROBE
+
+! PROBE: "-fpseudo-probe-for-profiling"
+! NO-PROBE-NOT: "-fpseudo-probe-for-profiling"
+
+subroutine test
+ implicit none
+ print *, 1
+end subroutine test
diff --git a/flang/test/Integration/pseudo-probe-for-profiling.f90 b/flang/test/Integration/pseudo-probe-for-profiling.f90
new file mode 100644
index 0000000000000..457715b352b04
--- /dev/null
+++ b/flang/test/Integration/pseudo-probe-for-profiling.f90
@@ -0,0 +1,24 @@
+! Test -fpseudo-probe-for-profiling option runs SampleProfileProbePass and emits llvm.pseudoprobe intrinsic calls.
+!
+! RUN: %flang_fc1 -emit-llvm -fdebug-pass-manager -fpseudo-probe-for-profiling -o /dev/null %s 2>&1 | FileCheck %s --check-prefix=PROBE-PASS
+! RUN: %flang_fc1 -emit-llvm -O0 -fpseudo-probe-for-profiling -o - %s | FileCheck %s --check-prefix=PROBE
+! RUN: %flang_fc1 -emit-llvm -O2 -fpseudo-probe-for-profiling -o - %s | FileCheck %s --check-prefix=PROBE
+
+! PROBE-PASS: Running pass: SampleProfileProbePass on {{.*}}
+
+! PROBE-LABEL: define void @foo
+! PROBE: call void @llvm.pseudoprobe(i64 [[#GUID:]], i64 1, i32 0, i64 -1)
+! PROBE: call void @llvm.pseudoprobe(i64 [[#GUID]], i64 2, i32 0, i64 -1)
+! PROBE: call void @llvm.pseudoprobe(i64 [[#GUID]], i64 4, i32 0, i64 -1)
+! PROBE: call void @llvm.pseudoprobe(i64 [[#GUID]], i64 6, i32 0, i64 -1)
+! PROBE: !llvm.pseudo_probe_desc = !{
+
+subroutine foo(x)
+ implicit none
+ integer, intent(in) :: x
+ if (x == 0) then
+ call bar
+ else
+ call go
+ end if
+end subroutine foo
>From 5025050b75d7a90c97a9e46db16ffe1514e558e2 Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Wed, 24 Jun 2026 15:12:59 +0530
Subject: [PATCH 2/3] [Flang][Driver]Addressed review comments
---
flang/lib/Frontend/CompilerInvocation.cpp | 3 ++-
flang/lib/Frontend/FrontendActions.cpp | 20 +++++++++++---------
2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 3144e13ae8c81..0f1ace5d62667 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -489,8 +489,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
args.getLastArgValue(clang::options::OPT_fprofile_sample_use_EQ);
if (args.hasFlag(clang::options::OPT_fpseudo_probe_for_profiling,
- clang::options::OPT_fno_pseudo_probe_for_profiling, false))
+ clang::options::OPT_fno_pseudo_probe_for_profiling, false)) {
opts.PseudoProbeForProfiling = 1;
+ }
// -mcmodel option.
if (const llvm::opt::Arg *a =
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index f1de4fd2b3fd2..7a030c7fa3c13 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -984,12 +984,6 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
opts.ProfileInstrumentUsePath, "", opts.ProfileRemappingFile,
opts.MemoryProfileUsePath, llvm::PGOOptions::IRUse, CSAction,
llvm::PGOOptions::ColdFuncOpt::Default, opts.DebugInfoForProfiling);
- } else if (opts.DebugInfoForProfiling) {
- // -fdebug-info-for-profiling
- pgoOpt = llvm::PGOOptions("", "", "", /*MemoryProfile=*/"",
- llvm::PGOOptions::NoAction,
- llvm::PGOOptions::NoCSAction,
- llvm::PGOOptions::ColdFuncOpt::Default, true);
} else if (!opts.SampleProfileFile.empty()) {
pgoOpt = llvm::PGOOptions(
opts.SampleProfileFile, "", opts.ProfileRemappingFile,
@@ -997,11 +991,19 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
llvm::PGOOptions::NoCSAction, llvm::PGOOptions::ColdFuncOpt::Default,
opts.DebugInfoForProfiling, opts.PseudoProbeForProfiling);
} else if (opts.PseudoProbeForProfiling) {
- // -fpseudo-probe-for-profiling
pgoOpt = llvm::PGOOptions(
- "", "", "", /*MemoryProfile=*/"", llvm::PGOOptions::NoAction,
+ /*ProfileFile=*/"", /*CSProfileGenFile=*/"",
+ /*ProfileRemappingFile=*/"",
+ /*MemoryProfile=*/"", llvm::PGOOptions::NoAction,
llvm::PGOOptions::NoCSAction, llvm::PGOOptions::ColdFuncOpt::Default,
- opts.DebugInfoForProfiling, true);
+ opts.DebugInfoForProfiling, /*PseudoProbeForProfiling=*/true);
+ } else if (opts.DebugInfoForProfiling) {
+ pgoOpt = llvm::PGOOptions(/*ProfileFile=*/"", /*CSProfileGenFile=*/"",
+ /*ProfileRemappingFile=*/"", /*MemoryProfile=*/"",
+ llvm::PGOOptions::NoAction,
+ llvm::PGOOptions::NoCSAction,
+ llvm::PGOOptions::ColdFuncOpt::Default,
+ /*PseudoProbeForProfiling=*/true);
}
llvm::StandardInstrumentations si(llvmModule->getContext(),
>From 412cdb14f5e7ec920029c1da87475b31cf0175ab Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Wed, 24 Jun 2026 16:33:27 +0530
Subject: [PATCH 3/3] [flang]Added regression test
---
flang/lib/Frontend/FrontendActions.cpp | 2 +-
flang/test/Integration/pseudo-probe-for-profiling.f90 | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 7a030c7fa3c13..5fe876595d5c0 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -1003,7 +1003,7 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
llvm::PGOOptions::NoAction,
llvm::PGOOptions::NoCSAction,
llvm::PGOOptions::ColdFuncOpt::Default,
- /*PseudoProbeForProfiling=*/true);
+ /*DebugInfoForProfiling=*/true);
}
llvm::StandardInstrumentations si(llvmModule->getContext(),
diff --git a/flang/test/Integration/pseudo-probe-for-profiling.f90 b/flang/test/Integration/pseudo-probe-for-profiling.f90
index 457715b352b04..a11cb4502dcf1 100644
--- a/flang/test/Integration/pseudo-probe-for-profiling.f90
+++ b/flang/test/Integration/pseudo-probe-for-profiling.f90
@@ -4,6 +4,10 @@
! RUN: %flang_fc1 -emit-llvm -O0 -fpseudo-probe-for-profiling -o - %s | FileCheck %s --check-prefix=PROBE
! RUN: %flang_fc1 -emit-llvm -O2 -fpseudo-probe-for-profiling -o - %s | FileCheck %s --check-prefix=PROBE
+! Test that -fdebug-info-for-profiling combined with -fpseudo-probe-for-profiling still emits pseudo-probes and debug info.
+! RUN: %flang_fc1 -emit-llvm -O2 -debug-info-kind=standalone \
+! RUN: -fdebug-info-for-profiling -fpseudo-probe-for-profiling -o - %s | FileCheck %s --check-prefix=PROBE-AND-DEBUG
+
! PROBE-PASS: Running pass: SampleProfileProbePass on {{.*}}
! PROBE-LABEL: define void @foo
@@ -13,6 +17,11 @@
! PROBE: call void @llvm.pseudoprobe(i64 [[#GUID]], i64 6, i32 0, i64 -1)
! PROBE: !llvm.pseudo_probe_desc = !{
+! PROBE-AND-DEBUG: call void @llvm.pseudoprobe
+! PROBE-AND-DEBUG: !llvm.pseudo_probe_desc = !{
+! PROBE-AND-DEBUG: !DICompileUnit({{.*}}debugInfoForProfiling: true{{.*}})
+! PROBE-AND-DEBUG: !DILexicalBlockFile({{.*}}discriminator:
+
subroutine foo(x)
implicit none
integer, intent(in) :: x
More information about the flang-commits
mailing list