Skip to content

Commit

Permalink
[SROA] Remove UB-implying metadata when promoting speculative instruc…
Browse files Browse the repository at this point in the history
…tion.

After D138238 introduced the then/else blocks, we should remove UB-implying metadata for the promoted speculative instruction.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D148456
  • Loading branch information
DianQK committed Apr 16, 2023
1 parent 2db0315 commit 2832d79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Expand Up @@ -1688,15 +1688,17 @@ static void rewriteMemOpOfSelect(SelectInst &SI, T &I,
bool IsThen = SuccBB == HeadBI->getSuccessor(0);
int SuccIdx = IsThen ? 0 : 1;
auto *NewMemOpBB = SuccBB == Tail ? Head : SuccBB;
auto &CondMemOp = cast<T>(*I.clone());
if (NewMemOpBB != Head) {
NewMemOpBB->setName(Head->getName() + (IsThen ? ".then" : ".else"));
if (isa<LoadInst>(I))
++NumLoadsPredicated;
else
++NumStoresPredicated;
} else
} else {
CondMemOp.dropUBImplyingAttrsAndUnknownMetadata();
++NumLoadsSpeculated;
auto &CondMemOp = cast<T>(*I.clone());
}
CondMemOp.insertBefore(NewMemOpBB->getTerminator());
Value *Ptr = SI.getOperand(1 + SuccIdx);
if (auto *PtrTy = Ptr->getType();
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Transforms/SROA/select-load.ll
Expand Up @@ -409,7 +409,31 @@ entry:
ret i32 %r
}

; When promoting speculative instruction, metadata that may trigger immediate UB should be dropped.
define void @load_of_select_with_noundef_nonnull(ptr %buffer, i1 %b) {
; CHECK-PRESERVE-CFG-LABEL: @load_of_select_with_noundef_nonnull(
; CHECK-PRESERVE-CFG-NEXT: [[UB_PTR:%.*]] = alloca ptr, align 8
; CHECK-PRESERVE-CFG-NEXT: [[SELECT_PTR:%.*]] = select i1 [[B:%.*]], ptr [[BUFFER:%.*]], ptr [[UB_PTR]]
; CHECK-PRESERVE-CFG-NEXT: [[LOAD_PTR:%.*]] = load ptr, ptr [[SELECT_PTR]], align 8, !nonnull !1, !noundef !1
; CHECK-PRESERVE-CFG-NEXT: ret void
;
; CHECK-MODIFY-CFG-LABEL: @load_of_select_with_noundef_nonnull(
; CHECK-MODIFY-CFG-NEXT: br i1 [[B:%.*]], label [[DOTTHEN:%.*]], label [[DOTCONT:%.*]]
; CHECK-MODIFY-CFG: .then:
; CHECK-MODIFY-CFG-NEXT: [[LOAD_PTR_THEN_VAL:%.*]] = load ptr, ptr [[BUFFER:%.*]], align 8, !nonnull !2, !noundef !2
; CHECK-MODIFY-CFG-NEXT: br label [[DOTCONT]]
; CHECK-MODIFY-CFG: .cont:
; CHECK-MODIFY-CFG-NEXT: [[LOAD_PTR:%.*]] = phi ptr [ [[LOAD_PTR_THEN_VAL]], [[DOTTHEN]] ], [ undef, [[TMP0:%.*]] ]
; CHECK-MODIFY-CFG-NEXT: ret void
;
%ub_ptr = alloca ptr
%select_ptr = select i1 %b, ptr %buffer, ptr %ub_ptr
%load_ptr = load ptr, ptr %select_ptr, !nonnull !1, !noundef !1
ret void
}

!0 = !{!"branch_weights", i32 1, i32 99}
!1 = !{}

; Ensure that the branch metadata is reversed to match the reversals above.

Expand Down

0 comments on commit 2832d79

Please sign in to comment.