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

Added support for option `-fpseudo-probe-for-profiling` in flang.

- When the option `-fpseudo-probe-for-profiling` is passed, the compiler sets 
the` PseudoProbeForProfiling` flag and triggers the `SampleProfileProbePass`. 
This pass inserts `llvm.pseudoprobe(..)` intrinsic calls and 
`!llvm.pseudo_probe_desc` metadata into the IR.

>From dd068cf738bab70f518bc4921764e4cce87c3689 Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <[email protected]>
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 ecd66325bf8da..31ece28600584 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 aed572c32ccd6..41ef23d2b28a3 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1017,6 +1017,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 d8bbb94bd8cde..67b4d6fff802e 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -57,6 +57,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 450bfb59b6e8f..9d8b0b1d19644 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 66602ed52f6cd..8386bbafa8209 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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to