-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[X86,SimplifyCFG] Support hoisting load/store with conditional faulting (Part II) #108812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1077be1
9d73fdd
15e25fb
40e2130
d6d50c4
de28ed9
8ec9409
fc7df9a
dfe6cc6
1ad8714
5a5de39
b1bda56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1663,18 +1663,29 @@ static bool areIdenticalUpToCommutativity(const Instruction *I1, | |
| static void hoistConditionalLoadsStores( | ||
| BranchInst *BI, | ||
| SmallVectorImpl<Instruction *> &SpeculatedConditionalLoadsStores, | ||
| bool Invert) { | ||
| std::optional<bool> Invert) { | ||
| auto &Context = BI->getParent()->getContext(); | ||
| auto *VCondTy = FixedVectorType::get(Type::getInt1Ty(Context), 1); | ||
| auto *Cond = BI->getOperand(0); | ||
| // Construct the condition if needed. | ||
| BasicBlock *BB = BI->getParent(); | ||
| IRBuilder<> Builder(SpeculatedConditionalLoadsStores.back()); | ||
| Value *Mask = Builder.CreateBitCast( | ||
| Invert ? Builder.CreateXor(Cond, ConstantInt::getTrue(Context)) : Cond, | ||
| VCondTy); | ||
| IRBuilder<> Builder(Invert ? SpeculatedConditionalLoadsStores.back() : BI); | ||
|
||
| Value *Mask = nullptr; | ||
| Value *Mask0 = nullptr; | ||
| Value *Mask1 = nullptr; | ||
| if (Invert) { | ||
| Mask = Builder.CreateBitCast( | ||
| *Invert ? Builder.CreateXor(Cond, ConstantInt::getTrue(Context)) : Cond, | ||
| VCondTy); | ||
| } else { | ||
| Mask0 = Builder.CreateBitCast( | ||
|
||
| Builder.CreateXor(Cond, ConstantInt::getTrue(Context)), VCondTy); | ||
| Mask1 = Builder.CreateBitCast(Cond, VCondTy); | ||
| } | ||
| for (auto *I : SpeculatedConditionalLoadsStores) { | ||
| IRBuilder<> Builder(I); | ||
| IRBuilder<> Builder(Invert ? I : BI); | ||
| if (!Invert) | ||
|
||
| Mask = I->getParent() == BI->getSuccessor(0) ? Mask1 : Mask0; | ||
| // We currently assume conditional faulting load/store is supported for | ||
| // scalar types only when creating new instructions. This can be easily | ||
| // extended for vector types in the future. | ||
|
|
@@ -1771,6 +1782,25 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI, | |
| if (Succ->hasAddressTaken() || !Succ->getSinglePredecessor()) | ||
| return false; | ||
|
|
||
| auto *BI = dyn_cast<BranchInst>(TI); | ||
| if (BI && HoistLoadsStoresWithCondFaulting && | ||
| Options.HoistLoadsStoresWithCondFaulting) { | ||
| SmallVector<Instruction *, 2> SpeculatedConditionalLoadsStores; | ||
| for (auto *Succ : successors(BB)) { | ||
| for (Instruction &I : drop_end(*Succ)) { | ||
| if (!isSafeCheapLoadStore(&I, TTI) || | ||
| SpeculatedConditionalLoadsStores.size() == | ||
| HoistLoadsStoresWithCondFaultingThreshold) | ||
| return false; | ||
| SpeculatedConditionalLoadsStores.push_back(&I); | ||
| } | ||
| } | ||
|
|
||
| if (!SpeculatedConditionalLoadsStores.empty()) | ||
|
||
| hoistConditionalLoadsStores(BI, SpeculatedConditionalLoadsStores, | ||
| std::nullopt); | ||
|
||
| } | ||
|
|
||
| // The second of pair is a SkipFlags bitmask. | ||
| using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>; | ||
| SmallVector<SuccIterPair, 8> SuccIterPairs; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,21 +276,19 @@ if.false: ; preds = %if.true, %entry | |
| } | ||
|
|
||
| ;; Both of successor 0 and successor 1 have a single predecessor. | ||
| ;; TODO: Support transform for this case. | ||
| define void @single_predecessor(ptr %p, ptr %q, i32 %a) { | ||
| ; CHECK-LABEL: @single_predecessor( | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[A:%.*]], 0 | ||
| ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]] | ||
| ; CHECK: common.ret: | ||
| ; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[TOBOOL]], true | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i1 [[TMP0]] to <1 x i1> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i1 [[TOBOOL]] to <1 x i1> | ||
| ; CHECK-NEXT: call void @llvm.masked.store.v1i32.p0(<1 x i32> <i32 1>, ptr [[Q:%.*]], i32 4, <1 x i1> [[TMP2]]) | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = call <1 x i32> @llvm.masked.load.v1i32.p0(ptr [[Q]], i32 4, <1 x i1> [[TMP1]], <1 x i32> poison) | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = bitcast <1 x i32> [[TMP3]] to i32 | ||
| ; CHECK-NEXT: [[TMP5:%.*]] = bitcast i32 [[TMP4]] to <1 x i32> | ||
| ; CHECK-NEXT: call void @llvm.masked.store.v1i32.p0(<1 x i32> [[TMP5]], ptr [[P:%.*]], i32 4, <1 x i1> [[TMP1]]) | ||
|
||
| ; CHECK-NEXT: ret void | ||
| ; CHECK: if.end: | ||
| ; CHECK-NEXT: store i32 1, ptr [[Q:%.*]], align 4 | ||
| ; CHECK-NEXT: br label [[COMMON_RET:%.*]] | ||
| ; CHECK: if.then: | ||
| ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[Q]], align 4 | ||
| ; CHECK-NEXT: store i32 [[TMP0]], ptr [[P:%.*]], align 4 | ||
| ; CHECK-NEXT: br label [[COMMON_RET]] | ||
| ; | ||
| entry: | ||
| %tobool = icmp ne i32 %a, 0 | ||
|
|
@@ -728,6 +726,34 @@ if.true: | |
| ret i32 %res | ||
| } | ||
|
|
||
| define void @diamondCFG(i32 %a, ptr %c, ptr %d) { | ||
|
||
| ; CHECK-LABEL: @diamondCFG( | ||
| ; CHECK-NEXT: entry: | ||
| ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[A:%.*]], 0 | ||
| ; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[TOBOOL_NOT]], true | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = bitcast i1 [[TMP0]] to <1 x i1> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = bitcast i1 [[TOBOOL_NOT]] to <1 x i1> | ||
| ; CHECK-NEXT: call void @llvm.masked.store.v1i32.p0(<1 x i32> zeroinitializer, ptr [[D:%.*]], i32 4, <1 x i1> [[TMP2]]) | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = bitcast i32 [[A]] to <1 x i32> | ||
| ; CHECK-NEXT: call void @llvm.masked.store.v1i32.p0(<1 x i32> [[TMP3]], ptr [[C:%.*]], i32 4, <1 x i1> [[TMP1]]) | ||
| ; CHECK-NEXT: ret void | ||
| ; | ||
| entry: | ||
| %tobool.not = icmp eq i32 %a, 0 | ||
| br i1 %tobool.not, label %if.else, label %if.then | ||
|
|
||
| if.then: ; preds = %entry | ||
| store i32 %a, ptr %c, align 4 | ||
| br label %if.end | ||
|
|
||
| if.else: ; preds = %entry | ||
| store i32 0, ptr %d, align 4 | ||
| br label %if.end | ||
|
|
||
| if.end: ; preds = %if.else, %if.then | ||
| ret void | ||
| } | ||
|
|
||
| declare i32 @read_memory_only() readonly nounwind willreturn speculatable | ||
|
|
||
| !llvm.dbg.cu = !{!0} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment like
?
It's a little hard to know when it's nullopt w/o searching for the caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.