Skip to content

Commit 2a07b4a

Browse files
Pierre-vhDavid Salinas
authored andcommitted
[StackSafetyAnalysis] Don't call getTruncateOrZeroExtend for pointers of different sizes (llvm#79804)
Otherwise SCEV asserts `Can't extend pointer!` Fixes SWDEV-442670 Change-Id: If1db5661c26760462acb7b05a1cd333a4eb01b88
1 parent 7953f53 commit 2a07b4a

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

llvm/lib/Analysis/StackSafetyAnalysis.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ class StackSafetyLocalAnalysis {
243243

244244
const ConstantRange UnknownRange;
245245

246+
/// FIXME: This function is a bandaid, it's only needed
247+
/// because this pass doesn't handle address spaces of different pointer
248+
/// sizes.
249+
///
250+
/// \returns \p Val's SCEV as a pointer of AS zero, or nullptr if it can't be
251+
/// converted to AS 0.
252+
const SCEV *getSCEVAsPointer(Value *Val);
253+
246254
ConstantRange offsetFrom(Value *Addr, Value *Base);
247255
ConstantRange getAccessRange(Value *Addr, Value *Base,
248256
const ConstantRange &SizeRange);
@@ -268,13 +276,29 @@ class StackSafetyLocalAnalysis {
268276
FunctionInfo<GlobalValue> run();
269277
};
270278

279+
const SCEV *StackSafetyLocalAnalysis::getSCEVAsPointer(Value *Val) {
280+
Type *ValTy = Val->getType();
281+
282+
// We don't handle targets with multiple address spaces.
283+
if (!ValTy->isPointerTy()) {
284+
auto *PtrTy = PointerType::getUnqual(SE.getContext());
285+
return SE.getTruncateOrZeroExtend(SE.getSCEV(Val), PtrTy);
286+
}
287+
288+
if (ValTy->getPointerAddressSpace() != 0)
289+
return nullptr;
290+
return SE.getSCEV(Val);
291+
}
292+
271293
ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) {
272294
if (!SE.isSCEVable(Addr->getType()) || !SE.isSCEVable(Base->getType()))
273295
return UnknownRange;
274296

275-
auto *PtrTy = PointerType::getUnqual(SE.getContext());
276-
const SCEV *AddrExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Addr), PtrTy);
277-
const SCEV *BaseExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Base), PtrTy);
297+
const SCEV *AddrExp = getSCEVAsPointer(Addr);
298+
const SCEV *BaseExp = getSCEVAsPointer(Base);
299+
if (!AddrExp || !BaseExp)
300+
return UnknownRange;
301+
278302
const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp);
279303
if (isa<SCEVCouldNotCompute>(Diff))
280304
return UnknownRange;
@@ -362,13 +386,11 @@ bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
362386

363387
const auto *I = cast<Instruction>(U.getUser());
364388

365-
auto ToCharPtr = [&](const SCEV *V) {
366-
auto *PtrTy = PointerType::getUnqual(SE.getContext());
367-
return SE.getTruncateOrZeroExtend(V, PtrTy);
368-
};
389+
const SCEV *AddrExp = getSCEVAsPointer(U.get());
390+
const SCEV *BaseExp = getSCEVAsPointer(AI);
391+
if (!AddrExp || !BaseExp)
392+
return false;
369393

370-
const SCEV *AddrExp = ToCharPtr(SE.getSCEV(U.get()));
371-
const SCEV *BaseExp = ToCharPtr(SE.getSCEV(AI));
372394
const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp);
373395
if (isa<SCEVCouldNotCompute>(Diff))
374396
return false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt %s -disable-output -S -passes="print<stack-safety-local>" 2>&1 | FileCheck %s
2+
3+
; Datalayout from AMDGPU, p5 is 32 bits and p is 64.
4+
; We used to call SCEV getTruncateOrZeroExtend on %x.ascast/%x which caused an assertion failure.
5+
6+
; CHECK: @a dso_preemptable
7+
; CHECK-NEXT: args uses:
8+
; CHECK-NEXT: x[]: full-set
9+
; CHECK-NEXT: allocas uses:
10+
11+
target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
12+
13+
define void @a(ptr addrspace(5) %x) {
14+
entry:
15+
%x.ascast = addrspacecast ptr addrspace(5) %x to ptr
16+
%tmp = load i64, ptr %x.ascast
17+
store i64 %tmp, ptr %x.ascast, align 8
18+
ret void
19+
}

0 commit comments

Comments
 (0)