Skip to content

[SYCL][HIP] Use valid cast for CUdeviceptr in HIP PI #6207

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
Jun 3, 2022
Merged
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
19 changes: 19 additions & 0 deletions sycl/plugins/hip/pi_hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2131,8 +2131,27 @@ pi_result hip_piMemGetInfo(pi_mem memObj, pi_mem_info queriedInfo,
/// \return PI_SUCCESS
pi_result hip_piextMemGetNativeHandle(pi_mem mem,
pi_native_handle *nativeHandle) {
#if defined(__HIP_PLATFORM_NVIDIA__)
if (sizeof(_pi_mem::mem_::buffer_mem_::native_type) >
sizeof(pi_native_handle)) {
// Check that all the upper bits that cannot be represented by
// pi_native_handle are empty.
// NOTE: The following shift might trigger a warning, but the check in the
// if above makes sure that this does not underflow.
_pi_mem::mem_::buffer_mem_::native_type upperBits =
mem->mem_.buffer_mem_.get() >> (sizeof(pi_native_handle) * CHAR_BIT);
if (upperBits) {
// Return an error if any of the remaining bits is non-zero.
return PI_INVALID_MEM_OBJECT;
}
}
*nativeHandle = static_cast<pi_native_handle>(mem->mem_.buffer_mem_.get());
Copy link
Contributor

Choose a reason for hiding this comment

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

From the issue:

Replacing the reinterpret_cast by static_cast avoids the compilation error, but might be unsafe due to narrowing.

Can we have a check that upper bits are zero and return some failure code if that's not the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @aelovikov-intel, thanks for the review.

I've added a check to make sure that the upper bits are zero.

#elif defined(__HIP_PLATFORM_AMD__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the code above not suitable for __HIP_PLATFORM_AMD__ ?

Copy link
Contributor

Choose a reason for hiding this comment

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

can you approve the workflow again, please?

My outlook filters for github notifications aren't perfect yet and different notifications end up in different folders. Just saw your comment.

I'd like to understand what exactly is wrong with the AMD platform because it's not obvious to me. The upperbits handling looks like the right thing to me regardless of the problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI. I approved the workflow both times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bader Thanks for approving the workflows! I'm not sure why the second run failed, I don't see how my changes could affect a reduction test on OpenCL on Intel GPU.

@aelovikov-intel: The definition of hipDeviceptr_t (which is the same as native_type, the return of mem->mem_buffer_mem_.get()) is different on Nvidia and AMD platforms.

On Nvidia/CUDA platforms, it is an alias for CUdeviceptr, which is unsigned long long, so a static_cast is required to convert it to pi_native_handle (unsigned long).

On AMD platforms, it is an alias for void*, and static_cast of void* to unsigned long are invalid, so a reinterpret_cast is required on the AMD platform (and no checking of upper bits).

Copy link
Contributor

Choose a reason for hiding this comment

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

And such a cast would error if information would be lost: https://godbolt.org/z/dj8TKhrnM

That makes sense, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aelovikov-intel Thanks for the approval!

What should I do about the failed workflow? I do not see any relation between the error and my changes, would it make sense to re-run the workflow? Or can the PR still be merged?

*nativeHandle =
reinterpret_cast<pi_native_handle>(mem->mem_.buffer_mem_.get());
#else
#error("Must define exactly one of __HIP_PLATFORM_AMD__ or __HIP_PLATFORM_NVIDIA__");
#endif
return PI_SUCCESS;
}

Expand Down