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 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 28 | #include "bcinfo/MetadataExtractor.h" |
| 29 | |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 30 | #include "slang_assert.h" |
| 31 | #include "slang_rs_context.h" |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 32 | #include "slang_rs_export_type.h" |
Matt Wala | eae0b7a | 2015-06-16 14:06:25 -0700 | [diff] [blame] | 33 | #include "slang_rs_special_func.h" |
David Gross | 46e146e | 2015-11-11 15:14:01 -0800 | [diff] [blame] | 34 | #include "slang_rs_special_kernel_param.h" |
Stephen Hines | 12580dc | 2011-10-10 17:44:10 -0700 | [diff] [blame] | 35 | #include "slang_version.h" |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 36 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 37 | namespace { |
| 38 | |
David Gross | cd7d312 | 2015-03-13 15:11:44 -0700 | [diff] [blame] | 39 | const size_t RS_KERNEL_INPUT_LIMIT = 8; // see frameworks/base/libs/rs/cpu_ref/rsCpuCoreRuntime.h |
| 40 | |
Matt Wala | eae0b7a | 2015-06-16 14:06:25 -0700 | [diff] [blame] | 41 | bool isRootRSFunc(const clang::FunctionDecl *FD) { |
| 42 | if (!FD) { |
| 43 | return false; |
| 44 | } |
| 45 | return FD->getName().equals("root"); |
David Gross | 7cc6876 | 2015-03-11 14:29:24 -0700 | [diff] [blame] | 46 | } |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 47 | |
Matt Wala | eae0b7a | 2015-06-16 14:06:25 -0700 | [diff] [blame] | 48 | } // end anonymous namespace |
| 49 | |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 50 | namespace slang { |
| 51 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 52 | // This function takes care of additional validation and construction of |
| 53 | // parameters related to forEach_* reflection. |
| 54 | bool RSExportForEach::validateAndConstructParams( |
| 55 | RSContext *Context, const clang::FunctionDecl *FD) { |
| 56 | slangAssert(Context && FD); |
| 57 | bool valid = true; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 58 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 59 | numParams = FD->getNumParams(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 60 | |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 61 | if (Context->getTargetAPI() < SLANG_JB_TARGET_API) { |
Jean-Luc Brouillet | 482caac | 2014-01-18 00:13:26 +0000 | [diff] [blame] | 62 | // 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] | 63 | if (!isRootRSFunc(FD)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 64 | Context->ReportError(FD->getLocation(), |
| 65 | "Non-root compute kernel %0() is " |
| 66 | "not supported in SDK levels %1-%2") |
| 67 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 68 | << (SLANG_JB_TARGET_API - 1); |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 73 | mResultType = FD->getReturnType().getCanonicalType(); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 74 | // Compute kernel functions are defined differently when the |
| 75 | // "__attribute__((kernel))" is set. |
Pirama Arumuga Nainar | b6a1435 | 2016-07-26 11:39:47 -0700 | [diff] [blame] | 76 | if (FD->hasAttr<clang::RenderScriptKernelAttr>()) { |
Stephen Hines | f075ffc | 2015-12-03 03:07:55 -0800 | [diff] [blame] | 77 | valid &= validateAndConstructKernelParams(Context, FD); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 78 | } else { |
Stephen Hines | f075ffc | 2015-12-03 03:07:55 -0800 | [diff] [blame] | 79 | valid &= validateAndConstructOldStyleParams(Context, FD); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 80 | } |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 81 | |
Stephen Hines | f075ffc | 2015-12-03 03:07:55 -0800 | [diff] [blame] | 82 | valid &= setSignatureMetadata(Context, FD); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 83 | return valid; |
| 84 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 85 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 86 | bool RSExportForEach::validateAndConstructOldStyleParams( |
| 87 | RSContext *Context, const clang::FunctionDecl *FD) { |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 88 | slangAssert(Context && FD); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 89 | // If numParams is 0, we already marked this as a graphics root(). |
| 90 | slangAssert(numParams > 0); |
| 91 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 92 | bool valid = true; |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 93 | |
| 94 | // Compute kernel functions of this style are required to return a void type. |
| 95 | clang::ASTContext &C = Context->getASTContext(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 96 | if (mResultType != C.VoidTy) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 97 | Context->ReportError(FD->getLocation(), |
| 98 | "Compute kernel %0() is required to return a " |
| 99 | "void type") |
| 100 | << FD->getName(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 101 | valid = false; |
| 102 | } |
| 103 | |
| 104 | // Validate remaining parameter types |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 105 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 106 | size_t IndexOfFirstSpecialParameter = numParams; |
Stephen Hines | f075ffc | 2015-12-03 03:07:55 -0800 | [diff] [blame] | 107 | valid &= processSpecialParameters(Context, FD, &IndexOfFirstSpecialParameter); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 108 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 109 | // Validate the non-special parameters, which should all be found before the |
| 110 | // first special parameter. |
| 111 | for (size_t i = 0; i < IndexOfFirstSpecialParameter; i++) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 112 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
| 113 | clang::QualType QT = PVD->getType().getCanonicalType(); |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 114 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 115 | if (!QT->isPointerType()) { |
| 116 | Context->ReportError(PVD->getLocation(), |
| 117 | "Compute kernel %0() cannot have non-pointer " |
Jean-Luc Brouillet | 9764eb3 | 2015-08-07 16:43:18 -0700 | [diff] [blame] | 118 | "parameters besides special parameters (%1). Parameter '%2' is " |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 119 | "of type: '%3'") |
David Gross | 46e146e | 2015-11-11 15:14:01 -0800 | [diff] [blame] | 120 | << FD->getName() << listSpecialKernelParameters(Context->getTargetAPI()) |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 121 | << PVD->getName() << PVD->getType().getAsString(); |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 122 | valid = false; |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | // The only non-const pointer should be out. |
| 127 | if (!QT->getPointeeType().isConstQualified()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 128 | if (mOut == nullptr) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 129 | mOut = PVD; |
| 130 | } else { |
| 131 | Context->ReportError(PVD->getLocation(), |
| 132 | "Compute kernel %0() can only have one non-const " |
| 133 | "pointer parameter. Parameters '%1' and '%2' are " |
| 134 | "both non-const.") |
| 135 | << FD->getName() << mOut->getName() << PVD->getName(); |
| 136 | valid = false; |
| 137 | } |
| 138 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 139 | if (mIns.empty() && mOut == nullptr) { |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 140 | mIns.push_back(PVD); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 141 | } else if (mUsrData == nullptr) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 142 | mUsrData = PVD; |
| 143 | } else { |
| 144 | Context->ReportError( |
| 145 | PVD->getLocation(), |
| 146 | "Unexpected parameter '%0' for compute kernel %1()") |
| 147 | << PVD->getName() << FD->getName(); |
| 148 | valid = false; |
| 149 | } |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 153 | if (mIns.empty() && !mOut) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 154 | Context->ReportError(FD->getLocation(), |
| 155 | "Compute kernel %0() must have at least one " |
| 156 | "parameter for in or out") |
| 157 | << FD->getName(); |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 158 | valid = false; |
| 159 | } |
| 160 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 161 | return valid; |
| 162 | } |
| 163 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 164 | bool RSExportForEach::validateAndConstructKernelParams( |
| 165 | RSContext *Context, const clang::FunctionDecl *FD) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 166 | slangAssert(Context && FD); |
| 167 | bool valid = true; |
| 168 | clang::ASTContext &C = Context->getASTContext(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 169 | |
| 170 | if (Context->getTargetAPI() < SLANG_JB_MR1_TARGET_API) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 171 | Context->ReportError(FD->getLocation(), |
| 172 | "Compute kernel %0() targeting SDK levels " |
| 173 | "%1-%2 may not use pass-by-value with " |
| 174 | "__attribute__((kernel))") |
| 175 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 176 | << (SLANG_JB_MR1_TARGET_API - 1); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // Denote that we are indeed a pass-by-value kernel. |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 181 | mIsKernelStyle = true; |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 182 | mHasReturnType = (mResultType != C.VoidTy); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 183 | |
| 184 | if (mResultType->isPointerType()) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 185 | Context->ReportError( |
| 186 | FD->getTypeSpecStartLoc(), |
| 187 | "Compute kernel %0() cannot return a pointer type: '%1'") |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 188 | << FD->getName() << mResultType.getAsString(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 189 | valid = false; |
| 190 | } |
| 191 | |
| 192 | // Validate remaining parameter types |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 193 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 194 | size_t IndexOfFirstSpecialParameter = numParams; |
Stephen Hines | f075ffc | 2015-12-03 03:07:55 -0800 | [diff] [blame] | 195 | valid &= processSpecialParameters(Context, FD, &IndexOfFirstSpecialParameter); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 196 | |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 197 | // Validate the non-special parameters, which should all be found before the |
| 198 | // first special. |
| 199 | for (size_t i = 0; i < IndexOfFirstSpecialParameter; i++) { |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 200 | const clang::ParmVarDecl *PVD = FD->getParamDecl(i); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 201 | |
Stephen Hines | bd0a7dd | 2015-07-01 19:24:00 -0700 | [diff] [blame] | 202 | if (Context->getTargetAPI() >= SLANG_M_TARGET_API || i == 0) { |
David Gross | cd7d312 | 2015-03-13 15:11:44 -0700 | [diff] [blame] | 203 | if (i >= RS_KERNEL_INPUT_LIMIT) { |
| 204 | Context->ReportError(PVD->getLocation(), |
| 205 | "Invalid parameter '%0' for compute kernel %1(). " |
Stephen Hines | bd0a7dd | 2015-07-01 19:24:00 -0700 | [diff] [blame] | 206 | "Kernels targeting SDK levels %2+ may not use " |
| 207 | "more than %3 input parameters.") << PVD->getName() << |
| 208 | FD->getName() << SLANG_M_TARGET_API << |
| 209 | int(RS_KERNEL_INPUT_LIMIT); |
David Gross | cd7d312 | 2015-03-13 15:11:44 -0700 | [diff] [blame] | 210 | |
| 211 | } else { |
| 212 | mIns.push_back(PVD); |
| 213 | } |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 214 | } else { |
| 215 | Context->ReportError(PVD->getLocation(), |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 216 | "Invalid parameter '%0' for compute kernel %1(). " |
| 217 | "Kernels targeting SDK levels %2-%3 may not use " |
| 218 | "multiple input parameters.") << PVD->getName() << |
| 219 | FD->getName() << SLANG_MINIMUM_TARGET_API << |
Stephen Hines | bd0a7dd | 2015-07-01 19:24:00 -0700 | [diff] [blame] | 220 | (SLANG_M_TARGET_API - 1); |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 221 | valid = false; |
| 222 | } |
| 223 | clang::QualType QT = PVD->getType().getCanonicalType(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 224 | if (QT->isPointerType()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 225 | Context->ReportError(PVD->getLocation(), |
| 226 | "Compute kernel %0() cannot have " |
| 227 | "parameter '%1' of pointer type: '%2'") |
| 228 | << FD->getName() << PVD->getName() << PVD->getType().getAsString(); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 229 | valid = false; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 230 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Check that we have at least one allocation to use for dimensions. |
Stephen Hines | bd0a7dd | 2015-07-01 19:24:00 -0700 | [diff] [blame] | 234 | if (valid && mIns.empty() && !mHasReturnType && Context->getTargetAPI() < SLANG_M_TARGET_API) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 235 | Context->ReportError(FD->getLocation(), |
David Gross | fb78d4c | 2015-03-31 13:56:05 -0700 | [diff] [blame] | 236 | "Compute kernel %0() targeting SDK levels " |
| 237 | "%1-%2 must have at least one " |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 238 | "input parameter or a non-void return " |
| 239 | "type") |
David Gross | fb78d4c | 2015-03-31 13:56:05 -0700 | [diff] [blame] | 240 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
Stephen Hines | bd0a7dd | 2015-07-01 19:24:00 -0700 | [diff] [blame] | 241 | << (SLANG_M_TARGET_API - 1); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 242 | valid = false; |
| 243 | } |
| 244 | |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 245 | return valid; |
| 246 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 247 | |
Jean-Luc Brouillet | 9764eb3 | 2015-08-07 16:43:18 -0700 | [diff] [blame] | 248 | // Process the optional special parameters: |
| 249 | // - Sets *IndexOfFirstSpecialParameter to the index of the first special parameter, or |
| 250 | // FD->getNumParams() if none are found. |
David Gross | 46e146e | 2015-11-11 15:14:01 -0800 | [diff] [blame] | 251 | // - Add bits to mSpecialParameterSignatureMetadata for the found special parameters. |
Jean-Luc Brouillet | 9764eb3 | 2015-08-07 16:43:18 -0700 | [diff] [blame] | 252 | // Returns true if no errors. |
| 253 | bool RSExportForEach::processSpecialParameters( |
Jean-Luc Brouillet | 42f81b2 | 2014-01-16 21:50:52 -0800 | [diff] [blame] | 254 | RSContext *Context, const clang::FunctionDecl *FD, |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 255 | size_t *IndexOfFirstSpecialParameter) { |
David Gross | 46e146e | 2015-11-11 15:14:01 -0800 | [diff] [blame] | 256 | auto DiagnosticCallback = [FD] { |
| 257 | std::ostringstream DiagnosticDescription; |
| 258 | DiagnosticDescription << "compute kernel " << FD->getName().str() << "()"; |
| 259 | return DiagnosticDescription.str(); |
| 260 | }; |
| 261 | return slang::processSpecialKernelParameters(Context, |
| 262 | DiagnosticCallback, |
| 263 | FD, |
| 264 | IndexOfFirstSpecialParameter, |
| 265 | &mSpecialParameterSignatureMetadata); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 268 | bool RSExportForEach::setSignatureMetadata(RSContext *Context, |
| 269 | const clang::FunctionDecl *FD) { |
| 270 | mSignatureMetadata = 0; |
| 271 | bool valid = true; |
| 272 | |
| 273 | if (mIsKernelStyle) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 274 | slangAssert(mOut == nullptr); |
| 275 | slangAssert(mUsrData == nullptr); |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 276 | } else { |
| 277 | slangAssert(!mHasReturnType); |
| 278 | } |
| 279 | |
| 280 | // Set up the bitwise metadata encoding for runtime argument passing. |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 281 | const bool HasOut = mOut || mHasReturnType; |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 282 | mSignatureMetadata |= (hasIns() ? bcinfo::MD_SIG_In : 0); |
| 283 | mSignatureMetadata |= (HasOut ? bcinfo::MD_SIG_Out : 0); |
| 284 | mSignatureMetadata |= (mUsrData ? bcinfo::MD_SIG_Usr : 0); |
| 285 | mSignatureMetadata |= (mIsKernelStyle ? bcinfo::MD_SIG_Kernel : 0); // pass-by-value |
| 286 | mSignatureMetadata |= mSpecialParameterSignatureMetadata; |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 287 | |
| 288 | if (Context->getTargetAPI() < SLANG_ICS_TARGET_API) { |
| 289 | // APIs before ICS cannot skip between parameters. It is ok, however, for |
| 290 | // them to omit further parameters (i.e. skipping X is ok if you skip Y). |
David Gross | 18c50eb | 2015-01-30 11:39:22 -0800 | [diff] [blame] | 291 | if (mSignatureMetadata != (bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out | bcinfo::MD_SIG_Usr | |
| 292 | bcinfo::MD_SIG_X | bcinfo::MD_SIG_Y) && |
| 293 | mSignatureMetadata != (bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out | bcinfo::MD_SIG_Usr | |
| 294 | bcinfo::MD_SIG_X) && |
| 295 | mSignatureMetadata != (bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out | bcinfo::MD_SIG_Usr) && |
| 296 | mSignatureMetadata != (bcinfo::MD_SIG_In | bcinfo::MD_SIG_Out) && |
| 297 | mSignatureMetadata != (bcinfo::MD_SIG_In)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 298 | Context->ReportError(FD->getLocation(), |
| 299 | "Compute kernel %0() targeting SDK levels " |
| 300 | "%1-%2 may not skip parameters") |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 301 | << FD->getName() << SLANG_MINIMUM_TARGET_API |
| 302 | << (SLANG_ICS_TARGET_API - 1); |
| 303 | valid = false; |
| 304 | } |
| 305 | } |
| 306 | return valid; |
| 307 | } |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 308 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 309 | RSExportForEach *RSExportForEach::Create(RSContext *Context, |
| 310 | const clang::FunctionDecl *FD) { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 311 | slangAssert(Context && FD); |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 312 | llvm::StringRef Name = FD->getName(); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 313 | RSExportForEach *FE; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 314 | |
| 315 | slangAssert(!Name.empty() && "Function must have a name"); |
| 316 | |
David Gross | 63e6108 | 2017-07-13 14:17:29 -0700 | [diff] [blame] | 317 | FE = new RSExportForEach(Context, Name, FD->getLocation()); |
David Gross | d80e58b | 2017-07-24 11:41:12 -0700 | [diff] [blame] | 318 | FE->mOrdinal = Context->getNextForEachOrdinal(); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 319 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 320 | if (!FE->validateAndConstructParams(Context, FD)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 321 | return nullptr; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | clang::ASTContext &Ctx = Context->getASTContext(); |
| 325 | |
Jean-Luc Brouillet | eca0534 | 2014-05-15 12:44:21 -0700 | [diff] [blame] | 326 | std::string Id = CreateDummyName("helper_foreach_param", FE->getName()); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 327 | |
David Gross | 50ff263 | 2017-07-06 14:12:46 -0700 | [diff] [blame] | 328 | // Construct type information about usrData, inputs, and |
| 329 | // outputs. Return null when there is an error exporting types. |
| 330 | |
| 331 | bool TypeExportError = false; |
| 332 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 333 | // Extract the usrData parameter (if we have one) |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 334 | if (FE->mUsrData) { |
| 335 | const clang::ParmVarDecl *PVD = FE->mUsrData; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 336 | clang::QualType QT = PVD->getType().getCanonicalType(); |
| 337 | slangAssert(QT->isPointerType() && |
| 338 | QT->getPointeeType().isConstQualified()); |
| 339 | |
| 340 | const clang::ASTContext &C = Context->getASTContext(); |
| 341 | if (QT->getPointeeType().getCanonicalType().getUnqualifiedType() == |
| 342 | C.VoidTy) { |
| 343 | // In the case of using const void*, we can't reflect an appopriate |
| 344 | // Java type, so we fall back to just reflecting the ain/aout parameters |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 345 | FE->mUsrData = nullptr; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 346 | } else { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 347 | clang::RecordDecl *RD = |
| 348 | clang::RecordDecl::Create(Ctx, clang::TTK_Struct, |
| 349 | Ctx.getTranslationUnitDecl(), |
| 350 | clang::SourceLocation(), |
| 351 | clang::SourceLocation(), |
| 352 | &Ctx.Idents.get(Id)); |
| 353 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 354 | clang::FieldDecl *FD = |
| 355 | clang::FieldDecl::Create(Ctx, |
| 356 | RD, |
| 357 | clang::SourceLocation(), |
| 358 | clang::SourceLocation(), |
| 359 | PVD->getIdentifier(), |
| 360 | QT->getPointeeType(), |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 361 | nullptr, |
| 362 | /* BitWidth = */ nullptr, |
Shih-wei Liao | 1688a3c | 2011-06-22 05:21:27 -0700 | [diff] [blame] | 363 | /* Mutable = */ false, |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 364 | /* HasInit = */ clang::ICIS_NoInit); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 365 | RD->addDecl(FD); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 366 | RD->completeDefinition(); |
| 367 | |
| 368 | // Create an export type iff we have a valid usrData type |
| 369 | clang::QualType T = Ctx.getTagDeclType(RD); |
| 370 | slangAssert(!T.isNull()); |
| 371 | |
Stephen Hines | 13fad85 | 2015-11-23 19:32:14 -0800 | [diff] [blame] | 372 | RSExportType *ET = |
| 373 | RSExportType::Create(Context, T.getTypePtr(), LegacyKernelArgument); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 374 | |
David Gross | 50ff263 | 2017-07-06 14:12:46 -0700 | [diff] [blame] | 375 | if (ET) { |
| 376 | slangAssert((ET->getClass() == RSExportType::ExportClassRecord) && |
| 377 | "Parameter packet must be a record"); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 378 | |
David Gross | 50ff263 | 2017-07-06 14:12:46 -0700 | [diff] [blame] | 379 | FE->mParamPacketType = static_cast<RSExportRecordType *>(ET); |
| 380 | } else { |
| 381 | TypeExportError = true; |
| 382 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 385 | |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 386 | if (FE->hasIns()) { |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 387 | for (InIter BI = FE->mIns.begin(), EI = FE->mIns.end(); BI != EI; BI++) { |
| 388 | const clang::Type *T = (*BI)->getType().getCanonicalType().getTypePtr(); |
Stephen Hines | 13fad85 | 2015-11-23 19:32:14 -0800 | [diff] [blame] | 389 | ExportKind EK = (FE->mIsKernelStyle ? NotLegacyKernelArgument : |
| 390 | LegacyKernelArgument); |
| 391 | RSExportType *InExportType = RSExportType::Create(Context, T, EK); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 392 | |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 393 | // It is not an error if we don't export an input type for legacy |
Stephen Hines | 13fad85 | 2015-11-23 19:32:14 -0800 | [diff] [blame] | 394 | // kernel arguments. This can happen in the case of a void pointer. |
David Gross | d80e58b | 2017-07-24 11:41:12 -0700 | [diff] [blame] | 395 | // See ReflectionState::addForEachIn(). |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 396 | if (FE->mIsKernelStyle && !InExportType) { |
| 397 | TypeExportError = true; |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | FE->mInTypes.push_back(InExportType); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 401 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 402 | } |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 403 | |
Jean-Luc Brouillet | 0f2a239 | 2014-01-14 11:40:57 -0800 | [diff] [blame] | 404 | if (FE->mIsKernelStyle && FE->mHasReturnType) { |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 405 | const clang::Type *ReturnType = FE->mResultType.getTypePtr(); |
Stephen Hines | 13fad85 | 2015-11-23 19:32:14 -0800 | [diff] [blame] | 406 | FE->mOutType = RSExportType::Create(Context, ReturnType, |
| 407 | NotLegacyKernelArgument); |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 408 | TypeExportError |= !FE->mOutType; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 409 | } else if (FE->mOut) { |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 410 | const clang::Type *OutType = |
| 411 | FE->mOut->getType().getCanonicalType().getTypePtr(); |
Stephen Hines | 13fad85 | 2015-11-23 19:32:14 -0800 | [diff] [blame] | 412 | FE->mOutType = RSExportType::Create(Context, OutType, LegacyKernelArgument); |
Stephen Hines | a088faa | 2015-06-22 19:46:56 -0700 | [diff] [blame] | 413 | // It is not an error if we don't export an output type. |
| 414 | // This can happen in the case of a void pointer. |
| 415 | } |
| 416 | |
| 417 | if (TypeExportError) { |
| 418 | slangAssert(Context->getDiagnostics()->hasErrorOccurred() && |
| 419 | "Error exporting type but no diagnostic message issued!"); |
| 420 | return nullptr; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return FE; |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 426 | RSExportForEach *RSExportForEach::CreateDummyRoot(RSContext *Context) { |
| 427 | slangAssert(Context); |
| 428 | llvm::StringRef Name = "root"; |
David Gross | 63e6108 | 2017-07-13 14:17:29 -0700 | [diff] [blame] | 429 | RSExportForEach *FE = new RSExportForEach(Context, Name, clang::SourceLocation()); |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 430 | FE->mDummyRoot = true; |
| 431 | return FE; |
| 432 | } |
| 433 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 434 | bool RSExportForEach::isRSForEachFunc(unsigned int targetAPI, |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 435 | const clang::FunctionDecl *FD) { |
Yang Ni | fb40ee2 | 2015-10-13 20:34:06 +0000 | [diff] [blame] | 436 | if (!FD) { |
| 437 | return false; |
| 438 | } |
Stephen Hines | 089cde3 | 2012-12-07 19:19:10 -0800 | [diff] [blame] | 439 | |
Matt Wala | c0c5dd8 | 2015-07-23 17:29:37 -0700 | [diff] [blame] | 440 | // Anything tagged as a kernel("") is definitely used with ForEach. |
Pirama Arumuga Nainar | b6a1435 | 2016-07-26 11:39:47 -0700 | [diff] [blame] | 441 | if (FD->hasAttr<clang::RenderScriptKernelAttr>()) { |
| 442 | return true; |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Matt Wala | eae0b7a | 2015-06-16 14:06:25 -0700 | [diff] [blame] | 445 | if (RSSpecialFunc::isGraphicsRootRSFunc(targetAPI, FD)) { |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
Stephen Hines | f736d5a | 2011-10-26 16:18:14 -0700 | [diff] [blame] | 448 | |
Stephen Hines | 7b51b55 | 2012-02-16 00:12:38 -0800 | [diff] [blame] | 449 | // Check if first parameter is a pointer (which is required for ForEach). |
| 450 | unsigned int numParams = FD->getNumParams(); |
| 451 | |
| 452 | if (numParams > 0) { |
| 453 | const clang::ParmVarDecl *PVD = FD->getParamDecl(0); |
| 454 | clang::QualType QT = PVD->getType().getCanonicalType(); |
| 455 | |
| 456 | if (QT->isPointerType()) { |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | // Any non-graphics root() is automatically a ForEach candidate. |
| 461 | // At this point, however, we know that it is not going to be a valid |
| 462 | // compute root() function (due to not having a pointer parameter). We |
| 463 | // still want to return true here, so that we can issue appropriate |
| 464 | // diagnostics. |
| 465 | if (isRootRSFunc(FD)) { |
| 466 | return true; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return false; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Yang Ni | 1946749 | 2015-10-27 15:24:41 -0700 | [diff] [blame] | 473 | unsigned RSExportForEach::getNumInputs(unsigned int targetAPI, |
| 474 | const clang::FunctionDecl *FD) { |
| 475 | unsigned numInputs = 0; |
Pirama Arumuga Nainar | b6a1435 | 2016-07-26 11:39:47 -0700 | [diff] [blame] | 476 | for (const clang::ParmVarDecl* param : FD->parameters()) { |
David Gross | 46e146e | 2015-11-11 15:14:01 -0800 | [diff] [blame] | 477 | if (!isSpecialKernelParameter(param->getName())) { |
Yang Ni | 1946749 | 2015-10-27 15:24:41 -0700 | [diff] [blame] | 478 | numInputs++; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return numInputs; |
| 483 | } |
| 484 | |
Stephen Hines | 5baf632 | 2011-04-25 17:21:15 -0700 | [diff] [blame] | 485 | } // namespace slang |