Skip to content

Commit d47401e

Browse files
committed
[OpenACC] Start enforcing 'device_type' clause values
Researching in prep of doing the implementation for lowering, I found that the source of the valid identifiers list from flang is in the frontend. This patch adds the same list to the frontend, but does it as a sema diagnostic, so we still parse it as an identifier/identifier-like thing, but then diagnose it as invalid later.
1 parent af9c04f commit d47401e

34 files changed

+360
-253
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13061,6 +13061,8 @@ def err_acc_decl_for_routine
1306113061
: Error<"expected function or lambda declaration for 'routine' construct">;
1306213062
def err_acc_invalid_modifier
1306313063
: Error<"OpenACC '%0' modifier not valid on '%1' clause">;
13064+
def err_acc_invalid_default_type
13065+
: Error<"invalid value %0 in '%1' clause; valid values are %2">;
1306413066

1306513067
// AMDGCN builtins diagnostics
1306613068
def err_amdgcn_load_lds_size_invalid_value : Error<"invalid size value">;

clang/lib/Sema/SemaOpenACCClause.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,13 +1338,50 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitDeviceTypeClause(
13381338
checkAlreadyHasClauseOfKind(SemaRef, ExistingClauses, Clause))
13391339
return nullptr;
13401340

1341-
// TODO OpenACC: Once we get enough of the CodeGen implemented that we have
1342-
// a source for the list of valid architectures, we need to warn on unknown
1343-
// identifiers here.
1341+
// The list of valid device_type values. Flang also has these hardcoded in
1342+
// openacc_parsers.cpp, as there does not seem to be a reliable backend
1343+
// source. The list below is sourced from Flang, though NVC++ supports only
1344+
// 'nvidia', 'host', 'multicore', and 'default'.
1345+
const std::array<llvm::StringLiteral, 6> ValidValues{
1346+
"default", "nvidia", "acc_device_nvidia", "radeon", "host", "multicore"};
1347+
// As an optimization, we have a manually maintained list of valid values
1348+
// below, rather than trying to calculate from above. These should be kept in
1349+
// sync if/when the above list ever changes.
1350+
std::string ValidValuesString =
1351+
"'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'";
1352+
1353+
llvm::SmallVector<DeviceTypeArgument> Architectures{
1354+
Clause.getDeviceTypeArchitectures()};
1355+
1356+
// The parser has ensured that we either have a single entry of just '*'
1357+
// (represented by a nullptr IdentifierInfo), or a list.
1358+
1359+
bool Diagnosed = false;
1360+
auto FilterPred = [&](const DeviceTypeArgument &Arch) {
1361+
// The '*' case.
1362+
if (!Arch.first)
1363+
return false;
1364+
return llvm::find_if(ValidValues, [&](StringRef RHS) {
1365+
return Arch.first->getName().equals_insensitive(RHS);
1366+
}) == ValidValues.end();
1367+
};
1368+
1369+
auto Diagnose = [&](const DeviceTypeArgument &Arch) {
1370+
Diagnosed = SemaRef.Diag(Arch.second, diag::err_acc_invalid_default_type)
1371+
<< Arch.first << Clause.getClauseKind() << ValidValuesString;
1372+
};
1373+
1374+
// There aren't stable enumertor versions of 'for-each-then-erase', so do it
1375+
// here. We DO keep track of whether we diagnosed something to make sure we
1376+
// don't do the 'erase_if' in the event that the first list didn't find
1377+
// anything.
1378+
llvm::for_each(llvm::make_filter_range(Architectures, FilterPred), Diagnose);
1379+
if (Diagnosed)
1380+
llvm::erase_if(Architectures, FilterPred);
13441381

13451382
return OpenACCDeviceTypeClause::Create(
13461383
Ctx, Clause.getClauseKind(), Clause.getBeginLoc(), Clause.getLParenLoc(),
1347-
Clause.getDeviceTypeArchitectures(), Clause.getEndLoc());
1384+
Architectures, Clause.getEndLoc());
13481385
}
13491386

