Skip to content

Commit c801703

Browse files
authored
[SYCLomatic] Fix the issue that loop unroll rule insertion may conflict with nd_item declaration insertion (#2831)
Signed-off-by: intwanghao <hao3.wang@intel.com>
1 parent 9b7b135 commit c801703

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

clang/lib/DPCT/RulesLang/OptimizeMigration.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ void ForLoopUnrollRule::runRule(
8585
auto IndentStr = getIndent(ForLoopLoc, SM).str();
8686
Repl = Repl + "#pragma unroll" + getNL() + IndentStr;
8787
auto Begin = SM.getSpellingLoc(ForLoop->getBeginLoc());
88-
emplaceTransformation(new InsertText(Begin, Repl));
88+
emplaceTransformation(new InsertText(Begin, Repl, 0,
89+
ReplacementType::RT_ForSYCLMigration,
90+
InsertPosition::IP_Right));
8991
}
9092
}
9193

clang/lib/DPCT/TextModification.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,11 @@ class InsertText : public TextModification {
231231

232232
public:
233233
InsertText(SourceLocation Loc, const std::string &S, unsigned PairID = 0,
234-
ReplacementType IsForCodePin = RT_ForSYCLMigration)
234+
ReplacementType IsForCodePin = RT_ForSYCLMigration,
235+
InsertPosition IP = InsertPosition::IP_Left)
235236
: TextModification(TMID::InsertText), Begin(Loc), T(S), PairID(PairID) {
236237
this->IsForCodePin = IsForCodePin;
238+
setInsertPosition(IP);
237239
}
238240
std::shared_ptr<ExtReplacement>
239241
getReplacement(const ASTContext &Context) const override;

clang/test/dpct/opt_loop_unroll.cu

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,34 @@ __device__ void bar(float *a, float *b) {
110110
copy(a, b, 10)
111111
}
112112

113+
114+
// CHECK: __dpct_inline__ void foo2(float *a) {
115+
// CHECK: auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
116+
// CHECK: #pragma unroll
117+
// CHECK: for(int i = 0; i < 10; i++) {
118+
// CHECK: a[i] = item_ct1.get_local_id(2);
119+
// CHECK: }
120+
// CHECK: #pragma unroll
121+
// CHECK: for(int i = 0; i < 10; i++) {
122+
// CHECK: a[i] = item_ct1.get_local_id(1);
123+
// CHECK: }
124+
// CHECK: }
125+
__global__ void foo2(float *a) {
126+
for(int i = 0; i < 10; i++) {
127+
a[i] = threadIdx.x;
128+
}
129+
130+
for(int i = 0; i < 10; i++) {
131+
a[i] = threadIdx.y;
132+
}
133+
134+
}
135+
136+
113137
int main(){
114138
float *a, *b, *c;
115139
kernel<<<10, 10>>>(a, b, c);
140+
foo2<<<10, 10>>>(a);
116141
return 0;
117142
}
118143
#endif

0 commit comments

Comments
 (0)