[llvm] 2832d79 - [SROA] Remove UB-implying metadata when promoting speculative instruction.
via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 16 07:37:41 PDT 2023
Author: DianQK
Date: 2023-04-16T22:35:52+08:00
New Revision: 2832d7941f4207f1fcf813b27cf08cecc3086959
URL: https://github.com/llvm/llvm-project/commit/2832d7941f4207f1fcf813b27cf08cecc3086959
DIFF: https://github.com/llvm/llvm-project/commit/2832d7941f4207f1fcf813b27cf08cecc3086959.diff
LOG: [SROA] Remove UB-implying metadata when promoting speculative instruction.
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
Added:
Modified:
llvm/lib/Transforms/Scalar/SROA.cpp
llvm/test/Transforms/SROA/select-load.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index d533f8f6191c1..d5615c902ad15 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -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();
diff --git a/llvm/test/Transforms/SROA/select-load.ll b/llvm/test/Transforms/SROA/select-load.ll
index aac7af93d2192..7df72419b0ce5 100644
--- a/llvm/test/Transforms/SROA/select-load.ll
+++ b/llvm/test/Transforms/SROA/select-load.ll
@@ -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.
More information about the llvm-commits
mailing list