Skip to content

Commit 95dd9c5

Browse files
azabaznotstellar
authored andcommitted
[OpenCL] Add support of language builtins for OpenCL C 3.0
OpenCL C 3.0 introduces optionality to some builtins, in particularly to those which are conditionally supported with pipe, device enqueue and generic address space features. The idea is to conditionally support such builtins depending on the language options being set for a certain feature. This allows users to define functions with names of those optional builtins in OpenCL (as such names are not reserved). Reviewed By: Anastasia Differential Revision: https://reviews.llvm.org/D118605 (cherry picked from commit bee4bd7)
1 parent fa24d7e commit 95dd9c5

File tree

9 files changed

+93
-72
lines changed

9 files changed

+93
-72
lines changed

clang/include/clang/Basic/Builtins.def

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,50 +1625,50 @@ LANGBUILTIN(__builtin_coro_suspend, "cIb", "n", COR_LANG)
16251625

16261626
// OpenCL v2.0 s6.13.16, s9.17.3.5 - Pipe functions.
16271627
// We need the generic prototype, since the packet type could be anything.
1628-
LANGBUILTIN(read_pipe, "i.", "tn", OCLC20_LANG)
1629-
LANGBUILTIN(write_pipe, "i.", "tn", OCLC20_LANG)
1628+
LANGBUILTIN(read_pipe, "i.", "tn", OCL_PIPE)
1629+
LANGBUILTIN(write_pipe, "i.", "tn", OCL_PIPE)
16301630

1631-
LANGBUILTIN(reserve_read_pipe, "i.", "tn", OCLC20_LANG)
1632-
LANGBUILTIN(reserve_write_pipe, "i.", "tn", OCLC20_LANG)
1631+
LANGBUILTIN(reserve_read_pipe, "i.", "tn", OCL_PIPE)
1632+
LANGBUILTIN(reserve_write_pipe, "i.", "tn", OCL_PIPE)
16331633

1634-
LANGBUILTIN(commit_write_pipe, "v.", "tn", OCLC20_LANG)
1635-
LANGBUILTIN(commit_read_pipe, "v.", "tn", OCLC20_LANG)
1634+
LANGBUILTIN(commit_write_pipe, "v.", "tn", OCL_PIPE)
1635+
LANGBUILTIN(commit_read_pipe, "v.", "tn", OCL_PIPE)
16361636

1637-
LANGBUILTIN(sub_group_reserve_read_pipe, "i.", "tn", OCLC20_LANG)
1638-
LANGBUILTIN(sub_group_reserve_write_pipe, "i.", "tn", OCLC20_LANG)
1637+
LANGBUILTIN(sub_group_reserve_read_pipe, "i.", "tn", OCL_PIPE)
1638+
LANGBUILTIN(sub_group_reserve_write_pipe, "i.", "tn", OCL_PIPE)
16391639

1640-
LANGBUILTIN(sub_group_commit_read_pipe, "v.", "tn", OCLC20_LANG)
1641-
LANGBUILTIN(sub_group_commit_write_pipe, "v.", "tn", OCLC20_LANG)
1640+
LANGBUILTIN(sub_group_commit_read_pipe, "v.", "tn", OCL_PIPE)
1641+
LANGBUILTIN(sub_group_commit_write_pipe, "v.", "tn", OCL_PIPE)
16421642

1643-
LANGBUILTIN(work_group_reserve_read_pipe, "i.", "tn", OCLC20_LANG)
1644-
LANGBUILTIN(work_group_reserve_write_pipe, "i.", "tn", OCLC20_LANG)
1643+
LANGBUILTIN(work_group_reserve_read_pipe, "i.", "tn", OCL_PIPE)
1644+
LANGBUILTIN(work_group_reserve_write_pipe, "i.", "tn", OCL_PIPE)
16451645

