RSKernelExpand.cpp revision c754d49ee856be620e041348a9f2b3d5610a5a26
1db169187dea4602e4ad32058762d23d474753fd0Stephen Hines/*
2db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * Copyright 2012, The Android Open Source Project
3db169187dea4602e4ad32058762d23d474753fd0Stephen Hines *
4db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * Licensed under the Apache License, Version 2.0 (the "License");
5db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * you may not use this file except in compliance with the License.
6db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * You may obtain a copy of the License at
7db169187dea4602e4ad32058762d23d474753fd0Stephen Hines *
8db169187dea4602e4ad32058762d23d474753fd0Stephen Hines *     http://www.apache.org/licenses/LICENSE-2.0
9db169187dea4602e4ad32058762d23d474753fd0Stephen Hines *
10db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * Unless required by applicable law or agreed to in writing, software
11db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * distributed under the License is distributed on an "AS IS" BASIS,
12db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * See the License for the specific language governing permissions and
14db169187dea4602e4ad32058762d23d474753fd0Stephen Hines * limitations under the License.
15db169187dea4602e4ad32058762d23d474753fd0Stephen Hines */
16db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
176e9e89d1ed049ec931bb1000948c12698e6c5484Stephen Hines#include "bcc/Assert.h"
18e198abec6c5e3eab380ccf6897b0a0b9c2dd92ddStephen Hines#include "bcc/Renderscript/RSTransforms.h"
197a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao
207a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao#include <cstdlib>
217a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao
22b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/DerivedTypes.h>
23b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/Function.h>
24b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/Instructions.h>
25b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/IRBuilder.h>
2618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser#include <llvm/IR/MDBuilder.h>
27b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/Module.h>
28c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include <llvm/Pass.h>
297ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines#include <llvm/Support/raw_ostream.h>
30b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/DataLayout.h>
31cd5b657c39a348bd7652a19c5be5035e9df44a42Tobias Grosser#include <llvm/IR/Function.h>
32b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include <llvm/IR/Type.h>
33806075b3a54af826fea78490fb213d8a0784138eTobias Grosser#include <llvm/Transforms/Utils/BasicBlockUtils.h>
34c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang
35c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Config/Config.h"
36ef73a242762bcd8113b9b65ceccbe7d909b5acbcZonr Chang#include "bcc/Support/Log.h"
37db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
38d88177580db4ddedf680854c51db333c97eabc59Stephen Hines#include "bcinfo/MetadataExtractor.h"
39d88177580db4ddedf680854c51db333c97eabc59Stephen Hines
405010f641d1df6bc3447646ca7ef837410fb9b3dcChris Wailes#define NUM_EXPANDED_FUNCTION_PARAMS 4
41bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
427a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liaousing namespace bcc;
437a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao
44db169187dea4602e4ad32058762d23d474753fd0Stephen Hinesnamespace {
457a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao
4614588cf0babf4596f1bcf4ea05ddd2ceb458a916Logan Chienstatic const bool gEnableRsTbaa = true;
479c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines
487a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao/* RSForEachExpandPass - This pass operates on functions that are able to be
497a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * called via rsForEach() or "foreach_<NAME>". We create an inner loop for the
507a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * ForEach-able function to be invoked over the appropriate data cells of the
517a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * input/output allocations (adjusting other relevant parameters as we go). We
527a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * support doing this for any ForEach-able compute kernels. The new function
537a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * name is the original function name followed by ".expand". Note that we
547a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao * still generate code for the original function.
557a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao */
567a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liaoclass RSForEachExpandPass : public llvm::ModulePass {
577a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liaoprivate:
58db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  static char ID;
59db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
60bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  llvm::Module *Module;
61bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  llvm::LLVMContext *Context;
62bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
63bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  /*
64bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes   * Pointer to LLVM type information for the ForEachStubType and the function
65bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes   * signature for expanded kernels.  These must be re-calculated for each
66bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes   * module the pass is run on.
67bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes   */
68bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  llvm::StructType   *ForEachStubType;
69bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  llvm::FunctionType *ExpandedFunctionType;
70db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
7125eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines  uint32_t mExportForEachCount;
7225eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines  const char **mExportForEachNameList;
7325eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines  const uint32_t *mExportForEachSignatureList;
74cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines
752b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  // Turns on optimization of allocation stride values.
762b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  bool mEnableStepOpt;
772b04086acbef6520ae2c54a868b1271abf053122Stephen Hines
78bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  uint32_t getRootSignature(llvm::Function *Function) {
79db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    const llvm::NamedMDNode *ExportForEachMetadata =
80bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes        Module->getNamedMetadata("#rs_export_foreach");
81db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
82db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    if (!ExportForEachMetadata) {
83db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      llvm::SmallVector<llvm::Type*, 8> RootArgTys;
84bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      for (llvm::Function::arg_iterator B = Function->arg_begin(),
85bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes                                        E = Function->arg_end();
86db169187dea4602e4ad32058762d23d474753fd0Stephen Hines           B != E;
87db169187dea4602e4ad32058762d23d474753fd0Stephen Hines           ++B) {
88db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        RootArgTys.push_back(B->getType());
89db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      }
90db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
91db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      // For pre-ICS bitcode, we may not have signature information. In that
92db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      // case, we use the size of the RootArgTys to select the number of
93db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      // arguments.
94db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      return (1 << RootArgTys.size()) - 1;
95db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
96db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
977ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    if (ExportForEachMetadata->getNumOperands() == 0) {
987ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines      return 0;
997ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    }
1007ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
1016e9e89d1ed049ec931bb1000948c12698e6c5484Stephen Hines    bccAssert(ExportForEachMetadata->getNumOperands() > 0);
102db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
103cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines    // We only handle the case for legacy root() functions here, so this is
104cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines    // hard-coded to look at only the first such function.
105db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    llvm::MDNode *SigNode = ExportForEachMetadata->getOperand(0);
106900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    if (SigNode != nullptr && SigNode->getNumOperands() == 1) {
107db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      llvm::Value *SigVal = SigNode->getOperand(0);
108db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      if (SigVal->getValueID() == llvm::Value::MDStringVal) {
109db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        llvm::StringRef SigString =
110db169187dea4602e4ad32058762d23d474753fd0Stephen Hines            static_cast<llvm::MDString*>(SigVal)->getString();
111db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        uint32_t Signature = 0;
112db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        if (SigString.getAsInteger(10, Signature)) {
113db169187dea4602e4ad32058762d23d474753fd0Stephen Hines          ALOGE("Non-integer signature value '%s'", SigString.str().c_str());
114db169187dea4602e4ad32058762d23d474753fd0Stephen Hines          return 0;
115db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        }
116db169187dea4602e4ad32058762d23d474753fd0Stephen Hines        return Signature;
117db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      }
118db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
119db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
120db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    return 0;
121db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  }
122db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
123429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray  bool isStepOptSupported(llvm::Type *AllocType) {
124429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
125429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(AllocType);
126429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*Context);
127429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
128429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (mEnableStepOpt) {
129429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return false;
130429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
131429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
132429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (AllocType == VoidPtrTy) {
133429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return false;
134429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
135429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
136429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (!PT) {
137429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return false;
138429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
139429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
140429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    // remaining conditions are 64-bit only
141429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (VoidPtrTy->getPrimitiveSizeInBits() == 32) {
142429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return true;
143429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
144429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
145429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    // coerce suggests an upconverted struct type, which we can't support
146429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (AllocType->getStructName().find("coerce") != llvm::StringRef::npos) {
147429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return false;
148429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
149429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
150429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    // 2xi64 and i128 suggest an upconverted struct type, which are also unsupported
151429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    llvm::Type *V2xi64Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(*Context), 2);
152429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    llvm::Type *Int128Ty = llvm::Type::getIntNTy(*Context, 128);
153429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (AllocType == V2xi64Ty || AllocType == Int128Ty) {
154429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray      return false;
155429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    }
156429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
157429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    return true;
158429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray  }
159429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray
1602b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  // Get the actual value we should use to step through an allocation.
1617b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  //
1627b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  // Normally the value we use to step through an allocation is given to us by
1637b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  // the driver. However, for certain primitive data types, we can derive an
1647b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  // integer constant for the step value. We use this integer constant whenever
1657b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  // possible to allow further compiler optimizations to take place.
1667b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser  //
167b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines  // DL - Target Data size/layout information.
1682b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  // T - Type of allocation (should be a pointer).
1692b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  // OrigStep - Original step increment (root.expand() input from driver).
170bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  llvm::Value *getStepValue(llvm::DataLayout *DL, llvm::Type *AllocType,
1712b04086acbef6520ae2c54a868b1271abf053122Stephen Hines                            llvm::Value *OrigStep) {
172b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines    bccAssert(DL);
173bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bccAssert(AllocType);
1742b04086acbef6520ae2c54a868b1271abf053122Stephen Hines    bccAssert(OrigStep);
175bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(AllocType);
176429d94acbc64480d4f75233b66b4824cdc8b19c0Tim Murray    if (isStepOptSupported(AllocType)) {
1772b04086acbef6520ae2c54a868b1271abf053122Stephen Hines      llvm::Type *ET = PT->getElementType();
178b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      uint64_t ETSize = DL->getTypeAllocSize(ET);
179bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*Context);
1802b04086acbef6520ae2c54a868b1271abf053122Stephen Hines      return llvm::ConstantInt::get(Int32Ty, ETSize);
1812b04086acbef6520ae2c54a868b1271abf053122Stephen Hines    } else {
1822b04086acbef6520ae2c54a868b1271abf053122Stephen Hines      return OrigStep;
1832b04086acbef6520ae2c54a868b1271abf053122Stephen Hines    }
1842b04086acbef6520ae2c54a868b1271abf053122Stephen Hines  }
1852b04086acbef6520ae2c54a868b1271abf053122Stephen Hines
186e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_INS         0
187e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_INESTRIDES  1
188e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_OUT         2
189e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_Y           3
190e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_Z           4
191e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_LID         5
192e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_USR         6
193e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_DIMX        7
194e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_DIMY        8
195e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_DIMZ        9
196e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes#define PARAM_FIELD_SLOT       10
197097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes
198097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes  /// Builds the types required by the pass for the given context.
199bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  void buildTypes(void) {
200bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    // Create the RsForEachStubParam struct.
201bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
202097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    llvm::Type *VoidPtrTy    = llvm::Type::getInt8PtrTy(*Context);
203097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    llvm::Type *VoidPtrPtrTy = VoidPtrTy->getPointerTo();
204097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    llvm::Type *Int32Ty      = llvm::Type::getInt32Ty(*Context);
205097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    llvm::Type *Int32PtrTy   = Int32Ty->getPointerTo();
206097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes
207097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    /* Defined in frameworks/base/libs/rs/cpu_ref/rsCpuCore.h:
208db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     *
209097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     * struct RsForEachKernelStruct{
210db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     *   const void *in;
211db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     *   void *out;
212db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     *   uint32_t y;
213db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     *   uint32_t z;
214097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   uint32_t lid;
215881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes     *   const void **ins;
216e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes     *   uint32_t *inEStrides;
217097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   const void *usr;
218097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   uint32_t dimX;
219097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   uint32_t dimY;
220097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   uint32_t dimZ;
221097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes     *   uint32_t slot;
222db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     * };
223db169187dea4602e4ad32058762d23d474753fd0Stephen Hines     */
224097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    llvm::SmallVector<llvm::Type*, 12> StructTypes;
225e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    StructTypes.push_back(VoidPtrPtrTy); // const void **ins
226e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    StructTypes.push_back(Int32PtrTy);   // uint32_t *inEStrides
227097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(VoidPtrTy);    // void *out
228097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t y
229097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t z
230097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t lid
231097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(VoidPtrTy);    // const void *usr
232097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t dimX
233097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t dimY
234097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t dimZ
235097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes    StructTypes.push_back(Int32Ty);      // uint32_t slot
236881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
237bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ForEachStubType =
238bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::StructType::create(StructTypes, "RsForEachStubParamStruct");
239bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
240bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    // Create the function type for expanded kernels.
241bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
242bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Type *ForEachStubPtrTy = ForEachStubType->getPointerTo();
243bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
244bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::SmallVector<llvm::Type*, 8> ParamTypes;
245bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ParamTypes.push_back(ForEachStubPtrTy); // const RsForEachStubParamStruct *p
246bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ParamTypes.push_back(Int32Ty);          // uint32_t x1
247bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ParamTypes.push_back(Int32Ty);          // uint32_t x2
248bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ParamTypes.push_back(Int32Ty);          // uint32_t outstep
249bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
250e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    ExpandedFunctionType =
251e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes        llvm::FunctionType::get(llvm::Type::getVoidTy(*Context), ParamTypes,
252e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                false);
2538ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser  }
2548ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser
255357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  /// @brief Create skeleton of the expanded function.
256357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  ///
257357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  /// This creates a function with the following signature:
258357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  ///
259357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  ///   void (const RsForEachStubParamStruct *p, uint32_t x1, uint32_t x2,
2605010f641d1df6bc3447646ca7ef837410fb9b3dcChris Wailes  ///         uint32_t outstep)
261357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  ///
262357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  llvm::Function *createEmptyExpandedFunction(llvm::StringRef OldName) {
263bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function *ExpandedFunction =
264bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Function::Create(ExpandedFunctionType,
265bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes                             llvm::GlobalValue::ExternalLinkage,
266bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes                             OldName + ".expand", Module);
267bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
268bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bccAssert(ExpandedFunction->arg_size() == NUM_EXPANDED_FUNCTION_PARAMS);
269bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
270bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function::arg_iterator AI = ExpandedFunction->arg_begin();
271bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
272bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    (AI++)->setName("p");
273bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    (AI++)->setName("x1");
274bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    (AI++)->setName("x2");
275bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    (AI++)->setName("arg_outstep");
276bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
277bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::BasicBlock *Begin = llvm::BasicBlock::Create(*Context, "Begin",
278bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes                                                       ExpandedFunction);
279806075b3a54af826fea78490fb213d8a0784138eTobias Grosser    llvm::IRBuilder<> Builder(Begin);
280806075b3a54af826fea78490fb213d8a0784138eTobias Grosser    Builder.CreateRetVoid();
281806075b3a54af826fea78490fb213d8a0784138eTobias Grosser
282bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    return ExpandedFunction;
283357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser  }
284357b58691936bef425bd315c13a2d8019d7e9c7eTobias Grosser
285e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @brief Create an empty loop
286e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///
287e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// Create a loop of the form:
288e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///
289e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// for (i = LowerBound; i < UpperBound; i++)
290e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///   ;
291e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///
292e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// After the loop has been created, the builder is set such that
293e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// instructions can be added to the loop body.
294e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///
295e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @param Builder The builder to use to build this loop. The current
296e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///                position of the builder is the position the loop
297e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  ///                will be inserted.
298e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @param LowerBound The first value of the loop iterator
299e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @param UpperBound The maximal value of the loop iterator
300e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @param LoopIV A reference that will be set to the loop iterator.
301e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  /// @return The BasicBlock that will be executed after the loop.
302e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  llvm::BasicBlock *createLoop(llvm::IRBuilder<> &Builder,
303e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser                               llvm::Value *LowerBound,
304e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser                               llvm::Value *UpperBound,
305e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser                               llvm::PHINode **LoopIV) {
306e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    assert(LowerBound->getType() == UpperBound->getType());
307e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser
308e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::BasicBlock *CondBB, *AfterBB, *HeaderBB;
309e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::Value *Cond, *IVNext;
310e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::PHINode *IV;
311e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser
312e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    CondBB = Builder.GetInsertBlock();
313e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    AfterBB = llvm::SplitBlock(CondBB, Builder.GetInsertPoint(), this);
314bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    HeaderBB = llvm::BasicBlock::Create(*Context, "Loop", CondBB->getParent());
315e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser
316e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // if (LowerBound < Upperbound)
317e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    //   goto LoopHeader
318e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // else
319e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    //   goto AfterBB
320e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    CondBB->getTerminator()->eraseFromParent();
321e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    Builder.SetInsertPoint(CondBB);
322e87a0518647d1f9c5249d6990c67737e0fb579e9Tobias Grosser    Cond = Builder.CreateICmpULT(LowerBound, UpperBound);
323e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    Builder.CreateCondBr(Cond, HeaderBB, AfterBB);
324e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser
325e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // iv = PHI [CondBB -> LowerBound], [LoopHeader -> NextIV ]
326e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // iv.next = iv + 1
327e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // if (iv.next < Upperbound)
328e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    //   goto LoopHeader
329e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    // else
330e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    //   goto AfterBB
331e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    Builder.SetInsertPoint(HeaderBB);
332e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    IV = Builder.CreatePHI(LowerBound->getType(), 2, "X");
333e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    IV->addIncoming(LowerBound, CondBB);
334e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    IVNext = Builder.CreateNUWAdd(IV, Builder.getInt32(1));
335e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    IV->addIncoming(IVNext, HeaderBB);
336e87a0518647d1f9c5249d6990c67737e0fb579e9Tobias Grosser    Cond = Builder.CreateICmpULT(IVNext, UpperBound);
337e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    Builder.CreateCondBr(Cond, HeaderBB, AfterBB);
338e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    AfterBB->setName("Exit");
339e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
340e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    *LoopIV = IV;
341e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    return AfterBB;
342e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser  }
343e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser
3448ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosserpublic:
34525eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines  RSForEachExpandPass(bool pEnableStepOpt)
346900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes      : ModulePass(ID), Module(nullptr), Context(nullptr),
347bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes        mEnableStepOpt(pEnableStepOpt) {
348bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
3498ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser  }
3508ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser
351c754d49ee856be620e041348a9f2b3d5610a5a26Stephen Hines  virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
352c754d49ee856be620e041348a9f2b3d5610a5a26Stephen Hines    // This pass does not use any other analysis passes, but it does
353c754d49ee856be620e041348a9f2b3d5610a5a26Stephen Hines    // add/wrap the existing functions in the module (thus altering the CFG).
354c754d49ee856be620e041348a9f2b3d5610a5a26Stephen Hines  }
355c754d49ee856be620e041348a9f2b3d5610a5a26Stephen Hines
3568ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser  /* Performs the actual optimization on a selected function. On success, the
3578ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser   * Module will contain a new function of the name "<NAME>.expand" that
3588ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser   * invokes <NAME>() in a loop with the appropriate parameters.
3598ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser   */
360bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  bool ExpandFunction(llvm::Function *Function, uint32_t Signature) {
361bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ALOGV("Expanding ForEach-able Function %s",
362bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes          Function->getName().str().c_str());
3638ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser
3648ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser    if (!Signature) {
365bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      Signature = getRootSignature(Function);
3668ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser      if (!Signature) {
3678ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser        // We couldn't determine how to expand this function based on its
3688ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser        // function signature.
3698ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser        return false;
3708ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser      }
3718ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser    }
3728ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser
373bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::DataLayout DL(Module);
3748ae4607d2dc5bc655d0c9225565d36ce2ebfc798Tobias Grosser
375bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function *ExpandedFunction =
376bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      createEmptyExpandedFunction(Function->getName());
377db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
378bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bccAssert(ExpandedFunction->arg_size() == NUM_EXPANDED_FUNCTION_PARAMS);
379db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
380bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    /*
381bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     * Extract the expanded function's parameters.  It is guaranteed by
382bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     * createEmptyExpandedFunction that there will be five parameters.
383bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     */
384bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function::arg_iterator ExpandedFunctionArgIter =
385bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      ExpandedFunction->arg_begin();
386db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
387bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_p       = &*(ExpandedFunctionArgIter++);
388bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_x1      = &*(ExpandedFunctionArgIter++);
389bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_x2      = &*(ExpandedFunctionArgIter++);
3905010f641d1df6bc3447646ca7ef837410fb9b3dcChris Wailes    llvm::Value *Arg_outstep = &*(ExpandedFunctionArgIter);
391bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
392900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *InStep  = nullptr;
393900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *OutStep = nullptr;
394db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
395db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    // Construct the actual function body.
396bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::IRBuilder<> Builder(ExpandedFunction->getEntryBlock().begin());
397db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
398cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines    // Collect and construct the arguments for the kernel().
399db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    // Note that we load any loop-invariant arguments before entering the Loop.
400bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function::arg_iterator FunctionArgIter = Function->arg_begin();
401db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
402900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Type  *InTy      = nullptr;
403900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *InBasePtr = nullptr;
404d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureIn(Signature)) {
405e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value    *InsMember  = Builder.CreateStructGEP(Arg_p,
406e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                           PARAM_FIELD_INS);
407e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::LoadInst *InsBasePtr = Builder.CreateLoad(InsMember, "inputs_base");
408e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
409e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value *InStepsMember =
410e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes        Builder.CreateStructGEP(Arg_p, PARAM_FIELD_INESTRIDES);
411e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::LoadInst *InStepsBase = Builder.CreateLoad(InStepsMember,
412e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                       "insteps_base");
413e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
414e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value *IndexVal = Builder.getInt32(0);
415e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
416e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value    *InStepAddr = Builder.CreateGEP(InStepsBase, IndexVal);
417e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::LoadInst *InStepArg  = Builder.CreateLoad(InStepAddr,
418e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                      "instep_addr");
419e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
420bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      InTy = (FunctionArgIter++)->getType();
421e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      InStep = getStepValue(&DL, InTy, InStepArg);
422e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
4232b04086acbef6520ae2c54a868b1271abf053122Stephen Hines      InStep->setName("instep");
424e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes
425e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value *InputAddr = Builder.CreateGEP(InsBasePtr, IndexVal);
426e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      InBasePtr = Builder.CreateLoad(InputAddr, "input_base");
427db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
428db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
429900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Type *OutTy = nullptr;
430900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *OutBasePtr = nullptr;
431d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureOut(Signature)) {
432bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      OutTy = (FunctionArgIter++)->getType();
433b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
4342b04086acbef6520ae2c54a868b1271abf053122Stephen Hines      OutStep->setName("outstep");
435097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes      OutBasePtr = Builder.CreateLoad(
436097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes                     Builder.CreateStructGEP(Arg_p, PARAM_FIELD_OUT));
437db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
438db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
439900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *UsrData = nullptr;
440d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureUsrData(Signature)) {
441bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Type *UsrDataTy = (FunctionArgIter++)->getType();
442db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      UsrData = Builder.CreatePointerCast(Builder.CreateLoad(
443097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes          Builder.CreateStructGEP(Arg_p, PARAM_FIELD_USR)), UsrDataTy);
444db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      UsrData->setName("UsrData");
445db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
446db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
447d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureX(Signature)) {
448bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      FunctionArgIter++;
449db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
450db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
451900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *Y = nullptr;
452d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureY(Signature)) {
453097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes      Y = Builder.CreateLoad(
454097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes            Builder.CreateStructGEP(Arg_p, PARAM_FIELD_Y), "Y");
455097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes
456bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      FunctionArgIter++;
457db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
458db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
459bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bccAssert(FunctionArgIter == Function->arg_end());
460db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
461e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::PHINode *IV;
462e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    createLoop(Builder, Arg_x1, Arg_x2, &IV);
463db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
464cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines    // Populate the actual call to kernel().
465db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    llvm::SmallVector<llvm::Value*, 8> RootArgs;
466db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
467900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *InPtr  = nullptr;
468900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *OutPtr = nullptr;
469db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
470ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser    // Calculate the current input and output pointers
47102f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    //
472ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser    // We always calculate the input/output pointers with a GEP operating on i8
47302f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    // values and only cast at the very end to OutTy. This is because the step
47402f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    // between two values is given in bytes.
47502f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    //
47602f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    // TODO: We could further optimize the output by using a GEP operation of
47702f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    // type 'OutTy' in cases where the element type of the allocation allows.
47802f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    if (OutBasePtr) {
47902f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser      llvm::Value *OutOffset = Builder.CreateSub(IV, Arg_x1);
48002f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser      OutOffset = Builder.CreateMul(OutOffset, OutStep);
48102f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser      OutPtr = Builder.CreateGEP(OutBasePtr, OutOffset);
48202f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser      OutPtr = Builder.CreatePointerCast(OutPtr, OutTy);
48302f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    }
484bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
485ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser    if (InBasePtr) {
486ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser      llvm::Value *InOffset = Builder.CreateSub(IV, Arg_x1);
487ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser      InOffset = Builder.CreateMul(InOffset, InStep);
488ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser      InPtr = Builder.CreateGEP(InBasePtr, InOffset);
489ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser      InPtr = Builder.CreatePointerCast(InPtr, InTy);
490ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser    }
49102f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser
492ae937ec9658c219a38d9d631d2569f19e21b50c0Tobias Grosser    if (InPtr) {
4937ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines      RootArgs.push_back(InPtr);
494db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
495db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
49602f3cd69a4103f91a51a2f988f2179c885d734b5Tobias Grosser    if (OutPtr) {
4977ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines      RootArgs.push_back(OutPtr);
498db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
499db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
500db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    if (UsrData) {
501db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      RootArgs.push_back(UsrData);
502db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
503db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
504e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::Value *X = IV;
505d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureX(Signature)) {
506db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      RootArgs.push_back(X);
507db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
508db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
509db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    if (Y) {
510db169187dea4602e4ad32058762d23d474753fd0Stephen Hines      RootArgs.push_back(Y);
511db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
512db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
513bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    Builder.CreateCall(Function, RootArgs);
514db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
5157ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    return true;
5167ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines  }
5177ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
5187ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines  /* Expand a pass-by-value kernel.
5197ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines   */
520bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  bool ExpandKernel(llvm::Function *Function, uint32_t Signature) {
521d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    bccAssert(bcinfo::MetadataExtractor::hasForEachSignatureKernel(Signature));
522bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    ALOGV("Expanding kernel Function %s", Function->getName().str().c_str());
5237ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
5247ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    // TODO: Refactor this to share functionality with ExpandFunction.
525bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::DataLayout DL(Module);
5267ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
527bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function *ExpandedFunction =
528bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      createEmptyExpandedFunction(Function->getName());
5297ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
530bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    /*
531bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     * Extract the expanded function's parameters.  It is guaranteed by
532bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     * createEmptyExpandedFunction that there will be five parameters.
533bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes     */
534881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
535881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    bccAssert(ExpandedFunction->arg_size() == NUM_EXPANDED_FUNCTION_PARAMS);
536881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
537bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Function::arg_iterator ExpandedFunctionArgIter =
538bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      ExpandedFunction->arg_begin();
539bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
540bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_p       = &*(ExpandedFunctionArgIter++);
541bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_x1      = &*(ExpandedFunctionArgIter++);
542bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *Arg_x2      = &*(ExpandedFunctionArgIter++);
5435010f641d1df6bc3447646ca7ef837410fb9b3dcChris Wailes    llvm::Value *Arg_outstep = &*(ExpandedFunctionArgIter);
5447ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
5457ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    // Construct the actual function body.
546bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::IRBuilder<> Builder(ExpandedFunction->getEntryBlock().begin());
5477ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
54818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // Create TBAA meta-data.
54918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    llvm::MDNode *TBAARenderScript, *TBAAAllocation, *TBAAPointer;
550bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::MDBuilder MDHelper(*Context);
55114588cf0babf4596f1bcf4ea05ddd2ceb458a916Logan Chien
55218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    TBAARenderScript = MDHelper.createTBAARoot("RenderScript TBAA");
553e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    TBAAAllocation = MDHelper.createTBAAScalarTypeNode("allocation",
554e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                       TBAARenderScript);
555e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    TBAAAllocation = MDHelper.createTBAAStructTagNode(TBAAAllocation,
556e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                      TBAAAllocation, 0);
557e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    TBAAPointer = MDHelper.createTBAAScalarTypeNode("pointer",
558e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes                                                    TBAARenderScript);
55914588cf0babf4596f1bcf4ea05ddd2ceb458a916Logan Chien    TBAAPointer = MDHelper.createTBAAStructTagNode(TBAAPointer, TBAAPointer, 0);
56018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
56150f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray    llvm::MDNode *AliasingDomain, *AliasingScope;
56250f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray    AliasingDomain = MDHelper.createAnonymousAliasScopeDomain("RS argument scope domain");
56350f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray    AliasingScope = MDHelper.createAnonymousAliasScope(AliasingDomain, "RS argument scope");
56450f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray
565881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    /*
566881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes     * Collect and construct the arguments for the kernel().
567881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes     *
568881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes     * Note that we load any loop-invariant arguments before entering the Loop.
569881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes     */
570881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    size_t NumInputs = Function->arg_size();
5717ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
572900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *Y = nullptr;
573881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    if (bcinfo::MetadataExtractor::hasForEachSignatureY(Signature)) {
574097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes      Y = Builder.CreateLoad(
575097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes            Builder.CreateStructGEP(Arg_p, PARAM_FIELD_Y), "Y");
576097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes
577881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      --NumInputs;
578881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    }
579881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
580881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    if (bcinfo::MetadataExtractor::hasForEachSignatureX(Signature)) {
581881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      --NumInputs;
582881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    }
583881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
584881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // No usrData parameter on kernels.
585881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    bccAssert(
586881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        !bcinfo::MetadataExtractor::hasForEachSignatureUsrData(Signature));
587881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
588881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    llvm::Function::arg_iterator ArgIter = Function->arg_begin();
589881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
590881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // Check the return type
591bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::Type     *OutTy            = nullptr;
592bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::Value    *OutStep          = nullptr;
593bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::LoadInst *OutBasePtr       = nullptr;
594bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::Value    *CastedOutBasePtr = nullptr;
595881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
596e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    bool PassOutByPointer = false;
597881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
598d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureOut(Signature)) {
599bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Type *OutBaseTy = Function->getReturnType();
600881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
60174a4b08235990916911b8fe758d656c1171faf26Stephen Hines      if (OutBaseTy->isVoidTy()) {
602e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes        PassOutByPointer = true;
603881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        OutTy = ArgIter->getType();
604881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
605881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        ArgIter++;
606881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        --NumInputs;
60774a4b08235990916911b8fe758d656c1171faf26Stephen Hines      } else {
60874a4b08235990916911b8fe758d656c1171faf26Stephen Hines        // We don't increment Args, since we are using the actual return type.
609881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        OutTy = OutBaseTy->getPointerTo();
61074a4b08235990916911b8fe758d656c1171faf26Stephen Hines      }
611881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
612b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
61374a4b08235990916911b8fe758d656c1171faf26Stephen Hines      OutStep->setName("outstep");
614097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes      OutBasePtr = Builder.CreateLoad(
615097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes                     Builder.CreateStructGEP(Arg_p, PARAM_FIELD_OUT));
616097ca14d8b6908d0e5b7f8f38011cad4fb94bc26Chris Wailes
6179c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines      if (gEnableRsTbaa) {
6189c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines        OutBasePtr->setMetadata("tbaa", TBAAPointer);
6199c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines      }
62050f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray
62150f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray      OutBasePtr->setMetadata("alias.scope", AliasingScope);
62250f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray
623bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray      CastedOutBasePtr = Builder.CreatePointerCast(OutBasePtr, OutTy, "casted_out");
62474a4b08235990916911b8fe758d656c1171faf26Stephen Hines    }
62574a4b08235990916911b8fe758d656c1171faf26Stephen Hines
626bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::SmallVector<llvm::Type*,  8> InTypes;
627bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::SmallVector<llvm::Value*, 8> InSteps;
628bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::SmallVector<llvm::Value*, 8> InBasePtrs;
629bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    llvm::SmallVector<bool,         8> InIsStructPointer;
630881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
631e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    if (NumInputs > 0) {
632e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value *InsMember = Builder.CreateStructGEP(Arg_p, PARAM_FIELD_INS);
633e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::LoadInst *InsBasePtr = Builder.CreateLoad(InsMember, "inputs_base");
6347ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
635e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::Value *InStepsMember =
636e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes        Builder.CreateStructGEP(Arg_p, PARAM_FIELD_INESTRIDES);
637e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      llvm::LoadInst *InStepsBase = Builder.CreateLoad(InStepsMember,
638881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes                                                         "insteps_base");
639881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
640881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      for (size_t InputIndex = 0; InputIndex < NumInputs;
641881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes           ++InputIndex, ArgIter++) {
642881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
643881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::Value *IndexVal = Builder.getInt32(InputIndex);
644881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
645881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::Value    *InStepAddr = Builder.CreateGEP(InStepsBase, IndexVal);
646881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::LoadInst *InStepArg  = Builder.CreateLoad(InStepAddr,
647881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes                                                          "instep_addr");
648881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
649326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          llvm::Type *InType = ArgIter->getType();
650326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes
651326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes        /*
652326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * AArch64 calling dictate that structs of sufficient size get passed by
653326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * pointer instead of passed by value.  This, combined with the fact
654326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * that we don't allow kernels to operate on pointer data means that if
655326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * we see a kernel with a pointer parameter we know that it is struct
656326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * input that has been promoted.  As such we don't need to convert its
657326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * type to a pointer.  Later we will need to know to avoid a load, so we
658326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         * save this information in InIsStructPointer.
659326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes         */
660326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          if (!InType->isPointerTy()) {
661326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes            InType = InType->getPointerTo();
662326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes            InIsStructPointer.push_back(false);
663326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          } else {
664326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes            InIsStructPointer.push_back(true);
665326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          }
666326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes
667881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::Value *InStep = getStepValue(&DL, InType, InStepArg);
668881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
669881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          InStep->setName("instep");
670881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
671881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::Value    *InputAddr = Builder.CreateGEP(InsBasePtr, IndexVal);
672881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          llvm::LoadInst *InBasePtr = Builder.CreateLoad(InputAddr,
673881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes                                                         "input_base");
674bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray          llvm::Value    *CastInBasePtr = Builder.CreatePointerCast(InBasePtr,
675bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray                                                                    InType, "casted_in");
676881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          if (gEnableRsTbaa) {
677881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes            InBasePtr->setMetadata("tbaa", TBAAPointer);
678881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          }
679881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
68050f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray          InBasePtr->setMetadata("alias.scope", AliasingScope);
68150f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray
682881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          InTypes.push_back(InType);
683881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes          InSteps.push_back(InStep);
684bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray          InBasePtrs.push_back(CastInBasePtr);
685881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      }
686881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    }
6877ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
688e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::PHINode *IV;
689e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    createLoop(Builder, Arg_x1, Arg_x2, &IV);
6907ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
6917ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    // Populate the actual call to kernel().
6927ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    llvm::SmallVector<llvm::Value*, 8> RootArgs;
6937ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
6944102bec56151fb5d9c962fb298412f34a6eacaa8Tobias Grosser    // Calculate the current input and output pointers
6957b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser    //
6967b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser    //
697881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // We always calculate the input/output pointers with a GEP operating on i8
698881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // values combined with a multiplication and only cast at the very end to
699881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // OutTy.  This is to account for dynamic stepping sizes when the value
700881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // isn't apparent at compile time.  In the (very common) case when we know
701881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // the step size at compile time, due to haveing complete type information
702881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // this multiplication will optmized out and produces code equivalent to a
703881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // a GEP on a pointer of the correct type.
704881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
705881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // Output
706881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
707900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    llvm::Value *OutPtr = nullptr;
708bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray    if (CastedOutBasePtr) {
7097b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser      llvm::Value *OutOffset = Builder.CreateSub(IV, Arg_x1);
710881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
711bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray      OutPtr    = Builder.CreateGEP(CastedOutBasePtr, OutOffset);
712bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
713e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes      if (PassOutByPointer) {
714881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        RootArgs.push_back(OutPtr);
715881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      }
7164102bec56151fb5d9c962fb298412f34a6eacaa8Tobias Grosser    }
7177b6629024395bf085ca64fcd5c69d2ded95e8d18Tobias Grosser
718881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    // Inputs
71974a4b08235990916911b8fe758d656c1171faf26Stephen Hines
720881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes    if (NumInputs > 0) {
721881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      llvm::Value *Offset = Builder.CreateSub(IV, Arg_x1);
722881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
723881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes      for (size_t Index = 0; Index < NumInputs; ++Index) {
724bb73b74a9f6ad26c2ab30557bfe6916a44ed75f6Tim Murray        llvm::Value *InPtr    = Builder.CreateGEP(InBasePtrs[Index], Offset);
725326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes        llvm::Value *Input;
726326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes
727326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes        if (InIsStructPointer[Index]) {
728326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          Input = InPtr;
729326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes
730326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes        } else {
731326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          llvm::LoadInst *InputLoad = Builder.CreateLoad(InPtr, "input");
732326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes
733326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          if (gEnableRsTbaa) {
734326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes            InputLoad->setMetadata("tbaa", TBAAAllocation);
735326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          }
736881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
73750f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray          InputLoad->setMetadata("alias.scope", AliasingScope);
73850f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray
739326d02a9f3cfe30caa21e5c2aecbd4c85112b363Chris Wailes          Input = InputLoad;
740881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        }
741881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes
742881cda4f5b793a1e7f7d33cf4a31efc3fdbba4fdChris Wailes        RootArgs.push_back(Input);
7439c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines      }
7447ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    }
7457ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
746e4a73f68e1b338881adf682c458e0b4b92ecd91eTobias Grosser    llvm::Value *X = IV;
747d88177580db4ddedf680854c51db333c97eabc59Stephen Hines    if (bcinfo::MetadataExtractor::hasForEachSignatureX(Signature)) {
7487ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines      RootArgs.push_back(X);
7497ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    }
7507ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
7517ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    if (Y) {
7527ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines      RootArgs.push_back(Y);
7537ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    }
7547ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
755bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::Value *RetVal = Builder.CreateCall(Function, RootArgs);
7567ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
757e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes    if (OutPtr && !PassOutByPointer) {
75818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser      llvm::StoreInst *Store = Builder.CreateStore(RetVal, OutPtr);
7599c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines      if (gEnableRsTbaa) {
7609c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines        Store->setMetadata("tbaa", TBAAAllocation);
7619c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines      }
76250f5eb4b27ce6dd2a02cf389ac2acfa95c6939f1Tim Murray      Store->setMetadata("alias.scope", AliasingScope);
7637ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines    }
7647ae3a828fade4e30f7940415949f33ac1f15dc77Stephen Hines
765db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    return true;
766db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  }
767db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
76818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// @brief Checks if pointers to allocation internals are exposed
76918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  ///
77018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// This function verifies if through the parameters passed to the kernel
77118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// or through calls to the runtime library the script gains access to
77218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// pointers pointing to data within a RenderScript Allocation.
77318a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// If we know we control all loads from and stores to data within
77418a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// RenderScript allocations and if we know the run-time internal accesses
77518a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// are all annotated with RenderScript TBAA metadata, only then we
77618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// can safely use TBAA to distinguish between generic and from-allocation
77718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// pointers.
778bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  bool allocPointersExposed(llvm::Module &Module) {
77918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // Old style kernel function can expose pointers to elements within
78018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // allocations.
78118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // TODO: Extend analysis to allow simple cases of old-style kernels.
78225eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    for (size_t i = 0; i < mExportForEachCount; ++i) {
78325eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      const char *Name = mExportForEachNameList[i];
78425eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      uint32_t Signature = mExportForEachSignatureList[i];
785bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      if (Module.getFunction(Name) &&
786d88177580db4ddedf680854c51db333c97eabc59Stephen Hines          !bcinfo::MetadataExtractor::hasForEachSignatureKernel(Signature)) {
78718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser        return true;
78818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser      }
78918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    }
79018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
79118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // Check for library functions that expose a pointer to an Allocation or
79218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // that are not yet annotated with RenderScript-specific tbaa information.
79318a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    static std::vector<std::string> Funcs;
79418a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
79518a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // rsGetElementAt(...)
79618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsGetElementAt13rs_allocationj");
79718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsGetElementAt13rs_allocationjj");
79818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsGetElementAt13rs_allocationjjj");
79918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // rsSetElementAt()
80018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvj");
80118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvjj");
80218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvjjj");
80318a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // rsGetElementAtYuv_uchar_Y()
80418a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_Y13rs_allocationjj");
80518a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // rsGetElementAtYuv_uchar_U()
80618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_U13rs_allocationjj");
80718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    // rsGetElementAtYuv_uchar_V()
80818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_V13rs_allocationjj");
80918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
81018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    for (std::vector<std::string>::iterator FI = Funcs.begin(),
81118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser                                            FE = Funcs.end();
81218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser         FI != FE; ++FI) {
813bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Function *Function = Module.getFunction(*FI);
81418a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
815bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      if (!Function) {
81618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser        ALOGE("Missing run-time function '%s'", FI->c_str());
81718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser        return true;
81818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser      }
81918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
820bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      if (Function->getNumUses() > 0) {
82118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser        return true;
82218a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser      }
82318a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    }
82418a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
82518a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    return false;
82618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  }
82718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
82818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// @brief Connect RenderScript TBAA metadata to C/C++ metadata
82918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  ///
83018a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  /// The TBAA metadata used to annotate loads/stores from RenderScript
831e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// Allocations is generated in a separate TBAA tree with a
832e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// "RenderScript TBAA" root node. LLVM does assume may-alias for all nodes in
833e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// unrelated alias analysis trees. This function makes the RenderScript TBAA
834e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// a subtree of the normal C/C++ TBAA tree aside of normal C/C++ types. With
835e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// the connected trees every access to an Allocation is resolved to
836e10b8641813c701159c6615faaa58dd1a9b31ce5Chris Wailes  /// must-alias if compared to a normal C/C++ access.
837bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  void connectRenderScriptTBAAMetadata(llvm::Module &Module) {
838bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::MDBuilder MDHelper(*Context);
839bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::MDNode *TBAARenderScript =
840bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      MDHelper.createTBAARoot("RenderScript TBAA");
84118a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
842bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::MDNode *TBAARoot     = MDHelper.createTBAARoot("Simple C/C++ TBAA");
843bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    llvm::MDNode *TBAAMergedRS = MDHelper.createTBAANode("RenderScript",
844bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes                                                         TBAARoot);
84518a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
84618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    TBAARenderScript->replaceAllUsesWith(TBAAMergedRS);
84718a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser  }
84818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
849bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes  virtual bool runOnModule(llvm::Module &Module) {
850bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bool Changed  = false;
851bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    this->Module  = &Module;
852bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    this->Context = &Module.getContext();
853bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
854bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    this->buildTypes();
855bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes
856bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bcinfo::MetadataExtractor me(&Module);
85725eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    if (!me.extract()) {
85825eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      ALOGE("Could not extract metadata from module!");
85925eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      return false;
86025eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    }
86125eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    mExportForEachCount = me.getExportForEachSignatureCount();
86225eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    mExportForEachNameList = me.getExportForEachNameList();
86325eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    mExportForEachSignatureList = me.getExportForEachSignatureList();
864db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
865bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes    bool AllocsExposed = allocPointersExposed(Module);
86618a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
86725eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines    for (size_t i = 0; i < mExportForEachCount; ++i) {
86825eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      const char *name = mExportForEachNameList[i];
86925eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines      uint32_t signature = mExportForEachSignatureList[i];
870bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      llvm::Function *kernel = Module.getFunction(name);
871cd5b657c39a348bd7652a19c5be5035e9df44a42Tobias Grosser      if (kernel) {
872d88177580db4ddedf680854c51db333c97eabc59Stephen Hines        if (bcinfo::MetadataExtractor::hasForEachSignatureKernel(signature)) {
873cd5b657c39a348bd7652a19c5be5035e9df44a42Tobias Grosser          Changed |= ExpandKernel(kernel, signature);
874acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser          kernel->setLinkage(llvm::GlobalValue::InternalLinkage);
875acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser        } else if (kernel->getReturnType()->isVoidTy()) {
876cd5b657c39a348bd7652a19c5be5035e9df44a42Tobias Grosser          Changed |= ExpandFunction(kernel, signature);
877acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser          kernel->setLinkage(llvm::GlobalValue::InternalLinkage);
878acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser        } else {
879acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser          // There are some graphics root functions that are not
880acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser          // expanded, but that will be called directly. For those
881acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser          // functions, we can not set the linkage to internal.
882acde6013e9c448547e59eed04afd2adbd9681a3aTobias Grosser        }
883cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines      }
884db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    }
885db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
8869c5263e00d89dfdd25d3a1706eb319e8529ec604Stephen Hines    if (gEnableRsTbaa && !AllocsExposed) {
887bdbff6e600b0d834e4770f65c7d2df93d7ef305cChris Wailes      connectRenderScriptTBAAMetadata(Module);
88818a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser    }
88918a38a3fc6fad8355891b771dd3c6537fa8699ecTobias Grosser
890cc366e573e31f43a6101fd6e04b90c6afdc3b7a7Stephen Hines    return Changed;
891db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  }
892db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
893db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  virtual const char *getPassName() const {
894db169187dea4602e4ad32058762d23d474753fd0Stephen Hines    return "ForEach-able Function Expansion";
895db169187dea4602e4ad32058762d23d474753fd0Stephen Hines  }
896db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
8977a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao}; // end RSForEachExpandPass
898db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
8997a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao} // end anonymous namespace
9007a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao
9017a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liaochar RSForEachExpandPass::ID = 0;
902db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
903db169187dea4602e4ad32058762d23d474753fd0Stephen Hinesnamespace bcc {
904db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
9057a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liaollvm::ModulePass *
90625eb586bb055ae07c7e82a2b1bdbd6936641580cStephen HinescreateRSForEachExpandPass(bool pEnableStepOpt){
90725eb586bb055ae07c7e82a2b1bdbd6936641580cStephen Hines  return new RSForEachExpandPass(pEnableStepOpt);
9087a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao}
909db169187dea4602e4ad32058762d23d474753fd0Stephen Hines
9107a66e6cbb1ae32cd56b19822c4e66560deb857dbShih-wei Liao} // end namespace bcc
911