[llvm] [clang] [CMake][PGO] Use check targets to generate profdata for PGO builds (PR #77347)

2024-01-11 Thread Tom Stellard via cfe-commits

https://github.com/tstellar updated 
https://github.com/llvm/llvm-project/pull/77347

>From 127e2ae83f33843cfb9c5cca314afa2fc9844239 Mon Sep 17 00:00:00 2001
From: Tom Stellard 
Date: Sat, 6 Jan 2024 07:46:01 +
Subject: [PATCH 1/4] [CMake][PGO] Use check-clang target to generate profdata
 for PGO builds

When doing a multi-stage PGO build of clang, run the check-clang and
check-llvm targets using the instrumented clang and use that profile
data for building the final stage2 clang.  This is what is recommended
by our official documentation: 
https://llvm.org/docs/HowToBuildWithPGO.html#building-clang-with-pgo

I benchmarked this change by compiling the SemaChecking.cpp file from
clang.  Using check-clang/check-llvm to generate the profile data gives a 25% 
speedup
in the PGO+LTO stage2 clang when compared to the stage1 clang (no-LTO).

Prior to this change, I was only seeing ~5% speedup when comparing the
stage2 and stage1 builds.
---
 clang/utils/perf-training/CMakeLists.txt |  6 +++---
 clang/utils/perf-training/perf-helper.py | 16 +---
 2 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/clang/utils/perf-training/CMakeLists.txt 
b/clang/utils/perf-training/CMakeLists.txt
index c6d51863fb1b5c..95ff8115aa538b 100644
--- a/clang/utils/perf-training/CMakeLists.txt
+++ b/clang/utils/perf-training/CMakeLists.txt
@@ -15,7 +15,7 @@ if(LLVM_BUILD_INSTRUMENTED)
 )
 
   add_custom_target(clear-profraw
-COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py 
clean ${CMAKE_CURRENT_BINARY_DIR} profraw
+COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py 
clean ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}/profiles/ profraw
 COMMENT "Clearing old profraw data")
 
   if(NOT LLVM_PROFDATA)
@@ -26,9 +26,9 @@ if(LLVM_BUILD_INSTRUMENTED)
 message(STATUS "To enable merging PGO data LLVM_PROFDATA has to point to 
llvm-profdata")
   else()
 add_custom_target(generate-profdata
-  COMMAND "${Python3_EXECUTABLE}" 
${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} 
${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR}
+  COMMAND "${Python3_EXECUTABLE}" 
${CMAKE_CURRENT_SOURCE_DIR}/perf-helper.py merge ${LLVM_PROFDATA} 
${CMAKE_CURRENT_BINARY_DIR}/clang.profdata ${CMAKE_CURRENT_BINARY_DIR} 
${CMAKE_BINARY_DIR}/profiles/
   COMMENT "Merging profdata"
-  DEPENDS generate-profraw)
+  DEPENDS generate-profraw check-clang check-llvm)
   endif()
 endif()
 
diff --git a/clang/utils/perf-training/perf-helper.py 
b/clang/utils/perf-training/perf-helper.py
index 99d6ab6ef0..bd8f74c9c2e129 100644
--- a/clang/utils/perf-training/perf-helper.py
+++ b/clang/utils/perf-training/perf-helper.py
@@ -30,26 +30,28 @@ def findFilesWithExtension(path, extension):
 
 
 def clean(args):
-if len(args) != 2:
+if len(args) < 2:
 print(
-"Usage: %s clean  \n" % __file__
+"Usage: %s clean  \n" % __file__
 + "\tRemoves all files with extension from ."
 )
 return 1
-for filename in findFilesWithExtension(args[0], args[1]):
-os.remove(filename)
+for path in args[1:-1]:
+   for filename in findFilesWithExtension(path, args[-1]):
+   os.remove(filename)
 return 0
 
 
 def merge(args):
-if len(args) != 3:
+if len(args) < 3:
 print(
-"Usage: %s merge   \n" % __file__
+"Usage: %s merge   \n" % __file__
 + "\tMerges all profraw files from path into output."
 )
 return 1
 cmd = [args[0], "merge", "-o", args[1]]
