| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===------ CodeGeneration.cpp - Code generate the Scops. -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The CodeGeneration pass takes a Scop created by ScopInfo and translates it |
| 11 | // back to LLVM-IR using Cloog. |
| 12 | // |
| 13 | // The Scop describes the high level memory behaviour of a control flow region. |
| 14 | // Transformation passes can update the schedule (execution order) of statements |
| 15 | // in the Scop. Cloog is used to generate an abstract syntax tree (clast) that |
| 16 | // reflects the updated execution order. This clast is used to create new |
| 17 | // LLVM-IR that is computational equivalent to the original control flow region, |
| 18 | // but executes its code in the new execution order defined by the changed |
| 19 | // scattering. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| Tobias Grosser | 0a91f32 | 2012-05-29 09:11:46 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/Cloog.h" |
| Sebastian Pop | e1f6554 | 2012-05-07 16:35:11 +0000 | [diff] [blame] | 24 | #ifdef CLOOG_FOUND |
| 25 | |
| 26 | #define DEBUG_TYPE "polly-codegen" |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 27 | #include "polly/Dependences.h" |
| Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 28 | #include "polly/LinkAllPasses.h" |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 29 | #include "polly/ScopInfo.h" |
| 30 | #include "polly/TempScopInfo.h" |
| Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 31 | #include "polly/CodeGen/CodeGeneration.h" |
| 32 | #include "polly/CodeGen/BlockGenerators.h" |
| 33 | #include "polly/CodeGen/LoopGenerators.h" |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 34 | #include "polly/CodeGen/PTXGenerator.h" |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 35 | #include "polly/CodeGen/Utils.h" |
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 36 | #include "polly/Support/GICHelper.h" |
| Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 37 | |
| Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
| Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SetVector.h" |
| Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/PostOrderIterator.h" |
| Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/LoopInfo.h" |
| 42 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
| 44 | #include "llvm/Support/Debug.h" |
| Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 45 | #include "llvm/IR/DataLayout.h" |
| Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 47 | |
| 48 | #define CLOOG_INT_GMP 1 |
| 49 | #include "cloog/cloog.h" |
| 50 | #include "cloog/isl/cloog.h" |
| 51 | |
| Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 52 | #include "isl/aff.h" |
| 53 | |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 54 | #include <vector> |
| 55 | #include <utility> |
| 56 | |
| 57 | using namespace polly; |
| 58 | using namespace llvm; |
| 59 | |
| 60 | struct isl_set; |
| 61 | |
| 62 | namespace polly { |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 63 | static cl::opt<bool> |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 64 | OpenMP("enable-polly-openmp", cl::desc("Generate OpenMP parallel code"), |
| 65 | cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"), |
| Tobias Grosser | 0acfcdb | 2012-03-16 17:17:16 +0000 | [diff] [blame] | 66 | cl::init(false), cl::ZeroOrMore); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 67 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 68 | #ifdef GPU_CODEGEN |
| 69 | static cl::opt<bool> |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 70 | GPGPU("enable-polly-gpgpu", cl::desc("Generate GPU parallel code"), cl::Hidden, |
| 71 | cl::value_desc("GPGPU code generation enabled if true"), cl::init(false), |
| 72 | cl::ZeroOrMore); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 73 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 74 | static cl::opt<std::string> GPUTriple( |
| 75 | "polly-gpgpu-triple", cl::desc("Target triple for GPU code generation"), |
| 76 | cl::Hidden, cl::init("")); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 77 | #endif /* GPU_CODEGEN */ |
| 78 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 79 | typedef DenseMap<const char *, Value *> CharMapT; |
| Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 80 | |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 81 | /// Class to generate LLVM-IR that calculates the value of a clast_expr. |
| 82 | class ClastExpCodeGen { |
| 83 | IRBuilder<> &Builder; |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 84 | const CharMapT &IVS; |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 85 | |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 86 | Value *codegen(const clast_name *e, Type *Ty); |
| 87 | Value *codegen(const clast_term *e, Type *Ty); |
| 88 | Value *codegen(const clast_binary *e, Type *Ty); |
| 89 | Value *codegen(const clast_reduction *r, Type *Ty); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 90 | public: |
| 91 | |
| 92 | // A generator for clast expressions. |
| 93 | // |
| 94 | // @param B The IRBuilder that defines where the code to calculate the |
| 95 | // clast expressions should be inserted. |
| 96 | // @param IVMAP A Map that translates strings describing the induction |
| 97 | // variables to the Values* that represent these variables |
| 98 | // on the LLVM side. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 99 | ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 100 | |
| 101 | // Generates code to calculate a given clast expression. |
| 102 | // |
| 103 | // @param e The expression to calculate. |
| 104 | // @return The Value that holds the result. |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 105 | Value *codegen(const clast_expr *e, Type *Ty); |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) { |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 109 | CharMapT::const_iterator I = IVS.find(e->name); |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 110 | |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 111 | assert(I != IVS.end() && "Clast name not found"); |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 112 | |
| 113 | return Builder.CreateSExtOrBitCast(I->second, Ty); |
| 114 | } |
| 115 | |
| 116 | Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) { |
| 117 | APInt a = APInt_from_MPZ(e->val); |
| 118 | |
| 119 | Value *ConstOne = ConstantInt::get(Builder.getContext(), a); |
| 120 | ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty); |
| 121 | |
| 122 | if (!e->var) |
| 123 | return ConstOne; |
| 124 | |
| 125 | Value *var = codegen(e->var, Ty); |
| 126 | return Builder.CreateMul(ConstOne, var); |
| 127 | } |
| 128 | |
| 129 | Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) { |
| 130 | Value *LHS = codegen(e->LHS, Ty); |
| 131 | |
| 132 | APInt RHS_AP = APInt_from_MPZ(e->RHS); |
| 133 | |
| 134 | Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP); |
| 135 | RHS = Builder.CreateSExtOrBitCast(RHS, Ty); |
| 136 | |
| 137 | switch (e->type) { |
| 138 | case clast_bin_mod: |
| 139 | return Builder.CreateSRem(LHS, RHS); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 140 | case clast_bin_fdiv: { |
| 141 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
| 142 | Value *One = ConstantInt::get(Ty, 1); |
| 143 | Value *Zero = ConstantInt::get(Ty, 0); |
| 144 | Value *Sum1 = Builder.CreateSub(LHS, RHS); |
| 145 | Value *Sum2 = Builder.CreateAdd(Sum1, One); |
| 146 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 147 | Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS); |
| 148 | return Builder.CreateSDiv(Dividend, RHS); |
| 149 | } |
| 150 | case clast_bin_cdiv: { |
| 151 | // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d |
| 152 | Value *One = ConstantInt::get(Ty, 1); |
| 153 | Value *Zero = ConstantInt::get(Ty, 0); |
| 154 | Value *Sum1 = Builder.CreateAdd(LHS, RHS); |
| 155 | Value *Sum2 = Builder.CreateSub(Sum1, One); |
| 156 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 157 | Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2); |
| 158 | return Builder.CreateSDiv(Dividend, RHS); |
| 159 | } |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 160 | case clast_bin_div: |
| 161 | return Builder.CreateSDiv(LHS, RHS); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 162 | } |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 163 | |
| 164 | llvm_unreachable("Unknown clast binary expression type"); |
| 165 | } |
| 166 | |
| 167 | Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) { |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 168 | assert((r->type == clast_red_min || r->type == clast_red_max || |
| 169 | r->type == clast_red_sum) && "Clast reduction type not supported"); |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 170 | Value *old = codegen(r->elts[0], Ty); |
| 171 | |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 172 | for (int i = 1; i < r->n; ++i) { |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 173 | Value *exprValue = codegen(r->elts[i], Ty); |
| 174 | |
| 175 | switch (r->type) { |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 176 | case clast_red_min: { |
| 177 | Value *cmp = Builder.CreateICmpSLT(old, exprValue); |
| 178 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 179 | break; |
| 180 | } |
| 181 | case clast_red_max: { |
| 182 | Value *cmp = Builder.CreateICmpSGT(old, exprValue); |
| 183 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 184 | break; |
| 185 | } |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 186 | case clast_red_sum: |
| 187 | old = Builder.CreateAdd(old, exprValue); |
| 188 | break; |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 189 | } |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 192 | return old; |
| 193 | } |
| 194 | |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 195 | ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap) |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 196 | : Builder(B), IVS(IVMap) {} |
| 197 | |
| 198 | Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) { |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 199 | switch (e->type) { |
| Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 200 | case clast_expr_name: |
| 201 | return codegen((const clast_name *)e, Ty); |
| 202 | case clast_expr_term: |
| 203 | return codegen((const clast_term *)e, Ty); |
| 204 | case clast_expr_bin: |
| 205 | return codegen((const clast_binary *)e, Ty); |
| 206 | case clast_expr_red: |
| 207 | return codegen((const clast_reduction *)e, Ty); |
| 208 | } |
| 209 | |
| 210 | llvm_unreachable("Unknown clast expression!"); |
| 211 | } |
| 212 | |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 213 | class ClastStmtCodeGen { |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 214 | public: |
| 215 | const std::vector<std::string> &getParallelLoops(); |
| 216 | |
| 217 | private: |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 218 | // The Scop we code generate. |
| 219 | Scop *S; |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 220 | Pass *P; |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 221 | |
| 222 | // The Builder specifies the current location to code generate at. |
| 223 | IRBuilder<> &Builder; |
| 224 | |
| 225 | // Map the Values from the old code to their counterparts in the new code. |
| 226 | ValueMapT ValueMap; |
| 227 | |
| 228 | // clastVars maps from the textual representation of a clast variable to its |
| 229 | // current *Value. clast variables are scheduling variables, original |
| 230 | // induction variables or parameters. They are used either in loop bounds or |
| 231 | // to define the statement instance that is executed. |
| 232 | // |
| 233 | // for (s = 0; s < n + 3; ++i) |
| 234 | // for (t = s; t < m; ++j) |
| 235 | // Stmt(i = s + 3 * m, j = t); |
| 236 | // |
| 237 | // {s,t,i,j,n,m} is the set of clast variables in this clast. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 238 | CharMapT ClastVars; |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 239 | |
| 240 | // Codegenerator for clast expressions. |
| 241 | ClastExpCodeGen ExpGen; |
| 242 | |
| 243 | // Do we currently generate parallel code? |
| 244 | bool parallelCodeGeneration; |
| 245 | |
| 246 | std::vector<std::string> parallelLoops; |
| 247 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 248 | void codegen(const clast_assignment *a); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 249 | |
| 250 | void codegen(const clast_assignment *a, ScopStmt *Statement, |
| 251 | unsigned Dimension, int vectorDim, |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 252 | std::vector<ValueMapT> *VectorVMap = 0); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 253 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 254 | void codegenSubstitutions(const clast_stmt *Assignment, ScopStmt *Statement, |
| 255 | int vectorDim = 0, |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 256 | std::vector<ValueMapT> *VectorVMap = 0); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 257 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 258 | void codegen(const clast_user_stmt *u, std::vector<Value *> *IVS = NULL, |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 259 | const char *iterator = NULL, isl_set *scatteringDomain = 0); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 260 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 261 | void codegen(const clast_block *b); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 262 | |
| 263 | /// @brief Create a classical sequential loop. |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 264 | void codegenForSequential(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 265 | |
| 266 | /// @brief Create OpenMP structure values. |
| 267 | /// |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 268 | /// Create a list of values that has to be stored into the OpenMP subfuncition |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 269 | /// structure. |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 270 | SetVector<Value *> getOMPValues(const clast_stmt *Body); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 271 | |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 272 | /// @brief Update ClastVars and ValueMap according to a value map. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 273 | /// |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 274 | /// @param VMap A map from old to new values. |
| 275 | void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 276 | |
| 277 | /// @brief Create an OpenMP parallel for loop. |
| 278 | /// |
| 279 | /// This loop reflects a loop as if it would have been created by an OpenMP |
| 280 | /// statement. |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 281 | void codegenForOpenMP(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 282 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 283 | #ifdef GPU_CODEGEN |
| 284 | /// @brief Create GPGPU device memory access values. |
| 285 | /// |
| 286 | /// Create a list of values that will be set to be parameters of the GPGPU |
| 287 | /// subfunction. These parameters represent device memory base addresses |
| 288 | /// and the size in bytes. |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 289 | SetVector<Value *> getGPUValues(unsigned &OutputBytes); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 290 | |
| 291 | /// @brief Create a GPU parallel for loop. |
| 292 | /// |
| 293 | /// This loop reflects a loop as if it would have been created by a GPU |
| 294 | /// statement. |
| 295 | void codegenForGPGPU(const clast_for *F); |
| 296 | |
| 297 | /// @brief Get innermost for loop. |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 298 | const clast_stmt * |
| 299 | getScheduleInfo(const clast_for *F, std::vector<int> &NumIters, |
| 300 | unsigned &LoopDepth, unsigned &NonPLoopDepth); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 301 | #endif /* GPU_CODEGEN */ |
| 302 | |
| Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 303 | /// @brief Check if a loop is parallel |
| 304 | /// |
| 305 | /// Detect if a clast_for loop can be executed in parallel. |
| 306 | /// |
| Tobias Grosser | c1b6cec | 2012-11-19 12:26:25 +0000 | [diff] [blame] | 307 | /// @param For The clast for loop to check. |
| Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 308 | /// |
| 309 | /// @return bool Returns true if the incoming clast_for statement can |
| 310 | /// execute in parallel. |
| 311 | bool isParallelFor(const clast_for *For); |
| 312 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 313 | bool isInnermostLoop(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 314 | |
| 315 | /// @brief Get the number of loop iterations for this loop. |
| 316 | /// @param f The clast for loop to check. |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 317 | int getNumberOfIterations(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 318 | |
| 319 | /// @brief Create vector instructions for this loop. |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 320 | void codegenForVector(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 321 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 322 | void codegen(const clast_for *f); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 323 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 324 | Value *codegen(const clast_equation *eq); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 325 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 326 | void codegen(const clast_guard *g); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 327 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 328 | void codegen(const clast_stmt *stmt); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 329 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 330 | void addParameters(const CloogNames *names); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 331 | |
| Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 332 | IntegerType *getIntPtrTy(); |
| 333 | |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 334 | public: |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 335 | void codegen(const clast_root *r); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 336 | |
| Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 337 | ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 338 | }; |
| 339 | } |
| 340 | |
| Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 341 | IntegerType *ClastStmtCodeGen::getIntPtrTy() { |
| Tobias Grosser | 5d01691d | 2012-11-01 16:45:18 +0000 | [diff] [blame] | 342 | return P->getAnalysis<DataLayout>().getIntPtrType(Builder.getContext()); |
| Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 345 | const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() { |
| 346 | return parallelLoops; |
| 347 | } |
| 348 | |
| 349 | void ClastStmtCodeGen::codegen(const clast_assignment *a) { |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 350 | Value *V = ExpGen.codegen(a->RHS, getIntPtrTy()); |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 351 | ClastVars[a->LHS] = V; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 354 | void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt, |
| 355 | unsigned Dim, int VectorDim, |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 356 | std::vector<ValueMapT> *VectorVMap) { |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 357 | const PHINode *PN; |
| Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 358 | Value *RHS; |
| 359 | |
| 360 | assert(!A->LHS && "Statement assignments do not have left hand side"); |
| 361 | |
| Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 362 | PN = Stmt->getInductionVariableForDimension(Dim); |
| Tobias Grosser | 0905a23 | 2012-04-03 12:24:32 +0000 | [diff] [blame] | 363 | RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty()); |
| 364 | RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 365 | |
| 366 | if (VectorVMap) |
| Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 367 | (*VectorVMap)[VectorDim][PN] = RHS; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 368 | |
| Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 369 | ValueMap[PN] = RHS; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 372 | void ClastStmtCodeGen::codegenSubstitutions( |
| 373 | const clast_stmt *Assignment, ScopStmt *Statement, int vectorDim, |
| 374 | std::vector<ValueMapT> *VectorVMap) { |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 375 | int Dimension = 0; |
| 376 | |
| 377 | while (Assignment) { |
| Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 378 | assert(CLAST_STMT_IS_A(Assignment, stmt_ass) && |
| 379 | "Substitions are expected to be assignments"); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 380 | codegen((const clast_assignment *)Assignment, Statement, Dimension, |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 381 | vectorDim, VectorVMap); |
| 382 | Assignment = Assignment->next; |
| 383 | Dimension++; |
| 384 | } |
| 385 | } |
| 386 | |
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 387 | // Takes the cloog specific domain and translates it into a map Statement -> |
| 388 | // PartialSchedule, where the PartialSchedule contains all the dimensions that |
| 389 | // have been code generated up to this point. |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 390 | static __isl_give isl_map * |
| 391 | extractPartialSchedule(ScopStmt *Statement, isl_set *Domain) { |
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 392 | isl_map *Schedule = Statement->getScattering(); |
| 393 | int ScheduledDimensions = isl_set_dim(Domain, isl_dim_set); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 394 | int UnscheduledDimensions = |
| 395 | isl_map_dim(Schedule, isl_dim_out) - ScheduledDimensions; |
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 396 | |
| 397 | return isl_map_project_out(Schedule, isl_dim_out, ScheduledDimensions, |
| 398 | UnscheduledDimensions); |
| 399 | } |
| 400 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 401 | void ClastStmtCodeGen::codegen(const clast_user_stmt *u, |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 402 | std::vector<Value *> *IVS, const char *iterator, |
| Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 403 | isl_set *Domain) { |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 404 | ScopStmt *Statement = (ScopStmt *)u->statement->usr; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 405 | |
| 406 | if (u->substitutions) |
| 407 | codegenSubstitutions(u->substitutions, Statement); |
| 408 | |
| Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 409 | int VectorDimensions = IVS ? IVS->size() : 1; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 410 | |
| Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 411 | if (VectorDimensions == 1) { |
| Tobias Grosser | 55d5208 | 2012-03-02 15:20:39 +0000 | [diff] [blame] | 412 | BlockGenerator::generate(Builder, *Statement, ValueMap, P); |
| Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | VectorValueMapT VectorMap(VectorDimensions); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 417 | |
| 418 | if (IVS) { |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 419 | assert(u->substitutions && "Substitutions expected!"); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 420 | int i = 0; |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 421 | for (std::vector<Value *>::iterator II = IVS->begin(), IE = IVS->end(); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 422 | II != IE; ++II) { |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 423 | ClastVars[iterator] = *II; |
| Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 424 | codegenSubstitutions(u->substitutions, Statement, i, &VectorMap); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 425 | i++; |
| 426 | } |
| 427 | } |
| 428 | |
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 429 | isl_map *Schedule = extractPartialSchedule(Statement, Domain); |
| 430 | VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Schedule, P); |
| 431 | isl_map_free(Schedule); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void ClastStmtCodeGen::codegen(const clast_block *b) { |
| 435 | if (b->body) |
| 436 | codegen(b->body); |
| 437 | } |
| 438 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 439 | void ClastStmtCodeGen::codegenForSequential(const clast_for *f) { |
| 440 | Value *LowerBound, *UpperBound, *IV, *Stride; |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 441 | BasicBlock *AfterBB; |
| Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 442 | Type *IntPtrTy = getIntPtrTy(); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 443 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 444 | LowerBound = ExpGen.codegen(f->LB, IntPtrTy); |
| 445 | UpperBound = ExpGen.codegen(f->UB, IntPtrTy); |
| 446 | Stride = Builder.getInt(APInt_from_MPZ(f->stride)); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 447 | |
| Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 448 | IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB, |
| 449 | CmpInst::ICMP_SLE); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 450 | |
| 451 | // Add loop iv to symbols. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 452 | ClastVars[f->iterator] = IV; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 453 | |
| 454 | if (f->body) |
| 455 | codegen(f->body); |
| 456 | |
| 457 | // Loop is finished, so remove its iv from the live symbols. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 458 | ClastVars.erase(f->iterator); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 459 | Builder.SetInsertPoint(AfterBB->begin()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 462 | // Helper class to determine all scalar parameters used in the basic blocks of a |
| 463 | // clast. Scalar parameters are scalar variables defined outside of the SCoP. |
| 464 | class ParameterVisitor : public ClastVisitor { |
| 465 | std::set<Value *> Values; |
| 466 | public: |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 467 | ParameterVisitor() : ClastVisitor(), Values() {} |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 468 | |
| 469 | void visitUser(const clast_user_stmt *Stmt) { |
| 470 | const ScopStmt *S = static_cast<const ScopStmt *>(Stmt->statement->usr); |
| 471 | const BasicBlock *BB = S->getBasicBlock(); |
| 472 | |
| 473 | // Check all the operands of instructions in the basic block. |
| 474 | for (BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); BI != BE; |
| 475 | ++BI) { |
| 476 | const Instruction &Inst = *BI; |
| 477 | for (Instruction::const_op_iterator II = Inst.op_begin(), |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 478 | IE = Inst.op_end(); |
| 479 | II != IE; ++II) { |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 480 | Value *SrcVal = *II; |
| 481 | |
| 482 | if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal)) |
| 483 | if (S->getParent()->getRegion().contains(OpInst)) |
| 484 | continue; |
| 485 | |
| 486 | if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal)) |
| 487 | Values.insert(SrcVal); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Iterator to iterate over the values found. |
| 493 | typedef std::set<Value *>::const_iterator const_iterator; |
| 494 | inline const_iterator begin() const { return Values.begin(); } |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 495 | inline const_iterator end() const { return Values.end(); } |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 496 | }; |
| 497 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 498 | SetVector<Value *> ClastStmtCodeGen::getOMPValues(const clast_stmt *Body) { |
| 499 | SetVector<Value *> Values; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 500 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 501 | // The clast variables |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 502 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); I != E; |
| 503 | I++) |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 504 | Values.insert(I->second); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 505 | |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 506 | // Find the temporaries that are referenced in the clast statements' |
| 507 | // basic blocks but are not defined by these blocks (e.g., references |
| 508 | // to function arguments or temporaries defined before the start of |
| 509 | // the SCoP). |
| 510 | ParameterVisitor Params; |
| 511 | Params.visit(Body); |
| 512 | |
| 513 | for (ParameterVisitor::const_iterator PI = Params.begin(), PE = Params.end(); |
| 514 | PI != PE; ++PI) { |
| 515 | Value *V = *PI; |
| 516 | Values.insert(V); |
| 517 | DEBUG(dbgs() << "Adding temporary for OMP copy-in: " << *V << "\n"); |
| 518 | } |
| 519 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 520 | return Values; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 523 | void ClastStmtCodeGen::updateWithValueMap( |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 524 | OMPGenerator::ValueToValueMapTy &VMap) { |
| 525 | std::set<Value *> Inserted; |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 526 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 527 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); I != E; |
| 528 | I++) { |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 529 | ClastVars[I->first] = VMap[I->second]; |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 530 | Inserted.insert(I->second); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 533 | for (OMPGenerator::ValueToValueMapTy::iterator I = VMap.begin(), |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 534 | E = VMap.end(); |
| 535 | I != E; ++I) { |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 536 | if (Inserted.count(I->first)) |
| 537 | continue; |
| 538 | |
| 539 | ValueMap[I->first] = I->second; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 543 | static void clearDomtree(Function *F, DominatorTree &DT) { |
| 544 | DomTreeNode *N = DT.getNode(&F->getEntryBlock()); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 545 | std::vector<BasicBlock *> Nodes; |
| 546 | for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I) |
| Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 547 | Nodes.push_back(I->getBlock()); |
| 548 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 549 | for (std::vector<BasicBlock *>::iterator I = Nodes.begin(), E = Nodes.end(); |
| Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 550 | I != E; ++I) |
| 551 | DT.eraseNode(*I); |
| 552 | } |
| 553 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 554 | void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) { |
| Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 555 | Value *Stride, *LB, *UB, *IV; |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 556 | BasicBlock::iterator LoopBody; |
| 557 | IntegerType *IntPtrTy = getIntPtrTy(); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 558 | SetVector<Value *> Values; |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 559 | OMPGenerator::ValueToValueMapTy VMap; |
| 560 | OMPGenerator OMPGen(Builder, P); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 561 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 562 | Stride = Builder.getInt(APInt_from_MPZ(For->stride)); |
| 563 | Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy); |
| Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 564 | LB = ExpGen.codegen(For->LB, IntPtrTy); |
| 565 | UB = ExpGen.codegen(For->UB, IntPtrTy); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 566 | |
| Tobias Grosser | 177982c | 2012-11-01 05:34:48 +0000 | [diff] [blame] | 567 | Values = getOMPValues(For->body); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 568 | |
| Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 569 | IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody); |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 570 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
| 571 | Builder.SetInsertPoint(LoopBody); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 572 | |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 573 | // Save the current values. |
| 574 | const ValueMapT ValueMapCopy = ValueMap; |
| 575 | const CharMapT ClastVarsCopy = ClastVars; |
| 576 | |
| 577 | updateWithValueMap(VMap); |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 578 | ClastVars[For->iterator] = IV; |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 579 | |
| 580 | if (For->body) |
| 581 | codegen(For->body); |
| 582 | |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 583 | // Restore the original values. |
| 584 | ValueMap = ValueMapCopy; |
| 585 | ClastVars = ClastVarsCopy; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 586 | |
| Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 587 | clearDomtree((*LoopBody).getParent()->getParent(), |
| 588 | P->getAnalysis<DominatorTree>()); |
| 589 | |
| Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 590 | Builder.SetInsertPoint(AfterLoop); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 593 | #ifdef GPU_CODEGEN |
| 594 | static unsigned getArraySizeInBytes(const ArrayType *AT) { |
| 595 | unsigned Bytes = AT->getNumElements(); |
| 596 | if (const ArrayType *T = dyn_cast<ArrayType>(AT->getElementType())) |
| 597 | Bytes *= getArraySizeInBytes(T); |
| 598 | else |
| 599 | Bytes *= AT->getElementType()->getPrimitiveSizeInBits() / 8; |
| 600 | |
| 601 | return Bytes; |
| 602 | } |
| 603 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 604 | SetVector<Value *> ClastStmtCodeGen::getGPUValues(unsigned &OutputBytes) { |
| 605 | SetVector<Value *> Values; |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 606 | OutputBytes = 0; |
| 607 | |
| 608 | // Record the memory reference base addresses. |
| 609 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { |
| 610 | ScopStmt *Stmt = *SI; |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 611 | for (SmallVector<MemoryAccess *, 8>::iterator I = Stmt->memacc_begin(), |
| 612 | E = Stmt->memacc_end(); |
| 613 | I != E; ++I) { |
| 614 | Value *BaseAddr = const_cast<Value *>((*I)->getBaseAddr()); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 615 | Values.insert((BaseAddr)); |
| 616 | |
| 617 | // FIXME: we assume that there is one and only one array to be written |
| 618 | // in a SCoP. |
| 619 | int NumWrites = 0; |
| 620 | if ((*I)->isWrite()) { |
| 621 | ++NumWrites; |
| 622 | assert(NumWrites <= 1 && |
| 623 | "We support at most one array to be written in a SCoP."); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 624 | if (const PointerType *PT = |
| 625 | dyn_cast<PointerType>(BaseAddr->getType())) { |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 626 | Type *T = PT->getArrayElementType(); |
| 627 | const ArrayType *ATy = dyn_cast<ArrayType>(T); |
| 628 | OutputBytes = getArraySizeInBytes(ATy); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return Values; |
| 635 | } |
| 636 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 637 | const clast_stmt *ClastStmtCodeGen::getScheduleInfo( |
| 638 | const clast_for *F, std::vector<int> &NumIters, unsigned &LoopDepth, |
| 639 | unsigned &NonPLoopDepth) { |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 640 | clast_stmt *Stmt = (clast_stmt *)F; |
| 641 | const clast_for *Result; |
| 642 | bool NonParaFlag = false; |
| 643 | LoopDepth = 0; |
| 644 | NonPLoopDepth = 0; |
| 645 | |
| 646 | while (Stmt) { |
| 647 | if (CLAST_STMT_IS_A(Stmt, stmt_for)) { |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 648 | const clast_for *T = (clast_for *)Stmt; |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 649 | if (isParallelFor(T)) { |
| 650 | if (!NonParaFlag) { |
| 651 | NumIters.push_back(getNumberOfIterations(T)); |
| 652 | Result = T; |
| 653 | } |
| 654 | } else |
| 655 | NonParaFlag = true; |
| 656 | |
| 657 | Stmt = T->body; |
| 658 | LoopDepth++; |
| 659 | continue; |
| 660 | } |
| 661 | Stmt = Stmt->next; |
| 662 | } |
| 663 | |
| 664 | assert(NumIters.size() == 4 && |
| 665 | "The loops should be tiled into 4-depth parallel loops and an " |
| 666 | "innermost non-parallel one (if exist)."); |
| 667 | NonPLoopDepth = LoopDepth - NumIters.size(); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 668 | assert(NonPLoopDepth <= 1 && |
| 669 | "We support only one innermost non-parallel loop currently."); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 670 | return (const clast_stmt *)Result->body; |
| 671 | } |
| 672 | |
| 673 | void ClastStmtCodeGen::codegenForGPGPU(const clast_for *F) { |
| 674 | BasicBlock::iterator LoopBody; |
| 675 | SetVector<Value *> Values; |
| 676 | SetVector<Value *> IVS; |
| 677 | std::vector<int> NumIterations; |
| 678 | PTXGenerator::ValueToValueMapTy VMap; |
| 679 | |
| Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 680 | assert(!GPUTriple.empty() && |
| 681 | "Target triple should be set properly for GPGPU code generation."); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 682 | PTXGenerator PTXGen(Builder, P, GPUTriple); |
| 683 | |
| 684 | // Get original IVS and ScopStmt |
| 685 | unsigned TiledLoopDepth, NonPLoopDepth; |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 686 | const clast_stmt *InnerStmt = |
| 687 | getScheduleInfo(F, NumIterations, TiledLoopDepth, NonPLoopDepth); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 688 | const clast_stmt *TmpStmt; |
| 689 | const clast_user_stmt *U; |
| 690 | const clast_for *InnerFor; |
| 691 | if (CLAST_STMT_IS_A(InnerStmt, stmt_for)) { |
| 692 | InnerFor = (const clast_for *)InnerStmt; |
| 693 | TmpStmt = InnerFor->body; |
| 694 | } else |
| 695 | TmpStmt = InnerStmt; |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 696 | U = (const clast_user_stmt *)TmpStmt; |
| 697 | ScopStmt *Statement = (ScopStmt *)U->statement->usr; |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 698 | for (unsigned i = 0; i < Statement->getNumIterators() - NonPLoopDepth; i++) { |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 699 | const Value *IV = Statement->getInductionVariableForDimension(i); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 700 | IVS.insert(const_cast<Value *>(IV)); |
| 701 | } |
| 702 | |
| 703 | unsigned OutBytes; |
| 704 | Values = getGPUValues(OutBytes); |
| 705 | PTXGen.setOutputBytes(OutBytes); |
| 706 | PTXGen.startGeneration(Values, IVS, VMap, &LoopBody); |
| 707 | |
| 708 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
| 709 | Builder.SetInsertPoint(LoopBody); |
| 710 | |
| 711 | BasicBlock *AfterBB = 0; |
| 712 | if (NonPLoopDepth) { |
| 713 | Value *LowerBound, *UpperBound, *IV, *Stride; |
| 714 | Type *IntPtrTy = getIntPtrTy(); |
| 715 | LowerBound = ExpGen.codegen(InnerFor->LB, IntPtrTy); |
| 716 | UpperBound = ExpGen.codegen(InnerFor->UB, IntPtrTy); |
| 717 | Stride = Builder.getInt(APInt_from_MPZ(InnerFor->stride)); |
| Tobias Grosser | 6f3d063 | 2012-11-30 00:39:49 +0000 | [diff] [blame] | 718 | IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB, |
| 719 | CmpInst::ICMP_SLE); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 720 | const Value *OldIV_ = Statement->getInductionVariableForDimension(2); |
| 721 | Value *OldIV = const_cast<Value *>(OldIV_); |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 722 | VMap.insert(std::make_pair<Value *, Value *>(OldIV, IV)); |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| Tobias Grosser | 46c6abb | 2012-11-30 01:05:05 +0000 | [diff] [blame] | 725 | updateWithValueMap(VMap); |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 726 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 727 | BlockGenerator::generate(Builder, *Statement, ValueMap, P); |
| Tobias Grosser | a17f666 | 2012-11-01 05:34:35 +0000 | [diff] [blame] | 728 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 729 | if (AfterBB) |
| 730 | Builder.SetInsertPoint(AfterBB->begin()); |
| 731 | |
| 732 | // FIXME: The replacement of the host base address with the parameter of ptx |
| 733 | // subfunction should have been done by updateWithValueMap. We use the |
| 734 | // following codes to avoid affecting other parts of Polly. This should be |
| 735 | // fixed later. |
| 736 | Function *FN = Builder.GetInsertBlock()->getParent(); |
| 737 | for (unsigned j = 0; j < Values.size(); j++) { |
| 738 | Value *baseAddr = Values[j]; |
| 739 | for (Function::iterator B = FN->begin(); B != FN->end(); ++B) { |
| 740 | for (BasicBlock::iterator I = B->begin(); I != B->end(); ++I) |
| 741 | I->replaceUsesOfWith(baseAddr, ValueMap[baseAddr]); |
| 742 | } |
| 743 | } |
| 744 | Builder.SetInsertPoint(AfterLoop); |
| 745 | PTXGen.setLaunchingParameters(NumIterations[0], NumIterations[1], |
| 746 | NumIterations[2], NumIterations[3]); |
| 747 | PTXGen.finishGeneration(FN); |
| 748 | } |
| 749 | #endif |
| 750 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 751 | bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) { |
| 752 | const clast_stmt *stmt = f->body; |
| 753 | |
| 754 | while (stmt) { |
| 755 | if (!CLAST_STMT_IS_A(stmt, stmt_user)) |
| 756 | return false; |
| 757 | |
| 758 | stmt = stmt->next; |
| 759 | } |
| 760 | |
| 761 | return true; |
| 762 | } |
| 763 | |
| Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 764 | int ClastStmtCodeGen::getNumberOfIterations(const clast_for *For) { |
| 765 | isl_set *LoopDomain = isl_set_copy(isl_set_from_cloog_domain(For->domain)); |
| Sebastian Pop | 2aa5c24 | 2012-12-18 08:56:51 +0000 | [diff] [blame] | 766 | int NumberOfIterations = polly::getNumberOfIterations(LoopDomain); |
| 767 | if (NumberOfIterations == -1) |
| 768 | return -1; |
| 769 | return NumberOfIterations / isl_int_get_si(For->stride) + 1; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 772 | void ClastStmtCodeGen::codegenForVector(const clast_for *F) { |
| 773 | DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";); |
| 774 | int VectorWidth = getNumberOfIterations(F); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 775 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 776 | Value *LB = ExpGen.codegen(F->LB, getIntPtrTy()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 777 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 778 | APInt Stride = APInt_from_MPZ(F->stride); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 779 | IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType()); |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 780 | Stride = Stride.zext(LoopIVType->getBitWidth()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 781 | Value *StrideValue = ConstantInt::get(LoopIVType, Stride); |
| 782 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 783 | std::vector<Value *> IVS(VectorWidth); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 784 | IVS[0] = LB; |
| 785 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 786 | for (int i = 1; i < VectorWidth; i++) |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 787 | IVS[i] = Builder.CreateAdd(IVS[i - 1], StrideValue, "p_vector_iv"); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 788 | |
| Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 789 | isl_set *Domain = isl_set_from_cloog_domain(F->domain); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 790 | |
| 791 | // Add loop iv to symbols. |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 792 | ClastVars[F->iterator] = LB; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 793 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 794 | const clast_stmt *Stmt = F->body; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 795 | |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 796 | while (Stmt) { |
| Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 797 | codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator, |
| 798 | isl_set_copy(Domain)); |
| Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 799 | Stmt = Stmt->next; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | // Loop is finished, so remove its iv from the live symbols. |
| Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 803 | isl_set_free(Domain); |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 804 | ClastVars.erase(F->iterator); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 807 | bool ClastStmtCodeGen::isParallelFor(const clast_for *f) { |
| 808 | isl_set *Domain = isl_set_from_cloog_domain(f->domain); |
| 809 | assert(Domain && "Cannot access domain of loop"); |
| 810 | |
| 811 | Dependences &D = P->getAnalysis<Dependences>(); |
| 812 | |
| 813 | return D.isParallelDimension(isl_set_copy(Domain), isl_set_n_dim(Domain)); |
| 814 | } |
| 815 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 816 | void ClastStmtCodeGen::codegen(const clast_for *f) { |
| Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 817 | bool Vector = PollyVectorizerChoice != VECTORIZER_NONE; |
| Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 818 | if ((Vector || OpenMP) && isParallelFor(f)) { |
| Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 819 | if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f)) && |
| 820 | (getNumberOfIterations(f) <= 16)) { |
| Tobias Grosser | ce3f537 | 2012-03-02 11:26:42 +0000 | [diff] [blame] | 821 | codegenForVector(f); |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | if (OpenMP && !parallelCodeGeneration) { |
| 826 | parallelCodeGeneration = true; |
| 827 | parallelLoops.push_back(f->iterator); |
| 828 | codegenForOpenMP(f); |
| 829 | parallelCodeGeneration = false; |
| 830 | return; |
| 831 | } |
| 832 | } |
| 833 | |
| Tobias Grosser | 6217e18 | 2012-08-03 12:50:07 +0000 | [diff] [blame] | 834 | #ifdef GPU_CODEGEN |
| 835 | if (GPGPU && isParallelFor(f)) { |
| 836 | if (!parallelCodeGeneration) { |
| 837 | parallelCodeGeneration = true; |
| 838 | parallelLoops.push_back(f->iterator); |
| 839 | codegenForGPGPU(f); |
| 840 | parallelCodeGeneration = false; |
| 841 | return; |
| 842 | } |
| 843 | } |
| 844 | #endif |
| 845 | |
| Tobias Grosser | ce3f537 | 2012-03-02 11:26:42 +0000 | [diff] [blame] | 846 | codegenForSequential(f); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | Value *ClastStmtCodeGen::codegen(const clast_equation *eq) { |
| Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 850 | Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy()); |
| 851 | Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 852 | CmpInst::Predicate P; |
| 853 | |
| 854 | if (eq->sign == 0) |
| 855 | P = ICmpInst::ICMP_EQ; |
| 856 | else if (eq->sign > 0) |
| 857 | P = ICmpInst::ICMP_SGE; |
| 858 | else |
| 859 | P = ICmpInst::ICMP_SLE; |
| 860 | |
| 861 | return Builder.CreateICmp(P, LHS, RHS); |
| 862 | } |
| 863 | |
| 864 | void ClastStmtCodeGen::codegen(const clast_guard *g) { |
| 865 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 866 | LLVMContext &Context = F->getContext(); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 867 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 868 | BasicBlock *CondBB = |
| 869 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 870 | CondBB->setName("polly.cond"); |
| 871 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 872 | MergeBB->setName("polly.merge"); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 873 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 874 | |
| Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 875 | DominatorTree &DT = P->getAnalysis<DominatorTree>(); |
| 876 | DT.addNewBlock(ThenBB, CondBB); |
| 877 | DT.changeImmediateDominator(MergeBB, CondBB); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 878 | |
| 879 | CondBB->getTerminator()->eraseFromParent(); |
| 880 | |
| 881 | Builder.SetInsertPoint(CondBB); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 882 | |
| 883 | Value *Predicate = codegen(&(g->eq[0])); |
| 884 | |
| 885 | for (int i = 1; i < g->n; ++i) { |
| 886 | Value *TmpPredicate = codegen(&(g->eq[i])); |
| 887 | Predicate = Builder.CreateAnd(Predicate, TmpPredicate); |
| 888 | } |
| 889 | |
| 890 | Builder.CreateCondBr(Predicate, ThenBB, MergeBB); |
| 891 | Builder.SetInsertPoint(ThenBB); |
| Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 892 | Builder.CreateBr(MergeBB); |
| 893 | Builder.SetInsertPoint(ThenBB->begin()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 894 | |
| 895 | codegen(g->then); |
| Tobias Grosser | 62a3c96 | 2012-02-16 09:56:21 +0000 | [diff] [blame] | 896 | |
| 897 | Builder.SetInsertPoint(MergeBB->begin()); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void ClastStmtCodeGen::codegen(const clast_stmt *stmt) { |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 901 | if (CLAST_STMT_IS_A(stmt, stmt_root)) |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 902 | assert(false && "No second root statement expected"); |
| 903 | else if (CLAST_STMT_IS_A(stmt, stmt_ass)) |
| 904 | codegen((const clast_assignment *)stmt); |
| 905 | else if (CLAST_STMT_IS_A(stmt, stmt_user)) |
| 906 | codegen((const clast_user_stmt *)stmt); |
| 907 | else if (CLAST_STMT_IS_A(stmt, stmt_block)) |
| 908 | codegen((const clast_block *)stmt); |
| 909 | else if (CLAST_STMT_IS_A(stmt, stmt_for)) |
| 910 | codegen((const clast_for *)stmt); |
| 911 | else if (CLAST_STMT_IS_A(stmt, stmt_guard)) |
| 912 | codegen((const clast_guard *)stmt); |
| 913 | |
| 914 | if (stmt->next) |
| 915 | codegen(stmt->next); |
| 916 | } |
| 917 | |
| 918 | void ClastStmtCodeGen::addParameters(const CloogNames *names) { |
| Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 919 | SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly"); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 920 | |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 921 | int i = 0; |
| 922 | for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end(); |
| 923 | PI != PE; ++PI) { |
| 924 | assert(i < names->nb_parameters && "Not enough parameter names"); |
| 925 | |
| 926 | const SCEV *Param = *PI; |
| 927 | Type *Ty = Param->getType(); |
| 928 | |
| 929 | Instruction *insertLocation = --(Builder.GetInsertBlock()->end()); |
| 930 | Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation); |
| Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 931 | ClastVars[names->parameters[i]] = V; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 932 | |
| 933 | ++i; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | void ClastStmtCodeGen::codegen(const clast_root *r) { |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 938 | addParameters(r->names); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 939 | |
| 940 | parallelCodeGeneration = false; |
| 941 | |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 942 | const clast_stmt *stmt = (const clast_stmt *)r; |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 943 | if (stmt->next) |
| 944 | codegen(stmt->next); |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 947 | ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) |
| 948 | : S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) { |
| 949 | } |
| Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 950 | |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 951 | namespace { |
| 952 | class CodeGeneration : public ScopPass { |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 953 | std::vector<std::string> ParallelLoops; |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 954 | |
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 955 | public: |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 956 | static char ID; |
| 957 | |
| 958 | CodeGeneration() : ScopPass(ID) {} |
| 959 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 960 | bool runOnScop(Scop &S) { |
| 961 | ParallelLoops.clear(); |
| Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 962 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 963 | assert(S.getRegion().isSimple() && "Only simple regions are supported"); |
| Tobias Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 964 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 965 | BasicBlock *StartBlock = executeScopConditionally(S, this); |
| Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 966 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 967 | IRBuilder<> Builder(StartBlock->begin()); |
| Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 968 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 969 | ClastStmtCodeGen CodeGen(&S, Builder, this); |
| Tobias Grosser | 3fdecae | 2011-05-14 19:02:39 +0000 | [diff] [blame] | 970 | CloogInfo &C = getAnalysis<CloogInfo>(); |
| 971 | CodeGen.codegen(C.getClast()); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 972 | |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 973 | ParallelLoops.insert(ParallelLoops.begin(), |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 974 | CodeGen.getParallelLoops().begin(), |
| 975 | CodeGen.getParallelLoops().end()); |
| Tobias Grosser | abb6dcd | 2011-05-14 19:02:34 +0000 | [diff] [blame] | 976 | return true; |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | virtual void printScop(raw_ostream &OS) const { |
| Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 980 | for (std::vector<std::string>::const_iterator PI = ParallelLoops.begin(), |
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame^] | 981 | PE = ParallelLoops.end(); |
| 982 | PI != PE; ++PI) |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 983 | OS << "Parallel loop with iterator '" << *PI << "' generated\n"; |
| 984 | } |
| 985 | |
| 986 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 987 | AU.addRequired<CloogInfo>(); |
| 988 | AU.addRequired<Dependences>(); |
| 989 | AU.addRequired<DominatorTree>(); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 990 | AU.addRequired<RegionInfo>(); |
| Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 991 | AU.addRequired<ScalarEvolution>(); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 992 | AU.addRequired<ScopDetection>(); |
| 993 | AU.addRequired<ScopInfo>(); |
| Micah Villmow | 7a3d820 | 2012-10-08 17:26:19 +0000 | [diff] [blame] | 994 | AU.addRequired<DataLayout>(); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 995 | |
| 996 | AU.addPreserved<CloogInfo>(); |
| 997 | AU.addPreserved<Dependences>(); |
| Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 998 | |
| Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 999 | // FIXME: We do not create LoopInfo for the newly generated loops. |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1000 | AU.addPreserved<LoopInfo>(); |
| 1001 | AU.addPreserved<DominatorTree>(); |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1002 | AU.addPreserved<ScopDetection>(); |
| 1003 | AU.addPreserved<ScalarEvolution>(); |
| Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 1004 | |
| Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 1005 | // FIXME: We do not yet add regions for the newly generated code to the |
| 1006 | // region tree. |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1007 | AU.addPreserved<RegionInfo>(); |
| 1008 | AU.addPreserved<TempScopInfo>(); |
| 1009 | AU.addPreserved<ScopInfo>(); |
| 1010 | AU.addPreservedID(IndependentBlocksID); |
| 1011 | } |
| 1012 | }; |
| 1013 | } |
| 1014 | |
| 1015 | char CodeGeneration::ID = 1; |
| 1016 | |
| Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1017 | INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", |
| Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 1018 | "Polly - Create LLVM-IR from SCoPs", false, false) |
| Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1019 | INITIALIZE_PASS_DEPENDENCY(CloogInfo) |
| 1020 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 1021 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 1022 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 1023 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 1024 | INITIALIZE_PASS_DEPENDENCY(ScopDetection) |
| Tobias Grosser | 660b58d | 2012-10-08 08:56:52 +0000 | [diff] [blame] | 1025 | INITIALIZE_PASS_DEPENDENCY(DataLayout) |
| Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1026 | INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", |
| Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 1027 | "Polly - Create LLVM-IR from SCoPs", false, false) |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1028 | |
| Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1029 | Pass *polly::createCodeGenerationPass() { |
| Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1030 | return new CodeGeneration(); |
| 1031 | } |
| Sebastian Pop | e1f6554 | 2012-05-07 16:35:11 +0000 | [diff] [blame] | 1032 | |
| 1033 | #endif // CLOOG_FOUND |