https://github.com/andreasmullen updated 
https://github.com/llvm/llvm-project/pull/239616

>From fd4c1ce021fc2a1be923f8a8d680eff2dfeb3cec Mon Sep 17 00:00:00 2001
From: Andreas Mullen <[email protected]>
Date: Fri, 6 Nov 2026 14:06:32 +0000
Subject: [PATCH 1/2] [Clang][Docs] Improve -fcs-profile-generate documentation

---
 clang/docs/UsersManual.md | 97 ++++++++++++++++++++++++++++-----------
 1 file changed, 71 insertions(+), 26 deletions(-)

diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 96d1c4e009c71a..73ed42d54904e0 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -3515,9 +3515,14 @@ better runtime performance. Frontend-based 
instrumentation, on the other hand,
 has better source correlation, so it should be used with source line-based
 coverage testing.
 
-The flag `-fcs-profile-generate` also instruments programs using the same
-instrumentation method as `-fprofile-generate`. However, it performs a
-post-inline late instrumentation and can produce context-sensitive profiles.
+To consider context-sensitive information in profile data, an additional
+stage when compiling is required. The first stage collects a base profile
+using `-fprofile-generate`. Context-sensitive profile information can then
+be collected using the `-fcs-profile-generate` option and the base profile
+with the `-fprofile-use` option. These separate profiles can be merged to
+produce one profile with information used before inlining (from the base 
profile)
+and context-sensitive information, which can then be used when compiling the
+final executable using the `-fprofile-use` option.
 
 Here are the steps for using profile guided optimization with
 instrumentation:
@@ -3646,41 +3651,81 @@ To override the path and filename at compile time, use
 :::{option} -fcs-profile-generate[=<dirname>]
 
 The `-fcs-profile-generate` and `-fcs-profile-generate=` flags will use
-the same instrumentation method, and generate the same profile as in the
-`-fprofile-generate` and `-fprofile-generate=` flags. The difference is
-that the instrumentation is performed after inlining so that the resulted
-profile has a better context sensitive information. They cannot be used
-together with `-fprofile-generate` and `-fprofile-generate=` flags.
-They are typically used in conjunction with `-fprofile-use` flag.
-The profile generated by `-fcs-profile-generate` and `-fprofile-generate`
-can be merged by llvm-profdata. A use example:
+the same IR instrumentation method as the `-fprofile-generate` and
+`-fprofile-generate=` flags, but insert instrumentation after inlining. This
+collects the context-sensitive information in the profile. They cannot be used
+together with the `-fprofile-generate` and `-fprofile-generate=` flags in the
+same compiler invocation. They are typically used in conjunction with
+the `-fprofile-use` flag. The profiles generated by `-fcs-profile-generate`
+and `-fprofile-generate` must be merged by llvm-profdata to produce
+the final context-sensitive PGO optimized executable. A worked example
+of producing this is described below:
+
+The first step is to compile an executable with IR instrumentation. Using
+the `-fprofile-generate` option will store the profile data collected from
+running the executable produced by this command.
 
 ```console
-$ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
-$ ./code
-$ llvm-profdata merge -output=code.profdata yyy/zzz/
+# General form: clang++ -O2 -fprofile-generate=<pathToBaseProfile> <source> -o 
<executable>
+$ clang++ -O2 -fprofile-generate=profiles/base main.cpp -o main-base
+```
+
+We can then run the IR instrumented executable to generate the profile
+data to be used when adding context-sensitivity.
+
+```console
+$ ./main-base
 ```
 
-The first few steps are the same as that in `-fprofile-generate`
-compilation. Then perform a second round of instrumentation.
+This will then create the raw profile data in the specified directory,
+`profiles/base/` in the example above. To ensure that Clang can read this
+profile, it must be converted using `llvm-profdata`.
 
 ```console
-$ clang++ -O2 -fprofile-use=code.profdata -fcs-profile-generate=sss/ttt \
-  -o cs_code
-$ ./cs_code
-$ llvm-profdata merge -output=cs_code.profdata sss/ttt code.profdata
+$ llvm-profdata merge profiles/base/ -o base.profdata
 ```
 
-The resulted `cs_code.prodata` combines `code.profdata` and the profile
-generated from binary `cs_code`. Profile `cs_code.profata` can be used by
-`-fprofile-use` compilation.
+The converted profile, `base.profdata` in the command above, will be
+outputted into the current directory. At this stage, the next step is
+to produce an instrumented build of the source code with context-sensitive
+instrumentation. This can be done using the `-fcs-profile-generate` option.
+
+We must still use the base profile collected using the IR instrumentation
+because it is used to guide inlining decisions, which can then be used by the
+context-sensitive instrumentation to generate a context-sensitive profile.
 
 ```console
-$ clang++ -O2 -fprofile-use=cs_code.profdata
+# General form: clang++ -O2 -fprofile-use=<baseProfile> 
-fcs-profile-generate=<pathToCSProfile> <source> -o <executable>
+$ clang++ -O2 -fprofile-use=base.profdata -fcs-profile-generate=profiles/cs/ 
main.cpp -o main-cs
 ```
 
