Skip to content

Commit f7df54c

Browse files
authored
[SYCLomatic] Refine the migration of thrust::device_malloc_allocator (#537)
Signed-off-by: chenwei.sun <[email protected]>
1 parent 36c4127 commit f7df54c

File tree

7 files changed

+104
-9
lines changed

7 files changed

+104
-9
lines changed

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,14 @@ bool TypeInDeclRule::replaceTemplateSpecialization(
21542154
return false;
21552155

21562156
const std::string RealTypeNameStr(Start, TyLen);
2157+
2158+
if (DpctGlobalInfo::getUsmLevel() == UsmLevel::UL_None &&
2159+
RealTypeNameStr.find("device_malloc_allocator") != std::string::npos) {
2160+
report(BeginLoc, Diagnostics::KNOWN_UNSUPPORTED_TYPE, false,
2161+
RealTypeNameStr);
2162+
return true;
2163+
}
2164+
21572165
requestHelperFeatureForTypeNames(RealTypeNameStr, BeginLoc);
21582166
std::string Replacement =
21592167
MapNames::findReplacedName(MapNames::TypeNamesMap, RealTypeNameStr);

clang/lib/DPCT/MapNames.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ void MapNames::setExplicitNamespaceMap() {
248248
getDpctNamespace() + "device_vector",
249249
HelperFeatureEnum::DplExtrasVector_device_vector)},
250250
{"thrust::device_malloc_allocator",
251-
std::make_shared<TypeNameRule>(getClNamespace() + "buffer_allocator")},
251+
std::make_shared<TypeNameRule>(getDpctNamespace() + "deprecated::usm_device_allocator",
252+
HelperFeatureEnum::Memory_usm_device_allocator_alias)},
252253
{"thrust::maximum",
253254
std::make_shared<TypeNameRule>("oneapi::dpl::maximum")},
254255
{"thrust::multiplies", std::make_shared<TypeNameRule>("std::multiplies")},

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,15 @@ template <typename T>
19881988
using usm_host_allocator = detail::deprecated::usm_allocator<T, sycl::usm::alloc::host>;
19891989
// DPCT_LABEL_END
19901990

1991+
// DPCT_LABEL_BEGIN|usm_device_allocator_alias|dpct
1992+
// DPCT_DEPENDENCY_BEGIN
1993+
// Memory|dpct_usm_allocator
1994+
// Device|get_default_queue
1995+
// DPCT_DEPENDENCY_END
1996+
// DPCT_CODE
1997+
template <typename T>
1998+
using usm_device_allocator = detail::deprecated::usm_allocator<T, sycl::usm::alloc::shared>;
1999+
// DPCT_LABEL_END
19912000
} // namespace deprecated
19922001

19932002
// DPCT_LABEL_BEGIN|pointer_attributes|dpct

clang/test/dpct/helper_files_ref/include/memory.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,8 @@ namespace deprecated {
13091309
template <typename T>
13101310
using usm_host_allocator = detail::deprecated::usm_allocator<T, sycl::usm::alloc::host>;
13111311

1312+
template <typename T>
1313+
using usm_device_allocator = detail::deprecated::usm_allocator<T, sycl::usm::alloc::shared>;
13121314
} // namespace deprecated
13131315

