Skip to content

Commit ff3c8eb

Browse files
shiltianyuxuanchen1997
authored andcommitted
[InstCombine] Fix a crash in PointerReplacer (#98987)
Summary: A crash could happen in `PointerReplacer::replace` when constructing a new select instruction and there is no replacement for one of its operand. This can happen when the operand is a load instruction that has been replaced earlier such that the operand itself is already the new value. In this case, it is not in the replacement map and `getReplacement` simply returns nullptr. Fix SWDEV-472192. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251751
1 parent ceae6e1 commit ff3c8eb

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,14 @@ void PointerReplacer::replace(Instruction *I) {
394394
NewI->setNoWrapFlags(GEP->getNoWrapFlags());
395395
WorkMap[GEP] = NewI;
396396
} else if (auto *SI = dyn_cast<SelectInst>(I)) {
397-
auto *NewSI = SelectInst::Create(
398-
SI->getCondition(), getReplacement(SI->getTrueValue()),
399-
getReplacement(SI->getFalseValue()), SI->getName(), nullptr, SI);
397+
Value *TrueValue = SI->getTrueValue();
398+
Value *FalseValue = SI->getFalseValue();
399+
if (Value *Replacement = getReplacement(TrueValue))
400+
TrueValue = Replacement;
401+
if (Value *Replacement = getReplacement(FalseValue))
402+
FalseValue = Replacement;
403+
auto *NewSI = SelectInst::Create(SI->getCondition(), TrueValue, FalseValue,
404+
SI->getName(), nullptr, SI);
400405
IC.InsertNewInstWith(NewSI, SI->getIterator());
401406
NewSI->takeName(SI);
402407
WorkMap[SI] = NewSI;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes=instcombine -S -o - %s | FileCheck %s
3+
; REQUIRES: amdgpu-registered-target
4+
5+
target triple = "amdgcn-amd-amdhsa"
6+
7+
%anon = type { i32, [8 x ptr], ptr }
8+
9+
define void @foo(ptr addrspace(4) byref(%anon) align 8 %0) {
10+
; CHECK-LABEL: @foo(
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr addrspace(4) [[TMP0:%.*]], align 8
13+
; CHECK-NEXT: br label [[FOR_COND10:%.*]]
14+
; CHECK: for.cond10:
15+
; CHECK-NEXT: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 8
16+
; CHECK-NEXT: store i64 [[TMP2]], ptr addrspace(1) null, align 8
17+
; CHECK-NEXT: br label [[FOR_COND10]]
18+
;
19+
entry:
20+
%coerce = alloca %anon, addrspace(5)
21+
call void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) %coerce, ptr addrspace(4) %0, i64 0, i1 false)
22+
%asc = addrspacecast ptr addrspace(5) %coerce to ptr
23+
%load = load ptr, ptr addrspace(5) %coerce
24+
%retval.0.i = select i1 false, ptr %asc, ptr %load
25+
br label %for.cond10
26+
27+
for.cond10: ; preds = %for.cond10, %entry
28+
%3 = load i64, ptr %retval.0.i
29+
store i64 %3, ptr addrspace(1) null
30+
br label %for.cond10
31+
}
32+
33+
declare void @llvm.memcpy.p5.p4.i64(ptr addrspace(5), ptr addrspace(4), i64, i1 immarg)

0 commit comments

Comments
 (0)