bool JSONExporter::runOnScop(Scop &S) { Region &R = S.getRegion(); std::string FileName = ImportDir + "/" + getFileName(S); Json::Value jscop = getJSON(S); Json::StyledWriter writer; std::string fileContent = writer.write(jscop); // Write to file. std::error_code EC; tool_output_file F(FileName, EC, llvm::sys::fs::F_Text); std::string FunctionName = R.getEntry()->getParent()->getName(); errs() << "Writing JScop '" << R.getNameStr() << "' in function '" << FunctionName << "' to '" << FileName << "'.\n"; if (!EC) { F.os() << fileContent; F.os().close(); if (!F.os().has_error()) { errs() << "\n"; F.keep(); return false; } } errs() << " error opening file for writing!\n"; F.os().clear_error(); return false; }
bool CloogInfo::runOnScop(Scop &S) { if (C) delete C; scop = &S; C = new Cloog(&S); Function *F = S.getRegion().getEntry()->getParent(); (void)F; DEBUG(dbgs() << ":: " << F->getName()); DEBUG(dbgs() << " : " << S.getRegion().getNameStr() << "\n"); DEBUG(C->pprint(dbgs())); return false; }
std::string JSONImporter::getFileName(Scop &S) const { std::string FunctionName = S.getRegion().getEntry()->getParent()->getName(); std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop"; if (ImportPostfix != "") FileName += "." + ImportPostfix; return FileName; }
__isl_give isl_pw_aff *SCEVAffinator::getPwAff(ScopStmt *Stmt, const SCEV *Scev) { Scop *S = Stmt->getParent(); const Region *Reg = &S->getRegion(); S->addParams(getParamsInAffineExpr(Reg, Scev, *S->getSE())); SCEVAffinator Affinator(Stmt); return Affinator.visit(Scev); }
void IslAstInfo::printScop(raw_ostream &OS, Scop &S) const { isl_ast_print_options *Options; isl_ast_node *RootNode = getAst(); Function *F = S.getRegion().getEntry()->getParent(); OS << ":: isl ast :: " << F->getName() << " :: " << S.getRegion().getNameStr() << "\n"; if (!RootNode) { OS << ":: isl ast generation and code generation was skipped!\n\n"; OS << ":: This is either because no useful optimizations could be applied " "(use -polly-process-unprofitable to enforce code generation) or " "because earlier passes such as dependence analysis timed out (use " "-polly-dependences-computeout=0 to set dependence analysis timeout " "to infinity)\n\n"; return; } isl_ast_expr *RunCondition = getRunCondition(); char *RtCStr, *AstStr; Options = isl_ast_print_options_alloc(S.getIslCtx()); Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr); isl_printer *P = isl_printer_to_str(S.getIslCtx()); P = isl_printer_print_ast_expr(P, RunCondition); RtCStr = isl_printer_get_str(P); P = isl_printer_flush(P); P = isl_printer_indent(P, 4); P = isl_printer_set_output_format(P, ISL_FORMAT_C); P = isl_ast_node_print(RootNode, P, Options); AstStr = isl_printer_get_str(P); isl_union_map *Schedule = isl_union_map_intersect_domain(S.getSchedule(), S.getDomains()); DEBUG({ dbgs() << S.getContextStr() << "\n"; dbgs() << stringFromIslObj(Schedule); });
bool polly::canSynthesize(const Value *V, const Scop &S, const llvm::LoopInfo *LI, ScalarEvolution *SE, Loop *Scope) { if (!V || !SE->isSCEVable(V->getType())) return false; if (const SCEV *Scev = SE->getSCEVAtScope(const_cast<Value *>(V), Scope)) if (!isa<SCEVCouldNotCompute>(Scev)) if (!hasScalarDepsInsideRegion(Scev, &S.getRegion(), Scope, false)) return true; return false; }
Json::Value JSONExporter::getJSON(Scop &S) const { Json::Value root; unsigned LineBegin, LineEnd; std::string FileName; getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName); std::string Location; if (LineBegin != (unsigned)-1) Location = FileName + ":" + std::to_string(LineBegin) + "-" + std::to_string(LineEnd); root["name"] = S.getRegion().getNameStr(); root["context"] = S.getContextStr(); if (LineBegin != (unsigned)-1) root["location"] = Location; root["statements"]; for (ScopStmt &Stmt : S) { Json::Value statement; statement["name"] = Stmt.getBaseName(); statement["domain"] = Stmt.getDomainStr(); statement["schedule"] = Stmt.getScheduleStr(); statement["accesses"]; for (MemoryAccess *MA : Stmt) { Json::Value access; access["kind"] = MA->isRead() ? "read" : "write"; access["relation"] = MA->getOriginalAccessRelationStr(); statement["accesses"].append(access); } root["statements"].append(statement); } return root; }
bool CloogExporter::runOnScop(Scop &S) { Region &R = S.getRegion(); CloogInfo &C = getAnalysis<CloogInfo>(); std::string FunctionName = R.getEntry()->getParent()->getNameStr(); std::string Filename = getFileName(&R); errs() << "Writing Scop '" << R.getNameStr() << "' in function '" << FunctionName << "' to '" << Filename << "'...\n"; FILE *F = fopen(Filename.c_str(), "w"); C.dump(F); fclose(F); return false; }
bool AliasCheckGenerator::runOnScop(Scop &S) { SD = &getAnalysis<ScopDetection>(); Region &ScopRegion = S.getRegion(); for (ScopDetection::reject_iterator RI = SD->reject_begin(), RE = SD->reject_end(); RI != RE; ++RI) { const Region *R = RI->first; RejectLog &Log = RI->second; // for (RejectReasonPtr RRPtr : Log) { // if (ReportAlias *AliasError = dyn_cast<ReportAlias>(RRPtr.get())) { // if (R == &ScopRegion) // } //} } return true; }
__isl_give isl_pw_aff * SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) { assert(Expr->isAffine() && "Only affine AddRecurrences allowed"); // Directly generate isl_pw_aff for Expr if 'start' is zero. if (Expr->getStart()->isZero()) { assert(S->getRegion().contains(Expr->getLoop()) && "Scop does not contain the loop referenced in this AddRec"); isl_pw_aff *Start = visit(Expr->getStart()); isl_pw_aff *Step = visit(Expr->getOperand(1)); isl_space *Space = isl_space_set_alloc(Ctx, 0, NbLoopSpaces); isl_local_space *LocalSpace = isl_local_space_from_space(Space); int loopDimension = getLoopDepth(Expr->getLoop()); isl_aff *LAff = isl_aff_set_coefficient_si( isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1); isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff); // TODO: Do we need to check for NSW and NUW? return isl_pw_aff_add(Start, isl_pw_aff_mul(Step, LPwAff)); } // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' // if 'start' is not zero. ScalarEvolution &SE = *S->getSE(); const SCEV *ZeroStartExpr = SE.getAddRecExpr( SE.getConstant(Expr->getStart()->getType(), 0), Expr->getStepRecurrence(SE), Expr->getLoop(), SCEV::FlagAnyWrap); isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr); isl_pw_aff *Start = visit(Expr->getStart()); return isl_pw_aff_add(ZeroStartResult, Start); }
void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) { createScalarInitialization(S.getRegion(), GlobalMap); createScalarFinalization(S.getRegion()); }
int SCEVAffinator::getLoopDepth(const Loop *L) { Loop *outerLoop = S->getRegion().outermostLoopInRegion(const_cast<Loop *>(L)); assert(outerLoop && "Scop does not contain this loop"); return L->getLoopDepth() - outerLoop->getLoopDepth(); }
/// @brief Generate LLVM-IR for the SCoP @p S. bool runOnScop(Scop &S) override { AI = &getAnalysis<IslAstInfo>(); // Check if we created an isl_ast root node, otherwise exit. isl_ast_node *AstRoot = AI->getAst(); if (!AstRoot) return false; LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); Region *R = &S.getRegion(); assert(!R->isTopLevelRegion() && "Top level regions are not supported"); ScopAnnotator Annotator; Annotator.buildAliasScopes(S); simplifyRegion(R, DT, LI, RI); assert(R->isSimple()); BasicBlock *EnteringBB = S.getRegion().getEnteringBlock(); assert(EnteringBB); PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); // Only build the run-time condition and parameters _after_ having // introduced the conditional branch. This is important as the conditional // branch will guard the original scop from new induction variables that // the SCEVExpander may introduce while code generating the parameters and // which may introduce scalar dependences that prevent us from correctly // code generating this scop. BasicBlock *StartBlock = executeScopConditionally(S, this, Builder.getTrue()); auto SplitBlock = StartBlock->getSinglePredecessor(); // First generate code for the hoisted invariant loads and transitively the // parameters they reference. Afterwards, for the remaining parameters that // might reference the hoisted loads. Finally, build the runtime check // that might reference both hoisted loads as well as parameters. // If the hoisting fails we have to bail and execute the original code. Builder.SetInsertPoint(SplitBlock->getTerminator()); if (!NodeBuilder.preloadInvariantLoads()) { auto *FalseI1 = Builder.getFalse(); auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator(); SplitBBTerm->setOperand(0, FalseI1); auto *StartBBTerm = StartBlock->getTerminator(); Builder.SetInsertPoint(StartBBTerm); Builder.CreateUnreachable(); StartBBTerm->eraseFromParent(); isl_ast_node_free(AstRoot); } else { NodeBuilder.addParameters(S.getContext()); Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC); Builder.SetInsertPoint(&StartBlock->front()); NodeBuilder.create(AstRoot); NodeBuilder.finalizeSCoP(S); fixRegionInfo(EnteringBB->getParent(), R->getParent()); } verifyGeneratedFunction(S, *EnteringBB->getParent()); // Mark the function such that we run additional cleanup passes on this // function (e.g. mem2reg to rediscover phi nodes). Function *F = EnteringBB->getParent(); F->addFnAttr("polly-optimized"); return true; }
std::pair<polly::BBPair, BranchInst *> polly::executeScopConditionally(Scop &S, Value *RTC, DominatorTree &DT, RegionInfo &RI, LoopInfo &LI) { Region &R = S.getRegion(); PollyIRBuilder Builder(S.getEntry()); // Before: // // \ / // // EnteringBB // // _____|_____ // // / EntryBB \ // // | (region) | // // \_ExitingBB_/ // // | // // ExitBB // // / \ // // Create a fork block. BasicBlock *EnteringBB = S.getEnteringBlock(); BasicBlock *EntryBB = S.getEntry(); assert(EnteringBB && "Must be a simple region"); BasicBlock *SplitBlock = splitEdge(EnteringBB, EntryBB, ".split_new_and_old", &DT, &LI, &RI); SplitBlock->setName("polly.split_new_and_old"); // If EntryBB is the exit block of the region that includes Prev, exclude // SplitBlock from that region by making it itself the exit block. This is // trivially possible because there is just one edge to EnteringBB. // This is necessary because we will add an outgoing edge from SplitBlock, // which would violate the single exit block requirement of PrevRegion. Region *PrevRegion = RI.getRegionFor(EnteringBB); while (PrevRegion->getExit() == EntryBB) { PrevRegion->replaceExit(SplitBlock); PrevRegion = PrevRegion->getParent(); } RI.setRegionFor(SplitBlock, PrevRegion); // Create a join block BasicBlock *ExitingBB = S.getExitingBlock(); BasicBlock *ExitBB = S.getExit(); assert(ExitingBB && "Must be a simple region"); BasicBlock *MergeBlock = splitEdge(ExitingBB, ExitBB, ".merge_new_and_old", &DT, &LI, &RI); MergeBlock->setName("polly.merge_new_and_old"); // Exclude the join block from the region. R.replaceExitRecursive(MergeBlock); RI.setRegionFor(MergeBlock, R.getParent()); // \ / // // EnteringBB // // | // // SplitBlock // // _____|_____ // // / EntryBB \ // // | (region) | // // \_ExitingBB_/ // // | // // MergeBlock // // | // // ExitBB // // / \ // // Create the start and exiting block. Function *F = SplitBlock->getParent(); BasicBlock *StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F); BasicBlock *ExitingBlock = BasicBlock::Create(F->getContext(), "polly.exiting", F); SplitBlock->getTerminator()->eraseFromParent(); Builder.SetInsertPoint(SplitBlock); BranchInst *CondBr = Builder.CreateCondBr(RTC, StartBlock, S.getEntry()); if (Loop *L = LI.getLoopFor(SplitBlock)) { L->addBasicBlockToLoop(StartBlock, LI); L->addBasicBlockToLoop(ExitingBlock, LI); } DT.addNewBlock(StartBlock, SplitBlock); DT.addNewBlock(ExitingBlock, StartBlock); RI.setRegionFor(StartBlock, RI.getRegionFor(SplitBlock)); RI.setRegionFor(ExitingBlock, RI.getRegionFor(SplitBlock)); // \ / // // EnteringBB // // | // // SplitBlock---------\ // // _____|_____ | // // / EntryBB \ StartBlock // // | (region) | | // // \_ExitingBB_/ ExitingBlock // // | // // MergeBlock // // | // // ExitBB // // / \ // // Connect start block to exiting block. Builder.SetInsertPoint(StartBlock); Builder.CreateBr(ExitingBlock); DT.changeImmediateDominator(ExitingBlock, StartBlock); // Connect exiting block to join block. Builder.SetInsertPoint(ExitingBlock); Builder.CreateBr(MergeBlock); DT.changeImmediateDominator(MergeBlock, SplitBlock); // \ / // // EnteringBB // // | // // SplitBlock---------\ // // _____|_____ | // // / EntryBB \ StartBlock // // | (region) | | // // \_ExitingBB_/ ExitingBlock // // | | // // MergeBlock---------/ // // | // // ExitBB // // / \ // // // Split the edge between SplitBlock and EntryBB, to avoid a critical edge. splitEdge(SplitBlock, EntryBB, ".pre_entry_bb", &DT, &LI, &RI); // \ / // // EnteringBB // // | // // SplitBlock---------\ // // | | // // PreEntryBB | // // _____|_____ | // // / EntryBB \ StartBlock // // | (region) | | // // \_ExitingBB_/ ExitingBlock // // | | // // MergeBlock---------/ // // | // // ExitBB // // / \ // return std::make_pair(std::make_pair(StartBlock, ExitingBlock), CondBr); }
std::string JSONExporter::getFileName(Scop &S) const { std::string FunctionName = S.getRegion().getEntry()->getParent()->getName(); std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop"; return FileName; }
bool JSONImporter::runOnScop(Scop &S) { Region &R = S.getRegion(); const Dependences &D = getAnalysis<DependenceInfo>().getDependences(); const DataLayout &DL = S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); std::string FileName = ImportDir + "/" + getFileName(S); std::string FunctionName = R.getEntry()->getParent()->getName(); errs() << "Reading JScop '" << R.getNameStr() << "' in function '" << FunctionName << "' from '" << FileName << "'.\n"; ErrorOr<std::unique_ptr<MemoryBuffer>> result = MemoryBuffer::getFile(FileName); std::error_code ec = result.getError(); if (ec) { errs() << "File could not be read: " << ec.message() << "\n"; return false; } Json::Reader reader; Json::Value jscop; bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop); if (!parsingSuccessful) { errs() << "JSCoP file could not be parsed\n"; return false; } isl_set *OldContext = S.getContext(); isl_set *NewContext = isl_set_read_from_str(S.getIslCtx(), jscop["context"].asCString()); for (unsigned i = 0; i < isl_set_dim(OldContext, isl_dim_param); i++) { isl_id *id = isl_set_get_dim_id(OldContext, isl_dim_param, i); NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, id); } isl_set_free(OldContext); S.setContext(NewContext); StatementToIslMapTy NewSchedule; int index = 0; for (ScopStmt &Stmt : S) { Json::Value schedule = jscop["statements"][index]["schedule"]; isl_map *m = isl_map_read_from_str(S.getIslCtx(), schedule.asCString()); isl_space *Space = Stmt.getDomainSpace(); // Copy the old tuple id. This is necessary to retain the user pointer, // that stores the reference to the ScopStmt this schedule belongs to. m = isl_map_set_tuple_id(m, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set)); for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { isl_id *id = isl_space_get_dim_id(Space, isl_dim_param, i); m = isl_map_set_dim_id(m, isl_dim_param, i, id); } isl_space_free(Space); NewSchedule[&Stmt] = m; index++; } if (!D.isValidSchedule(S, &NewSchedule)) { errs() << "JScop file contains a schedule that changes the " << "dependences. Use -disable-polly-legality to continue anyways\n"; for (StatementToIslMapTy::iterator SI = NewSchedule.begin(), SE = NewSchedule.end(); SI != SE; ++SI) isl_map_free(SI->second); return false; } auto ScheduleMap = isl_union_map_empty(S.getParamSpace()); for (ScopStmt &Stmt : S) { if (NewSchedule.find(&Stmt) != NewSchedule.end()) ScheduleMap = isl_union_map_add_map(ScheduleMap, NewSchedule[&Stmt]); else ScheduleMap = isl_union_map_add_map(ScheduleMap, Stmt.getSchedule()); } S.setSchedule(ScheduleMap); int statementIdx = 0; for (ScopStmt &Stmt : S) { int memoryAccessIdx = 0; for (MemoryAccess *MA : Stmt) { Json::Value accesses = jscop["statements"][statementIdx]["accesses"] [memoryAccessIdx]["relation"]; isl_map *newAccessMap = isl_map_read_from_str(S.getIslCtx(), accesses.asCString()); isl_map *currentAccessMap = MA->getAccessRelation(); if (isl_map_dim(newAccessMap, isl_dim_param) != isl_map_dim(currentAccessMap, isl_dim_param)) { errs() << "JScop file changes the number of parameter dimensions\n"; isl_map_free(currentAccessMap); isl_map_free(newAccessMap); return false; } isl_id *OutId = isl_map_get_tuple_id(currentAccessMap, isl_dim_out); newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_out, OutId); if (MA->isArrayKind()) { // We keep the old alignment, thus we cannot allow accesses to memory // locations that were not accessed before if the alignment of the // access is not the default alignment. bool SpecialAlignment = true; if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) { SpecialAlignment = DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment(); } else if (StoreInst *StoreI = dyn_cast<StoreInst>(MA->getAccessInstruction())) { SpecialAlignment = DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) != StoreI->getAlignment(); } if (SpecialAlignment) { isl_set *newAccessSet = isl_map_range(isl_map_copy(newAccessMap)); isl_set *currentAccessSet = isl_map_range(isl_map_copy(currentAccessMap)); bool isSubset = isl_set_is_subset(newAccessSet, currentAccessSet); isl_set_free(newAccessSet); isl_set_free(currentAccessSet); if (!isSubset) { errs() << "JScop file changes the accessed memory\n"; isl_map_free(currentAccessMap); isl_map_free(newAccessMap); return false; } } } // We need to copy the isl_ids for the parameter dimensions to the new // map. Without doing this the current map would have different // ids then the new one, even though both are named identically. for (unsigned i = 0; i < isl_map_dim(currentAccessMap, isl_dim_param); i++) { isl_id *id = isl_map_get_dim_id(currentAccessMap, isl_dim_param, i); newAccessMap = isl_map_set_dim_id(newAccessMap, isl_dim_param, i, id); } // Copy the old tuple id. This is necessary to retain the user pointer, // that stores the reference to the ScopStmt this access belongs to. isl_id *Id = isl_map_get_tuple_id(currentAccessMap, isl_dim_in); newAccessMap = isl_map_set_tuple_id(newAccessMap, isl_dim_in, Id); if (!isl_map_has_equal_space(currentAccessMap, newAccessMap)) { errs() << "JScop file contains access function with incompatible " << "dimensions\n"; isl_map_free(currentAccessMap); isl_map_free(newAccessMap); return false; } auto NewAccessDomain = isl_map_domain(isl_map_copy(newAccessMap)); auto CurrentAccessDomain = isl_map_domain(isl_map_copy(currentAccessMap)); NewAccessDomain = isl_set_intersect_params(NewAccessDomain, S.getContext()); CurrentAccessDomain = isl_set_intersect_params(CurrentAccessDomain, S.getContext()); if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) == isl_bool_false) { errs() << "Mapping not defined for all iteration domain elements\n"; isl_set_free(CurrentAccessDomain); isl_set_free(NewAccessDomain); isl_map_free(currentAccessMap); isl_map_free(newAccessMap); return false; } isl_set_free(CurrentAccessDomain); isl_set_free(NewAccessDomain); if (!isl_map_is_equal(newAccessMap, currentAccessMap)) { // Statistics. ++NewAccessMapFound; newAccessStrings.push_back(accesses.asCString()); MA->setNewAccessRelation(newAccessMap); } else { isl_map_free(newAccessMap); } isl_map_free(currentAccessMap); memoryAccessIdx++; } statementIdx++; } return false; }