Skip to content

Commit 3ac41fd

Browse files
committed
[AST] Enable expression of OpenCL language address spaces an attribute
Enable a way to set OpenCL language address space using attributes in addition to existing keywords. Signed-off-by: Victor Lomuller victor@codeplay.com
1 parent 047bfea commit 3ac41fd

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

clang/include/clang/Basic/Attr.td

+5-5
Original file line numberDiff line numberDiff line change
@@ -1070,27 +1070,27 @@ def OpenCLAccess : Attr {
10701070
}
10711071

10721072
def OpenCLPrivateAddressSpace : TypeAttr {
1073-
let Spellings = [Keyword<"__private">, Keyword<"private">];
1073+
let Spellings = [Keyword<"__private">, Keyword<"private">, Clang<"ocl_private">];
10741074
let Documentation = [OpenCLAddressSpacePrivateDocs];
10751075
}
10761076

10771077
def OpenCLGlobalAddressSpace : TypeAttr {
1078-
let Spellings = [Keyword<"__global">, Keyword<"global">];
1078+
let Spellings = [Keyword<"__global">, Keyword<"global">, Clang<"ocl_global">];
10791079
let Documentation = [OpenCLAddressSpaceGlobalDocs];
10801080
}
10811081

10821082
def OpenCLLocalAddressSpace : TypeAttr {
1083-
let Spellings = [Keyword<"__local">, Keyword<"local">];
1083+
let Spellings = [Keyword<"__local">, Keyword<"local">, Clang<"ocl_local">];
10841084
let Documentation = [OpenCLAddressSpaceLocalDocs];
10851085
}
10861086

10871087
def OpenCLConstantAddressSpace : TypeAttr {
1088-
let Spellings = [Keyword<"__constant">, Keyword<"constant">];
1088+
let Spellings = [Keyword<"__constant">, Keyword<"constant">, Clang<"ocl_constant">];
10891089
let Documentation = [OpenCLAddressSpaceConstantDocs];
10901090
}
10911091

10921092
def OpenCLGenericAddressSpace : TypeAttr {
1093-
let Spellings = [Keyword<"__generic">, Keyword<"generic">];
1093+
let Spellings = [Keyword<"__generic">, Keyword<"generic">, Clang<"ocl_generic">];
10941094
let Documentation = [OpenCLAddressSpaceGenericDocs];
10951095
}
10961096

clang/lib/Sema/SemaType.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -7449,6 +7449,16 @@ static void HandleLifetimeBoundAttr(TypeProcessingState &State,
74497449
}
74507450
}
74517451

7452+
static bool isAddressSpaceKind(const ParsedAttr &attr) {
7453+
auto attrKind = attr.getKind();
7454+
7455+
return attrKind == ParsedAttr::AT_AddressSpace ||
7456+
attrKind == ParsedAttr::AT_OpenCLPrivateAddressSpace ||
7457+
attrKind == ParsedAttr::AT_OpenCLGlobalAddressSpace ||
7458+
attrKind == ParsedAttr::AT_OpenCLLocalAddressSpace ||
7459+
attrKind == ParsedAttr::AT_OpenCLConstantAddressSpace ||
7460+
attrKind == ParsedAttr::AT_OpenCLGenericAddressSpace;
7461+
}
74527462

74537463
static void processTypeAttrs(TypeProcessingState &state, QualType &type,
74547464
TypeAttrLocation TAL,
@@ -7487,11 +7497,11 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
74877497
if (!IsTypeAttr)
74887498
continue;
74897499
}
7490-
} else if (TAL != TAL_DeclChunk &&
7491-
attr.getKind() != ParsedAttr::AT_AddressSpace) {
7500+
} else if (TAL != TAL_DeclChunk && !isAddressSpaceKind(attr)) {
74927501
// Otherwise, only consider type processing for a C++11 attribute if
74937502
// it's actually been applied to a type.
7494-
// We also allow C++11 address_space attributes to pass through.
7503+
// We also allow C++11 address_space and
7504+
// opencl language address space attributes to pass through.
74957505
continue;
74967506
}
74977507
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %clang_cc1 %s -ast-dump | FileCheck %s
2+
3+
// Verify that the language address space attribute is
4+
// understood correctly by clang.
5+
6+
void langas() {
7+
// CHECK: VarDecl {{.*}} x_global '__global int *'
8+
__attribute__((ocl_global)) int *x_global;
9+
10+
// CHECK: VarDecl {{.*}} z_global '__global int *'
11+
[[clang::ocl_global]] int *z_global;
12+
13+
// CHECK: VarDecl {{.*}} x_local '__local int *'
14+
__attribute__((ocl_local)) int *x_local;
15+
16+
// CHECK: VarDecl {{.*}} z_local '__local int *'
17+
[[clang::ocl_local]] int *z_local;
18+
19+
// CHECK: VarDecl {{.*}} x_constant '__constant int *'
20+
__attribute__((ocl_constant)) int *x_constant;
21+
22+
// CHECK: VarDecl {{.*}} z_constant '__constant int *'
23+
[[clang::ocl_constant]] int *z_constant;
24+
25+
// CHECK: VarDecl {{.*}} x_private 'int *'
26+
__attribute__((ocl_private)) int *x_private;
27+
28+
// CHECK: VarDecl {{.*}} z_private 'int *'
29+
[[clang::ocl_private]] int *z_private;
30+
31+
// CHECK: VarDecl {{.*}} x_generic '__generic int *'
32+
__attribute__((ocl_generic)) int *x_generic;
33+
34+
// CHECK: VarDecl {{.*}} z_generic '__generic int *'
35+
[[clang::ocl_generic]] int *z_generic;
36+
}

clang/test/SemaOpenCL/address-spaces.cl

+16
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,19 @@ void func_multiple_addr(void) {
241241
__private private_int_t var5; // expected-warning {{multiple identical address spaces specified for type}}
242242
__private private_int_t *var6;// expected-warning {{multiple identical address spaces specified for type}}
243243
}
244+
245+
void func_multiple_addr2(void) {
246+
typedef __private int private_int_t;
247+
__private __attribute__((ocl_global)) int var1; // expected-error {{multiple address spaces specified for type}}
248+
__private __attribute__((ocl_global)) int *var2; // expected-error {{multiple address spaces specified for type}}
249+
__attribute__((ocl_global)) private_int_t var3; // expected-error {{multiple address spaces specified for type}}
250+
__attribute__((ocl_global)) private_int_t *var4; // expected-error {{multiple address spaces specified for type}}
251+
__attribute__((ocl_private)) private_int_t var5; // expected-warning {{multiple identical address spaces specified for type}}
252+
__attribute__((ocl_private)) private_int_t *var6; // expected-warning {{multiple identical address spaces specified for type}}
253+
#if __OPENCL_CPP_VERSION__
254+
[[clang::ocl_private]] __global int var7; // expected-error {{multiple address spaces specified for type}}
255+
[[clang::ocl_private]] __global int *var8; // expected-error {{multiple address spaces specified for type}}
256+
[[clang::ocl_private]] private_int_t var9; // expected-warning {{multiple identical address spaces specified for type}}
257+
[[clang::ocl_private]] private_int_t *var10; // expected-warning {{multiple identical address spaces specified for type}}
258+
#endif // !__OPENCL_CPP_VERSION__
259+
}

0 commit comments

Comments
 (0)