[SROA] Prevent load atomic vector from being generated - #112432
Conversation
These are illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter.
|
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-llvm-transforms Author: None (jofrn) ChangesThese are illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter. Full diff: https://github.com/llvm/llvm-project/pull/112432.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 92589ab17da313..450ecdf20ef009 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -2853,6 +2853,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
bool visitLoadInst(LoadInst &LI) {
LLVM_DEBUG(dbgs() << " original: " << LI << "\n");
+
+ // load atomic vector would be generated, which is illegal
+ if (LI.isAtomic() && NewAI.getAllocatedType()->isVectorTy())
+ return false;
+
Value *OldOp = LI.getOperand(0);
assert(OldOp == OldPtr);
diff --git a/llvm/test/Transforms/SROA/atomic-vector.ll b/llvm/test/Transforms/SROA/atomic-vector.ll
new file mode 100644
index 00000000000000..d43ae653fba1dd
--- /dev/null
+++ b/llvm/test/Transforms/SROA/atomic-vector.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -passes='sroa' -S 2>&1 | FileCheck %s --check-prefix=ERR
+; RUN: opt < %s -passes='sroa' -S | FileCheck %s
+
+define float @atomic_vector() {
+; ERR-NOT: atomic load operand must have integer, pointer, or floating point type!
+; ERR-NOT: <1 x float> {{%.*}} = load atomic volatile <1 x float>, ptr {{%.*}} acquire, align 4
+; CHECK: %1 = alloca <1 x float>, align 4
+; CHECK-NEXT: store <1 x float> undef, ptr %1, align 4
+; CHECK-NEXT: %2 = load atomic volatile float, ptr %1 acquire, align 4
+; CHECK-NEXT: ret float %2
+ %1 = alloca <1 x float>
+ %2 = alloca <1 x float>
+ %3 = alloca ptr
+ call void @llvm.memcpy.p0.p0.i64(ptr %2, ptr %1, i64 4, i1 false)
+ store ptr %2, ptr %3
+ %4 = load ptr, ptr %3
+ %5 = load atomic volatile float, ptr %4 acquire, align 4
+ ret float %5
+}
|
|
This is a follow-up to #111414, which used -disable-verify. This commit prevents atomic load vector at all. |
| LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); | ||
|
|
||
| // load atomic vector would be generated, which is illegal | ||
| if (LI.isAtomic() && NewAI.getAllocatedType()->isVectorTy()) |
There was a problem hiding this comment.
Is it illegal from LangRef's perspective?
There was a problem hiding this comment.
It is at least illegal from the perspective of IR/Verifier.
There was a problem hiding this comment.
The restriction is dumb and we should relax it. Instead of just hardcoding isVectorTy here, should have some kind of LoadInst::isValidAtomicType or something
There was a problem hiding this comment.
The verifier check should be moved into a LoadInst helper
There was a problem hiding this comment.
No alloca reference, just the type. The implementation should match Verifier::visitLoadInst, the isIntOrIntPtrTy || isFloatingPointTy and checkAtomicMemAccessSize (also add some non-atomic size tests?)
There was a problem hiding this comment.
We need to ensure an atomic does not have a vector type. If the load is atomic and the alloca that will lend its type over has a vector type, then we will generate an atomic vector, which are illegal. We need to ensure that this doesn't occur.
There was a problem hiding this comment.
No, you need to ensure the atomic is a valid type for an atomic load.
Alternatively you can do the load with the equivalent sized type and then bitcast (which is why this restriction is dumb in the first place, the lowering can always do the same)
There was a problem hiding this comment.
We are ensuring it is a valid type in the case under question, by checking the alloca's type. The invalid load will not be generated if the alloca has a vector type as that type will overwrite the load's type.
There was a problem hiding this comment.
We don't want any loads that are atomic to have a vector type, regardless of whether the atomic itself is already valid; so checking if the atomic is more valid before translating it in AllocaSliceRewriter will miss cases where we form an invalid atomic during SROA (and then later form a vector type with it in visitLoadInst). Even though it is not likely as SROA probably won't form these, it illustrates why we don't need the extra checks here.
| ; CHECK-NEXT: [[TMP2:%.*]] = load atomic volatile float, ptr [[TMP1]] acquire, align 4 | ||
| ; CHECK-NEXT: ret float [[TMP2]] | ||
| ; | ||
| %1 = alloca <1 x float> |
| ; CHECK-NEXT: ret float [[TMP2]] | ||
| ; | ||
| %1 = alloca <1 x float> | ||
| %2 = alloca <1 x float> |
There was a problem hiding this comment.
Test an integer and pointer vector too
| ; CHECK-NEXT: ret i32 [[A_0_LOAD_EXT]] | ||
| ; CHECK-NEXT: [[A:%.*]] = alloca i1, align 1 | ||
| ; CHECK-NEXT: [[V:%.*]] = load atomic i32, ptr [[A]] seq_cst, align 4 | ||
| ; CHECK-NEXT: ret i32 [[V]] |
There was a problem hiding this comment.
Due to this test, we may want to check only the type without DL. Adding too many checks hinders optimization.
| if (AO == AtomicOrdering::Release || AO == AtomicOrdering::AcquireRelease) | ||
| return false; | ||
| unsigned Size = DL.getTypeSizeInBits(Ty); | ||
| return Size >= 8 && !(Size & (Size - 1)); |
There was a problem hiding this comment.
basictest.ll fails due to checking DL here.
| if (!Ty->isIntOrPtrTy() && !Ty->isFloatingPointTy()) | ||
| return false; | ||
| return true; |
There was a problem hiding this comment.
Direct return of boolean expression
| bool visitLoadInst(LoadInst &LI) { | ||
| LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); | ||
|
|
||
| // load atomic vector would be generated, which is illegal |
There was a problem hiding this comment.
| // load atomic vector would be generated, which is illegal | |
| // Load atomic vector would be generated, which is illegal. |
| %indirect = load ptr, ptr %direct | ||
| %ret = load atomic volatile ptr, ptr %indirect acquire, align 4 | ||
| ret ptr %ret | ||
| } |
There was a problem hiding this comment.
Test a <2 x i16> or some other real vector. 1 x is a degenerate case
| /// Returns false if this type would be invalid in the | ||
| /// creation of a load atomic instruction. | ||
| static bool isValidAtomicTy(Type *Ty); | ||
| static bool isValidAtomicTy(Type *Ty, const DataLayout *DL = nullptr, |
There was a problem hiding this comment.
DataLayout must be mandatory
There was a problem hiding this comment.
Then we need to omit it entirely because otherwise we will be checking the type size of the alloca, which we want to avoid if to optimize basictest.ll.
| return false; | ||
| if (AO == AtomicOrdering::Release || AO == AtomicOrdering::AcquireRelease) | ||
| return false; | ||
| if (DL) { |
| %indirect = load ptr, ptr %direct | ||
| %ret = load atomic volatile i32, ptr %indirect acquire, align 4 | ||
| ret i32 %ret | ||
| } |
There was a problem hiding this comment.
Add test for the non-byte illegal case?
198a549 to
db0a8fe
Compare
|
@jofrn out of date patch? |
|
The following correctness issue was found by llvm-hackme. This comment is generated by an automated correctness checking service designed to help identify critical correctness bugs (opt crashes or Alive2 miscompilations) and improve PR review efficiency under limited reviewer bandwidth. The reproducer was generated by LLM. This bug is a regression introduced by this PR. It does not reproduce on the baseline commit. ReproducerKind: crash IR Reproducer: ; RUN: opt -passes=sroa -S
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define i32 @crash() {
%a = alloca <2 x i32>, align 8
store <2 x i32> <i32 1, i32 2>, ptr %a, align 8
%v = load atomic i32, ptr %a monotonic, align 4
ret i32 %v
}Stacktrace: Baseline Revision: |
dtcxzyw
left a comment
There was a problem hiding this comment.
A correctness issue was identified by llvm-hackme. Please see the issue comment for details.
load atomic <n x T>is illegal, and they can be formed from SROA via indirect volatile loads in the AllocaSliceRewriter.