blob: 2b166898a656a4fd4c4bd485e4c57a6cf16d2abf [file] [log] [blame]
alan-bakerf67468c2019-11-25 15:51:49 -05001// 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
SJW173c7e92020-03-16 08:44:47 -050018#include <string>
19
alan-bakerf67468c2019-11-25 15:51:49 -050020#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Function.h"
22
SJW173c7e92020-03-16 08:44:47 -050023#include "BuiltinsEnum.h"
24
alan-bakerf67468c2019-11-25 15:51:49 -050025namespace clspv {
26
SJW173c7e92020-03-16 08:44:47 -050027namespace Builtins {
28
29struct ParamTypeInfo {
30 bool is_signed = false; // is element type signed
31 llvm::Type::TypeID type_id = llvm::Type::VoidTyID; // element type
SJW2c317da2020-03-23 07:39:13 -050032 uint32_t byte_len = 0; // element byte length
SJW173c7e92020-03-16 08:44:47 -050033 int vector_size = 0; // number of elements (0 == not a vector)
34 std::string name; // struct name
SJW2c317da2020-03-23 07:39:13 -050035
36 bool isSampler() const;
SJW173c7e92020-03-16 08:44:47 -050037};
38
39class FunctionInfo {
40 bool is_valid_ = false;
41 Builtins::BuiltinType type_ = Builtins::kBuiltinNone;
42 std::string name_;
43 ParamTypeInfo return_type_; // only used for convert, where return type is
44 // embedded in the name
45 std::vector<ParamTypeInfo> params_;
46
47public:
48 FunctionInfo() = default;
49 FunctionInfo(const std::string &_name);
50
51 bool isValid() const { return is_valid_; }
52 Builtins::BuiltinType getType() const { return type_; }
53 operator int() const { return type_; }
54 const std::string &getName() const { return name_; }
55 const ParamTypeInfo &getParameter(size_t arg) const;
56 const ParamTypeInfo &getLastParameter() const { return params_.back(); }
57 size_t getParameterCount() const { return params_.size(); }
58 const ParamTypeInfo &getReturnType() const { return return_type_; }
59
60private:
61 bool GetFromMangledNameCheck(const std::string &mangled_name);
62};
63
64/// Primary Interface
65// returns a FunctionInfo representation of the mangled name
66const FunctionInfo &Lookup(const std::string &mangled_name);
67inline const FunctionInfo &Lookup(llvm::StringRef mangled_name) {
68 return Lookup(mangled_name.str());
69}
70inline const FunctionInfo &Lookup(llvm::Function *func) {
71 return Lookup(func->getName());
72}
73
74/// Legacy
alan-bakerf67468c2019-11-25 15:51:49 -050075// Returns true if the function is an OpenCL image builtin.
76bool IsImageBuiltin(llvm::StringRef name);
77inline bool IsImageBuiltin(llvm::Function *f) {
78 return IsImageBuiltin(f->getName());
79}
80
81// Returns true if the function is an OpenCL sampled image read.
82bool IsSampledImageRead(llvm::StringRef name);
83inline bool IsSampledImageRead(llvm::Function *f) {
84 return IsSampledImageRead(f->getName());
85}
86
87// Returns true if the function is an OpenCL sampled image read of float type.
88bool IsFloatSampledImageRead(llvm::StringRef name);
89inline bool IsFloatSampledImageRead(llvm::Function *f) {
90 return IsFloatSampledImageRead(f->getName());
91}
92
93// Returns true if the function is an OpenCL sampled image read of uint type.
94bool IsUintSampledImageRead(llvm::StringRef name);
95inline bool IsUintSampledImageRead(llvm::Function *f) {
96 return IsUintSampledImageRead(f->getName());
97}
98
99// Returns true if the function is an OpenCL sampled image read of int type.
100bool IsIntSampledImageRead(llvm::StringRef name);
101inline bool IsIntSampledImageRead(llvm::Function *f) {
102 return IsIntSampledImageRead(f->getName());
103}
104
alan-baker75090e42020-02-20 11:21:04 -0500105// Returns true if the function is an OpenCL image read.
106bool IsUnsampledImageRead(llvm::StringRef name);
107inline bool IsUnsampledImageRead(llvm::Function *f) {
108 return IsUnsampledImageRead(f->getName());
109}
110
111// Returns true if the function is an OpenCL image read of float type.
112bool IsFloatUnsampledImageRead(llvm::StringRef name);
113inline bool IsFloatUnsampledImageRead(llvm::Function *f) {
114 return IsFloatUnsampledImageRead(f->getName());
115}
116
117// Returns true if the function is an OpenCL image read of uint type.
118bool IsUintUnsampledImageRead(llvm::StringRef name);
119inline bool IsUintUnsampledImageRead(llvm::Function *f) {
120 return IsUintUnsampledImageRead(f->getName());
121}
122
123// Returns true if the function is an OpenCL image read of int type.
124bool IsIntUnsampledImageRead(llvm::StringRef name);
125inline bool IsIntUnsampledImageRead(llvm::Function *f) {
126 return IsIntUnsampledImageRead(f->getName());
127}
128
alan-bakerf67468c2019-11-25 15:51:49 -0500129// Returns true if the function is an OpenCL image write.
130bool IsImageWrite(llvm::StringRef name);
131inline bool IsImageWrite(llvm::Function *f) {
132 return IsImageWrite(f->getName());
133}
134
135// Returns true if the function is an OpenCL image write of float type.
136bool IsFloatImageWrite(llvm::StringRef name);
137inline bool IsFloatImageWrite(llvm::Function *f) {
138 return IsFloatImageWrite(f->getName());
139}
140
141// Returns true if the function is an OpenCL image write of uint type.
142bool IsUintImageWrite(llvm::StringRef name);
143inline bool IsUintImageWrite(llvm::Function *f) {
144 return IsUintImageWrite(f->getName());
145}
146
147// Returns true if the function is an OpenCL image write of int type.
148bool IsIntImageWrite(llvm::StringRef name);
149inline bool IsIntImageWrite(llvm::Function *f) {
150 return IsIntImageWrite(f->getName());
151}
152
153// Returns true if the function is an OpenCL image height query.
154bool IsGetImageHeight(llvm::StringRef name);
155inline bool IsGetImageHeight(llvm::Function *f) {
156 return IsGetImageHeight(f->getName());
157}
158
159// Returns true if the function is an OpenCL image width query.
160bool IsGetImageWidth(llvm::StringRef name);
161inline bool IsGetImageWidth(llvm::Function *f) {
162 return IsGetImageWidth(f->getName());
163}
164
alan-bakerce179f12019-12-06 19:02:22 -0500165// Returns true if the function is an OpenCL image depth query.
166bool IsGetImageDepth(llvm::StringRef name);
167inline bool IsGetImageDepth(llvm::Function *f) {
168 return IsGetImageDepth(f->getName());
169}
170
171// Returns true if the function is an OpenCL image dim query.
172bool IsGetImageDim(llvm::StringRef name);
173inline bool IsGetImageDim(llvm::Function *f) {
174 return IsGetImageDim(f->getName());
175}
176
177// Returns true if the function is an OpenCL image query.
178bool IsImageQuery(llvm::StringRef name);
179inline bool IsImageQuery(llvm::Function *f) {
180 return IsImageQuery(f->getName());
181}
182
SJW173c7e92020-03-16 08:44:47 -0500183} // namespace Builtins
184
alan-bakerf67468c2019-11-25 15:51:49 -0500185} // namespace clspv
186
187#endif // CLSPV_LIB_BUILTINS_H_