Skip to content

Commit

Permalink
[OpenCL] Clang diagnostics allow reporting C++ for OpenCL version.
Browse files Browse the repository at this point in the history
Some Clang diagnostics could only report OpenCL C version. Because
C++ for OpenCL can be used as an alternative to OpenCL C, the text
for diagnostics should reflect that.

Desrciptions modified for these diagnostics:
`err_opencl_unknown_type_specifier`
`warn_option_invalid_ocl_version`
`err_attribute_requires_opencl_version`
`warn_opencl_attr_deprecated_ignored`
`ext_opencl_ext_vector_type_rgba_selector`

Differential Revision: https://reviews.llvm.org/D107648
  • Loading branch information
Topotuna committed Aug 13, 2021
1 parent c46546b commit cfdfb75
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 28 deletions.
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticCommonKinds.td
Expand Up @@ -149,8 +149,8 @@ def err_nullability_conflicting : Error<

// OpenCL Section 6.8.g
def err_opencl_unknown_type_specifier : Error<
"%select{OpenCL C|C++ for OpenCL}0 version %1 does not support the "
"'%2' %select{type qualifier|storage class specifier}3">;
"%0 does not support the '%1' "
"%select{type qualifier|storage class specifier}2">;

def warn_unknown_attribute_ignored : Warning<
"unknown attribute %0 ignored">, InGroup<UnknownAttributes>;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticFrontendKinds.td
Expand Up @@ -247,7 +247,7 @@ def err_invalid_vfs_overlay : Error<
"invalid virtual filesystem overlay file '%0'">, DefaultFatal;

def warn_option_invalid_ocl_version : Warning<
"OpenCL version %0 does not support the option '%1'">, InGroup<Deprecated>;
"%0 does not support the option '%1'">, InGroup<Deprecated>;

def err_builtin_needs_feature : Error<"%0 needs target feature %1">;
def err_function_needs_feature : Error<
Expand Down
7 changes: 3 additions & 4 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -2959,7 +2959,7 @@ def err_attribute_requires_positive_integer : Error<
"%0 attribute requires a %select{positive|non-negative}1 "
"integral compile time constant expression">;
def err_attribute_requires_opencl_version : Error<
"%0 attribute requires OpenCL version %1%select{| or above}2">;
"attribute %0 is supported in the OpenCL version %1%select{| onwards}2">;
def err_invalid_branch_protection_spec : Error<
"invalid or misplaced branch protection specification '%0'">;
def warn_unsupported_target_attribute
Expand Down Expand Up @@ -10098,8 +10098,7 @@ def err_opencl_type_can_only_be_used_as_function_parameter : Error <
def err_opencl_type_not_found : Error<
"%0 type %1 not found; include the base header with -finclude-default-header">;
def warn_opencl_attr_deprecated_ignored : Warning <
"%0 attribute is deprecated and ignored in OpenCL version %1">,
InGroup<IgnoredAttributes>;
"%0 attribute is deprecated and ignored in %1">, InGroup<IgnoredAttributes>;
def err_opencl_variadic_function : Error<
"invalid prototype, variadic arguments are not allowed in OpenCL">;
def err_opencl_requires_extension : Error<
Expand Down Expand Up @@ -10164,7 +10163,7 @@ def err_opencl_builtin_expected_type : Error<

// OpenCL v3.0 s6.3.7 - Vector Components
def ext_opencl_ext_vector_type_rgba_selector: ExtWarn<
"vector component name '%0' is an OpenCL C version 3.0 feature">,
"vector component name '%0' is a feature from OpenCL version 3.0 onwards">,
InGroup<OpenCLUnsupportedRGBA>;

