Skip to content

Commit f9bc1b3

Browse files
committed
[OpenCL] Defines helper function for kernel language compatible OpenCL version
This change defines a helper function getOpenCLCompatibleVersion() inside LangOptions class. The function contains mapping between C++ for OpenCL versions and their corresponding compatible OpenCL versions. This mapping function should be updated each time a new C++ for OpenCL language version is introduced. The helper function is expected to simplify conditions on OpenCL C and C++ for OpenCL versions inside compiler code. Code refactoring performed. Differential Revision: https://reviews.llvm.org/D108693
1 parent c1184ca commit f9bc1b3

14 files changed

+44
-36
lines changed

clang/include/clang/Basic/LangOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ class LangOptions : public LangOptionsBase {
444444
/// Return the OpenCL C or C++ version as a VersionTuple.
445445
VersionTuple getOpenCLVersionTuple() const;
446446

447+
/// Return the OpenCL version that kernel language is compatible with
448+
unsigned getOpenCLCompatibleVersion() const;
449+
447450
/// Return the OpenCL C or C++ for OpenCL language name and version
448451
/// as a string.
449452
std::string getOpenCLVersionString() const;

clang/include/clang/Basic/OpenCLOptions.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static inline OpenCLVersionID encodeOpenCLVersion(unsigned OpenCLVersion) {
5858
// mask.
5959
static inline bool isOpenCLVersionContainedInMask(const LangOptions &LO,
6060
unsigned Mask) {
61-
auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
61+
auto CLVer = LO.getOpenCLCompatibleVersion();
6262
OpenCLVersionID Code = encodeOpenCLVersion(CLVer);
6363
return Mask & Code;
6464
}
@@ -79,7 +79,7 @@ class OpenCLOptions {
7979
// the __opencl_c_program_scope_global_variables feature is supported
8080
// C++ for OpenCL inherits rule from OpenCL C v2.0.
8181
bool areProgramScopeVariablesSupported(const LangOptions &Opts) const {
82-
return Opts.OpenCLCPlusPlus || Opts.OpenCLVersion == 200 ||
82+
return Opts.getOpenCLCompatibleVersion() == 200 ||
8383
(Opts.OpenCLVersion == 300 &&
8484
isSupported("__opencl_c_program_scope_global_variables", Opts));
8585
}
@@ -115,8 +115,7 @@ class OpenCLOptions {
115115
// Is option available in OpenCL version \p LO.
116116
bool isAvailableIn(const LangOptions &LO) const {
117117
// In C++ mode all extensions should work at least as in v2.0.
118-
auto CLVer = LO.OpenCLCPlusPlus ? 200 : LO.OpenCLVersion;
119-
return CLVer >= Avail;
118+
return LO.getOpenCLCompatibleVersion() >= Avail;
120119
}
121120

122121
// Is core option in OpenCL version \p LO.

clang/lib/Basic/Builtins.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
7272
bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
7373
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
7474
bool OclC2Unsupported =
75-
(LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) &&
75+
(LangOpts.getOpenCLCompatibleVersion() != 200) &&
7676
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
7777
bool OclCUnsupported = !LangOpts.OpenCL &&
7878
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);

clang/lib/Basic/LangOptions.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ VersionTuple LangOptions::getOpenCLVersionTuple() const {
5252
return VersionTuple(Ver / 100, (Ver % 100) / 10);
5353
}
5454

55+
unsigned LangOptions::getOpenCLCompatibleVersion() const {
56+
if (!OpenCLCPlusPlus)
57+
return OpenCLVersion;
58+
if (OpenCLCPlusPlusVersion == 100)
59+
return 200;
60+
if (OpenCLCPlusPlusVersion == 202100)
61+
return 300;
62+
llvm_unreachable("Unknown OpenCL version");
63+
}
64+
5565
void LangOptions::remapPathPrefix(SmallString<256> &Path) const {
5666
for (const auto &Entry : MacroPrefixMap)
5767
if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))

clang/lib/Basic/Targets.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ bool TargetInfo::validateOpenCLTarget(const LangOptions &Opts,
745745

746746
// Validate that feature macros are set properly for OpenCL C 3.0.
747747
// In other cases assume that target is always valid.
748-
if (Opts.OpenCLCPlusPlus || Opts.OpenCLVersion < 300)
748+
if (Opts.getOpenCLCompatibleVersion() < 300)
749749
return true;
750750

751751
return OpenCLOptions::diagnoseUnsupportedFeatureDependencies(*this, Diags) &&

clang/lib/CodeGen/CodeGenModule.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,9 @@ void CodeGenModule::Release() {
733733
if (getTriple().isSPIR()) {
734734
// SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
735735
// opencl.spir.version named metadata.
736-
// C++ is backwards compatible with OpenCL v2.0.
737-
auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
736+
// C++ for OpenCL has a distinct mapping for version compatibility with
737+
// OpenCL.
738+
auto Version = LangOpts.getOpenCLCompatibleVersion();
738739
llvm::Metadata *SPIRVerElts[] = {
739740
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
740741
Int32Ty, Version / 100)),
@@ -827,9 +828,8 @@ void CodeGenModule::Release() {
827828
void CodeGenModule::EmitOpenCLMetadata() {
828829
// SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
829830
// opencl.ocl.version named metadata node.
830-
// C++ is backwards compatible with OpenCL v2.0.
831-
// FIXME: We might need to add CXX version at some point too?
832-
auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
831+
// C++ for OpenCL has a distinct mapping for versions compatibile with OpenCL.
832+
auto Version = LangOpts.getOpenCLCompatibleVersion();
833833
llvm::Metadata *OCLVerElts[] = {
834834
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
835835
Int32Ty, Version / 100)),

clang/lib/Frontend/CompilerInvocation.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
504504
// This option should be deprecated for CL > 1.0 because
505505
// this option was added for compatibility with OpenCL 1.0.
506506
if (Args.getLastArg(OPT_cl_strict_aliasing) &&
507-
(LangOpts.OpenCLCPlusPlus || LangOpts.OpenCLVersion > 100))
507+
(LangOpts.getOpenCLCompatibleVersion() > 100))
508508
Diags.Report(diag::warn_option_invalid_ocl_version)
509509
<< LangOpts.getOpenCLVersionString()
510510
<< Args.getLastArg(OPT_cl_strict_aliasing)->getAsString(Args);
@@ -3174,9 +3174,8 @@ void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
31743174
Opts.ZVector = 0;
31753175
Opts.setDefaultFPContractMode(LangOptions::FPM_On);
31763176
Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
3177-
Opts.OpenCLPipes = Opts.OpenCLCPlusPlus || Opts.OpenCLVersion == 200;
3178-
Opts.OpenCLGenericAddressSpace =
3179-
Opts.OpenCLCPlusPlus || Opts.OpenCLVersion == 200;
3177+
Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
3178+
Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
31803179

31813180
// Include default header file for OpenCL.
31823181
if (Opts.IncludeDefaultHeader) {

clang/lib/Parse/ParseDecl.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -3985,8 +3985,8 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
39853985
isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
39863986
break;
39873987
case tok::kw_pipe:
3988-
if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 &&
3989-
!getLangOpts().OpenCLCPlusPlus)) {
3988+
if (!getLangOpts().OpenCL ||
3989+
getLangOpts().getOpenCLCompatibleVersion() < 200) {
39903990
// OpenCL 2.0 and later define this keyword. OpenCL 1.2 and earlier
39913991
// should support the "pipe" word as identifier.
39923992
Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
@@ -5171,8 +5171,8 @@ bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
51715171

51725172
// OpenCL 2.0 and later define this keyword.
51735173
case tok::kw_pipe:
5174-
return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) ||
5175-
getLangOpts().OpenCLCPlusPlus;
5174+
return getLangOpts().OpenCL &&
5175+
getLangOpts().getOpenCLCompatibleVersion() >= 200;
51765176

51775177
case tok::identifier: // foo::bar
51785178
// Unfortunate hack to support "Class.factoryMethod" notation.
@@ -5702,8 +5702,8 @@ static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
57025702
return true;
57035703

57045704
// OpenCL 2.0 and later define this keyword.
5705-
if (Kind == tok::kw_pipe &&
5706-
((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus))
5705+
if (Kind == tok::kw_pipe && Lang.OpenCL &&
5706+
Lang.getOpenCLCompatibleVersion() >= 200)
57075707
return true;
57085708

57095709
if (!Lang.CPlusPlus)

clang/lib/Sema/DeclSpec.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,7 @@ bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
631631
case SCS_extern:
632632
case SCS_private_extern:
633633
case SCS_static:
634-
if (S.getLangOpts().OpenCLVersion < 120 &&
635-
!S.getLangOpts().OpenCLCPlusPlus) {
634+
if (S.getLangOpts().getOpenCLCompatibleVersion() < 120) {
636635
DiagID = diag::err_opencl_unknown_type_specifier;
637636
PrevSpec = getSpecifierName(SC);
638637
return true;

clang/lib/Sema/Sema.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void Sema::Initialize() {
330330
Context.getTargetInfo().getSupportedOpenCLOpts(), getLangOpts());
331331
addImplicitTypedef("sampler_t", Context.OCLSamplerTy);
332332
addImplicitTypedef("event_t", Context.OCLEventTy);
333-
if (getLangOpts().OpenCLCPlusPlus || getLangOpts().OpenCLVersion >= 200) {
333+
if (getLangOpts().getOpenCLCompatibleVersion() >= 200) {
334334
addImplicitTypedef("clk_event_t", Context.OCLClkEventTy);
335335
addImplicitTypedef("queue_t", Context.OCLQueueTy);
336336
if (getLangOpts().OpenCLPipes)

clang/lib/Sema/SemaDecl.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -8834,8 +8834,7 @@ static void checkIsValidOpenCLKernelParameter(
88348834
// OpenCL v3.0 s6.11.a:
88358835
// A kernel function argument cannot be declared as a pointer to a pointer
88368836
// type. [...] This restriction only applies to OpenCL C 1.2 or below.
8837-
if (S.getLangOpts().OpenCLVersion <= 120 &&
8838-
!S.getLangOpts().OpenCLCPlusPlus) {
8837+
if (S.getLangOpts().getOpenCLCompatibleVersion() <= 120) {
88398838
S.Diag(Param->getLocation(), diag::err_opencl_ptrptr_kernel_param);
88408839
D.setInvalidType();
88418840
return;
@@ -10017,7 +10016,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1001710016

1001810017
// OpenCL 2.0 pipe restrictions forbids pipe packet types to be non-value
1001910018
// types.
10020-
if (getLangOpts().OpenCLVersion >= 200 || getLangOpts().OpenCLCPlusPlus) {
10019+
if (getLangOpts().getOpenCLCompatibleVersion() >= 200) {
1002110020
if(const PipeType *PipeTy = PT->getAs<PipeType>()) {
1002210021
QualType ElemTy = PipeTy->getElementType();
1002310022
if (ElemTy->isReferenceType() || ElemTy->isPointerType()) {

clang/lib/Sema/SemaDeclAttr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7519,7 +7519,7 @@ static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
75197519
}
75207520

75217521
static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
7522-
if (S.LangOpts.OpenCLVersion < 200 && !S.LangOpts.OpenCLCPlusPlusVersion)
7522+
if (S.LangOpts.getOpenCLCompatibleVersion() < 200)
75237523
S.Diag(AL.getLoc(), diag::err_attribute_requires_opencl_version)
75247524
<< AL << "2.0" << 1;
75257525
else

clang/lib/Sema/SemaExpr.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -12263,7 +12263,7 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
1226312263
return computeResultTy();
1226412264
}
1226512265

12266-
if (getLangOpts().OpenCLVersion >= 200 || getLangOpts().OpenCLCPlusPlus) {
12266+
if (getLangOpts().getOpenCLCompatibleVersion() >= 200) {
1226712267
if (LHSType->isClkEventT() && RHSType->isClkEventT()) {
1226812268
return computeResultTy();
1226912269
}
@@ -12522,8 +12522,9 @@ QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS,
1252212522
/*AllowBoolConversions*/false);
1252312523
if (vType.isNull())
1252412524
return InvalidOperands(Loc, LHS, RHS);
12525-
if (getLangOpts().OpenCL && getLangOpts().OpenCLVersion < 120 &&
12526-
!getLangOpts().OpenCLCPlusPlus && vType->hasFloatingRepresentation())
12525+
if (getLangOpts().OpenCL &&
12526+
getLangOpts().getOpenCLCompatibleVersion() < 120 &&
12527+
vType->hasFloatingRepresentation())
1252712528
return InvalidOperands(Loc, LHS, RHS);
1252812529
// FIXME: The check for C++ here is for GCC compatibility. GCC rejects the
1252912530
// usage of the logical operators && and || with vectors in C. This
@@ -14974,8 +14975,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
1497414975
}
1497514976
} else if (resultType->isExtVectorType()) {
1497614977
if (Context.getLangOpts().OpenCL &&
14977-
Context.getLangOpts().OpenCLVersion < 120 &&
14978-
!Context.getLangOpts().OpenCLCPlusPlus) {
14978+
Context.getLangOpts().getOpenCLCompatibleVersion() < 120) {
1497914979
// OpenCL v1.1 6.3.h: The logical operator not (!) does not
1498014980
// operate on vector float types.
1498114981
QualType T = resultType->castAs<ExtVectorType>()->getElementType();

clang/lib/Sema/SemaType.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,7 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
13691369
// value being declared, poison it as invalid so we don't get chains of
13701370
// errors.
13711371
declarator.setInvalidType(true);
1372-
} else if ((S.getLangOpts().OpenCLVersion >= 200 ||
1373-
S.getLangOpts().OpenCLCPlusPlus) &&
1372+
} else if (S.getLangOpts().getOpenCLCompatibleVersion() >= 200 &&
13741373
DS.isTypeSpecPipe()) {
13751374
S.Diag(DeclLoc, diag::err_missing_actual_pipe_type)
13761375
<< DS.getSourceRange();
@@ -5093,7 +5092,7 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
50935092
"__cl_clang_variadic_functions", S.getLangOpts()) &&
50945093
!(D.getIdentifier() &&
50955094
((D.getIdentifier()->getName() == "printf" &&
5096-
(LangOpts.OpenCLCPlusPlus || LangOpts.OpenCLVersion >= 120)) ||
5095+
LangOpts.getOpenCLCompatibleVersion() >= 120) ||
50975096
D.getIdentifier()->getName().startswith("__")))) {
50985097
S.Diag(D.getIdentifierLoc(), diag::err_opencl_variadic_function);
50995098
D.setInvalidType(true);

0 commit comments

Comments
 (0)