1646-
LANGBUILTIN(work_group_commit_read_pipe, "v.", "tn", OCLC20_LANG)
1647-
LANGBUILTIN(work_group_commit_write_pipe, "v.", "tn", OCLC20_LANG)
1646+
LANGBUILTIN(work_group_commit_read_pipe, "v.", "tn", OCL_PIPE)
1647+
LANGBUILTIN(work_group_commit_write_pipe, "v.", "tn", OCL_PIPE)
16481648

1649-
LANGBUILTIN(get_pipe_num_packets, "Ui.", "tn", OCLC20_LANG)
1650-
LANGBUILTIN(get_pipe_max_packets, "Ui.", "tn", OCLC20_LANG)
1649+
LANGBUILTIN(get_pipe_num_packets, "Ui.", "tn", OCL_PIPE)
1650+
LANGBUILTIN(get_pipe_max_packets, "Ui.", "tn", OCL_PIPE)
16511651

16521652
// OpenCL v2.0 s6.13.17 - Enqueue kernel functions.
16531653
// Custom builtin check allows to perform special check of passed block arguments.
1654-
LANGBUILTIN(enqueue_kernel, "i.", "tn", OCLC20_LANG)
1655-
LANGBUILTIN(get_kernel_work_group_size, "Ui.", "tn", OCLC20_LANG)
1656-
LANGBUILTIN(get_kernel_preferred_work_group_size_multiple, "Ui.", "tn", OCLC20_LANG)
1657-
LANGBUILTIN(get_kernel_max_sub_group_size_for_ndrange, "Ui.", "tn", OCLC20_LANG)
1658-
LANGBUILTIN(get_kernel_sub_group_count_for_ndrange, "Ui.", "tn", OCLC20_LANG)
1654+
LANGBUILTIN(enqueue_kernel, "i.", "tn", OCL_DSE)
1655+
LANGBUILTIN(get_kernel_work_group_size, "Ui.", "tn", OCL_DSE)
1656+
LANGBUILTIN(get_kernel_preferred_work_group_size_multiple, "Ui.", "tn", OCL_DSE)
1657+
LANGBUILTIN(get_kernel_max_sub_group_size_for_ndrange, "Ui.", "tn", OCL_DSE)
1658+
LANGBUILTIN(get_kernel_sub_group_count_for_ndrange, "Ui.", "tn", OCL_DSE)
16591659

16601660
// OpenCL v2.0 s6.13.9 - Address space qualifier functions.
16611661
// FIXME: Pointer parameters of OpenCL builtins should have their address space
16621662
// requirement defined.
1663-
LANGBUILTIN(to_global, "v*v*", "tn", OCLC20_LANG)
1664-
LANGBUILTIN(to_local, "v*v*", "tn", OCLC20_LANG)
1665-
LANGBUILTIN(to_private, "v*v*", "tn", OCLC20_LANG)
1663+
LANGBUILTIN(to_global, "v*v*", "tn", OCL_GAS)
1664+
LANGBUILTIN(to_local, "v*v*", "tn", OCL_GAS)
1665+
LANGBUILTIN(to_private, "v*v*", "tn", OCL_GAS)
16661666

16671667
// OpenCL half load/store builtin
1668-
LANGBUILTIN(__builtin_store_half, "vdh*", "n", ALL_OCLC_LANGUAGES)
1669-
LANGBUILTIN(__builtin_store_halff, "vfh*", "n", ALL_OCLC_LANGUAGES)
1670-
LANGBUILTIN(__builtin_load_half, "dhC*", "nc", ALL_OCLC_LANGUAGES)
1671-
LANGBUILTIN(__builtin_load_halff, "fhC*", "nc", ALL_OCLC_LANGUAGES)
1668+
LANGBUILTIN(__builtin_store_half, "vdh*", "n", ALL_OCL_LANGUAGES)
1669+
LANGBUILTIN(__builtin_store_halff, "vfh*", "n", ALL_OCL_LANGUAGES)
1670+
LANGBUILTIN(__builtin_load_half, "dhC*", "nc", ALL_OCL_LANGUAGES)
1671+
LANGBUILTIN(__builtin_load_halff, "fhC*", "nc", ALL_OCL_LANGUAGES)
16721672