-cmd.extend(findFilesWithExtension(args[2], "profraw"))
+for i in range(2, len(args)):
+  cmd.extend(findFilesWithExtension(args[i], "profraw"))
 subprocess.check_call(cmd)
 return 0
 

>From 4f7734584af3aa9a18bde17349ceccbef3658c53 Mon Sep 17 00:00:00 2001
From: Tom Stellard 
Date: Mon, 8 Jan 2024 18:07:31 +
Subject: [PATCH 2/4] Fix python formatting

---
 clang/utils/perf-training/perf-helper.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/utils/perf-training/perf-helper.py 
b/clang/utils/perf-training/perf-helper.py
index bd8f74c9c2e129..844aa274f049aa 100644
--- a/clang/utils/perf-training/perf-helper.py
+++ b/clang/utils/perf-training/perf-helper.py
@@ -37,8 +37,8 @@ def clean(args):
 )
 return 1
 for path in args[1:-1]:
-   for filename in findFilesWithExtension(path, args[-1]):
-   os.remove(filename)
+for filename in findFilesWithExtension(path, args[-1]):
+os.remove(filename)
 return 0
 
 
@@ -51,7 +51,7 @@ def merge(args):
 return 1
 cmd = [args[0], "merge", "-o", args[1]]
 for i in range(2, len(args)):
-  cmd.extend(findFilesWithExtension(args[i], "profraw"))
+cmd.extend(findFilesWithExtension(args[i], "profraw"))
 subprocess.check_call(cmd)
 return 0
 

>From 

[clang] [CMake][PGO] Use check targets to generate profdata for PGO builds (PR #77347)

2024-01-08 Thread Tom Stellard via cfe-commits

tstellar wrote:

My goal right now is to make this simple and easy to use.  I'm not opposed to 
making something configurable, but it would be nice to have a default that 
actually helped improve performance, so that a new user or a distribution 
maintainer could just use the cache file as is.

What about if we tried to improve the existing in tree corpus (which only has 
hello-world).  @petrhosek Is there anything in your corpus that you can 
contribute upstream?  Any suggestions for what to put in there?  Maybe a few of 
the bigger c++ files from in the tree?

https://github.com/llvm/llvm-project/pull/77347
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMake][PGO] Use check targets to generate profdata for PGO builds (PR #77347)

2024-01-08 Thread Petr Hosek via cfe-commits

petrhosek wrote:

I'd also prefer to make this configurable, we're using our own corpus which in 
my experiments both produces better results and takes less time than 
`check-llvm` and `check-clang`.

We should also consider updating the documentation since I don't think that 
`check-llvm` and `check-clang` is what we should be recommending; LLVM and 
Clang tests have different goal, that is to provide maximum coverage which 
often means exercising various corner cases and error conditions, but these are 
not helpful when collecting profiles, in fact they can be harmful.

I think that [LLVM test-suite](https://github.com/llvm/llvm-test-suite/) is 
actually a better fit since more representative of real code. In our  training 
corpus, I for example use 
[CppPerformanceBenchmarks](https://gitlab.com/chriscox/CppPerformanceBenchmarks)
 (an older version is also [included in LLVM test 
suite](https://github.com/llvm/llvm-test-suite/tree/9ca97f5027150f7e507e5ab4c56f38a29fb3c696/SingleSource/Benchmarks/Adobe-C%2B%2B)).

https://github.com/llvm/llvm-project/pull/77347
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMake][PGO] Use check targets to generate profdata for PGO builds (PR #77347)

2024-01-08 Thread Chris B via cfe-commits

llvm-beanz wrote:

Looping in some Apple people. I'm unsure if Apple is still using this 
infrastructure for generating PGO data. If so this change will impact them. 
When I wrote this, Apple had some additional test cases that got layered on top 
of the publicly available tests.

It might make sense to make this configurable by having a user-definable 
(advanced) variable that specifies which targets to run to generate PGO data.

cc: @ributzka, @Bigcheese

https://github.com/llvm/llvm-project/pull/77347
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CMake][PGO] Use check targets to generate profdata for PGO builds (PR #77347)

2024-01-08 Thread Tom Stellard via cfe-commits

https://github.com/tstellar edited 
https://github.com/llvm/llvm-project/pull/77347
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits