19dfe4e7cSTobias Grosser //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===// 29dfe4e7cSTobias Grosser // 39dfe4e7cSTobias Grosser // The LLVM Compiler Infrastructure 49dfe4e7cSTobias Grosser // 59dfe4e7cSTobias Grosser // This file is distributed under the University of Illinois Open Source 69dfe4e7cSTobias Grosser // License. See LICENSE.TXT for details. 79dfe4e7cSTobias Grosser // 89dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===// 99dfe4e7cSTobias Grosser // 109dfe4e7cSTobias Grosser // Take a scop created by ScopInfo and map it to GPU code using the ppcg 119dfe4e7cSTobias Grosser // GPU mapping strategy. 129dfe4e7cSTobias Grosser // 139dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===// 149dfe4e7cSTobias Grosser 159dfe4e7cSTobias Grosser #include "polly/CodeGen/IslNodeBuilder.h" 1638fc0aedSTobias Grosser #include "polly/CodeGen/Utils.h" 179dfe4e7cSTobias Grosser #include "polly/DependenceInfo.h" 189dfe4e7cSTobias Grosser #include "polly/LinkAllPasses.h" 19f384594dSTobias Grosser #include "polly/Options.h" 209dfe4e7cSTobias Grosser #include "polly/ScopInfo.h" 219dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h" 229dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h" 239dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h" 249dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h" 259dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 269dfe4e7cSTobias Grosser 27f384594dSTobias Grosser #include "isl/union_map.h" 28f384594dSTobias Grosser 29e938517eSTobias Grosser extern "C" { 30a56f8f8eSTobias Grosser #include "ppcg/cuda.h" 31a56f8f8eSTobias Grosser #include "ppcg/gpu.h" 32a56f8f8eSTobias Grosser #include "ppcg/gpu_print.h" 33a56f8f8eSTobias Grosser #include "ppcg/ppcg.h" 34a56f8f8eSTobias Grosser #include "ppcg/schedule.h" 35e938517eSTobias Grosser } 36e938517eSTobias Grosser 379dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h" 389dfe4e7cSTobias Grosser 399dfe4e7cSTobias Grosser using namespace polly; 409dfe4e7cSTobias Grosser using namespace llvm; 419dfe4e7cSTobias Grosser 429dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg" 439dfe4e7cSTobias Grosser 44f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule", 45f384594dSTobias Grosser cl::desc("Dump the computed GPU Schedule"), 46681bd568STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore, 47f384594dSTobias Grosser cl::cat(PollyCategory)); 4869b46751STobias Grosser 4969b46751STobias Grosser static cl::opt<bool> 5069b46751STobias Grosser DumpCode("polly-acc-dump-code", 5169b46751STobias Grosser cl::desc("Dump C code describing the GPU mapping"), cl::Hidden, 5269b46751STobias Grosser cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 5369b46751STobias Grosser 5432837fe3STobias Grosser static cl::opt<bool> DumpKernelIR("polly-acc-dump-kernel-ir", 5532837fe3STobias Grosser cl::desc("Dump the kernel LLVM-IR"), 5632837fe3STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore, 5732837fe3STobias Grosser cl::cat(PollyCategory)); 5832837fe3STobias Grosser 5960c60025STobias Grosser /// Create the ast expressions for a ScopStmt. 6060c60025STobias Grosser /// 6160c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each 6260c60025STobias Grosser /// of the scheduled ScopStmts. 6360c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt( 6460c60025STobias Grosser void *Stmt, isl_ast_build *Build, 6560c60025STobias Grosser isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA, 6660c60025STobias Grosser isl_id *Id, void *User), 6760c60025STobias Grosser void *UserIndex, 6860c60025STobias Grosser isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User), 6960c60025STobias Grosser void *User_expr) { 7060c60025STobias Grosser 7160c60025STobias Grosser // TODO: Implement the AST expression generation. For now we just return a 7260c60025STobias Grosser // nullptr to ensure that we do not free uninitialized pointers. 7360c60025STobias Grosser 7460c60025STobias Grosser return nullptr; 7560c60025STobias Grosser } 76f384594dSTobias Grosser 7738fc0aedSTobias Grosser /// Generate code for a GPU specific isl AST. 7838fc0aedSTobias Grosser /// 7938fc0aedSTobias Grosser /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which 8038fc0aedSTobias Grosser /// generates code for general-prupose AST nodes, with special functionality 8138fc0aedSTobias Grosser /// for generating GPU specific user nodes. 8238fc0aedSTobias Grosser /// 8338fc0aedSTobias Grosser /// @see GPUNodeBuilder::createUser 8438fc0aedSTobias Grosser class GPUNodeBuilder : public IslNodeBuilder { 8538fc0aedSTobias Grosser public: 8638fc0aedSTobias Grosser GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P, 8738fc0aedSTobias Grosser const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE, 8832837fe3STobias Grosser DominatorTree &DT, Scop &S, gpu_prog *Prog) 8932837fe3STobias Grosser : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S), Prog(Prog) {} 9038fc0aedSTobias Grosser 9138fc0aedSTobias Grosser private: 9232837fe3STobias Grosser /// A module containing GPU code. 9332837fe3STobias Grosser /// 9432837fe3STobias Grosser /// This pointer is only set in case we are currently generating GPU code. 9532837fe3STobias Grosser std::unique_ptr<Module> GPUModule; 9632837fe3STobias Grosser 9732837fe3STobias Grosser /// The GPU program we generate code for. 9832837fe3STobias Grosser gpu_prog *Prog; 9932837fe3STobias Grosser 100472f9654STobias Grosser /// Class to free isl_ids. 101472f9654STobias Grosser class IslIdDeleter { 102472f9654STobias Grosser public: 103472f9654STobias Grosser void operator()(__isl_take isl_id *Id) { isl_id_free(Id); }; 104472f9654STobias Grosser }; 105472f9654STobias Grosser 106472f9654STobias Grosser /// A set containing all isl_ids allocated in a GPU kernel. 107472f9654STobias Grosser /// 108472f9654STobias Grosser /// By releasing this set all isl_ids will be freed. 109472f9654STobias Grosser std::set<std::unique_ptr<isl_id, IslIdDeleter>> KernelIDs; 110472f9654STobias Grosser 11138fc0aedSTobias Grosser /// Create code for user-defined AST nodes. 11238fc0aedSTobias Grosser /// 11338fc0aedSTobias Grosser /// These AST nodes can be of type: 11438fc0aedSTobias Grosser /// 11538fc0aedSTobias Grosser /// - ScopStmt: A computational statement (TODO) 11638fc0aedSTobias Grosser /// - Kernel: A GPU kernel call (TODO) 11738fc0aedSTobias Grosser /// - Data-Transfer: A GPU <-> CPU data-transfer (TODO) 11838fc0aedSTobias Grosser /// 1191fb9b64dSTobias Grosser /// @param UserStmt The ast node to generate code for. 1201fb9b64dSTobias Grosser virtual void createUser(__isl_take isl_ast_node *UserStmt); 12132837fe3STobias Grosser 12232837fe3STobias Grosser /// Create GPU kernel. 12332837fe3STobias Grosser /// 12432837fe3STobias Grosser /// Code generate the kernel described by @p KernelStmt. 12532837fe3STobias Grosser /// 12632837fe3STobias Grosser /// @param KernelStmt The ast node to generate kernel code for. 12732837fe3STobias Grosser void createKernel(__isl_take isl_ast_node *KernelStmt); 12832837fe3STobias Grosser 12932837fe3STobias Grosser /// Create kernel function. 13032837fe3STobias Grosser /// 13132837fe3STobias Grosser /// Create a kernel function located in a newly created module that can serve 13232837fe3STobias Grosser /// as target for device code generation. Set the Builder to point to the 13332837fe3STobias Grosser /// start block of this newly created function. 13432837fe3STobias Grosser /// 13532837fe3STobias Grosser /// @param Kernel The kernel to generate code for. 13632837fe3STobias Grosser void createKernelFunction(ppcg_kernel *Kernel); 13732837fe3STobias Grosser 13832837fe3STobias Grosser /// Create the declaration of a kernel function. 13932837fe3STobias Grosser /// 14032837fe3STobias Grosser /// The kernel function takes as arguments: 14132837fe3STobias Grosser /// 14232837fe3STobias Grosser /// - One i8 pointer for each external array reference used in the kernel. 143f6044bd0STobias Grosser /// - Host iterators 144c84a1995STobias Grosser /// - Parameters 14532837fe3STobias Grosser /// - Other LLVM Value references (TODO) 14632837fe3STobias Grosser /// 14732837fe3STobias Grosser /// @param Kernel The kernel to generate the function declaration for. 14832837fe3STobias Grosser /// @returns The newly declared function. 14932837fe3STobias Grosser Function *createKernelFunctionDecl(ppcg_kernel *Kernel); 15032837fe3STobias Grosser 151472f9654STobias Grosser /// Insert intrinsic functions to obtain thread and block ids. 152472f9654STobias Grosser /// 153472f9654STobias Grosser /// @param The kernel to generate the intrinsic functions for. 154472f9654STobias Grosser void insertKernelIntrinsics(ppcg_kernel *Kernel); 155472f9654STobias Grosser 15632837fe3STobias Grosser /// Finalize the generation of the kernel function. 15732837fe3STobias Grosser /// 15832837fe3STobias Grosser /// Free the LLVM-IR module corresponding to the kernel and -- if requested -- 15932837fe3STobias Grosser /// dump its IR to stderr. 16032837fe3STobias Grosser void finalizeKernelFunction(); 1611fb9b64dSTobias Grosser }; 1621fb9b64dSTobias Grosser 1631fb9b64dSTobias Grosser void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) { 16432837fe3STobias Grosser isl_ast_expr *Expr = isl_ast_node_user_get_expr(UserStmt); 16532837fe3STobias Grosser isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); 16632837fe3STobias Grosser isl_id *Id = isl_ast_expr_get_id(StmtExpr); 16732837fe3STobias Grosser isl_id_free(Id); 16832837fe3STobias Grosser isl_ast_expr_free(StmtExpr); 16932837fe3STobias Grosser 17032837fe3STobias Grosser const char *Str = isl_id_get_name(Id); 17132837fe3STobias Grosser if (!strcmp(Str, "kernel")) { 17232837fe3STobias Grosser createKernel(UserStmt); 17332837fe3STobias Grosser isl_ast_expr_free(Expr); 17432837fe3STobias Grosser return; 17532837fe3STobias Grosser } 17632837fe3STobias Grosser 17732837fe3STobias Grosser isl_ast_expr_free(Expr); 1781fb9b64dSTobias Grosser isl_ast_node_free(UserStmt); 17938fc0aedSTobias Grosser return; 18038fc0aedSTobias Grosser } 18138fc0aedSTobias Grosser 18232837fe3STobias Grosser void GPUNodeBuilder::createKernel(__isl_take isl_ast_node *KernelStmt) { 18332837fe3STobias Grosser isl_id *Id = isl_ast_node_get_annotation(KernelStmt); 18432837fe3STobias Grosser ppcg_kernel *Kernel = (ppcg_kernel *)isl_id_get_user(Id); 18532837fe3STobias Grosser isl_id_free(Id); 18632837fe3STobias Grosser isl_ast_node_free(KernelStmt); 18732837fe3STobias Grosser 18832837fe3STobias Grosser assert(Kernel->tree && "Device AST of kernel node is empty"); 18932837fe3STobias Grosser 19032837fe3STobias Grosser Instruction &HostInsertPoint = *Builder.GetInsertPoint(); 191472f9654STobias Grosser IslExprBuilder::IDToValueTy HostIDs = IDToValue; 19232837fe3STobias Grosser 19332837fe3STobias Grosser createKernelFunction(Kernel); 19432837fe3STobias Grosser 195*59ab0705STobias Grosser create(isl_ast_node_copy(Kernel->tree)); 196*59ab0705STobias Grosser 19732837fe3STobias Grosser Builder.SetInsertPoint(&HostInsertPoint); 198472f9654STobias Grosser IDToValue = HostIDs; 19932837fe3STobias Grosser 20032837fe3STobias Grosser finalizeKernelFunction(); 20132837fe3STobias Grosser } 20232837fe3STobias Grosser 20332837fe3STobias Grosser /// Compute the DataLayout string for the NVPTX backend. 20432837fe3STobias Grosser /// 20532837fe3STobias Grosser /// @param is64Bit Are we looking for a 64 bit architecture? 20632837fe3STobias Grosser static std::string computeNVPTXDataLayout(bool is64Bit) { 20732837fe3STobias Grosser std::string Ret = "e"; 20832837fe3STobias Grosser 20932837fe3STobias Grosser if (!is64Bit) 21032837fe3STobias Grosser Ret += "-p:32:32"; 21132837fe3STobias Grosser 21232837fe3STobias Grosser Ret += "-i64:64-v16:16-v32:32-n16:32:64"; 21332837fe3STobias Grosser 21432837fe3STobias Grosser return Ret; 21532837fe3STobias Grosser } 21632837fe3STobias Grosser 21732837fe3STobias Grosser Function *GPUNodeBuilder::createKernelFunctionDecl(ppcg_kernel *Kernel) { 21832837fe3STobias Grosser std::vector<Type *> Args; 21932837fe3STobias Grosser std::string Identifier = "kernel_" + std::to_string(Kernel->id); 22032837fe3STobias Grosser 22132837fe3STobias Grosser for (long i = 0; i < Prog->n_array; i++) { 22232837fe3STobias Grosser if (!ppcg_kernel_requires_array_argument(Kernel, i)) 22332837fe3STobias Grosser continue; 22432837fe3STobias Grosser 22532837fe3STobias Grosser Args.push_back(Builder.getInt8PtrTy()); 22632837fe3STobias Grosser } 22732837fe3STobias Grosser 228f6044bd0STobias Grosser int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set); 229f6044bd0STobias Grosser 230f6044bd0STobias Grosser for (long i = 0; i < NumHostIters; i++) 231f6044bd0STobias Grosser Args.push_back(Builder.getInt64Ty()); 232f6044bd0STobias Grosser 233c84a1995STobias Grosser int NumVars = isl_space_dim(Kernel->space, isl_dim_param); 234c84a1995STobias Grosser 235c84a1995STobias Grosser for (long i = 0; i < NumVars; i++) 236c84a1995STobias Grosser Args.push_back(Builder.getInt64Ty()); 237c84a1995STobias Grosser 23832837fe3STobias Grosser auto *FT = FunctionType::get(Builder.getVoidTy(), Args, false); 23932837fe3STobias Grosser auto *FN = Function::Create(FT, Function::ExternalLinkage, Identifier, 24032837fe3STobias Grosser GPUModule.get()); 24132837fe3STobias Grosser FN->setCallingConv(CallingConv::PTX_Kernel); 24232837fe3STobias Grosser 24332837fe3STobias Grosser auto Arg = FN->arg_begin(); 24432837fe3STobias Grosser for (long i = 0; i < Kernel->n_array; i++) { 24532837fe3STobias Grosser if (!ppcg_kernel_requires_array_argument(Kernel, i)) 24632837fe3STobias Grosser continue; 24732837fe3STobias Grosser 24832837fe3STobias Grosser Arg->setName(Prog->array[i].name); 24932837fe3STobias Grosser Arg++; 25032837fe3STobias Grosser } 25132837fe3STobias Grosser 252f6044bd0STobias Grosser for (long i = 0; i < NumHostIters; i++) { 253f6044bd0STobias Grosser isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i); 254f6044bd0STobias Grosser Arg->setName(isl_id_get_name(Id)); 255f6044bd0STobias Grosser IDToValue[Id] = &*Arg; 256f6044bd0STobias Grosser KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 257f6044bd0STobias Grosser Arg++; 258f6044bd0STobias Grosser } 259f6044bd0STobias Grosser 260c84a1995STobias Grosser for (long i = 0; i < NumVars; i++) { 261c84a1995STobias Grosser isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i); 262c84a1995STobias Grosser Arg->setName(isl_id_get_name(Id)); 263c84a1995STobias Grosser IDToValue[Id] = &*Arg; 264c84a1995STobias Grosser KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 265c84a1995STobias Grosser Arg++; 266c84a1995STobias Grosser } 267c84a1995STobias Grosser 26832837fe3STobias Grosser return FN; 26932837fe3STobias Grosser } 27032837fe3STobias Grosser 271472f9654STobias Grosser void GPUNodeBuilder::insertKernelIntrinsics(ppcg_kernel *Kernel) { 272472f9654STobias Grosser Intrinsic::ID IntrinsicsBID[] = {Intrinsic::nvvm_read_ptx_sreg_ctaid_x, 273472f9654STobias Grosser Intrinsic::nvvm_read_ptx_sreg_ctaid_y}; 274472f9654STobias Grosser 275472f9654STobias Grosser Intrinsic::ID IntrinsicsTID[] = {Intrinsic::nvvm_read_ptx_sreg_tid_x, 276472f9654STobias Grosser Intrinsic::nvvm_read_ptx_sreg_tid_y, 277472f9654STobias Grosser Intrinsic::nvvm_read_ptx_sreg_tid_z}; 278472f9654STobias Grosser 279472f9654STobias Grosser auto addId = [this](__isl_take isl_id *Id, Intrinsic::ID Intr) mutable { 280472f9654STobias Grosser std::string Name = isl_id_get_name(Id); 281472f9654STobias Grosser Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 282472f9654STobias Grosser Function *IntrinsicFn = Intrinsic::getDeclaration(M, Intr); 283472f9654STobias Grosser Value *Val = Builder.CreateCall(IntrinsicFn, {}); 284472f9654STobias Grosser Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name); 285472f9654STobias Grosser IDToValue[Id] = Val; 286472f9654STobias Grosser KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id)); 287472f9654STobias Grosser }; 288472f9654STobias Grosser 289472f9654STobias Grosser for (int i = 0; i < Kernel->n_grid; ++i) { 290472f9654STobias Grosser isl_id *Id = isl_id_list_get_id(Kernel->block_ids, i); 291472f9654STobias Grosser addId(Id, IntrinsicsBID[i]); 292472f9654STobias Grosser } 293472f9654STobias Grosser 294472f9654STobias Grosser for (int i = 0; i < Kernel->n_block; ++i) { 295472f9654STobias Grosser isl_id *Id = isl_id_list_get_id(Kernel->thread_ids, i); 296472f9654STobias Grosser addId(Id, IntrinsicsTID[i]); 297472f9654STobias Grosser } 298472f9654STobias Grosser } 299472f9654STobias Grosser 30032837fe3STobias Grosser void GPUNodeBuilder::createKernelFunction(ppcg_kernel *Kernel) { 30132837fe3STobias Grosser 30232837fe3STobias Grosser std::string Identifier = "kernel_" + std::to_string(Kernel->id); 30332837fe3STobias Grosser GPUModule.reset(new Module(Identifier, Builder.getContext())); 30432837fe3STobias Grosser GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda")); 30532837fe3STobias Grosser GPUModule->setDataLayout(computeNVPTXDataLayout(true /* is64Bit */)); 30632837fe3STobias Grosser 30732837fe3STobias Grosser Function *FN = createKernelFunctionDecl(Kernel); 30832837fe3STobias Grosser 309*59ab0705STobias Grosser BasicBlock *PrevBlock = Builder.GetInsertBlock(); 31032837fe3STobias Grosser auto EntryBlock = BasicBlock::Create(Builder.getContext(), "entry", FN); 31132837fe3STobias Grosser 312*59ab0705STobias Grosser DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 313*59ab0705STobias Grosser DT.addNewBlock(EntryBlock, PrevBlock); 314*59ab0705STobias Grosser 31532837fe3STobias Grosser Builder.SetInsertPoint(EntryBlock); 31632837fe3STobias Grosser Builder.CreateRetVoid(); 31732837fe3STobias Grosser Builder.SetInsertPoint(EntryBlock, EntryBlock->begin()); 318472f9654STobias Grosser 319472f9654STobias Grosser insertKernelIntrinsics(Kernel); 32032837fe3STobias Grosser } 32132837fe3STobias Grosser 32232837fe3STobias Grosser void GPUNodeBuilder::finalizeKernelFunction() { 32332837fe3STobias Grosser 32432837fe3STobias Grosser if (DumpKernelIR) 32532837fe3STobias Grosser outs() << *GPUModule << "\n"; 32632837fe3STobias Grosser 32732837fe3STobias Grosser GPUModule.release(); 328472f9654STobias Grosser KernelIDs.clear(); 32932837fe3STobias Grosser } 33032837fe3STobias Grosser 3319dfe4e7cSTobias Grosser namespace { 3329dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass { 3339dfe4e7cSTobias Grosser public: 3349dfe4e7cSTobias Grosser static char ID; 3359dfe4e7cSTobias Grosser 336e938517eSTobias Grosser /// The scop that is currently processed. 337e938517eSTobias Grosser Scop *S; 338e938517eSTobias Grosser 33938fc0aedSTobias Grosser LoopInfo *LI; 34038fc0aedSTobias Grosser DominatorTree *DT; 34138fc0aedSTobias Grosser ScalarEvolution *SE; 34238fc0aedSTobias Grosser const DataLayout *DL; 34338fc0aedSTobias Grosser RegionInfo *RI; 34438fc0aedSTobias Grosser 3459dfe4e7cSTobias Grosser PPCGCodeGeneration() : ScopPass(ID) {} 3469dfe4e7cSTobias Grosser 347e938517eSTobias Grosser /// Construct compilation options for PPCG. 348e938517eSTobias Grosser /// 349e938517eSTobias Grosser /// @returns The compilation options. 350e938517eSTobias Grosser ppcg_options *createPPCGOptions() { 351e938517eSTobias Grosser auto DebugOptions = 352e938517eSTobias Grosser (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options)); 353e938517eSTobias Grosser auto Options = (ppcg_options *)malloc(sizeof(ppcg_options)); 354e938517eSTobias Grosser 355e938517eSTobias Grosser DebugOptions->dump_schedule_constraints = false; 356e938517eSTobias Grosser DebugOptions->dump_schedule = false; 357e938517eSTobias Grosser DebugOptions->dump_final_schedule = false; 358e938517eSTobias Grosser DebugOptions->dump_sizes = false; 359e938517eSTobias Grosser 360e938517eSTobias Grosser Options->debug = DebugOptions; 361e938517eSTobias Grosser 362e938517eSTobias Grosser Options->reschedule = true; 363e938517eSTobias Grosser Options->scale_tile_loops = false; 364e938517eSTobias Grosser Options->wrap = false; 365e938517eSTobias Grosser 366e938517eSTobias Grosser Options->non_negative_parameters = false; 367e938517eSTobias Grosser Options->ctx = nullptr; 368e938517eSTobias Grosser Options->sizes = nullptr; 369e938517eSTobias Grosser 3704eaedde5STobias Grosser Options->tile_size = 32; 3714eaedde5STobias Grosser 372e938517eSTobias Grosser Options->use_private_memory = false; 373e938517eSTobias Grosser Options->use_shared_memory = false; 374e938517eSTobias Grosser Options->max_shared_memory = 0; 375e938517eSTobias Grosser 376e938517eSTobias Grosser Options->target = PPCG_TARGET_CUDA; 377e938517eSTobias Grosser Options->openmp = false; 378e938517eSTobias Grosser Options->linearize_device_arrays = true; 379e938517eSTobias Grosser Options->live_range_reordering = false; 380e938517eSTobias Grosser 381e938517eSTobias Grosser Options->opencl_compiler_options = nullptr; 382e938517eSTobias Grosser Options->opencl_use_gpu = false; 383e938517eSTobias Grosser Options->opencl_n_include_file = 0; 384e938517eSTobias Grosser Options->opencl_include_files = nullptr; 385e938517eSTobias Grosser Options->opencl_print_kernel_types = false; 386e938517eSTobias Grosser Options->opencl_embed_kernel_code = false; 387e938517eSTobias Grosser 388e938517eSTobias Grosser Options->save_schedule_file = nullptr; 389e938517eSTobias Grosser Options->load_schedule_file = nullptr; 390e938517eSTobias Grosser 391e938517eSTobias Grosser return Options; 392e938517eSTobias Grosser } 393e938517eSTobias Grosser 394f384594dSTobias Grosser /// Get a tagged access relation containing all accesses of type @p AccessTy. 395f384594dSTobias Grosser /// 396f384594dSTobias Grosser /// Instead of a normal access of the form: 397f384594dSTobias Grosser /// 398f384594dSTobias Grosser /// Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)] 399f384594dSTobias Grosser /// 400f384594dSTobias Grosser /// a tagged access has the form 401f384594dSTobias Grosser /// 402f384594dSTobias Grosser /// [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)] 403f384594dSTobias Grosser /// 404f384594dSTobias Grosser /// where 'id' is an additional space that references the memory access that 405f384594dSTobias Grosser /// triggered the access. 406f384594dSTobias Grosser /// 407f384594dSTobias Grosser /// @param AccessTy The type of the memory accesses to collect. 408f384594dSTobias Grosser /// 409f384594dSTobias Grosser /// @return The relation describing all tagged memory accesses. 410f384594dSTobias Grosser isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) { 411f384594dSTobias Grosser isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace()); 412f384594dSTobias Grosser 413f384594dSTobias Grosser for (auto &Stmt : *S) 414f384594dSTobias Grosser for (auto &Acc : Stmt) 415f384594dSTobias Grosser if (Acc->getType() == AccessTy) { 416f384594dSTobias Grosser isl_map *Relation = Acc->getAccessRelation(); 417f384594dSTobias Grosser Relation = isl_map_intersect_domain(Relation, Stmt.getDomain()); 418f384594dSTobias Grosser 419f384594dSTobias Grosser isl_space *Space = isl_map_get_space(Relation); 420f384594dSTobias Grosser Space = isl_space_range(Space); 421f384594dSTobias Grosser Space = isl_space_from_range(Space); 4226293ba69STobias Grosser Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId()); 423f384594dSTobias Grosser isl_map *Universe = isl_map_universe(Space); 424f384594dSTobias Grosser Relation = isl_map_domain_product(Relation, Universe); 425f384594dSTobias Grosser Accesses = isl_union_map_add_map(Accesses, Relation); 426f384594dSTobias Grosser } 427f384594dSTobias Grosser 428f384594dSTobias Grosser return Accesses; 429f384594dSTobias Grosser } 430f384594dSTobias Grosser 431f384594dSTobias Grosser /// Get the set of all read accesses, tagged with the access id. 432f384594dSTobias Grosser /// 433f384594dSTobias Grosser /// @see getTaggedAccesses 434f384594dSTobias Grosser isl_union_map *getTaggedReads() { 435f384594dSTobias Grosser return getTaggedAccesses(MemoryAccess::READ); 436f384594dSTobias Grosser } 437f384594dSTobias Grosser 438f384594dSTobias Grosser /// Get the set of all may (and must) accesses, tagged with the access id. 439f384594dSTobias Grosser /// 440f384594dSTobias Grosser /// @see getTaggedAccesses 441f384594dSTobias Grosser isl_union_map *getTaggedMayWrites() { 442f384594dSTobias Grosser return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE), 443f384594dSTobias Grosser getTaggedAccesses(MemoryAccess::MUST_WRITE)); 444f384594dSTobias Grosser } 445f384594dSTobias Grosser 446f384594dSTobias Grosser /// Get the set of all must accesses, tagged with the access id. 447f384594dSTobias Grosser /// 448f384594dSTobias Grosser /// @see getTaggedAccesses 449f384594dSTobias Grosser isl_union_map *getTaggedMustWrites() { 450f384594dSTobias Grosser return getTaggedAccesses(MemoryAccess::MUST_WRITE); 451f384594dSTobias Grosser } 452f384594dSTobias Grosser 453aef5196fSTobias Grosser /// Collect parameter and array names as isl_ids. 454aef5196fSTobias Grosser /// 455aef5196fSTobias Grosser /// To reason about the different parameters and arrays used, ppcg requires 456aef5196fSTobias Grosser /// a list of all isl_ids in use. As PPCG traditionally performs 457aef5196fSTobias Grosser /// source-to-source compilation each of these isl_ids is mapped to the 458aef5196fSTobias Grosser /// expression that represents it. As we do not have a corresponding 459aef5196fSTobias Grosser /// expression in Polly, we just map each id to a 'zero' expression to match 460aef5196fSTobias Grosser /// the data format that ppcg expects. 461aef5196fSTobias Grosser /// 462aef5196fSTobias Grosser /// @returns Retun a map from collected ids to 'zero' ast expressions. 463aef5196fSTobias Grosser __isl_give isl_id_to_ast_expr *getNames() { 464aef5196fSTobias Grosser auto *Names = isl_id_to_ast_expr_alloc( 465bd81a7eeSTobias Grosser S->getIslCtx(), 466bd81a7eeSTobias Grosser S->getNumParams() + std::distance(S->array_begin(), S->array_end())); 467aef5196fSTobias Grosser auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx())); 468aef5196fSTobias Grosser auto *Space = S->getParamSpace(); 469aef5196fSTobias Grosser 470aef5196fSTobias Grosser for (int I = 0, E = S->getNumParams(); I < E; ++I) { 471aef5196fSTobias Grosser isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I); 472aef5196fSTobias Grosser Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 473aef5196fSTobias Grosser } 474aef5196fSTobias Grosser 475aef5196fSTobias Grosser for (auto &Array : S->arrays()) { 476aef5196fSTobias Grosser auto Id = Array.second->getBasePtrId(); 477aef5196fSTobias Grosser Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 478aef5196fSTobias Grosser } 479aef5196fSTobias Grosser 480aef5196fSTobias Grosser isl_space_free(Space); 481aef5196fSTobias Grosser isl_ast_expr_free(Zero); 482aef5196fSTobias Grosser 483aef5196fSTobias Grosser return Names; 484aef5196fSTobias Grosser } 485aef5196fSTobias Grosser 486e938517eSTobias Grosser /// Create a new PPCG scop from the current scop. 487e938517eSTobias Grosser /// 488f384594dSTobias Grosser /// The PPCG scop is initialized with data from the current polly::Scop. From 489f384594dSTobias Grosser /// this initial data, the data-dependences in the PPCG scop are initialized. 490f384594dSTobias Grosser /// We do not use Polly's dependence analysis for now, to ensure we match 491f384594dSTobias Grosser /// the PPCG default behaviour more closely. 492e938517eSTobias Grosser /// 493e938517eSTobias Grosser /// @returns A new ppcg scop. 494e938517eSTobias Grosser ppcg_scop *createPPCGScop() { 495e938517eSTobias Grosser auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop)); 496e938517eSTobias Grosser 497e938517eSTobias Grosser PPCGScop->options = createPPCGOptions(); 498e938517eSTobias Grosser 499e938517eSTobias Grosser PPCGScop->start = 0; 500e938517eSTobias Grosser PPCGScop->end = 0; 501e938517eSTobias Grosser 502f384594dSTobias Grosser PPCGScop->context = S->getContext(); 503f384594dSTobias Grosser PPCGScop->domain = S->getDomains(); 504e938517eSTobias Grosser PPCGScop->call = nullptr; 505f384594dSTobias Grosser PPCGScop->tagged_reads = getTaggedReads(); 506f384594dSTobias Grosser PPCGScop->reads = S->getReads(); 507e938517eSTobias Grosser PPCGScop->live_in = nullptr; 508f384594dSTobias Grosser PPCGScop->tagged_may_writes = getTaggedMayWrites(); 509f384594dSTobias Grosser PPCGScop->may_writes = S->getWrites(); 510f384594dSTobias Grosser PPCGScop->tagged_must_writes = getTaggedMustWrites(); 511f384594dSTobias Grosser PPCGScop->must_writes = S->getMustWrites(); 512e938517eSTobias Grosser PPCGScop->live_out = nullptr; 513f384594dSTobias Grosser PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace()); 514e938517eSTobias Grosser PPCGScop->tagger = nullptr; 515e938517eSTobias Grosser 516e938517eSTobias Grosser PPCGScop->independence = nullptr; 517e938517eSTobias Grosser PPCGScop->dep_flow = nullptr; 518e938517eSTobias Grosser PPCGScop->tagged_dep_flow = nullptr; 519e938517eSTobias Grosser PPCGScop->dep_false = nullptr; 520e938517eSTobias Grosser PPCGScop->dep_forced = nullptr; 521e938517eSTobias Grosser PPCGScop->dep_order = nullptr; 522e938517eSTobias Grosser PPCGScop->tagged_dep_order = nullptr; 523e938517eSTobias Grosser 524f384594dSTobias Grosser PPCGScop->schedule = S->getScheduleTree(); 525aef5196fSTobias Grosser PPCGScop->names = getNames(); 526e938517eSTobias Grosser 527e938517eSTobias Grosser PPCGScop->pet = nullptr; 528e938517eSTobias Grosser 529f384594dSTobias Grosser compute_tagger(PPCGScop); 530f384594dSTobias Grosser compute_dependences(PPCGScop); 531f384594dSTobias Grosser 532e938517eSTobias Grosser return PPCGScop; 533e938517eSTobias Grosser } 534e938517eSTobias Grosser 53560f63b49STobias Grosser /// Collect the array acesses in a statement. 53660f63b49STobias Grosser /// 53760f63b49STobias Grosser /// @param Stmt The statement for which to collect the accesses. 53860f63b49STobias Grosser /// 53960f63b49STobias Grosser /// @returns A list of array accesses. 54060f63b49STobias Grosser gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) { 54160f63b49STobias Grosser gpu_stmt_access *Accesses = nullptr; 54260f63b49STobias Grosser 54360f63b49STobias Grosser for (MemoryAccess *Acc : Stmt) { 54460f63b49STobias Grosser auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access); 54560f63b49STobias Grosser Access->read = Acc->isRead(); 54660f63b49STobias Grosser Access->write = Acc->isWrite(); 54760f63b49STobias Grosser Access->access = Acc->getAccessRelation(); 54860f63b49STobias Grosser isl_space *Space = isl_map_get_space(Access->access); 54960f63b49STobias Grosser Space = isl_space_range(Space); 55060f63b49STobias Grosser Space = isl_space_from_range(Space); 5516293ba69STobias Grosser Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId()); 55260f63b49STobias Grosser isl_map *Universe = isl_map_universe(Space); 55360f63b49STobias Grosser Access->tagged_access = 55460f63b49STobias Grosser isl_map_domain_product(Acc->getAccessRelation(), Universe); 55560f63b49STobias Grosser Access->exact_write = Acc->isWrite(); 55660f63b49STobias Grosser Access->ref_id = Acc->getId(); 55760f63b49STobias Grosser Access->next = Accesses; 55860f63b49STobias Grosser Accesses = Access; 55960f63b49STobias Grosser } 56060f63b49STobias Grosser 56160f63b49STobias Grosser return Accesses; 56260f63b49STobias Grosser } 56360f63b49STobias Grosser 56469b46751STobias Grosser /// Collect the list of GPU statements. 56569b46751STobias Grosser /// 56669b46751STobias Grosser /// Each statement has an id, a pointer to the underlying data structure, 56769b46751STobias Grosser /// as well as a list with all memory accesses. 56869b46751STobias Grosser /// 56969b46751STobias Grosser /// TODO: Initialize the list of memory accesses. 57069b46751STobias Grosser /// 57169b46751STobias Grosser /// @returns A linked-list of statements. 57269b46751STobias Grosser gpu_stmt *getStatements() { 57369b46751STobias Grosser gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt, 57469b46751STobias Grosser std::distance(S->begin(), S->end())); 57569b46751STobias Grosser 57669b46751STobias Grosser int i = 0; 57769b46751STobias Grosser for (auto &Stmt : *S) { 57869b46751STobias Grosser gpu_stmt *GPUStmt = &Stmts[i]; 57969b46751STobias Grosser 58069b46751STobias Grosser GPUStmt->id = Stmt.getDomainId(); 58169b46751STobias Grosser 58269b46751STobias Grosser // We use the pet stmt pointer to keep track of the Polly statements. 58369b46751STobias Grosser GPUStmt->stmt = (pet_stmt *)&Stmt; 58460f63b49STobias Grosser GPUStmt->accesses = getStmtAccesses(Stmt); 58569b46751STobias Grosser i++; 58669b46751STobias Grosser } 58769b46751STobias Grosser 58869b46751STobias Grosser return Stmts; 58969b46751STobias Grosser } 59069b46751STobias Grosser 59160f63b49STobias Grosser /// Derive the extent of an array. 59260f63b49STobias Grosser /// 59360f63b49STobias Grosser /// The extent of an array is defined by the set of memory locations for 59460f63b49STobias Grosser /// which a memory access in the iteration domain exists. 59560f63b49STobias Grosser /// 59660f63b49STobias Grosser /// @param Array The array to derive the extent for. 59760f63b49STobias Grosser /// 59860f63b49STobias Grosser /// @returns An isl_set describing the extent of the array. 59960f63b49STobias Grosser __isl_give isl_set *getExtent(ScopArrayInfo *Array) { 60060f63b49STobias Grosser isl_union_map *Accesses = S->getAccesses(); 60160f63b49STobias Grosser Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains()); 60260f63b49STobias Grosser isl_union_set *AccessUSet = isl_union_map_range(Accesses); 60360f63b49STobias Grosser isl_set *AccessSet = 60460f63b49STobias Grosser isl_union_set_extract_set(AccessUSet, Array->getSpace()); 60560f63b49STobias Grosser isl_union_set_free(AccessUSet); 60660f63b49STobias Grosser 60760f63b49STobias Grosser return AccessSet; 60860f63b49STobias Grosser } 60960f63b49STobias Grosser 61060f63b49STobias Grosser /// Derive the bounds of an array. 61160f63b49STobias Grosser /// 61260f63b49STobias Grosser /// For the first dimension we derive the bound of the array from the extent 61360f63b49STobias Grosser /// of this dimension. For inner dimensions we obtain their size directly from 61460f63b49STobias Grosser /// ScopArrayInfo. 61560f63b49STobias Grosser /// 61660f63b49STobias Grosser /// @param PPCGArray The array to compute bounds for. 61760f63b49STobias Grosser /// @param Array The polly array from which to take the information. 61860f63b49STobias Grosser void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) { 61960f63b49STobias Grosser if (PPCGArray.n_index > 0) { 62060f63b49STobias Grosser isl_set *Dom = isl_set_copy(PPCGArray.extent); 62160f63b49STobias Grosser Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1); 62260f63b49STobias Grosser isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0); 62360f63b49STobias Grosser isl_set_free(Dom); 62460f63b49STobias Grosser Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound)); 62560f63b49STobias Grosser isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom)); 62660f63b49STobias Grosser isl_aff *One = isl_aff_zero_on_domain(LS); 62760f63b49STobias Grosser One = isl_aff_add_constant_si(One, 1); 62860f63b49STobias Grosser Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One)); 62960f63b49STobias Grosser Bound = isl_pw_aff_gist(Bound, S->getContext()); 63060f63b49STobias Grosser PPCGArray.bound[0] = Bound; 63160f63b49STobias Grosser } 63260f63b49STobias Grosser 63360f63b49STobias Grosser for (unsigned i = 1; i < PPCGArray.n_index; ++i) { 63460f63b49STobias Grosser isl_pw_aff *Bound = Array->getDimensionSizePw(i); 63560f63b49STobias Grosser auto LS = isl_pw_aff_get_domain_space(Bound); 63660f63b49STobias Grosser auto Aff = isl_multi_aff_zero(LS); 63760f63b49STobias Grosser Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff); 63860f63b49STobias Grosser PPCGArray.bound[i] = Bound; 63960f63b49STobias Grosser } 64060f63b49STobias Grosser } 64160f63b49STobias Grosser 64260f63b49STobias Grosser /// Create the arrays for @p PPCGProg. 64360f63b49STobias Grosser /// 64460f63b49STobias Grosser /// @param PPCGProg The program to compute the arrays for. 64560f63b49STobias Grosser void createArrays(gpu_prog *PPCGProg) { 64660f63b49STobias Grosser int i = 0; 64760f63b49STobias Grosser for (auto &Element : S->arrays()) { 64860f63b49STobias Grosser ScopArrayInfo *Array = Element.second.get(); 64960f63b49STobias Grosser 65060f63b49STobias Grosser std::string TypeName; 65160f63b49STobias Grosser raw_string_ostream OS(TypeName); 65260f63b49STobias Grosser 65360f63b49STobias Grosser OS << *Array->getElementType(); 65460f63b49STobias Grosser TypeName = OS.str(); 65560f63b49STobias Grosser 65660f63b49STobias Grosser gpu_array_info &PPCGArray = PPCGProg->array[i]; 65760f63b49STobias Grosser 65860f63b49STobias Grosser PPCGArray.space = Array->getSpace(); 65960f63b49STobias Grosser PPCGArray.type = strdup(TypeName.c_str()); 66060f63b49STobias Grosser PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8; 66160f63b49STobias Grosser PPCGArray.name = strdup(Array->getName().c_str()); 66260f63b49STobias Grosser PPCGArray.extent = nullptr; 66360f63b49STobias Grosser PPCGArray.n_index = Array->getNumberOfDimensions(); 66460f63b49STobias Grosser PPCGArray.bound = 66560f63b49STobias Grosser isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index); 66660f63b49STobias Grosser PPCGArray.extent = getExtent(Array); 66760f63b49STobias Grosser PPCGArray.n_ref = 0; 66860f63b49STobias Grosser PPCGArray.refs = nullptr; 66960f63b49STobias Grosser PPCGArray.accessed = true; 67060f63b49STobias Grosser PPCGArray.read_only_scalar = false; 67160f63b49STobias Grosser PPCGArray.has_compound_element = false; 67260f63b49STobias Grosser PPCGArray.local = false; 67360f63b49STobias Grosser PPCGArray.declare_local = false; 67460f63b49STobias Grosser PPCGArray.global = false; 67560f63b49STobias Grosser PPCGArray.linearize = false; 67660f63b49STobias Grosser PPCGArray.dep_order = nullptr; 67760f63b49STobias Grosser 67860f63b49STobias Grosser setArrayBounds(PPCGArray, Array); 6792d010dafSTobias Grosser i++; 680b9fc860aSTobias Grosser 681b9fc860aSTobias Grosser collect_references(PPCGProg, &PPCGArray); 68260f63b49STobias Grosser } 68360f63b49STobias Grosser } 68460f63b49STobias Grosser 68560f63b49STobias Grosser /// Create an identity map between the arrays in the scop. 68660f63b49STobias Grosser /// 68760f63b49STobias Grosser /// @returns An identity map between the arrays in the scop. 68860f63b49STobias Grosser isl_union_map *getArrayIdentity() { 68960f63b49STobias Grosser isl_union_map *Maps = isl_union_map_empty(S->getParamSpace()); 69060f63b49STobias Grosser 69160f63b49STobias Grosser for (auto &Item : S->arrays()) { 69260f63b49STobias Grosser ScopArrayInfo *Array = Item.second.get(); 69360f63b49STobias Grosser isl_space *Space = Array->getSpace(); 69460f63b49STobias Grosser Space = isl_space_map_from_set(Space); 69560f63b49STobias Grosser isl_map *Identity = isl_map_identity(Space); 69660f63b49STobias Grosser Maps = isl_union_map_add_map(Maps, Identity); 69760f63b49STobias Grosser } 69860f63b49STobias Grosser 69960f63b49STobias Grosser return Maps; 70060f63b49STobias Grosser } 70160f63b49STobias Grosser 702e938517eSTobias Grosser /// Create a default-initialized PPCG GPU program. 703e938517eSTobias Grosser /// 704e938517eSTobias Grosser /// @returns A new gpu grogram description. 705e938517eSTobias Grosser gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) { 706e938517eSTobias Grosser 707e938517eSTobias Grosser if (!PPCGScop) 708e938517eSTobias Grosser return nullptr; 709e938517eSTobias Grosser 710e938517eSTobias Grosser auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog); 711e938517eSTobias Grosser 712e938517eSTobias Grosser PPCGProg->ctx = S->getIslCtx(); 713e938517eSTobias Grosser PPCGProg->scop = PPCGScop; 714aef5196fSTobias Grosser PPCGProg->context = isl_set_copy(PPCGScop->context); 71560f63b49STobias Grosser PPCGProg->read = isl_union_map_copy(PPCGScop->reads); 71660f63b49STobias Grosser PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes); 71760f63b49STobias Grosser PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes); 71860f63b49STobias Grosser PPCGProg->tagged_must_kill = 71960f63b49STobias Grosser isl_union_map_copy(PPCGScop->tagged_must_kills); 72060f63b49STobias Grosser PPCGProg->to_inner = getArrayIdentity(); 72160f63b49STobias Grosser PPCGProg->to_outer = getArrayIdentity(); 72260f63b49STobias Grosser PPCGProg->may_persist = compute_may_persist(PPCGProg); 723e938517eSTobias Grosser PPCGProg->any_to_outer = nullptr; 724e938517eSTobias Grosser PPCGProg->array_order = nullptr; 72569b46751STobias Grosser PPCGProg->n_stmts = std::distance(S->begin(), S->end()); 72669b46751STobias Grosser PPCGProg->stmts = getStatements(); 72760f63b49STobias Grosser PPCGProg->n_array = std::distance(S->array_begin(), S->array_end()); 72860f63b49STobias Grosser PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info, 72960f63b49STobias Grosser PPCGProg->n_array); 73060f63b49STobias Grosser 73160f63b49STobias Grosser createArrays(PPCGProg); 732e938517eSTobias Grosser 733e938517eSTobias Grosser return PPCGProg; 734e938517eSTobias Grosser } 735e938517eSTobias Grosser 73669b46751STobias Grosser struct PrintGPUUserData { 73769b46751STobias Grosser struct cuda_info *CudaInfo; 73869b46751STobias Grosser struct gpu_prog *PPCGProg; 73969b46751STobias Grosser std::vector<ppcg_kernel *> Kernels; 74069b46751STobias Grosser }; 74169b46751STobias Grosser 74269b46751STobias Grosser /// Print a user statement node in the host code. 74369b46751STobias Grosser /// 74469b46751STobias Grosser /// We use ppcg's printing facilities to print the actual statement and 74569b46751STobias Grosser /// additionally build up a list of all kernels that are encountered in the 74669b46751STobias Grosser /// host ast. 74769b46751STobias Grosser /// 74869b46751STobias Grosser /// @param P The printer to print to 74969b46751STobias Grosser /// @param Options The printing options to use 75069b46751STobias Grosser /// @param Node The node to print 75169b46751STobias Grosser /// @param User A user pointer to carry additional data. This pointer is 75269b46751STobias Grosser /// expected to be of type PrintGPUUserData. 75369b46751STobias Grosser /// 75469b46751STobias Grosser /// @returns A printer to which the output has been printed. 75569b46751STobias Grosser static __isl_give isl_printer * 75669b46751STobias Grosser printHostUser(__isl_take isl_printer *P, 75769b46751STobias Grosser __isl_take isl_ast_print_options *Options, 75869b46751STobias Grosser __isl_take isl_ast_node *Node, void *User) { 75969b46751STobias Grosser auto Data = (struct PrintGPUUserData *)User; 76069b46751STobias Grosser auto Id = isl_ast_node_get_annotation(Node); 76169b46751STobias Grosser 76269b46751STobias Grosser if (Id) { 76320251734STobias Grosser bool IsUser = !strcmp(isl_id_get_name(Id), "user"); 76420251734STobias Grosser 76520251734STobias Grosser // If this is a user statement, format it ourselves as ppcg would 76620251734STobias Grosser // otherwise try to call pet functionality that is not available in 76720251734STobias Grosser // Polly. 76820251734STobias Grosser if (IsUser) { 76920251734STobias Grosser P = isl_printer_start_line(P); 77020251734STobias Grosser P = isl_printer_print_ast_node(P, Node); 77120251734STobias Grosser P = isl_printer_end_line(P); 77220251734STobias Grosser isl_id_free(Id); 77320251734STobias Grosser isl_ast_print_options_free(Options); 77420251734STobias Grosser return P; 77520251734STobias Grosser } 77620251734STobias Grosser 77769b46751STobias Grosser auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id); 77869b46751STobias Grosser isl_id_free(Id); 77969b46751STobias Grosser Data->Kernels.push_back(Kernel); 78069b46751STobias Grosser } 78169b46751STobias Grosser 78269b46751STobias Grosser return print_host_user(P, Options, Node, User); 78369b46751STobias Grosser } 78469b46751STobias Grosser 78569b46751STobias Grosser /// Print C code corresponding to the control flow in @p Kernel. 78669b46751STobias Grosser /// 78769b46751STobias Grosser /// @param Kernel The kernel to print 78869b46751STobias Grosser void printKernel(ppcg_kernel *Kernel) { 78969b46751STobias Grosser auto *P = isl_printer_to_str(S->getIslCtx()); 79069b46751STobias Grosser P = isl_printer_set_output_format(P, ISL_FORMAT_C); 79169b46751STobias Grosser auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 79269b46751STobias Grosser P = isl_ast_node_print(Kernel->tree, P, Options); 79369b46751STobias Grosser char *String = isl_printer_get_str(P); 79469b46751STobias Grosser printf("%s\n", String); 79569b46751STobias Grosser free(String); 79669b46751STobias Grosser isl_printer_free(P); 79769b46751STobias Grosser } 79869b46751STobias Grosser 79969b46751STobias Grosser /// Print C code corresponding to the GPU code described by @p Tree. 80069b46751STobias Grosser /// 80169b46751STobias Grosser /// @param Tree An AST describing GPU code 80269b46751STobias Grosser /// @param PPCGProg The PPCG program from which @Tree has been constructed. 80369b46751STobias Grosser void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) { 80469b46751STobias Grosser auto *P = isl_printer_to_str(S->getIslCtx()); 80569b46751STobias Grosser P = isl_printer_set_output_format(P, ISL_FORMAT_C); 80669b46751STobias Grosser 80769b46751STobias Grosser PrintGPUUserData Data; 80869b46751STobias Grosser Data.PPCGProg = PPCGProg; 80969b46751STobias Grosser 81069b46751STobias Grosser auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 81169b46751STobias Grosser Options = 81269b46751STobias Grosser isl_ast_print_options_set_print_user(Options, printHostUser, &Data); 81369b46751STobias Grosser P = isl_ast_node_print(Tree, P, Options); 81469b46751STobias Grosser char *String = isl_printer_get_str(P); 81569b46751STobias Grosser printf("# host\n"); 81669b46751STobias Grosser printf("%s\n", String); 81769b46751STobias Grosser free(String); 81869b46751STobias Grosser isl_printer_free(P); 81969b46751STobias Grosser 82069b46751STobias Grosser for (auto Kernel : Data.Kernels) { 82169b46751STobias Grosser printf("# kernel%d\n", Kernel->id); 82269b46751STobias Grosser printKernel(Kernel); 82369b46751STobias Grosser } 82469b46751STobias Grosser } 82569b46751STobias Grosser 826f384594dSTobias Grosser // Generate a GPU program using PPCG. 827f384594dSTobias Grosser // 828f384594dSTobias Grosser // GPU mapping consists of multiple steps: 829f384594dSTobias Grosser // 830f384594dSTobias Grosser // 1) Compute new schedule for the program. 831f384594dSTobias Grosser // 2) Map schedule to GPU (TODO) 832f384594dSTobias Grosser // 3) Generate code for new schedule (TODO) 833f384594dSTobias Grosser // 834f384594dSTobias Grosser // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer 835f384594dSTobias Grosser // is mostly CPU specific. Instead, we use PPCG's GPU code generation 836f384594dSTobias Grosser // strategy directly from this pass. 837f384594dSTobias Grosser gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) { 838f384594dSTobias Grosser 839f384594dSTobias Grosser auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen); 840f384594dSTobias Grosser 841f384594dSTobias Grosser PPCGGen->ctx = S->getIslCtx(); 842f384594dSTobias Grosser PPCGGen->options = PPCGScop->options; 843f384594dSTobias Grosser PPCGGen->print = nullptr; 844f384594dSTobias Grosser PPCGGen->print_user = nullptr; 84560c60025STobias Grosser PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt; 846f384594dSTobias Grosser PPCGGen->prog = PPCGProg; 847f384594dSTobias Grosser PPCGGen->tree = nullptr; 848f384594dSTobias Grosser PPCGGen->types.n = 0; 849f384594dSTobias Grosser PPCGGen->types.name = nullptr; 850f384594dSTobias Grosser PPCGGen->sizes = nullptr; 851f384594dSTobias Grosser PPCGGen->used_sizes = nullptr; 852f384594dSTobias Grosser PPCGGen->kernel_id = 0; 853f384594dSTobias Grosser 854f384594dSTobias Grosser // Set scheduling strategy to same strategy PPCG is using. 855f384594dSTobias Grosser isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true); 856f384594dSTobias Grosser isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true); 8572341fe9eSTobias Grosser isl_options_set_schedule_whole_component(PPCGGen->ctx, false); 858f384594dSTobias Grosser 859f384594dSTobias Grosser isl_schedule *Schedule = get_schedule(PPCGGen); 860f384594dSTobias Grosser 861aef5196fSTobias Grosser int has_permutable = has_any_permutable_node(Schedule); 862aef5196fSTobias Grosser 86369b46751STobias Grosser if (!has_permutable || has_permutable < 0) { 864aef5196fSTobias Grosser Schedule = isl_schedule_free(Schedule); 86569b46751STobias Grosser } else { 866aef5196fSTobias Grosser Schedule = map_to_device(PPCGGen, Schedule); 86769b46751STobias Grosser PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule)); 86869b46751STobias Grosser } 869aef5196fSTobias Grosser 870f384594dSTobias Grosser if (DumpSchedule) { 871f384594dSTobias Grosser isl_printer *P = isl_printer_to_str(S->getIslCtx()); 872f384594dSTobias Grosser P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); 873f384594dSTobias Grosser P = isl_printer_print_str(P, "Schedule\n"); 874f384594dSTobias Grosser P = isl_printer_print_str(P, "========\n"); 875f384594dSTobias Grosser if (Schedule) 876f384594dSTobias Grosser P = isl_printer_print_schedule(P, Schedule); 877f384594dSTobias Grosser else 878f384594dSTobias Grosser P = isl_printer_print_str(P, "No schedule found\n"); 879f384594dSTobias Grosser 880f384594dSTobias Grosser printf("%s\n", isl_printer_get_str(P)); 881f384594dSTobias Grosser isl_printer_free(P); 882f384594dSTobias Grosser } 883f384594dSTobias Grosser 88469b46751STobias Grosser if (DumpCode) { 88569b46751STobias Grosser printf("Code\n"); 88669b46751STobias Grosser printf("====\n"); 88769b46751STobias Grosser if (PPCGGen->tree) 88869b46751STobias Grosser printGPUTree(PPCGGen->tree, PPCGProg); 88969b46751STobias Grosser else 89069b46751STobias Grosser printf("No code generated\n"); 89169b46751STobias Grosser } 89269b46751STobias Grosser 893f384594dSTobias Grosser isl_schedule_free(Schedule); 894f384594dSTobias Grosser 895f384594dSTobias Grosser return PPCGGen; 896f384594dSTobias Grosser } 897f384594dSTobias Grosser 898f384594dSTobias Grosser /// Free gpu_gen structure. 899f384594dSTobias Grosser /// 900f384594dSTobias Grosser /// @param PPCGGen The ppcg_gen object to free. 901f384594dSTobias Grosser void freePPCGGen(gpu_gen *PPCGGen) { 902f384594dSTobias Grosser isl_ast_node_free(PPCGGen->tree); 903f384594dSTobias Grosser isl_union_map_free(PPCGGen->sizes); 904f384594dSTobias Grosser isl_union_map_free(PPCGGen->used_sizes); 905f384594dSTobias Grosser free(PPCGGen); 906f384594dSTobias Grosser } 907f384594dSTobias Grosser 908b307ed4dSTobias Grosser /// Free the options in the ppcg scop structure. 909b307ed4dSTobias Grosser /// 910b307ed4dSTobias Grosser /// ppcg is not freeing these options for us. To avoid leaks we do this 911b307ed4dSTobias Grosser /// ourselves. 912b307ed4dSTobias Grosser /// 913b307ed4dSTobias Grosser /// @param PPCGScop The scop referencing the options to free. 914b307ed4dSTobias Grosser void freeOptions(ppcg_scop *PPCGScop) { 915b307ed4dSTobias Grosser free(PPCGScop->options->debug); 916b307ed4dSTobias Grosser PPCGScop->options->debug = nullptr; 917b307ed4dSTobias Grosser free(PPCGScop->options); 918b307ed4dSTobias Grosser PPCGScop->options = nullptr; 919b307ed4dSTobias Grosser } 920b307ed4dSTobias Grosser 92138fc0aedSTobias Grosser /// Generate code for a given GPU AST described by @p Root. 92238fc0aedSTobias Grosser /// 92332837fe3STobias Grosser /// @param Root An isl_ast_node pointing to the root of the GPU AST. 92432837fe3STobias Grosser /// @param Prog The GPU Program to generate code for. 92532837fe3STobias Grosser void generateCode(__isl_take isl_ast_node *Root, gpu_prog *Prog) { 92638fc0aedSTobias Grosser ScopAnnotator Annotator; 92738fc0aedSTobias Grosser Annotator.buildAliasScopes(*S); 92838fc0aedSTobias Grosser 92938fc0aedSTobias Grosser Region *R = &S->getRegion(); 93038fc0aedSTobias Grosser 93138fc0aedSTobias Grosser simplifyRegion(R, DT, LI, RI); 93238fc0aedSTobias Grosser 93338fc0aedSTobias Grosser BasicBlock *EnteringBB = R->getEnteringBlock(); 93438fc0aedSTobias Grosser 93538fc0aedSTobias Grosser PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 93638fc0aedSTobias Grosser 93732837fe3STobias Grosser GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, *S, 93832837fe3STobias Grosser Prog); 93938fc0aedSTobias Grosser 94038fc0aedSTobias Grosser // Only build the run-time condition and parameters _after_ having 94138fc0aedSTobias Grosser // introduced the conditional branch. This is important as the conditional 94238fc0aedSTobias Grosser // branch will guard the original scop from new induction variables that 94338fc0aedSTobias Grosser // the SCEVExpander may introduce while code generating the parameters and 94438fc0aedSTobias Grosser // which may introduce scalar dependences that prevent us from correctly 94538fc0aedSTobias Grosser // code generating this scop. 94638fc0aedSTobias Grosser BasicBlock *StartBlock = 94738fc0aedSTobias Grosser executeScopConditionally(*S, this, Builder.getTrue()); 94838fc0aedSTobias Grosser 94938fc0aedSTobias Grosser // TODO: Handle LICM 95038fc0aedSTobias Grosser // TODO: Verify run-time checks 95138fc0aedSTobias Grosser auto SplitBlock = StartBlock->getSinglePredecessor(); 95238fc0aedSTobias Grosser Builder.SetInsertPoint(SplitBlock->getTerminator()); 95338fc0aedSTobias Grosser NodeBuilder.addParameters(S->getContext()); 95438fc0aedSTobias Grosser Builder.SetInsertPoint(&*StartBlock->begin()); 95538fc0aedSTobias Grosser NodeBuilder.create(Root); 95638fc0aedSTobias Grosser NodeBuilder.finalizeSCoP(*S); 95738fc0aedSTobias Grosser } 95838fc0aedSTobias Grosser 959e938517eSTobias Grosser bool runOnScop(Scop &CurrentScop) override { 960e938517eSTobias Grosser S = &CurrentScop; 96138fc0aedSTobias Grosser LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 96238fc0aedSTobias Grosser DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 96338fc0aedSTobias Grosser SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 96438fc0aedSTobias Grosser DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout(); 96538fc0aedSTobias Grosser RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 966e938517eSTobias Grosser 967e938517eSTobias Grosser auto PPCGScop = createPPCGScop(); 968e938517eSTobias Grosser auto PPCGProg = createPPCGProg(PPCGScop); 969f384594dSTobias Grosser auto PPCGGen = generateGPU(PPCGScop, PPCGProg); 97038fc0aedSTobias Grosser 97138fc0aedSTobias Grosser if (PPCGGen->tree) 97232837fe3STobias Grosser generateCode(isl_ast_node_copy(PPCGGen->tree), PPCGProg); 97338fc0aedSTobias Grosser 974b307ed4dSTobias Grosser freeOptions(PPCGScop); 975f384594dSTobias Grosser freePPCGGen(PPCGGen); 976e938517eSTobias Grosser gpu_prog_free(PPCGProg); 977e938517eSTobias Grosser ppcg_scop_free(PPCGScop); 978e938517eSTobias Grosser 979e938517eSTobias Grosser return true; 980e938517eSTobias Grosser } 9819dfe4e7cSTobias Grosser 9829dfe4e7cSTobias Grosser void printScop(raw_ostream &, Scop &) const override {} 9839dfe4e7cSTobias Grosser 9849dfe4e7cSTobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 9859dfe4e7cSTobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 9869dfe4e7cSTobias Grosser AU.addRequired<RegionInfoPass>(); 9879dfe4e7cSTobias Grosser AU.addRequired<ScalarEvolutionWrapperPass>(); 9889dfe4e7cSTobias Grosser AU.addRequired<ScopDetection>(); 9899dfe4e7cSTobias Grosser AU.addRequired<ScopInfoRegionPass>(); 9909dfe4e7cSTobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 9919dfe4e7cSTobias Grosser 9929dfe4e7cSTobias Grosser AU.addPreserved<AAResultsWrapperPass>(); 9939dfe4e7cSTobias Grosser AU.addPreserved<BasicAAWrapperPass>(); 9949dfe4e7cSTobias Grosser AU.addPreserved<LoopInfoWrapperPass>(); 9959dfe4e7cSTobias Grosser AU.addPreserved<DominatorTreeWrapperPass>(); 9969dfe4e7cSTobias Grosser AU.addPreserved<GlobalsAAWrapperPass>(); 9979dfe4e7cSTobias Grosser AU.addPreserved<PostDominatorTreeWrapperPass>(); 9989dfe4e7cSTobias Grosser AU.addPreserved<ScopDetection>(); 9999dfe4e7cSTobias Grosser AU.addPreserved<ScalarEvolutionWrapperPass>(); 10009dfe4e7cSTobias Grosser AU.addPreserved<SCEVAAWrapperPass>(); 10019dfe4e7cSTobias Grosser 10029dfe4e7cSTobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 10039dfe4e7cSTobias Grosser // region tree. 10049dfe4e7cSTobias Grosser AU.addPreserved<RegionInfoPass>(); 10059dfe4e7cSTobias Grosser AU.addPreserved<ScopInfoRegionPass>(); 10069dfe4e7cSTobias Grosser } 10079dfe4e7cSTobias Grosser }; 10089dfe4e7cSTobias Grosser } 10099dfe4e7cSTobias Grosser 10109dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1; 10119dfe4e7cSTobias Grosser 10129dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); } 10139dfe4e7cSTobias Grosser 10149dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg", 10159dfe4e7cSTobias Grosser "Polly - Apply PPCG translation to SCOP", false, false) 10169dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 10179dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 10189dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 10199dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 10209dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 10219dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection); 10229dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg", 10239dfe4e7cSTobias Grosser "Polly - Apply PPCG translation to SCOP", false, false) 1024