16731673
// Builtins for os_log/os_trace
16741674
BUILTIN(__builtin_os_log_format_buffer_size, "zcC*.", "p:0:nut")

clang/include/clang/Basic/Builtins.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ class IdentifierTable;
2828
class LangOptions;
2929

3030
enum LanguageID {
31-
GNU_LANG = 0x1, // builtin requires GNU mode.
32-
C_LANG = 0x2, // builtin for c only.
33-
CXX_LANG = 0x4, // builtin for cplusplus only.
34-
OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
35-
MS_LANG = 0x10, // builtin requires MS mode.
36-
OCLC20_LANG = 0x20, // builtin for OpenCL C 2.0 only.
37-
OCLC1X_LANG = 0x40, // builtin for OpenCL C 1.x only.
38-
OMP_LANG = 0x80, // builtin requires OpenMP.
39-
CUDA_LANG = 0x100, // builtin requires CUDA.
40-
COR_LANG = 0x200, // builtin requires use of 'fcoroutine-ts' option.
31+
GNU_LANG = 0x1, // builtin requires GNU mode.
32+
C_LANG = 0x2, // builtin for c only.
33+
CXX_LANG = 0x4, // builtin for cplusplus only.
34+
OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
35+
MS_LANG = 0x10, // builtin requires MS mode.
36+
OMP_LANG = 0x20, // builtin requires OpenMP.
37+
CUDA_LANG = 0x40, // builtin requires CUDA.
38+
COR_LANG = 0x80, // builtin requires use of 'fcoroutine-ts' option.
39+
OCL_GAS = 0x100, // builtin requires OpenCL generic address space.
40+
OCL_PIPE = 0x200, // builtin requires OpenCL pipe.
41+
OCL_DSE = 0x400, // builtin requires OpenCL device side enqueue.
42+
ALL_OCL_LANGUAGES = 0x800, // builtin for OCL languages.
4143
ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
4244
ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode.
43-
ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG, // builtin requires MS mode.
44-
ALL_OCLC_LANGUAGES = OCLC1X_LANG | OCLC20_LANG // builtin for OCLC languages.
45+
ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode.
4546
};
4647

4748
namespace Builtin {

clang/lib/Basic/Builtins.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
6969
bool MSModeUnsupported =
7070
!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
7171
bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
72-
bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
73-
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
74-
bool OclC2Unsupported =
75-
(LangOpts.getOpenCLCompatibleVersion() != 200) &&
76-
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
77-
bool OclCUnsupported = !LangOpts.OpenCL &&
78-
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
72+
bool OclCUnsupported =
73+
!LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES);
74+
bool OclGASUnsupported =
75+
!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS);
76+
bool OclPipeUnsupported =
77+
!LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE);
78+
// Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
79+
// support is indicated with language option for blocks.
80+
bool OclDSEUnsupported =
81+
(LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
82+
(BuiltinInfo.Langs & OCL_DSE);
7983
bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
8084
bool CUDAUnsupported = !LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG;
8185
bool CPlusPlusUnsupported =
8286
!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
8387
return !BuiltinsUnsupported && !CorBuiltinsUnsupported &&
84-
!MathBuiltinsUnsupported && !OclCUnsupported && !OclC1Unsupported &&
85-
!OclC2Unsupported && !OpenMPUnsupported && !GnuModeUnsupported &&
86-
!MSModeUnsupported && !ObjCUnsupported && !CPlusPlusUnsupported &&
87-
!CUDAUnsupported;
88+
!MathBuiltinsUnsupported && !OclCUnsupported && !OclGASUnsupported &&
89+
!OclPipeUnsupported && !OclDSEUnsupported && !OpenMPUnsupported &&
90+
!GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
91+
!CPlusPlusUnsupported && !CUDAUnsupported;
8892
}
8993

