[PATCH] D49492: Run bounds checking sanitizer earlier to make it easier to optimize away its checks.

Joel Galenson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 18 09:13:12 PDT 2018


jgalenson created this revision.
jgalenson added reviewers: eugenis, chandlerc.
Herald added a subscriber: cfe-commits.

Running the bounds checking sanitizer earlier makes it easier for other optimizations to remove the checks it inserts. 
 While it could also inhibit other optimizations, I ran a few benchmarks and this did seem to improve performance and code size slightly.

Note that I'm not sure how to hook this up to the new PM, as I couldn't find a similar hook into the beginning of the pipeline.  Are there any suggestions on what to do about that, or is it fine for this to work just on the old PM?


Repository:
  rC Clang

https://reviews.llvm.org/D49492

Files:
  lib/CodeGen/BackendUtil.cpp
  test/CodeGen/bounds-checking-opt.c


Index: test/CodeGen/bounds-checking-opt.c
===================================================================
--- /dev/null
+++ test/CodeGen/bounds-checking-opt.c
@@ -0,0 +1,20 @@
+// RUN: %clang -S -fsanitize=local-bounds -emit-llvm -target x86_64-- %s -o - | FileCheck -check-prefix=O0 %s
+// RUN: %clang -S -fsanitize=local-bounds -emit-llvm -target x86_64-- -O2 %s -o - | FileCheck -check-prefix=O2 %s
+// RUN: %clang -S -fsanitize=local-bounds -emit-llvm -target aarch64-- %s -o - | FileCheck -check-prefix=O0 %s
+// RUN: %clang -S -fsanitize=local-bounds -emit-llvm -target aarch64-- -O2 %s -o - | FileCheck -check-prefix=O2 %s
+
+extern void fill(int *arr);
+
+// CHECK-LABEL: @f
+int f(int x) {
+  // O0: call {{.*}} @llvm.trap
+  // O2-NOT: call {{.*}} @llvm.trap
+  int foo[1000];
+  fill(foo);
+  int sum = 0;
+  #pragma clang loop vectorize(disable)
+  #pragma clang loop unroll(disable)
+  for (unsigned i = 0; i < 1000; i++)
+    sum += foo[i];
+  return sum;
+}
Index: lib/CodeGen/BackendUtil.cpp
===================================================================
--- lib/CodeGen/BackendUtil.cpp
+++ lib/CodeGen/BackendUtil.cpp
@@ -562,7 +562,7 @@
     addCoroutinePassesToExtensionPoints(PMBuilder);
 
   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
-    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
+    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
                            addBoundsCheckingPass);
     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
                            addBoundsCheckingPass);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49492.156092.patch
Type: text/x-patch
Size: 1594 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180718/ab8aff81/attachment.bin>


More information about the cfe-commits mailing list