[OpenCL] Change extension handling for -fdeclare-opencl-builtins

Until now, the `-fdeclare-opencl-builtins` option behaved differently
compared to inclusion of `opencl-c.h`: builtins that are part of an
extension were only available if the extension was enabled using the
corresponding pragma.

Builtins that belong to an extension are guarded using a preprocessor
macro (that is named after the extension) in `opencl-c.h`.  Align the
behaviour of `-fdeclare-opencl-builtins` with this.

Co-authored-by: Anastasia Stulova

Differential Revision: https://reviews.llvm.org/D95616
This commit is contained in:
Sven van Haastregt 2021-02-02 11:15:29 +00:00
parent 48230355e9
commit dc00c96b2d
2 changed files with 10 additions and 39 deletions

View File

@ -743,18 +743,6 @@ static void GetOpenCLBuiltinFctOverloads(
}
}
/// Add extensions to the function declaration.
/// \param S (in/out) The Sema instance.
/// \param BIDecl (in) Description of the builtin.
/// \param FDecl (in/out) FunctionDecl instance.
static void AddOpenCLExtensions(Sema &S, const OpenCLBuiltinStruct &BIDecl,
FunctionDecl *FDecl) {
// Fetch extension associated with a function prototype.
StringRef E = FunctionExtensionTable[BIDecl.Extension];
if (E != "")
S.setOpenCLExtensionForDecl(FDecl, E);
}
/// When trying to resolve a function name, if isOpenCLBuiltin() returns a
/// non-null <Index, Len> pair, then the name is referencing an OpenCL
/// builtin function. Add all candidate signatures to the LookUpResult.
@ -790,6 +778,13 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
(OpenCLVersion >= OpenCLBuiltin.MaxVersion))
continue;
// Ignore this builtin function if it carries an extension macro that is
// not defined. This indicates that the extension is not supported by the
// target, so the builtin function should not be available.
StringRef Ext = FunctionExtensionTable[OpenCLBuiltin.Extension];
if (!Ext.empty() && !S.getPreprocessor().isMacroDefined(Ext))
continue;
SmallVector<QualType, 1> RetTypes;
SmallVector<SmallVector<QualType, 1>, 5> ArgTypes;
@ -843,8 +838,6 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
if (!S.getLangOpts().OpenCLCPlusPlus)
NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context));
AddOpenCLExtensions(S, OpenCLBuiltin, NewOpenCLBuiltin);
LR.addDecl(NewOpenCLBuiltin);
}
}

View File

@ -38,26 +38,6 @@ kernel void test_pointers(volatile global void *global_p, global const int4 *a)
prefetch(a, 2);
atom_add((volatile __global int *)global_p, i);
#if !defined(__OPENCL_CPP_VERSION__) && __OPENCL_C_VERSION__ < CL_VERSION_1_1
// expected-error@-2{{no matching function for call to 'atom_add'}}
// There are two potential definitions of the function "atom_add", both are
// currently disabled because the associated extension is disabled.
// expected-note@-6{{candidate function not viable: cannot pass pointer to address space '__global' as a pointer to address space '__local' in 1st argument}}
// expected-note@-7{{candidate function not viable: no known conversion}}
// expected-note@-8{{candidate function not viable: no known conversion}}
// expected-note@-9{{candidate function not viable: no known conversion}}
// expected-note@-10{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
// expected-note@-11{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
// expected-note@-12{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
// expected-note@-13{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
#endif
#if __OPENCL_C_VERSION__ < CL_VERSION_1_1
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#endif
atom_add((volatile __global int *)global_p, i);
atom_cmpxchg((volatile __global unsigned int *)global_p, ui, ui);
}
@ -146,11 +126,9 @@ kernel void basic_image_writeonly(write_only image1d_buffer_t image_write_only_i
kernel void basic_subgroup(global uint *out) {
out[0] = get_sub_group_size();
#if defined(__OPENCL_CPP_VERSION__)
// expected-error@-2{{no matching function for call to 'get_sub_group_size'}}
// expected-note@-3{{candidate unavailable as it requires OpenCL extension 'cl_khr_subgroups' to be enabled}}
#else
// expected-error@-5{{use of declaration 'get_sub_group_size' requires cl_khr_subgroups extension to be enabled}}
#if __OPENCL_C_VERSION__ <= CL_VERSION_1_2 && !defined(__OPENCL_CPP_VERSION__)
// expected-error@-2{{implicit declaration of function 'get_sub_group_size' is invalid in OpenCL}}
// expected-error@-3{{implicit conversion changes signedness}}
#endif
}