Also, can anyone point me to a good tutorial on the subject? I can't find any.
-
a good tutorial on the subject of what? A g++ switch you don't know what to use for? – jalf Dec 06 '10 at 11:57
-
Thanks for the -1. I have tried playing with `-fprofile-arcs` sometime ago. And it created some files which I understood were profiles of branches taken and so on... But, I stumbled upon `-fprofile-use` and `-fprofile-generate` today, so I thought of asking this question. I'll edit my question. – nakiya Dec 06 '10 at 12:28
-
13Why is this getting downvoted and voted to close? It's a perfectly valid question. – nico Dec 06 '10 at 12:30
-
Maybe they are used to enable profile-guided optimizations? – Daniel Lidström Dec 06 '10 at 12:37
-
9@jalf it's a perfectly valid question on how to use profile-guided optimizations – Laurynas Biveinis Dec 06 '10 at 13:31
-
@Laurynas: No, a perfectly valid question on that would be "how do I use profile-guided optimizations with G++". This question seemingly picks a random compiler switch (from `g++ --help`?) and then asks "what is it for". Is the OP asking "how do I use profile-guided optimization", or "what is profiling", or "I saw -fprofile-generate used in a makefile, and I have no clue what it means"? From the question asked here, I have no clue. – jalf Dec 06 '10 at 13:40
-
1@jalf: I'll change "What is the use of ..." to "How to use ...". Would that satisfy you. Though I know that most people understand what I ask in this question. – nakiya Dec 06 '10 at 14:08
-
1@nakiya: if you want to know how to do profile-guided optimization in G++, then ask that. When you ask like this, I have to wonder if it is the specific switches you're interested in, or the general goal of "using profile-guided optimization to speed up my application". – jalf Dec 06 '10 at 14:21
-
1@jalf: "profile-guided optimization :" This is the first time I ever heard this phrase even. I edited the question twice now. I'll edit again, in the hope of getting an answer :( – nakiya Dec 06 '10 at 17:04
3 Answers
-fprofile-generate will instrument the application with profiling code. The application will, while actually running, log certain events that could improve performance if this usage pattern was known at compile time. Branches, possibility for inlining, etc, can all be logged, but I'm not sure in detail how GCC implements this.
After the program exits, it will dump all this data into *.gcda files, which are essentially log data for a test run. After rebuilding the application with -fprofile-use flag, GCC will take the *.gcda log data into account when doing its optimizations, usually increasing the performance significantly. Of course, this depends on many factors.

- 4,978
- 1
- 31
- 34
-
9By the way, if your program is multi-threaded, then the generated profile data will probably be inconsistent and you will likely get errors when you try to use it. So, you probably need to also pass `-fprofile-correction` for the second invocation of GCC. – Nicu Stiurca Apr 30 '15 at 20:55
-
I can't get rid of the error. The command I'm using is `gcc hello_world.c -o demo_fdo -fprofile-use -freorder-blocks-and-partition -O3 -fprofile-correction -Wcoverage-mismatch` still getting the coverage-mismatch error. What am I doing wrong? – gbriones.gdl Jun 19 '15 at 16:30
From this example:
g++ -O3 -fprofile-generate [more params here, like -march=native ...] -o executable_name
// run my program's benchmarks, or something to stress its most common path
g++ -O3 -fprofile-use [more params here, like -march=native...] -o executable_name
Basically, you initially compile and link with this extra flag for both compiling and linking: -fprofile-generate
(from here).
Then, when you run it, by default it will create .gcda files "next" to your .o files, it seems (hard coded to the full path where they were built).
You can optionally change where it creates these .gcda files with the -fprofile-dir=XXX setting.
Then you re compile and relink using the -fprofile-use
parameter, and it compiles it using profile guided goodness.

- 62,887
- 36
- 269
- 388
-
4By the way, if your program is multi-threaded, then the generated profile data will probably be inconsistent and you will likely get errors when you try to use it. So, you probably need to also pass `-fprofile-correction` for the second invocation of g++. – Nicu Stiurca Apr 30 '15 at 20:54
-
1I can't get rid of the error. The command I'm using is `gcc hello_world.c -o demo_fdo -fprofile-use -freorder-blocks-and-partition -O3 -fprofile-correction -Wcoverage-mismatch` still getting the coverage-mismatch error. What am I doing wrong? – gbriones.gdl Jun 19 '15 at 16:27
The tricky bit is setting up the makefiles.
You definitely need separate output directories for object files. I would recommend naming them "profile" and "release". You might have to copy the *.gcda files that result from the profile run so that GCC finds them in the release build step.
The result will almost certainly be faster. It will probably be larger as well. The -fprofile-use option enables many other optimization steps that are otherwise only enabled by -O3.

- 53,022
- 10
- 79
- 131
-
Can you comment which optimization steps are those? Or cite some document? – JohnTortugo Dec 14 '12 at 15:04
-
2@JohnTortugo: `info gcc` and search for profile-use. It says: "The following options are enabled: -fbranch-probabilities', '-fvpt', '-funroll-loops', `'-fpeel-loops', '-ftracer'" – Zan Lynx Dec 14 '12 at 22:22