Skip to content

Commit c541cf0

Browse files
authored
[SYCLomatic] Support query CUDA syntax sugar of --query-api-mapping. (#2376)
Signed-off-by: Tang, Jiajun [email protected]
1 parent 234abeb commit c541cf0

File tree

10 files changed

+147
-24
lines changed

10 files changed

+147
-24
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Start
2+
__constant__ int v;
3+
// End
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Start
2+
__device__ int v;
3+
__device__ void f() {}
4+
// End
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Start
2+
__global__ void f() {}
3+
// End
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Start
2+
__host__ void f() {}
3+
// End
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Start
2+
__managed__ int v;
3+
// End
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Start
2+
__global__ void f() { __shared__ int v; }
3+
// End
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__global__ void f() {}
2+
3+
void test() {
4+
dim3 gridDim, blockDim;
5+
// Start
6+
f<<<gridDim, blockDim>>>();
7+
// End
8+
}

clang/lib/DPCT/APIMapping/QueryAPIMapping.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "QueryAPIMapping.h"
10+
#include "llvm/ADT/Twine.h"
1011
#include "llvm/Support/raw_ostream.h"
1112
#include <unordered_map>
1213

@@ -27,34 +28,32 @@ void APIMapping::registerEntry(std::string Name, llvm::StringRef SourceCode) {
2728
const auto TargetIndex = EntryArray.size();
2829
EntryMap[Name] = TargetIndex; // Set the entry whether it exist or not.
2930
// Try to fuzz the original API name (only when the entry not exist):
30-
// 1. Remove partial or all leading '_'.
31-
// 2. For each name got by step 1, put 4 kind of fuzzed name into the map
31+
// 1. Change "Name" to lower case. (Querying will change "Key" to lower too)
32+
// 2. Remove partial or all suffix '_'.
33+
std::transform(Name.begin(), Name.end(), Name.begin(), ::tolower);
34+
while (Name.back() == '_') {
35+
Name.erase(Name.end() - 1);
36+
EntryMap.try_emplace(Name, TargetIndex);
37+
}
38+
const auto EmplaceWithAndWithoutSuffix = [TargetIndex](
39+
llvm::StringRef Name,
40+
llvm::StringRef Suffix) {
41+
EntryMap.try_emplace(Name.str(), TargetIndex);
42+
if (Name.take_back(Suffix.size()) == Suffix) {
43+
EntryMap.try_emplace(Name.drop_back(Suffix.size()).str(), TargetIndex);
44+
} else {
45+
EntryMap.try_emplace(llvm::Twine(Name).concat(Suffix).str(), TargetIndex);
46+
}
47+
};
48+
// 3. Remove partial or all leading '_'.
49+
// 4. For each name got by step 1, put 2 kind of fuzzed name into the map
3250
// keys:
3351
// (1) original name
3452
// (2) remove or add Suffix "_v2"
35-
// (3) first char upper case name
36-
// (4) all char upper case name
37-
// (5) all char lower case name
38-
for (int i = Name.find_first_not_of("_"); i >= 0; --i) {
39-
auto TempName = Name;
40-
std::string Suffix = "_v2";
41-
if (TempName.size() > Suffix.length() &&
42-
TempName.substr(TempName.size() - Suffix.length()) == Suffix) {
43-
EntryMap.try_emplace(TempName.substr(0, TempName.size() - 3),
44-
TargetIndex);
45-
} else {
46-
EntryMap.try_emplace(TempName + Suffix, TargetIndex);
47-
}
48-
TempName[i] = std::toupper(TempName[i]);
49-
EntryMap.try_emplace(TempName, TargetIndex);
50-
std::transform(TempName.begin(), TempName.end(), TempName.begin(),
51-
::toupper);
52-
EntryMap.try_emplace(TempName, TargetIndex);
53-
std::transform(TempName.begin(), TempName.end(), TempName.begin(),
54-
::tolower);
55-
EntryMap.try_emplace(TempName, TargetIndex);
53+
EmplaceWithAndWithoutSuffix(Name, "_v2");
54+
while (Name.front() == '_') {
5655
Name.erase(0, 1);
57-
EntryMap.try_emplace(Name, TargetIndex);
56+
EmplaceWithAndWithoutSuffix(Name, "_v2");
5857
}
5958
EntryArray.emplace_back(SourceCode);
6059
}
@@ -68,6 +67,10 @@ llvm::StringRef APIMapping::getAPISourceCode(std::string Key) {
6867
Key.erase(Key.find_last_not_of(" ") + 1);
6968
auto Iter = EntryMap.find(Key);
7069
if (Iter == EntryMap.end()) {
70+
if (Key.find('<') != std::string::npos ||
71+
Key.find('>') != std::string::npos) {
72+
Key = "kernel";
73+
}
7174
std::transform(Key.begin(), Key.end(), Key.begin(), ::tolower);
7275
Iter = EntryMap.find(Key);
7376
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=kernel | FileCheck %s -check-prefix=KERNEL
2+
3+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping="<<<>>>" | FileCheck %s -check-prefix=KERNEL
4+
5+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping="<<<" | FileCheck %s -check-prefix=KERNEL
6+
7+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=">>>" | FileCheck %s -check-prefix=KERNEL
8+
9+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping="kernel<<<...>>>" | FileCheck %s -check-prefix=KERNEL
10+
11+
// KERNEL: CUDA API:
12+
// KERNEL-NEXT: f<<<gridDim, blockDim>>>();
13+
// KERNEL-NEXT: Is migrated to:
14+
// KERNEL-NEXT: dpct::get_in_order_queue().parallel_for(
15+
// KERNEL-NEXT: sycl::nd_range<3>(gridDim * blockDim, blockDim),
16+
// KERNEL-NEXT: [=](sycl::nd_item<3> item_ct1) {
17+
// KERNEL-NEXT: f();
18+
// KERNEL-NEXT: });
19+
20+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__constant__ | FileCheck %s -check-prefix=__CONSTANT__
21+
22+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__constant | FileCheck %s -check-prefix=__CONSTANT__
23+
24+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=constant | FileCheck %s -check-prefix=__CONSTANT__
25+
26+
// __CONSTANT__: CUDA API:
27+
// __CONSTANT__-NEXT: __constant__ int v;
28+
// __CONSTANT__-NEXT: Is migrated to:
29+
// __CONSTANT__-NEXT: static dpct::constant_memory<int, 0> v;
30+
31+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__device__ | FileCheck %s -check-prefix=__DEVICE__
32+
33+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__device | FileCheck %s -check-prefix=__DEVICE__
34+
35+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=device | FileCheck %s -check-prefix=__DEVICE__
36+
37+
// __DEVICE__: CUDA API:
38+
// __DEVICE__-NEXT: __device__ int v;
39+
// __DEVICE__-NEXT: __device__ void f() {}
40+
// __DEVICE__-NEXT: Is migrated to:
41+
// __DEVICE__-NEXT: dpct::global_memory<int, 0> v;
42+
// __DEVICE__-NEXT: void f() {}
43+
44+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__global__ | FileCheck %s -check-prefix=__GLOBAL__
45+
46+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__global | FileCheck %s -check-prefix=__GLOBAL__
47+
48+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=global | FileCheck %s -check-prefix=__GLOBAL__
49+
50+
// __GLOBAL__: CUDA API:
51+
// __GLOBAL__-NEXT: __global__ void f() {}
52+
// __GLOBAL__-NEXT: Is migrated to:
53+
// __GLOBAL__-NEXT: void f() {}
54+
55+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__host__ | FileCheck %s -check-prefix=__HOST__
56+
57+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__host | FileCheck %s -check-prefix=__HOST__
58+
59+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=host | FileCheck %s -check-prefix=__HOST__
60+
61+
// __HOST__: CUDA API:
62+
// __HOST__-NEXT: __host__ void f() {}
63+
// __HOST__-NEXT: Is migrated to:
64+
// __HOST__-NEXT: void f() {}
65+
66+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__managed__ | FileCheck %s -check-prefix=__MANAGED__
67+
68+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__managed | FileCheck %s -check-prefix=__MANAGED__
69+
70+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=managed | FileCheck %s -check-prefix=__MANAGED__
71+
72+
// __MANAGED__: CUDA API:
73+
// __MANAGED__-NEXT: __managed__ int v;
74+
// __MANAGED__-NEXT: Is migrated to:
75+
// __MANAGED__-NEXT: dpct::shared_memory<int, 0> v;
76+
77+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__shared__ | FileCheck %s -check-prefix=__SHARED__
78+
79+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=__shared | FileCheck %s -check-prefix=__SHARED__
80+
81+
// RUN: dpct --cuda-include-path="%cuda-path/include" --query-api-mapping=shared | FileCheck %s -check-prefix=__SHARED__
82+
83+
// __SHARED__: CUDA API:
84+
// __SHARED__-NEXT: __global__ void f() { __shared__ int v; }
85+
// __SHARED__-NEXT: Is migrated to:
86+
// __SHARED__-NEXT: void f(int &v) { }

clang/test/dpct/query_api_mapping/test_all.cu

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
// CHECK-NEXT: __byte_perm
4646
// CHECK-NEXT: __clz
4747
// CHECK-NEXT: __clzll
48+
// CHECK-NEXT: __constant__
4849
// CHECK-NEXT: __cosf
4950
// CHECK-NEXT: __dadd_rd
5051
// CHECK-NEXT: __dadd_rn
@@ -54,6 +55,7 @@
5455
// CHECK-NEXT: __ddiv_rn
5556
// CHECK-NEXT: __ddiv_ru
5657
// CHECK-NEXT: __ddiv_rz
58+
// CHECK-NEXT: __device__
5759
// CHECK-NEXT: __dmul_rd
5860
// CHECK-NEXT: __dmul_rn
5961
// CHECK-NEXT: __dmul_ru
@@ -167,6 +169,7 @@
167169
// CHECK-NEXT: __fsub_rn
168170
// CHECK-NEXT: __fsub_ru
169171
// CHECK-NEXT: __fsub_rz
172+
// CHECK-NEXT: __global__
170173
// CHECK-NEXT: __h2div
171174
// CHECK-NEXT: __habs
172175
// CHECK-NEXT: __habs2
@@ -290,6 +293,7 @@
290293
// CHECK-NEXT: __hneu
291294
// CHECK-NEXT: __hneu2
292295
// CHECK-NEXT: __hneu2_mask
296+
// CHECK-NEXT: __host__
293297
// CHECK-NEXT: __hsub
294298
// CHECK-NEXT: __hsub2
295299
// CHECK-NEXT: __hsub2_rn
@@ -344,6 +348,7 @@
344348
// CHECK-NEXT: __lowhigh2highlow
345349
// CHECK-NEXT: __lows2bfloat162
346350
// CHECK-NEXT: __lows2half2
351+
// CHECK-NEXT: __managed__
347352
// CHECK-NEXT: __match_all_sync
348353
// CHECK-NEXT: __match_any_sync
349354
// CHECK-NEXT: __mul24
@@ -362,6 +367,7 @@
362367
// CHECK-NEXT: __rhadd
363368
// CHECK-NEXT: __sad
364369
// CHECK-NEXT: __saturatef
370+
// CHECK-NEXT: __shared__
365371
// CHECK-NEXT: __shfl
366372
// CHECK-NEXT: __shfl_down
367373
// CHECK-NEXT: __shfl_down_sync
@@ -1832,6 +1838,7 @@
18321838
// CHECK-NEXT: j1f
18331839
// CHECK-NEXT: jn
18341840
// CHECK-NEXT: jnf
1841+
// CHECK-NEXT: kernel
18351842
// CHECK-NEXT: ldexp
18361843
// CHECK-NEXT: ldexpf
18371844
// CHECK-NEXT: lgamma

0 commit comments

Comments
 (0)