Skip to content

Commit a4db727

Browse files
authored
[SYCLomatic] Remove register storage class specifier which cause compiler error in C++17 code (#2848)
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
1 parent 996a732 commit a4db727

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ class NamedDecl : public Decl {
260260

261261
private:
262262
NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY;
263+
#ifdef SYCLomatic_CUSTOMIZATION
264+
SourceLocation StorageClassSpecLoc = SourceLocation();
265+
#endif
263266

264267
protected:
265268
NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
@@ -487,6 +490,12 @@ class NamedDecl : public Decl {
487490

488491
ObjCStringFormatFamily getObjCFStringFormattingFamily() const;
489492

493+
#ifdef SYCLomatic_CUSTOMIZATION
494+
SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
495+
496+
void setStorageClassSpecLoc(SourceLocation Loc) { StorageClassSpecLoc = Loc; }
497+
#endif
498+
490499
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
491500
static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; }
492501
};

clang/lib/DPCT/RulesLang/RulesLangNoneAPIAndType.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ extern DpctOption<opt, bool> ProcessAll;
4141
extern DpctOption<opt, bool> AsyncHandler;
4242

4343
namespace clang {
44+
namespace ast_matchers {
45+
AST_MATCHER(VarDecl, hasRegisterStorage) {
46+
return Node.getStorageClass() == SC_Register;
47+
}
48+
} // namespace ast_matchers
4449
namespace dpct {
4550

4651
void LinkageSpecDeclRule::registerMatcher(MatchFinder &MF) {
@@ -1282,22 +1287,25 @@ void RemoveBaseClassRule::runRule(const MatchFinder::MatchResult &Result) {
12821287
}
12831288
}
12841289

1285-
// The EDG frontend can allow code like below:
1286-
//
1287-
// template <class T1, class T2> struct AAAAA {
1288-
// template <class T3> void foo(T3 x);
1289-
// };
1290-
// template <typename T4, typename T5>
1291-
// template <typename T6>
1292-
// void AAAAA<T4, T5>::foo<T6>(T6 x) {}
1293-
//
1294-
// But clang/gcc emits error.
1295-
// We suppress the error in Sema and record the source range and remove
1296-
// the "invalid" code in this rule.
12971290
void CompatWithClangRule::registerMatcher(ast_matchers::MatchFinder &MF) {
1291+
// The EDG frontend can allow code like below:
1292+
//
1293+
// template <class T1, class T2> struct AAAAA {
1294+
// template <class T3> void foo(T3 x);
1295+
// };
1296+
// template <typename T4, typename T5>
1297+
// template <typename T6>
1298+
// void AAAAA<T4, T5>::foo<T6>(T6 x) {}
1299+
//
1300+
// But clang/gcc emits error.
1301+
// We suppress the error in Sema and record the source range and remove
1302+
// the "invalid" code in this rule.
12981303
MF.addMatcher(
12991304
cxxMethodDecl(hasParent(functionTemplateDecl())).bind("TemplateMethod"),
13001305
this);
1306+
// ISO C++17 does not allow the 'register' storage class specifier. nvcc
1307+
// issues a warning when it encounters it, but keeps compiling.
1308+
MF.addMatcher(varDecl(hasRegisterStorage()).bind("RegisterStorage"), this);
13011309
}
13021310

13031311
void CompatWithClangRule::runRule(
@@ -1314,6 +1322,12 @@ void CompatWithClangRule::runRule(
13141322
auto Length = End.getRawEncoding() - Begin.getRawEncoding();
13151323
emplaceTransformation(new ReplaceText(Begin, Length, ""));
13161324
}
1325+
} else if (const auto *VD =
1326+
getNodeAsType<VarDecl>(Result, "RegisterStorage")) {
1327+
SourceLocation SL = VD->getStorageClassSpecLoc();
1328+
if (DpctGlobalInfo::getContext().getLangOpts().CUDA && SL.isValid()) {
1329+
emplaceTransformation(new ReplaceText(SL, std::strlen("register"), ""));
1330+
}
13171331
}
13181332
}
13191333

clang/lib/Sema/SemaDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7872,6 +7872,12 @@ NamedDecl *Sema::ActOnVariableDeclarator(
78727872
}
78737873
}
78747874

7875+
#ifdef SYCLomatic_CUSTOMIZATION
7876+
if (SC != SC_None) {
7877+
NewVD->setStorageClassSpecLoc(D.getDeclSpec().getStorageClassSpecLoc());
7878+
}
7879+
#endif
7880+
78757881
// Set the lexical context. If the declarator has a C++ scope specifier, the
78767882
// lexical context will be different from the semantic context.
78777883
NewVD->setLexicalDeclContext(CurContext);
@@ -15288,6 +15294,11 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D,
1528815294
if (getLangOpts().OpenCL)
1528915295
deduceOpenCLAddressSpace(New);
1529015296

15297+
#ifdef SYCLomatic_CUSTOMIZATION
15298+
if (SC != SC_None) {
15299+
New->setStorageClassSpecLoc(D.getDeclSpec().getStorageClassSpecLoc());
15300+
}
15301+
#endif
1529115302
return New;
1529215303
}
1529315304

clang/test/dpct/register_storage.cu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: dpct --format-range=none --out-root %T/register_storage %s --cuda-include-path="%cuda-path/include"
2+
// RUN: FileCheck %s --match-full-lines --input-file %T/register_storage/register_storage.dp.cpp
3+
// RUN: %if build_lit %{icpx -c -fsycl %T/register_storage/register_storage.dp.cpp -o %T/register_storage/register_storage.dp.o %}
4+
5+
// CHECK: void foo( int b) {
6+
// CHECK-NEXT: int a = 5;
7+
// CHECK-NEXT: }
8+
void foo(register int b) {
9+
register int a = 5;
10+
}

0 commit comments

Comments
 (0)