blob: d251dde6d4cda97fde3a35d34b58f8b7c36df8d4 [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
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
37class 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
45public:
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
58private:
59 bool GetFromMangledNameCheck(const std::string &mangled_name);
60};
61
62/// Primary Interface
63// returns a FunctionInfo representation of the mangled name
64const FunctionInfo &Lookup(const std::string &mangled_name);
65inline const FunctionInfo &Lookup(llvm::StringRef mangled_name) {
66 return Lookup(mangled_name.str());
67}
68inline const FunctionInfo &Lookup(llvm::Function *func) {
69 return Lookup(func->getName());
70}
71
72/// Legacy
alan-bakerf67468c2019-11-25 15:51:49 -050073// Returns true if the function is an OpenCL image builtin.
74bool IsImageBuiltin(llvm::StringRef name);
75inline bool IsImageBuiltin(llvm::Function *f) {
76 return IsImageBuiltin(f->getName());
77}
78
79// Returns true if the function is an OpenCL sampled image read.
80bool IsSampledImageRead(llvm::StringRef name);
81inline 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.
86bool IsFloatSampledImageRead(llvm::StringRef name);
87inline 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.
92bool IsUintSampledImageRead(llvm::StringRef name);
93inline 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.
98bool IsIntSampledImageRead(llvm::StringRef name);
99inline bool IsIntSampledImageRead(llvm::Function *f) {
100 return IsIntSampledImageRead(f->getName());
101}
102
alan-baker75090e42020-02-20 11:21:04 -0500103// Returns true if the function is an OpenCL image read.
104bool IsUnsampledImageRead(llvm::StringRef name);
105inline 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.
110bool IsFloatUnsampledImageRead(llvm::StringRef name);
111inline 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.
116bool IsUintUnsampledImageRead(llvm::StringRef name);
117inline 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.
122bool IsIntUnsampledImageRead(llvm::StringRef name);
123inline bool IsIntUnsampledImageRead(llvm::Function *f) {
124 return IsIntUnsampledImageRead(f->getName());
125}
126
alan-bakerf67468c2019-11-25 15:51:49 -0500127// Returns true if the function is an OpenCL image write.
128bool IsImageWrite(llvm::StringRef name);
129inline 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.
134bool IsFloatImageWrite(llvm::StringRef name);
135inline 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.
140bool IsUintImageWrite(llvm::StringRef name);
141inline 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.
146bool IsIntImageWrite(llvm::StringRef name);
147inline bool IsIntImageWrite(llvm::Function *f) {
148 return IsIntImageWrite(f->getName());
149}
150
151// Returns true if the function is an OpenCL image height query.
152bool IsGetImageHeight(llvm::StringRef name);
153inline bool IsGetImageHeight(llvm::Function *f) {
154 return IsGetImageHeight(f->getName());
155}
156
157// Returns true if the function is an OpenCL image width query.
158bool IsGetImageWidth(llvm::StringRef name);
159inline bool IsGetImageWidth(llvm::Function *f) {
160 return IsGetImageWidth(f->getName());
161}
162
alan-bakerce179f12019-12-06 19:02:22 -0500163// Returns true if the function is an OpenCL image depth query.
164bool IsGetImageDepth(llvm::StringRef name);
165inline bool IsGetImageDepth(llvm::Function *f) {
166 return IsGetImageDepth(f->getName());
167}
168
169// Returns true if the function is an OpenCL image dim query.
170bool IsGetImageDim(llvm::StringRef name);
171inline bool IsGetImageDim(llvm::Function *f) {
172 return IsGetImageDim(f->getName());
173}
174
175// Returns true if the function is an OpenCL image query.
176bool IsImageQuery(llvm::StringRef name);
177inline bool IsImageQuery(llvm::Function *f) {
178 return IsImageQuery(f->getName());
179}
180
SJW173c7e92020-03-16 08:44:47 -0500181} // namespace Builtins
182
alan-bakerf67468c2019-11-25 15:51:49 -0500183} // namespace clspv
184
185#endif // CLSPV_LIB_BUILTINS_H_