13141316
class pointer_attributes {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: dpct --format-range=none --use-custom-helper=api -out-root %T/Memory/api_test46_out %s --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2+
// RUN: grep "IsCalled" %T/Memory/api_test46_out/MainSourceFiles.yaml | wc -l > %T/Memory/api_test46_out/count.txt
3+
// RUN: FileCheck --input-file %T/Memory/api_test46_out/count.txt --match-full-lines %s
4+
// RUN: rm -rf %T/Memory/api_test46_out
5+
6+
// CHECK: 21
7+
// TEST_FEATURE: Memory_usm_device_allocator_alias
8+
9+
#include <thrust/device_allocator.h>
10+
#include <thrust/device_malloc_allocator.h>
11+
#include <thrust/iterator/iterator_traits.h>
12+
13+
namespace cusp
14+
{
15+
template <typename T, typename MemorySpace>
16+
struct default_memory_allocator
17+
: thrust::device_malloc_allocator<T>
18+
{};
19+
}
20+
21+
int main(int argc, char *argv[]) {
22+
return 0;
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// UNSUPPORTED: cuda-8.0, cuda-10.1, cuda-10.2, cuda-11.0, cuda-11.1, cuda-11.2, cuda-11.3
2+
// UNSUPPORTED: v8.0, v10.1, v10.2, v11.0, v11.1, v11.2, v11.3
3+
// RUN: dpct --usm-level=none --format-range=none -out-root %T/thrust-for-h2o4gpu-specific-case-noneusm %s --cuda-include-path="%cuda-path/include" -- -std=c++14 -x cuda --cuda-host-only
4+
// RUN: FileCheck --input-file %T/thrust-for-h2o4gpu-specific-case-noneusm/thrust-for-h2o4gpu-specific-case-noneusm.dp.cpp --match-full-lines %s
5+
6+
// CHECK: #include <oneapi/dpl/execution>
7+
// CHECK-NEXT:#include <oneapi/dpl/algorithm>
8+
// CHECK-NEXT:#define DPCT_USM_LEVEL_NONE
9+
// CHECK-NEXT:#include <sycl/sycl.hpp>
10+
// CHECK-NEXT:#include <dpct/dpct.hpp>
11+
// CHECK-NEXT:#include <dpct/dpl_utils.hpp>
12+
#include <thrust/copy.h>
13+
#include <thrust/device_vector.h>
14+
#include <thrust/device_allocator.h>
15+
#include <thrust/device_malloc_allocator.h>
16+
#include <thrust/iterator/iterator_traits.h>
17+
18+
// CHECK: /*
19+
// CHECK-NEXT:DPCT1082:{{[0-9]+}}: Migration of thrust::device_malloc_allocator type is not supported.
20+
// CHECK-NEXT:*/
21+
// CHECK-NEXT:template <typename T> void foo_cpy(dpct::device_vector<T, thrust::device_malloc_allocator<T>> &Do, dpct::device_vector<T, thrust::device_malloc_allocator<T>> &Di) {
22+
// CHECK-NEXT: return;
23+
// CHECK-NEXT:}
24+
template <typename T> void foo_cpy(thrust::device_vector<T, thrust::device_malloc_allocator<T>> &Do, thrust::device_vector<T, thrust::device_malloc_allocator<T>> &Di) {
25+
return;
26+
}
27+
28+
29+
void foo_1() {
30+
// CHECK: /*
31+
// CHECK-NEXT: DPCT1082:{{[0-9]+}}: Migration of thrust::device_malloc_allocator type is not supported.
32+
// CHECK-NEXT: */
33+
// CHECK-NEXT: dpct::device_vector<int, thrust::device_malloc_allocator<int>> **tt=NULL;
34+
// CHECK-NEXT: /*
35+
// CHECK-NEXT: DPCT1082:{{[0-9]+}}: Migration of thrust::device_malloc_allocator type is not supported.
36+
// CHECK-NEXT: */
37+
// CHECK-NEXT: dpct::device_vector<int, thrust::device_malloc_allocator<int>> *dd[10];
38+
// CHECK-NEXT: foo_cpy(*tt[0], *dd[0]);
39+
thrust::device_vector<int, thrust::device_malloc_allocator<int>> **tt=NULL;
40+
thrust::device_vector<int, thrust::device_malloc_allocator<int>> *dd[10];
41+
foo_cpy(*tt[0], *dd[0]);
42+
}
43+
44+
// CHECK:/*
45+
// CHECK-NEXT:DPCT1082:{{[0-9]+}}: Migration of thrust::device_malloc_allocator type is not supported.
46+
// CHECK-NEXT:*/
47+
namespace foo_struct { template <typename T, typename MemorySpace> struct default_memory_allocator : thrust::device_malloc_allocator<T>{};}
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// UNSUPPORTED: cuda-8.0, cuda-10.1, cuda-10.2, cuda-11.0, cuda-11.1, cuda-11.2, cuda-11.3, cuda-11.4, cuda-11.5, cuda-11.6, cuda-11.7, cuda-11.8, cuda-12.0, cuda-12.1
2-
// UNSUPPORTED: v8.0, v10.1, v10.2, v11.0, v11.1, v11.2, v11.3, v11.4, v11.5, v11.6, v11.7, v11.8, v12.0, v12.1
1+
// UNSUPPORTED: cuda-8.0, cuda-10.1, cuda-10.2, cuda-11.0, cuda-11.1, cuda-11.2, cuda-11.3
2+
// UNSUPPORTED: v8.0, v10.1, v10.2, v11.0, v11.1, v11.2, v11.3
33
// RUN: dpct --format-range=none -out-root %T/thrust-for-h2o4gpu-specific-case %s --cuda-include-path="%cuda-path/include" -- -std=c++14 -x cuda --cuda-host-only
44
// RUN: FileCheck --input-file %T/thrust-for-h2o4gpu-specific-case/thrust-for-h2o4gpu-specific-case.dp.cpp --match-full-lines %s
55

@@ -11,24 +11,29 @@
1111
// CHECK-NEXT: #include <dpct/dpl_utils.hpp>
1212
#include <thrust/copy.h>
1313
#include <thrust/device_vector.h>
14+
#include <thrust/device_allocator.h>
15+
#include <thrust/device_malloc_allocator.h>
16+
#include <thrust/iterator/iterator_traits.h>
1417

15-
//CHECK: template <typename T>
16-
//CHECK-NEXT:void foo_cpy(dpct::device_vector<T, sycl::buffer_allocator<T>> &Do, dpct::device_vector<T, sycl::buffer_allocator<T>> &Di) {
18+
//CHECK:template <typename T>
19+
//CHECK-NEXT:void foo_cpy(dpct::device_vector<T, dpct::deprecated::usm_device_allocator<T>> &Do, dpct::device_vector<T, dpct::deprecated::usm_device_allocator<T>> &Di) {
1720
//CHECK-NEXT: return;
1821
//CHECK-NEXT:}
1922
template <typename T>
2023
void foo_cpy(thrust::device_vector<T, thrust::device_malloc_allocator<T>> &Do, thrust::device_vector<T, thrust::device_malloc_allocator<T>> &Di) {
2124
return;
2225
}
2326

24-
//CHECK: void foo_1() {
25-
//CHECK-NEXT: dpct::device_vector<int, sycl::buffer_allocator<int>> **tt=NULL;
26-
//CHECK-NEXT: dpct::device_vector<int, sycl::buffer_allocator<int>> *dd[10];
27-
//CHECK-NEXT: foo_cpy(*tt[0], *dd[0]);
27+
//CHECK:void foo_1() {
28+
//CHECK-NEXT: dpct::device_vector<int, dpct::deprecated::usm_device_allocator<int>> **tt=NULL;
29+
//CHECK-NEXT: dpct::device_vector<int, dpct::deprecated::usm_device_allocator<int>> *dd[10];
30+
//CHECK-NEXT: foo_cpy(*tt[0], *dd[0]);
2831
//CHECK-NEXT:}
2932
void foo_1() {
3033
thrust::device_vector<int, thrust::device_malloc_allocator<int>> **tt=NULL;
3134
thrust::device_vector<int, thrust::device_malloc_allocator<int>> *dd[10];
3235
foo_cpy(*tt[0], *dd[0]);
3336
}
3437

38+
//CHECK:namespace foo_struct { template <typename T, typename MemorySpace> struct default_memory_allocator : dpct::deprecated::usm_device_allocator<T>{};}
39+
namespace foo_struct { template <typename T, typename MemorySpace> struct default_memory_allocator : thrust::device_malloc_allocator<T>{};}

0 commit comments

Comments
 (0)