Skip to content

Commit 4e18ea9

Browse files
authored
[SYCLomatic] Ensure semicolon is not removed when removing a call not surrounded by curly braces (#679)
Signed-off-by: Cai, Justin <[email protected]>
1 parent 4d11000 commit 4e18ea9

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

clang/lib/DPCT/TextModification.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ using namespace clang;
2424
using namespace clang::dpct;
2525
using namespace clang::tooling;
2626

27+
bool ReplaceStmt::inCompoundStmt(const Stmt *E) {
28+
auto &context = DpctGlobalInfo::getContext();
29+
const auto parents = context.getParents(*E);
30+
return std::find_if(parents.begin(), parents.end(), [](const DynTypedNode &n) {
31+
return bool(n.get<CompoundStmt>());
32+
}) != parents.end();
33+
}
34+
2735
std::shared_ptr<ExtReplacement>
2836
ReplaceStmt::getReplacement(const ASTContext &Context) const {
2937
if (this->isIgnoreTM())

clang/lib/DPCT/TextModification.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,21 @@ class ReplaceStmt : public TextModification {
273273
// this macro will be removed also.
274274
mutable bool IsMacroRemoved = false;
275275

276+
static bool inCompoundStmt(const Stmt *E);
277+
276278
public:
277279
template <class... Args>
278280
ReplaceStmt(const Stmt *E, Args &&...S)
279281
: TextModification(TMID::ReplaceStmt), TheStmt(E), IsProcessMacro(false),
280-
ReplacementString(std::forward<Args>(S)...) {}
282+
ReplacementString(std::forward<Args>(S)...),
283+
IsCleanup(inCompoundStmt(E)) {}
281284

282285
template <class... Args>
283286
ReplaceStmt(const Stmt *E, bool IsNeedProcessMacro, Args &&...S)
284287
: TextModification(TMID::ReplaceStmt), TheStmt(E),
285288
IsProcessMacro(IsNeedProcessMacro),
286-
ReplacementString(std::forward<Args>(S)...) {}
289+
ReplacementString(std::forward<Args>(S)...),
290+
IsCleanup(inCompoundStmt(E)) {}
287291

288292
template <class... Args>
289293
ReplaceStmt(const Stmt *E, bool IsNeedProcessMacro, bool IsNeedCleanup,

clang/test/dpct/dnn/semicolon.cu

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// RUN: dpct %s --out-root %T/semicolon --cuda-include-path="%cuda-path/include" --format-range=none
2+
// RUN: FileCheck %s --match-full-lines --input-file %T/semicolon/semicolon.dp.cpp
3+
#include <cudnn.h>
4+
5+
int main() {
6+
// CHECK: if (true) {
7+
// CHECK-NEXT: /*
8+
// CHECK-NEXT: DPCT1026:0: The call to cudnnDestroy was removed because this call is redundant in SYCL.
9+
// CHECK-NEXT: */
10+
// CHECK-NEXT: }
11+
if (true) {
12+
cudnnDestroy(nullptr);
13+
}
14+
15+
// CHECK: if (true)
16+
// CHECK-NEXT: /*
17+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroy was removed because this call is redundant in SYCL.
18+
// CHECK-NEXT: */
19+
// CHECK-NEXT: ;
20+
if (true)
21+
cudnnDestroy(nullptr);
22+
23+
// CHECK: /*
24+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateTensorDescriptor was removed because this call is redundant in SYCL.
25+
// CHECK-NEXT: */
26+
// CHECK-NEXT: if (true) ;
27+
if (true) cudnnCreateTensorDescriptor(nullptr);
28+
29+
// CHECK: /*
30+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyTensorDescriptor was removed because this call is redundant in SYCL.
31+
// CHECK-NEXT: */
32+
// CHECK-NEXT: if (true) ;
33+
if (true) cudnnDestroyTensorDescriptor(nullptr);
34+
35+
// CHECK: /*
36+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateActivationDescriptor was removed because this call is redundant in SYCL.
37+
// CHECK-NEXT: */
38+
// CHECK-NEXT: if (true) ;
39+
if (true) cudnnCreateActivationDescriptor(nullptr);
40+
41+
// CHECK: /*
42+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyActivationDescriptor was removed because this call is redundant in SYCL.
43+
// CHECK-NEXT: */
44+
// CHECK-NEXT: if (true) ;
45+
if (true) cudnnDestroyActivationDescriptor(nullptr);
46+
47+
// CHECK: /*
48+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateLRNDescriptor was removed because this call is redundant in SYCL.
49+
// CHECK-NEXT: */
50+
// CHECK-NEXT: if (true) ;
51+
if (true) cudnnCreateLRNDescriptor(nullptr);
52+
53+
// CHECK: /*
54+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyLRNDescriptor was removed because this call is redundant in SYCL.
55+
// CHECK-NEXT: */
56+
// CHECK-NEXT: if (true) ;
57+
if (true) cudnnDestroyLRNDescriptor(nullptr);
58+
59+
// CHECK: /*
60+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreatePoolingDescriptor was removed because this call is redundant in SYCL.
61+
// CHECK-NEXT: */
62+
// CHECK-NEXT: if (true) ;
63+
if (true) cudnnCreatePoolingDescriptor(nullptr);
64+
65+
// CHECK: /*
66+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyPoolingDescriptor was removed because this call is redundant in SYCL.
67+
// CHECK-NEXT: */
68+
// CHECK-NEXT: if (true) ;
69+
if (true) cudnnDestroyPoolingDescriptor(nullptr);
70+
71+
// CHECK: /*
72+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateReduceTensorDescriptor was removed because this call is redundant in SYCL.
73+
// CHECK-NEXT: */
74+
// CHECK-NEXT: if (true) ;
75+
if (true) cudnnCreateReduceTensorDescriptor(nullptr);
76+
77+
// CHECK: /*
78+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyReduceTensorDescriptor was removed because this call is redundant in SYCL.
79+
// CHECK-NEXT: */
80+
// CHECK-NEXT: if (true) ;
81+
if (true) cudnnDestroyReduceTensorDescriptor(nullptr);
82+
83+
// CHECK: /*
84+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateOpTensorDescriptor was removed because this call is redundant in SYCL.
85+
// CHECK-NEXT: */
86+
// CHECK-NEXT: if (true) ;
87+
if (true) cudnnCreateOpTensorDescriptor(nullptr);
88+
89+
// CHECK: /*
90+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyOpTensorDescriptor was removed because this call is redundant in SYCL.
91+
// CHECK-NEXT: */
92+
// CHECK-NEXT: if (true) ;
93+
if (true) cudnnDestroyOpTensorDescriptor(nullptr);
94+
95+
// CHECK: /*
96+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateFilterDescriptor was removed because this call is redundant in SYCL.
97+
// CHECK-NEXT: */
98+
// CHECK-NEXT: if (true) ;
99+
if (true) cudnnCreateFilterDescriptor(nullptr);
100+
101+
// CHECK: /*
102+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyFilterDescriptor was removed because this call is redundant in SYCL.
103+
// CHECK-NEXT: */
104+
// CHECK-NEXT: if (true) ;
105+
if (true) cudnnDestroyFilterDescriptor(nullptr);
106+
107+
// CHECK: /*
108+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateConvolutionDescriptor was removed because this call is redundant in SYCL.
109+
// CHECK-NEXT: */
110+
// CHECK-NEXT: if (true) ;
111+
if (true) cudnnCreateConvolutionDescriptor(nullptr);
112+
113+
// CHECK: /*
114+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyConvolutionDescriptor was removed because this call is redundant in SYCL.
115+
// CHECK-NEXT: */
116+
// CHECK-NEXT: if (true) ;
117+
if (true) cudnnDestroyConvolutionDescriptor(nullptr);
118+
119+
// CHECK: /*
120+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateRNNDescriptor was removed because this call is redundant in SYCL.
121+
// CHECK-NEXT: */
122+
// CHECK-NEXT: if (true) ;
123+
if (true) cudnnCreateRNNDescriptor(nullptr);
124+
125+
// CHECK: /*
126+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnCreateRNNDataDescriptor was removed because this call is redundant in SYCL.
127+
// CHECK-NEXT: */
128+
// CHECK-NEXT: if (true) ;
129+
if (true) cudnnCreateRNNDataDescriptor(nullptr);
130+
131+
// CHECK: /*
132+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyRNNDescriptor was removed because this call is redundant in SYCL.
133+
// CHECK-NEXT: */
134+
// CHECK-NEXT: if (true) ;
135+
if (true) cudnnDestroyRNNDescriptor(nullptr);
136+
137+
// CHECK: /*
138+
// CHECK-NEXT: DPCT1026:{{[0-9]+}}: The call to cudnnDestroyRNNDataDescriptor was removed because this call is redundant in SYCL.
139+
// CHECK-NEXT: */
140+
// CHECK-NEXT: if (true) ;
141+
if (true) cudnnDestroyRNNDataDescriptor(nullptr);
142+
}

0 commit comments

Comments
 (0)