diff --git a/llvm/utils/gn/build/BUILD.gn b/llvm/utils/gn/build/BUILD.gn index ee8a83c946a3e8..522f54f734da54 100644 --- a/llvm/utils/gn/build/BUILD.gn +++ b/llvm/utils/gn/build/BUILD.gn @@ -5,7 +5,7 @@ import("//llvm/utils/gn/build/toolchain/compiler.gni") import("//llvm/utils/gn/build/toolchain/target_flags.gni") declare_args() { - # Whether to build everything with coverage information. + # Whether to build everything with test coverage information. # After building with this, run tests and then run # llvm/utils/prepare-code-coverage-artifact.py \ # --compilation-dir=out/gn \ @@ -14,6 +14,20 @@ declare_args() { # to generate a HTML report for the binaries passed in the last line. llvm_build_instrumented_coverage = false + # Whether to build everything with instrumentation for PGO + # After building with this: + # 1. Remove old profile data with `rm *.profraw` + # 2. Run the built instrumented binaries. + # This will produce *.profraw files in the current working directory. + # 3. Run `llvm-profdata merge *.profraw -o llvm.profdata` to merge them. + # 4. Then build again, with this set to false, and with + # `llvm_pgo_use = "//llvm.profdata"` set to use the created profile. + llvm_pgo_instrument = false + + # If non-empty, path to merged profiling data used for optimization + # See documentation for llvm_pgo_instrument for how to create profile data. + llvm_pgo_use = "" + # If set, puts relative paths in debug info. # Makes the build output independent of the build directory, but makes # most debuggers harder to use. See "Getting to local determinism" and @@ -28,6 +42,12 @@ declare_args() { assert(!llvm_build_instrumented_coverage || is_clang, "llvm_build_instrumented_coverage requires clang as host compiler") +assert(!llvm_pgo_instrument || is_clang, + "llvm_pgo_instrument requires clang as host compiler") +assert(llvm_pgo_use == "" || is_clang, + "llvm_pgo_use requires clang as host compiler") +assert(!llvm_pgo_instrument || llvm_pgo_use == "", + "set at most one of llvm_pgo_instrument and llvm_pgo_use") config("compiler_defaults") { defines = [] @@ -272,6 +292,23 @@ config("compiler_defaults") { ldflags += [ "-fprofile-instr-generate" ] } } + if (llvm_pgo_instrument) { + cflags += [ "-fprofile-generate" ] + if (current_os != "win") { + ldflags += [ "-fprofile-generate" ] + } + } + if (llvm_pgo_use != "") { + cflags += [ + "-fprofile-use=" + rebase_path(llvm_pgo_use, root_build_dir), + + # There are always quite a few diags like + # warning: foo.cpp: Function control flow change detected + # (hash mismatch) [-Wbackend-plugin] + # in a PGO build. Since they're not unexpected, silence them. + "-Wno-backend-plugin", + ] + } # Deterministic build setup, see # http://blog.llvm.org/2019/11/deterministic-builds-with-clang-and-lld.html