Skip to content

Commit a94a862

Browse files
authored
[SYCLomatic] Fix the bug of migration that CUDART_VERSION would not be migrated (#840)
Signed-off-by: Ni, Wenhui <[email protected]>
1 parent 17ac089 commit a94a862

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ void IncludesCallbacks::insertCudaArchRepl(
156156
return;
157157
}
158158

159-
void IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok) {
159+
bool IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok) {
160160
bool IsInAnalysisScope = isInAnalysisScope(MacroNameTok.getLocation());
161161
if (!IsInAnalysisScope) {
162-
return;
162+
return false;
163163
}
164164
if (!MacroNameTok.getIdentifierInfo()) {
165-
return;
165+
return false;
166166
}
167167
std::string MacroName = MacroNameTok.getIdentifierInfo()->getName().str();
168168
auto Iter = MapNames::MacrosMap.find(MacroName);
@@ -175,14 +175,17 @@ void IncludesCallbacks::ReplaceCuMacro(const Token &MacroNameTok) {
175175
requestFeature(HelperFeatureEnum::Dpct_dpct_compatibility_temp,
176176
MacroNameTok.getLocation());
177177
insertCudaArchRepl(Repl->getReplacement(DpctGlobalInfo::getContext()));
178+
return true;
178179
}
179-
return;
180+
return false;
180181
}
181182
if (MacroName == "__CUDACC__" &&
182183
!MacroNameTok.getIdentifierInfo()->hasMacroDefinition())
183-
return;
184+
return false;
184185
TransformSet.emplace_back(Repl);
186+
return true;
185187
}
188+
return false;
186189
}
187190

188191
void IncludesCallbacks::MacroDefined(const Token &MacroNameTok,
@@ -396,29 +399,9 @@ void IncludesCallbacks::MacroExpands(const Token &MacroNameTok,
396399
if (!IsInAnalysisScope) {
397400
return;
398401
}
399-
400-
if (MacroNameTok.getIdentifierInfo() &&
401-
MacroNameTok.getIdentifierInfo()->getName() == "__CUDA_ARCH__") {
402-
if (DpctGlobalInfo::getInstance().getContext().getLangOpts().CUDA) {
403-
requestFeature(HelperFeatureEnum::Dpct_dpct_compatibility_temp,
404-
Range.getBegin());
405-
auto Repl = std::make_shared<ReplaceText>(Range.getBegin(), 13,
406-
"DPCT_COMPATIBILITY_TEMP");
407-
insertCudaArchRepl(Repl->getReplacement(DpctGlobalInfo::getContext()));
408-
}
409-
return;
410-
}
411-
// CUFFT_FORWARD and CUFFT_INVERSE are migrated to integer literal in all
412-
// places except in cufftExec call.
413-
// CUFFT_FORWARD and CUFFT_INVERSE in cufftExec call are migrated with
414-
// FFTDirExpr and longer replacement will overlap shorter replacement, so the
415-
// migration is expected.
416-
else if (MacroNameTok.getIdentifierInfo() &&
417-
MacroNameTok.getIdentifierInfo()->getName() == "CUFFT_FORWARD") {
418-
TransformSet.emplace_back(new ReplaceText(Range.getBegin(), 13, "-1"));
419-
} else if (MacroNameTok.getIdentifierInfo() &&
420-
MacroNameTok.getIdentifierInfo()->getName() == "CUFFT_INVERSE") {
421-
TransformSet.emplace_back(new ReplaceText(Range.getBegin(), 13, "1"));
402+
403+
if (ReplaceCuMacro(MacroNameTok)){
404+
return ;
422405
}
423406

424407
// For the un-specialized struct, there is no AST for the extern function

clang/lib/DPCT/ASTTraversal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class IncludesCallbacks : public PPCallbacks {
6060
void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
6161
const MacroDefinition &MD) override;
6262
// TODO: implement one of this for each source language.
63-
void ReplaceCuMacro(const Token &MacroNameTok);
63+
bool ReplaceCuMacro(const Token &MacroNameTok);
6464
void ReplaceCuMacro(SourceRange ConditionRange, IfType IT,
6565
SourceLocation IfLoc, SourceLocation ElifLoc);
6666
void Defined(const Token &MacroNameTok, const MacroDefinition &MD,

clang/lib/DPCT/MapNames.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,8 @@ const MapNames::MapTy MapNames::MacrosMap{
41874187
{"CUDART_VERSION", "SYCL_LANGUAGE_VERSION"},
41884188
{"CUBLAS_V2_H_", "MKL_SYCL_HPP"},
41894189
{"__CUDA__", "SYCL_LANGUAGE_VERSION"},
4190+
{"CUFFT_FORWARD", "-1"},
4191+
{"CUFFT_INVERSE", "1"},
41904192
//...
41914193
};
41924194

clang/test/dpct/predefined_macro_replacement.cu

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,22 @@ int2 b;
154154
a.x = 1;
155155
return a.x;
156156
}
157+
158+
int foo2(){
159+
int version;
160+
//CHECK: int ret = (version = dpct::get_current_device().get_major_version(), 0);
161+
int ret = cudaRuntimeGetVersion(&version);
162+
int major = version / 1000;
163+
int minor = (version - major * 1000) / 10;
164+
int pl = version - major * 1000 - minor * 10;
165+
//CHECK: if (version != SYCL_LANGUAGE_VERSION) {
166+
//CHECK-NEXT: major = SYCL_LANGUAGE_VERSION / 1000;
167+
//CHECK-NEXT: minor = (SYCL_LANGUAGE_VERSION - major * 1000) / 10;
168+
//CHECK-NEXT: pl = SYCL_LANGUAGE_VERSION - major * 1000 - minor * 10;
169+
//CHECK-NEXT: }
170+
if (version != CUDART_VERSION) {
171+
major = CUDART_VERSION / 1000;
172+
minor = (CUDART_VERSION - major * 1000) / 10;
173+
pl = CUDART_VERSION - major * 1000 - minor * 10;
174+
}
175+
}

0 commit comments

Comments
 (0)