Skip to content

Commit cf3ef15

Browse files
author
Anastasia Stulova
committed
[OpenCL] Add builtin declarations by default.
This change enables the builtin function declarations in clang driver by default using the Tablegen solution along with the implicit include of 'opencl-c-base.h' header. A new flag '-cl-no-stdinc' disabling all default declarations and header includes is added. If any other mechanisms were used to include the declarations (e.g. with -Xclang -finclude-default-header) and the new default approach is not sufficient the, `-cl-no-stdinc` flag has to be used with clang to activate the old behavior. Tags: #clang Differential Revision: https://reviews.llvm.org/D96515
1 parent 1908488 commit cf3ef15

File tree

6 files changed

+31
-2
lines changed

6 files changed

+31
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-round
818818
def cl_uniform_work_group_size : Flag<["-"], "cl-uniform-work-group-size">, Group<opencl_Group>, Flags<[CC1Option]>,
819819
HelpText<"OpenCL only. Defines that the global work-size be a multiple of the work-group size specified to clEnqueueNDRangeKernel">,
820820
MarshallingInfoFlag<CodeGenOpts<"UniformWGSize">>;
821+
def cl_no_stdinc : Flag<["-"], "cl-no-stdinc">, Group<opencl_Group>,
822+
HelpText<"OpenCL only. Disables all standard includes containing non-native compiler types and functions.">;
821823
def client__name : JoinedOrSeparate<["-"], "client_name">;
822824
def combine : Flag<["-", "--"], "combine">, Flags<[NoXarchOption, Unsupported]>;
823825
def compatibility__version : JoinedOrSeparate<["-"], "compatibility_version">;

clang/include/clang/Driver/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ namespace types {
8181
/// isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers).
8282
bool isObjC(ID Id);
8383

84+
/// isOpenCL - Is this an "OpenCL" input.
85+
bool isOpenCL(ID Id);
86+
8487
/// isFortran - Is this a Fortran input.
8588
bool isFortran(ID Id);
8689

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,8 @@ static void RenderTrivialAutoVarInitOptions(const Driver &D,
31933193
}
31943194
}
31953195

3196-
static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
3196+
static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
3197+
types::ID InputType) {
31973198
// cl-denorms-are-zero is not forwarded. It is translated into a generic flag
31983199
// for denormal flushing handling based on the target.
31993200
const unsigned ForwardedArguments[] = {
@@ -3218,6 +3219,13 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
32183219
for (const auto &Arg : ForwardedArguments)
32193220
if (const auto *A = Args.getLastArg(Arg))
32203221
CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
3222+
3223+
// Only add the default headers if we are compiling OpenCL sources.
3224+
if ((types::isOpenCL(InputType) || Args.hasArg(options::OPT_cl_std_EQ)) &&
3225+
!Args.hasArg(options::OPT_cl_no_stdinc)) {
3226+
CmdArgs.push_back("-finclude-default-header");
3227+
CmdArgs.push_back("-fdeclare-opencl-builtins");
3228+
}
32213229
}
32223230

32233231
static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
@@ -5726,7 +5734,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57265734
}
57275735

57285736
// Forward -cl options to -cc1
5729-
RenderOpenCLOptions(Args, CmdArgs);
5737+
RenderOpenCLOptions(Args, CmdArgs, InputType);
57305738

57315739
if (IsHIP) {
57325740
if (Args.hasFlag(options::OPT_fhip_new_launch_api,

clang/lib/Driver/Types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ bool types::isObjC(ID Id) {
160160
}
161161
}
162162

163+
bool types::isOpenCL(ID Id) { return Id == TY_CL; }
164+
163165
bool types::isCXX(ID Id) {
164166
switch (Id) {
165167
default:

clang/test/Driver/default-includes.cl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang %s -Xclang -verify -fsyntax-only
2+
// RUN: %clang %s -cl-no-stdinc -Xclang -verify -DNOINC -fsyntax-only
3+
4+
#ifndef NOINC
5+
//expected-no-diagnostics
6+
#endif
7+
8+
void test() {
9+
int i = get_global_id(0);
10+
#ifdef NOINC
11+
//expected-error@-2{{implicit declaration of function 'get_global_id' is invalid in OpenCL}}
12+
#endif
13+
}

clang/unittests/AST/MatchVerifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ MatchVerifier<NodeType>::match(const std::string &Code,
117117
FileName = "input.cc";
118118
break;
119119
case Lang_OpenCL:
120+
Args.push_back("-cl-no-stdinc");
120121
FileName = "input.cl";
121122
break;
122123
case Lang_OBJCXX:

0 commit comments

Comments
 (0)