Skip to content

Commit cc06cc1

Browse files
authored
Merge pull request #2350 from pbalcer/fix-get-native-mem
[L0 v2] fix urMemGetNativeHandle
2 parents 328ea3f + c85e8c0 commit cc06cc1

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

source/adapters/level_zero/v2/memory.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ void *ur_discrete_mem_handle_t::getDevicePtr(
260260
std::ignore = access;
261261
std::ignore = size;
262262
std::ignore = migrate;
263-
264263
if (!activeAllocationDevice) {
265264
if (!hDevice) {
266265
hDevice = hContext->getDevices()[0];
@@ -269,6 +268,10 @@ void *ur_discrete_mem_handle_t::getDevicePtr(
269268
allocateOnDevice(hDevice, getSize());
270269
}
271270

271+
if (!hDevice) {
272+
hDevice = activeAllocationDevice;
273+
}
274+
272275
char *ptr;
273276
if (activeAllocationDevice == hDevice) {
274277
ptr = ur_cast<char *>(deviceAllocations[hDevice->Id.value()].get());
@@ -559,12 +562,10 @@ ur_result_t urMemRelease(ur_mem_handle_t hMem) try {
559562
ur_result_t urMemGetNativeHandle(ur_mem_handle_t hMem,
560563
ur_device_handle_t hDevice,
561564
ur_native_handle_t *phNativeMem) try {
562-
std::ignore = hDevice;
563-
564565
std::scoped_lock<ur_shared_mutex> lock(hMem->getMutex());
565566

566567
auto ptr = hMem->getDevicePtr(
567-
nullptr, ur_mem_handle_t_::device_access_mode_t::read_write, 0,
568+
hDevice, ur_mem_handle_t_::device_access_mode_t::read_write, 0,
568569
hMem->getSize(), nullptr);
569570
*phNativeMem = reinterpret_cast<ur_native_handle_t>(ptr);
570571
return UR_RESULT_SUCCESS;

test/conformance/memory/memory_adapter_level_zero_v2.match

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
{{OPT}}urMemImageGetInfoTest.InvalidSizeSmall/*
1111
{{OPT}}urMemImageGetInfoTest.InvalidNullPointerParamValue/*
1212
{{OPT}}urMemImageGetInfoTest.InvalidNullPointerPropSizeRet/*
13-
{{OPT}}urMemBufferCreateWithNativeHandleTest.Success/*
14-
{{OPT}}urMemBufferCreateWithNativeHandleTest.SuccessWithOwnedNativeHandle/*
15-
{{OPT}}urMemBufferCreateWithNativeHandleTest.SuccessWithUnOwnedNativeHandle/*
16-
{{OPT}}urMemBufferCreateWithNativeHandleTest.InvalidNullHandle/*
17-
{{OPT}}urMemBufferCreateWithNativeHandleTest.InvalidNullPointer/*
1813
{{OPT}}urMemImageCreateWithNativeHandleTest.Success/*
1914
{{OPT}}urMemImageCreateWithNativeHandleTest.InvalidNullHandle/*
2015
{{OPT}}urMemImageCreateWithNativeHandleTest.InvalidNullPointer/*

test/conformance/memory/urMemGetNativeHandle.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
33
// See LICENSE.TXT
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#include "ur_api.h"
6+
#include <gtest/gtest.h>
57
#include <uur/fixtures.h>
68

79
using urMemGetNativeHandleTest = uur::urMemBufferTest;
@@ -14,6 +16,26 @@ TEST_P(urMemGetNativeHandleTest, Success) {
1416
}
1517
}
1618

19+
TEST_P(urMemGetNativeHandleTest, SuccessNullDeviceTwice) {
20+
ur_native_handle_t hNativeMem = 0;
21+
if (auto error = urMemGetNativeHandle(buffer, nullptr, &hNativeMem)) {
22+
ASSERT_TRUE(error == UR_RESULT_ERROR_UNSUPPORTED_FEATURE ||
23+
error == UR_RESULT_ERROR_INVALID_NULL_HANDLE);
24+
}
25+
if (auto error = urMemGetNativeHandle(buffer, nullptr, &hNativeMem)) {
26+
ASSERT_TRUE(error == UR_RESULT_ERROR_UNSUPPORTED_FEATURE ||
27+
error == UR_RESULT_ERROR_INVALID_NULL_HANDLE);
28+
}
29+
}
30+
31+
TEST_P(urMemGetNativeHandleTest, SuccessNullDevice) {
32+
ur_native_handle_t hNativeMem = 0;
33+
if (auto error = urMemGetNativeHandle(buffer, nullptr, &hNativeMem)) {
34+
ASSERT_TRUE(error == UR_RESULT_ERROR_UNSUPPORTED_FEATURE ||
35+
error == UR_RESULT_ERROR_INVALID_NULL_HANDLE);
36+
}
37+
}
38+
1739
TEST_P(urMemGetNativeHandleTest, InvalidNullHandleMem) {
1840
ur_native_handle_t phNativeMem;
1941
ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_HANDLE,

0 commit comments

Comments
 (0)