Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 2 | * Copyright 2011-2012, The Android Open Source Project |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 3 | * |
| 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 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 17 | #include "slang_rs_export_foreach.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "clang/AST/ASTContext.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 22 | #include "clang/AST/Attr.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 24 | #include "clang/AST/TypeLoc.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 25 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 26 | #include "llvm/IR/DerivedTypes.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 27 | |
| 28 | #include "slang_assert.h" |
| 29 | #include "slang_rs_context.h" |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 30 | #include "slang_rs_export_type.h" |
Stephen Hines | 12580dc | 2011-10-10 17:44:10 -0700 | [diff] [blame] | 31 | #include "slang_version.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 32 | |
| 33 | namespace slang { |
| 34 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 35 | // This function takes care of additional validation and construction of |
| 36 | // parameters related to forEach_* reflection. |
| 37 | bool RSExportForEach::validateAndConstructParams( |
| 38 | RSContext *Context, const clang::FunctionDecl *FD) { |
| 39 | slangAssert(Context && FD); |
| 40 | bool valid = true; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 41 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 42 | numParams = FD->getNumParams(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 43 | |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 44 | if (Context->getTargetAPI() < SLANG_JB_TARGET_API) { |
Jean-Luc Brouillet | 482caac | 2014-01-18 00:13:26 +0000 | [diff] [blame] | 45 | // Before JellyBean, we allowed only one kernel per file. It must be called "root". |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 46 | if (!isRootRSFunc(FD)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 47 | Context->ReportError(FD->getLocation(), |
| 48 | "Non-root compute kernel %0() is " |
| 49 | "not supported in SDK levels %1-%2") |
| 50 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 51 | << (SLANG_JB_TARGET_API - 1); |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 56 | mResultType = FD->getReturnType().getCanonicalType(); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 57 | // Compute kernel functions are defined differently when the |
| 58 | // "__attribute__((kernel))" is set. |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 59 | if (FD->hasAttr<clang::KernelAttr>()) { |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 60 | valid |= validateAndConstructKernelParams(Context, FD); |
| 61 | } else { |
| 62 | valid |= validateAndConstructOldStyleParams(Context, FD); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 63 | } |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 64 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 65 | valid |= setSignatureMetadata(Context, FD); |
| 66 | return valid; |
| 67 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 68 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 69 | bool RSExportForEach::validateAndConstructOldStyleParams( |
| 70 | RSContext *Context, const clang::FunctionDecl *FD) { |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 71 | slangAssert(Context && FD); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 72 | // If numParams is 0, we already marked this as a graphics root(). |
| 73 | slangAssert(numParams > 0); |
| 74 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 75 | bool valid = true; |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 76 | |
| 77 | // Compute kernel functions of this style are required to return a void type. |
| 78 | clang::ASTContext &C = Context->getASTContext(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 79 | if (mResultType != C.VoidTy) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 80 | Context->ReportError(FD->getLocation(), |
| 81 | "Compute kernel %0() is required to return a " |
| 82 | "void type") |
| 83 | << FD->getName(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 84 | valid = false; |
| 85 | } |
| 86 | |
| 87 | // Validate remaining parameter types |
| 88 | // TODO(all): Add support for LOD/face when we have them |
| 89 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 90 | size_t IndexOfFirstIterator = numParams; |
| 91 | valid |= validateIterationParameters(Context, FD, &IndexOfFirstIterator); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 92 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 93 | // Validate the non-iterator parameters, which should all be found before the |
| 94 | // first iterator. |
| 95 | for (size_t i = 0; i < IndexOfFirstIterator; i++) { |
| 96 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 97 | clang::QualType QT = PVD->getType().getCanonicalType(); |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 98 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 99 | if (!QT->isPointerType()) { |
| 100 | Context->ReportError(PVD->getLocation(), |
| 101 | "Compute kernel %0() cannot have non-pointer " |
| 102 | "parameters besides 'x' and 'y'. Parameter '%1' is " |
| 103 | "of type: '%2'") |
| 104 | << FD->getName() << PVD->getName() << PVD->getType().getAsString(); |
| 105 | valid = false; |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | // The only non-const pointer should be out. |
| 110 | if (!QT->getPointeeType().isConstQualified()) { |
| 111 | if (mOut == NULL) { |
| 112 | mOut = PVD; |
| 113 | } else { |
| 114 | Context->ReportError(PVD->getLocation(), |
| 115 | "Compute kernel %0() can only have one non-const " |
| 116 | "pointer parameter. Parameters '%1' and '%2' are " |
| 117 | "both non-const.") |
| 118 | << FD->getName() << mOut->getName() << PVD->getName(); |
| 119 | valid = false; |
| 120 | } |
| 121 | } else { |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 122 | if (mIns.empty() && mOut == NULL) { |
| 123 | mIns.push_back(PVD); |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 124 | } else if (mUsrData == NULL) { |
| 125 | mUsrData = PVD; |
| 126 | } else { |
| 127 | Context->ReportError( |
| 128 | PVD->getLocation(), |
| 129 | "Unexpected parameter '%0' for compute kernel %1()") |
| 130 | << PVD->getName() << FD->getName(); |
| 131 | valid = false; |
| 132 | } |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 136 | if (mIns.empty() && !mOut) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 137 | Context->ReportError(FD->getLocation(), |
| 138 | "Compute kernel %0() must have at least one " |
| 139 | "parameter for in or out") |
| 140 | << FD->getName(); |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 141 | valid = false; |
| 142 | } |
| 143 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 144 | return valid; |
| 145 | } |
| 146 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 147 | bool RSExportForEach::validateAndConstructKernelParams( |
| 148 | RSContext *Context, const clang::FunctionDecl *FD) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 149 | slangAssert(Context && FD); |
| 150 | bool valid = true; |
| 151 | clang::ASTContext &C = Context->getASTContext(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 152 | |
| 153 | if (Context->getTargetAPI() < SLANG_JB_MR1_TARGET_API) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 154 | Context->ReportError(FD->getLocation(), |
| 155 | "Compute kernel %0() targeting SDK levels " |
| 156 | "%1-%2 may not use pass-by-value with " |
| 157 | "__attribute__((kernel))") |
| 158 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 159 | << (SLANG_JB_MR1_TARGET_API - 1); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Denote that we are indeed a pass-by-value kernel. |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 164 | mIsKernelStyle = true; |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 165 | mHasReturnType = (mResultType != C.VoidTy); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 166 | |
| 167 | if (mResultType->isPointerType()) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 168 | Context->ReportError( |
| 169 | FD->getTypeSpecStartLoc(), |
| 170 | "Compute kernel %0() cannot return a pointer type: '%1'") |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 171 | << FD->getName() << mResultType.getAsString(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 172 | valid = false; |
| 173 | } |
| 174 | |
| 175 | // Validate remaining parameter types |
| 176 | // TODO(all): Add support for LOD/face when we have them |
| 177 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 178 | size_t IndexOfFirstIterator = numParams; |
| 179 | valid |= validateIterationParameters(Context, FD, &IndexOfFirstIterator); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 180 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 181 | // Validate the non-iterator parameters, which should all be found before the |
| 182 | // first iterator. |
| 183 | for (size_t i = 0; i < IndexOfFirstIterator; i++) { |
| 184 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * FIXME: Change this to a test against an actual API version when the |
| 188 | * multi-input feature is officially supported. |
| 189 | */ |
| 190 | if (Context->getTargetAPI() == SLANG_DEVELOPMENT_TARGET_API || i == 0) { |
| 191 | mIns.push_back(PVD); |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 192 | } else { |
| 193 | Context->ReportError(PVD->getLocation(), |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 194 | "Invalid parameter '%0' for compute kernel %1(). " |
| 195 | "Kernels targeting SDK levels %2-%3 may not use " |
| 196 | "multiple input parameters.") << PVD->getName() << |
| 197 | FD->getName() << SLANG_MINIMUM_TARGET_API << |
| 198 | SLANG_MAXIMUM_TARGET_API; |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 199 | valid = false; |
| 200 | } |
| 201 | clang::QualType QT = PVD->getType().getCanonicalType(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 202 | if (QT->isPointerType()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 203 | Context->ReportError(PVD->getLocation(), |
| 204 | "Compute kernel %0() cannot have " |
| 205 | "parameter '%1' of pointer type: '%2'") |
| 206 | << FD->getName() << PVD->getName() << PVD->getType().getAsString(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 207 | valid = false; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 208 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Check that we have at least one allocation to use for dimensions. |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 212 | if (valid && mIns.empty() && !mHasReturnType) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 213 | Context->ReportError(FD->getLocation(), |
| 214 | "Compute kernel %0() must have at least one " |
| 215 | "input parameter or a non-void return " |
| 216 | "type") |
| 217 | << FD->getName(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 218 | valid = false; |
| 219 | } |
| 220 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 221 | return valid; |
| 222 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 223 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 224 | // Search for the optional x and y parameters. Returns true if valid. Also |
| 225 | // sets *IndexOfFirstIterator to the index of the first iterator parameter, or |
| 226 | // FD->getNumParams() if none are found. |
| 227 | bool RSExportForEach::validateIterationParameters( |
| 228 | RSContext *Context, const clang::FunctionDecl *FD, |
| 229 | size_t *IndexOfFirstIterator) { |
| 230 | slangAssert(IndexOfFirstIterator != NULL); |
| 231 | slangAssert(mX == NULL && mY == NULL); |
| 232 | clang::ASTContext &C = Context->getASTContext(); |
| 233 | |
| 234 | // Find the x and y parameters if present. |
| 235 | size_t NumParams = FD->getNumParams(); |
| 236 | *IndexOfFirstIterator = NumParams; |
| 237 | bool valid = true; |
| 238 | for (size_t i = 0; i < NumParams; i++) { |
| 239 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 240 | llvm::StringRef ParamName = PVD->getName(); |
| 241 | if (ParamName.equals("x")) { |
| 242 | slangAssert(mX == NULL); // We won't be invoked if two 'x' are present. |
| 243 | mX = PVD; |
| 244 | if (mY != NULL) { |
| 245 | Context->ReportError(PVD->getLocation(), |
| 246 | "In compute kernel %0(), parameter 'x' should " |
| 247 | "be defined before parameter 'y'") |
| 248 | << FD->getName(); |
| 249 | valid = false; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 250 | } |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 251 | } else if (ParamName.equals("y")) { |
| 252 | slangAssert(mY == NULL); // We won't be invoked if two 'y' are present. |
| 253 | mY = PVD; |
| 254 | } else { |
| 255 | // It's neither x nor y. |
| 256 | if (*IndexOfFirstIterator < NumParams) { |
| 257 | Context->ReportError(PVD->getLocation(), |
| 258 | "In compute kernel %0(), parameter '%1' cannot " |
| 259 | "appear after the 'x' and 'y' parameters") |
| 260 | << FD->getName() << ParamName; |
| 261 | valid = false; |
| 262 | } |
| 263 | continue; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 264 | } |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 265 | // Validate the data type of x and y. |
| 266 | clang::QualType QT = PVD->getType().getCanonicalType(); |
Jean-Luc Brouillet | 482caac | 2014-01-18 00:13:26 +0000 | [diff] [blame] | 267 | clang::QualType UT = QT.getUnqualifiedType(); |
| 268 | if (UT != C.UnsignedIntTy && UT != C.IntTy) { |
| 269 | Context->ReportError(PVD->getLocation(), |
| 270 | "Parameter '%0' must be of type 'int' or " |
| 271 | "'unsigned int'. It is of type '%1'") |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 272 | << ParamName << PVD->getType().getAsString(); |
| 273 | valid = false; |
| 274 | } |
| 275 | // If this is the first time we find an iterator, save it. |
| 276 | if (*IndexOfFirstIterator >= NumParams) { |
| 277 | *IndexOfFirstIterator = i; |
| 278 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 279 | } |
Jean-Luc Brouillet | 482caac | 2014-01-18 00:13:26 +0000 | [diff] [blame] | 280 | // Check that x and y have the same type. |
| 281 | if (mX != NULL and mY != NULL) { |
| 282 | clang::QualType XType = mX->getType(); |
| 283 | clang::QualType YType = mY->getType(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 284 | |
Jean-Luc Brouillet | 482caac | 2014-01-18 00:13:26 +0000 | [diff] [blame] | 285 | if (XType != YType) { |
| 286 | Context->ReportError(mY->getLocation(), |
| 287 | "Parameter 'x' and 'y' must be of the same type. " |
| 288 | "'x' is of type '%0' while 'y' is of type '%1'") |
| 289 | << XType.getAsString() << YType.getAsString(); |
| 290 | valid = false; |
| 291 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 292 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 293 | return valid; |
| 294 | } |
| 295 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 296 | bool RSExportForEach::setSignatureMetadata(RSContext *Context, |
| 297 | const clang::FunctionDecl *FD) { |
| 298 | mSignatureMetadata = 0; |
| 299 | bool valid = true; |
| 300 | |
| 301 | if (mIsKernelStyle) { |
| 302 | slangAssert(mOut == NULL); |
| 303 | slangAssert(mUsrData == NULL); |
| 304 | } else { |
| 305 | slangAssert(!mHasReturnType); |
| 306 | } |
| 307 | |
| 308 | // Set up the bitwise metadata encoding for runtime argument passing. |
| 309 | // TODO: If this bit field is re-used from C++ code, define the values in a header. |
| 310 | const bool HasOut = mOut || mHasReturnType; |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 311 | mSignatureMetadata |= (hasIns() ? 0x01 : 0); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 312 | mSignatureMetadata |= (HasOut ? 0x02 : 0); |
| 313 | mSignatureMetadata |= (mUsrData ? 0x04 : 0); |
| 314 | mSignatureMetadata |= (mX ? 0x08 : 0); |
| 315 | mSignatureMetadata |= (mY ? 0x10 : 0); |
| 316 | mSignatureMetadata |= (mIsKernelStyle ? 0x20 : 0); // pass-by-value |
| 317 | |
| 318 | if (Context->getTargetAPI() < SLANG_ICS_TARGET_API) { |
| 319 | // APIs before ICS cannot skip between parameters. It is ok, however, for |
| 320 | // them to omit further parameters (i.e. skipping X is ok if you skip Y). |
| 321 | if (mSignatureMetadata != 0x1f && // In, Out, UsrData, X, Y |
| 322 | mSignatureMetadata != 0x0f && // In, Out, UsrData, X |
| 323 | mSignatureMetadata != 0x07 && // In, Out, UsrData |
| 324 | mSignatureMetadata != 0x03 && // In, Out |
| 325 | mSignatureMetadata != 0x01) { // In |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 326 | Context->ReportError(FD->getLocation(), |
| 327 | "Compute kernel %0() targeting SDK levels " |
| 328 | "%1-%2 may not skip parameters") |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 329 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 330 | << (SLANG_ICS_TARGET_API - 1); |
| 331 | valid = false; |
| 332 | } |
| 333 | } |
| 334 | return valid; |
| 335 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 336 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 337 | RSExportForEach *RSExportForEach::Create(RSContext *Context, |
| 338 | const clang::FunctionDecl *FD) { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 339 | slangAssert(Context && FD); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 340 | llvm::StringRef Name = FD->getName(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 341 | RSExportForEach *FE; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 342 | |
| 343 | slangAssert(!Name.empty() && "Function must have a name"); |
| 344 | |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 345 | FE = new RSExportForEach(Context, Name); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 346 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 347 | if (!FE->validateAndConstructParams(Context, FD)) { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 348 | return NULL; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | clang::ASTContext &Ctx = Context->getASTContext(); |
| 352 | |
Jean-Luc Brouillet | eca0534 | 2014-05-15 12:44:21 -0700 | [diff] [blame] | 353 | std::string Id = CreateDummyName("helper_foreach_param", FE->getName()); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 354 | |
| 355 | // Extract the usrData parameter (if we have one) |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 356 | if (FE->mUsrData) { |
| 357 | const clang::ParmVarDecl *PVD = FE->mUsrData; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 358 | clang::QualType QT = PVD->getType().getCanonicalType(); |
| 359 | slangAssert(QT->isPointerType() && |
| 360 | QT->getPointeeType().isConstQualified()); |
| 361 | |
| 362 | const clang::ASTContext &C = Context->getASTContext(); |
| 363 | if (QT->getPointeeType().getCanonicalType().getUnqualifiedType() == |
| 364 | C.VoidTy) { |
| 365 | // In the case of using const void*, we can't reflect an appopriate |
| 366 | // Java type, so we fall back to just reflecting the ain/aout parameters |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 367 | FE->mUsrData = NULL; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 368 | } else { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 369 | clang::RecordDecl *RD = |
| 370 | clang::RecordDecl::Create(Ctx, clang::TTK_Struct, |
| 371 | Ctx.getTranslationUnitDecl(), |
| 372 | clang::SourceLocation(), |
| 373 | clang::SourceLocation(), |
| 374 | &Ctx.Idents.get(Id)); |
| 375 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 376 | clang::FieldDecl *FD = |
| 377 | clang::FieldDecl::Create(Ctx, |
| 378 | RD, |
| 379 | clang::SourceLocation(), |
| 380 | clang::SourceLocation(), |
| 381 | PVD->getIdentifier(), |
| 382 | QT->getPointeeType(), |
| 383 | NULL, |
Shih-wei Liao | 1688a3c | 2011-06-22 05:21:27 -0700 | [diff] [blame] | 384 | /* BitWidth = */ NULL, |
| 385 | /* Mutable = */ false, |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 386 | /* HasInit = */ clang::ICIS_NoInit); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 387 | RD->addDecl(FD); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 388 | RD->completeDefinition(); |
| 389 | |
| 390 | // Create an export type iff we have a valid usrData type |
| 391 | clang::QualType T = Ctx.getTagDeclType(RD); |
| 392 | slangAssert(!T.isNull()); |
| 393 | |
| 394 | RSExportType *ET = RSExportType::Create(Context, T.getTypePtr()); |
| 395 | |
| 396 | if (ET == NULL) { |
| 397 | fprintf(stderr, "Failed to export the function %s. There's at least " |
| 398 | "one parameter whose type is not supported by the " |
| 399 | "reflection\n", FE->getName().c_str()); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | slangAssert((ET->getClass() == RSExportType::ExportClassRecord) && |
| 404 | "Parameter packet must be a record"); |
| 405 | |
| 406 | FE->mParamPacketType = static_cast<RSExportRecordType *>(ET); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 409 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 410 | if (FE->hasIns()) { |
| 411 | |
| 412 | for (InIter BI = FE->mIns.begin(), EI = FE->mIns.end(); BI != EI; BI++) { |
| 413 | const clang::Type *T = (*BI)->getType().getCanonicalType().getTypePtr(); |
| 414 | RSExportType *InExportType = RSExportType::Create(Context, T); |
| 415 | |
| 416 | if (FE->mIsKernelStyle) { |
| 417 | slangAssert(InExportType != NULL); |
| 418 | } |
| 419 | |
| 420 | FE->mInTypes.push_back(InExportType); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 421 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 422 | } |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 423 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 424 | if (FE->mIsKernelStyle && FE->mHasReturnType) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 425 | const clang::Type *T = FE->mResultType.getTypePtr(); |
| 426 | FE->mOutType = RSExportType::Create(Context, T); |
| 427 | slangAssert(FE->mOutType); |
| 428 | } else if (FE->mOut) { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 429 | const clang::Type *T = FE->mOut->getType().getCanonicalType().getTypePtr(); |
| 430 | FE->mOutType = RSExportType::Create(Context, T); |
| 431 | } |
| 432 | |
| 433 | return FE; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 436 | RSExportForEach *RSExportForEach::CreateDummyRoot(RSContext *Context) { |
| 437 | slangAssert(Context); |
| 438 | llvm::StringRef Name = "root"; |
| 439 | RSExportForEach *FE = new RSExportForEach(Context, Name); |
| 440 | FE->mDummyRoot = true; |
| 441 | return FE; |
| 442 | } |
| 443 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 444 | bool RSExportForEach::isGraphicsRootRSFunc(unsigned int targetAPI, |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 445 | const clang::FunctionDecl *FD) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 446 | if (FD->hasAttr<clang::KernelAttr>()) { |
| 447 | return false; |
| 448 | } |
| 449 | |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 450 | if (!isRootRSFunc(FD)) { |
| 451 | return false; |
| 452 | } |
| 453 | |
| 454 | if (FD->getNumParams() == 0) { |
| 455 | // Graphics root function |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | // Check for legacy graphics root function (with single parameter). |
| 460 | if ((targetAPI < SLANG_ICS_TARGET_API) && (FD->getNumParams() == 1)) { |
| 461 | const clang::QualType &IntType = FD->getASTContext().IntTy; |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 462 | if (FD->getReturnType().getCanonicalType() == IntType) { |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return false; |
| 468 | } |
| 469 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 470 | bool RSExportForEach::isRSForEachFunc(unsigned int targetAPI, |
| 471 | slang::RSContext* Context, |
| 472 | const clang::FunctionDecl *FD) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 473 | slangAssert(Context && FD); |
Stephen Hines | 089cde3 | 2012-12-07 19:19:10 -0800 | [diff] [blame] | 474 | bool hasKernelAttr = FD->hasAttr<clang::KernelAttr>(); |
| 475 | |
| 476 | if (FD->getStorageClass() == clang::SC_Static) { |
| 477 | if (hasKernelAttr) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 478 | Context->ReportError(FD->getLocation(), |
| 479 | "Invalid use of attribute kernel with " |
| 480 | "static function declaration: %0") |
| 481 | << FD->getName(); |
Stephen Hines | 089cde3 | 2012-12-07 19:19:10 -0800 | [diff] [blame] | 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 486 | // Anything tagged as a kernel is definitely used with ForEach. |
Stephen Hines | 089cde3 | 2012-12-07 19:19:10 -0800 | [diff] [blame] | 487 | if (hasKernelAttr) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 488 | return true; |
| 489 | } |
| 490 | |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 491 | if (isGraphicsRootRSFunc(targetAPI, FD)) { |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 492 | return false; |
| 493 | } |
Stephen Hines | f736d5a | 2011-10-26 16:18:14 -0700 | [diff] [blame] | 494 | |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 495 | // Check if first parameter is a pointer (which is required for ForEach). |
| 496 | unsigned int numParams = FD->getNumParams(); |
| 497 | |
| 498 | if (numParams > 0) { |
| 499 | const clang::ParmVarDecl *PVD = FD->getParamDecl(0); |
| 500 | clang::QualType QT = PVD->getType().getCanonicalType(); |
| 501 | |
| 502 | if (QT->isPointerType()) { |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | // Any non-graphics root() is automatically a ForEach candidate. |
| 507 | // At this point, however, we know that it is not going to be a valid |
| 508 | // compute root() function (due to not having a pointer parameter). We |
| 509 | // still want to return true here, so that we can issue appropriate |
| 510 | // diagnostics. |
| 511 | if (isRootRSFunc(FD)) { |
| 512 | return true; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return false; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 519 | bool |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 520 | RSExportForEach::validateSpecialFuncDecl(unsigned int targetAPI, |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 521 | slang::RSContext *Context, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 522 | clang::FunctionDecl const *FD) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 523 | slangAssert(Context && FD); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 524 | bool valid = true; |
| 525 | const clang::ASTContext &C = FD->getASTContext(); |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 526 | const clang::QualType &IntType = FD->getASTContext().IntTy; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 527 | |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 528 | if (isGraphicsRootRSFunc(targetAPI, FD)) { |
| 529 | if ((targetAPI < SLANG_ICS_TARGET_API) && (FD->getNumParams() == 1)) { |
| 530 | // Legacy graphics root function |
| 531 | const clang::ParmVarDecl *PVD = FD->getParamDecl(0); |
| 532 | clang::QualType QT = PVD->getType().getCanonicalType(); |
| 533 | if (QT != IntType) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 534 | Context->ReportError(PVD->getLocation(), |
| 535 | "invalid parameter type for legacy " |
| 536 | "graphics root() function: %0") |
| 537 | << PVD->getType(); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 538 | valid = false; |
| 539 | } |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Graphics root function, so verify that it returns an int |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 543 | if (FD->getReturnType().getCanonicalType() != IntType) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 544 | Context->ReportError(FD->getLocation(), |
| 545 | "root() is required to return " |
| 546 | "an int for graphics usage"); |
Stephen Hines | 9999ec3 | 2012-02-10 18:22:14 -0800 | [diff] [blame] | 547 | valid = false; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 548 | } |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 549 | } else if (isInitRSFunc(FD) || isDtorRSFunc(FD)) { |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 550 | if (FD->getNumParams() != 0) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 551 | Context->ReportError(FD->getLocation(), |
| 552 | "%0(void) is required to have no " |
| 553 | "parameters") |
| 554 | << FD->getName(); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 555 | valid = false; |
| 556 | } |
| 557 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 558 | if (FD->getReturnType().getCanonicalType() != C.VoidTy) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 559 | Context->ReportError(FD->getLocation(), |
| 560 | "%0(void) is required to have a void " |
| 561 | "return type") |
| 562 | << FD->getName(); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 563 | valid = false; |
| 564 | } |
Stephen Hines | 3cb586a | 2011-05-05 18:20:22 -0700 | [diff] [blame] | 565 | } else { |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 566 | slangAssert(false && "must be called on root, init or .rs.dtor function!"); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return valid; |
| 570 | } |
| 571 | |
| 572 | } // namespace slang |