13501387
OpenACCClause *SemaOpenACCClauseVisitor::VisitAutoClause(

clang/test/AST/ast-print-openacc-combined-construct.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,40 @@ void foo() {
3737
bool SomeB;
3838
struct SomeStruct{} SomeStructImpl;
3939

40-
//CHECK: #pragma acc parallel loop dtype(SomeB)
40+
//CHECK: #pragma acc parallel loop dtype(default)
4141
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
4242
// CHECK-NEXT: ;
43-
#pragma acc parallel loop dtype(SomeB)
43+
#pragma acc parallel loop dtype(default)
4444
for(int i = 0;i<5;++i);
4545

46-
//CHECK: #pragma acc serial loop device_type(SomeStruct)
46+
//CHECK: #pragma acc serial loop device_type(radeon, host)
4747
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
4848
// CHECK-NEXT: ;
49-
#pragma acc serial loop device_type(SomeStruct)
49+
#pragma acc serial loop device_type(radeon, host)
5050
for(int i = 0;i<5;++i);
5151

52-
//CHECK: #pragma acc kernels loop device_type(int)
52+
//CHECK: #pragma acc kernels loop device_type(nvidia)
5353
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
5454
// CHECK-NEXT: ;
55-
#pragma acc kernels loop device_type(int)
55+
#pragma acc kernels loop device_type(nvidia)
5656
for(int i = 0;i<5;++i);
5757

58-
//CHECK: #pragma acc parallel loop dtype(bool)
58+
//CHECK: #pragma acc parallel loop dtype(multicore)
5959
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
6060
// CHECK-NEXT: ;
61-
#pragma acc parallel loop dtype(bool)
61+
#pragma acc parallel loop dtype(multicore)
6262
for(int i = 0;i<5;++i);
6363

64-
//CHECK: #pragma acc serial loop device_type(SomeStructImpl)
64+
//CHECK: #pragma acc serial loop device_type(default)
6565
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
6666
// CHECK-NEXT: ;
67-
#pragma acc serial loop device_type (SomeStructImpl)
67+
#pragma acc serial loop device_type (default)
6868
for(int i = 0;i<5;++i);
6969

70-
// CHECK: #pragma acc kernels loop dtype(AnotherIdent)
70+
// CHECK: #pragma acc kernels loop dtype(host)
7171
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
7272
// CHECK-NEXT: ;
73-
#pragma acc kernels loop dtype(AnotherIdent)
73+
#pragma acc kernels loop dtype(host)
7474
for(int i = 0;i<5;++i);
7575

7676
int i;

clang/test/AST/ast-print-openacc-compute-construct.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,24 @@ void foo() {
115115
bool SomeB;
116116
struct SomeStruct{} SomeStructImpl;
117117

118-
//CHECK: #pragma acc parallel dtype(SomeB)
119-
#pragma acc parallel dtype(SomeB)
118+
//CHECK: #pragma acc parallel dtype(default)
119+
#pragma acc parallel dtype(default)
120120
while(true);
121121

122-
//CHECK: #pragma acc parallel device_type(SomeStruct)
123-
#pragma acc parallel device_type(SomeStruct)
122+
//CHECK: #pragma acc parallel device_type(radeon)
123+
#pragma acc parallel device_type(radeon)
124124
while(true);
125125

126-
//CHECK: #pragma acc parallel device_type(int)
127-
#pragma acc parallel device_type(int)
126+
//CHECK: #pragma acc parallel device_type(nvidia)
127+
#pragma acc parallel device_type(nvidia)
128128
while(true);
129129

130-
//CHECK: #pragma acc parallel dtype(bool)
131-
#pragma acc parallel dtype(bool)
130+
//CHECK: #pragma acc parallel dtype(multicore)
131+
#pragma acc parallel dtype(multicore)
132132
while(true);
133133

134-
//CHECK: #pragma acc parallel device_type(SomeStructImpl)
135-
#pragma acc parallel device_type (SomeStructImpl)
134+
//CHECK: #pragma acc parallel device_type(host)
135+
#pragma acc parallel device_type (host)
136136
while(true);
137137

138138
//CHECK: #pragma acc parallel reduction(+: iPtr)

clang/test/AST/ast-print-openacc-data-construct.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
void foo() {
44
int Var;
5-
// TODO OpenACC: These are only legal if they have one of a list of clauses on
6-
// them, so the 'check' lines should start to include those once we implement
7-
// them. For now, they don't emit those because they are 'not implemented'.
8-
95
// CHECK: #pragma acc data default(none)
106
#pragma acc data default(none)
117
;
128

13-
// CHECK: #pragma acc data default(none) device_type(int)
14-
#pragma acc data default(none) device_type(int)
9+
// CHECK: #pragma acc data default(none) device_type(radeon)
10+
#pragma acc data default(none) device_type(radeon)
1511
;
1612

1713
// CHECK: #pragma acc enter data copyin(Var)

clang/test/AST/ast-print-openacc-init-construct.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ void uses() {
99
#pragma acc init device_type(*) device_num(Int)
1010
// CHECK: #pragma acc init device_type(*) if(Int == 5)
1111
#pragma acc init device_type(*) if (Int == 5)
12-
// CHECK: #pragma acc init device_type(SomeName)
13-
#pragma acc init device_type(SomeName)
12+
// CHECK: #pragma acc init device_type(radeon)
13+
#pragma acc init device_type(radeon)
1414
}

clang/test/AST/ast-print-openacc-loop-construct.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ void foo() {
1010
#pragma acc loop
1111
for(int i = 0;i<5;++i);
1212

13-
// CHECK: #pragma acc loop device_type(SomeStruct)
13+
// CHECK: #pragma acc loop device_type(default)
1414
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
1515
// CHECK-NEXT: ;
16-
#pragma acc loop device_type(SomeStruct)
16+
#pragma acc loop device_type(default)
1717
for(int i = 0;i<5;++i);
1818

19-
// CHECK: #pragma acc loop device_type(int)
19+
// CHECK: #pragma acc loop device_type(nvidia)
2020
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
2121
// CHECK-NEXT: ;
22-
#pragma acc loop device_type(int)
22+
#pragma acc loop device_type(nvidia)
2323
for(int i = 0;i<5;++i);
2424

25-
// CHECK: #pragma acc loop dtype(bool)
25+
// CHECK: #pragma acc loop dtype(radeon)
2626
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
2727
// CHECK-NEXT: ;
28-
#pragma acc loop dtype(bool)
28+
#pragma acc loop dtype(radeon)
2929
for(int i = 0;i<5;++i);
3030

31-
// CHECK: #pragma acc loop dtype(AnotherIdent)
31+
// CHECK: #pragma acc loop dtype(host)
3232
// CHECK-NEXT: for (int i = 0; i < 5; ++i)
3333
// CHECK-NEXT: ;
34-
#pragma acc loop dtype(AnotherIdent)
34+
#pragma acc loop dtype(host)
3535
for(int i = 0;i<5;++i);
3636

3737
// CHECK: #pragma acc loop independent

clang/test/AST/ast-print-openacc-routine-construct.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ auto Lambda = [](){};
88
int function();
99
#pragma acc routine (function) vector nohost bind("string")
1010
// CHECK: #pragma acc routine(function) vector nohost bind("string")
11-
#pragma acc routine(function) device_type(Something) seq
12-
// CHECK-NEXT: #pragma acc routine(function) device_type(Something) seq
13-
#pragma acc routine(function) dtype(Something) seq
14-
// CHECK-NEXT: #pragma acc routine(function) dtype(Something) seq
11+
#pragma acc routine(function) device_type(multicore) seq
12+
// CHECK-NEXT: #pragma acc routine(function) device_type(multicore) seq
13+
#pragma acc routine(function) dtype(radeon) seq
14+
// CHECK-NEXT: #pragma acc routine(function) dtype(radeon) seq
1515

1616
#pragma acc routine nohost vector
1717
int function2();
@@ -136,8 +136,8 @@ struct DepS {
136136

137137
#pragma acc routine (MemFunc) worker dtype(*)
138138
// CHECK-NEXT: #pragma acc routine(MemFunc) worker dtype(*)
139-
#pragma acc routine (MemFunc) device_type(Lambda) vector
140-
// CHECK-NEXT: #pragma acc routine(MemFunc) device_type(Lambda) vector
139+
#pragma acc routine (MemFunc) device_type(nvidia) vector
140+
// CHECK-NEXT: #pragma acc routine(MemFunc) device_type(nvidia) vector
141141
};
142142

143143
// CHECK: #pragma acc routine(DepS<int>::Lambda) gang bind("string")

clang/test/AST/ast-print-openacc-set-construct.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
unsigned Int;
44

55
void uses() {
6-
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(I) device_num(Int)
7-
#pragma acc set default_async(Int) if (Int == 5) device_type(I) device_num(Int)
8-
// CHECK: #pragma acc set default_async(Int) device_type(I) device_num(Int)
9-
#pragma acc set default_async(Int) device_type(I) device_num(Int)
6+
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(default) device_num(Int)
7+
#pragma acc set default_async(Int) if (Int == 5) device_type(default) device_num(Int)
8+
// CHECK: #pragma acc set default_async(Int) device_type(nvidia) device_num(Int)
9+
#pragma acc set default_async(Int) device_type(nvidia) device_num(Int)
1010
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_num(Int)
1111
#pragma acc set default_async(Int) if (Int == 5) device_num(Int)
12-
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(I)
13-
#pragma acc set default_async(Int) if (Int == 5) device_type(I)
14-
// CHECK: #pragma acc set if(Int == 5) device_type(I) device_num(Int)
15-
#pragma acc set if (Int == 5) device_type(I) device_num(Int)
12+
// CHECK: #pragma acc set default_async(Int) if(Int == 5) device_type(host)
13+
#pragma acc set default_async(Int) if (Int == 5) device_type(host)
14+
// CHECK: #pragma acc set if(Int == 5) device_type(multicore) device_num(Int)
15+
#pragma acc set if (Int == 5) device_type(multicore) device_num(Int)
1616
// CHECK: #pragma acc set default_async(Int)
1717
#pragma acc set default_async(Int)
1818
// CHECK: #pragma acc set if(Int == 5)
1919
#pragma acc set if (Int == 5)
20-
// CHECK: #pragma acc set device_type(I)
21-
#pragma acc set device_type(I)
20+
// CHECK: #pragma acc set device_type(radeon)
21+
#pragma acc set device_type(radeon)
2222
// CHECK: #pragma acc set device_num(Int)
2323
#pragma acc set device_num(Int)
2424
}

clang/test/AST/ast-print-openacc-shutdown-construct.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ void uses() {
99
#pragma acc shutdown device_type(*) device_num(Int)
1010
// CHECK: #pragma acc shutdown device_type(*) if(Int == 5)
1111
#pragma acc shutdown device_type(*) if (Int == 5)
12-
// CHECK: #pragma acc shutdown device_type(SomeName)
13-
#pragma acc shutdown device_type(SomeName)
12+
// CHECK: #pragma acc shutdown device_type(radeon)
13+
#pragma acc shutdown device_type(radeon)
1414
}

clang/test/AST/ast-print-openacc-update-construct.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ void uses(bool cond) {
3030
// CHECK: #pragma acc update self(I) wait(devnum: I : queues: *iPtr, I) if(I == array[I]) async(I)
3131
#pragma acc update self(I) wait(devnum:I:queues:*iPtr, I) if(I == array[I]) async(I)
3232

33-
// CHECK: #pragma acc update self(I) device_type(I) dtype(H)
34-
#pragma acc update self(I) device_type(I) dtype(H)
33+
// CHECK: #pragma acc update self(I) device_type(nvidia) dtype(host)
34+
#pragma acc update self(I) device_type(nvidia) dtype(host)
3535

36-
// CHECK: #pragma acc update self(I) device_type(J) dtype(K)
37-
#pragma acc update self(I) device_type(J) dtype(K)
36+
// CHECK: #pragma acc update self(I) device_type(radeon) dtype(multicore)
37+
#pragma acc update self(I) device_type(radeon) dtype(multicore)
3838

3939
// CHECK: #pragma acc update self(I, iPtr, array, array[1], array[1:2])
4040
#pragma acc update self(I, iPtr, array, array[1], array[1:2])

clang/test/ParserOpenACC/parse-clauses.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,11 +1080,27 @@ void device_type() {
10801080
#pragma acc parallel dtype(31, "bar")
10811081
{}
10821082

1083+
// expected-error@+4{{invalid value 'ident' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1084+
// expected-error@+3{{invalid value 'auto' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1085+
// expected-error@+2{{invalid value 'int' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1086+
// expected-error@+1{{invalid value 'float' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
10831087
#pragma acc parallel device_type(ident, auto, int, float)
10841088
{}
1089+
// expected-error@+4{{invalid value 'ident' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1090+
// expected-error@+3{{invalid value 'auto' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1091+
// expected-error@+2{{invalid value 'int' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1092+
// expected-error@+1{{invalid value 'float' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
10851093
#pragma acc parallel dtype(ident, auto, int, float)
10861094
{}
10871095

1096+
// expected-error@+8{{invalid value 'ident' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1097+
// expected-error@+7{{invalid value 'auto' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1098+
// expected-error@+6{{invalid value 'int' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1099+
// expected-error@+5{{invalid value 'float' in 'device_type' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1100+
// expected-error@+4{{invalid value 'ident' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1101+
// expected-error@+3{{invalid value 'auto' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1102+
// expected-error@+2{{invalid value 'int' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
1103+
// expected-error@+1{{invalid value 'float' in 'dtype' clause; valid values are 'default', 'nvidia', 'acc_device_nvidia', 'radeon', 'host', 'multicore'}}
10881104
#pragma acc parallel device_type(ident, auto, int, float) dtype(ident, auto, int, float)
10891105
{}
10901106
}

0 commit comments

Comments
 (0)