Skip to content

Commit 05737b0

Browse files
kswiecickibb-ur
authored andcommitted
Implement exp-usm-host-alloc-register.yml spec (#21062)
Depends on: intel/llvm#21049.
1 parent 00b7b95 commit 05737b0

4 files changed

Lines changed: 103 additions & 5 deletions

File tree

source/adapters/level_zero/v2/usm.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,14 +845,28 @@ ur_result_t UR_APICALL urUSMContextMemcpyExp(ur_context_handle_t hContext,
845845
}
846846

847847
ur_result_t urUSMHostAllocRegisterExp(
848-
ur_context_handle_t /*hContext*/, void * /*pHostMem*/, size_t /*size*/,
848+
ur_context_handle_t hContext, void *pHostMem, size_t size,
849849
const ur_exp_usm_host_alloc_register_properties_t * /*pProperties*/) {
850-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
850+
ze_external_memmap_sysmem_ext_desc_t sysMemDesc = {
851+
ZE_STRUCTURE_TYPE_EXTERNAL_MEMMAP_SYSMEM_EXT_DESC, nullptr, pHostMem,
852+
size};
853+
854+
ze_host_mem_alloc_desc_t hostDesc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC,
855+
&sysMemDesc, 0};
856+
857+
void *mappedMem = nullptr;
858+
ZE2UR_CALL(zeMemAllocHost,
859+
(hContext->getZeHandle(), &hostDesc, size, 1, &mappedMem));
860+
assert(mappedMem == pHostMem);
861+
862+
return UR_RESULT_SUCCESS;
851863
}
852864

853-
ur_result_t urUSMHostAllocUnregisterExp(ur_context_handle_t /*hContext*/,
854-
void * /*pHostMem*/) {
855-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
865+
ur_result_t urUSMHostAllocUnregisterExp(ur_context_handle_t hContext,
866+
void *pHostMem) {
867+
ZE2UR_CALL(zeMemFree, (hContext->getZeHandle(), pHostMem));
868+
869+
return UR_RESULT_SUCCESS;
856870
}
857871

858872
} // namespace ur::level_zero

test/conformance/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ add_subdirectory(sampler)
7171
add_subdirectory(virtual_memory)
7272
add_subdirectory(exp_usm_context_memcpy)
7373
add_subdirectory(exp_graph)
74+
add_subdirectory(exp_usm_host_mem_register)
7475

7576
set(TEST_SUBDIRECTORIES_DPCXX
7677
"device_code"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (C) 2026 Intel Corporation
2+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
# See LICENSE.TXT
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
add_conformance_devices_test(exp_usm_host_mem_register
7+
urUSMHostMemRegister.cpp)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (C) 2026 Intel Corporation
2+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
3+
// Exceptions. See LICENSE.TXT
4+
//
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
7+
#include "uur/fixtures.h"
8+
9+
struct urUSMHostMemRegisterTest : uur::urQueueTest {
10+
static constexpr size_t allocSize = (1 << 12); // 4KB
11+
static constexpr uint8_t testValue = 0x77;
12+
void *alloc = nullptr;
13+
14+
void SetUp() override {
15+
UUR_RETURN_ON_FATAL_FAILURE(urQueueTest::SetUp());
16+
17+
UUR_KNOWN_FAILURE_ON(uur::CUDA{}, uur::HIP{}, uur::NativeCPU{},
18+
uur::OpenCL{}, uur::LevelZero{});
19+
20+
alloc = aligned_alloc(allocSize, allocSize);
21+
ASSERT_NE(alloc, nullptr);
22+
}
23+
24+
void TearDown() override {
25+
if (alloc) {
26+
free(alloc);
27+
}
28+
urQueueTest::TearDown();
29+
}
30+
31+
void validateBuffer(const void *buffer, const size_t size,
32+
const uint8_t expectedValue) {
33+
const uint8_t *byteBuffer = static_cast<const uint8_t *>(buffer);
34+
for (size_t i = 0; i < size; ++i) {
35+
ASSERT_EQ(byteBuffer[i], expectedValue)
36+
<< "Buffer mismatch at index " << i << ": expected "
37+
<< static_cast<int>(expectedValue) << ", got "
38+
<< static_cast<int>(byteBuffer[i]);
39+
}
40+
}
41+
};
42+
43+
UUR_INSTANTIATE_DEVICE_TEST_SUITE(urUSMHostMemRegisterTest);
44+
45+
TEST_P(urUSMHostMemRegisterTest, Success) {
46+
ASSERT_SUCCESS(urUSMHostAllocRegisterExp(context, alloc, allocSize, nullptr));
47+
48+
void *alloc2 = nullptr;
49+
ASSERT_SUCCESS(urUSMHostAlloc(context, nullptr, nullptr, allocSize, &alloc2));
50+
51+
memset(alloc, testValue, allocSize);
52+
memset(alloc2, 0, allocSize);
53+
54+
ASSERT_SUCCESS(urEnqueueUSMMemcpy(queue, true, alloc2, alloc, allocSize, 0,
55+
nullptr, nullptr));
56+
57+
validateBuffer(alloc2, allocSize, testValue);
58+
59+
ASSERT_SUCCESS(urUSMHostAllocUnregisterExp(context, alloc));
60+
ASSERT_SUCCESS(urUSMFree(context, alloc2));
61+
}
62+
63+
TEST_P(urUSMHostMemRegisterTest, InvalidNullHandleContext) {
64+
ASSERT_EQ(urUSMHostAllocRegisterExp(nullptr, alloc, allocSize, nullptr),
65+
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
66+
}
67+
68+
TEST_P(urUSMHostMemRegisterTest, InvalidValueAllocSize) {
69+
ASSERT_EQ(urUSMHostAllocRegisterExp(context, alloc, 0, nullptr),
70+
UR_RESULT_ERROR_INVALID_VALUE);
71+
}
72+
73+
TEST_P(urUSMHostMemRegisterTest, InvalidNullPointerHostMem) {
74+
ASSERT_EQ(urUSMHostAllocRegisterExp(context, nullptr, allocSize, nullptr),
75+
UR_RESULT_ERROR_INVALID_NULL_POINTER);
76+
}

0 commit comments

Comments
 (0)