@@ -66,7 +66,6 @@ ur_integrated_buffer_handle_t::ur_integrated_buffer_handle_t(
6666 if (ret == UR_RESULT_SUCCESS && memProps.type != ZE_MEMORY_TYPE_UNKNOWN) {
6767 // Already a USM allocation - just use it directly without import
6868 this ->ptr = usm_unique_ptr_t (hostPtr, [](void *) {});
69- // No copy-back needed for USM pointers
7069 return ;
7170 }
7271
@@ -134,16 +133,52 @@ void *ur_integrated_buffer_handle_t::getDevicePtr(
134133}
135134
136135void *ur_integrated_buffer_handle_t ::mapHostPtr(
137- ur_map_flags_t /* flags*/ , size_t offset, size_t /* size */ ,
136+ ur_map_flags_t flags, size_t offset, size_t mapSize ,
138137 ze_command_list_handle_t /* cmdList*/ , wait_list_view & /* waitListView*/ ) {
139- // For integrated devices, both device and host access the same memory
138+ if (writeBackPtr) {
139+ // Copy-back path: user gets back their original pointer
140+ void *mappedPtr = ur_cast<char *>(writeBackPtr) + offset;
141+
142+ if (flags & UR_MAP_FLAG_READ) {
143+ std::memcpy (mappedPtr, ur_cast<char *>(ptr.get ()) + offset, mapSize);
144+ }
145+
146+ // Track this mapping for unmap
147+ mappedRegions.emplace_back (
148+ usm_unique_ptr_t (mappedPtr, [](void *) {}), mapSize, offset, flags);
149+
150+ return mappedPtr;
151+ }
152+
153+ // Zero-copy path: for successfully imported or USM pointers
140154 return ur_cast<char *>(ptr.get ()) + offset;
141155}
142156
143157void ur_integrated_buffer_handle_t::unmapHostPtr (
144- void * /* pMappedPtr*/ , ze_command_list_handle_t /* cmdList*/ ,
158+ void *pMappedPtr, ze_command_list_handle_t /* cmdList*/ ,
145159 wait_list_view & /* waitListView*/ ) {
146- // No-op: integrated buffers use zero-copy, no synchronization needed
160+ if (writeBackPtr) {
161+ // Copy-back path: find the mapped region and copy data back if needed
162+ auto mappedRegion =
163+ std::find_if (mappedRegions.begin (), mappedRegions.end (),
164+ [pMappedPtr](const host_allocation_desc_t &desc) {
165+ return desc.ptr .get () == pMappedPtr;
166+ });
167+
168+ if (mappedRegion == mappedRegions.end ()) {
169+ UR_DFAILURE (" could not find pMappedPtr:" << pMappedPtr);
170+ throw UR_RESULT_ERROR_INVALID_ARGUMENT;
171+ }
172+
173+ if (mappedRegion->flags & (UR_MAP_FLAG_WRITE | UR_MAP_FLAG_WRITE_INVALIDATE_REGION)) {
174+ std::memcpy (ur_cast<char *>(ptr.get ()) + mappedRegion->offset ,
175+ mappedRegion->ptr .get (), mappedRegion->size );
176+ }
177+
178+ mappedRegions.erase (mappedRegion);
179+ return ;
180+ }
181+ // No op for zero-copy path, memory is synced
147182}
148183
149184static v2::raii::command_list_unique_handle
0 commit comments