-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[InstCombine]PtrReplacer: Correctly handle select with unavailable operands #148829
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
Conversation
…erands The testcase I added previously failed because a SelectInst with invalid operands was created (one side `addrspace(4)`, the other `addrspace(5)`). PointerReplacer needs to dig deeper if the true and/or false instructions of the select are not available. Fixes SWDEV-542957
This stack of pull requests is managed by Graphite. Learn more about stacking. |
@llvm/pr-subscribers-llvm-transforms Author: Pierre van Houtryve (Pierre-vh) ChangesThe testcase I added previously failed because a SelectInst with invalid operands was created (one side Fixes SWDEV-542957 Full diff: https://github.com/llvm/llvm-project/pull/148829.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index e791bc4b56e52..2cc1bc9fa4a67 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -277,6 +277,15 @@ bool PointerReplacer::collectUsers() {
Worklist.emplace_back(I);
};
+ auto TryPushInstOperand = [&](Instruction *InstOp) {
+ if (!UsersToReplace.contains(InstOp)) {
+ if (!ValuesToRevisit.insert(InstOp))
+ return false;
+ Worklist.emplace_back(InstOp);
+ }
+ return true;
+ };
+
PushUsersToWorklist(&Root);
while (!Worklist.empty()) {
Instruction *Inst = Worklist.pop_back_val();
@@ -309,12 +318,8 @@ bool PointerReplacer::collectUsers() {
// incoming values.
Worklist.emplace_back(PHI);
for (unsigned Idx = 0; Idx < PHI->getNumIncomingValues(); ++Idx) {
- auto *IncomingValue = cast<Instruction>(PHI->getIncomingValue(Idx));
- if (UsersToReplace.contains(IncomingValue))
- continue;
- if (!ValuesToRevisit.insert(IncomingValue))
+ if (!TryPushInstOperand(cast<Instruction>(PHI->getIncomingValue(Idx))))
return false;
- Worklist.emplace_back(IncomingValue);
}
} else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
auto *TrueInst = dyn_cast<Instruction>(SI->getTrueValue());
@@ -322,8 +327,17 @@ bool PointerReplacer::collectUsers() {
if (!TrueInst || !FalseInst)
return false;
- UsersToReplace.insert(SI);
- PushUsersToWorklist(SI);
+ if (isAvailable(TrueInst) && isAvailable(FalseInst)) {
+ UsersToReplace.insert(SI);
+ PushUsersToWorklist(SI);
+ continue;
+ }
+
+ // Push select back onto the stack, followed by unavailable true/false
+ // value.
+ Worklist.emplace_back(SI);
+ if (!TryPushInstOperand(TrueInst) || !TryPushInstOperand(FalseInst))
+ return false;
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
UsersToReplace.insert(GEP);
PushUsersToWorklist(GEP);
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll b/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
new file mode 100644
index 0000000000000..921a72364e6f9
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -passes=instcombine %s -S -o - | FileCheck %s
+
+; Crashed in IC PtrReplacer because an invalid select was generated with addrspace(4) and addrspace(5)
+; operands.
+
+define amdgpu_kernel void @eggs(ptr addrspace(4) byref([12 x i8]) align 16 %arg) {
+; CHECK-LABEL: define amdgpu_kernel void @eggs(
+; CHECK-SAME: ptr addrspace(4) byref([12 x i8]) align 16 [[ARG:%.*]]) {
+; CHECK-NEXT: [[BB:.*:]]
+; CHECK-NEXT: ret void
+;
+bb:
+ %alloca = alloca i32, i32 0, align 8, addrspace(5)
+ %alloca1 = alloca [12 x i8], align 16, addrspace(5)
+ call void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) %alloca1, ptr addrspace(4) %arg, i64 0, i1 false)
+ %select = select i1 false, ptr addrspace(5) %alloca1, ptr addrspace(5) %alloca
+ call void @llvm.memcpy.p0.p5.i64(ptr null, ptr addrspace(5) %select, i64 0, i1 false)
+ ret void
+}
|
@llvm/pr-subscribers-backend-amdgpu Author: Pierre van Houtryve (Pierre-vh) ChangesThe testcase I added previously failed because a SelectInst with invalid operands was created (one side Fixes SWDEV-542957 Full diff: https://github.com/llvm/llvm-project/pull/148829.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index e791bc4b56e52..2cc1bc9fa4a67 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -277,6 +277,15 @@ bool PointerReplacer::collectUsers() {
Worklist.emplace_back(I);
};
+ auto TryPushInstOperand = [&](Instruction *InstOp) {
+ if (!UsersToReplace.contains(InstOp)) {
+ if (!ValuesToRevisit.insert(InstOp))
+ return false;
+ Worklist.emplace_back(InstOp);
+ }
+ return true;
+ };
+
PushUsersToWorklist(&Root);
while (!Worklist.empty()) {
Instruction *Inst = Worklist.pop_back_val();
@@ -309,12 +318,8 @@ bool PointerReplacer::collectUsers() {
// incoming values.
Worklist.emplace_back(PHI);
for (unsigned Idx = 0; Idx < PHI->getNumIncomingValues(); ++Idx) {
- auto *IncomingValue = cast<Instruction>(PHI->getIncomingValue(Idx));
- if (UsersToReplace.contains(IncomingValue))
- continue;
- if (!ValuesToRevisit.insert(IncomingValue))
+ if (!TryPushInstOperand(cast<Instruction>(PHI->getIncomingValue(Idx))))
return false;
- Worklist.emplace_back(IncomingValue);
}
} else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
auto *TrueInst = dyn_cast<Instruction>(SI->getTrueValue());
@@ -322,8 +327,17 @@ bool PointerReplacer::collectUsers() {
if (!TrueInst || !FalseInst)
return false;
- UsersToReplace.insert(SI);
- PushUsersToWorklist(SI);
+ if (isAvailable(TrueInst) && isAvailable(FalseInst)) {
+ UsersToReplace.insert(SI);
+ PushUsersToWorklist(SI);
+ continue;
+ }
+
+ // Push select back onto the stack, followed by unavailable true/false
+ // value.
+ Worklist.emplace_back(SI);
+ if (!TryPushInstOperand(TrueInst) || !TryPushInstOperand(FalseInst))
+ return false;
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
UsersToReplace.insert(GEP);
PushUsersToWorklist(GEP);
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll b/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
new file mode 100644
index 0000000000000..921a72364e6f9
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -passes=instcombine %s -S -o - | FileCheck %s
+
+; Crashed in IC PtrReplacer because an invalid select was generated with addrspace(4) and addrspace(5)
+; operands.
+
+define amdgpu_kernel void @eggs(ptr addrspace(4) byref([12 x i8]) align 16 %arg) {
+; CHECK-LABEL: define amdgpu_kernel void @eggs(
+; CHECK-SAME: ptr addrspace(4) byref([12 x i8]) align 16 [[ARG:%.*]]) {
+; CHECK-NEXT: [[BB:.*:]]
+; CHECK-NEXT: ret void
+;
+bb:
+ %alloca = alloca i32, i32 0, align 8, addrspace(5)
+ %alloca1 = alloca [12 x i8], align 16, addrspace(5)
+ call void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) %alloca1, ptr addrspace(4) %arg, i64 0, i1 false)
+ %select = select i1 false, ptr addrspace(5) %alloca1, ptr addrspace(5) %alloca
+ call void @llvm.memcpy.p0.p5.i64(ptr null, ptr addrspace(5) %select, i64 0, i1 false)
+ ret void
+}
|
llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
Outdated
Show resolved
Hide resolved
llvm/test/Transforms/InstCombine/AMDGPU/ptr-replacer-select-addrspacecast-crash.ll
Outdated
Show resolved
Hide resolved
if (UsersToReplace.contains(IncomingValue)) | ||
continue; | ||
if (!ValuesToRevisit.insert(IncomingValue)) | ||
if (!TryPushInstOperand(cast<Instruction>(PHI->getIncomingValue(Idx)))) |
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.
This is not tested
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.
This is a NFC and is already being tested in AMDGPU/ptr-replace-alloca.ll
; Crashed in IC PtrReplacer because an invalid select was generated with addrspace(4) and addrspace(5) | ||
; operands. | ||
|
||
define amdgpu_kernel void @eggs(ptr addrspace(4) byref([12 x i8]) align 16 %arg) { |
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.
More descriptive function name, please.
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.
eggs and milk, I actually love it. :-D
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.
Sure, I don't mind as long as other tests are named bagel, croissant, etc. Consistent breakfast is key for a healthy compiler ;)
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.
no I was just kidding :-D
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.
In my defense, the first one came from -passes=metarenamer
(which I always use on reduced test cases), but the second one is just me continuing the trend 😄
I'll update the test names
ret void | ||
} | ||
|
||
define amdgpu_kernel void @milk(ptr addrspace(4) byref([12 x i8]) align 16 %arg) { |
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.
ditto
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.
These tests could be merged into AMDGPU/ptr-replace-alloca.ll
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/26944 Here is the relevant piece of the build log for the reference
|
The testcase I added previously failed because a SelectInst with invalid operands was created (one side
addrspace(4)
, the otheraddrspace(5)
).PointerReplacer needs to dig deeper if the true and/or false instructions of the select are not available.
Fixes SWDEV-542957