[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
Mon Jun 22 00:41:03 PDT 2026


https://github.com/kaviya2510 updated https://github.com/llvm/llvm-project/pull/205046

>From b25b6bc237e90b3851649c25ec9a9cd02d10fc38 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] [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 e4a9d95ece0ab..6d7ee5c34b0ab 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1942,7 +1942,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 5f536b5d96f90..adcf9dc2eeb8c 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



More information about the flang-commits mailing list