Skip to content

Commit b039ffa

Browse files
author
LU-JOHN
authored
[SYCL] Do not crash if no matching store found in SYCLMutatePrintfAddrspacePass (#15658)
In SYCLMutatePrintfAddrspacePass, when analyzing a format pointer to printf, do not crash when analyzing backwards through load/stores if no matching store is found. --------- Signed-off-by: Lu, John <[email protected]>
1 parent de5e4ee commit b039ffa

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

llvm/lib/SYCLLowerIR/MutatePrintfAddrspace.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,12 @@ Value *stripToMemorySource(Value *V) {
160160
Value *MemoryAccess = V;
161161
if (auto *LI = dyn_cast<LoadInst>(MemoryAccess)) {
162162
Value *LoadSource = LI->getPointerOperand();
163-
auto *Store = cast<StoreInst>(*llvm::find_if(
164-
LoadSource->users(), [](User *U) { return isa<StoreInst>(U); }));
165-
MemoryAccess = Store->getValueOperand();
163+
auto Users = LoadSource->users();
164+
auto I = llvm::find_if(Users, [](User *U) { return isa<StoreInst>(U); });
165+
if (I != Users.end()) {
166+
auto *Store = cast<StoreInst>(*I);
167+
MemoryAccess = Store->getValueOperand();
168+
}
166169
}
167170
return MemoryAccess->stripPointerCastsAndAliases();
168171
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
;; This test is derived from generic_as_variadic_no_opt.ll.
2+
;; This test ensures that when SYCLMutatePrintfAddrspace analyzes a load
3+
;; of the format pointer for a printf, it will not crash if a
4+
;; corresponding store is not found.
5+
6+
; RUN: not opt < %s -passes=SYCLMutatePrintfAddrspace -S 2>&1 | FileCheck %s
7+
8+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
9+
target triple = "spir64-unknown-unknown"
10+
11+
$_ZN2cl4sycl3ext6oneapi12experimental6printfIcJfEEEiPKT_DpT0_ = comdat any
12+
13+
; CHECK: error: experimental::printf requires format string to reside in constant address space. The compiler wasn't able to automatically convert your format string into constant address space when processing builtin _Z18__spirv_ocl_printf{{.*}} called in function {{.*}}.
14+
15+
; Function Attrs: convergent mustprogress noinline norecurse optnone
16+
define linkonce_odr dso_local spir_func i32 @_ZN2cl4sycl3ext6oneapi12experimental6printfIcJfEEEiPKT_DpT0_(ptr addrspace(4) %__format, float %args) #2 comdat {
17+
entry:
18+
%retval = alloca i32, align 4
19+
%__format.addr = alloca ptr addrspace(4), align 8
20+
%args.addr = alloca float, align 4
21+
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
22+
%__format.addr.ascast = addrspacecast ptr %__format.addr to ptr addrspace(4)
23+
%args.addr.ascast = addrspacecast ptr %args.addr to ptr addrspace(4)
24+
; Remove store to ensure SYCLMutatePrintfAddrspace will not crash.
25+
; store ptr addrspace(4) %__format, ptr addrspace(4) %__format.addr.ascast, align 8
26+
store float %args, ptr addrspace(4) %args.addr.ascast, align 4
27+
%0 = load ptr addrspace(4), ptr addrspace(4) %__format.addr.ascast, align 8
28+
%1 = load float, ptr addrspace(4) %args.addr.ascast, align 4
29+
%call = call spir_func i32 (ptr addrspace(4), ...) @_Z18__spirv_ocl_printfPKcz(ptr addrspace(4) %0, float %1) #8
30+
ret i32 %call
31+
}
32+
33+
; Function Attrs: convergent
34+
declare dso_local spir_func i32 @_Z18__spirv_ocl_printfPKcz(ptr addrspace(4), ...) #7
35+
36+
attributes #2 = { convergent mustprogress noinline norecurse optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
37+
attributes #7 = { convergent "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
38+
attributes #8 = { convergent }
39+
40+
!llvm.module.flags = !{!0, !1}
41+
!opencl.spir.version = !{!2}
42+
!spirv.Source = !{!3}
43+
44+
!0 = !{i32 1, !"wchar_size", i32 4}
45+
!1 = !{i32 7, !"frame-pointer", i32 2}
46+
!2 = !{i32 1, i32 2}
47+
!3 = !{i32 4, i32 100000}

0 commit comments

Comments
 (0)