-The above command will read both profiles to the compiler at the identical
-point of instrumentations.
+Running this executable allows us to collect the context-sensitive
+profile information. In this example, the profile will be written
+to `profiles/cs/`.
+
+```console
+$ ./main-cs
+```
+
+To be able to produce the final CSPGO-optimized executable,
+both the base profile and the context-sensitive profiles must be
+merged into one profile. This is because they provide information
+for different stages of the compilation pipeline. It should
+be noted that the combined profile will be written into the
+current directory.
+
+```console
+$ llvm-profdata merge base.profdata profiles/cs/ -o combined.profdata
+```
+
+Using this new profile, we can compile the final optimized executable
+with CSPGO optimization using the `-fprofile-use` option. The command
+below will ensure that the compilation uses the base profile before
+inlining and the context-sensitive profile after inlining.
+
+```console
+$ clang++ -O2 -fprofile-use=combined.profdata main.cpp -o main-final
+```
 :::
 
 :::{option} -fprofile-use[=<pathname>]

>From be4b4635b4517498c38f57a4e9de9b0095698e39 Mon Sep 17 00:00:00 2001
From: Andreas Mullen <[email protected]>
Date: Tue, 10 Nov 2026 11:24:09 +0000
Subject: [PATCH 2/2] [Clang][Docs] Clarify optional first stage and simplify
 -fcs-profile-generate workflow

---
 clang/docs/UsersManual.md | 54 ++++++++++++++-------------------------
 1 file changed, 19 insertions(+), 35 deletions(-)

diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 73ed42d54904e0..40be0e6537af17 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -3515,14 +3515,11 @@ better runtime performance. Frontend-based 
instrumentation, on the other hand,
 has better source correlation, so it should be used with source line-based
 coverage testing.
 
-To consider context-sensitive information in profile data, an additional
-stage when compiling is required. The first stage collects a base profile
-using `-fprofile-generate`. Context-sensitive profile information can then
-be collected using the `-fcs-profile-generate` option and the base profile
-with the `-fprofile-use` option. These separate profiles can be merged to
-produce one profile with information used before inlining (from the base 
profile)
-and context-sensitive information, which can then be used when compiling the
-final executable using the `-fprofile-use` option.
+After inlining, the `-fcs-profile-generate` option inserts additional
+instrumentation to collect context-sensitive profile information. It can be
+used without an existing profile that has been collected using 
`-fprofile-generate`.
+However, it is common to merge the profiles generated from each option. A 
worked
+example is given when discussing `-fcs-profile-generate` further.
 
 Here are the steps for using profile guided optimization with
 instrumentation:
@@ -3657,40 +3654,28 @@ collects the context-sensitive information in the 
profile. They cannot be used
 together with the `-fprofile-generate` and `-fprofile-generate=` flags in the
 same compiler invocation. They are typically used in conjunction with
 the `-fprofile-use` flag. The profiles generated by `-fcs-profile-generate`
-and `-fprofile-generate` must be merged by llvm-profdata to produce
-the final context-sensitive PGO optimized executable. A worked example
-of producing this is described below:
+and `-fprofile-generate` can be merged by llvm-profdata to produce
+a context-sensitive PGO optimized executable. A worked example
+of this two-stage profiling workflow is described below:
 
-The first step is to compile an executable with IR instrumentation. Using
-the `-fprofile-generate` option will store the profile data collected from
-running the executable produced by this command.
+The first stage is to compile and run an executable with IR instrumentation to
+produce a base profile using the `-fprofile-generate` option, which is stored
+in `profiles/base/` in the example below. `llvm-profdata` is used to convert
+the raw profile data into a readable format for Clang.
 
 ```console
 # General form: clang++ -O2 -fprofile-generate=<pathToBaseProfile> <source> -o 
<executable>
 $ clang++ -O2 -fprofile-generate=profiles/base main.cpp -o main-base
-```
-
-We can then run the IR instrumented executable to generate the profile
-data to be used when adding context-sensitivity.
-
-```console
 $ ./main-base
-```
-
-This will then create the raw profile data in the specified directory,
-`profiles/base/` in the example above. To ensure that Clang can read this
-profile, it must be converted using `llvm-profdata`.
-
-```console
 $ llvm-profdata merge profiles/base/ -o base.profdata
 ```
 
-The converted profile, `base.profdata` in the command above, will be
+The converted profile, `base.profdata`, above, will be
 outputted into the current directory. At this stage, the next step is
 to produce an instrumented build of the source code with context-sensitive
 instrumentation. This can be done using the `-fcs-profile-generate` option.
 
-We must still use the base profile collected using the IR instrumentation
+We still use the base profile collected using the IR instrumentation
 because it is used to guide inlining decisions, which can then be used by the
 context-sensitive instrumentation to generate a context-sensitive profile.
 
@@ -3707,12 +3692,11 @@ to `profiles/cs/`.
 $ ./main-cs
 ```
 
-To be able to produce the final CSPGO-optimized executable,
-both the base profile and the context-sensitive profiles must be
-merged into one profile. This is because they provide information
-for different stages of the compilation pipeline. It should
-be noted that the combined profile will be written into the
-current directory.
+When using profiles collected using both the `-fprofile-generate` and
+`-fcs-profile-generate` options, they must be merged into one profile.
+This is because they provide information for different stages of the
+compilation pipeline. It should be noted that the combined profile
+will be written into the current directory.
 
 ```console
 $ llvm-profdata merge base.profdata profiles/cs/ -o combined.profdata

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

Reply via email to