diff --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn --- a/llvm/utils/gn/build/BUILD.gn +++ b/llvm/utils/gn/build/BUILD.gn @@ -373,6 +373,42 @@ } } + if (pgo_phase != 0) { + assert(is_clang && current_os == "linux", + "PGO only supported on Linux/Clang") + if (pgo_phase == 1) { + cflags += [ "-fprofile-generate" ] + ldflags += [ "-fprofile-generate" ] + } else { + assert(pgo_phase == 2, "pgo_phase should be 0, 1, or 2") + assert(pgo_profile_path != "", "Need pgo_profile_path for pgo_phase = 2") + cflags += [ "-fprofile-instr-use=" + pgo_profile_path ] + } + } + + if (propeller_phase != 0) { + assert(is_clang && current_os == "linux", + "Propeller only supported on Linux/Clang") + if (propeller_phase == 1) { + cflags += [ + "-funique-internal-linkage-names", + "-fbasic-block-sections=labels", + ] + } else { + assert(propeller_phase == 2, "propeller_phase should be 0, 1, or 2") + assert(propeller_profile_dir != "", + "Need propeller_profile_dir for propeller_phase = 2") + cflags += [ + "-funique-internal-linkage-names", + "-fbasic-block-sections=list=" + propeller_profile_dir + "/cluster.txt", + ] + ldflags += [ + "-Wl,--symbol-ordering-file=" + propeller_profile_dir + "/symorder.txt", + "-Wl,--no-warn-symbol-ordering", + ] + } + } + cflags_objcc = cflags_cc } diff --git a/llvm/utils/gn/build/buildflags.gni b/llvm/utils/gn/build/buildflags.gni --- a/llvm/utils/gn/build/buildflags.gni +++ b/llvm/utils/gn/build/buildflags.gni @@ -22,6 +22,36 @@ # Max jobs per ThinLTO link. max_jobs_per_lto_link = 8 + + # The current PGO phase. + # 0: No PGO. + # 1: Build binaries with PGO instrumentation. + # 2: Optimize binaries with PGO profile provided via pgo_profile_dir. + # + # To build with PGO: + # 1) Build with `pgo_phase = 1` + # 2) Run binary with representative workload, creating *.profraw files + # 3) Run `llvm-profdata merge *.profraw -o llvm.profdata` + # 4) Build with `pgo_phase = 2` and `pgo_profile_path = "path/to/llvm.profdata"` + pgo_phase = 0 + + # Path to PGO profile. Only used when pgo_phase = 2. + pgo_profile_path = "" + + # The current Propeller phase. + # 0: No Propeller. + # 1: Build binaries suitable for propeller profile generation. + # 2: Optimize binaries with Propeller profiles provided in propeller_profile_dir. + # + # To build with Propeller: + # 1) Build with `propeller_phase = 1` + # 2) Run binary with representative workload under `perf record -e cycles:u -j any,u`, creating perf.data + # 3) Run `create_llvm_prof --format=propeller --binary=path/to/binary --profile=perf.data --out=cluster.txt --propeller_symorder=symorder.txt` (https://github.com/google/autofdo) + # 4) Build with `propeller_phase = 2` and `propeller_profile_dir = path/to/dirwithtxts` + propeller_phase = 0 + + # Path to propeller profile directory containing cluster.txt and symorder.txt. Only used when propeller_phase = 2. + propeller_profile_dir = "" } # args that depend on other args must live in a later declare_args() block.