alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1 | // Copyright 2019 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef CLSPV_LIB_BUILTINS_H_ |
| 16 | #define CLSPV_LIB_BUILTINS_H_ |
| 17 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame^] | 18 | #include <string> |
| 19 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame^] | 23 | #include "BuiltinsEnum.h" |
| 24 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 25 | namespace clspv { |
| 26 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame^] | 27 | namespace Builtins { |
| 28 | |
| 29 | struct ParamTypeInfo { |
| 30 | bool is_signed = false; // is element type signed |
| 31 | llvm::Type::TypeID type_id = llvm::Type::VoidTyID; // element type |
| 32 | int byte_len = 0; // element byte length |
| 33 | int vector_size = 0; // number of elements (0 == not a vector) |
| 34 | std::string name; // struct name |
| 35 | }; |
| 36 | |
| 37 | class FunctionInfo { |
| 38 | bool is_valid_ = false; |
| 39 | Builtins::BuiltinType type_ = Builtins::kBuiltinNone; |
| 40 | std::string name_; |
| 41 | ParamTypeInfo return_type_; // only used for convert, where return type is |
| 42 | // embedded in the name |
| 43 | std::vector<ParamTypeInfo> params_; |
| 44 | |
| 45 | public: |
| 46 | FunctionInfo() = default; |
| 47 | FunctionInfo(const std::string &_name); |
| 48 | |
| 49 | bool isValid() const { return is_valid_; } |
| 50 | Builtins::BuiltinType getType() const { return type_; } |
| 51 | operator int() const { return type_; } |
| 52 | const std::string &getName() const { return name_; } |
| 53 | const ParamTypeInfo &getParameter(size_t arg) const; |
| 54 | const ParamTypeInfo &getLastParameter() const { return params_.back(); } |
| 55 | size_t getParameterCount() const { return params_.size(); } |
| 56 | const ParamTypeInfo &getReturnType() const { return return_type_; } |
| 57 | |
| 58 | private: |
| 59 | bool GetFromMangledNameCheck(const std::string &mangled_name); |
| 60 | }; |
| 61 | |
| 62 | /// Primary Interface |
| 63 | // returns a FunctionInfo representation of the mangled name |
| 64 | const FunctionInfo &Lookup(const std::string &mangled_name); |
| 65 | inline const FunctionInfo &Lookup(llvm::StringRef mangled_name) { |
| 66 | return Lookup(mangled_name.str()); |
| 67 | } |
| 68 | inline const FunctionInfo &Lookup(llvm::Function *func) { |
| 69 | return Lookup(func->getName()); |
| 70 | } |
| 71 | |
| 72 | /// Legacy |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 73 | // Returns true if the function is an OpenCL image builtin. |
| 74 | bool IsImageBuiltin(llvm::StringRef name); |
| 75 | inline bool IsImageBuiltin(llvm::Function *f) { |
| 76 | return IsImageBuiltin(f->getName()); |
| 77 | } |
| 78 | |
| 79 | // Returns true if the function is an OpenCL sampled image read. |
| 80 | bool IsSampledImageRead(llvm::StringRef name); |
| 81 | inline bool IsSampledImageRead(llvm::Function *f) { |
| 82 | return IsSampledImageRead(f->getName()); |
| 83 | } |
| 84 | |
| 85 | // Returns true if the function is an OpenCL sampled image read of float type. |
| 86 | bool IsFloatSampledImageRead(llvm::StringRef name); |
| 87 | inline bool IsFloatSampledImageRead(llvm::Function *f) { |
| 88 | return IsFloatSampledImageRead(f->getName()); |
| 89 | } |
| 90 | |
| 91 | // Returns true if the function is an OpenCL sampled image read of uint type. |
| 92 | bool IsUintSampledImageRead(llvm::StringRef name); |
| 93 | inline bool IsUintSampledImageRead(llvm::Function *f) { |
| 94 | return IsUintSampledImageRead(f->getName()); |
| 95 | } |
| 96 | |
| 97 | // Returns true if the function is an OpenCL sampled image read of int type. |
| 98 | bool IsIntSampledImageRead(llvm::StringRef name); |
| 99 | inline bool IsIntSampledImageRead(llvm::Function *f) { |
| 100 | return IsIntSampledImageRead(f->getName()); |
| 101 | } |
| 102 | |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 103 | // Returns true if the function is an OpenCL image read. |
| 104 | bool IsUnsampledImageRead(llvm::StringRef name); |
| 105 | inline bool IsUnsampledImageRead(llvm::Function *f) { |
| 106 | return IsUnsampledImageRead(f->getName()); |
| 107 | } |
| 108 | |
| 109 | // Returns true if the function is an OpenCL image read of float type. |
| 110 | bool IsFloatUnsampledImageRead(llvm::StringRef name); |
| 111 | inline bool IsFloatUnsampledImageRead(llvm::Function *f) { |
| 112 | return IsFloatUnsampledImageRead(f->getName()); |
| 113 | } |
| 114 | |
| 115 | // Returns true if the function is an OpenCL image read of uint type. |
| 116 | bool IsUintUnsampledImageRead(llvm::StringRef name); |
| 117 | inline bool IsUintUnsampledImageRead(llvm::Function *f) { |
| 118 | return IsUintUnsampledImageRead(f->getName()); |
| 119 | } |
| 120 | |
| 121 | // Returns true if the function is an OpenCL image read of int type. |
| 122 | bool IsIntUnsampledImageRead(llvm::StringRef name); |
| 123 | inline bool IsIntUnsampledImageRead(llvm::Function *f) { |
| 124 | return IsIntUnsampledImageRead(f->getName()); |
| 125 | } |
| 126 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 127 | // Returns true if the function is an OpenCL image write. |
| 128 | bool IsImageWrite(llvm::StringRef name); |
| 129 | inline bool IsImageWrite(llvm::Function *f) { |
| 130 | return IsImageWrite(f->getName()); |
| 131 | } |
| 132 | |
| 133 | // Returns true if the function is an OpenCL image write of float type. |
| 134 | bool IsFloatImageWrite(llvm::StringRef name); |
| 135 | inline bool IsFloatImageWrite(llvm::Function *f) { |
| 136 | return IsFloatImageWrite(f->getName()); |
| 137 | } |
| 138 | |
| 139 | // Returns true if the function is an OpenCL image write of uint type. |
| 140 | bool IsUintImageWrite(llvm::StringRef name); |
| 141 | inline bool IsUintImageWrite(llvm::Function *f) { |
| 142 | return IsUintImageWrite(f->getName()); |
| 143 | } |
| 144 | |
| 145 | // Returns true if the function is an OpenCL image write of int type. |
| 146 | bool IsIntImageWrite(llvm::StringRef name); |
| 147 | inline bool IsIntImageWrite(llvm::Function *f) { |
| 148 | return IsIntImageWrite(f->getName()); |
| 149 | } |
| 150 | |
| 151 | // Returns true if the function is an OpenCL image height query. |
| 152 | bool IsGetImageHeight(llvm::StringRef name); |
| 153 | inline bool IsGetImageHeight(llvm::Function *f) { |
| 154 | return IsGetImageHeight(f->getName()); |
| 155 | } |
| 156 | |
| 157 | // Returns true if the function is an OpenCL image width query. |
| 158 | bool IsGetImageWidth(llvm::StringRef name); |
| 159 | inline bool IsGetImageWidth(llvm::Function *f) { |
| 160 | return IsGetImageWidth(f->getName()); |
| 161 | } |
| 162 | |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 163 | // Returns true if the function is an OpenCL image depth query. |
| 164 | bool IsGetImageDepth(llvm::StringRef name); |
| 165 | inline bool IsGetImageDepth(llvm::Function *f) { |
| 166 | return IsGetImageDepth(f->getName()); |
| 167 | } |
| 168 | |
| 169 | // Returns true if the function is an OpenCL image dim query. |
| 170 | bool IsGetImageDim(llvm::StringRef name); |
| 171 | inline bool IsGetImageDim(llvm::Function *f) { |
| 172 | return IsGetImageDim(f->getName()); |
| 173 | } |
| 174 | |
| 175 | // Returns true if the function is an OpenCL image query. |
| 176 | bool IsImageQuery(llvm::StringRef name); |
| 177 | inline bool IsImageQuery(llvm::Function *f) { |
| 178 | return IsImageQuery(f->getName()); |
| 179 | } |
| 180 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame^] | 181 | } // namespace Builtins |
| 182 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 183 | } // namespace clspv |
| 184 | |
| 185 | #endif // CLSPV_LIB_BUILTINS_H_ |