1//===--- OpenCLOptions.h ----------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9/// 10/// \file 11/// \brief Defines the clang::OpenCLOptions class. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_BASIC_OPENCLOPTIONS_H 16#define LLVM_CLANG_BASIC_OPENCLOPTIONS_H 17 18#include "llvm/ADT/StringMap.h" 19 20namespace clang { 21 22/// \brief OpenCL supported extensions and optional core features 23class OpenCLOptions { 24 struct Info { 25 bool Supported; // Is this option supported 26 bool Enabled; // Is this option enabled 27 unsigned Avail; // Option starts to be available in this OpenCL version 28 unsigned Core; // Option becomes (optional) core feature in this OpenCL 29 // version 30 Info(bool S = false, bool E = false, unsigned A = 100, unsigned C = ~0U) 31 :Supported(S), Enabled(E), Avail(A), Core(C){} 32 }; 33 llvm::StringMap<Info> OptMap; 34public: 35 bool isKnown(llvm::StringRef Ext) const { 36 return OptMap.find(Ext) != OptMap.end(); 37 } 38 39 bool isEnabled(llvm::StringRef Ext) const { 40 return OptMap.find(Ext)->second.Enabled; 41 } 42 43 // Is supported as either an extension or an (optional) core feature for 44 // OpenCL version \p CLVer. 45 bool isSupported(llvm::StringRef Ext, unsigned CLVer) const { 46 auto I = OptMap.find(Ext)->getValue(); 47 return I.Supported && I.Avail <= CLVer; 48 } 49 50 // Is supported (optional) OpenCL core features for OpenCL version \p CLVer. 51 // For supported extension, return false. 52 bool isSupportedCore(llvm::StringRef Ext, unsigned CLVer) const { 53 auto I = OptMap.find(Ext)->getValue(); 54 return I.Supported && I.Avail <= CLVer && 55 I.Core != ~0U && CLVer >= I.Core; 56 } 57 58 // Is supported OpenCL extension for OpenCL version \p CLVer. 59 // For supported (optional) core feature, return false. 60 bool isSupportedExtension(llvm::StringRef Ext, unsigned CLVer) const { 61 auto I = OptMap.find(Ext)->getValue(); 62 return I.Supported && I.Avail <= CLVer && 63 (I.Core == ~0U || CLVer < I.Core); 64 } 65 66 void enable(llvm::StringRef Ext, bool V = true) { 67 OptMap[Ext].Enabled = V; 68 } 69 70 /// \brief Enable or disable support for OpenCL extensions 71 /// \param Ext name of the extension optionally prefixed with 72 /// '+' or '-' 73 /// \param V used when \p Ext is not prefixed by '+' or '-' 74 void support(llvm::StringRef Ext, bool V = true) { 75 assert(!Ext.empty() && "Extension is empty."); 76 77 switch (Ext[0]) { 78 case '+': 79 V = true; 80 Ext = Ext.drop_front(); 81 break; 82 case '-': 83 V = false; 84 Ext = Ext.drop_front(); 85 break; 86 } 87 88 if (Ext.equals("all")) { 89 supportAll(V); 90 return; 91 } 92 OptMap[Ext].Supported = V; 93 } 94 95 OpenCLOptions(){ 96#define OPENCLEXT_INTERNAL(Ext, AvailVer, CoreVer) \ 97 OptMap[#Ext].Avail = AvailVer; \ 98 OptMap[#Ext].Core = CoreVer; 99#include "clang/Basic/OpenCLExtensions.def" 100 } 101 102 void addSupport(const OpenCLOptions &Opts) { 103 for (auto &I:Opts.OptMap) 104 if (I.second.Supported) 105 OptMap[I.getKey()].Supported = true; 106 } 107 108 void copy(const OpenCLOptions &Opts) { 109 OptMap = Opts.OptMap; 110 } 111 112 // Turn on or off support of all options. 113 void supportAll(bool On = true) { 114 for (llvm::StringMap<Info>::iterator I = OptMap.begin(), 115 E = OptMap.end(); I != E; ++I) 116 I->second.Supported = On; 117 } 118 119 void disableAll() { 120 for (llvm::StringMap<Info>::iterator I = OptMap.begin(), 121 E = OptMap.end(); I != E; ++I) 122 I->second.Enabled = false; 123 } 124 125 void enableSupportedCore(unsigned CLVer) { 126 for (llvm::StringMap<Info>::iterator I = OptMap.begin(), 127 E = OptMap.end(); I != E; ++I) 128 if (isSupportedCore(I->getKey(), CLVer)) 129 I->second.Enabled = true; 130 } 131 132 friend class ASTWriter; 133 friend class ASTReader; 134}; 135 136} // end namespace clang 137 138#endif 139