blob: d5928f5a0b15a294fcd6540718c4d0ba72e8cafb [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
David Grossd80e58b2017-07-24 11:41:12 -070037// Base class for reflecting control-side forEach
Stephen Hines593a8942011-05-10 15:29:50 -070038class RSExportForEach : public RSExportable {
Chris Wailesc9454af2014-06-13 17:25:40 -070039 public:
40
41 typedef llvm::SmallVectorImpl<const clang::ParmVarDecl*> InVec;
42 typedef llvm::SmallVectorImpl<const RSExportType*> InTypeVec;
43
44 typedef InVec::const_iterator InIter;
45 typedef InTypeVec::const_iterator InTypeIter;
46
Stephen Hines593a8942011-05-10 15:29:50 -070047 private:
48 std::string mName;
David Grossd80e58b2017-07-24 11:41:12 -070049
50 // For diagnostic purposes, we record the order in which we parse
Jeff Sharkey1ebb3422020-07-31 09:26:44 -060051 // foreach kernels. Does not apply to a placeholder root.
David Grossd80e58b2017-07-24 11:41:12 -070052 unsigned mOrdinal;
53
Stephen Hines593a8942011-05-10 15:29:50 -070054 RSExportRecordType *mParamPacketType;
Chris Wailesc9454af2014-06-13 17:25:40 -070055 llvm::SmallVector<const RSExportType*, 16> mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070056 RSExportType *mOutType;
Stephen Hines593a8942011-05-10 15:29:50 -070057 size_t numParams;
58
Stephen Hines7b51b552012-02-16 00:12:38 -080059 unsigned int mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -070060
Chris Wailesc9454af2014-06-13 17:25:40 -070061 llvm::SmallVector<const clang::ParmVarDecl*, 16> mIns;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070062 const clang::ParmVarDecl *mOut;
63 const clang::ParmVarDecl *mUsrData;
David Gross18c50eb2015-01-30 11:39:22 -080064
65 // Accumulator for metadata bits corresponding to special parameters.
66 unsigned int mSpecialParameterSignatureMetadata;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070067
Stephen Hines9ca96e72012-09-13 16:57:06 -070068 clang::QualType mResultType; // return type (if present).
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080069 bool mHasReturnType; // does this kernel have a return type?
70 bool mIsKernelStyle; // is this a pass-by-value kernel?
Stephen Hines9ca96e72012-09-13 16:57:06 -070071
Stephen Hinesc17e1982012-02-22 12:30:45 -080072 bool mDummyRoot;
73
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070074 // TODO(all): Add support for LOD/face when we have them
David Gross63e61082017-07-13 14:17:29 -070075 RSExportForEach(RSContext *Context, const llvm::StringRef &Name, clang::SourceLocation Loc)
76 : RSExportable(Context, RSExportable::EX_FOREACH, Loc),
David Grossd80e58b2017-07-24 11:41:12 -070077 mName(Name.data(), Name.size()), mOrdinal(~unsigned(0)),
78 mParamPacketType(nullptr),
Chris Wailes5abbe0e2014-08-12 15:58:29 -070079 mOutType(nullptr), numParams(0), mSignatureMetadata(0),
David Gross18c50eb2015-01-30 11:39:22 -080080 mOut(nullptr), mUsrData(nullptr), mSpecialParameterSignatureMetadata(0),
Narayan Kamath50cab072014-03-25 15:06:36 +000081 mResultType(clang::QualType()), mHasReturnType(false),
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080082 mIsKernelStyle(false), mDummyRoot(false) {
Stephen Hines593a8942011-05-10 15:29:50 -070083 }
84
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070085 bool validateAndConstructParams(RSContext *Context,
86 const clang::FunctionDecl *FD);
87
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080088 bool validateAndConstructOldStyleParams(RSContext *Context,
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080089 const clang::FunctionDecl *FD);
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080090
Stephen Hines9ca96e72012-09-13 16:57:06 -070091 bool validateAndConstructKernelParams(RSContext *Context,
92 const clang::FunctionDecl *FD);
93
Jean-Luc Brouillet9764eb32015-08-07 16:43:18 -070094 bool processSpecialParameters(RSContext *Context,
95 const clang::FunctionDecl *FD,
96 size_t *IndexOfFirstSpecialParameter);
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080097
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080098 bool setSignatureMetadata(RSContext *Context,
99 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700100 public:
101 static RSExportForEach *Create(RSContext *Context,
102 const clang::FunctionDecl *FD);
103
Stephen Hinesc17e1982012-02-22 12:30:45 -0800104 static RSExportForEach *CreateDummyRoot(RSContext *Context);
105
Stephen Hines593a8942011-05-10 15:29:50 -0700106 inline const std::string &getName() const {
107 return mName;
108 }
109
David Grossd80e58b2017-07-24 11:41:12 -0700110 inline unsigned getOrdinal() const {
111 slangAssert(!mDummyRoot);
112 return mOrdinal;
113 }
114
Stephen Hines593a8942011-05-10 15:29:50 -0700115 inline size_t getNumParameters() const {
116 return numParams;
117 }
118
Chris Wailesc9454af2014-06-13 17:25:40 -0700119 inline bool hasIns() const {
120 return (!mIns.empty());
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700121 }
122
123 inline bool hasOut() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700124 return (mOut != nullptr);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700125 }
126
127 inline bool hasUsrData() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700128 return (mUsrData != nullptr);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700129 }
130
Stephen Hines9ca96e72012-09-13 16:57:06 -0700131 inline bool hasReturn() const {
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -0800132 return mHasReturnType;
Stephen Hines9ca96e72012-09-13 16:57:06 -0700133 }
134
Chris Wailesc9454af2014-06-13 17:25:40 -0700135 inline const InVec& getIns() const {
136 return mIns;
137 }
138
139 inline const InTypeVec& getInTypes() const {
140 return mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700141 }
142
143 inline const RSExportType *getOutType() const {
144 return mOutType;
145 }
146
147 inline const RSExportRecordType *getParamPacketType() const {
148 return mParamPacketType;
149 }
Stephen Hines593a8942011-05-10 15:29:50 -0700150
Stephen Hines7b51b552012-02-16 00:12:38 -0800151 inline unsigned int getSignatureMetadata() const {
152 return mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700153 }
154
Stephen Hinesc17e1982012-02-22 12:30:45 -0800155 inline bool isDummyRoot() const {
156 return mDummyRoot;
157 }
158
David Grossd80e58b2017-07-24 11:41:12 -0700159 // is this a pass-by-value kernel?
160 inline bool isKernelStyle() const {
161 return mIsKernelStyle;
162 }
163
Stephen Hines593a8942011-05-10 15:29:50 -0700164 typedef RSExportRecordType::const_field_iterator const_param_iterator;
165
166 inline const_param_iterator params_begin() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700167 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines593a8942011-05-10 15:29:50 -0700168 "Get parameter from export foreach having no parameter!");
169 return mParamPacketType->fields_begin();
170 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700171
Stephen Hines593a8942011-05-10 15:29:50 -0700172 inline const_param_iterator params_end() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700173 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines593a8942011-05-10 15:29:50 -0700174 "Get parameter from export foreach having no parameter!");
175 return mParamPacketType->fields_end();
176 }
David Grossd80e58b2017-07-24 11:41:12 -0700177 inline size_t params_count() const {
178 return (mParamPacketType ? mParamPacketType->fields_size() : 0);
179 }
Stephen Hines593a8942011-05-10 15:29:50 -0700180
Matt Walaf5b882c2015-06-15 17:45:45 -0700181 static bool isRSForEachFunc(unsigned int targetAPI,
Stephen Hines089cde32012-12-07 19:19:10 -0800182 const clang::FunctionDecl *FD);
Yang Ni19467492015-10-27 15:24:41 -0700183
184 static unsigned getNumInputs(unsigned int targetAPI,
185 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700186}; // RSExportForEach
187
188} // namespace slang
189
190#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ NOLINT