[PATCH] kbuild: Add support for Clang's polyhedral loop optimizer.

From: Diab Neiroukh
Date: Wed Jan 20 2021 - 12:54:03 EST

Polly is able to optimize various loops throughout the kernel for cache
locality. A mathematical representation of the program, based on
polyhedra, is analysed to find opportunistic optimisations in memory
access patterns which then leads to loop transformations.

Polly is not built with LLVM by default, and requires LLVM to be compiled
with the Polly "project". This can be done by adding Polly to


Preliminary benchmarking seems to show an improvement of around two
percent across perf benchmarks:

Benchmark | Control | Polly
bonnie++ -x 2 -s 4096 -r 0 | 12.610s | 12.547s
perf bench futex requeue | 33.553s | 33.094s
perf bench futex wake | 1.032s | 1.021s
perf bench futex wake-parallel | 1.049s | 1.025s
perf bench futex requeue | 1.037s | 1.020s

Furthermore, Polly does not produce a much larger image size netting it
to be a "free" optimisation. A comparison of a bzImage for a kernel with
and without Polly is shown below:

bzImage | stat --printf="%s\n"
Control | 9333728
Polly | 9345792

Compile times were one percent different at best, which is well within
the range of noise. Therefore, I can say with certainty that Polly has
a minimal effect on compile times, if none.

Suggested-by: Danny Lin <danny@xxxxxxxxxxx>
Signed-off-by: Diab Neiroukh <lazerl0rd@xxxxxxxxxxx>
Makefile | 16 ++++++++++++++++
init/Kconfig | 13 +++++++++++++
2 files changed, 29 insertions(+)

diff --git a/Makefile b/Makefile
index b9d3a47c57cf..00f15bde5f8b 100644
--- a/Makefile
+++ b/Makefile
@@ -740,6 +740,22 @@ else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE

+KBUILD_CFLAGS += -mllvm -polly \
+ -mllvm -polly-ast-use-context \
+ -mllvm -polly-invariant-load-hoisting \
+ -mllvm -polly-opt-fusion=max \
+ -mllvm -polly-run-inliner \
+ -mllvm -polly-vectorizer=stripmine
+# Polly may optimise loops with dead paths beyound what the linker
+# can understand. This may negate the effect of the linker's DCE
+# so we tell Polly to perfom proven DCE on the loops it optimises
+# in order to preserve the overall effect of the linker's DCE.
+KBUILD_CFLAGS += -mllvm -polly-run-dce
# Tell gcc to never replace conditional load with a non-conditional one
KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0)
KBUILD_CFLAGS += $(call cc-option,-fno-allow-store-data-races)
diff --git a/init/Kconfig b/init/Kconfig
index 05131b3ad0f2..266d7d03ccd1 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -177,6 +177,19 @@ config BUILD_SALT
This is mostly useful for distributions which want to ensure the
build is unique between builds. It's safe to leave the default.

+ bool "Use Clang Polly optimizations"
+ depends on CC_IS_CLANG && $(cc-option,-mllvm -polly)
+ depends on !COMPILE_TEST
+ help
+ This option enables Clang's polyhedral loop optimizer known as
+ Polly. Polly is able to optimize various loops throughout the
+ kernel for cache locality. This requires a Clang toolchain
+ compiled with support for Polly. More information can be found
+ from Polly's website:
+ https://polly.llvm.org