def err_openclcxx_placement_new : Error<
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Expand Up @@ -444,6 +444,10 @@ class LangOptions : public LangOptionsBase {
/// Return the OpenCL C or C++ version as a VersionTuple.
VersionTuple getOpenCLVersionTuple() const;

/// Return the OpenCL C or C++ for OpenCL language name and version
/// as a string.
std::string getOpenCLVersionString() const;

/// Check if return address signing is enabled.
bool hasSignReturnAddress() const {
return getSignReturnAddressScope() != SignReturnAddressScopeKind::None;
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Basic/LangOptions.cpp
Expand Up @@ -56,6 +56,16 @@ void LangOptions::remapPathPrefix(SmallString<256> &Path) const {
break;
}

std::string LangOptions::getOpenCLVersionString() const {
std::string Result;
{
llvm::raw_string_ostream Out(Result);
Out << (OpenCLCPlusPlus ? "C++ for OpenCL" : "OpenCL C") << " version "
<< getOpenCLVersionTuple().getAsString();
}
return Result;
}

FPOptions FPOptions::defaultWithoutTrailingStorage(const LangOptions &LO) {
FPOptions result(LO);
return result;
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -503,9 +503,10 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
// -cl-strict-aliasing needs to emit diagnostic in the case where CL > 1.0.
// This option should be deprecated for CL > 1.0 because
// this option was added for compatibility with OpenCL 1.0.
if (Args.getLastArg(OPT_cl_strict_aliasing) && LangOpts.OpenCLVersion > 100)
if (Args.getLastArg(OPT_cl_strict_aliasing) &&
(LangOpts.OpenCLCPlusPlus || LangOpts.OpenCLVersion > 100))
Diags.Report(diag::warn_option_invalid_ocl_version)
<< LangOpts.getOpenCLVersionTuple().getAsString()
<< LangOpts.getOpenCLVersionString()
<< Args.getLastArg(OPT_cl_strict_aliasing)->getAsString(Args);

if (Arg *A = Args.getLastArg(OPT_fdefault_calling_conv_EQ)) {
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Parse/ParseDecl.cpp
Expand Up @@ -4182,9 +4182,8 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
<< FixItHint::CreateRemoval(
SourceRange(Loc, DS.getEndLoc()));
else if (DiagID == diag::err_opencl_unknown_type_specifier) {
Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus
<< getLangOpts().getOpenCLVersionTuple().getAsString()
<< PrevSpec << isStorageClass;
Diag(Loc, DiagID) << getLangOpts().getOpenCLVersionString() << PrevSpec
<< isStorageClass;
} else
Diag(Loc, DiagID) << PrevSpec;
}
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/SemaDecl.cpp
Expand Up @@ -7330,10 +7330,9 @@ NamedDecl *Sema::ActOnVariableDeclarator(

DeclSpec::TSCS TSC = D.getDeclSpec().getThreadStorageClassSpec();
if (TSC != TSCS_unspecified) {
bool IsCXX = getLangOpts().OpenCLCPlusPlus;
Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
diag::err_opencl_unknown_type_specifier)
<< IsCXX << getLangOpts().getOpenCLVersionTuple().getAsString()
<< getLangOpts().getOpenCLVersionString()
<< DeclSpec::getSpecifierName(TSC) << 1;
NewVD->setInvalidDecl();
}
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Sema/SemaDeclAttr.cpp
Expand Up @@ -7488,12 +7488,12 @@ static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
}

static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
if (S.LangOpts.OpenCLVersion != 200)
if (S.LangOpts.OpenCLVersion < 200 && !S.LangOpts.OpenCLCPlusPlusVersion)
S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
<< AL << "2.0" << 0;
<< AL << "2.0" << 1;
else
S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored) << AL
<< "2.0";
S.Diag(AL.getLoc(), diag::warn_opencl_attr_deprecated_ignored)
<< AL << S.LangOpts.getOpenCLVersionString();
}