9094
/// initializeBuiltins - Mark the identifiers for all the builtins with their

clang/test/CodeGenOpenCL/cl20-device-side-enqueue.cl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL2.0 -ffake-address-space-map -O0 -emit-llvm -o - -triple "spir-unknown-unknown" | FileCheck %s --check-prefix=COMMON --check-prefix=B32
22
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL2.0 -ffake-address-space-map -O0 -emit-llvm -o - -triple "spir64-unknown-unknown" | FileCheck %s --check-prefix=COMMON --check-prefix=B64
33
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL2.0 -ffake-address-space-map -O1 -emit-llvm -o - -triple "spir64-unknown-unknown" | FileCheck %s --check-prefix=CHECK-LIFETIMES
4+
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL3.0 -ffake-address-space-map -O0 -emit-llvm -o - -triple "spir-unknown-unknown" | FileCheck %s --check-prefix=COMMON --check-prefix=B32
5+
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL3.0 -ffake-address-space-map -O0 -emit-llvm -o - -triple "spir64-unknown-unknown" | FileCheck %s --check-prefix=COMMON --check-prefix=B64
6+
// RUN: %clang_cc1 -disable-noundef-analysis %s -cl-std=CL3.0 -ffake-address-space-map -O1 -emit-llvm -o - -triple "spir64-unknown-unknown" | FileCheck %s --check-prefix=CHECK-LIFETIMES
47

58
#pragma OPENCL EXTENSION cl_khr_subgroups : enable
69

clang/test/CodeGenOpenCL/pipe_types.cl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O0 -cl-std=CL2.0 -DTEST_STRUCT -o - %s | FileCheck --check-prefixes=CHECK,CHECK-STRUCT %s
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O0 -cl-std=CL2.0 -o - %s | FileCheck --check-prefixes=CHECK,CHECK-STRUCT %s
22
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O0 -cl-std=CL3.0 -cl-ext=-all,+__opencl_c_pipes,+__opencl_c_generic_address_space,+__opencl_c_program_scope_global_variables -o - %s | FileCheck --check-prefixes=CHECK %s
33
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O0 -cl-std=CL3.0 -cl-ext=-all,+__opencl_c_pipes,+__opencl_c_generic_address_space -o - %s | FileCheck --check-prefixes=CHECK %s
44
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O0 -cl-std=clc++2021 -cl-ext=-all,+__opencl_c_pipes,+__opencl_c_generic_address_space,+__opencl_c_program_scope_global_variables -o - %s | FileCheck --check-prefixes=CHECK %s
@@ -36,8 +36,6 @@ kernel void test6(MyPipe p) {
3636
// CHECK: define{{.*}} spir_kernel void @test6(%opencl.pipe_ro_t* %p)
3737
}
3838

