1 //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===// 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 // Take a scop created by ScopInfo and map it to GPU code using the ppcg 11 // GPU mapping strategy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "polly/CodeGen/PPCGCodeGeneration.h" 16 #include "polly/CodeGen/CodeGeneration.h" 17 #include "polly/CodeGen/IslAst.h" 18 #include "polly/CodeGen/IslNodeBuilder.h" 19 #include "polly/CodeGen/Utils.h" 20 #include "polly/DependenceInfo.h" 21 #include "polly/LinkAllPasses.h" 22 #include "polly/Options.h" 23 #include "polly/ScopDetection.h" 24 #include "polly/ScopInfo.h" 25 #include "polly/Support/SCEVValidator.h" 26 #include "llvm/ADT/PostOrderIterator.h" 27 #include "llvm/Analysis/AliasAnalysis.h" 28 #include "llvm/Analysis/BasicAliasAnalysis.h" 29 #include "llvm/Analysis/GlobalsModRef.h" 30 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 31 #include "llvm/Analysis/TargetLibraryInfo.h" 32 #include "llvm/Analysis/TargetTransformInfo.h" 33 #include "llvm/IR/LegacyPassManager.h" 34 #include "llvm/IR/Verifier.h" 35 #include "llvm/IRReader/IRReader.h" 36 #include "llvm/Linker/Linker.h" 37 #include "llvm/Support/TargetRegistry.h" 38 #include "llvm/Support/TargetSelect.h" 39 #include "llvm/Target/TargetMachine.h" 40 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 41 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 42 43 #include "isl/union_map.h" 44 45 extern "C" { 46 #include "ppcg/cuda.h" 47 #include "ppcg/gpu.h" 48 #include "ppcg/gpu_print.h" 49 #include "ppcg/ppcg.h" 50 #include "ppcg/schedule.h" 51 } 52 53 #include "llvm/Support/Debug.h" 54 55 using namespace polly; 56 using namespace llvm; 57 58 #define DEBUG_TYPE "polly-codegen-ppcg" 59 60 static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule", 61 cl::desc("Dump the computed GPU Schedule"), 62 cl::Hidden, cl::init(false), cl::ZeroOrMore, 63 cl::cat(PollyCategory)); 64 65 static cl::opt<bool> 66 DumpCode("polly-acc-dump-code", 67 cl::desc("Dump C code describing the GPU mapping"), cl::Hidden, 68 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 69 70 static cl::opt<bool> DumpKernelIR("polly-acc-dump-kernel-ir", 71 cl::desc("Dump the kernel LLVM-IR"), 72 cl::Hidden, cl::init(false), cl::ZeroOrMore, 73 cl::cat(PollyCategory)); 74 75 static cl::opt<bool> DumpKernelASM("polly-acc-dump-kernel-asm", 76 cl::desc("Dump the kernel assembly code"), 77 cl::Hidden, cl::init(false), cl::ZeroOrMore, 78 cl::cat(PollyCategory)); 79 80 static cl::opt<bool> FastMath("polly-acc-fastmath", 81 cl::desc("Allow unsafe math optimizations"), 82 cl::Hidden, cl::init(false), cl::ZeroOrMore, 83 cl::cat(PollyCategory)); 84 static cl::opt<bool> SharedMemory("polly-acc-use-shared", 85 cl::desc("Use shared memory"), cl::Hidden, 86 cl::init(false), cl::ZeroOrMore, 87 cl::cat(PollyCategory)); 88 static cl::opt<bool> PrivateMemory("polly-acc-use-private", 89 cl::desc("Use private memory"), cl::Hidden, 90 cl::init(false), cl::ZeroOrMore, 91 cl::cat(PollyCategory)); 92 93 bool polly::PollyManagedMemory; 94 static cl::opt<bool, true> 95 XManagedMemory("polly-acc-codegen-managed-memory", 96 cl::desc("Generate Host kernel code assuming" 97 " that all memory has been" 98 " declared as managed memory"), 99 cl::location(PollyManagedMemory), cl::Hidden, 100 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 101 102 static cl::opt<bool> 103 FailOnVerifyModuleFailure("polly-acc-fail-on-verify-module-failure", 104 cl::desc("Fail and generate a backtrace if" 105 " verifyModule fails on the GPU " 106 " kernel module."), 107 cl::Hidden, cl::init(false), cl::ZeroOrMore, 108 cl::cat(PollyCategory)); 109 110 static cl::opt<std::string> CUDALibDevice( 111 "polly-acc-libdevice", cl::desc("Path to CUDA libdevice"), cl::Hidden, 112 cl::init("/usr/local/cuda/nvvm/libdevice/libdevice.compute_20.10.ll"), 113 cl::ZeroOrMore, cl::cat(PollyCategory)); 114 115 static cl::opt<std::string> 116 CudaVersion("polly-acc-cuda-version", 117 cl::desc("The CUDA version to compile for"), cl::Hidden, 118 cl::init("sm_30"), cl::ZeroOrMore, cl::cat(PollyCategory)); 119 120 static cl::opt<int> 121 MinCompute("polly-acc-mincompute", 122 cl::desc("Minimal number of compute statements to run on GPU."), 123 cl::Hidden, cl::init(10 * 512 * 512)); 124 125 /// Return a unique name for a Scop, which is the scop region with the 126 /// function name. 127 std::string getUniqueScopName(const Scop *S) { 128 return "Scop Region: " + S->getNameStr() + 129 " | Function: " + std::string(S->getFunction().getName()); 130 } 131 132 /// Used to store information PPCG wants for kills. This information is 133 /// used by live range reordering. 134 /// 135 /// @see computeLiveRangeReordering 136 /// @see GPUNodeBuilder::createPPCGScop 137 /// @see GPUNodeBuilder::createPPCGProg 138 struct MustKillsInfo { 139 /// Collection of all kill statements that will be sequenced at the end of 140 /// PPCGScop->schedule. 141 /// 142 /// The nodes in `KillsSchedule` will be merged using `isl_schedule_set` 143 /// which merges schedules in *arbitrary* order. 144 /// (we don't care about the order of the kills anyway). 145 isl::schedule KillsSchedule; 146 /// Map from kill statement instances to scalars that need to be 147 /// killed. 148 /// 149 /// We currently derive kill information for: 150 /// 1. phi nodes. PHI nodes are not alive outside the scop and can 151 /// consequently all be killed. 152 /// 2. Scalar arrays that are not used outside the Scop. This is 153 /// checked by `isScalarUsesContainedInScop`. 154 /// [params] -> { [Stmt_phantom[] -> ref_phantom[]] -> scalar_to_kill[] } 155 isl::union_map TaggedMustKills; 156 157 /// Tagged must kills stripped of the tags. 158 /// [params] -> { Stmt_phantom[] -> scalar_to_kill[] } 159 isl::union_map MustKills; 160 161 MustKillsInfo() : KillsSchedule(nullptr) {} 162 }; 163 164 /// Check if SAI's uses are entirely contained within Scop S. 165 /// If a scalar is used only with a Scop, we are free to kill it, as no data 166 /// can flow in/out of the value any more. 167 /// @see computeMustKillsInfo 168 static bool isScalarUsesContainedInScop(const Scop &S, 169 const ScopArrayInfo *SAI) { 170 assert(SAI->isValueKind() && "this function only deals with scalars." 171 " Dealing with arrays required alias analysis"); 172 173 const Region &R = S.getRegion(); 174 for (User *U : SAI->getBasePtr()->users()) { 175 Instruction *I = dyn_cast<Instruction>(U); 176 assert(I && "invalid user of scop array info"); 177 if (!R.contains(I)) 178 return false; 179 } 180 return true; 181 } 182 183 /// Compute must-kills needed to enable live range reordering with PPCG. 184 /// 185 /// @params S The Scop to compute live range reordering information 186 /// @returns live range reordering information that can be used to setup 187 /// PPCG. 188 static MustKillsInfo computeMustKillsInfo(const Scop &S) { 189 const isl::space ParamSpace = S.getParamSpace(); 190 MustKillsInfo Info; 191 192 // 1. Collect all ScopArrayInfo that satisfy *any* of the criteria: 193 // 1.1 phi nodes in scop. 194 // 1.2 scalars that are only used within the scop 195 SmallVector<isl::id, 4> KillMemIds; 196 for (ScopArrayInfo *SAI : S.arrays()) { 197 if (SAI->isPHIKind() || 198 (SAI->isValueKind() && isScalarUsesContainedInScop(S, SAI))) 199 KillMemIds.push_back(isl::manage(SAI->getBasePtrId().release())); 200 } 201 202 Info.TaggedMustKills = isl::union_map::empty(ParamSpace); 203 Info.MustKills = isl::union_map::empty(ParamSpace); 204 205 // Initialising KillsSchedule to `isl_set_empty` creates an empty node in the 206 // schedule: 207 // - filter: "[control] -> { }" 208 // So, we choose to not create this to keep the output a little nicer, 209 // at the cost of some code complexity. 210 Info.KillsSchedule = nullptr; 211 212 for (isl::id &ToKillId : KillMemIds) { 213 isl::id KillStmtId = isl::id::alloc( 214 S.getIslCtx(), 215 std::string("SKill_phantom_").append(ToKillId.get_name()), nullptr); 216 217 // NOTE: construction of tagged_must_kill: 218 // 2. We need to construct a map: 219 // [param] -> { [Stmt_phantom[] -> ref_phantom[]] -> scalar_to_kill[] } 220 // To construct this, we use `isl_map_domain_product` on 2 maps`: 221 // 2a. StmtToScalar: 222 // [param] -> { Stmt_phantom[] -> scalar_to_kill[] } 223 // 2b. PhantomRefToScalar: 224 // [param] -> { ref_phantom[] -> scalar_to_kill[] } 225 // 226 // Combining these with `isl_map_domain_product` gives us 227 // TaggedMustKill: 228 // [param] -> { [Stmt[] -> phantom_ref[]] -> scalar_to_kill[] } 229 230 // 2a. [param] -> { Stmt[] -> scalar_to_kill[] } 231 isl::map StmtToScalar = isl::map::universe(ParamSpace); 232 StmtToScalar = StmtToScalar.set_tuple_id(isl::dim::in, isl::id(KillStmtId)); 233 StmtToScalar = StmtToScalar.set_tuple_id(isl::dim::out, isl::id(ToKillId)); 234 235 isl::id PhantomRefId = isl::id::alloc( 236 S.getIslCtx(), std::string("ref_phantom") + ToKillId.get_name(), 237 nullptr); 238 239 // 2b. [param] -> { phantom_ref[] -> scalar_to_kill[] } 240 isl::map PhantomRefToScalar = isl::map::universe(ParamSpace); 241 PhantomRefToScalar = 242 PhantomRefToScalar.set_tuple_id(isl::dim::in, PhantomRefId); 243 PhantomRefToScalar = 244 PhantomRefToScalar.set_tuple_id(isl::dim::out, ToKillId); 245 246 // 2. [param] -> { [Stmt[] -> phantom_ref[]] -> scalar_to_kill[] } 247 isl::map TaggedMustKill = StmtToScalar.domain_product(PhantomRefToScalar); 248 Info.TaggedMustKills = Info.TaggedMustKills.unite(TaggedMustKill); 249 250 // 2. [param] -> { Stmt[] -> scalar_to_kill[] } 251 Info.MustKills = Info.TaggedMustKills.domain_factor_domain(); 252 253 // 3. Create the kill schedule of the form: 254 // "[param] -> { Stmt_phantom[] }" 255 // Then add this to Info.KillsSchedule. 256 isl::space KillStmtSpace = ParamSpace; 257 KillStmtSpace = KillStmtSpace.set_tuple_id(isl::dim::set, KillStmtId); 258 isl::union_set KillStmtDomain = isl::set::universe(KillStmtSpace); 259 260 isl::schedule KillSchedule = isl::schedule::from_domain(KillStmtDomain); 261 if (Info.KillsSchedule) 262 Info.KillsSchedule = Info.KillsSchedule.set(KillSchedule); 263 else 264 Info.KillsSchedule = KillSchedule; 265 } 266 267 return Info; 268 } 269 270 /// Create the ast expressions for a ScopStmt. 271 /// 272 /// This function is a callback for to generate the ast expressions for each 273 /// of the scheduled ScopStmts. 274 static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt( 275 void *StmtT, __isl_take isl_ast_build *Build_C, 276 isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA, 277 isl_id *Id, void *User), 278 void *UserIndex, 279 isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User), 280 void *UserExpr) { 281 282 ScopStmt *Stmt = (ScopStmt *)StmtT; 283 284 if (!Stmt || !Build_C) 285 return NULL; 286 287 isl::ast_build Build = isl::manage(isl_ast_build_copy(Build_C)); 288 isl::ctx Ctx = Build.get_ctx(); 289 isl::id_to_ast_expr RefToExpr = isl::id_to_ast_expr::alloc(Ctx, 0); 290 291 Stmt->setAstBuild(Build); 292 293 for (MemoryAccess *Acc : *Stmt) { 294 isl::map AddrFunc = Acc->getAddressFunction(); 295 AddrFunc = AddrFunc.intersect_domain(Stmt->getDomain()); 296 297 isl::id RefId = Acc->getId(); 298 isl::pw_multi_aff PMA = isl::pw_multi_aff::from_map(AddrFunc); 299 300 isl::multi_pw_aff MPA = isl::multi_pw_aff(PMA); 301 MPA = MPA.coalesce(); 302 MPA = isl::manage(FunctionIndex(MPA.release(), RefId.get(), UserIndex)); 303 304 isl::ast_expr Access = Build.access_from(MPA); 305 Access = isl::manage(FunctionExpr(Access.release(), RefId.get(), UserExpr)); 306 RefToExpr = RefToExpr.set(RefId, Access); 307 } 308 309 return RefToExpr.release(); 310 } 311 312 /// Given a LLVM Type, compute its size in bytes, 313 static int computeSizeInBytes(const Type *T) { 314 int bytes = T->getPrimitiveSizeInBits() / 8; 315 if (bytes == 0) 316 bytes = T->getScalarSizeInBits() / 8; 317 return bytes; 318 } 319 320 /// Generate code for a GPU specific isl AST. 321 /// 322 /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which 323 /// generates code for general-purpose AST nodes, with special functionality 324 /// for generating GPU specific user nodes. 325 /// 326 /// @see GPUNodeBuilder::createUser 327 class GPUNodeBuilder : public IslNodeBuilder { 328 public: 329 GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, 330 const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE, 331 DominatorTree &DT, Scop &S, BasicBlock *StartBlock, 332 gpu_prog *Prog, GPURuntime Runtime, GPUArch Arch) 333 : IslNodeBuilder(Builder, Annotator, DL, LI, SE, DT, S, StartBlock), 334 Prog(Prog), Runtime(Runtime), Arch(Arch) { 335 getExprBuilder().setIDToSAI(&IDToSAI); 336 } 337 338 /// Create after-run-time-check initialization code. 339 void initializeAfterRTH(); 340 341 /// Finalize the generated scop. 342 virtual void finalize(); 343 344 /// Track if the full build process was successful. 345 /// 346 /// This value is set to false, if throughout the build process an error 347 /// occurred which prevents us from generating valid GPU code. 348 bool BuildSuccessful = true; 349 350 /// The maximal number of loops surrounding a sequential kernel. 351 unsigned DeepestSequential = 0; 352 353 /// The maximal number of loops surrounding a parallel kernel. 354 unsigned DeepestParallel = 0; 355 356 /// Return the name to set for the ptx_kernel. 357 std::string getKernelFuncName(int Kernel_id); 358 359 private: 360 /// A vector of array base pointers for which a new ScopArrayInfo was created. 361 /// 362 /// This vector is used to delete the ScopArrayInfo when it is not needed any 363 /// more. 364 std::vector<Value *> LocalArrays; 365 366 /// A map from ScopArrays to their corresponding device allocations. 367 std::map<ScopArrayInfo *, Value *> DeviceAllocations; 368 369 /// The current GPU context. 370 Value *GPUContext; 371 372 /// The set of isl_ids allocated in the kernel 373 std::vector<isl_id *> KernelIds; 374 375 /// A module containing GPU code. 376 /// 377 /// This pointer is only set in case we are currently generating GPU code. 378 std::unique_ptr<Module> GPUModule; 379 380 /// The GPU program we generate code for. 381 gpu_prog *Prog; 382 383 /// The GPU Runtime implementation to use (OpenCL or CUDA). 384 GPURuntime Runtime; 385 386 /// The GPU Architecture to target. 387 GPUArch Arch; 388 389 /// Class to free isl_ids. 390 class IslIdDeleter { 391 public: 392 void operator()(__isl_take isl_id *Id) { isl_id_free(Id); }; 393 }; 394 395 /// A set containing all isl_ids allocated in a GPU kernel. 396 /// 397 /// By releasing this set all isl_ids will be freed. 398 std::set<std::unique_ptr<isl_id, IslIdDeleter>> KernelIDs; 399 400 IslExprBuilder::IDToScopArrayInfoTy IDToSAI; 401 402 /// Create code for user-defined AST nodes. 403 /// 404 /// These AST nodes can be of type: 405 /// 406 /// - ScopStmt: A computational statement (TODO) 407 /// - Kernel: A GPU kernel call (TODO) 408 /// - Data-Transfer: A GPU <-> CPU data-transfer 409 /// - In-kernel synchronization 410 /// - In-kernel memory copy statement 411 /// 412 /// @param UserStmt The ast node to generate code for. 413 virtual void createUser(__isl_take isl_ast_node *UserStmt); 414 415 enum DataDirection { HOST_TO_DEVICE, DEVICE_TO_HOST }; 416 417 /// Create code for a data transfer statement 418 /// 419 /// @param TransferStmt The data transfer statement. 420 /// @param Direction The direction in which to transfer data. 421 void createDataTransfer(__isl_take isl_ast_node *TransferStmt, 422 enum DataDirection Direction); 423 424 /// Find llvm::Values referenced in GPU kernel. 425 /// 426 /// @param Kernel The kernel to scan for llvm::Values 427 /// 428 /// @returns A tuple, whose: 429 /// - First element contains the set of values referenced by the 430 /// kernel 431 /// - Second element contains the set of functions referenced by the 432 /// kernel. All functions in the set satisfy 433 /// `isValidFunctionInKernel`. 434 /// - Third element contains loops that have induction variables 435 /// which are used in the kernel, *and* these loops are *neither* 436 /// in the scop, nor do they immediately surroung the Scop. 437 /// See [Code generation of induction variables of loops outside 438 /// Scops] 439 std::tuple<SetVector<Value *>, SetVector<Function *>, SetVector<const Loop *>, 440 isl::space> 441 getReferencesInKernel(ppcg_kernel *Kernel); 442 443 /// Compute the sizes of the execution grid for a given kernel. 444 /// 445 /// @param Kernel The kernel to compute grid sizes for. 446 /// 447 /// @returns A tuple with grid sizes for X and Y dimension 448 std::tuple<Value *, Value *> getGridSizes(ppcg_kernel *Kernel); 449 450 /// Get the managed array pointer for sending host pointers to the device. 451 /// \note 452 /// This is to be used only with managed memory 453 Value *getManagedDeviceArray(gpu_array_info *Array, ScopArrayInfo *ArrayInfo); 454 455 /// Compute the sizes of the thread blocks for a given kernel. 456 /// 457 /// @param Kernel The kernel to compute thread block sizes for. 458 /// 459 /// @returns A tuple with thread block sizes for X, Y, and Z dimensions. 460 std::tuple<Value *, Value *, Value *> getBlockSizes(ppcg_kernel *Kernel); 461 462 /// Store a specific kernel launch parameter in the array of kernel launch 463 /// parameters. 464 /// 465 /// @param Parameters The list of parameters in which to store. 466 /// @param Param The kernel launch parameter to store. 467 /// @param Index The index in the parameter list, at which to store the 468 /// parameter. 469 void insertStoreParameter(Instruction *Parameters, Instruction *Param, 470 int Index); 471 472 /// Create kernel launch parameters. 473 /// 474 /// @param Kernel The kernel to create parameters for. 475 /// @param F The kernel function that has been created. 476 /// @param SubtreeValues The set of llvm::Values referenced by this kernel. 477 /// 478 /// @returns A stack allocated array with pointers to the parameter 479 /// values that are passed to the kernel. 480 Value *createLaunchParameters(ppcg_kernel *Kernel, Function *F, 481 SetVector<Value *> SubtreeValues); 482 483 /// Create declarations for kernel variable. 484 /// 485 /// This includes shared memory declarations. 486 /// 487 /// @param Kernel The kernel definition to create variables for. 488 /// @param FN The function into which to generate the variables. 489 void createKernelVariables(ppcg_kernel *Kernel, Function *FN); 490 491 /// Add CUDA annotations to module. 492 /// 493 /// Add a set of CUDA annotations that declares the maximal block dimensions 494 /// that will be used to execute the CUDA kernel. This allows the NVIDIA 495 /// PTX compiler to bound the number of allocated registers to ensure the 496 /// resulting kernel is known to run with up to as many block dimensions 497 /// as specified here. 498 /// 499 /// @param M The module to add the annotations to. 500 /// @param BlockDimX The size of block dimension X. 501 /// @param BlockDimY The size of block dimension Y. 502 /// @param BlockDimZ The size of block dimension Z. 503 void addCUDAAnnotations(Module *M, Value *BlockDimX, Value *BlockDimY, 504 Value *BlockDimZ); 505 506 /// Create GPU kernel. 507 /// 508 /// Code generate the kernel described by @p KernelStmt. 509 /// 510 /// @param KernelStmt The ast node to generate kernel code for. 511 void createKernel(__isl_take isl_ast_node *KernelStmt); 512 513 /// Generate code that computes the size of an array. 514 /// 515 /// @param Array The array for which to compute a size. 516 Value *getArraySize(gpu_array_info *Array); 517 518 /// Generate code to compute the minimal offset at which an array is accessed. 519 /// 520 /// The offset of an array is the minimal array location accessed in a scop. 521 /// 522 /// Example: 523 /// 524 /// for (long i = 0; i < 100; i++) 525 /// A[i + 42] += ... 526 /// 527 /// getArrayOffset(A) results in 42. 528 /// 529 /// @param Array The array for which to compute the offset. 530 /// @returns An llvm::Value that contains the offset of the array. 531 Value *getArrayOffset(gpu_array_info *Array); 532 533 /// Prepare the kernel arguments for kernel code generation 534 /// 535 /// @param Kernel The kernel to generate code for. 536 /// @param FN The function created for the kernel. 537 void prepareKernelArguments(ppcg_kernel *Kernel, Function *FN); 538 539 /// Create kernel function. 540 /// 541 /// Create a kernel function located in a newly created module that can serve 542 /// as target for device code generation. Set the Builder to point to the 543 /// start block of this newly created function. 544 /// 545 /// @param Kernel The kernel to generate code for. 546 /// @param SubtreeValues The set of llvm::Values referenced by this kernel. 547 /// @param SubtreeFunctions The set of llvm::Functions referenced by this 548 /// kernel. 549 void createKernelFunction(ppcg_kernel *Kernel, 550 SetVector<Value *> &SubtreeValues, 551 SetVector<Function *> &SubtreeFunctions); 552 553 /// Create the declaration of a kernel function. 554 /// 555 /// The kernel function takes as arguments: 556 /// 557 /// - One i8 pointer for each external array reference used in the kernel. 558 /// - Host iterators 559 /// - Parameters 560 /// - Other LLVM Value references (TODO) 561 /// 562 /// @param Kernel The kernel to generate the function declaration for. 563 /// @param SubtreeValues The set of llvm::Values referenced by this kernel. 564 /// 565 /// @returns The newly declared function. 566 Function *createKernelFunctionDecl(ppcg_kernel *Kernel, 567 SetVector<Value *> &SubtreeValues); 568 569 /// Insert intrinsic functions to obtain thread and block ids. 570 /// 571 /// @param The kernel to generate the intrinsic functions for. 572 void insertKernelIntrinsics(ppcg_kernel *Kernel); 573 574 /// Insert function calls to retrieve the SPIR group/local ids. 575 /// 576 /// @param The kernel to generate the function calls for. 577 void insertKernelCallsSPIR(ppcg_kernel *Kernel); 578 579 /// Setup the creation of functions referenced by the GPU kernel. 580 /// 581 /// 1. Create new function declarations in GPUModule which are the same as 582 /// SubtreeFunctions. 583 /// 584 /// 2. Populate IslNodeBuilder::ValueMap with mappings from 585 /// old functions (that come from the original module) to new functions 586 /// (that are created within GPUModule). That way, we generate references 587 /// to the correct function (in GPUModule) in BlockGenerator. 588 /// 589 /// @see IslNodeBuilder::ValueMap 590 /// @see BlockGenerator::GlobalMap 591 /// @see BlockGenerator::getNewValue 592 /// @see GPUNodeBuilder::getReferencesInKernel. 593 /// 594 /// @param SubtreeFunctions The set of llvm::Functions referenced by 595 /// this kernel. 596 void setupKernelSubtreeFunctions(SetVector<Function *> SubtreeFunctions); 597 598 /// Create a global-to-shared or shared-to-global copy statement. 599 /// 600 /// @param CopyStmt The copy statement to generate code for 601 void createKernelCopy(ppcg_kernel_stmt *CopyStmt); 602 603 /// Create code for a ScopStmt called in @p Expr. 604 /// 605 /// @param Expr The expression containing the call. 606 /// @param KernelStmt The kernel statement referenced in the call. 607 void createScopStmt(isl_ast_expr *Expr, ppcg_kernel_stmt *KernelStmt); 608 609 /// Create an in-kernel synchronization call. 610 void createKernelSync(); 611 612 /// Create a PTX assembly string for the current GPU kernel. 613 /// 614 /// @returns A string containing the corresponding PTX assembly code. 615 std::string createKernelASM(); 616 617 /// Remove references from the dominator tree to the kernel function @p F. 618 /// 619 /// @param F The function to remove references to. 620 void clearDominators(Function *F); 621 622 /// Remove references from scalar evolution to the kernel function @p F. 623 /// 624 /// @param F The function to remove references to. 625 void clearScalarEvolution(Function *F); 626 627 /// Remove references from loop info to the kernel function @p F. 628 /// 629 /// @param F The function to remove references to. 630 void clearLoops(Function *F); 631 632 /// Check if the scop requires to be linked with CUDA's libdevice. 633 bool requiresCUDALibDevice(); 634 635 /// Link with the NVIDIA libdevice library (if needed and available). 636 void addCUDALibDevice(); 637 638 /// Finalize the generation of the kernel function. 639 /// 640 /// Free the LLVM-IR module corresponding to the kernel and -- if requested -- 641 /// dump its IR to stderr. 642 /// 643 /// @returns The Assembly string of the kernel. 644 std::string finalizeKernelFunction(); 645 646 /// Finalize the generation of the kernel arguments. 647 /// 648 /// This function ensures that not-read-only scalars used in a kernel are 649 /// stored back to the global memory location they are backed with before 650 /// the kernel terminates. 651 /// 652 /// @params Kernel The kernel to finalize kernel arguments for. 653 void finalizeKernelArguments(ppcg_kernel *Kernel); 654 655 /// Create code that allocates memory to store arrays on device. 656 void allocateDeviceArrays(); 657 658 /// Create code to prepare the managed device pointers. 659 void prepareManagedDeviceArrays(); 660 661 /// Free all allocated device arrays. 662 void freeDeviceArrays(); 663 664 /// Create a call to initialize the GPU context. 665 /// 666 /// @returns A pointer to the newly initialized context. 667 Value *createCallInitContext(); 668 669 /// Create a call to get the device pointer for a kernel allocation. 670 /// 671 /// @param Allocation The Polly GPU allocation 672 /// 673 /// @returns The device parameter corresponding to this allocation. 674 Value *createCallGetDevicePtr(Value *Allocation); 675 676 /// Create a call to free the GPU context. 677 /// 678 /// @param Context A pointer to an initialized GPU context. 679 void createCallFreeContext(Value *Context); 680 681 /// Create a call to allocate memory on the device. 682 /// 683 /// @param Size The size of memory to allocate 684 /// 685 /// @returns A pointer that identifies this allocation. 686 Value *createCallAllocateMemoryForDevice(Value *Size); 687 688 /// Create a call to free a device array. 689 /// 690 /// @param Array The device array to free. 691 void createCallFreeDeviceMemory(Value *Array); 692 693 /// Create a call to copy data from host to device. 694 /// 695 /// @param HostPtr A pointer to the host data that should be copied. 696 /// @param DevicePtr A device pointer specifying the location to copy to. 697 void createCallCopyFromHostToDevice(Value *HostPtr, Value *DevicePtr, 698 Value *Size); 699 700 /// Create a call to copy data from device to host. 701 /// 702 /// @param DevicePtr A pointer to the device data that should be copied. 703 /// @param HostPtr A host pointer specifying the location to copy to. 704 void createCallCopyFromDeviceToHost(Value *DevicePtr, Value *HostPtr, 705 Value *Size); 706 707 /// Create a call to synchronize Host & Device. 708 /// \note 709 /// This is to be used only with managed memory. 710 void createCallSynchronizeDevice(); 711 712 /// Create a call to get a kernel from an assembly string. 713 /// 714 /// @param Buffer The string describing the kernel. 715 /// @param Entry The name of the kernel function to call. 716 /// 717 /// @returns A pointer to a kernel object 718 Value *createCallGetKernel(Value *Buffer, Value *Entry); 719 720 /// Create a call to free a GPU kernel. 721 /// 722 /// @param GPUKernel THe kernel to free. 723 void createCallFreeKernel(Value *GPUKernel); 724 725 /// Create a call to launch a GPU kernel. 726 /// 727 /// @param GPUKernel The kernel to launch. 728 /// @param GridDimX The size of the first grid dimension. 729 /// @param GridDimY The size of the second grid dimension. 730 /// @param GridBlockX The size of the first block dimension. 731 /// @param GridBlockY The size of the second block dimension. 732 /// @param GridBlockZ The size of the third block dimension. 733 /// @param Parameters A pointer to an array that contains itself pointers to 734 /// the parameter values passed for each kernel argument. 735 void createCallLaunchKernel(Value *GPUKernel, Value *GridDimX, 736 Value *GridDimY, Value *BlockDimX, 737 Value *BlockDimY, Value *BlockDimZ, 738 Value *Parameters); 739 }; 740 741 std::string GPUNodeBuilder::getKernelFuncName(int Kernel_id) { 742 return "FUNC_" + S.getFunction().getName().str() + "_SCOP_" + 743 std::to_string(S.getID()) + "_KERNEL_" + std::to_string(Kernel_id); 744 } 745 746 void GPUNodeBuilder::initializeAfterRTH() { 747 BasicBlock *NewBB = SplitBlock(Builder.GetInsertBlock(), 748 &*Builder.GetInsertPoint(), &DT, &LI); 749 NewBB->setName("polly.acc.initialize"); 750 Builder.SetInsertPoint(&NewBB->front()); 751 752 GPUContext = createCallInitContext(); 753 754 if (!PollyManagedMemory) 755 allocateDeviceArrays(); 756 else 757 prepareManagedDeviceArrays(); 758 } 759 760 void GPUNodeBuilder::finalize() { 761 if (!PollyManagedMemory) 762 freeDeviceArrays(); 763 764 createCallFreeContext(GPUContext); 765 IslNodeBuilder::finalize(); 766 } 767 768 void GPUNodeBuilder::allocateDeviceArrays() { 769 assert(!PollyManagedMemory && 770 "Managed memory will directly send host pointers " 771 "to the kernel. There is no need for device arrays"); 772 isl_ast_build *Build = isl_ast_build_from_context(S.getContext().release()); 773 774 for (int i = 0; i < Prog->n_array; ++i) { 775 gpu_array_info *Array = &Prog->array[i]; 776 auto *ScopArray = (ScopArrayInfo *)Array->user; 777 std::string DevArrayName("p_dev_array_"); 778 DevArrayName.append(Array->name); 779 780 Value *ArraySize = getArraySize(Array); 781 Value *Offset = getArrayOffset(Array); 782 if (Offset) 783 ArraySize = Builder.CreateSub( 784 ArraySize, 785 Builder.CreateMul(Offset, 786 Builder.getInt64(ScopArray->getElemSizeInBytes()))); 787 const SCEV *SizeSCEV = SE.getSCEV(ArraySize); 788 // It makes no sense to have an array of size 0. The CUDA API will 789 // throw an error anyway if we invoke `cuMallocManaged` with size `0`. We 790 // choose to be defensive and catch this at the compile phase. It is 791 // most likely that we are doing something wrong with size computation. 792 if (SizeSCEV->isZero()) { 793 errs() << getUniqueScopName(&S) 794 << " has computed array size 0: " << *ArraySize 795 << " | for array: " << *(ScopArray->getBasePtr()) 796 << ". This is illegal, exiting.\n"; 797 report_fatal_error("array size was computed to be 0"); 798 } 799 800 Value *DevArray = createCallAllocateMemoryForDevice(ArraySize); 801 DevArray->setName(DevArrayName); 802 DeviceAllocations[ScopArray] = DevArray; 803 } 804 805 isl_ast_build_free(Build); 806 } 807 808 void GPUNodeBuilder::prepareManagedDeviceArrays() { 809 assert(PollyManagedMemory && 810 "Device array most only be prepared in managed-memory mode"); 811 for (int i = 0; i < Prog->n_array; ++i) { 812 gpu_array_info *Array = &Prog->array[i]; 813 ScopArrayInfo *ScopArray = (ScopArrayInfo *)Array->user; 814 Value *HostPtr; 815 816 if (gpu_array_is_scalar(Array)) 817 HostPtr = BlockGen.getOrCreateAlloca(ScopArray); 818 else 819 HostPtr = ScopArray->getBasePtr(); 820 HostPtr = getLatestValue(HostPtr); 821 822 Value *Offset = getArrayOffset(Array); 823 if (Offset) { 824 HostPtr = Builder.CreatePointerCast( 825 HostPtr, ScopArray->getElementType()->getPointerTo()); 826 HostPtr = Builder.CreateGEP(HostPtr, Offset); 827 } 828 829 HostPtr = Builder.CreatePointerCast(HostPtr, Builder.getInt8PtrTy()); 830 DeviceAllocations[ScopArray] = HostPtr; 831 } 832 } 833 834 void GPUNodeBuilder::addCUDAAnnotations(Module *M, Value *BlockDimX, 835 Value *BlockDimY, Value *BlockDimZ) { 836 auto AnnotationNode = M->getOrInsertNamedMetadata("nvvm.annotations"); 837 838 for (auto &F : *M) { 839 if (F.getCallingConv() != CallingConv::PTX_Kernel) 840 continue; 841 842 Value *V[] = {BlockDimX, BlockDimY, BlockDimZ}; 843 844 Metadata *Elements[] = { 845 ValueAsMetadata::get(&F), MDString::get(M->getContext(), "maxntidx"), 846 ValueAsMetadata::get(V[0]), MDString::get(M->getContext(), "maxntidy"), 847 ValueAsMetadata::get(V[1]), MDString::get(M->getContext(), "maxntidz"), 848 ValueAsMetadata::get(V[2]), 849 }; 850 MDNode *Node = MDNode::get(M->getContext(), Elements); 851 AnnotationNode->addOperand(Node); 852 } 853 } 854 855 void GPUNodeBuilder::freeDeviceArrays() { 856 assert(!PollyManagedMemory && "Managed memory does not use device arrays"); 857 for (auto &Array : DeviceAllocations) 858 createCallFreeDeviceMemory(Array.second); 859 } 860 861 Value *GPUNodeBuilder::createCallGetKernel(Value *Buffer, Value *Entry) { 862 const char *Name = "polly_getKernel"; 863 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 864 Function *F = M->getFunction(Name); 865 866 // If F is not available, declare it. 867 if (!F) { 868 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 869 std::vector<Type *> Args; 870 Args.push_back(Builder.getInt8PtrTy()); 871 Args.push_back(Builder.getInt8PtrTy()); 872 FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false); 873 F = Function::Create(Ty, Linkage, Name, M); 874 } 875 876 return Builder.CreateCall(F, {Buffer, Entry}); 877 } 878 879 Value *GPUNodeBuilder::createCallGetDevicePtr(Value *Allocation) { 880 const char *Name = "polly_getDevicePtr"; 881 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 882 Function *F = M->getFunction(Name); 883 884 // If F is not available, declare it. 885 if (!F) { 886 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 887 std::vector<Type *> Args; 888 Args.push_back(Builder.getInt8PtrTy()); 889 FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false); 890 F = Function::Create(Ty, Linkage, Name, M); 891 } 892 893 return Builder.CreateCall(F, {Allocation}); 894 } 895 896 void GPUNodeBuilder::createCallLaunchKernel(Value *GPUKernel, Value *GridDimX, 897 Value *GridDimY, Value *BlockDimX, 898 Value *BlockDimY, Value *BlockDimZ, 899 Value *Parameters) { 900 const char *Name = "polly_launchKernel"; 901 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 902 Function *F = M->getFunction(Name); 903 904 // If F is not available, declare it. 905 if (!F) { 906 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 907 std::vector<Type *> Args; 908 Args.push_back(Builder.getInt8PtrTy()); 909 Args.push_back(Builder.getInt32Ty()); 910 Args.push_back(Builder.getInt32Ty()); 911 Args.push_back(Builder.getInt32Ty()); 912 Args.push_back(Builder.getInt32Ty()); 913 Args.push_back(Builder.getInt32Ty()); 914 Args.push_back(Builder.getInt8PtrTy()); 915 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 916 F = Function::Create(Ty, Linkage, Name, M); 917 } 918 919 Builder.CreateCall(F, {GPUKernel, GridDimX, GridDimY, BlockDimX, BlockDimY, 920 BlockDimZ, Parameters}); 921 } 922 923 void GPUNodeBuilder::createCallFreeKernel(Value *GPUKernel) { 924 const char *Name = "polly_freeKernel"; 925 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 926 Function *F = M->getFunction(Name); 927 928 // If F is not available, declare it. 929 if (!F) { 930 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 931 std::vector<Type *> Args; 932 Args.push_back(Builder.getInt8PtrTy()); 933 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 934 F = Function::Create(Ty, Linkage, Name, M); 935 } 936 937 Builder.CreateCall(F, {GPUKernel}); 938 } 939 940 void GPUNodeBuilder::createCallFreeDeviceMemory(Value *Array) { 941 assert(!PollyManagedMemory && 942 "Managed memory does not allocate or free memory " 943 "for device"); 944 const char *Name = "polly_freeDeviceMemory"; 945 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 946 Function *F = M->getFunction(Name); 947 948 // If F is not available, declare it. 949 if (!F) { 950 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 951 std::vector<Type *> Args; 952 Args.push_back(Builder.getInt8PtrTy()); 953 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 954 F = Function::Create(Ty, Linkage, Name, M); 955 } 956 957 Builder.CreateCall(F, {Array}); 958 } 959 960 Value *GPUNodeBuilder::createCallAllocateMemoryForDevice(Value *Size) { 961 assert(!PollyManagedMemory && 962 "Managed memory does not allocate or free memory " 963 "for device"); 964 const char *Name = "polly_allocateMemoryForDevice"; 965 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 966 Function *F = M->getFunction(Name); 967 968 // If F is not available, declare it. 969 if (!F) { 970 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 971 std::vector<Type *> Args; 972 Args.push_back(Builder.getInt64Ty()); 973 FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false); 974 F = Function::Create(Ty, Linkage, Name, M); 975 } 976 977 return Builder.CreateCall(F, {Size}); 978 } 979 980 void GPUNodeBuilder::createCallCopyFromHostToDevice(Value *HostData, 981 Value *DeviceData, 982 Value *Size) { 983 assert(!PollyManagedMemory && 984 "Managed memory does not transfer memory between " 985 "device and host"); 986 const char *Name = "polly_copyFromHostToDevice"; 987 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 988 Function *F = M->getFunction(Name); 989 990 // If F is not available, declare it. 991 if (!F) { 992 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 993 std::vector<Type *> Args; 994 Args.push_back(Builder.getInt8PtrTy()); 995 Args.push_back(Builder.getInt8PtrTy()); 996 Args.push_back(Builder.getInt64Ty()); 997 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 998 F = Function::Create(Ty, Linkage, Name, M); 999 } 1000 1001 Builder.CreateCall(F, {HostData, DeviceData, Size}); 1002 } 1003 1004 void GPUNodeBuilder::createCallCopyFromDeviceToHost(Value *DeviceData, 1005 Value *HostData, 1006 Value *Size) { 1007 assert(!PollyManagedMemory && 1008 "Managed memory does not transfer memory between " 1009 "device and host"); 1010 const char *Name = "polly_copyFromDeviceToHost"; 1011 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 1012 Function *F = M->getFunction(Name); 1013 1014 // If F is not available, declare it. 1015 if (!F) { 1016 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 1017 std::vector<Type *> Args; 1018 Args.push_back(Builder.getInt8PtrTy()); 1019 Args.push_back(Builder.getInt8PtrTy()); 1020 Args.push_back(Builder.getInt64Ty()); 1021 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 1022 F = Function::Create(Ty, Linkage, Name, M); 1023 } 1024 1025 Builder.CreateCall(F, {DeviceData, HostData, Size}); 1026 } 1027 1028 void GPUNodeBuilder::createCallSynchronizeDevice() { 1029 assert(PollyManagedMemory && "explicit synchronization is only necessary for " 1030 "managed memory"); 1031 const char *Name = "polly_synchronizeDevice"; 1032 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 1033 Function *F = M->getFunction(Name); 1034 1035 // If F is not available, declare it. 1036 if (!F) { 1037 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 1038 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 1039 F = Function::Create(Ty, Linkage, Name, M); 1040 } 1041 1042 Builder.CreateCall(F); 1043 } 1044 1045 Value *GPUNodeBuilder::createCallInitContext() { 1046 const char *Name; 1047 1048 switch (Runtime) { 1049 case GPURuntime::CUDA: 1050 Name = "polly_initContextCUDA"; 1051 break; 1052 case GPURuntime::OpenCL: 1053 Name = "polly_initContextCL"; 1054 break; 1055 } 1056 1057 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 1058 Function *F = M->getFunction(Name); 1059 1060 // If F is not available, declare it. 1061 if (!F) { 1062 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 1063 std::vector<Type *> Args; 1064 FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false); 1065 F = Function::Create(Ty, Linkage, Name, M); 1066 } 1067 1068 return Builder.CreateCall(F, {}); 1069 } 1070 1071 void GPUNodeBuilder::createCallFreeContext(Value *Context) { 1072 const char *Name = "polly_freeContext"; 1073 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 1074 Function *F = M->getFunction(Name); 1075 1076 // If F is not available, declare it. 1077 if (!F) { 1078 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 1079 std::vector<Type *> Args; 1080 Args.push_back(Builder.getInt8PtrTy()); 1081 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 1082 F = Function::Create(Ty, Linkage, Name, M); 1083 } 1084 1085 Builder.CreateCall(F, {Context}); 1086 } 1087 1088 /// Check if one string is a prefix of another. 1089 /// 1090 /// @param String The string in which to look for the prefix. 1091 /// @param Prefix The prefix to look for. 1092 static bool isPrefix(std::string String, std::string Prefix) { 1093 return String.find(Prefix) == 0; 1094 } 1095 1096 Value *GPUNodeBuilder::getArraySize(gpu_array_info *Array) { 1097 isl::ast_build Build = isl::ast_build::from_context(S.getContext()); 1098 Value *ArraySize = ConstantInt::get(Builder.getInt64Ty(), Array->size); 1099 1100 if (!gpu_array_is_scalar(Array)) { 1101 isl::multi_pw_aff ArrayBound = 1102 isl::manage(isl_multi_pw_aff_copy(Array->bound)); 1103 1104 isl::pw_aff OffsetDimZero = ArrayBound.get_pw_aff(0); 1105 isl::ast_expr Res = Build.expr_from(OffsetDimZero); 1106 1107 for (unsigned int i = 1; i < Array->n_index; i++) { 1108 isl::pw_aff Bound_I = ArrayBound.get_pw_aff(i); 1109 isl::ast_expr Expr = Build.expr_from(Bound_I); 1110 Res = Res.mul(Expr); 1111 } 1112 1113 Value *NumElements = ExprBuilder.create(Res.release()); 1114 if (NumElements->getType() != ArraySize->getType()) 1115 NumElements = Builder.CreateSExt(NumElements, ArraySize->getType()); 1116 ArraySize = Builder.CreateMul(ArraySize, NumElements); 1117 } 1118 return ArraySize; 1119 } 1120 1121 Value *GPUNodeBuilder::getArrayOffset(gpu_array_info *Array) { 1122 if (gpu_array_is_scalar(Array)) 1123 return nullptr; 1124 1125 isl::ast_build Build = isl::ast_build::from_context(S.getContext()); 1126 1127 isl::set Min = isl::manage(isl_set_copy(Array->extent)).lexmin(); 1128 1129 isl::set ZeroSet = isl::set::universe(Min.get_space()); 1130 1131 for (long i = 0; i < Min.dim(isl::dim::set); i++) 1132 ZeroSet = ZeroSet.fix_si(isl::dim::set, i, 0); 1133 1134 if (Min.is_subset(ZeroSet)) { 1135 return nullptr; 1136 } 1137 1138 isl::ast_expr Result = isl::ast_expr::from_val(isl::val(Min.get_ctx(), 0)); 1139 1140 for (long i = 0; i < Min.dim(isl::dim::set); i++) { 1141 if (i > 0) { 1142 isl::pw_aff Bound_I = 1143 isl::manage(isl_multi_pw_aff_get_pw_aff(Array->bound, i - 1)); 1144 isl::ast_expr BExpr = Build.expr_from(Bound_I); 1145 Result = Result.mul(BExpr); 1146 } 1147 isl::pw_aff DimMin = Min.dim_min(i); 1148 isl::ast_expr MExpr = Build.expr_from(DimMin); 1149 Result = Result.add(MExpr); 1150 } 1151 1152 return ExprBuilder.create(Result.release()); 1153 } 1154 1155 Value *GPUNodeBuilder::getManagedDeviceArray(gpu_array_info *Array, 1156 ScopArrayInfo *ArrayInfo) { 1157 assert(PollyManagedMemory && "Only used when you wish to get a host " 1158 "pointer for sending data to the kernel, " 1159 "with managed memory"); 1160 std::map<ScopArrayInfo *, Value *>::iterator it; 1161 it = DeviceAllocations.find(ArrayInfo); 1162 assert(it != DeviceAllocations.end() && 1163 "Device array expected to be available"); 1164 return it->second; 1165 } 1166 1167 void GPUNodeBuilder::createDataTransfer(__isl_take isl_ast_node *TransferStmt, 1168 enum DataDirection Direction) { 1169 assert(!PollyManagedMemory && "Managed memory needs no data transfers"); 1170 isl_ast_expr *Expr = isl_ast_node_user_get_expr(TransferStmt); 1171 isl_ast_expr *Arg = isl_ast_expr_get_op_arg(Expr, 0); 1172 isl_id *Id = isl_ast_expr_get_id(Arg); 1173 auto Array = (gpu_array_info *)isl_id_get_user(Id); 1174 auto ScopArray = (ScopArrayInfo *)(Array->user); 1175 1176 Value *Size = getArraySize(Array); 1177 Value *Offset = getArrayOffset(Array); 1178 Value *DevPtr = DeviceAllocations[ScopArray]; 1179 1180 Value *HostPtr; 1181 1182 if (gpu_array_is_scalar(Array)) 1183 HostPtr = BlockGen.getOrCreateAlloca(ScopArray); 1184 else 1185 HostPtr = ScopArray->getBasePtr(); 1186 HostPtr = getLatestValue(HostPtr); 1187 1188 if (Offset) { 1189 HostPtr = Builder.CreatePointerCast( 1190 HostPtr, ScopArray->getElementType()->getPointerTo()); 1191 HostPtr = Builder.CreateGEP(HostPtr, Offset); 1192 } 1193 1194 HostPtr = Builder.CreatePointerCast(HostPtr, Builder.getInt8PtrTy()); 1195 1196 if (Offset) { 1197 Size = Builder.CreateSub( 1198 Size, Builder.CreateMul( 1199 Offset, Builder.getInt64(ScopArray->getElemSizeInBytes()))); 1200 } 1201 1202 if (Direction == HOST_TO_DEVICE) 1203 createCallCopyFromHostToDevice(HostPtr, DevPtr, Size); 1204 else 1205 createCallCopyFromDeviceToHost(DevPtr, HostPtr, Size); 1206 1207 isl_id_free(Id); 1208 isl_ast_expr_free(Arg); 1209 isl_ast_expr_free(Expr); 1210 isl_ast_node_free(TransferStmt); 1211 } 1212 1213 void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) { 1214 isl_ast_expr *Expr = isl_ast_node_user_get_expr(UserStmt); 1215 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); 1216 isl_id *Id = isl_ast_expr_get_id(StmtExpr); 1217 isl_id_free(Id); 1218 isl_ast_expr_free(StmtExpr); 1219 1220 const char *Str = isl_id_get_name(Id); 1221 if (!strcmp(Str, "kernel")) { 1222 createKernel(UserStmt); 1223 if (PollyManagedMemory) 1224 createCallSynchronizeDevice(); 1225 isl_ast_expr_free(Expr); 1226 return; 1227 } 1228 if (!strcmp(Str, "init_device")) { 1229 initializeAfterRTH(); 1230 isl_ast_node_free(UserStmt); 1231 isl_ast_expr_free(Expr); 1232 return; 1233 } 1234 if (!strcmp(Str, "clear_device")) { 1235 finalize(); 1236 isl_ast_node_free(UserStmt); 1237 isl_ast_expr_free(Expr); 1238 return; 1239 } 1240 if (isPrefix(Str, "to_device")) { 1241 if (!PollyManagedMemory) 1242 createDataTransfer(UserStmt, HOST_TO_DEVICE); 1243 else 1244 isl_ast_node_free(UserStmt); 1245 1246 isl_ast_expr_free(Expr); 1247 return; 1248 } 1249 1250 if (isPrefix(Str, "from_device")) { 1251 if (!PollyManagedMemory) { 1252 createDataTransfer(UserStmt, DEVICE_TO_HOST); 1253 } else { 1254 isl_ast_node_free(UserStmt); 1255 } 1256 isl_ast_expr_free(Expr); 1257 return; 1258 } 1259 1260 isl_id *Anno = isl_ast_node_get_annotation(UserStmt); 1261 struct ppcg_kernel_stmt *KernelStmt = 1262 (struct ppcg_kernel_stmt *)isl_id_get_user(Anno); 1263 isl_id_free(Anno); 1264 1265 switch (KernelStmt->type) { 1266 case ppcg_kernel_domain: 1267 createScopStmt(Expr, KernelStmt); 1268 isl_ast_node_free(UserStmt); 1269 return; 1270 case ppcg_kernel_copy: 1271 createKernelCopy(KernelStmt); 1272 isl_ast_expr_free(Expr); 1273 isl_ast_node_free(UserStmt); 1274 return; 1275 case ppcg_kernel_sync: 1276 createKernelSync(); 1277 isl_ast_expr_free(Expr); 1278 isl_ast_node_free(UserStmt); 1279 return; 1280 } 1281 1282 isl_ast_expr_free(Expr); 1283 isl_ast_node_free(UserStmt); 1284 return; 1285 } 1286 void GPUNodeBuilder::createKernelCopy(ppcg_kernel_stmt *KernelStmt) { 1287 isl_ast_expr *LocalIndex = isl_ast_expr_copy(KernelStmt->u.c.local_index); 1288 LocalIndex = isl_ast_expr_address_of(LocalIndex); 1289 Value *LocalAddr = ExprBuilder.create(LocalIndex); 1290 isl_ast_expr *Index = isl_ast_expr_copy(KernelStmt->u.c.index); 1291 Index = isl_ast_expr_address_of(Index); 1292 Value *GlobalAddr = ExprBuilder.create(Index); 1293 1294 if (KernelStmt->u.c.read) { 1295 LoadInst *Load = Builder.CreateLoad(GlobalAddr, "shared.read"); 1296 Builder.CreateStore(Load, LocalAddr); 1297 } else { 1298 LoadInst *Load = Builder.CreateLoad(LocalAddr, "shared.write"); 1299 Builder.CreateStore(Load, GlobalAddr); 1300 } 1301 } 1302 1303 void GPUNodeBuilder::createScopStmt(isl_ast_expr *Expr, 1304 ppcg_kernel_stmt *KernelStmt) { 1305 auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt; 1306 isl_id_to_ast_expr *Indexes = KernelStmt->u.d.ref2expr; 1307 1308 LoopToScevMapT LTS; 1309 LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end()); 1310 1311 createSubstitutions(Expr, Stmt, LTS); 1312 1313 if (Stmt->isBlockStmt()) 1314 BlockGen.copyStmt(*Stmt, LTS, Indexes); 1315 else 1316 RegionGen.copyStmt(*Stmt, LTS, Indexes); 1317 } 1318 1319 void GPUNodeBuilder::createKernelSync() { 1320 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 1321 const char *SpirName = "__gen_ocl_barrier_global"; 1322 1323 Function *Sync; 1324 1325 switch (Arch) { 1326 case GPUArch::SPIR64: 1327 case GPUArch::SPIR32: 1328 Sync = M->getFunction(SpirName); 1329 1330 // If Sync is not available, declare it. 1331 if (!Sync) { 1332 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 1333 std::vector<Type *> Args; 1334 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false); 1335 Sync = Function::Create(Ty, Linkage, SpirName, M); 1336 Sync->setCallingConv(CallingConv::SPIR_FUNC); 1337 } 1338 break; 1339 case GPUArch::NVPTX64: 1340 Sync = Intrinsic::getDeclaration(M, Intrinsic::nvvm_barrier0); 1341 break; 1342 } 1343 1344 Builder.CreateCall(Sync, {}); 1345 } 1346 1347 /// Collect llvm::Values referenced from @p Node 1348 /// 1349 /// This function only applies to isl_ast_nodes that are user_nodes referring 1350 /// to a ScopStmt. All other node types are ignore. 1351 /// 1352 /// @param Node The node to collect references for. 1353 /// @param User A user pointer used as storage for the data that is collected. 1354 /// 1355 /// @returns isl_bool_true if data could be collected successfully. 1356 isl_bool collectReferencesInGPUStmt(__isl_keep isl_ast_node *Node, void *User) { 1357 if (isl_ast_node_get_type(Node) != isl_ast_node_user) 1358 return isl_bool_true; 1359 1360 isl_ast_expr *Expr = isl_ast_node_user_get_expr(Node); 1361 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); 1362 isl_id *Id = isl_ast_expr_get_id(StmtExpr); 1363 const char *Str = isl_id_get_name(Id); 1364 isl_id_free(Id); 1365 isl_ast_expr_free(StmtExpr); 1366 isl_ast_expr_free(Expr); 1367 1368 if (!isPrefix(Str, "Stmt")) 1369 return isl_bool_true; 1370 1371 Id = isl_ast_node_get_annotation(Node); 1372 auto *KernelStmt = (ppcg_kernel_stmt *)isl_id_get_user(Id); 1373 auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt; 1374 isl_id_free(Id); 1375 1376 addReferencesFromStmt(Stmt, User, false /* CreateScalarRefs */); 1377 1378 return isl_bool_true; 1379 } 1380 1381 /// A list of functions that are available in NVIDIA's libdevice. 1382 const std::set<std::string> CUDALibDeviceFunctions = { 1383 "exp", "expf", "expl", "cos", "cosf", 1384 "sqrt", "sqrtf", "copysign", "copysignf", "copysignl"}; 1385 1386 /// Return the corresponding CUDA libdevice function name for @p F. 1387 /// 1388 /// Return "" if we are not compiling for CUDA. 1389 std::string getCUDALibDeviceFuntion(Function *F) { 1390 if (CUDALibDeviceFunctions.count(F->getName())) 1391 return std::string("__nv_") + std::string(F->getName()); 1392 1393 return ""; 1394 } 1395 1396 /// Check if F is a function that we can code-generate in a GPU kernel. 1397 static bool isValidFunctionInKernel(llvm::Function *F, bool AllowLibDevice) { 1398 assert(F && "F is an invalid pointer"); 1399 // We string compare against the name of the function to allow 1400 // all variants of the intrinsic "llvm.sqrt.*", "llvm.fabs", and 1401 // "llvm.copysign". 1402 const StringRef Name = F->getName(); 1403 1404 if (AllowLibDevice && getCUDALibDeviceFuntion(F).length() > 0) 1405 return true; 1406 1407 return F->isIntrinsic() && 1408 (Name.startswith("llvm.sqrt") || Name.startswith("llvm.fabs") || 1409 Name.startswith("llvm.copysign")); 1410 } 1411 1412 /// Do not take `Function` as a subtree value. 1413 /// 1414 /// We try to take the reference of all subtree values and pass them along 1415 /// to the kernel from the host. Taking an address of any function and 1416 /// trying to pass along is nonsensical. Only allow `Value`s that are not 1417 /// `Function`s. 1418 static bool isValidSubtreeValue(llvm::Value *V) { return !isa<Function>(V); } 1419 1420 /// Return `Function`s from `RawSubtreeValues`. 1421 static SetVector<Function *> 1422 getFunctionsFromRawSubtreeValues(SetVector<Value *> RawSubtreeValues, 1423 bool AllowCUDALibDevice) { 1424 SetVector<Function *> SubtreeFunctions; 1425 for (Value *It : RawSubtreeValues) { 1426 Function *F = dyn_cast<Function>(It); 1427 if (F) { 1428 assert(isValidFunctionInKernel(F, AllowCUDALibDevice) && 1429 "Code should have bailed out by " 1430 "this point if an invalid function " 1431 "were present in a kernel."); 1432 SubtreeFunctions.insert(F); 1433 } 1434 } 1435 return SubtreeFunctions; 1436 } 1437 1438 std::tuple<SetVector<Value *>, SetVector<Function *>, SetVector<const Loop *>, 1439 isl::space> 1440 GPUNodeBuilder::getReferencesInKernel(ppcg_kernel *Kernel) { 1441 SetVector<Value *> SubtreeValues; 1442 SetVector<const SCEV *> SCEVs; 1443 SetVector<const Loop *> Loops; 1444 isl::space ParamSpace = isl::space(S.getIslCtx(), 0, 0).params(); 1445 SubtreeReferences References = { 1446 LI, SE, S, ValueMap, SubtreeValues, SCEVs, getBlockGenerator(), 1447 &ParamSpace}; 1448 1449 for (const auto &I : IDToValue) 1450 SubtreeValues.insert(I.second); 1451 1452 // NOTE: this is populated in IslNodeBuilder::addParameters 1453 // See [Code generation of induction variables of loops outside Scops]. 1454 for (const auto &I : OutsideLoopIterations) 1455 SubtreeValues.insert(cast<SCEVUnknown>(I.second)->getValue()); 1456 1457 isl_ast_node_foreach_descendant_top_down( 1458 Kernel->tree, collectReferencesInGPUStmt, &References); 1459 1460 for (const SCEV *Expr : SCEVs) { 1461 findValues(Expr, SE, SubtreeValues); 1462 findLoops(Expr, Loops); 1463 } 1464 1465 Loops.remove_if([this](const Loop *L) { 1466 return S.contains(L) || L->contains(S.getEntry()); 1467 }); 1468 1469 for (auto &SAI : S.arrays()) 1470 SubtreeValues.remove(SAI->getBasePtr()); 1471 1472 isl_space *Space = S.getParamSpace().release(); 1473 for (long i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { 1474 isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i); 1475 assert(IDToValue.count(Id)); 1476 Value *Val = IDToValue[Id]; 1477 SubtreeValues.remove(Val); 1478 isl_id_free(Id); 1479 } 1480 isl_space_free(Space); 1481 1482 for (long i = 0; i < isl_space_dim(Kernel->space, isl_dim_set); i++) { 1483 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i); 1484 assert(IDToValue.count(Id)); 1485 Value *Val = IDToValue[Id]; 1486 SubtreeValues.remove(Val); 1487 isl_id_free(Id); 1488 } 1489 1490 // Note: { ValidSubtreeValues, ValidSubtreeFunctions } partitions 1491 // SubtreeValues. This is important, because we should not lose any 1492 // SubtreeValues in the process of constructing the 1493 // "ValidSubtree{Values, Functions} sets. Nor should the set 1494 // ValidSubtree{Values, Functions} have any common element. 1495 auto ValidSubtreeValuesIt = 1496 make_filter_range(SubtreeValues, isValidSubtreeValue); 1497 SetVector<Value *> ValidSubtreeValues(ValidSubtreeValuesIt.begin(), 1498 ValidSubtreeValuesIt.end()); 1499 1500 bool AllowCUDALibDevice = Arch == GPUArch::NVPTX64; 1501 1502 SetVector<Function *> ValidSubtreeFunctions( 1503 getFunctionsFromRawSubtreeValues(SubtreeValues, AllowCUDALibDevice)); 1504 1505 // @see IslNodeBuilder::getReferencesInSubtree 1506 SetVector<Value *> ReplacedValues; 1507 for (Value *V : ValidSubtreeValues) { 1508 auto It = ValueMap.find(V); 1509 if (It == ValueMap.end()) 1510 ReplacedValues.insert(V); 1511 else 1512 ReplacedValues.insert(It->second); 1513 } 1514 return std::make_tuple(ReplacedValues, ValidSubtreeFunctions, Loops, 1515 ParamSpace); 1516 } 1517 1518 void GPUNodeBuilder::clearDominators(Function *F) { 1519 DomTreeNode *N = DT.getNode(&F->getEntryBlock()); 1520 std::vector<BasicBlock *> Nodes; 1521 for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I) 1522 Nodes.push_back(I->getBlock()); 1523 1524 for (BasicBlock *BB : Nodes) 1525 DT.eraseNode(BB); 1526 } 1527 1528 void GPUNodeBuilder::clearScalarEvolution(Function *F) { 1529 for (BasicBlock &BB : *F) { 1530 Loop *L = LI.getLoopFor(&BB); 1531 if (L) 1532 SE.forgetLoop(L); 1533 } 1534 } 1535 1536 void GPUNodeBuilder::clearLoops(Function *F) { 1537 for (BasicBlock &BB : *F) { 1538 Loop *L = LI.getLoopFor(&BB); 1539 if (L) 1540 SE.forgetLoop(L); 1541 LI.removeBlock(&BB); 1542 } 1543 } 1544 1545 std::tuple<Value *, Value *> GPUNodeBuilder::getGridSizes(ppcg_kernel *Kernel) { 1546 std::vector<Value *> Sizes; 1547 isl::ast_build Context = isl::ast_build::from_context(S.getContext()); 1548 1549 isl::multi_pw_aff GridSizePwAffs = 1550 isl::manage(isl_multi_pw_aff_copy(Kernel->grid_size)); 1551 for (long i = 0; i < Kernel->n_grid; i++) { 1552 isl::pw_aff Size = GridSizePwAffs.get_pw_aff(i); 1553 isl::ast_expr GridSize = Context.expr_from(Size); 1554 Value *Res = ExprBuilder.create(GridSize.release()); 1555 Res = Builder.CreateTrunc(Res, Builder.getInt32Ty()); 1556 Sizes.push_back(Res); 1557 } 1558 1559 for (long i = Kernel->n_grid; i < 3; i++) 1560 Sizes.push_back(ConstantInt::get(Builder.getInt32Ty(), 1)); 1561 1562 return std::make_tuple(Sizes[0], Sizes[1]); 1563 } 1564 1565 std::tuple<Value *, Value *, Value *> 1566 GPUNodeBuilder::getBlockSizes(ppcg_kernel *Kernel) { 1567 std::vector<Value *> Sizes; 1568 1569 for (long i = 0; i < Kernel->n_block; i++) { 1570 Value *Res = ConstantInt::get(Builder.getInt32Ty(), Kernel->block_dim[i]); 1571 Sizes.push_back(Res); 1572 } 1573 1574 for (long i = Kernel->n_block; i < 3; i++) 1575 Sizes.push_back(ConstantInt::get(Builder.getInt32Ty(), 1)); 1576 1577 return std::make_tuple(Sizes[0], Sizes[1], Sizes[2]); 1578 } 1579 1580 void GPUNodeBuilder::insertStoreParameter(Instruction *Parameters, 1581 Instruction *Param, int Index) { 1582 Value *Slot = Builder.CreateGEP( 1583 Parameters, {Builder.getInt64(0), Builder.getInt64(Index)}); 1584 Value *ParamTyped = Builder.CreatePointerCast(Param, Builder.getInt8PtrTy()); 1585 Builder.CreateStore(ParamTyped, Slot); 1586 } 1587 1588 Value * 1589 GPUNodeBuilder::createLaunchParameters(ppcg_kernel *Kernel, Function *F, 1590 SetVector<Value *> SubtreeValues) { 1591 const int NumArgs = F->arg_size(); 1592 std::vector<int> ArgSizes(NumArgs); 1593 1594 // If we are using the OpenCL Runtime, we need to add the kernel argument 1595 // sizes to the end of the launch-parameter list, so OpenCL can determine 1596 // how big the respective kernel arguments are. 1597 // Here we need to reserve adequate space for that. 1598 Type *ArrayTy; 1599 if (Runtime == GPURuntime::OpenCL) 1600 ArrayTy = ArrayType::get(Builder.getInt8PtrTy(), 2 * NumArgs); 1601 else 1602 ArrayTy = ArrayType::get(Builder.getInt8PtrTy(), NumArgs); 1603 1604 BasicBlock *EntryBlock = 1605 &Builder.GetInsertBlock()->getParent()->getEntryBlock(); 1606 auto AddressSpace = F->getParent()->getDataLayout().getAllocaAddrSpace(); 1607 std::string Launch = "polly_launch_" + std::to_string(Kernel->id); 1608 Instruction *Parameters = new AllocaInst( 1609 ArrayTy, AddressSpace, Launch + "_params", EntryBlock->getTerminator()); 1610 1611 int Index = 0; 1612 for (long i = 0; i < Prog->n_array; i++) { 1613 if (!ppcg_kernel_requires_array_argument(Kernel, i)) 1614 continue; 1615 1616 isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set); 1617 const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(isl::manage(Id)); 1618 1619 if (Runtime == GPURuntime::OpenCL) 1620 ArgSizes[Index] = SAI->getElemSizeInBytes(); 1621 1622 Value *DevArray = nullptr; 1623 if (PollyManagedMemory) { 1624 DevArray = getManagedDeviceArray(&Prog->array[i], 1625 const_cast<ScopArrayInfo *>(SAI)); 1626 } else { 1627 DevArray = DeviceAllocations[const_cast<ScopArrayInfo *>(SAI)]; 1628 DevArray = createCallGetDevicePtr(DevArray); 1629 } 1630 assert(DevArray != nullptr && "Array to be offloaded to device not " 1631 "initialized"); 1632 Value *Offset = getArrayOffset(&Prog->array[i]); 1633 1634 if (Offset) { 1635 DevArray = Builder.CreatePointerCast( 1636 DevArray, SAI->getElementType()->getPointerTo()); 1637 DevArray = Builder.CreateGEP(DevArray, Builder.CreateNeg(Offset)); 1638 DevArray = Builder.CreatePointerCast(DevArray, Builder.getInt8PtrTy()); 1639 } 1640 Value *Slot = Builder.CreateGEP( 1641 Parameters, {Builder.getInt64(0), Builder.getInt64(Index)}); 1642 1643 if (gpu_array_is_read_only_scalar(&Prog->array[i])) { 1644 Value *ValPtr = nullptr; 1645 if (PollyManagedMemory) 1646 ValPtr = DevArray; 1647 else 1648 ValPtr = BlockGen.getOrCreateAlloca(SAI); 1649 1650 assert(ValPtr != nullptr && "ValPtr that should point to a valid object" 1651 " to be stored into Parameters"); 1652 Value *ValPtrCast = 1653 Builder.CreatePointerCast(ValPtr, Builder.getInt8PtrTy()); 1654 Builder.CreateStore(ValPtrCast, Slot); 1655 } else { 1656 Instruction *Param = 1657 new AllocaInst(Builder.getInt8PtrTy(), AddressSpace, 1658 Launch + "_param_" + std::to_string(Index), 1659 EntryBlock->getTerminator()); 1660 Builder.CreateStore(DevArray, Param); 1661 Value *ParamTyped = 1662 Builder.CreatePointerCast(Param, Builder.getInt8PtrTy()); 1663 Builder.CreateStore(ParamTyped, Slot); 1664 } 1665 Index++; 1666 } 1667 1668 int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set); 1669 1670 for (long i = 0; i < NumHostIters; i++) { 1671 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i); 1672 Value *Val = IDToValue[Id]; 1673 isl_id_free(Id); 1674 1675 if (Runtime == GPURuntime::OpenCL) 1676 ArgSizes[Index] = computeSizeInBytes(Val->getType()); 1677 1678 Instruction *Param = 1679 new AllocaInst(Val->getType(), AddressSpace, 1680 Launch + "_param_" + std::to_string(Index), 1681 EntryBlock->getTerminator()); 1682 Builder.CreateStore(Val, Param); 1683 insertStoreParameter(Parameters, Param, Index); 1684 Index++; 1685 } 1686 1687 int NumVars = isl_space_dim(Kernel->space, isl_dim_param); 1688 1689 for (long i = 0; i < NumVars; i++) { 1690 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i); 1691 Value *Val = IDToValue[Id]; 1692 if (ValueMap.count(Val)) 1693 Val = ValueMap[Val]; 1694 isl_id_free(Id); 1695 1696 if (Runtime == GPURuntime::OpenCL) 1697 ArgSizes[Index] = computeSizeInBytes(Val->getType()); 1698 1699 Instruction *Param = 1700 new AllocaInst(Val->getType(), AddressSpace, 1701 Launch + "_param_" + std::to_string(Index), 1702 EntryBlock->getTerminator()); 1703 Builder.CreateStore(Val, Param); 1704 insertStoreParameter(Parameters, Param, Index); 1705 Index++; 1706 } 1707 1708 for (auto Val : SubtreeValues) { 1709 if (Runtime == GPURuntime::OpenCL) 1710 ArgSizes[Index] = computeSizeInBytes(Val->getType()); 1711 1712 Instruction *Param = 1713 new AllocaInst(Val->getType(), AddressSpace, 1714 Launch + "_param_" + std::to_string(Index), 1715 EntryBlock->getTerminator()); 1716 Builder.CreateStore(Val, Param); 1717 insertStoreParameter(Parameters, Param, Index); 1718 Index++; 1719 } 1720 1721 if (Runtime == GPURuntime::OpenCL) { 1722 for (int i = 0; i < NumArgs; i++) { 1723 Value *Val = ConstantInt::get(Builder.getInt32Ty(), ArgSizes[i]); 1724 Instruction *Param = 1725 new AllocaInst(Builder.getInt32Ty(), AddressSpace, 1726 Launch + "_param_size_" + std::to_string(i), 1727 EntryBlock->getTerminator()); 1728 Builder.CreateStore(Val, Param); 1729 insertStoreParameter(Parameters, Param, Index); 1730 Index++; 1731 } 1732 } 1733 1734 auto Location = EntryBlock->getTerminator(); 1735 return new BitCastInst(Parameters, Builder.getInt8PtrTy(), 1736 Launch + "_params_i8ptr", Location); 1737 } 1738 1739 void GPUNodeBuilder::setupKernelSubtreeFunctions( 1740 SetVector<Function *> SubtreeFunctions) { 1741 for (auto Fn : SubtreeFunctions) { 1742 const std::string ClonedFnName = Fn->getName(); 1743 Function *Clone = GPUModule->getFunction(ClonedFnName); 1744 if (!Clone) 1745 Clone = 1746 Function::Create(Fn->getFunctionType(), GlobalValue::ExternalLinkage, 1747 ClonedFnName, GPUModule.get()); 1748 assert(Clone && "Expected cloned function to be initialized."); 1749 assert(ValueMap.find(Fn) == ValueMap.end() && 1750 "Fn already present in ValueMap"); 1751 ValueMap[Fn] = Clone; 1752 } 1753 } 1754 void GPUNodeBuilder::createKernel(__isl_take isl_ast_node *KernelStmt) { 1755 isl_id *Id = isl_ast_node_get_annotation(KernelStmt); 1756 ppcg_kernel *Kernel = (ppcg_kernel *)isl_id_get_user(Id); 1757 isl_id_free(Id); 1758 isl_ast_node_free(KernelStmt); 1759 1760 if (Kernel->n_grid > 1) 1761 DeepestParallel = 1762 std::max(DeepestParallel, isl_space_dim(Kernel->space, isl_dim_set)); 1763 else 1764 DeepestSequential = 1765 std::max(DeepestSequential, isl_space_dim(Kernel->space, isl_dim_set)); 1766 1767 Value *BlockDimX, *BlockDimY, *BlockDimZ; 1768 std::tie(BlockDimX, BlockDimY, BlockDimZ) = getBlockSizes(Kernel); 1769 1770 SetVector<Value *> SubtreeValues; 1771 SetVector<Function *> SubtreeFunctions; 1772 SetVector<const Loop *> Loops; 1773 isl::space ParamSpace; 1774 std::tie(SubtreeValues, SubtreeFunctions, Loops, ParamSpace) = 1775 getReferencesInKernel(Kernel); 1776 1777 // Add parameters that appear only in the access function to the kernel 1778 // space. This is important to make sure that all isl_ids are passed as 1779 // parameters to the kernel, even though we may not have all parameters 1780 // in the context to improve compile time. 1781 Kernel->space = isl_space_align_params(Kernel->space, ParamSpace.release()); 1782 1783 assert(Kernel->tree && "Device AST of kernel node is empty"); 1784 1785 Instruction &HostInsertPoint = *Builder.GetInsertPoint(); 1786 IslExprBuilder::IDToValueTy HostIDs = IDToValue; 1787 ValueMapT HostValueMap = ValueMap; 1788 BlockGenerator::AllocaMapTy HostScalarMap = ScalarMap; 1789 ScalarMap.clear(); 1790 1791 // Create for all loops we depend on values that contain the current loop 1792 // iteration. These values are necessary to generate code for SCEVs that 1793 // depend on such loops. As a result we need to pass them to the subfunction. 1794 for (const Loop *L : Loops) { 1795 const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), 1796 SE.getUnknown(Builder.getInt64(1)), 1797 L, SCEV::FlagAnyWrap); 1798 Value *V = generateSCEV(OuterLIV); 1799 OutsideLoopIterations[L] = SE.getUnknown(V); 1800 SubtreeValues.insert(V); 1801 } 1802 1803 createKernelFunction(Kernel, SubtreeValues, SubtreeFunctions); 1804 setupKernelSubtreeFunctions(SubtreeFunctions); 1805 1806 create(isl_ast_node_copy(Kernel->tree)); 1807 1808 finalizeKernelArguments(Kernel); 1809 Function *F = Builder.GetInsertBlock()->getParent(); 1810 if (Arch == GPUArch::NVPTX64) 1811 addCUDAAnnotations(F->getParent(), BlockDimX, BlockDimY, BlockDimZ); 1812 clearDominators(F); 1813 clearScalarEvolution(F); 1814 clearLoops(F); 1815 1816 IDToValue = HostIDs; 1817 1818 ValueMap = std::move(HostValueMap); 1819 ScalarMap = std::move(HostScalarMap); 1820 EscapeMap.clear(); 1821 IDToSAI.clear(); 1822 Annotator.resetAlternativeAliasBases(); 1823 for (auto &BasePtr : LocalArrays) 1824 S.invalidateScopArrayInfo(BasePtr, MemoryKind::Array); 1825 LocalArrays.clear(); 1826 1827 std::string ASMString = finalizeKernelFunction(); 1828 Builder.SetInsertPoint(&HostInsertPoint); 1829 Value *Parameters = createLaunchParameters(Kernel, F, SubtreeValues); 1830 1831 std::string Name = getKernelFuncName(Kernel->id); 1832 Value *KernelString = Builder.CreateGlobalStringPtr(ASMString, Name); 1833 Value *NameString = Builder.CreateGlobalStringPtr(Name, Name + "_name"); 1834 Value *GPUKernel = createCallGetKernel(KernelString, NameString); 1835 1836 Value *GridDimX, *GridDimY; 1837 std::tie(GridDimX, GridDimY) = getGridSizes(Kernel); 1838 1839 createCallLaunchKernel(GPUKernel, GridDimX, GridDimY, BlockDimX, BlockDimY, 1840 BlockDimZ, Parameters); 1841 createCallFreeKernel(GPUKernel); 1842 1843 for (auto Id : KernelIds) 1844 isl_id_free(Id); 1845 1846 KernelIds.clear(); 1847 } 1848 1849 /// Compute the DataLayout string for the NVPTX backend. 1850 /// 1851 /// @param is64Bit Are we looking for a 64 bit architecture? 1852 static std::string computeNVPTXDataLayout(bool is64Bit) { 1853 std::string Ret = ""; 1854 1855 if (!is64Bit) { 1856 Ret += "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:" 1857 "64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:" 1858 "64-v128:128:128-n16:32:64"; 1859 } else { 1860 Ret += "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:" 1861 "64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:" 1862 "64-v128:128:128-n16:32:64"; 1863 } 1864 1865 return Ret; 1866 } 1867 1868 /// Compute the DataLayout string for a SPIR kernel. 1869 /// 1870 /// @param is64Bit Are we looking for a 64 bit architecture? 1871 static std::string computeSPIRDataLayout(bool is64Bit) { 1872 std::string Ret = ""; 1873 1874 if (!is64Bit) { 1875 Ret += "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:" 1876 "64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:" 1877 "32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:" 1878 "256:256-v256:256:256-v512:512:512-v1024:1024:1024"; 1879 } else { 1880 Ret += "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:" 1881 "64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:" 1882 "32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:" 1883 "256:256-v256:256:256-v512:512:512-v1024:1024:1024"; 1884 } 1885 1886 return Ret; 1887 } 1888 1889 Function * 1890 GPUNodeBuilder::createKernelFunctionDecl(ppcg_kernel *Kernel, 1891 SetVector<Value *> &SubtreeValues) { 1892 std::vector<Type *> Args; 1893 std::string Identifier = getKernelFuncName(Kernel->id); 1894 1895 std::vector<Metadata *> MemoryType; 1896 1897 for (long i = 0; i < Prog->n_array; i++) { 1898 if (!ppcg_kernel_requires_array_argument(Kernel, i)) 1899 continue; 1900 1901 if (gpu_array_is_read_only_scalar(&Prog->array[i])) { 1902 isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set); 1903 const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(isl::manage(Id)); 1904 Args.push_back(SAI->getElementType()); 1905 MemoryType.push_back( 1906 ConstantAsMetadata::get(ConstantInt::get(Builder.getInt32Ty(), 0))); 1907 } else { 1908 static const int UseGlobalMemory = 1; 1909 Args.push_back(Builder.getInt8PtrTy(UseGlobalMemory)); 1910 MemoryType.push_back( 1911 ConstantAsMetadata::get(ConstantInt::get(Builder.getInt32Ty(), 1))); 1912 } 1913 } 1914 1915 int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set); 1916 1917 for (long i = 0; i < NumHostIters; i++) { 1918 Args.push_back(Builder.getInt64Ty()); 1919 MemoryType.push_back( 1920 ConstantAsMetadata::get(ConstantInt::get(Builder.getInt32Ty(), 0))); 1921 } 1922 1923 int NumVars = isl_space_dim(Kernel->space, isl_dim_param); 1924 1925 for (long i = 0; i < NumVars; i++) { 1926 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i); 1927 Value *Val = IDToValue[Id]; 1928 isl_id_free(Id); 1929 Args.push_back(Val->getType()); 1930 MemoryType.push_back( 1931 ConstantAsMetadata::get(ConstantInt::get(Builder.getInt32Ty(), 0))); 1932 } 1933 1934 for (auto *V : SubtreeValues) { 1935 Args.push_back(V->getType()); 1936 MemoryType.push_back( 1937 ConstantAsMetadata::get(ConstantInt::get(Builder.getInt32Ty(), 0))); 1938 } 1939 1940 auto *FT = FunctionType::get(Builder.getVoidTy(), Args, false); 1941 auto *FN = Function::Create(FT, Function::ExternalLinkage, Identifier, 1942 GPUModule.get()); 1943 1944 std::vector<Metadata *> EmptyStrings; 1945 1946 for (unsigned int i = 0; i < MemoryType.size(); i++) { 1947 EmptyStrings.push_back(MDString::get(FN->getContext(), "")); 1948 } 1949 1950 if (Arch == GPUArch::SPIR32 || Arch == GPUArch::SPIR64) { 1951 FN->setMetadata("kernel_arg_addr_space", 1952 MDNode::get(FN->getContext(), MemoryType)); 1953 FN->setMetadata("kernel_arg_name", 1954 MDNode::get(FN->getContext(), EmptyStrings)); 1955 FN->setMetadata("kernel_arg_access_qual", 1956 MDNode::get(FN->getContext(), EmptyStrings)); 1957 FN->setMetadata("kernel_arg_type", 1958 MDNode::get(FN->getContext(), EmptyStrings)); 1959 FN->setMetadata("kernel_arg_type_qual", 1960 MDNode::get(FN->getContext(), EmptyStrings)); 1961 FN->setMetadata("kernel_arg_base_type", 1962 MDNode::get(FN->getContext(), EmptyStrings)); 1963 } 1964 1965 switch (Arch) { 1966 case GPUArch::NVPTX64: 1967 FN->setCallingConv(CallingConv::PTX_Kernel); 1968 break; 1969 case GPUArch::SPIR32: 1970 case GPUArch::SPIR64: 1971 FN->setCallingConv(CallingConv::SPIR_KERNEL); 1972 break; 1973 } 1974 1975 auto Arg = FN->arg_begin(); 1976 for (long i = 0; i < Kernel->n_array; i++) { 1977 if (!ppcg_kernel_requires_array_argument(Kernel, i)) 1978 continue; 1979 1980 Arg->setName(Kernel->array[i].array->name); 1981 1982 isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set); 1983 const ScopArrayInfo *SAI = 1984 ScopArrayInfo::getFromId(isl::manage(isl_id_copy(Id))); 1985 Type *EleTy = SAI->getElementType(); 1986 Value *Val = &*Arg; 1987 SmallVector<const SCEV *, 4> Sizes; 1988 isl_ast_build *Build = 1989 isl_ast_build_from_context(isl_set_copy(Prog->context)); 1990 Sizes.push_back(nullptr); 1991 for (long j = 1; j < Kernel->array[i].array->n_index; j++) { 1992 isl_ast_expr *DimSize = isl_ast_build_expr_from_pw_aff( 1993 Build, isl_multi_pw_aff_get_pw_aff(Kernel->array[i].array->bound, j)); 1994 auto V = ExprBuilder.create(DimSize); 1995 Sizes.push_back(SE.getSCEV(V)); 1996 } 1997 const ScopArrayInfo *SAIRep = 1998 S.getOrCreateScopArrayInfo(Val, EleTy, Sizes, MemoryKind::Array); 1999 LocalArrays.push_back(Val); 2000 2001 isl_ast_build_free(Build); 2002 KernelIds.push_back(Id); 2003 IDToSAI[Id] = SAIRep; 2004 Arg++; 2005 } 2006 2007 for (long i = 0; i < NumHostIters; i++) { 2008 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i); 2009 Arg->setName(isl_id_get_name(Id)); 2010 IDToValue[Id] = &*Arg; 2011 KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 2012 Arg++; 2013 } 2014 2015 for (long i = 0; i < NumVars; i++) { 2016 isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i); 2017 Arg->setName(isl_id_get_name(Id)); 2018 Value *Val = IDToValue[Id]; 2019 ValueMap[Val] = &*Arg; 2020 IDToValue[Id] = &*Arg; 2021 KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 2022 Arg++; 2023 } 2024 2025 for (auto *V : SubtreeValues) { 2026 Arg->setName(V->getName()); 2027 ValueMap[V] = &*Arg; 2028 Arg++; 2029 } 2030 2031 return FN; 2032 } 2033 2034 void GPUNodeBuilder::insertKernelIntrinsics(ppcg_kernel *Kernel) { 2035 Intrinsic::ID IntrinsicsBID[2]; 2036 Intrinsic::ID IntrinsicsTID[3]; 2037 2038 switch (Arch) { 2039 case GPUArch::SPIR64: 2040 case GPUArch::SPIR32: 2041 llvm_unreachable("Cannot generate NVVM intrinsics for SPIR"); 2042 case GPUArch::NVPTX64: 2043 IntrinsicsBID[0] = Intrinsic::nvvm_read_ptx_sreg_ctaid_x; 2044 IntrinsicsBID[1] = Intrinsic::nvvm_read_ptx_sreg_ctaid_y; 2045 2046 IntrinsicsTID[0] = Intrinsic::nvvm_read_ptx_sreg_tid_x; 2047 IntrinsicsTID[1] = Intrinsic::nvvm_read_ptx_sreg_tid_y; 2048 IntrinsicsTID[2] = Intrinsic::nvvm_read_ptx_sreg_tid_z; 2049 break; 2050 } 2051 2052 auto addId = [this](__isl_take isl_id *Id, Intrinsic::ID Intr) mutable { 2053 std::string Name = isl_id_get_name(Id); 2054 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 2055 Function *IntrinsicFn = Intrinsic::getDeclaration(M, Intr); 2056 Value *Val = Builder.CreateCall(IntrinsicFn, {}); 2057 Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name); 2058 IDToValue[Id] = Val; 2059 KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 2060 }; 2061 2062 for (int i = 0; i < Kernel->n_grid; ++i) { 2063 isl_id *Id = isl_id_list_get_id(Kernel->block_ids, i); 2064 addId(Id, IntrinsicsBID[i]); 2065 } 2066 2067 for (int i = 0; i < Kernel->n_block; ++i) { 2068 isl_id *Id = isl_id_list_get_id(Kernel->thread_ids, i); 2069 addId(Id, IntrinsicsTID[i]); 2070 } 2071 } 2072 2073 void GPUNodeBuilder::insertKernelCallsSPIR(ppcg_kernel *Kernel) { 2074 const char *GroupName[3] = {"__gen_ocl_get_group_id0", 2075 "__gen_ocl_get_group_id1", 2076 "__gen_ocl_get_group_id2"}; 2077 2078 const char *LocalName[3] = {"__gen_ocl_get_local_id0", 2079 "__gen_ocl_get_local_id1", 2080 "__gen_ocl_get_local_id2"}; 2081 2082 auto createFunc = [this](const char *Name, __isl_take isl_id *Id) mutable { 2083 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 2084 Function *FN = M->getFunction(Name); 2085 2086 // If FN is not available, declare it. 2087 if (!FN) { 2088 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 2089 std::vector<Type *> Args; 2090 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), Args, false); 2091 FN = Function::Create(Ty, Linkage, Name, M); 2092 FN->setCallingConv(CallingConv::SPIR_FUNC); 2093 } 2094 2095 Value *Val = Builder.CreateCall(FN, {}); 2096 Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name); 2097 IDToValue[Id] = Val; 2098 KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 2099 }; 2100 2101 for (int i = 0; i < Kernel->n_grid; ++i) 2102 createFunc(GroupName[i], isl_id_list_get_id(Kernel->block_ids, i)); 2103 2104 for (int i = 0; i < Kernel->n_block; ++i) 2105 createFunc(LocalName[i], isl_id_list_get_id(Kernel->thread_ids, i)); 2106 } 2107 2108 void GPUNodeBuilder::prepareKernelArguments(ppcg_kernel *Kernel, Function *FN) { 2109 auto Arg = FN->arg_begin(); 2110 for (long i = 0; i < Kernel->n_array; i++) { 2111 if (!ppcg_kernel_requires_array_argument(Kernel, i)) 2112 continue; 2113 2114 isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set); 2115 const ScopArrayInfo *SAI = 2116 ScopArrayInfo::getFromId(isl::manage(isl_id_copy(Id))); 2117 isl_id_free(Id); 2118 2119 if (SAI->getNumberOfDimensions() > 0) { 2120 Arg++; 2121 continue; 2122 } 2123 2124 Value *Val = &*Arg; 2125 2126 if (!gpu_array_is_read_only_scalar(&Prog->array[i])) { 2127 Type *TypePtr = SAI->getElementType()->getPointerTo(); 2128 Value *TypedArgPtr = Builder.CreatePointerCast(Val, TypePtr); 2129 Val = Builder.CreateLoad(TypedArgPtr); 2130 } 2131 2132 Value *Alloca = BlockGen.getOrCreateAlloca(SAI); 2133 Builder.CreateStore(Val, Alloca); 2134 2135 Arg++; 2136 } 2137 } 2138 2139 void GPUNodeBuilder::finalizeKernelArguments(ppcg_kernel *Kernel) { 2140 auto *FN = Builder.GetInsertBlock()->getParent(); 2141 auto Arg = FN->arg_begin(); 2142 2143 bool StoredScalar = false; 2144 for (long i = 0; i < Kernel->n_array; i++) { 2145 if (!ppcg_kernel_requires_array_argument(Kernel, i)) 2146 continue; 2147 2148 isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set); 2149 const ScopArrayInfo *SAI = 2150 ScopArrayInfo::getFromId(isl::manage(isl_id_copy(Id))); 2151 isl_id_free(Id); 2152 2153 if (SAI->getNumberOfDimensions() > 0) { 2154 Arg++; 2155 continue; 2156 } 2157 2158 if (gpu_array_is_read_only_scalar(&Prog->array[i])) { 2159 Arg++; 2160 continue; 2161 } 2162 2163 Value *Alloca = BlockGen.getOrCreateAlloca(SAI); 2164 Value *ArgPtr = &*Arg; 2165 Type *TypePtr = SAI->getElementType()->getPointerTo(); 2166 Value *TypedArgPtr = Builder.CreatePointerCast(ArgPtr, TypePtr); 2167 Value *Val = Builder.CreateLoad(Alloca); 2168 Builder.CreateStore(Val, TypedArgPtr); 2169 StoredScalar = true; 2170 2171 Arg++; 2172 } 2173 2174 if (StoredScalar) { 2175 /// In case more than one thread contains scalar stores, the generated 2176 /// code might be incorrect, if we only store at the end of the kernel. 2177 /// To support this case we need to store these scalars back at each 2178 /// memory store or at least before each kernel barrier. 2179 if (Kernel->n_block != 0 || Kernel->n_grid != 0) { 2180 BuildSuccessful = 0; 2181 DEBUG( 2182 dbgs() << getUniqueScopName(&S) 2183 << " has a store to a scalar value that" 2184 " would be undefined to run in parallel. Bailing out.\n";); 2185 } 2186 } 2187 } 2188 2189 void GPUNodeBuilder::createKernelVariables(ppcg_kernel *Kernel, Function *FN) { 2190 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 2191 2192 for (int i = 0; i < Kernel->n_var; ++i) { 2193 struct ppcg_kernel_var &Var = Kernel->var[i]; 2194 isl_id *Id = isl_space_get_tuple_id(Var.array->space, isl_dim_set); 2195 Type *EleTy = ScopArrayInfo::getFromId(isl::manage(Id))->getElementType(); 2196 2197 Type *ArrayTy = EleTy; 2198 SmallVector<const SCEV *, 4> Sizes; 2199 2200 Sizes.push_back(nullptr); 2201 for (unsigned int j = 1; j < Var.array->n_index; ++j) { 2202 isl_val *Val = isl_vec_get_element_val(Var.size, j); 2203 long Bound = isl_val_get_num_si(Val); 2204 isl_val_free(Val); 2205 Sizes.push_back(S.getSE()->getConstant(Builder.getInt64Ty(), Bound)); 2206 } 2207 2208 for (int j = Var.array->n_index - 1; j >= 0; --j) { 2209 isl_val *Val = isl_vec_get_element_val(Var.size, j); 2210 long Bound = isl_val_get_num_si(Val); 2211 isl_val_free(Val); 2212 ArrayTy = ArrayType::get(ArrayTy, Bound); 2213 } 2214 2215 const ScopArrayInfo *SAI; 2216 Value *Allocation; 2217 if (Var.type == ppcg_access_shared) { 2218 auto GlobalVar = new GlobalVariable( 2219 *M, ArrayTy, false, GlobalValue::InternalLinkage, 0, Var.name, 2220 nullptr, GlobalValue::ThreadLocalMode::NotThreadLocal, 3); 2221 GlobalVar->setAlignment(EleTy->getPrimitiveSizeInBits() / 8); 2222 GlobalVar->setInitializer(Constant::getNullValue(ArrayTy)); 2223 2224 Allocation = GlobalVar; 2225 } else if (Var.type == ppcg_access_private) { 2226 Allocation = Builder.CreateAlloca(ArrayTy, 0, "private_array"); 2227 } else { 2228 llvm_unreachable("unknown variable type"); 2229 } 2230 SAI = 2231 S.getOrCreateScopArrayInfo(Allocation, EleTy, Sizes, MemoryKind::Array); 2232 Id = isl_id_alloc(S.getIslCtx(), Var.name, nullptr); 2233 IDToValue[Id] = Allocation; 2234 LocalArrays.push_back(Allocation); 2235 KernelIds.push_back(Id); 2236 IDToSAI[Id] = SAI; 2237 } 2238 } 2239 2240 void GPUNodeBuilder::createKernelFunction( 2241 ppcg_kernel *Kernel, SetVector<Value *> &SubtreeValues, 2242 SetVector<Function *> &SubtreeFunctions) { 2243 std::string Identifier = getKernelFuncName(Kernel->id); 2244 GPUModule.reset(new Module(Identifier, Builder.getContext())); 2245 2246 switch (Arch) { 2247 case GPUArch::NVPTX64: 2248 if (Runtime == GPURuntime::CUDA) 2249 GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda")); 2250 else if (Runtime == GPURuntime::OpenCL) 2251 GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-nvcl")); 2252 GPUModule->setDataLayout(computeNVPTXDataLayout(true /* is64Bit */)); 2253 break; 2254 case GPUArch::SPIR32: 2255 GPUModule->setTargetTriple(Triple::normalize("spir-unknown-unknown")); 2256 GPUModule->setDataLayout(computeSPIRDataLayout(false /* is64Bit */)); 2257 break; 2258 case GPUArch::SPIR64: 2259 GPUModule->setTargetTriple(Triple::normalize("spir64-unknown-unknown")); 2260 GPUModule->setDataLayout(computeSPIRDataLayout(true /* is64Bit */)); 2261 break; 2262 } 2263 2264 Function *FN = createKernelFunctionDecl(Kernel, SubtreeValues); 2265 2266 BasicBlock *PrevBlock = Builder.GetInsertBlock(); 2267 auto EntryBlock = BasicBlock::Create(Builder.getContext(), "entry", FN); 2268 2269 DT.addNewBlock(EntryBlock, PrevBlock); 2270 2271 Builder.SetInsertPoint(EntryBlock); 2272 Builder.CreateRetVoid(); 2273 Builder.SetInsertPoint(EntryBlock, EntryBlock->begin()); 2274 2275 ScopDetection::markFunctionAsInvalid(FN); 2276 2277 prepareKernelArguments(Kernel, FN); 2278 createKernelVariables(Kernel, FN); 2279 2280 switch (Arch) { 2281 case GPUArch::NVPTX64: 2282 insertKernelIntrinsics(Kernel); 2283 break; 2284 case GPUArch::SPIR32: 2285 case GPUArch::SPIR64: 2286 insertKernelCallsSPIR(Kernel); 2287 break; 2288 } 2289 } 2290 2291 std::string GPUNodeBuilder::createKernelASM() { 2292 llvm::Triple GPUTriple; 2293 2294 switch (Arch) { 2295 case GPUArch::NVPTX64: 2296 switch (Runtime) { 2297 case GPURuntime::CUDA: 2298 GPUTriple = llvm::Triple(Triple::normalize("nvptx64-nvidia-cuda")); 2299 break; 2300 case GPURuntime::OpenCL: 2301 GPUTriple = llvm::Triple(Triple::normalize("nvptx64-nvidia-nvcl")); 2302 break; 2303 } 2304 break; 2305 case GPUArch::SPIR64: 2306 case GPUArch::SPIR32: 2307 std::string SPIRAssembly; 2308 raw_string_ostream IROstream(SPIRAssembly); 2309 IROstream << *GPUModule; 2310 IROstream.flush(); 2311 return SPIRAssembly; 2312 } 2313 2314 std::string ErrMsg; 2315 auto GPUTarget = TargetRegistry::lookupTarget(GPUTriple.getTriple(), ErrMsg); 2316 2317 if (!GPUTarget) { 2318 errs() << ErrMsg << "\n"; 2319 return ""; 2320 } 2321 2322 TargetOptions Options; 2323 Options.UnsafeFPMath = FastMath; 2324 2325 std::string subtarget; 2326 2327 switch (Arch) { 2328 case GPUArch::NVPTX64: 2329 subtarget = CudaVersion; 2330 break; 2331 case GPUArch::SPIR32: 2332 case GPUArch::SPIR64: 2333 llvm_unreachable("No subtarget for SPIR architecture"); 2334 } 2335 2336 std::unique_ptr<TargetMachine> TargetM(GPUTarget->createTargetMachine( 2337 GPUTriple.getTriple(), subtarget, "", Options, Optional<Reloc::Model>())); 2338 2339 SmallString<0> ASMString; 2340 raw_svector_ostream ASMStream(ASMString); 2341 llvm::legacy::PassManager PM; 2342 2343 PM.add(createTargetTransformInfoWrapperPass(TargetM->getTargetIRAnalysis())); 2344 2345 if (TargetM->addPassesToEmitFile( 2346 PM, ASMStream, TargetMachine::CGFT_AssemblyFile, true /* verify */)) { 2347 errs() << "The target does not support generation of this file type!\n"; 2348 return ""; 2349 } 2350 2351 PM.run(*GPUModule); 2352 2353 return ASMStream.str(); 2354 } 2355 2356 bool GPUNodeBuilder::requiresCUDALibDevice() { 2357 bool RequiresLibDevice = false; 2358 for (Function &F : GPUModule->functions()) { 2359 if (!F.isDeclaration()) 2360 continue; 2361 2362 std::string CUDALibDeviceFunc = getCUDALibDeviceFuntion(&F); 2363 if (CUDALibDeviceFunc.length() != 0) { 2364 F.setName(CUDALibDeviceFunc); 2365 RequiresLibDevice = true; 2366 } 2367 } 2368 2369 return RequiresLibDevice; 2370 } 2371 2372 void GPUNodeBuilder::addCUDALibDevice() { 2373 if (Arch != GPUArch::NVPTX64) 2374 return; 2375 2376 if (requiresCUDALibDevice()) { 2377 SMDiagnostic Error; 2378 2379 errs() << CUDALibDevice << "\n"; 2380 auto LibDeviceModule = 2381 parseIRFile(CUDALibDevice, Error, GPUModule->getContext()); 2382 2383 if (!LibDeviceModule) { 2384 BuildSuccessful = false; 2385 report_fatal_error("Could not find or load libdevice. Skipping GPU " 2386 "kernel generation. Please set -polly-acc-libdevice " 2387 "accordingly.\n"); 2388 return; 2389 } 2390 2391 Linker L(*GPUModule); 2392 2393 // Set an nvptx64 target triple to avoid linker warnings. The original 2394 // triple of the libdevice files are nvptx-unknown-unknown. 2395 LibDeviceModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda")); 2396 L.linkInModule(std::move(LibDeviceModule), Linker::LinkOnlyNeeded); 2397 } 2398 } 2399 2400 std::string GPUNodeBuilder::finalizeKernelFunction() { 2401 2402 if (verifyModule(*GPUModule)) { 2403 DEBUG(dbgs() << "verifyModule failed on module:\n"; 2404 GPUModule->print(dbgs(), nullptr); dbgs() << "\n";); 2405 DEBUG(dbgs() << "verifyModule Error:\n"; 2406 verifyModule(*GPUModule, &dbgs());); 2407 2408 if (FailOnVerifyModuleFailure) 2409 llvm_unreachable("VerifyModule failed."); 2410 2411 BuildSuccessful = false; 2412 return ""; 2413 } 2414 2415 addCUDALibDevice(); 2416 2417 if (DumpKernelIR) 2418 outs() << *GPUModule << "\n"; 2419 2420 if (Arch != GPUArch::SPIR32 && Arch != GPUArch::SPIR64) { 2421 // Optimize module. 2422 llvm::legacy::PassManager OptPasses; 2423 PassManagerBuilder PassBuilder; 2424 PassBuilder.OptLevel = 3; 2425 PassBuilder.SizeLevel = 0; 2426 PassBuilder.populateModulePassManager(OptPasses); 2427 OptPasses.run(*GPUModule); 2428 } 2429 2430 std::string Assembly = createKernelASM(); 2431 2432 if (DumpKernelASM) 2433 outs() << Assembly << "\n"; 2434 2435 GPUModule.release(); 2436 KernelIDs.clear(); 2437 2438 return Assembly; 2439 } 2440 /// Construct an `isl_pw_aff_list` from a vector of `isl_pw_aff` 2441 /// @param PwAffs The list of piecewise affine functions to create an 2442 /// `isl_pw_aff_list` from. We expect an rvalue ref because 2443 /// all the isl_pw_aff are used up by this function. 2444 /// 2445 /// @returns The `isl_pw_aff_list`. 2446 __isl_give isl_pw_aff_list * 2447 createPwAffList(isl_ctx *Context, 2448 const std::vector<__isl_take isl_pw_aff *> &&PwAffs) { 2449 isl_pw_aff_list *List = isl_pw_aff_list_alloc(Context, PwAffs.size()); 2450 2451 for (unsigned i = 0; i < PwAffs.size(); i++) { 2452 List = isl_pw_aff_list_insert(List, i, PwAffs[i]); 2453 } 2454 return List; 2455 } 2456 2457 /// Align all the `PwAffs` such that they have the same parameter dimensions. 2458 /// 2459 /// We loop over all `pw_aff` and align all of their spaces together to 2460 /// create a common space for all the `pw_aff`. This common space is the 2461 /// `AlignSpace`. We then align all the `pw_aff` to this space. We start 2462 /// with the given `SeedSpace`. 2463 /// @param PwAffs The list of piecewise affine functions we want to align. 2464 /// This is an rvalue reference because the entire vector is 2465 /// used up by the end of the operation. 2466 /// @param SeedSpace The space to start the alignment process with. 2467 /// @returns A std::pair, whose first element is the aligned space, 2468 /// whose second element is the vector of aligned piecewise 2469 /// affines. 2470 static std::pair<__isl_give isl_space *, std::vector<__isl_give isl_pw_aff *>> 2471 alignPwAffs(const std::vector<__isl_take isl_pw_aff *> &&PwAffs, 2472 __isl_take isl_space *SeedSpace) { 2473 assert(SeedSpace && "Invalid seed space given."); 2474 2475 isl_space *AlignSpace = SeedSpace; 2476 for (isl_pw_aff *PwAff : PwAffs) { 2477 isl_space *PwAffSpace = isl_pw_aff_get_domain_space(PwAff); 2478 AlignSpace = isl_space_align_params(AlignSpace, PwAffSpace); 2479 } 2480 std::vector<isl_pw_aff *> AdjustedPwAffs; 2481 2482 for (unsigned i = 0; i < PwAffs.size(); i++) { 2483 isl_pw_aff *Adjusted = PwAffs[i]; 2484 assert(Adjusted && "Invalid pw_aff given."); 2485 Adjusted = isl_pw_aff_align_params(Adjusted, isl_space_copy(AlignSpace)); 2486 AdjustedPwAffs.push_back(Adjusted); 2487 } 2488 return std::make_pair(AlignSpace, AdjustedPwAffs); 2489 } 2490 2491 namespace { 2492 class PPCGCodeGeneration : public ScopPass { 2493 public: 2494 static char ID; 2495 2496 GPURuntime Runtime = GPURuntime::CUDA; 2497 2498 GPUArch Architecture = GPUArch::NVPTX64; 2499 2500 /// The scop that is currently processed. 2501 Scop *S; 2502 2503 LoopInfo *LI; 2504 DominatorTree *DT; 2505 ScalarEvolution *SE; 2506 const DataLayout *DL; 2507 RegionInfo *RI; 2508 2509 PPCGCodeGeneration() : ScopPass(ID) {} 2510 2511 /// Construct compilation options for PPCG. 2512 /// 2513 /// @returns The compilation options. 2514 ppcg_options *createPPCGOptions() { 2515 auto DebugOptions = 2516 (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options)); 2517 auto Options = (ppcg_options *)malloc(sizeof(ppcg_options)); 2518 2519 DebugOptions->dump_schedule_constraints = false; 2520 DebugOptions->dump_schedule = false; 2521 DebugOptions->dump_final_schedule = false; 2522 DebugOptions->dump_sizes = false; 2523 DebugOptions->verbose = false; 2524 2525 Options->debug = DebugOptions; 2526 2527 Options->group_chains = false; 2528 Options->reschedule = true; 2529 Options->scale_tile_loops = false; 2530 Options->wrap = false; 2531 2532 Options->non_negative_parameters = false; 2533 Options->ctx = nullptr; 2534 Options->sizes = nullptr; 2535 2536 Options->tile = true; 2537 Options->tile_size = 32; 2538 2539 Options->isolate_full_tiles = false; 2540 2541 Options->use_private_memory = PrivateMemory; 2542 Options->use_shared_memory = SharedMemory; 2543 Options->max_shared_memory = 48 * 1024; 2544 2545 Options->target = PPCG_TARGET_CUDA; 2546 Options->openmp = false; 2547 Options->linearize_device_arrays = true; 2548 Options->allow_gnu_extensions = false; 2549 2550 Options->unroll_copy_shared = false; 2551 Options->unroll_gpu_tile = false; 2552 Options->live_range_reordering = true; 2553 2554 Options->live_range_reordering = true; 2555 Options->hybrid = false; 2556 Options->opencl_compiler_options = nullptr; 2557 Options->opencl_use_gpu = false; 2558 Options->opencl_n_include_file = 0; 2559 Options->opencl_include_files = nullptr; 2560 Options->opencl_print_kernel_types = false; 2561 Options->opencl_embed_kernel_code = false; 2562 2563 Options->save_schedule_file = nullptr; 2564 Options->load_schedule_file = nullptr; 2565 2566 return Options; 2567 } 2568 2569 /// Get a tagged access relation containing all accesses of type @p AccessTy. 2570 /// 2571 /// Instead of a normal access of the form: 2572 /// 2573 /// Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)] 2574 /// 2575 /// a tagged access has the form 2576 /// 2577 /// [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)] 2578 /// 2579 /// where 'id' is an additional space that references the memory access that 2580 /// triggered the access. 2581 /// 2582 /// @param AccessTy The type of the memory accesses to collect. 2583 /// 2584 /// @return The relation describing all tagged memory accesses. 2585 isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) { 2586 isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace().release()); 2587 2588 for (auto &Stmt : *S) 2589 for (auto &Acc : Stmt) 2590 if (Acc->getType() == AccessTy) { 2591 isl_map *Relation = Acc->getAccessRelation().release(); 2592 Relation = 2593 isl_map_intersect_domain(Relation, Stmt.getDomain().release()); 2594 2595 isl_space *Space = isl_map_get_space(Relation); 2596 Space = isl_space_range(Space); 2597 Space = isl_space_from_range(Space); 2598 Space = 2599 isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId().release()); 2600 isl_map *Universe = isl_map_universe(Space); 2601 Relation = isl_map_domain_product(Relation, Universe); 2602 Accesses = isl_union_map_add_map(Accesses, Relation); 2603 } 2604 2605 return Accesses; 2606 } 2607 2608 /// Get the set of all read accesses, tagged with the access id. 2609 /// 2610 /// @see getTaggedAccesses 2611 isl_union_map *getTaggedReads() { 2612 return getTaggedAccesses(MemoryAccess::READ); 2613 } 2614 2615 /// Get the set of all may (and must) accesses, tagged with the access id. 2616 /// 2617 /// @see getTaggedAccesses 2618 isl_union_map *getTaggedMayWrites() { 2619 return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE), 2620 getTaggedAccesses(MemoryAccess::MUST_WRITE)); 2621 } 2622 2623 /// Get the set of all must accesses, tagged with the access id. 2624 /// 2625 /// @see getTaggedAccesses 2626 isl_union_map *getTaggedMustWrites() { 2627 return getTaggedAccesses(MemoryAccess::MUST_WRITE); 2628 } 2629 2630 /// Collect parameter and array names as isl_ids. 2631 /// 2632 /// To reason about the different parameters and arrays used, ppcg requires 2633 /// a list of all isl_ids in use. As PPCG traditionally performs 2634 /// source-to-source compilation each of these isl_ids is mapped to the 2635 /// expression that represents it. As we do not have a corresponding 2636 /// expression in Polly, we just map each id to a 'zero' expression to match 2637 /// the data format that ppcg expects. 2638 /// 2639 /// @returns Retun a map from collected ids to 'zero' ast expressions. 2640 __isl_give isl_id_to_ast_expr *getNames() { 2641 auto *Names = isl_id_to_ast_expr_alloc( 2642 S->getIslCtx(), 2643 S->getNumParams() + std::distance(S->array_begin(), S->array_end())); 2644 auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx())); 2645 2646 for (const SCEV *P : S->parameters()) { 2647 isl_id *Id = S->getIdForParam(P).release(); 2648 Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 2649 } 2650 2651 for (auto &Array : S->arrays()) { 2652 auto Id = Array->getBasePtrId().release(); 2653 Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 2654 } 2655 2656 isl_ast_expr_free(Zero); 2657 2658 return Names; 2659 } 2660 2661 /// Remove unreferenced parameter dimensions from union_map. 2662 isl::union_map removeUnusedParameters(isl::union_map UMap) { 2663 auto New = isl::union_map::empty(isl::space(UMap.get_ctx(), 0, 0)); 2664 2665 auto RemoveUnusedDims = [&New](isl::map S) -> isl::stat { 2666 int Removed = 0; 2667 int NumDims = S.dim(isl::dim::param); 2668 for (long i = 0; i < NumDims; i++) { 2669 const int Dim = i - Removed; 2670 if (!S.involves_dims(isl::dim::param, Dim, 1)) { 2671 S = S.remove_dims(isl::dim::param, Dim, 1); 2672 Removed++; 2673 } 2674 } 2675 New = New.unite(S); 2676 return isl::stat::ok; 2677 }; 2678 2679 UMap.foreach_map(RemoveUnusedDims); 2680 return New; 2681 } 2682 2683 /// Remove unreferenced parameter dimensions from union_set. 2684 isl::union_set removeUnusedParameters(isl::union_set USet) { 2685 auto New = isl::union_set::empty(isl::space(USet.get_ctx(), 0, 0)); 2686 2687 auto RemoveUnusedDims = [&New](isl::set S) -> isl::stat { 2688 int Removed = 0; 2689 int NumDims = S.dim(isl::dim::param); 2690 for (long i = 0; i < NumDims; i++) { 2691 const int Dim = i - Removed; 2692 if (!S.involves_dims(isl::dim::param, Dim, 1)) { 2693 S = S.remove_dims(isl::dim::param, Dim, 1); 2694 Removed++; 2695 } 2696 } 2697 New = New.unite(S); 2698 return isl::stat::ok; 2699 }; 2700 2701 USet.foreach_set(RemoveUnusedDims); 2702 return New; 2703 } 2704 2705 /// Simplify PPCG scop to improve compile time. 2706 /// 2707 /// We drop unused parameter dimensions to reduce the size of the sets we are 2708 /// working with. Especially the computed dependences tend to accumulate a lot 2709 /// of parameters that are present in the input memory accesses, but often are 2710 /// not necessary to express the actual dependences. As isl represents maps 2711 /// and sets with dense matrices, reducing the dimensionality of isl sets 2712 /// commonly reduces code generation performance. 2713 void simplifyPPCGScop(ppcg_scop *PPCGScop) { 2714 PPCGScop->domain = 2715 removeUnusedParameters(isl::manage(PPCGScop->domain)).release(); 2716 2717 PPCGScop->dep_forced = 2718 removeUnusedParameters(isl::manage(PPCGScop->dep_forced)).release(); 2719 PPCGScop->dep_false = 2720 removeUnusedParameters(isl::manage(PPCGScop->dep_false)).release(); 2721 PPCGScop->dep_flow = 2722 removeUnusedParameters(isl::manage(PPCGScop->dep_flow)).release(); 2723 PPCGScop->tagged_dep_flow = 2724 removeUnusedParameters(isl::manage(PPCGScop->tagged_dep_flow)) 2725 .release(); 2726 2727 PPCGScop->tagged_dep_order = 2728 removeUnusedParameters(isl::manage(PPCGScop->tagged_dep_order)) 2729 .release(); 2730 } 2731 2732 /// Create a new PPCG scop from the current scop. 2733 /// 2734 /// The PPCG scop is initialized with data from the current polly::Scop. From 2735 /// this initial data, the data-dependences in the PPCG scop are initialized. 2736 /// We do not use Polly's dependence analysis for now, to ensure we match 2737 /// the PPCG default behaviour more closely. 2738 /// 2739 /// @returns A new ppcg scop. 2740 ppcg_scop *createPPCGScop() { 2741 MustKillsInfo KillsInfo = computeMustKillsInfo(*S); 2742 2743 auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop)); 2744 2745 PPCGScop->options = createPPCGOptions(); 2746 // enable live range reordering 2747 PPCGScop->options->live_range_reordering = 1; 2748 2749 PPCGScop->start = 0; 2750 PPCGScop->end = 0; 2751 2752 PPCGScop->context = S->getContext().release(); 2753 PPCGScop->domain = S->getDomains().release(); 2754 // TODO: investigate this further. PPCG calls collect_call_domains. 2755 PPCGScop->call = isl_union_set_from_set(S->getContext().release()); 2756 PPCGScop->tagged_reads = getTaggedReads(); 2757 PPCGScop->reads = S->getReads().release(); 2758 PPCGScop->live_in = nullptr; 2759 PPCGScop->tagged_may_writes = getTaggedMayWrites(); 2760 PPCGScop->may_writes = S->getWrites().release(); 2761 PPCGScop->tagged_must_writes = getTaggedMustWrites(); 2762 PPCGScop->must_writes = S->getMustWrites().release(); 2763 PPCGScop->live_out = nullptr; 2764 PPCGScop->tagged_must_kills = KillsInfo.TaggedMustKills.take(); 2765 PPCGScop->must_kills = KillsInfo.MustKills.take(); 2766 2767 PPCGScop->tagger = nullptr; 2768 PPCGScop->independence = 2769 isl_union_map_empty(isl_set_get_space(PPCGScop->context)); 2770 PPCGScop->dep_flow = nullptr; 2771 PPCGScop->tagged_dep_flow = nullptr; 2772 PPCGScop->dep_false = nullptr; 2773 PPCGScop->dep_forced = nullptr; 2774 PPCGScop->dep_order = nullptr; 2775 PPCGScop->tagged_dep_order = nullptr; 2776 2777 PPCGScop->schedule = S->getScheduleTree().release(); 2778 // If we have something non-trivial to kill, add it to the schedule 2779 if (KillsInfo.KillsSchedule.get()) 2780 PPCGScop->schedule = isl_schedule_sequence( 2781 PPCGScop->schedule, KillsInfo.KillsSchedule.take()); 2782 2783 PPCGScop->names = getNames(); 2784 PPCGScop->pet = nullptr; 2785 2786 compute_tagger(PPCGScop); 2787 compute_dependences(PPCGScop); 2788 eliminate_dead_code(PPCGScop); 2789 simplifyPPCGScop(PPCGScop); 2790 2791 return PPCGScop; 2792 } 2793 2794 /// Collect the array accesses in a statement. 2795 /// 2796 /// @param Stmt The statement for which to collect the accesses. 2797 /// 2798 /// @returns A list of array accesses. 2799 gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) { 2800 gpu_stmt_access *Accesses = nullptr; 2801 2802 for (MemoryAccess *Acc : Stmt) { 2803 auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access); 2804 Access->read = Acc->isRead(); 2805 Access->write = Acc->isWrite(); 2806 Access->access = Acc->getAccessRelation().release(); 2807 isl_space *Space = isl_map_get_space(Access->access); 2808 Space = isl_space_range(Space); 2809 Space = isl_space_from_range(Space); 2810 Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId().release()); 2811 isl_map *Universe = isl_map_universe(Space); 2812 Access->tagged_access = 2813 isl_map_domain_product(Acc->getAccessRelation().release(), Universe); 2814 Access->exact_write = !Acc->isMayWrite(); 2815 Access->ref_id = Acc->getId().release(); 2816 Access->next = Accesses; 2817 Access->n_index = Acc->getScopArrayInfo()->getNumberOfDimensions(); 2818 Accesses = Access; 2819 } 2820 2821 return Accesses; 2822 } 2823 2824 /// Collect the list of GPU statements. 2825 /// 2826 /// Each statement has an id, a pointer to the underlying data structure, 2827 /// as well as a list with all memory accesses. 2828 /// 2829 /// TODO: Initialize the list of memory accesses. 2830 /// 2831 /// @returns A linked-list of statements. 2832 gpu_stmt *getStatements() { 2833 gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt, 2834 std::distance(S->begin(), S->end())); 2835 2836 int i = 0; 2837 for (auto &Stmt : *S) { 2838 gpu_stmt *GPUStmt = &Stmts[i]; 2839 2840 GPUStmt->id = Stmt.getDomainId().release(); 2841 2842 // We use the pet stmt pointer to keep track of the Polly statements. 2843 GPUStmt->stmt = (pet_stmt *)&Stmt; 2844 GPUStmt->accesses = getStmtAccesses(Stmt); 2845 i++; 2846 } 2847 2848 return Stmts; 2849 } 2850 2851 /// Derive the extent of an array. 2852 /// 2853 /// The extent of an array is the set of elements that are within the 2854 /// accessed array. For the inner dimensions, the extent constraints are 2855 /// 0 and the size of the corresponding array dimension. For the first 2856 /// (outermost) dimension, the extent constraints are the minimal and maximal 2857 /// subscript value for the first dimension. 2858 /// 2859 /// @param Array The array to derive the extent for. 2860 /// 2861 /// @returns An isl_set describing the extent of the array. 2862 isl::set getExtent(ScopArrayInfo *Array) { 2863 unsigned NumDims = Array->getNumberOfDimensions(); 2864 2865 if (Array->getNumberOfDimensions() == 0) 2866 return isl::set::universe(Array->getSpace()); 2867 2868 isl::union_map Accesses = S->getAccesses(Array); 2869 isl::union_set AccessUSet = Accesses.range(); 2870 AccessUSet = AccessUSet.coalesce(); 2871 AccessUSet = AccessUSet.detect_equalities(); 2872 AccessUSet = AccessUSet.coalesce(); 2873 2874 if (AccessUSet.is_empty()) 2875 return isl::set::empty(Array->getSpace()); 2876 2877 isl::set AccessSet = AccessUSet.extract_set(Array->getSpace()); 2878 2879 isl::local_space LS = isl::local_space(Array->getSpace()); 2880 2881 isl::pw_aff Val = isl::aff::var_on_domain(LS, isl::dim::set, 0); 2882 isl::pw_aff OuterMin = AccessSet.dim_min(0); 2883 isl::pw_aff OuterMax = AccessSet.dim_max(0); 2884 OuterMin = OuterMin.add_dims(isl::dim::in, Val.dim(isl::dim::in)); 2885 OuterMax = OuterMax.add_dims(isl::dim::in, Val.dim(isl::dim::in)); 2886 OuterMin = OuterMin.set_tuple_id(isl::dim::in, Array->getBasePtrId()); 2887 OuterMax = OuterMax.set_tuple_id(isl::dim::in, Array->getBasePtrId()); 2888 2889 isl::set Extent = isl::set::universe(Array->getSpace()); 2890 2891 Extent = Extent.intersect(OuterMin.le_set(Val)); 2892 Extent = Extent.intersect(OuterMax.ge_set(Val)); 2893 2894 for (unsigned i = 1; i < NumDims; ++i) 2895 Extent = Extent.lower_bound_si(isl::dim::set, i, 0); 2896 2897 for (unsigned i = 0; i < NumDims; ++i) { 2898 isl::pw_aff PwAff = Array->getDimensionSizePw(i); 2899 2900 // isl_pw_aff can be NULL for zero dimension. Only in the case of a 2901 // Fortran array will we have a legitimate dimension. 2902 if (PwAff.is_null()) { 2903 assert(i == 0 && "invalid dimension isl_pw_aff for nonzero dimension"); 2904 continue; 2905 } 2906 2907 isl::pw_aff Val = isl::aff::var_on_domain( 2908 isl::local_space(Array->getSpace()), isl::dim::set, i); 2909 PwAff = PwAff.add_dims(isl::dim::in, Val.dim(isl::dim::in)); 2910 PwAff = PwAff.set_tuple_id(isl::dim::in, Val.get_tuple_id(isl::dim::in)); 2911 isl::set Set = PwAff.gt_set(Val); 2912 Extent = Set.intersect(Extent); 2913 } 2914 2915 return Extent; 2916 } 2917 2918 /// Derive the bounds of an array. 2919 /// 2920 /// For the first dimension we derive the bound of the array from the extent 2921 /// of this dimension. For inner dimensions we obtain their size directly from 2922 /// ScopArrayInfo. 2923 /// 2924 /// @param PPCGArray The array to compute bounds for. 2925 /// @param Array The polly array from which to take the information. 2926 void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) { 2927 std::vector<isl_pw_aff *> Bounds; 2928 2929 if (PPCGArray.n_index > 0) { 2930 if (isl_set_is_empty(PPCGArray.extent)) { 2931 isl_set *Dom = isl_set_copy(PPCGArray.extent); 2932 isl_local_space *LS = isl_local_space_from_space( 2933 isl_space_params(isl_set_get_space(Dom))); 2934 isl_set_free(Dom); 2935 isl_pw_aff *Zero = isl_pw_aff_from_aff(isl_aff_zero_on_domain(LS)); 2936 Bounds.push_back(Zero); 2937 } else { 2938 isl_set *Dom = isl_set_copy(PPCGArray.extent); 2939 Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1); 2940 isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0); 2941 isl_set_free(Dom); 2942 Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound)); 2943 isl_local_space *LS = 2944 isl_local_space_from_space(isl_set_get_space(Dom)); 2945 isl_aff *One = isl_aff_zero_on_domain(LS); 2946 One = isl_aff_add_constant_si(One, 1); 2947 Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One)); 2948 Bound = isl_pw_aff_gist(Bound, S->getContext().release()); 2949 Bounds.push_back(Bound); 2950 } 2951 } 2952 2953 for (unsigned i = 1; i < PPCGArray.n_index; ++i) { 2954 isl_pw_aff *Bound = Array->getDimensionSizePw(i).release(); 2955 auto LS = isl_pw_aff_get_domain_space(Bound); 2956 auto Aff = isl_multi_aff_zero(LS); 2957 Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff); 2958 Bounds.push_back(Bound); 2959 } 2960 2961 /// To construct a `isl_multi_pw_aff`, we need all the indivisual `pw_aff` 2962 /// to have the same parameter dimensions. So, we need to align them to an 2963 /// appropriate space. 2964 /// Scop::Context is _not_ an appropriate space, because when we have 2965 /// `-polly-ignore-parameter-bounds` enabled, the Scop::Context does not 2966 /// contain all parameter dimensions. 2967 /// So, use the helper `alignPwAffs` to align all the `isl_pw_aff` together. 2968 isl_space *SeedAlignSpace = S->getParamSpace().release(); 2969 SeedAlignSpace = isl_space_add_dims(SeedAlignSpace, isl_dim_set, 1); 2970 2971 isl_space *AlignSpace = nullptr; 2972 std::vector<isl_pw_aff *> AlignedBounds; 2973 std::tie(AlignSpace, AlignedBounds) = 2974 alignPwAffs(std::move(Bounds), SeedAlignSpace); 2975 2976 assert(AlignSpace && "alignPwAffs did not initialise AlignSpace"); 2977 2978 isl_pw_aff_list *BoundsList = 2979 createPwAffList(S->getIslCtx(), std::move(AlignedBounds)); 2980 2981 isl_space *BoundsSpace = isl_set_get_space(PPCGArray.extent); 2982 BoundsSpace = isl_space_align_params(BoundsSpace, AlignSpace); 2983 2984 assert(BoundsSpace && "Unable to access space of array."); 2985 assert(BoundsList && "Unable to access list of bounds."); 2986 2987 PPCGArray.bound = 2988 isl_multi_pw_aff_from_pw_aff_list(BoundsSpace, BoundsList); 2989 assert(PPCGArray.bound && "PPCGArray.bound was not constructed correctly."); 2990 } 2991 2992 /// Create the arrays for @p PPCGProg. 2993 /// 2994 /// @param PPCGProg The program to compute the arrays for. 2995 void createArrays(gpu_prog *PPCGProg, 2996 const SmallVector<ScopArrayInfo *, 4> &ValidSAIs) { 2997 int i = 0; 2998 for (auto &Array : ValidSAIs) { 2999 std::string TypeName; 3000 raw_string_ostream OS(TypeName); 3001 3002 OS << *Array->getElementType(); 3003 TypeName = OS.str(); 3004 3005 gpu_array_info &PPCGArray = PPCGProg->array[i]; 3006 3007 PPCGArray.space = Array->getSpace().release(); 3008 PPCGArray.type = strdup(TypeName.c_str()); 3009 PPCGArray.size = DL->getTypeAllocSize(Array->getElementType()); 3010 PPCGArray.name = strdup(Array->getName().c_str()); 3011 PPCGArray.extent = nullptr; 3012 PPCGArray.n_index = Array->getNumberOfDimensions(); 3013 PPCGArray.extent = getExtent(Array).release(); 3014 PPCGArray.n_ref = 0; 3015 PPCGArray.refs = nullptr; 3016 PPCGArray.accessed = true; 3017 PPCGArray.read_only_scalar = 3018 Array->isReadOnly() && Array->getNumberOfDimensions() == 0; 3019 PPCGArray.has_compound_element = false; 3020 PPCGArray.local = false; 3021 PPCGArray.declare_local = false; 3022 PPCGArray.global = false; 3023 PPCGArray.linearize = false; 3024 PPCGArray.dep_order = nullptr; 3025 PPCGArray.user = Array; 3026 3027 PPCGArray.bound = nullptr; 3028 setArrayBounds(PPCGArray, Array); 3029 i++; 3030 3031 collect_references(PPCGProg, &PPCGArray); 3032 } 3033 } 3034 3035 /// Create an identity map between the arrays in the scop. 3036 /// 3037 /// @returns An identity map between the arrays in the scop. 3038 isl_union_map *getArrayIdentity() { 3039 isl_union_map *Maps = isl_union_map_empty(S->getParamSpace().release()); 3040 3041 for (auto &Array : S->arrays()) { 3042 isl_space *Space = Array->getSpace().release(); 3043 Space = isl_space_map_from_set(Space); 3044 isl_map *Identity = isl_map_identity(Space); 3045 Maps = isl_union_map_add_map(Maps, Identity); 3046 } 3047 3048 return Maps; 3049 } 3050 3051 /// Create a default-initialized PPCG GPU program. 3052 /// 3053 /// @returns A new gpu program description. 3054 gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) { 3055 3056 if (!PPCGScop) 3057 return nullptr; 3058 3059 auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog); 3060 3061 PPCGProg->ctx = S->getIslCtx(); 3062 PPCGProg->scop = PPCGScop; 3063 PPCGProg->context = isl_set_copy(PPCGScop->context); 3064 PPCGProg->read = isl_union_map_copy(PPCGScop->reads); 3065 PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes); 3066 PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes); 3067 PPCGProg->tagged_must_kill = 3068 isl_union_map_copy(PPCGScop->tagged_must_kills); 3069 PPCGProg->to_inner = getArrayIdentity(); 3070 PPCGProg->to_outer = getArrayIdentity(); 3071 // TODO: verify that this assignment is correct. 3072 PPCGProg->any_to_outer = nullptr; 3073 3074 // this needs to be set when live range reordering is enabled. 3075 // NOTE: I believe that is conservatively correct. I'm not sure 3076 // what the semantics of this is. 3077 // Quoting PPCG/gpu.h: "Order dependences on non-scalars." 3078 PPCGProg->array_order = 3079 isl_union_map_empty(isl_set_get_space(PPCGScop->context)); 3080 PPCGProg->n_stmts = std::distance(S->begin(), S->end()); 3081 PPCGProg->stmts = getStatements(); 3082 3083 // Only consider arrays that have a non-empty extent. 3084 // Otherwise, this will cause us to consider the following kinds of 3085 // empty arrays: 3086 // 1. Invariant loads that are represented by SAI objects. 3087 // 2. Arrays with statically known zero size. 3088 auto ValidSAIsRange = 3089 make_filter_range(S->arrays(), [this](ScopArrayInfo *SAI) -> bool { 3090 return !getExtent(SAI).is_empty(); 3091 }); 3092 SmallVector<ScopArrayInfo *, 4> ValidSAIs(ValidSAIsRange.begin(), 3093 ValidSAIsRange.end()); 3094 3095 PPCGProg->n_array = 3096 ValidSAIs.size(); // std::distance(S->array_begin(), S->array_end()); 3097 PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info, 3098 PPCGProg->n_array); 3099 3100 createArrays(PPCGProg, ValidSAIs); 3101 3102 PPCGProg->may_persist = compute_may_persist(PPCGProg); 3103 return PPCGProg; 3104 } 3105 3106 struct PrintGPUUserData { 3107 struct cuda_info *CudaInfo; 3108 struct gpu_prog *PPCGProg; 3109 std::vector<ppcg_kernel *> Kernels; 3110 }; 3111 3112 /// Print a user statement node in the host code. 3113 /// 3114 /// We use ppcg's printing facilities to print the actual statement and 3115 /// additionally build up a list of all kernels that are encountered in the 3116 /// host ast. 3117 /// 3118 /// @param P The printer to print to 3119 /// @param Options The printing options to use 3120 /// @param Node The node to print 3121 /// @param User A user pointer to carry additional data. This pointer is 3122 /// expected to be of type PrintGPUUserData. 3123 /// 3124 /// @returns A printer to which the output has been printed. 3125 static __isl_give isl_printer * 3126 printHostUser(__isl_take isl_printer *P, 3127 __isl_take isl_ast_print_options *Options, 3128 __isl_take isl_ast_node *Node, void *User) { 3129 auto Data = (struct PrintGPUUserData *)User; 3130 auto Id = isl_ast_node_get_annotation(Node); 3131 3132 if (Id) { 3133 bool IsUser = !strcmp(isl_id_get_name(Id), "user"); 3134 3135 // If this is a user statement, format it ourselves as ppcg would 3136 // otherwise try to call pet functionality that is not available in 3137 // Polly. 3138 if (IsUser) { 3139 P = isl_printer_start_line(P); 3140 P = isl_printer_print_ast_node(P, Node); 3141 P = isl_printer_end_line(P); 3142 isl_id_free(Id); 3143 isl_ast_print_options_free(Options); 3144 return P; 3145 } 3146 3147 auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id); 3148 isl_id_free(Id); 3149 Data->Kernels.push_back(Kernel); 3150 } 3151 3152 return print_host_user(P, Options, Node, User); 3153 } 3154 3155 /// Print C code corresponding to the control flow in @p Kernel. 3156 /// 3157 /// @param Kernel The kernel to print 3158 void printKernel(ppcg_kernel *Kernel) { 3159 auto *P = isl_printer_to_str(S->getIslCtx()); 3160 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 3161 auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 3162 P = isl_ast_node_print(Kernel->tree, P, Options); 3163 char *String = isl_printer_get_str(P); 3164 printf("%s\n", String); 3165 free(String); 3166 isl_printer_free(P); 3167 } 3168 3169 /// Print C code corresponding to the GPU code described by @p Tree. 3170 /// 3171 /// @param Tree An AST describing GPU code 3172 /// @param PPCGProg The PPCG program from which @Tree has been constructed. 3173 void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) { 3174 auto *P = isl_printer_to_str(S->getIslCtx()); 3175 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 3176 3177 PrintGPUUserData Data; 3178 Data.PPCGProg = PPCGProg; 3179 3180 auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 3181 Options = 3182 isl_ast_print_options_set_print_user(Options, printHostUser, &Data); 3183 P = isl_ast_node_print(Tree, P, Options); 3184 char *String = isl_printer_get_str(P); 3185 printf("# host\n"); 3186 printf("%s\n", String); 3187 free(String); 3188 isl_printer_free(P); 3189 3190 for (auto Kernel : Data.Kernels) { 3191 printf("# kernel%d\n", Kernel->id); 3192 printKernel(Kernel); 3193 } 3194 } 3195 3196 // Generate a GPU program using PPCG. 3197 // 3198 // GPU mapping consists of multiple steps: 3199 // 3200 // 1) Compute new schedule for the program. 3201 // 2) Map schedule to GPU (TODO) 3202 // 3) Generate code for new schedule (TODO) 3203 // 3204 // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer 3205 // is mostly CPU specific. Instead, we use PPCG's GPU code generation 3206 // strategy directly from this pass. 3207 gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) { 3208 3209 auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen); 3210 3211 PPCGGen->ctx = S->getIslCtx(); 3212 PPCGGen->options = PPCGScop->options; 3213 PPCGGen->print = nullptr; 3214 PPCGGen->print_user = nullptr; 3215 PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt; 3216 PPCGGen->prog = PPCGProg; 3217 PPCGGen->tree = nullptr; 3218 PPCGGen->types.n = 0; 3219 PPCGGen->types.name = nullptr; 3220 PPCGGen->sizes = nullptr; 3221 PPCGGen->used_sizes = nullptr; 3222 PPCGGen->kernel_id = 0; 3223 3224 // Set scheduling strategy to same strategy PPCG is using. 3225 isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true); 3226 isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true); 3227 isl_options_set_schedule_whole_component(PPCGGen->ctx, false); 3228 3229 isl_schedule *Schedule = get_schedule(PPCGGen); 3230 3231 /// Copy to and from device functions may introduce new parameters, which 3232 /// must be present in the schedule tree root for code generation. Hence, 3233 /// we ensure that all possible parameters are introduced from this point. 3234 if (!PollyManagedMemory) 3235 Schedule = 3236 isl_schedule_align_params(Schedule, S->getFullParamSpace().release()); 3237 3238 int has_permutable = has_any_permutable_node(Schedule); 3239 3240 if (!has_permutable || has_permutable < 0) { 3241 Schedule = isl_schedule_free(Schedule); 3242 DEBUG(dbgs() << getUniqueScopName(S) 3243 << " does not have permutable bands. Bailing out\n";); 3244 } else { 3245 const bool CreateTransferToFromDevice = !PollyManagedMemory; 3246 Schedule = map_to_device(PPCGGen, Schedule, CreateTransferToFromDevice); 3247 PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule)); 3248 } 3249 3250 if (DumpSchedule) { 3251 isl_printer *P = isl_printer_to_str(S->getIslCtx()); 3252 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); 3253 P = isl_printer_print_str(P, "Schedule\n"); 3254 P = isl_printer_print_str(P, "========\n"); 3255 if (Schedule) 3256 P = isl_printer_print_schedule(P, Schedule); 3257 else 3258 P = isl_printer_print_str(P, "No schedule found\n"); 3259 3260 printf("%s\n", isl_printer_get_str(P)); 3261 isl_printer_free(P); 3262 } 3263 3264 if (DumpCode) { 3265 printf("Code\n"); 3266 printf("====\n"); 3267 if (PPCGGen->tree) 3268 printGPUTree(PPCGGen->tree, PPCGProg); 3269 else 3270 printf("No code generated\n"); 3271 } 3272 3273 isl_schedule_free(Schedule); 3274 3275 return PPCGGen; 3276 } 3277 3278 /// Free gpu_gen structure. 3279 /// 3280 /// @param PPCGGen The ppcg_gen object to free. 3281 void freePPCGGen(gpu_gen *PPCGGen) { 3282 isl_ast_node_free(PPCGGen->tree); 3283 isl_union_map_free(PPCGGen->sizes); 3284 isl_union_map_free(PPCGGen->used_sizes); 3285 free(PPCGGen); 3286 } 3287 3288 /// Free the options in the ppcg scop structure. 3289 /// 3290 /// ppcg is not freeing these options for us. To avoid leaks we do this 3291 /// ourselves. 3292 /// 3293 /// @param PPCGScop The scop referencing the options to free. 3294 void freeOptions(ppcg_scop *PPCGScop) { 3295 free(PPCGScop->options->debug); 3296 PPCGScop->options->debug = nullptr; 3297 free(PPCGScop->options); 3298 PPCGScop->options = nullptr; 3299 } 3300 3301 /// Approximate the number of points in the set. 3302 /// 3303 /// This function returns an ast expression that overapproximates the number 3304 /// of points in an isl set through the rectangular hull surrounding this set. 3305 /// 3306 /// @param Set The set to count. 3307 /// @param Build The isl ast build object to use for creating the ast 3308 /// expression. 3309 /// 3310 /// @returns An approximation of the number of points in the set. 3311 __isl_give isl_ast_expr *approxPointsInSet(__isl_take isl_set *Set, 3312 __isl_keep isl_ast_build *Build) { 3313 3314 isl_val *One = isl_val_int_from_si(isl_set_get_ctx(Set), 1); 3315 auto *Expr = isl_ast_expr_from_val(isl_val_copy(One)); 3316 3317 isl_space *Space = isl_set_get_space(Set); 3318 Space = isl_space_params(Space); 3319 auto *Univ = isl_set_universe(Space); 3320 isl_pw_aff *OneAff = isl_pw_aff_val_on_domain(Univ, One); 3321 3322 for (long i = 0; i < isl_set_dim(Set, isl_dim_set); i++) { 3323 isl_pw_aff *Max = isl_set_dim_max(isl_set_copy(Set), i); 3324 isl_pw_aff *Min = isl_set_dim_min(isl_set_copy(Set), i); 3325 isl_pw_aff *DimSize = isl_pw_aff_sub(Max, Min); 3326 DimSize = isl_pw_aff_add(DimSize, isl_pw_aff_copy(OneAff)); 3327 auto DimSizeExpr = isl_ast_build_expr_from_pw_aff(Build, DimSize); 3328 Expr = isl_ast_expr_mul(Expr, DimSizeExpr); 3329 } 3330 3331 isl_set_free(Set); 3332 isl_pw_aff_free(OneAff); 3333 3334 return Expr; 3335 } 3336 3337 /// Approximate a number of dynamic instructions executed by a given 3338 /// statement. 3339 /// 3340 /// @param Stmt The statement for which to compute the number of dynamic 3341 /// instructions. 3342 /// @param Build The isl ast build object to use for creating the ast 3343 /// expression. 3344 /// @returns An approximation of the number of dynamic instructions executed 3345 /// by @p Stmt. 3346 __isl_give isl_ast_expr *approxDynamicInst(ScopStmt &Stmt, 3347 __isl_keep isl_ast_build *Build) { 3348 auto Iterations = approxPointsInSet(Stmt.getDomain().release(), Build); 3349 3350 long InstCount = 0; 3351 3352 if (Stmt.isBlockStmt()) { 3353 auto *BB = Stmt.getBasicBlock(); 3354 InstCount = std::distance(BB->begin(), BB->end()); 3355 } else { 3356 auto *R = Stmt.getRegion(); 3357 3358 for (auto *BB : R->blocks()) { 3359 InstCount += std::distance(BB->begin(), BB->end()); 3360 } 3361 } 3362 3363 isl_val *InstVal = isl_val_int_from_si(S->getIslCtx(), InstCount); 3364 auto *InstExpr = isl_ast_expr_from_val(InstVal); 3365 return isl_ast_expr_mul(InstExpr, Iterations); 3366 } 3367 3368 /// Approximate dynamic instructions executed in scop. 3369 /// 3370 /// @param S The scop for which to approximate dynamic instructions. 3371 /// @param Build The isl ast build object to use for creating the ast 3372 /// expression. 3373 /// @returns An approximation of the number of dynamic instructions executed 3374 /// in @p S. 3375 __isl_give isl_ast_expr * 3376 getNumberOfIterations(Scop &S, __isl_keep isl_ast_build *Build) { 3377 isl_ast_expr *Instructions; 3378 3379 isl_val *Zero = isl_val_int_from_si(S.getIslCtx(), 0); 3380 Instructions = isl_ast_expr_from_val(Zero); 3381 3382 for (ScopStmt &Stmt : S) { 3383 isl_ast_expr *StmtInstructions = approxDynamicInst(Stmt, Build); 3384 Instructions = isl_ast_expr_add(Instructions, StmtInstructions); 3385 } 3386 return Instructions; 3387 } 3388 3389 /// Create a check that ensures sufficient compute in scop. 3390 /// 3391 /// @param S The scop for which to ensure sufficient compute. 3392 /// @param Build The isl ast build object to use for creating the ast 3393 /// expression. 3394 /// @returns An expression that evaluates to TRUE in case of sufficient 3395 /// compute and to FALSE, otherwise. 3396 __isl_give isl_ast_expr * 3397 createSufficientComputeCheck(Scop &S, __isl_keep isl_ast_build *Build) { 3398 auto Iterations = getNumberOfIterations(S, Build); 3399 auto *MinComputeVal = isl_val_int_from_si(S.getIslCtx(), MinCompute); 3400 auto *MinComputeExpr = isl_ast_expr_from_val(MinComputeVal); 3401 return isl_ast_expr_ge(Iterations, MinComputeExpr); 3402 } 3403 3404 /// Check if the basic block contains a function we cannot codegen for GPU 3405 /// kernels. 3406 /// 3407 /// If this basic block does something with a `Function` other than calling 3408 /// a function that we support in a kernel, return true. 3409 bool containsInvalidKernelFunctionInBlock(const BasicBlock *BB, 3410 bool AllowCUDALibDevice) { 3411 for (const Instruction &Inst : *BB) { 3412 const CallInst *Call = dyn_cast<CallInst>(&Inst); 3413 if (Call && isValidFunctionInKernel(Call->getCalledFunction(), 3414 AllowCUDALibDevice)) { 3415 continue; 3416 } 3417 3418 for (Value *SrcVal : Inst.operands()) { 3419 PointerType *p = dyn_cast<PointerType>(SrcVal->getType()); 3420 if (!p) 3421 continue; 3422 if (isa<FunctionType>(p->getElementType())) 3423 return true; 3424 } 3425 } 3426 return false; 3427 } 3428 3429 /// Return whether the Scop S uses functions in a way that we do not support. 3430 bool containsInvalidKernelFunction(const Scop &S, bool AllowCUDALibDevice) { 3431 for (auto &Stmt : S) { 3432 if (Stmt.isBlockStmt()) { 3433 if (containsInvalidKernelFunctionInBlock(Stmt.getBasicBlock(), 3434 AllowCUDALibDevice)) 3435 return true; 3436 } else { 3437 assert(Stmt.isRegionStmt() && 3438 "Stmt was neither block nor region statement"); 3439 for (const BasicBlock *BB : Stmt.getRegion()->blocks()) 3440 if (containsInvalidKernelFunctionInBlock(BB, AllowCUDALibDevice)) 3441 return true; 3442 } 3443 } 3444 return false; 3445 } 3446 3447 /// Generate code for a given GPU AST described by @p Root. 3448 /// 3449 /// @param Root An isl_ast_node pointing to the root of the GPU AST. 3450 /// @param Prog The GPU Program to generate code for. 3451 void generateCode(__isl_take isl_ast_node *Root, gpu_prog *Prog) { 3452 ScopAnnotator Annotator; 3453 Annotator.buildAliasScopes(*S); 3454 3455 Region *R = &S->getRegion(); 3456 3457 simplifyRegion(R, DT, LI, RI); 3458 3459 BasicBlock *EnteringBB = R->getEnteringBlock(); 3460 3461 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 3462 3463 // Only build the run-time condition and parameters _after_ having 3464 // introduced the conditional branch. This is important as the conditional 3465 // branch will guard the original scop from new induction variables that 3466 // the SCEVExpander may introduce while code generating the parameters and 3467 // which may introduce scalar dependences that prevent us from correctly 3468 // code generating this scop. 3469 BBPair StartExitBlocks; 3470 BranchInst *CondBr = nullptr; 3471 std::tie(StartExitBlocks, CondBr) = 3472 executeScopConditionally(*S, Builder.getTrue(), *DT, *RI, *LI); 3473 BasicBlock *StartBlock = std::get<0>(StartExitBlocks); 3474 3475 assert(CondBr && "CondBr not initialized by executeScopConditionally"); 3476 3477 GPUNodeBuilder NodeBuilder(Builder, Annotator, *DL, *LI, *SE, *DT, *S, 3478 StartBlock, Prog, Runtime, Architecture); 3479 3480 // TODO: Handle LICM 3481 auto SplitBlock = StartBlock->getSinglePredecessor(); 3482 Builder.SetInsertPoint(SplitBlock->getTerminator()); 3483 3484 isl_ast_build *Build = isl_ast_build_alloc(S->getIslCtx()); 3485 isl_ast_expr *Condition = IslAst::buildRunCondition(*S, Build); 3486 isl_ast_expr *SufficientCompute = createSufficientComputeCheck(*S, Build); 3487 Condition = isl_ast_expr_and(Condition, SufficientCompute); 3488 isl_ast_build_free(Build); 3489 3490 // preload invariant loads. Note: This should happen before the RTC 3491 // because the RTC may depend on values that are invariant load hoisted. 3492 if (!NodeBuilder.preloadInvariantLoads()) { 3493 DEBUG(dbgs() << "preloading invariant loads failed in function: " + 3494 S->getFunction().getName() + 3495 " | Scop Region: " + S->getNameStr()); 3496 // adjust the dominator tree accordingly. 3497 auto *ExitingBlock = StartBlock->getUniqueSuccessor(); 3498 assert(ExitingBlock); 3499 auto *MergeBlock = ExitingBlock->getUniqueSuccessor(); 3500 assert(MergeBlock); 3501 polly::markBlockUnreachable(*StartBlock, Builder); 3502 polly::markBlockUnreachable(*ExitingBlock, Builder); 3503 auto *ExitingBB = S->getExitingBlock(); 3504 assert(ExitingBB); 3505 3506 DT->changeImmediateDominator(MergeBlock, ExitingBB); 3507 DT->eraseNode(ExitingBlock); 3508 isl_ast_expr_free(Condition); 3509 isl_ast_node_free(Root); 3510 } else { 3511 3512 NodeBuilder.addParameters(S->getContext().release()); 3513 Value *RTC = NodeBuilder.createRTC(Condition); 3514 Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC); 3515 3516 Builder.SetInsertPoint(&*StartBlock->begin()); 3517 3518 NodeBuilder.create(Root); 3519 } 3520 3521 /// In case a sequential kernel has more surrounding loops as any parallel 3522 /// kernel, the SCoP is probably mostly sequential. Hence, there is no 3523 /// point in running it on a GPU. 3524 if (NodeBuilder.DeepestSequential > NodeBuilder.DeepestParallel) 3525 CondBr->setOperand(0, Builder.getFalse()); 3526 3527 if (!NodeBuilder.BuildSuccessful) 3528 CondBr->setOperand(0, Builder.getFalse()); 3529 } 3530 3531 bool runOnScop(Scop &CurrentScop) override { 3532 S = &CurrentScop; 3533 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 3534 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 3535 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 3536 DL = &S->getRegion().getEntry()->getModule()->getDataLayout(); 3537 RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 3538 3539 DEBUG(dbgs() << "PPCGCodeGen running on : " << getUniqueScopName(S) 3540 << " | loop depth: " << S->getMaxLoopDepth() << "\n"); 3541 3542 // We currently do not support functions other than intrinsics inside 3543 // kernels, as code generation will need to offload function calls to the 3544 // kernel. This may lead to a kernel trying to call a function on the host. 3545 // This also allows us to prevent codegen from trying to take the 3546 // address of an intrinsic function to send to the kernel. 3547 if (containsInvalidKernelFunction(CurrentScop, 3548 Architecture == GPUArch::NVPTX64)) { 3549 DEBUG( 3550 dbgs() << getUniqueScopName(S) 3551 << " contains function which cannot be materialised in a GPU " 3552 "kernel. Bailing out.\n";); 3553 return false; 3554 } 3555 3556 auto PPCGScop = createPPCGScop(); 3557 auto PPCGProg = createPPCGProg(PPCGScop); 3558 auto PPCGGen = generateGPU(PPCGScop, PPCGProg); 3559 3560 if (PPCGGen->tree) { 3561 generateCode(isl_ast_node_copy(PPCGGen->tree), PPCGProg); 3562 CurrentScop.markAsToBeSkipped(); 3563 } else { 3564 DEBUG(dbgs() << getUniqueScopName(S) 3565 << " has empty PPCGGen->tree. Bailing out.\n"); 3566 } 3567 3568 freeOptions(PPCGScop); 3569 freePPCGGen(PPCGGen); 3570 gpu_prog_free(PPCGProg); 3571 ppcg_scop_free(PPCGScop); 3572 3573 return true; 3574 } 3575 3576 void printScop(raw_ostream &, Scop &) const override {} 3577 3578 void getAnalysisUsage(AnalysisUsage &AU) const override { 3579 AU.addRequired<DominatorTreeWrapperPass>(); 3580 AU.addRequired<RegionInfoPass>(); 3581 AU.addRequired<ScalarEvolutionWrapperPass>(); 3582 AU.addRequired<ScopDetectionWrapperPass>(); 3583 AU.addRequired<ScopInfoRegionPass>(); 3584 AU.addRequired<LoopInfoWrapperPass>(); 3585 3586 AU.addPreserved<AAResultsWrapperPass>(); 3587 AU.addPreserved<BasicAAWrapperPass>(); 3588 AU.addPreserved<LoopInfoWrapperPass>(); 3589 AU.addPreserved<DominatorTreeWrapperPass>(); 3590 AU.addPreserved<GlobalsAAWrapperPass>(); 3591 AU.addPreserved<ScopDetectionWrapperPass>(); 3592 AU.addPreserved<ScalarEvolutionWrapperPass>(); 3593 AU.addPreserved<SCEVAAWrapperPass>(); 3594 3595 // FIXME: We do not yet add regions for the newly generated code to the 3596 // region tree. 3597 AU.addPreserved<RegionInfoPass>(); 3598 AU.addPreserved<ScopInfoRegionPass>(); 3599 } 3600 }; 3601 } // namespace 3602 3603 char PPCGCodeGeneration::ID = 1; 3604 3605 Pass *polly::createPPCGCodeGenerationPass(GPUArch Arch, GPURuntime Runtime) { 3606 PPCGCodeGeneration *generator = new PPCGCodeGeneration(); 3607 generator->Runtime = Runtime; 3608 generator->Architecture = Arch; 3609 return generator; 3610 } 3611 3612 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg", 3613 "Polly - Apply PPCG translation to SCOP", false, false) 3614 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 3615 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 3616 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 3617 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 3618 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 3619 INITIALIZE_PASS_DEPENDENCY(ScopDetectionWrapperPass); 3620 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg", 3621 "Polly - Apply PPCG translation to SCOP", false, false) 3622