Skip to content

Conversation

@AbhayKanhere
Copy link

@AbhayKanhere AbhayKanhere commented Jan 5, 2026

cherry-pick trap checks out of the loop patch from upstream.

rdar://162704645

fmayer and others added 2 commits January 5, 2026 10:26
…cal writes (llvm#155901)

This is important to optimize patterns that frequently appear with
bounds checks:

```
for (int i = 0; i < N; ++i) {
  bar[i] = foo[i] + 123;
}
```

which gets roughly turned into

```
for (int i = 0; i < N; ++i) {
  if (i >= size of foo)
     ubsan.trap();
  if (i >= size of bar)
     ubsan.trap();
  bar[i] = foo[i] + 123;
}
```

Motivating example:
https://github.com/google/boringssl/blob/main/crypto/fipsmodule/hmac/hmac.cc.inc#L138

I hand-verified the assembly and confirmed that this optimization
removes the check in the loop.
This also allowed the loop to be vectorized.

Alive2: https://alive2.llvm.org/ce/z/3qMdLF

I did a `stage2-check-all` for both normal and
`-DBOOTSTRAP_CMAKE_C[XX]_FLAGS="-fsanitize=array-bounds
-fsanitize-trap=all"`.

I also ran some Google-internal tests with `fsanitize=array-bounds`.
Everything passes.

(cherry picked from commit 39b0cbe)

 Conflicts:
	llvm/test/Transforms/IndVarSimplify/X86/overflow-intrinsics.ll

 rdar://162704645
@AbhayKanhere AbhayKanhere requested a review from a team as a code owner January 5, 2026 18:47
@AbhayKanhere AbhayKanhere self-assigned this Jan 5, 2026
@AbhayKanhere AbhayKanhere requested review from aemerson and removed request for a team January 5, 2026 18:48
@AbhayKanhere
Copy link
Author

@swift-ci please test macOS
@swift-ci please test Linux
@swift-ci please test Windows

@AbhayKanhere
Copy link
Author

@swift-ci please test Linux

@AbhayKanhere
Copy link
Author

@swift-ci please test macOS

@adrian-prantl
Copy link

@swift-ci test macos

@adrian-prantl
Copy link

@swift-ci test windows

@AbhayKanhere AbhayKanhere changed the title [IndVarSimplofy] Cherry-pick IndVarSimplify hoisting trap checks out of loop. [IndVarSimplify] Cherry-pick IndVarSimplify hoisting trap checks out of loop. Jan 7, 2026
@AbhayKanhere
Copy link
Author

@swift-ci test macos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants