Skip to content

Commit dc00c96

Browse files
committed
[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
1 parent 4823035 commit dc00c96

File tree

2 files changed

+10
-39
lines changed

2 files changed

+10
-39
lines changed

clang/lib/Sema/SemaLookup.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -743,18 +743,6 @@ static void GetOpenCLBuiltinFctOverloads(
743743
}
744744
}
745745

746-
/// Add extensions to the function declaration.
747-
/// \param S (in/out) The Sema instance.
748-
/// \param BIDecl (in) Description of the builtin.
749-
/// \param FDecl (in/out) FunctionDecl instance.
750-
static void AddOpenCLExtensions(Sema &S, const OpenCLBuiltinStruct &BIDecl,
751-
FunctionDecl *FDecl) {
752-
// Fetch extension associated with a function prototype.
753-
StringRef E = FunctionExtensionTable[BIDecl.Extension];
754-
if (E != "")
755-
S.setOpenCLExtensionForDecl(FDecl, E);
756-
}
757-
758746
/// When trying to resolve a function name, if isOpenCLBuiltin() returns a
759747
/// non-null <Index, Len> pair, then the name is referencing an OpenCL
760748
/// builtin function. Add all candidate signatures to the LookUpResult.
@@ -790,6 +778,13 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
790778
(OpenCLVersion >= OpenCLBuiltin.MaxVersion))
791779
continue;
792780

781+
// Ignore this builtin function if it carries an extension macro that is
782+
// not defined. This indicates that the extension is not supported by the
783+
// target, so the builtin function should not be available.
784+
StringRef Ext = FunctionExtensionTable[OpenCLBuiltin.Extension];
785+
if (!Ext.empty() && !S.getPreprocessor().isMacroDefined(Ext))
786+
continue;
787+
793788
SmallVector<QualType, 1> RetTypes;
794789
SmallVector<SmallVector<QualType, 1>, 5> ArgTypes;
795790

@@ -843,8 +838,6 @@ static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR,
843838
if (!S.getLangOpts().OpenCLCPlusPlus)
844839
NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context));
845840

846-
AddOpenCLExtensions(S, OpenCLBuiltin, NewOpenCLBuiltin);
847-
848841
LR.addDecl(NewOpenCLBuiltin);
849842
}
850843
}

clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,6 @@ kernel void test_pointers(volatile global void *global_p, global const int4 *a)
3838

3939
prefetch(a, 2);
4040

41-
atom_add((volatile __global int *)global_p, i);
42-
#if !defined(__OPENCL_CPP_VERSION__) && __OPENCL_C_VERSION__ < CL_VERSION_1_1
43-
// expected-error@-2{{no matching function for call to 'atom_add'}}
44-
45-
// There are two potential definitions of the function "atom_add", both are
46-
// currently disabled because the associated extension is disabled.
47-
// expected-note@-6{{candidate function not viable: cannot pass pointer to address space '__global' as a pointer to address space '__local' in 1st argument}}
48-
// expected-note@-7{{candidate function not viable: no known conversion}}
49-
// expected-note@-8{{candidate function not viable: no known conversion}}
50-
// expected-note@-9{{candidate function not viable: no known conversion}}
51-
// expected-note@-10{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
52-
// expected-note@-11{{candidate unavailable as it requires OpenCL extension 'cl_khr_global_int32_base_atomics' to be enabled}}
53-
// expected-note@-12{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
54-
// expected-note@-13{{candidate unavailable as it requires OpenCL extension 'cl_khr_int64_base_atomics' to be enabled}}
55-
#endif
56-
57-
#if __OPENCL_C_VERSION__ < CL_VERSION_1_1
58-
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
59-
#endif
60-
6141
atom_add((volatile __global int *)global_p, i);
6242
atom_cmpxchg((volatile __global unsigned int *)global_p, ui, ui);
6343
}
@@ -146,11 +126,9 @@ kernel void basic_image_writeonly(write_only image1d_buffer_t image_write_only_i
146126

147127
kernel void basic_subgroup(global uint *out) {
148128
out[0] = get_sub_group_size();
149-
#if defined(__OPENCL_CPP_VERSION__)
150-
// expected-error@-2{{no matching function for call to 'get_sub_group_size'}}
151-
// expected-note@-3{{candidate unavailable as it requires OpenCL extension 'cl_khr_subgroups' to be enabled}}
152-
#else
153-
// expected-error@-5{{use of declaration 'get_sub_group_size' requires cl_khr_subgroups extension to be enabled}}
129+
#if __OPENCL_C_VERSION__ <= CL_VERSION_1_2 && !defined(__OPENCL_CPP_VERSION__)
130+
// expected-error@-2{{implicit declaration of function 'get_sub_group_size' is invalid in OpenCL}}
131+
// expected-error@-3{{implicit conversion changes signedness}}
154132
#endif
155133
}
156134

0 commit comments

Comments
 (0)