Skip to content

[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

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -309,21 +318,26 @@ 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))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not tested

Copy link
Contributor

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

return false;
Worklist.emplace_back(IncomingValue);
}
} else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
auto *TrueInst = dyn_cast<Instruction>(SI->getTrueValue());
auto *FalseInst = dyn_cast<Instruction>(SI->getFalseValue());
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);
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/Transforms/InstCombine/AMDGPU/ptr-replace-alloca.ll
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,37 @@ sink:
ret <2 x i64> %val.sink
}

; Crashed in IC PtrReplacer because an invalid select was generated with addrspace(4) and addrspace(5)
; operands.
define amdgpu_kernel void @select_addr4_addr5(ptr addrspace(4) byref([12 x i8]) align 16 %arg) {
; CHECK-LABEL: define amdgpu_kernel void @select_addr4_addr5(
; 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
}

; Same as above but with swapped operands on the select.
define amdgpu_kernel void @select_addr4_addr5_swapped(ptr addrspace(4) byref([12 x i8]) align 16 %arg) {
; CHECK-LABEL: define amdgpu_kernel void @select_addr4_addr5_swapped(
; 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) %alloca, ptr addrspace(5) %alloca1
call void @llvm.memcpy.p0.p5.i64(ptr null, ptr addrspace(5) %select, i64 0, i1 false)
ret void
}

declare void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) noalias writeonly captures(none), ptr addrspace(4) noalias readonly captures(none), i64, i1 immarg) #0
Loading