Skip to content

Commit ff80033

Browse files
authored
[SYCLomatic] Support the cuPointerGetAttribute API migration when USM is none(#2737)
Signed-off-by: Chen, Sheng S <[email protected]>
1 parent 32bd65d commit ff80033

File tree

9 files changed

+60
-8
lines changed

9 files changed

+60
-8
lines changed

clang/lib/DPCT/RuleInfra/MapNames.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,9 +1226,6 @@ void MapNames::setExplicitNamespaceMap(
12261226
{"CU_POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE",
12271227
std::make_shared<EnumNameRule>(getDpctNamespace() +
12281228
"pointer_attributes::type::unsupported")},
1229-
{"CU_POINTER_ATTRIBUTE_RANGE_START_ADDR",
1230-
std::make_shared<EnumNameRule>(getDpctNamespace() +
1231-
"pointer_attributes::type::unsupported")},
12321229
{"CU_POINTER_ATTRIBUTE_RANGE_SIZE",
12331230
std::make_shared<EnumNameRule>(getDpctNamespace() +
12341231
"pointer_attributes::type::unsupported")},

clang/lib/DPCT/RulesLang/APINamesMemory.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,20 @@ ASSIGNABLE_FACTORY(
440440
"cuPointerGetAttributes", CALL(MapNames::getDpctNamespace() + "pointer_attributes::get",
441441
ARG(0), ARG(1), ARG(2), ARG(3)))))
442442

443+
ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
444+
CheckEnumArgStr(1, "CU_POINTER_ATTRIBUTE_RANGE_START_ADDR"),
445+
CONDITIONAL_FACTORY_ENTRY(
446+
checkIsUSM(),
447+
UNSUPPORT_FACTORY_ENTRY("cuPointerGetAttribute",
448+
Diagnostics::UNSUPPORTED_PARAM, ARG(1)),
449+
FEATURE_REQUEST_FACTORY(
450+
HelperFeatureEnum::device_ext,
451+
ASSIGN_FACTORY_ENTRY(
452+
"cuPointerGetAttribute", ARG(0),
453+
CALL(MapNames::getDpctNamespace() + "get_base_addr", ARG(2))))),
454+
UNSUPPORT_FACTORY_ENTRY("cuPointerGetAttribute",
455+
Diagnostics::UNSUPPORTED_PARAM, ARG(1))))
456+
443457
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
444458
HelperFeatureEnum::device_ext,
445459
CONDITIONAL_FACTORY_ENTRY(

clang/lib/DPCT/RulesLang/RewriterSYCLcompat.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ SYCLCOMPAT_UNSUPPORT("cub::StoreDirectWarpStriped")
7777
SYCLCOMPAT_UNSUPPORT("cub::ShuffleDown")
7878
SYCLCOMPAT_UNSUPPORT("cub::ShuffleUp")
7979
SYCLCOMPAT_UNSUPPORT("cuPointerGetAttributes")
80+
SYCLCOMPAT_UNSUPPORT("cuPointerGetAttribute")
8081
});
8182
// clang-format on
8283
initRewriterMethodMapCooperativeGroupsSYCLcompat(RewriterMap);

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,10 +2183,10 @@ void FunctionCallRule::registerMatcher(MatchFinder &MF) {
21832183
"cudaRuntimeGetVersion", "clock64", "__nanosleep",
21842184
"cudaFuncSetSharedMemConfig", "cuFuncSetCacheConfig",
21852185
"cudaPointerGetAttributes", "cuPointerGetAttributes",
2186-
"cuCtxSetCacheConfig", "cuCtxSetLimit", "cudaCtxResetPersistingL2Cache",
2187-
"cuCtxResetPersistingL2Cache", "cudaStreamSetAttribute",
2188-
"cudaStreamGetAttribute", "cudaProfilerStart", "cudaProfilerStop",
2189-
"__trap", "cuCtxEnablePeerAccess");
2186+
"cuPointerGetAttribute", "cuCtxSetCacheConfig", "cuCtxSetLimit",
2187+
"cudaCtxResetPersistingL2Cache", "cuCtxResetPersistingL2Cache",
2188+
"cudaStreamSetAttribute", "cudaStreamGetAttribute", "cudaProfilerStart",
2189+
"cudaProfilerStop", "__trap", "cuCtxEnablePeerAccess");
21902190
};
21912191

