Skip to content

[SYCL] Do not crash if no matching store found #15658

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
Oct 11, 2024
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
9 changes: 6 additions & 3 deletions llvm/lib/SYCLLowerIR/MutatePrintfAddrspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ Value *stripToMemorySource(Value *V) {
Value *MemoryAccess = V;
if (auto *LI = dyn_cast<LoadInst>(MemoryAccess)) {
Value *LoadSource = LI->getPointerOperand();
auto *Store = cast<StoreInst>(*llvm::find_if(
LoadSource->users(), [](User *U) { return isa<StoreInst>(U); }));
MemoryAccess = Store->getValueOperand();
auto Users = LoadSource->users();
auto I = llvm::find_if(Users, [](User *U) { return isa<StoreInst>(U); });
if (I != Users.end()) {
auto *Store = cast<StoreInst>(*I);
MemoryAccess = Store->getValueOperand();
}
}
return MemoryAccess->stripPointerCastsAndAliases();
Copy link
Contributor

Choose a reason for hiding this comment

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

could we hit a similar problem here?

Copy link
Contributor Author

@LU-JOHN LU-JOHN Oct 10, 2024

Choose a reason for hiding this comment

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

I don't think it's possible to have a problem. A store must have a value. SpecConstants.cpp has similar code:
V = Store->getValueOperand()->stripPointerCasts();

Copy link
Contributor

Choose a reason for hiding this comment

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

ah ok yeah i see, thanks

}
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/SYCLLowerIR/printf_addrspace/no_matching_store.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
;; This test is derived from generic_as_variadic_no_opt.ll.
;; This test ensures that when SYCLMutatePrintfAddrspace analyzes a load
;; of the format pointer for a printf, it will not crash if a
;; corresponding store is not found.

; RUN: not opt < %s -passes=SYCLMutatePrintfAddrspace -S 2>&1 | FileCheck %s

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"
target triple = "spir64-unknown-unknown"

$_ZN2cl4sycl3ext6oneapi12experimental6printfIcJfEEEiPKT_DpT0_ = comdat any

; 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 {{.*}}.

; Function Attrs: convergent mustprogress noinline norecurse optnone
define linkonce_odr dso_local spir_func i32 @_ZN2cl4sycl3ext6oneapi12experimental6printfIcJfEEEiPKT_DpT0_(ptr addrspace(4) %__format, float %args) #2 comdat {
entry:
%retval = alloca i32, align 4
%__format.addr = alloca ptr addrspace(4), align 8
%args.addr = alloca float, align 4
%retval.ascast = addrspacecast ptr %retval to ptr addrspace(4)
%__format.addr.ascast = addrspacecast ptr %__format.addr to ptr addrspace(4)
%args.addr.ascast = addrspacecast ptr %args.addr to ptr addrspace(4)
; Remove store to ensure SYCLMutatePrintfAddrspace will not crash.
; store ptr addrspace(4) %__format, ptr addrspace(4) %__format.addr.ascast, align 8
store float %args, ptr addrspace(4) %args.addr.ascast, align 4
%0 = load ptr addrspace(4), ptr addrspace(4) %__format.addr.ascast, align 8
%1 = load float, ptr addrspace(4) %args.addr.ascast, align 4
%call = call spir_func i32 (ptr addrspace(4), ...) @_Z18__spirv_ocl_printfPKcz(ptr addrspace(4) %0, float %1) #8
ret i32 %call
}

; Function Attrs: convergent
declare dso_local spir_func i32 @_Z18__spirv_ocl_printfPKcz(ptr addrspace(4), ...) #7

attributes #2 = { convergent mustprogress noinline norecurse optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #7 = { convergent "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #8 = { convergent }

!llvm.module.flags = !{!0, !1}
!opencl.spir.version = !{!2}
!spirv.Source = !{!3}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"frame-pointer", i32 2}
!2 = !{i32 1, i32 2}
!3 = !{i32 4, i32 100000}
Loading