static void handleOpenCLAccessAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Expand Down
10 changes: 6 additions & 4 deletions clang/test/Frontend/opencl.cl
Expand Up @@ -2,14 +2,15 @@
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=CL1.1 -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=CL1.2 -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=CL2.0 -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=clc++ -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=clc++1.0 -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -fblocks -DBLOCKS -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=CL1.1 -fblocks -DBLOCKS -DSYNTAX
// RUN: %clang_cc1 %s -verify -fsyntax-only -cl-std=CL1.2 -fblocks -DBLOCKS -DSYNTAX
// RUN: %clang_cc1 %s -triple amdgcn--amdhsa -x c -std=c99 -verify -fsyntax-only -DSYNTAX
// RUN: %clang_cc1 -cl-std=CL1.1 -cl-strict-aliasing -fblocks %s 2>&1 | FileCheck --check-prefix=CHECK-INVALID-OPENCL-VERSION11 %s
// RUN: %clang_cc1 -cl-std=CL1.2 -cl-strict-aliasing -fblocks %s 2>&1 | FileCheck --check-prefix=CHECK-INVALID-OPENCL-VERSION12 %s
// RUN: %clang_cc1 -cl-std=CL2.0 -cl-strict-aliasing %s 2>&1 | FileCheck --check-prefix=CHECK-INVALID-OPENCL-VERSION20 %s
// RUN: %clang_cc1 -cl-std=clc++1.0 -cl-strict-aliasing -fblocks %s 2>&1 | FileCheck --check-prefix=CHECK-INVALID-OPENCLCPP-VERSION10 %s

#ifdef SYNTAX
class test{
Expand All @@ -30,6 +31,7 @@ typedef int (^bl_t)(void);
// expected-error@-6{{blocks support disabled - compile with -fblocks or pick a deployment target that supports them}}
#endif

// CHECK-INVALID-OPENCL-VERSION11: warning: OpenCL version 1.1 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCL-VERSION12: warning: OpenCL version 1.2 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCL-VERSION20: warning: OpenCL version 2.0 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCL-VERSION11: warning: OpenCL C version 1.1 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCL-VERSION12: warning: OpenCL C version 1.2 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCL-VERSION20: warning: OpenCL C version 2.0 does not support the option '-cl-strict-aliasing'
// CHECK-INVALID-OPENCLCPP-VERSION10: warning: C++ for OpenCL version 1.0 does not support the option '-cl-strict-aliasing'
5 changes: 3 additions & 2 deletions clang/test/SemaOpenCL/ext_vectors.cl
@@ -1,6 +1,7 @@
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL1.1
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL2.0
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=clc++1.0

typedef float float4 __attribute__((ext_vector_type(4)));

Expand All @@ -9,12 +10,12 @@ void test_ext_vector_accessors(float4 V) {

V = V.abgr;
#if (__OPENCL_C_VERSION__ < 300)
// expected-warning@-2 {{vector component name 'a' is an OpenCL C version 3.0 feature}}
// expected-warning@-2 {{vector component name 'a' is a feature from OpenCL version 3.0 onwards}}
#endif

V = V.xyzr;
// expected-error@-1 {{illegal vector component name 'r'}}
#if (__OPENCL_C_VERSION__ < 300)
// expected-warning@-3 {{vector component name 'r' is an OpenCL C version 3.0 feature}}
// expected-warning@-3 {{vector component name 'r' is a feature from OpenCL version 3.0 onwards}}
#endif
}
11 changes: 7 additions & 4 deletions clang/test/SemaOpenCL/nosvm.cl
@@ -1,13 +1,16 @@
// RUN: %clang_cc1 -verify %s
// RUN: %clang_cc1 -verify -cl-std=CL2.0 -D CL20 %s
// RUN: %clang_cc1 -verify -cl-std=CL2.0 %s
// RUN: %clang_cc1 -verify -cl-std=clc++1.0 %s
// RUN: %clang_cc1 -verify -x c -D NOCL %s

#ifndef NOCL
kernel void f(__attribute__((nosvm)) global int* a);
#ifndef CL20
// expected-error@-2 {{'nosvm' attribute requires OpenCL version 2.0}}
#if (__OPENCL_C_VERSION__ == 200)
// expected-warning@-2 {{'nosvm' attribute is deprecated and ignored in OpenCL C version 2.0}}
#elif (__OPENCL_CPP_VERSION__ == 100)
// expected-warning@-4 {{'nosvm' attribute is deprecated and ignored in C++ for OpenCL version 1.0}}
#else
// expected-warning@-4 {{'nosvm' attribute is deprecated and ignored in OpenCL version 2.0}}
// expected-error@-6 {{attribute 'nosvm' is supported in the OpenCL version 2.0 onwards}}
#endif

__attribute__((nosvm)) void g(); // expected-warning {{'nosvm' attribute only applies to variables}}
Expand Down

0 comments on commit cfdfb75

Please sign in to comment.