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); 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; +}