[llvm] [X86][SimplifyCFG][CodeGen] Support hoisting load/store with conditional faulting (PR #95515)

Shengchen Kan via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 06:20:35 PDT 2024


KanRobert wrote:

> I'm somewhat skeptical about having something like this as a generic transform in SimplifyCFG. This is going to substantially alter the IR on affected targets, in ways that subsequent optimization passes likely won't be able to deal well with. I think it would be a lot safer to start by forming these only in the backend.

It does not introduce new IRs but generates `masked.load/store`. I believe the subsequent optimization passes can handle it well since `masked.load/store` is target-independent intrinsic and was added in llvm long time ago. One proof is

```
bash$ cat test.ll
entry:
  %0 = bitcast i1 false to <1 x i1>
  %1 = call <1 x i16> @llvm.masked.load.v1i16.p0(ptr %b, i32 4, <1 x i1> %0, <1 x i16> <i16 0>)
  %2 = bitcast <1 x i16> %1 to i16
  ret i16 %2
}

define i64 @cond_true(ptr %b) {
entry:
  %0 = bitcast i1 true to <1 x i1>
  %1 = call <1 x i64> @llvm.masked.load.v1i64.p0(ptr %b, i32 4, <1 x i1> %0, <1 x i64> <i64 0>)
  %2 = bitcast <1 x i64> %1 to i64
  ret i64 %2
}
```
```
bash$ opt -O2 -S test.ll
; ModuleID = 'test.ll'
source_filename = "test.ll"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
define noundef i16 @cond_false(ptr nocapture readnone %b) local_unnamed_addr #0 {
entry:
  ret i16 0
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read)
define i64 @cond_true(ptr nocapture readonly %b) local_unnamed_addr #1 {
entry:
  %unmaskedload1 = load i64, ptr %b, align 4
  ret i64 %unmaskedload1
}
```
The mask can be optimize away by middle-end.

I'd like to do it in middle-end instead of backend b/c

1. No need to duplicate logic for SimplifyCFG in backend
2. It gives more chance for `SimplifyCFGOpt::SpeculativelyExecuteBB`
3. It gives more chance for X86 CCMP/CTEST opt, which requires the two CMPs be in same BB before DAG selection.



https://github.com/llvm/llvm-project/pull/95515


More information about the llvm-commits mailing list