Skip to content
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

Add check to early exit when OpenCL builtin is not found in the map #2525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

asudarsa
Copy link
Contributor

Several user defined/declared functions were being identified as 'possible' OpenCL builtins as they had prefixes (e.g. atomic_) similar to OpenCL builtin function names. However, they were not present in the OCLSPIRVBuiltinMap.
One of the OCLToSPIRV utility functions calls OCLSPIRVBuiltinMap::map function on such user defined functions and this results in a crash.
The correct way to do this will be to check if the OCL function name key is present in the map and exit early if not.

This PR does that.

Thanks

@asudarsa asudarsa requested a review from svenvh April 22, 2024 13:25
@asudarsa
Copy link
Contributor Author

This PR should help us to get #2500 back by reverting #2508

Thanks

@bwlodarcz
Copy link
Contributor

The fix of the issue is likely a little bigger than "atomic". The changes covers "atomic" prefix but there are other prefixes in OCLToSPIRVBase::visitCallInst in OCLToSPIRV.cpp:196 like "convert" or "VLoad" which needs to be handled with care as well. I didn't do deep dive but by quickly glimpsing the code of the function handling that cases it's imaginable to lead Translator again into bogus state by carefully crafted function name. What do you think @MrSidims, @asudarsa?

@MrSidims MrSidims added stale Old not addressed issues and removed stale Old not addressed issues labels Apr 23, 2024
@asudarsa
Copy link
Contributor Author

The fix of the issue is likely a little bigger than "atomic". The changes covers "atomic" prefix but there are other prefixes in OCLToSPIRVBase::visitCallInst in OCLToSPIRV.cpp:196 like "convert" or "VLoad" which needs to be handled with care as well. I didn't do deep dive but by quickly glimpsing the code of the function handling that cases it's imaginable to lead Translator again into bogus state by carefully crafted function name. What do you think @MrSidims, @asudarsa?

Hi @bwlodarcz

Thanks much for the feedback.
I am now trying several tests trying to manually create the user defined functions with OCL looking names. I will update the PR with my findings.

Sincerely

@asudarsa
Copy link
Contributor Author

The fix of the issue is likely a little bigger than "atomic". The changes covers "atomic" prefix but there are other prefixes in OCLToSPIRVBase::visitCallInst in OCLToSPIRV.cpp:196 like "convert" or "VLoad" which needs to be handled with care as well. I didn't do deep dive but by quickly glimpsing the code of the function handling that cases it's imaginable to lead Translator again into bogus state by carefully crafted function name. What do you think @MrSidims, @asudarsa?

The transBuiltin(...) function already has the check to see if the function we are looking at has a valid OCL builtin (See

if (OCLSPIRVBuiltinMap::find(Info.UniqName, &OC)) {
).
This check was missing in transAtomicBuiltin(...) and this PR adds that check. I do not see any other site where this check is required. So, I think this PR can be submitted as is.

Thanks for all the feedback.

@bwlodarcz
Copy link
Contributor

@asudarsa Unfortunately the change doesn't work/is not enough. Let's replace atomic_ with ndrange_ and we are entering again bogus state.

; Test that user defined/declared functions are not wrongly identified as OpenCL builtins.

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"

; Function Attrs: mustprogress norecurse nounwind
define weak_odr dso_local spir_kernel void @_ZTSN4test_ocl_builtin(ptr addrspace(4) noundef %arrayidx.i311) {
entry:
     %call.i312 = call spir_func noundef i32 @_Z28ndrange_fetch_and_sub_uint32Pii(ptr addrspace(4) noundef %arrayidx.i311, i32 noundef 1)
     %call.i313 = call spir_func noundef i32 @_Z28ndrange_fetch_and_add_uint32Pii(ptr addrspace(4) noundef %arrayidx.i311, i32 noundef 1)
     ret void
}

; Function Attrs: convergent inlinehint mustprogress norecurse nounwind
define linkonce_odr dso_local spir_func noundef i32 @_Z28ndrange_fetch_and_sub_uint32Pii(ptr addrspace(4) noundef %p, i32 noundef %x) {
entry:
  %call.i.i.i.i.i.i = tail call spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef %p, i32 noundef 5)
  %0 = addrspacecast ptr addrspace(1) %call.i.i.i.i.i.i to ptr addrspace(4)
  call spir_func void @__itt_offload_atomic_op_start(ptr addrspace(4) %0, i32 2, i32 0)
  %call3.i.i = tail call spir_func noundef i32 @_Z18__spirv_AtomicISubPU3AS1iN5__spv5Scope4FlagENS1_19MemorySemanticsMask4FlagEi(ptr addrspace(1) noundef %call.i.i.i.i.i.i, i32 noundef 1, i32 noundef 896, i32 noundef %x)
  %1 = addrspacecast ptr addrspace(1) %call.i.i.i.i.i.i to ptr addrspace(4)
  call spir_func void @__itt_offload_atomic_op_finish(ptr addrspace(4) %1, i32 2, i32 0)
  ret i32 %call3.i.i
}

declare dso_local spir_func noundef i32 @_Z28ndrange_fetch_and_add_uint32Pii(ptr addrspace(4) noundef %p, i32 noundef %x)

declare dso_local spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef, i32 noundef)

declare dso_local spir_func void @__itt_offload_atomic_op_start(ptr addrspace(4) noundef %object, i32 noundef %op_type, i32 noundef %mem_order)

declare dso_local spir_func noundef i32 @_Z18__spirv_AtomicISubPU3AS1iN5__spv5Scope4FlagENS1_19MemorySemanticsMask4FlagEi(ptr addrspace(1) noundef, i32 noundef, i32 noundef, i32 noundef)

declare dso_local spir_func void @__itt_offload_atomic_op_finish(ptr addrspace(4) noundef %object, i32 noundef %op_type, i32 noundef %mem_order)

!opencl.spir.version = !{!0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0}
!spirv.Source = !{!1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1}

!0 = !{i32 1, i32 2}
!1 = !{i32 4, i32 100000}

Standard llvm-as followed by llvm-spirv sequence to reproduce.
The test fails on assertions in debug in OCLToSPIRVBase::visitCallNDRange.

Copy link
Contributor

@bwlodarcz bwlodarcz left a comment

Choose a reason for hiding this comment

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

Described in previous comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants