Path: blob/main/contrib/llvm-project/clang/lib/Basic/OpenCLOptions.cpp
35232 views
//===--- OpenCLOptions.cpp---------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Basic/OpenCLOptions.h"9#include "clang/Basic/Diagnostic.h"10#include "clang/Basic/TargetInfo.h"1112namespace clang {1314// First feature in a pair requires the second one to be supported.15static const std::pair<StringRef, StringRef> DependentFeaturesList[] = {16{"__opencl_c_read_write_images", "__opencl_c_images"},17{"__opencl_c_3d_image_writes", "__opencl_c_images"},18{"__opencl_c_pipes", "__opencl_c_generic_address_space"},19{"__opencl_c_device_enqueue", "__opencl_c_generic_address_space"},20{"__opencl_c_device_enqueue", "__opencl_c_program_scope_global_variables"}};2122// Extensions and equivalent feature pairs.23static const std::pair<StringRef, StringRef> FeatureExtensionMap[] = {24{"cl_khr_fp64", "__opencl_c_fp64"},25{"cl_khr_3d_image_writes", "__opencl_c_3d_image_writes"}};2627bool OpenCLOptions::isKnown(llvm::StringRef Ext) const {28return OptMap.contains(Ext);29}3031bool OpenCLOptions::isAvailableOption(llvm::StringRef Ext,32const LangOptions &LO) const {33if (!isKnown(Ext))34return false;3536auto &OptInfo = OptMap.find(Ext)->getValue();37if (OptInfo.isCoreIn(LO) || OptInfo.isOptionalCoreIn(LO))38return isSupported(Ext, LO);3940return isEnabled(Ext);41}4243bool OpenCLOptions::isEnabled(llvm::StringRef Ext) const {44auto I = OptMap.find(Ext);45return I != OptMap.end() && I->getValue().Enabled;46}4748bool OpenCLOptions::isWithPragma(llvm::StringRef Ext) const {49auto E = OptMap.find(Ext);50return E != OptMap.end() && E->second.WithPragma;51}5253bool OpenCLOptions::isSupported(llvm::StringRef Ext,54const LangOptions &LO) const {55auto I = OptMap.find(Ext);56return I != OptMap.end() && I->getValue().Supported &&57I->getValue().isAvailableIn(LO);58}5960bool OpenCLOptions::isSupportedCore(llvm::StringRef Ext,61const LangOptions &LO) const {62auto I = OptMap.find(Ext);63return I != OptMap.end() && I->getValue().Supported &&64I->getValue().isCoreIn(LO);65}6667bool OpenCLOptions::isSupportedOptionalCore(llvm::StringRef Ext,68const LangOptions &LO) const {69auto I = OptMap.find(Ext);70return I != OptMap.end() && I->getValue().Supported &&71I->getValue().isOptionalCoreIn(LO);72}7374bool OpenCLOptions::isSupportedCoreOrOptionalCore(llvm::StringRef Ext,75const LangOptions &LO) const {76return isSupportedCore(Ext, LO) || isSupportedOptionalCore(Ext, LO);77}7879bool OpenCLOptions::isSupportedExtension(llvm::StringRef Ext,80const LangOptions &LO) const {81auto I = OptMap.find(Ext);82return I != OptMap.end() && I->getValue().Supported &&83I->getValue().isAvailableIn(LO) &&84!isSupportedCoreOrOptionalCore(Ext, LO);85}8687void OpenCLOptions::enable(llvm::StringRef Ext, bool V) {88OptMap[Ext].Enabled = V;89}9091void OpenCLOptions::acceptsPragma(llvm::StringRef Ext, bool V) {92OptMap[Ext].WithPragma = V;93}9495void OpenCLOptions::support(llvm::StringRef Ext, bool V) {96assert(!Ext.empty() && "Extension is empty.");97assert(Ext[0] != '+' && Ext[0] != '-');98OptMap[Ext].Supported = V;99}100101OpenCLOptions::OpenCLOptions() {102#define OPENCL_GENERIC_EXTENSION(Ext, ...) \103OptMap.insert_or_assign(#Ext, OpenCLOptionInfo{__VA_ARGS__});104#include "clang/Basic/OpenCLExtensions.def"105}106107void OpenCLOptions::addSupport(const llvm::StringMap<bool> &FeaturesMap,108const LangOptions &Opts) {109for (const auto &F : FeaturesMap) {110const auto &Name = F.getKey();111if (F.getValue() && isKnown(Name) && OptMap[Name].isAvailableIn(Opts))112support(Name);113}114}115116void OpenCLOptions::disableAll() {117for (auto &Opt : OptMap)118Opt.getValue().Enabled = false;119}120121bool OpenCLOptions::diagnoseUnsupportedFeatureDependencies(122const TargetInfo &TI, DiagnosticsEngine &Diags) {123auto OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();124125bool IsValid = true;126for (auto &FeaturePair : DependentFeaturesList) {127auto Feature = FeaturePair.first;128auto Dep = FeaturePair.second;129if (TI.hasFeatureEnabled(OpenCLFeaturesMap, Feature) &&130!TI.hasFeatureEnabled(OpenCLFeaturesMap, Dep)) {131IsValid = false;132Diags.Report(diag::err_opencl_feature_requires) << Feature << Dep;133}134}135return IsValid;136}137138bool OpenCLOptions::diagnoseFeatureExtensionDifferences(139const TargetInfo &TI, DiagnosticsEngine &Diags) {140auto OpenCLFeaturesMap = TI.getSupportedOpenCLOpts();141142bool IsValid = true;143for (auto &ExtAndFeat : FeatureExtensionMap)144if (TI.hasFeatureEnabled(OpenCLFeaturesMap, ExtAndFeat.first) !=145TI.hasFeatureEnabled(OpenCLFeaturesMap, ExtAndFeat.second)) {146IsValid = false;147Diags.Report(diag::err_opencl_extension_and_feature_differs)148<< ExtAndFeat.first << ExtAndFeat.second;149}150return IsValid;151}152153} // end namespace clang154155156