blob: 581d8a1df571797e82e6d23496da49063a786118 [file] [log] [blame]
Stephen Hines593a8942011-05-10 15:29:50 -07001/*
Stephen Hines9999ec32012-02-10 18:22:14 -08002 * Copyright 2011-2012, The Android Open Source Project
Stephen Hines593a8942011-05-10 15:29:50 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_
19
20#include "llvm/ADT/StringRef.h"
Chris Wailesc9454af2014-06-13 17:25:40 -070021#include "llvm/ADT/SmallVector.h"
Stephen Hines593a8942011-05-10 15:29:50 -070022#include "llvm/Support/raw_ostream.h"
23
24#include "clang/AST/Decl.h"
25
26#include "slang_assert.h"
27#include "slang_rs_context.h"
28#include "slang_rs_exportable.h"
29#include "slang_rs_export_type.h"
30
31namespace clang {
32 class FunctionDecl;
33} // namespace clang
34
35namespace slang {
36
37// Base class for reflecting control-side forEach (currently for root()
38// functions that fit appropriate criteria)
39class RSExportForEach : public RSExportable {
Chris Wailesc9454af2014-06-13 17:25:40 -070040 public:
41
42 typedef llvm::SmallVectorImpl<const clang::ParmVarDecl*> InVec;
43 typedef llvm::SmallVectorImpl<const RSExportType*> InTypeVec;
44
45 typedef InVec::const_iterator InIter;
46 typedef InTypeVec::const_iterator InTypeIter;
47
Stephen Hines593a8942011-05-10 15:29:50 -070048 private:
49 std::string mName;
50 RSExportRecordType *mParamPacketType;
Chris Wailesc9454af2014-06-13 17:25:40 -070051 llvm::SmallVector<const RSExportType*, 16> mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070052 RSExportType *mOutType;
Stephen Hines593a8942011-05-10 15:29:50 -070053 size_t numParams;
54
Stephen Hines7b51b552012-02-16 00:12:38 -080055 unsigned int mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -070056
Chris Wailesc9454af2014-06-13 17:25:40 -070057 llvm::SmallVector<const clang::ParmVarDecl*, 16> mIns;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070058 const clang::ParmVarDecl *mOut;
59 const clang::ParmVarDecl *mUsrData;
David Gross18c50eb2015-01-30 11:39:22 -080060
61 // Accumulator for metadata bits corresponding to special parameters.
62 unsigned int mSpecialParameterSignatureMetadata;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070063
Stephen Hines9ca96e72012-09-13 16:57:06 -070064 clang::QualType mResultType; // return type (if present).
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080065 bool mHasReturnType; // does this kernel have a return type?
66 bool mIsKernelStyle; // is this a pass-by-value kernel?
Stephen Hines9ca96e72012-09-13 16:57:06 -070067
Stephen Hinesc17e1982012-02-22 12:30:45 -080068 bool mDummyRoot;
69
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070070 // TODO(all): Add support for LOD/face when we have them
Stephen Hinesc17e1982012-02-22 12:30:45 -080071 RSExportForEach(RSContext *Context, const llvm::StringRef &Name)
Stephen Hines593a8942011-05-10 15:29:50 -070072 : RSExportable(Context, RSExportable::EX_FOREACH),
Chris Wailes5abbe0e2014-08-12 15:58:29 -070073 mName(Name.data(), Name.size()), mParamPacketType(nullptr),
74 mOutType(nullptr), numParams(0), mSignatureMetadata(0),
David Gross18c50eb2015-01-30 11:39:22 -080075 mOut(nullptr), mUsrData(nullptr), mSpecialParameterSignatureMetadata(0),
Narayan Kamath50cab072014-03-25 15:06:36 +000076 mResultType(clang::QualType()), mHasReturnType(false),
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080077 mIsKernelStyle(false), mDummyRoot(false) {
Stephen Hines593a8942011-05-10 15:29:50 -070078 }
79
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070080 bool validateAndConstructParams(RSContext *Context,
81 const clang::FunctionDecl *FD);
82
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080083 bool validateAndConstructOldStyleParams(RSContext *Context,
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080084 const clang::FunctionDecl *FD);
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080085
Stephen Hines9ca96e72012-09-13 16:57:06 -070086 bool validateAndConstructKernelParams(RSContext *Context,
87 const clang::FunctionDecl *FD);
88
Jean-Luc Brouillet9764eb32015-08-07 16:43:18 -070089 bool processSpecialParameters(RSContext *Context,
90 const clang::FunctionDecl *FD,
91 size_t *IndexOfFirstSpecialParameter);
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080092
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080093 bool setSignatureMetadata(RSContext *Context,
94 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -070095 public:
96 static RSExportForEach *Create(RSContext *Context,
97 const clang::FunctionDecl *FD);
98
Stephen Hinesc17e1982012-02-22 12:30:45 -080099 static RSExportForEach *CreateDummyRoot(RSContext *Context);
100
Stephen Hines593a8942011-05-10 15:29:50 -0700101 inline const std::string &getName() const {
102 return mName;
103 }
104
105 inline size_t getNumParameters() const {
106 return numParams;
107 }
108
Chris Wailesc9454af2014-06-13 17:25:40 -0700109 inline bool hasIns() const {
110 return (!mIns.empty());
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700111 }
112
113 inline bool hasOut() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700114 return (mOut != nullptr);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700115 }
116
117 inline bool hasUsrData() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700118 return (mUsrData != nullptr);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700119 }
120
Stephen Hines9ca96e72012-09-13 16:57:06 -0700121 inline bool hasReturn() const {
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -0800122 return mHasReturnType;
Stephen Hines9ca96e72012-09-13 16:57:06 -0700123 }
124
Chris Wailesc9454af2014-06-13 17:25:40 -0700125 inline const InVec& getIns() const {
126 return mIns;
127 }
128
129 inline const InTypeVec& getInTypes() const {
130 return mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700131 }
132
133 inline const RSExportType *getOutType() const {
134 return mOutType;
135 }
136
137 inline const RSExportRecordType *getParamPacketType() const {
138 return mParamPacketType;
139 }
Stephen Hines593a8942011-05-10 15:29:50 -0700140
Stephen Hines7b51b552012-02-16 00:12:38 -0800141 inline unsigned int getSignatureMetadata() const {
142 return mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700143 }
144
Stephen Hinesc17e1982012-02-22 12:30:45 -0800145 inline bool isDummyRoot() const {
146 return mDummyRoot;
147 }
148
Stephen Hines593a8942011-05-10 15:29:50 -0700149 typedef RSExportRecordType::const_field_iterator const_param_iterator;
150
151 inline const_param_iterator params_begin() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700152 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines593a8942011-05-10 15:29:50 -0700153 "Get parameter from export foreach having no parameter!");
154 return mParamPacketType->fields_begin();
155 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700156
Stephen Hines593a8942011-05-10 15:29:50 -0700157 inline const_param_iterator params_end() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700158 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines593a8942011-05-10 15:29:50 -0700159 "Get parameter from export foreach having no parameter!");
160 return mParamPacketType->fields_end();
161 }
162
163 inline static bool isInitRSFunc(const clang::FunctionDecl *FD) {
164 if (!FD) {
165 return false;
166 }
167 const llvm::StringRef Name = FD->getName();
168 static llvm::StringRef FuncInit("init");
169 return Name.equals(FuncInit);
170 }
171
172 inline static bool isRootRSFunc(const clang::FunctionDecl *FD) {
173 if (!FD) {
174 return false;
175 }
176 const llvm::StringRef Name = FD->getName();
177 static llvm::StringRef FuncRoot("root");
178 return Name.equals(FuncRoot);
179 }
180
Stephen Hines688e64b2011-08-23 16:01:25 -0700181 inline static bool isDtorRSFunc(const clang::FunctionDecl *FD) {
182 if (!FD) {
183 return false;
184 }
185 const llvm::StringRef Name = FD->getName();
186 static llvm::StringRef FuncDtor(".rs.dtor");
187 return Name.equals(FuncDtor);
188 }
189
Chris Wailesc9454af2014-06-13 17:25:40 -0700190 static bool isGraphicsRootRSFunc(unsigned int targetAPI,
Stephen Hines9999ec32012-02-10 18:22:14 -0800191 const clang::FunctionDecl *FD);
192
Chris Wailesc9454af2014-06-13 17:25:40 -0700193 static bool isRSForEachFunc(unsigned int targetAPI, slang::RSContext *Context,
Stephen Hines089cde32012-12-07 19:19:10 -0800194 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700195
Chris Wailesc9454af2014-06-13 17:25:40 -0700196 inline static bool isSpecialRSFunc(unsigned int targetAPI,
Stephen Hines9999ec32012-02-10 18:22:14 -0800197 const clang::FunctionDecl *FD) {
198 return isGraphicsRootRSFunc(targetAPI, FD) || isInitRSFunc(FD) ||
199 isDtorRSFunc(FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700200 }
201
Chris Wailesc9454af2014-06-13 17:25:40 -0700202 static bool validateSpecialFuncDecl(unsigned int targetAPI,
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800203 slang::RSContext *Context,
Stephen Hines593a8942011-05-10 15:29:50 -0700204 const clang::FunctionDecl *FD);
205}; // RSExportForEach
206
207} // namespace slang
208
209#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ NOLINT