/** * Add to sets coveredNodes and coveredArcs the cfg arcs that are now * covered given the new region containing the translations in * selectedVec. */ static void markCovered(const TransCFG& cfg, const RegionDescPtr region, const TransIDVec& selectedVec, const TransIDSet heads, TransIDSet& coveredNodes, TransCFG::ArcPtrSet& coveredArcs) { assert(selectedVec.size() > 0); TransID newHead = selectedVec[0]; assert(!region->empty()); assert(newHead == getTransId(region->entry()->id())); // Mark all region's nodes as covered. coveredNodes.insert(selectedVec.begin(), selectedVec.end()); // Mark all incoming arcs into newHead from covered nodes as covered. for (auto arc : cfg.inArcs(newHead)) { TransID src = arc->src(); if (coveredNodes.count(src)) { coveredArcs.insert(arc); } } // Mark all CFG arcs within the region as covered. region->forEachArc([&](RegionDesc::BlockId src, RegionDesc::BlockId dst) { if (!hasTransId(src) || !hasTransId(dst)) return; TransID srcTid = getTransId(src); TransID dstTid = getTransId(dst); assert(cfg.hasArc(srcTid, dstTid)); bool foundArc = false; for (auto arc : cfg.outArcs(srcTid)) { if (arc->dst() == dstTid) { coveredArcs.insert(arc); foundArc = true; } } always_assert(foundArc); }); // Mark all outgoing arcs from the region to a head node as covered. for (auto node : selectedVec) { for (auto arc : cfg.outArcs(node)) { if (heads.count(arc->dst())) { coveredArcs.insert(arc); } } } }
static bool allArcsCovered(const TransCFG::ArcPtrVec& arcs, const TransCFG::ArcPtrSet& coveredArcs) { for (auto arc : arcs) { if (!coveredArcs.count(arc)) { return false; } } return true; }
/** * Add to sets coveredNodes and coveredArcs the cfg arcs that are now * covered given the new region containing the translations in * selectedVec. */ static void markCovered(const TransCFG& cfg, const TransIDVec selectedVec, const TransIDSet heads, TransIDSet& coveredNodes, TransCFG::ArcPtrSet& coveredArcs) { assert(selectedVec.size() > 0); TransID newHead = selectedVec[0]; // Mark all region's nodes as covered. coveredNodes.insert(selectedVec.begin(), selectedVec.end()); // Mark all incoming arcs into newHead from covered nodes as covered. for (auto arc : cfg.inArcs(newHead)) { TransID src = arc->src(); if (coveredNodes.count(src)) { coveredArcs.insert(arc); } } // Mark all arcs between consecutive region nodes as covered. for (size_t i = 0; i < selectedVec.size() - 1; i++) { TransID node = selectedVec[i]; TransID next = selectedVec[i + 1]; bool foundArc = false; for (auto arc : cfg.outArcs(node)) { if (arc->dst() == next) { coveredArcs.insert(arc); foundArc = true; } } always_assert(foundArc); } // Mark all outgoing arcs from the region to a head node as covered. for (auto node : selectedVec) { for (auto arc : cfg.outArcs(node)) { if (heads.count(arc->dst())) { coveredArcs.insert(arc); } } } }
/** * Regionize a func, so that each node and each arc in its TransCFG is * "covered". A node is covered if any region contains it. An arc T1->T2 * is covered if either: * * a) T1 and T2 are in the same region R and T2 immediately follows * T1 in R. * b) T2 is the head (first translation) of a region. * * Basic algorithm: * * 1) sort nodes in decreasing weight order * 2) for each node N: * 2.1) if N and all its incoming arcs are covered, then continue * 2.2) select a region starting at this node and mark nodes/arcs as * covered appropriately */ void regionizeFunc(const Func* func, MCGenerator* mcg, RegionVec& regions) { const Timer rf_timer(Timer::regionizeFunc); assert(RuntimeOption::EvalJitPGO); auto const funcId = func->getFuncId(); auto const profData = mcg->tx().profData(); TransCFG cfg(funcId, profData, mcg->tx().getSrcDB(), mcg->getJmpToTransIDMap()); if (Trace::moduleEnabled(HPHP::Trace::pgo, 5)) { auto dotFileName = folly::to<std::string>( "/tmp/func-cfg-", funcId, ".dot"); cfg.print(dotFileName, funcId, profData, nullptr); FTRACE(5, "regionizeFunc: initial CFG for func {} saved to file {}\n", funcId, dotFileName); } TransCFG::ArcPtrVec arcs = cfg.arcs(); std::vector<TransID> nodes = cfg.nodes(); std::sort(nodes.begin(), nodes.end(), [&](TransID tid1, TransID tid2) -> bool { if (RuntimeOption::EvalJitPGORegionSelector == "wholecfg") { auto bcOff1 = profData->transStartBcOff(tid1); auto bcOff2 = profData->transStartBcOff(tid2); if (bcOff1 != bcOff2) return bcOff1 < bcOff2; } if (cfg.weight(tid1) != cfg.weight(tid2)) { return cfg.weight(tid1) > cfg.weight(tid2); } // In case of ties, pick older translations first, in an // attempt to start loops at their headers. return tid1 < tid2; }); TransCFG::ArcPtrSet coveredArcs; TransIDSet coveredNodes; TransIDSet heads; TransIDToRegionMap headToRegion; RegionToTransIDsMap regionToTransIds; regions.clear(); for (auto node : nodes) { if (!coveredNodes.count(node) || !allArcsCovered(cfg.inArcs(node), coveredArcs)) { TransID newHead = node; FTRACE(6, "regionizeFunc: selecting trace to cover node {}\n", newHead); TransIDSet selectedSet; TransIDVec selectedVec; RegionDescPtr region; if (RuntimeOption::EvalJitPGORegionSelector == "hottrace") { region = selectHotTrace(newHead, profData, cfg, selectedSet, &selectedVec); } else if (RuntimeOption::EvalJitPGORegionSelector == "wholecfg") { region = selectWholeCFG(newHead, profData, cfg, selectedSet, &selectedVec); } else { always_assert(0 && "Invalid value for EvalJitPGORegionSelector"); } FTRACE(6, "regionizeFunc: selected region to cover node {}\n{}\n", newHead, show(*region)); profData->setOptimized(profData->transSrcKey(newHead)); assert(selectedVec.size() > 0 && selectedVec[0] == newHead); regions.push_back(region); heads.insert(newHead); markCovered(cfg, region, selectedVec, heads, coveredNodes, coveredArcs); regionToTransIds[region] = selectedVec; headToRegion[newHead] = region; FTRACE(6, "regionizeFunc: selected trace: {}\n", folly::join(", ", selectedVec)); } } assert(coveredNodes.size() == cfg.nodes().size()); assert(coveredArcs.size() == arcs.size()); sortRegions(regions, func, cfg, profData, headToRegion, regionToTransIds); if (debug && Trace::moduleEnabled(HPHP::Trace::pgo, 5)) { FTRACE(5, "\n--------------------------------------------\n" "regionizeFunc({}): computed regions:\n", funcId); for (auto region : regions) { FTRACE(5, "{}\n\n", show(*region)); } } }
/** * Regionize a func, so that each node and each arc in its TransCFG is * "covered". A node is covered if any region contains it. An arc T1->T2 * is covered if either: * * a) T1 and T2 are in the same region R and T2 immediately follows * T1 in R. * b) T2 is the head (first translation) of a region. * * Basic algorithm: * * 1) sort nodes in decreasing weight order * 2) for each node N: * 2.1) if N and all its incoming arcs are covered, then continue * 2.2) select a region starting at this node and mark nodes/arcs as * covered appropriately */ void regionizeFunc(const Func* func, Transl::TranslatorX64* tx64, RegionVec& regions) { assert(RuntimeOption::EvalJitPGO); FuncId funcId = func->getFuncId(); ProfData* profData = tx64->profData(); TransCFG cfg(funcId, profData, tx64->getSrcDB(), tx64->getJmpToTransIDMap()); if (Trace::moduleEnabled(HPHP::Trace::pgo, 5)) { string dotFileName = folly::to<string>("/tmp/func-cfg-", funcId, ".dot"); cfg.print(dotFileName, funcId, profData, nullptr); FTRACE(5, "regionizeFunc: initial CFG for func {} saved to file {}\n", funcId, dotFileName); } TransCFG::ArcPtrVec arcs = cfg.arcs(); vector<TransID> nodes = cfg.nodes(); std::sort(nodes.begin(), nodes.end(), [&](TransID tid1, TransID tid2) -> bool { if (cfg.weight(tid1) != cfg.weight(tid2)) { return cfg.weight(tid1) > cfg.weight(tid2); } // In case of ties, pick older translations first, in an // attempt to start loops at their headers. return tid1 < tid2; }); TransCFG::ArcPtrSet coveredArcs; TransIDSet coveredNodes; TransIDSet heads; TransIDToRegionMap headToRegion; RegionToTransIDsMap regionToTransIds; regions.clear(); for (auto node : nodes) { if (!setContains(coveredNodes, node) || !allArcsCovered(cfg.inArcs(node), coveredArcs)) { TransID newHead = node; FTRACE(6, "regionizeFunc: selecting trace to cover node {}\n", newHead); TransIDSet selectedSet; TransIDVec selectedVec; RegionDescPtr region = selectHotTrace(newHead, profData, cfg, selectedSet, &selectedVec); profData->setOptimized(profData->transSrcKey(newHead)); assert(selectedVec.size() > 0 && selectedVec[0] == newHead); regions.push_back(region); heads.insert(newHead); markCovered(cfg, selectedVec, heads, coveredNodes, coveredArcs); regionToTransIds[region] = selectedVec; headToRegion[newHead] = region; FTRACE(6, "regionizeFunc: selected trace: {}\n", folly::join(", ", selectedVec)); } } assert(coveredNodes.size() == cfg.nodes().size()); assert(coveredArcs.size() == arcs.size()); sortRegion(regions, func, cfg, profData, headToRegion, regionToTransIds); if (debug && Trace::moduleEnabled(HPHP::Trace::pgo, 5)) { FTRACE(5, "\n--------------------------------------------\n" "regionizeFunc({}): computed regions:\n", funcId); for (auto region : regions) { FTRACE(5, "{}\n\n", show(*region)); } } }