21922192
MF.addMatcher(

clang/lib/DPCT/SrcAPI/APINames.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ ENTRY(cuMemPrefetchAsync, cuMemPrefetchAsync, true, NO_FLAG, P4, "Partial: USM o
17651765
ENTRY(cuMemPrefetchAsync_v2, cuMemPrefetchAsync_v2, false, NO_FLAG, P4, "Partial: USM only, DPCT1007")
17661766
ENTRY(cuMemRangeGetAttribute, cuMemRangeGetAttribute, false, NO_FLAG, P4, "comment")
17671767
ENTRY(cuMemRangeGetAttributes, cuMemRangeGetAttributes, false, NO_FLAG, P4, "comment")
1768-
ENTRY(cuPointerGetAttribute, cuPointerGetAttribute, false, NO_FLAG, P4, "comment")
1768+
ENTRY(cuPointerGetAttribute, cuPointerGetAttribute, true, NO_FLAG, P4, "Partial: none USM for start address API, DPCT1007.")
17691769
ENTRY(cuPointerGetAttributes, cuPointerGetAttributes, true, NO_FLAG, P4, "Partial: Only 5 attributes are supported")
17701770
ENTRY(cuPointerSetAttribute, cuPointerSetAttribute, false, NO_FLAG, P4, "comment")
17711771

clang/runtime/dpct-rt/include/dpct/detail/memory_detail.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class mem_mgr {
124124
return it->second;
125125
}
126126

127+
void *get_base_addr(const void *ptr) {
128+
allocation alloc = translate_ptr(ptr);
129+
return alloc.alloc_ptr;
130+
}
131+
127132
/// Check if the pointer represents device pointer or not.
128133
bool is_device_ptr(const void *ptr) const {
129134
std::lock_guard<std::mutex> lock(m_mutex);

clang/runtime/dpct-rt/include/dpct/memory.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ static inline bool is_device_ptr(T ptr) {
124124
}
125125
return false;
126126
}
127+
/// Get the base address of the allocated memory for the given pointer \p ptr.
128+
template <class T> static inline void *get_base_addr(T ptr) {
129+
if constexpr (std::is_pointer<T>::value) {
130+
return detail::mem_mgr::instance().get_base_addr(ptr);
131+
}
132+
return nullptr;
133+
}
127134
#endif
128135

129136
/// Get the buffer and the offset of a piece of memory pointed to by \p ptr.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// UNSUPPORTED: cuda-8.0, cuda-9.0, cuda-9.1, cuda-9.2
2+
// UNSUPPORTED: v8.0, v9.0, v9.1, v9.2
3+
// RUN: dpct --format-range=none --usm-level=restricted -out-root %T/device_prop %s --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only -std=c++11
4+
// RUN: FileCheck --match-full-lines --input-file %T/device_prop/cudaGetPointer.dp.cpp %s
5+
6+
7+
#include <cuda.h>
8+
void test_attribute() {
9+
void *base_ptr;
10+
void *ptr;
11+
// CHECK:/*
12+
// CHECK-NEXT:DPCT1067:{{[0-9]+}}: The 'CU_POINTER_ATTRIBUTE_RANGE_START_ADDR' parameter could not be migrated. You may need to update the code manually.
13+
// CHECK-NEXT:*/
14+
if (cuPointerGetAttribute(base_ptr, CU_POINTER_ATTRIBUTE_RANGE_START_ADDR, (CUdeviceptr)ptr) != CUDA_SUCCESS);
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// UNSUPPORTED: cuda-8.0, cuda-9.0, cuda-9.1, cuda-9.2
2+
// UNSUPPORTED: v8.0, v9.0, v9.1, v9.2
3+
// RUN: dpct --format-range=none --usm-level=none -out-root %T/device_prop %s --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only -std=c++11
4+
// RUN: FileCheck --match-full-lines --input-file %T/device_prop/cudaGetPointer_none_usm.dp.cpp %s
5+
// RUN: %if build_lit %{icpx -c -fsycl %T/device_prop/cudaGetPointer_none_usm.dp.cpp -o %T/device_prop/cudaGetPointer_none_usm.dp.o %}
6+
7+
#include <cuda.h>
8+
void test_attribute() {
9+
void *base_ptr;
10+
void *ptr;
11+
//CHECK: if (DPCT_CHECK_ERROR(base_ptr = dpct::get_base_addr((dpct::device_ptr)ptr)) != 0);
12+
if (cuPointerGetAttribute(base_ptr, CU_POINTER_ATTRIBUTE_RANGE_START_ADDR, (CUdeviceptr)ptr) != CUDA_SUCCESS);
13+
}

0 commit comments

Comments
 (0)