39-
#ifdef TEST_STRUCT
40-
// FIXME: not supported for OpenCL C 3.0 as language built-ins not supported yet
4139
struct Person {
4240
const char *Name;
4341
bool isFemale;
@@ -52,4 +50,3 @@ void test_reserved_read_pipe(global struct Person *SDst,
5250
read_pipe (SPipe, SDst);
5351
// CHECK-STRUCT: call i32 @__read_pipe_2(%opencl.pipe_ro_t* %{{.*}}, i8* %{{.*}}, i32 16, i32 8)
5452
}
55-
#endif // TEST_STRUCT

clang/test/CodeGenOpenCL/to_addr_builtin.cl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -O0 -cl-std=clc++ -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -O0 -cl-std=cl2.0 -o - %s | FileCheck %s
3+
// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -O0 -cl-std=cl3.0 -o - %s | FileCheck %s
24

35
// CHECK: %[[A:.*]] = type { float, float, float }
46
typedef struct {

clang/test/SemaOpenCL/cl20-device-side-enqueue.cl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// RUN: %clang_cc1 %s -cl-std=CL2.0 -triple "spir-unknown-unknown" -verify -pedantic -fsyntax-only -DB32 -DQUALS="const volatile"
44
// RUN: %clang_cc1 %s -cl-std=CL2.0 -triple "spir64-unknown-unknown" -verify -pedantic -fsyntax-only -Wconversion -DWCONV -DQUALS=
55
// RUN: %clang_cc1 %s -cl-std=CL2.0 -triple "spir64-unknown-unknown" -verify -pedantic -fsyntax-only -Wconversion -DWCONV -DQUALS="const volatile"
6+
// RUN: %clang_cc1 %s -cl-std=CL3.0 -triple "spir-unknown-unknown" -verify -pedantic -fsyntax-only -DB32 -DQUALS=
7+
// RUN: %clang_cc1 %s -cl-std=CL3.0 -triple "spir-unknown-unknown" -verify -pedantic -fsyntax-only -DB32 -DQUALS= -cl-ext=-cl_khr_subgroups
8+
// RUN: %clang_cc1 %s -cl-std=CL3.0 -triple "spir-unknown-unknown" -verify -pedantic -fsyntax-only -DB32 -DQUALS="const volatile"
9+
// RUN: %clang_cc1 %s -cl-std=CL3.0 -triple "spir64-unknown-unknown" -verify -pedantic -fsyntax-only -Wconversion -DWCONV -DQUALS=
10+
// RUN: %clang_cc1 %s -cl-std=CL3.0 -triple "spir64-unknown-unknown" -verify -pedantic -fsyntax-only -Wconversion -DWCONV -DQUALS="const volatile"
611

712
typedef struct {int a;} ndrange_t;
813
// Diagnostic tests for different overloads of enqueue_kernel from Table 6.13.17.1 of OpenCL 2.0 Spec.

clang/test/SemaOpenCL/clang-builtin-version.cl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %clang_cc1 %s -fblocks -verify -pedantic -fsyntax-only -ferror-limit 100
2+
// RUN: %clang_cc1 %s -fblocks -verify -pedantic -fsyntax-only -ferror-limit 100 -cl-std=CL1.2
3+
// RUN: %clang_cc1 %s -fblocks -verify -pedantic -fsyntax-only -ferror-limit 100 -cl-std=CL3.0 -cl-ext=-__opencl_c_device_enqueue,-__opencl_c_generic_address_space,-__opencl_c_pipes
24

3-
// Confirm CL2.0 Clang builtins are not available in earlier versions
5+
// Confirm CL2.0 Clang builtins are not available in earlier versions and in OpenCL C 3.0 without required features.
46

57
kernel void dse_builtins() {
68
int tmp;
@@ -13,6 +15,11 @@ kernel void dse_builtins() {
1315
size = get_kernel_preferred_work_group_size_multiple(^(void) { // expected-error{{implicit declaration of function 'get_kernel_preferred_work_group_size_multiple' is invalid in OpenCL}}
1416
return;
1517
});
18+
#if (__OPENCL_C_VERSION__ >= CL_VERSION_3_0) && !defined(__opencl_c_device_enqueue)
19+
// expected-error@-10{{support disabled - compile with -fblocks or for OpenCL C 2.0 or OpenCL C 3.0 with __opencl_c_device_enqueue feature}}
20+
// expected-error@-8{{support disabled - compile with -fblocks or for OpenCL C 2.0 or OpenCL C 3.0 with __opencl_c_device_enqueue feature}}
21+
// expected-error@-6{{support disabled - compile with -fblocks or for OpenCL C 2.0 or OpenCL C 3.0 with __opencl_c_device_enqueue feature}}
22+
#endif
1623
}
1724

1825
void pipe_builtins() {

0 commit comments

Comments
 (0)