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" 169dfe4e7cSTobias Grosser #include "polly/DependenceInfo.h" 179dfe4e7cSTobias Grosser #include "polly/LinkAllPasses.h" 18f384594dSTobias Grosser #include "polly/Options.h" 199dfe4e7cSTobias Grosser #include "polly/ScopInfo.h" 209dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h" 219dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h" 229dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h" 239dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h" 249dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 259dfe4e7cSTobias Grosser 26f384594dSTobias Grosser #include "isl/union_map.h" 27f384594dSTobias Grosser 28e938517eSTobias Grosser extern "C" { 29*69b46751STobias Grosser #include "cuda.h" 30e938517eSTobias Grosser #include "gpu.h" 31*69b46751STobias Grosser #include "gpu_print.h" 32e938517eSTobias Grosser #include "ppcg.h" 33*69b46751STobias Grosser #include "schedule.h" 34e938517eSTobias Grosser } 35e938517eSTobias Grosser 369dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h" 379dfe4e7cSTobias Grosser 389dfe4e7cSTobias Grosser using namespace polly; 399dfe4e7cSTobias Grosser using namespace llvm; 409dfe4e7cSTobias Grosser 419dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg" 429dfe4e7cSTobias Grosser 43f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule", 44f384594dSTobias Grosser cl::desc("Dump the computed GPU Schedule"), 45681bd568STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore, 46f384594dSTobias Grosser cl::cat(PollyCategory)); 47*69b46751STobias Grosser 48*69b46751STobias Grosser static cl::opt<bool> 49*69b46751STobias Grosser DumpCode("polly-acc-dump-code", 50*69b46751STobias Grosser cl::desc("Dump C code describing the GPU mapping"), cl::Hidden, 51*69b46751STobias Grosser cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 52*69b46751STobias Grosser 5360c60025STobias Grosser /// Create the ast expressions for a ScopStmt. 5460c60025STobias Grosser /// 5560c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each 5660c60025STobias Grosser /// of the scheduled ScopStmts. 5760c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt( 5860c60025STobias Grosser void *Stmt, isl_ast_build *Build, 5960c60025STobias Grosser isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA, 6060c60025STobias Grosser isl_id *Id, void *User), 6160c60025STobias Grosser void *UserIndex, 6260c60025STobias Grosser isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User), 6360c60025STobias Grosser void *User_expr) { 6460c60025STobias Grosser 6560c60025STobias Grosser // TODO: Implement the AST expression generation. For now we just return a 6660c60025STobias Grosser // nullptr to ensure that we do not free uninitialized pointers. 6760c60025STobias Grosser 6860c60025STobias Grosser return nullptr; 6960c60025STobias Grosser } 70f384594dSTobias Grosser 719dfe4e7cSTobias Grosser namespace { 729dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass { 739dfe4e7cSTobias Grosser public: 749dfe4e7cSTobias Grosser static char ID; 759dfe4e7cSTobias Grosser 76e938517eSTobias Grosser /// The scop that is currently processed. 77e938517eSTobias Grosser Scop *S; 78e938517eSTobias Grosser 799dfe4e7cSTobias Grosser PPCGCodeGeneration() : ScopPass(ID) {} 809dfe4e7cSTobias Grosser 81e938517eSTobias Grosser /// Construct compilation options for PPCG. 82e938517eSTobias Grosser /// 83e938517eSTobias Grosser /// @returns The compilation options. 84e938517eSTobias Grosser ppcg_options *createPPCGOptions() { 85e938517eSTobias Grosser auto DebugOptions = 86e938517eSTobias Grosser (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options)); 87e938517eSTobias Grosser auto Options = (ppcg_options *)malloc(sizeof(ppcg_options)); 88e938517eSTobias Grosser 89e938517eSTobias Grosser DebugOptions->dump_schedule_constraints = false; 90e938517eSTobias Grosser DebugOptions->dump_schedule = false; 91e938517eSTobias Grosser DebugOptions->dump_final_schedule = false; 92e938517eSTobias Grosser DebugOptions->dump_sizes = false; 93e938517eSTobias Grosser 94e938517eSTobias Grosser Options->debug = DebugOptions; 95e938517eSTobias Grosser 96e938517eSTobias Grosser Options->reschedule = true; 97e938517eSTobias Grosser Options->scale_tile_loops = false; 98e938517eSTobias Grosser Options->wrap = false; 99e938517eSTobias Grosser 100e938517eSTobias Grosser Options->non_negative_parameters = false; 101e938517eSTobias Grosser Options->ctx = nullptr; 102e938517eSTobias Grosser Options->sizes = nullptr; 103e938517eSTobias Grosser 1044eaedde5STobias Grosser Options->tile_size = 32; 1054eaedde5STobias Grosser 106e938517eSTobias Grosser Options->use_private_memory = false; 107e938517eSTobias Grosser Options->use_shared_memory = false; 108e938517eSTobias Grosser Options->max_shared_memory = 0; 109e938517eSTobias Grosser 110e938517eSTobias Grosser Options->target = PPCG_TARGET_CUDA; 111e938517eSTobias Grosser Options->openmp = false; 112e938517eSTobias Grosser Options->linearize_device_arrays = true; 113e938517eSTobias Grosser Options->live_range_reordering = false; 114e938517eSTobias Grosser 115e938517eSTobias Grosser Options->opencl_compiler_options = nullptr; 116e938517eSTobias Grosser Options->opencl_use_gpu = false; 117e938517eSTobias Grosser Options->opencl_n_include_file = 0; 118e938517eSTobias Grosser Options->opencl_include_files = nullptr; 119e938517eSTobias Grosser Options->opencl_print_kernel_types = false; 120e938517eSTobias Grosser Options->opencl_embed_kernel_code = false; 121e938517eSTobias Grosser 122e938517eSTobias Grosser Options->save_schedule_file = nullptr; 123e938517eSTobias Grosser Options->load_schedule_file = nullptr; 124e938517eSTobias Grosser 125e938517eSTobias Grosser return Options; 126e938517eSTobias Grosser } 127e938517eSTobias Grosser 128f384594dSTobias Grosser /// Get a tagged access relation containing all accesses of type @p AccessTy. 129f384594dSTobias Grosser /// 130f384594dSTobias Grosser /// Instead of a normal access of the form: 131f384594dSTobias Grosser /// 132f384594dSTobias Grosser /// Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)] 133f384594dSTobias Grosser /// 134f384594dSTobias Grosser /// a tagged access has the form 135f384594dSTobias Grosser /// 136f384594dSTobias Grosser /// [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)] 137f384594dSTobias Grosser /// 138f384594dSTobias Grosser /// where 'id' is an additional space that references the memory access that 139f384594dSTobias Grosser /// triggered the access. 140f384594dSTobias Grosser /// 141f384594dSTobias Grosser /// @param AccessTy The type of the memory accesses to collect. 142f384594dSTobias Grosser /// 143f384594dSTobias Grosser /// @return The relation describing all tagged memory accesses. 144f384594dSTobias Grosser isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) { 145f384594dSTobias Grosser isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace()); 146f384594dSTobias Grosser 147f384594dSTobias Grosser for (auto &Stmt : *S) 148f384594dSTobias Grosser for (auto &Acc : Stmt) 149f384594dSTobias Grosser if (Acc->getType() == AccessTy) { 150f384594dSTobias Grosser isl_map *Relation = Acc->getAccessRelation(); 151f384594dSTobias Grosser Relation = isl_map_intersect_domain(Relation, Stmt.getDomain()); 152f384594dSTobias Grosser 153f384594dSTobias Grosser isl_space *Space = isl_map_get_space(Relation); 154f384594dSTobias Grosser Space = isl_space_range(Space); 155f384594dSTobias Grosser Space = isl_space_from_range(Space); 156f384594dSTobias Grosser isl_map *Universe = isl_map_universe(Space); 157f384594dSTobias Grosser Relation = isl_map_domain_product(Relation, Universe); 158f384594dSTobias Grosser Accesses = isl_union_map_add_map(Accesses, Relation); 159f384594dSTobias Grosser } 160f384594dSTobias Grosser 161f384594dSTobias Grosser return Accesses; 162f384594dSTobias Grosser } 163f384594dSTobias Grosser 164f384594dSTobias Grosser /// Get the set of all read accesses, tagged with the access id. 165f384594dSTobias Grosser /// 166f384594dSTobias Grosser /// @see getTaggedAccesses 167f384594dSTobias Grosser isl_union_map *getTaggedReads() { 168f384594dSTobias Grosser return getTaggedAccesses(MemoryAccess::READ); 169f384594dSTobias Grosser } 170f384594dSTobias Grosser 171f384594dSTobias Grosser /// Get the set of all may (and must) accesses, tagged with the access id. 172f384594dSTobias Grosser /// 173f384594dSTobias Grosser /// @see getTaggedAccesses 174f384594dSTobias Grosser isl_union_map *getTaggedMayWrites() { 175f384594dSTobias Grosser return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE), 176f384594dSTobias Grosser getTaggedAccesses(MemoryAccess::MUST_WRITE)); 177f384594dSTobias Grosser } 178f384594dSTobias Grosser 179f384594dSTobias Grosser /// Get the set of all must accesses, tagged with the access id. 180f384594dSTobias Grosser /// 181f384594dSTobias Grosser /// @see getTaggedAccesses 182f384594dSTobias Grosser isl_union_map *getTaggedMustWrites() { 183f384594dSTobias Grosser return getTaggedAccesses(MemoryAccess::MUST_WRITE); 184f384594dSTobias Grosser } 185f384594dSTobias Grosser 186aef5196fSTobias Grosser /// Collect parameter and array names as isl_ids. 187aef5196fSTobias Grosser /// 188aef5196fSTobias Grosser /// To reason about the different parameters and arrays used, ppcg requires 189aef5196fSTobias Grosser /// a list of all isl_ids in use. As PPCG traditionally performs 190aef5196fSTobias Grosser /// source-to-source compilation each of these isl_ids is mapped to the 191aef5196fSTobias Grosser /// expression that represents it. As we do not have a corresponding 192aef5196fSTobias Grosser /// expression in Polly, we just map each id to a 'zero' expression to match 193aef5196fSTobias Grosser /// the data format that ppcg expects. 194aef5196fSTobias Grosser /// 195aef5196fSTobias Grosser /// @returns Retun a map from collected ids to 'zero' ast expressions. 196aef5196fSTobias Grosser __isl_give isl_id_to_ast_expr *getNames() { 197aef5196fSTobias Grosser auto *Names = isl_id_to_ast_expr_alloc( 198bd81a7eeSTobias Grosser S->getIslCtx(), 199bd81a7eeSTobias Grosser S->getNumParams() + std::distance(S->array_begin(), S->array_end())); 200aef5196fSTobias Grosser auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx())); 201aef5196fSTobias Grosser auto *Space = S->getParamSpace(); 202aef5196fSTobias Grosser 203aef5196fSTobias Grosser for (int I = 0, E = S->getNumParams(); I < E; ++I) { 204aef5196fSTobias Grosser isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I); 205aef5196fSTobias Grosser Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 206aef5196fSTobias Grosser } 207aef5196fSTobias Grosser 208aef5196fSTobias Grosser for (auto &Array : S->arrays()) { 209aef5196fSTobias Grosser auto Id = Array.second->getBasePtrId(); 210aef5196fSTobias Grosser Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero)); 211aef5196fSTobias Grosser } 212aef5196fSTobias Grosser 213aef5196fSTobias Grosser isl_space_free(Space); 214aef5196fSTobias Grosser isl_ast_expr_free(Zero); 215aef5196fSTobias Grosser 216aef5196fSTobias Grosser return Names; 217aef5196fSTobias Grosser } 218aef5196fSTobias Grosser 219e938517eSTobias Grosser /// Create a new PPCG scop from the current scop. 220e938517eSTobias Grosser /// 221f384594dSTobias Grosser /// The PPCG scop is initialized with data from the current polly::Scop. From 222f384594dSTobias Grosser /// this initial data, the data-dependences in the PPCG scop are initialized. 223f384594dSTobias Grosser /// We do not use Polly's dependence analysis for now, to ensure we match 224f384594dSTobias Grosser /// the PPCG default behaviour more closely. 225e938517eSTobias Grosser /// 226e938517eSTobias Grosser /// @returns A new ppcg scop. 227e938517eSTobias Grosser ppcg_scop *createPPCGScop() { 228e938517eSTobias Grosser auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop)); 229e938517eSTobias Grosser 230e938517eSTobias Grosser PPCGScop->options = createPPCGOptions(); 231e938517eSTobias Grosser 232e938517eSTobias Grosser PPCGScop->start = 0; 233e938517eSTobias Grosser PPCGScop->end = 0; 234e938517eSTobias Grosser 235f384594dSTobias Grosser PPCGScop->context = S->getContext(); 236f384594dSTobias Grosser PPCGScop->domain = S->getDomains(); 237e938517eSTobias Grosser PPCGScop->call = nullptr; 238f384594dSTobias Grosser PPCGScop->tagged_reads = getTaggedReads(); 239f384594dSTobias Grosser PPCGScop->reads = S->getReads(); 240e938517eSTobias Grosser PPCGScop->live_in = nullptr; 241f384594dSTobias Grosser PPCGScop->tagged_may_writes = getTaggedMayWrites(); 242f384594dSTobias Grosser PPCGScop->may_writes = S->getWrites(); 243f384594dSTobias Grosser PPCGScop->tagged_must_writes = getTaggedMustWrites(); 244f384594dSTobias Grosser PPCGScop->must_writes = S->getMustWrites(); 245e938517eSTobias Grosser PPCGScop->live_out = nullptr; 246f384594dSTobias Grosser PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace()); 247e938517eSTobias Grosser PPCGScop->tagger = nullptr; 248e938517eSTobias Grosser 249e938517eSTobias Grosser PPCGScop->independence = nullptr; 250e938517eSTobias Grosser PPCGScop->dep_flow = nullptr; 251e938517eSTobias Grosser PPCGScop->tagged_dep_flow = nullptr; 252e938517eSTobias Grosser PPCGScop->dep_false = nullptr; 253e938517eSTobias Grosser PPCGScop->dep_forced = nullptr; 254e938517eSTobias Grosser PPCGScop->dep_order = nullptr; 255e938517eSTobias Grosser PPCGScop->tagged_dep_order = nullptr; 256e938517eSTobias Grosser 257f384594dSTobias Grosser PPCGScop->schedule = S->getScheduleTree(); 258aef5196fSTobias Grosser PPCGScop->names = getNames(); 259e938517eSTobias Grosser 260e938517eSTobias Grosser PPCGScop->pet = nullptr; 261e938517eSTobias Grosser 262f384594dSTobias Grosser compute_tagger(PPCGScop); 263f384594dSTobias Grosser compute_dependences(PPCGScop); 264f384594dSTobias Grosser 265e938517eSTobias Grosser return PPCGScop; 266e938517eSTobias Grosser } 267e938517eSTobias Grosser 268*69b46751STobias Grosser /// Collect the list of GPU statements. 269*69b46751STobias Grosser /// 270*69b46751STobias Grosser /// Each statement has an id, a pointer to the underlying data structure, 271*69b46751STobias Grosser /// as well as a list with all memory accesses. 272*69b46751STobias Grosser /// 273*69b46751STobias Grosser /// TODO: Initialize the list of memory accesses. 274*69b46751STobias Grosser /// 275*69b46751STobias Grosser /// @returns A linked-list of statements. 276*69b46751STobias Grosser gpu_stmt *getStatements() { 277*69b46751STobias Grosser gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt, 278*69b46751STobias Grosser std::distance(S->begin(), S->end())); 279*69b46751STobias Grosser 280*69b46751STobias Grosser int i = 0; 281*69b46751STobias Grosser for (auto &Stmt : *S) { 282*69b46751STobias Grosser gpu_stmt *GPUStmt = &Stmts[i]; 283*69b46751STobias Grosser 284*69b46751STobias Grosser GPUStmt->id = Stmt.getDomainId(); 285*69b46751STobias Grosser 286*69b46751STobias Grosser // We use the pet stmt pointer to keep track of the Polly statements. 287*69b46751STobias Grosser GPUStmt->stmt = (pet_stmt *)&Stmt; 288*69b46751STobias Grosser GPUStmt->accesses = nullptr; 289*69b46751STobias Grosser i++; 290*69b46751STobias Grosser } 291*69b46751STobias Grosser 292*69b46751STobias Grosser return Stmts; 293*69b46751STobias Grosser } 294*69b46751STobias Grosser 295e938517eSTobias Grosser /// Create a default-initialized PPCG GPU program. 296e938517eSTobias Grosser /// 297e938517eSTobias Grosser /// @returns A new gpu grogram description. 298e938517eSTobias Grosser gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) { 299e938517eSTobias Grosser 300e938517eSTobias Grosser if (!PPCGScop) 301e938517eSTobias Grosser return nullptr; 302e938517eSTobias Grosser 303e938517eSTobias Grosser auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog); 304e938517eSTobias Grosser 305e938517eSTobias Grosser PPCGProg->ctx = S->getIslCtx(); 306e938517eSTobias Grosser PPCGProg->scop = PPCGScop; 307aef5196fSTobias Grosser PPCGProg->context = isl_set_copy(PPCGScop->context); 308e938517eSTobias Grosser PPCGProg->read = nullptr; 309e938517eSTobias Grosser PPCGProg->may_write = nullptr; 310e938517eSTobias Grosser PPCGProg->must_write = nullptr; 311e938517eSTobias Grosser PPCGProg->tagged_must_kill = nullptr; 312e938517eSTobias Grosser PPCGProg->may_persist = nullptr; 313e938517eSTobias Grosser PPCGProg->to_outer = nullptr; 314e938517eSTobias Grosser PPCGProg->to_inner = nullptr; 315e938517eSTobias Grosser PPCGProg->any_to_outer = nullptr; 316e938517eSTobias Grosser PPCGProg->array_order = nullptr; 317*69b46751STobias Grosser PPCGProg->n_stmts = std::distance(S->begin(), S->end()); 318*69b46751STobias Grosser PPCGProg->stmts = getStatements(); 319e938517eSTobias Grosser PPCGProg->n_array = 0; 320e938517eSTobias Grosser PPCGProg->array = nullptr; 321e938517eSTobias Grosser 322e938517eSTobias Grosser return PPCGProg; 323e938517eSTobias Grosser } 324e938517eSTobias Grosser 325*69b46751STobias Grosser struct PrintGPUUserData { 326*69b46751STobias Grosser struct cuda_info *CudaInfo; 327*69b46751STobias Grosser struct gpu_prog *PPCGProg; 328*69b46751STobias Grosser std::vector<ppcg_kernel *> Kernels; 329*69b46751STobias Grosser }; 330*69b46751STobias Grosser 331*69b46751STobias Grosser /// Print a user statement node in the host code. 332*69b46751STobias Grosser /// 333*69b46751STobias Grosser /// We use ppcg's printing facilities to print the actual statement and 334*69b46751STobias Grosser /// additionally build up a list of all kernels that are encountered in the 335*69b46751STobias Grosser /// host ast. 336*69b46751STobias Grosser /// 337*69b46751STobias Grosser /// @param P The printer to print to 338*69b46751STobias Grosser /// @param Options The printing options to use 339*69b46751STobias Grosser /// @param Node The node to print 340*69b46751STobias Grosser /// @param User A user pointer to carry additional data. This pointer is 341*69b46751STobias Grosser /// expected to be of type PrintGPUUserData. 342*69b46751STobias Grosser /// 343*69b46751STobias Grosser /// @returns A printer to which the output has been printed. 344*69b46751STobias Grosser static __isl_give isl_printer * 345*69b46751STobias Grosser printHostUser(__isl_take isl_printer *P, 346*69b46751STobias Grosser __isl_take isl_ast_print_options *Options, 347*69b46751STobias Grosser __isl_take isl_ast_node *Node, void *User) { 348*69b46751STobias Grosser auto Data = (struct PrintGPUUserData *)User; 349*69b46751STobias Grosser auto Id = isl_ast_node_get_annotation(Node); 350*69b46751STobias Grosser 351*69b46751STobias Grosser if (Id) { 352*69b46751STobias Grosser auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id); 353*69b46751STobias Grosser isl_id_free(Id); 354*69b46751STobias Grosser Data->Kernels.push_back(Kernel); 355*69b46751STobias Grosser } 356*69b46751STobias Grosser 357*69b46751STobias Grosser return print_host_user(P, Options, Node, User); 358*69b46751STobias Grosser } 359*69b46751STobias Grosser 360*69b46751STobias Grosser /// Print C code corresponding to the control flow in @p Kernel. 361*69b46751STobias Grosser /// 362*69b46751STobias Grosser /// @param Kernel The kernel to print 363*69b46751STobias Grosser void printKernel(ppcg_kernel *Kernel) { 364*69b46751STobias Grosser auto *P = isl_printer_to_str(S->getIslCtx()); 365*69b46751STobias Grosser P = isl_printer_set_output_format(P, ISL_FORMAT_C); 366*69b46751STobias Grosser auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 367*69b46751STobias Grosser P = isl_ast_node_print(Kernel->tree, P, Options); 368*69b46751STobias Grosser char *String = isl_printer_get_str(P); 369*69b46751STobias Grosser printf("%s\n", String); 370*69b46751STobias Grosser free(String); 371*69b46751STobias Grosser isl_printer_free(P); 372*69b46751STobias Grosser } 373*69b46751STobias Grosser 374*69b46751STobias Grosser /// Print C code corresponding to the GPU code described by @p Tree. 375*69b46751STobias Grosser /// 376*69b46751STobias Grosser /// @param Tree An AST describing GPU code 377*69b46751STobias Grosser /// @param PPCGProg The PPCG program from which @Tree has been constructed. 378*69b46751STobias Grosser void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) { 379*69b46751STobias Grosser auto *P = isl_printer_to_str(S->getIslCtx()); 380*69b46751STobias Grosser P = isl_printer_set_output_format(P, ISL_FORMAT_C); 381*69b46751STobias Grosser 382*69b46751STobias Grosser PrintGPUUserData Data; 383*69b46751STobias Grosser Data.PPCGProg = PPCGProg; 384*69b46751STobias Grosser 385*69b46751STobias Grosser auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 386*69b46751STobias Grosser Options = 387*69b46751STobias Grosser isl_ast_print_options_set_print_user(Options, printHostUser, &Data); 388*69b46751STobias Grosser P = isl_ast_node_print(Tree, P, Options); 389*69b46751STobias Grosser char *String = isl_printer_get_str(P); 390*69b46751STobias Grosser printf("# host\n"); 391*69b46751STobias Grosser printf("%s\n", String); 392*69b46751STobias Grosser free(String); 393*69b46751STobias Grosser isl_printer_free(P); 394*69b46751STobias Grosser 395*69b46751STobias Grosser for (auto Kernel : Data.Kernels) { 396*69b46751STobias Grosser printf("# kernel%d\n", Kernel->id); 397*69b46751STobias Grosser printKernel(Kernel); 398*69b46751STobias Grosser } 399*69b46751STobias Grosser } 400*69b46751STobias Grosser 401f384594dSTobias Grosser // Generate a GPU program using PPCG. 402f384594dSTobias Grosser // 403f384594dSTobias Grosser // GPU mapping consists of multiple steps: 404f384594dSTobias Grosser // 405f384594dSTobias Grosser // 1) Compute new schedule for the program. 406f384594dSTobias Grosser // 2) Map schedule to GPU (TODO) 407f384594dSTobias Grosser // 3) Generate code for new schedule (TODO) 408f384594dSTobias Grosser // 409f384594dSTobias Grosser // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer 410f384594dSTobias Grosser // is mostly CPU specific. Instead, we use PPCG's GPU code generation 411f384594dSTobias Grosser // strategy directly from this pass. 412f384594dSTobias Grosser gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) { 413f384594dSTobias Grosser 414f384594dSTobias Grosser auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen); 415f384594dSTobias Grosser 416f384594dSTobias Grosser PPCGGen->ctx = S->getIslCtx(); 417f384594dSTobias Grosser PPCGGen->options = PPCGScop->options; 418f384594dSTobias Grosser PPCGGen->print = nullptr; 419f384594dSTobias Grosser PPCGGen->print_user = nullptr; 42060c60025STobias Grosser PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt; 421f384594dSTobias Grosser PPCGGen->prog = PPCGProg; 422f384594dSTobias Grosser PPCGGen->tree = nullptr; 423f384594dSTobias Grosser PPCGGen->types.n = 0; 424f384594dSTobias Grosser PPCGGen->types.name = nullptr; 425f384594dSTobias Grosser PPCGGen->sizes = nullptr; 426f384594dSTobias Grosser PPCGGen->used_sizes = nullptr; 427f384594dSTobias Grosser PPCGGen->kernel_id = 0; 428f384594dSTobias Grosser 429f384594dSTobias Grosser // Set scheduling strategy to same strategy PPCG is using. 430f384594dSTobias Grosser isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true); 431f384594dSTobias Grosser isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true); 432f384594dSTobias Grosser 433f384594dSTobias Grosser isl_schedule *Schedule = get_schedule(PPCGGen); 434f384594dSTobias Grosser 435aef5196fSTobias Grosser int has_permutable = has_any_permutable_node(Schedule); 436aef5196fSTobias Grosser 437*69b46751STobias Grosser if (!has_permutable || has_permutable < 0) { 438aef5196fSTobias Grosser Schedule = isl_schedule_free(Schedule); 439*69b46751STobias Grosser } else { 440aef5196fSTobias Grosser Schedule = map_to_device(PPCGGen, Schedule); 441*69b46751STobias Grosser PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule)); 442*69b46751STobias Grosser } 443aef5196fSTobias Grosser 444f384594dSTobias Grosser if (DumpSchedule) { 445f384594dSTobias Grosser isl_printer *P = isl_printer_to_str(S->getIslCtx()); 446f384594dSTobias Grosser P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); 447f384594dSTobias Grosser P = isl_printer_print_str(P, "Schedule\n"); 448f384594dSTobias Grosser P = isl_printer_print_str(P, "========\n"); 449f384594dSTobias Grosser if (Schedule) 450f384594dSTobias Grosser P = isl_printer_print_schedule(P, Schedule); 451f384594dSTobias Grosser else 452f384594dSTobias Grosser P = isl_printer_print_str(P, "No schedule found\n"); 453f384594dSTobias Grosser 454f384594dSTobias Grosser printf("%s\n", isl_printer_get_str(P)); 455f384594dSTobias Grosser isl_printer_free(P); 456f384594dSTobias Grosser } 457f384594dSTobias Grosser 458*69b46751STobias Grosser if (DumpCode) { 459*69b46751STobias Grosser printf("Code\n"); 460*69b46751STobias Grosser printf("====\n"); 461*69b46751STobias Grosser if (PPCGGen->tree) 462*69b46751STobias Grosser printGPUTree(PPCGGen->tree, PPCGProg); 463*69b46751STobias Grosser else 464*69b46751STobias Grosser printf("No code generated\n"); 465*69b46751STobias Grosser } 466*69b46751STobias Grosser 467f384594dSTobias Grosser isl_schedule_free(Schedule); 468f384594dSTobias Grosser 469f384594dSTobias Grosser return PPCGGen; 470f384594dSTobias Grosser } 471f384594dSTobias Grosser 472f384594dSTobias Grosser /// Free gpu_gen structure. 473f384594dSTobias Grosser /// 474f384594dSTobias Grosser /// @param PPCGGen The ppcg_gen object to free. 475f384594dSTobias Grosser void freePPCGGen(gpu_gen *PPCGGen) { 476f384594dSTobias Grosser isl_ast_node_free(PPCGGen->tree); 477f384594dSTobias Grosser isl_union_map_free(PPCGGen->sizes); 478f384594dSTobias Grosser isl_union_map_free(PPCGGen->used_sizes); 479f384594dSTobias Grosser free(PPCGGen); 480f384594dSTobias Grosser } 481f384594dSTobias Grosser 482e938517eSTobias Grosser bool runOnScop(Scop &CurrentScop) override { 483e938517eSTobias Grosser S = &CurrentScop; 484e938517eSTobias Grosser 485e938517eSTobias Grosser auto PPCGScop = createPPCGScop(); 486e938517eSTobias Grosser auto PPCGProg = createPPCGProg(PPCGScop); 487f384594dSTobias Grosser auto PPCGGen = generateGPU(PPCGScop, PPCGProg); 488f384594dSTobias Grosser freePPCGGen(PPCGGen); 489e938517eSTobias Grosser gpu_prog_free(PPCGProg); 490e938517eSTobias Grosser ppcg_scop_free(PPCGScop); 491e938517eSTobias Grosser 492e938517eSTobias Grosser return true; 493e938517eSTobias Grosser } 4949dfe4e7cSTobias Grosser 4959dfe4e7cSTobias Grosser void printScop(raw_ostream &, Scop &) const override {} 4969dfe4e7cSTobias Grosser 4979dfe4e7cSTobias Grosser void getAnalysisUsage(AnalysisUsage &AU) const override { 4989dfe4e7cSTobias Grosser AU.addRequired<DominatorTreeWrapperPass>(); 4999dfe4e7cSTobias Grosser AU.addRequired<RegionInfoPass>(); 5009dfe4e7cSTobias Grosser AU.addRequired<ScalarEvolutionWrapperPass>(); 5019dfe4e7cSTobias Grosser AU.addRequired<ScopDetection>(); 5029dfe4e7cSTobias Grosser AU.addRequired<ScopInfoRegionPass>(); 5039dfe4e7cSTobias Grosser AU.addRequired<LoopInfoWrapperPass>(); 5049dfe4e7cSTobias Grosser 5059dfe4e7cSTobias Grosser AU.addPreserved<AAResultsWrapperPass>(); 5069dfe4e7cSTobias Grosser AU.addPreserved<BasicAAWrapperPass>(); 5079dfe4e7cSTobias Grosser AU.addPreserved<LoopInfoWrapperPass>(); 5089dfe4e7cSTobias Grosser AU.addPreserved<DominatorTreeWrapperPass>(); 5099dfe4e7cSTobias Grosser AU.addPreserved<GlobalsAAWrapperPass>(); 5109dfe4e7cSTobias Grosser AU.addPreserved<PostDominatorTreeWrapperPass>(); 5119dfe4e7cSTobias Grosser AU.addPreserved<ScopDetection>(); 5129dfe4e7cSTobias Grosser AU.addPreserved<ScalarEvolutionWrapperPass>(); 5139dfe4e7cSTobias Grosser AU.addPreserved<SCEVAAWrapperPass>(); 5149dfe4e7cSTobias Grosser 5159dfe4e7cSTobias Grosser // FIXME: We do not yet add regions for the newly generated code to the 5169dfe4e7cSTobias Grosser // region tree. 5179dfe4e7cSTobias Grosser AU.addPreserved<RegionInfoPass>(); 5189dfe4e7cSTobias Grosser AU.addPreserved<ScopInfoRegionPass>(); 5199dfe4e7cSTobias Grosser } 5209dfe4e7cSTobias Grosser }; 5219dfe4e7cSTobias Grosser } 5229dfe4e7cSTobias Grosser 5239dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1; 5249dfe4e7cSTobias Grosser 5259dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); } 5269dfe4e7cSTobias Grosser 5279dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg", 5289dfe4e7cSTobias Grosser "Polly - Apply PPCG translation to SCOP", false, false) 5299dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 5309dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 5319dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 5329dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 5339dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 5349dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection); 5359dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg", 5369dfe4e7cSTobias Grosser "Polly - Apply PPCG translation to SCOP", false, false) 537