Commit 8a78ab27 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Refcounting-pass optimization

Don't use ValueMap::operator[], since that is much more
expensive than lookup().
parent 013e6135
...@@ -520,6 +520,8 @@ bool endingRefsDifferent(const BlockMap& lhs, const BlockMap& rhs) { ...@@ -520,6 +520,8 @@ bool endingRefsDifferent(const BlockMap& lhs, const BlockMap& rhs) {
} }
void RefcountTracker::addRefcounts(IRGenState* irstate) { void RefcountTracker::addRefcounts(IRGenState* irstate) {
Timer _t("refcounting");
llvm::Function* f = irstate->getLLVMFunction(); llvm::Function* f = irstate->getLLVMFunction();
RefcountTracker* rt = irstate->getRefcounts(); RefcountTracker* rt = irstate->getRefcounts();
...@@ -693,7 +695,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -693,7 +695,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
for (auto v : tracked_values) { for (auto v : tracked_values) {
// hash = hash * 31 + std::hash<llvm::StringRef>()(v->getName()); // hash = hash * 31 + std::hash<llvm::StringRef>()(v->getName());
assert(rt->vars.count(v)); assert(rt->vars.count(v));
auto refstate = rt->vars[v]; const auto refstate = rt->vars.lookup(v);
int min_refs = 1000000000; int min_refs = 1000000000;
for (auto SBB : successors) { for (auto SBB : successors) {
...@@ -752,12 +754,12 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -752,12 +754,12 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
for (auto v : rt->refs_consumed[&I]) { for (auto v : rt->refs_consumed[&I]) {
num_consumed_by_inst[v]++; num_consumed_by_inst[v]++;
assert(rt->vars[v].reftype != RefType::UNKNOWN); assert(rt->vars.count(v) && rt->vars.lookup(v).reftype != RefType::UNKNOWN);
num_times_as_op[v]; // just make sure it appears in there num_times_as_op[v]; // just make sure it appears in there
} }
for (auto v : rt->refs_used[&I]) { for (auto v : rt->refs_used[&I]) {
assert(rt->vars[v].reftype != RefType::UNKNOWN); assert(rt->vars.lookup(v).reftype != RefType::UNKNOWN);
num_times_as_op[v]++; num_times_as_op[v]++;
} }
...@@ -783,15 +785,19 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -783,15 +785,19 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
//llvm::outs() << "Last use of " << *op << " is at " << I << "; adding a decref after\n"; //llvm::outs() << "Last use of " << *op << " is at " << I << "; adding a decref after\n";
if (llvm::InvokeInst* invoke = llvm::dyn_cast<llvm::InvokeInst>(&I)) { if (llvm::InvokeInst* invoke = llvm::dyn_cast<llvm::InvokeInst>(&I)) {
state.decrefs.push_back(RefOp({op, rt->vars[op].nullable, 1, NULL, invoke->getNormalDest(), bb})); state.decrefs.push_back(
state.decrefs.push_back(RefOp({op, rt->vars[op].nullable, 1, NULL, invoke->getUnwindDest(), bb})); RefOp({ op, rt->vars.lookup(op).nullable, 1, NULL, invoke->getNormalDest(), bb }));
state.decrefs.push_back(
RefOp({ op, rt->vars.lookup(op).nullable, 1, NULL, invoke->getUnwindDest(), bb }));
} else { } else {
assert(&I != I.getParent()->getTerminator()); assert(&I != I.getParent()->getTerminator());
auto next = I.getNextNode(); auto next = I.getNextNode();
//while (llvm::isa<llvm::PHINode>(next)) // while (llvm::isa<llvm::PHINode>(next))
//next = next->getNextNode(); // next = next->getNextNode();
ASSERT(!llvm::isa<llvm::UnreachableInst>(next), "Can't add decrefs after this function..."); ASSERT(!llvm::isa<llvm::UnreachableInst>(next),
state.decrefs.push_back(RefOp({op, rt->vars[op].nullable, 1, next, NULL, NULL})); "Can't add decrefs after this function...");
state.decrefs.push_back(
RefOp({ op, rt->vars.lookup(op).nullable, 1, next, NULL, NULL }));
} }
state.ending_refs[op] = 1; state.ending_refs[op] = 1;
} }
...@@ -823,7 +829,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -823,7 +829,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
llvm::Instruction* inst = &II; llvm::Instruction* inst = &II;
if (!rt->vars.count(inst)) if (!rt->vars.count(inst))
continue; continue;
auto&& rstate = rt->vars[inst]; const auto&& rstate = rt->vars.lookup(inst);
// hash = hash * 31 + std::hash<llvm::StringRef>()(inst->getName()); // hash = hash * 31 + std::hash<llvm::StringRef>()(inst->getName());
...@@ -880,9 +886,10 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -880,9 +886,10 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
assert(found); assert(found);
} }
#endif #endif
assert(rt->vars[p.first].reftype == RefType::BORROWED); assert(rt->vars.lookup(p.first).reftype == RefType::BORROWED);
state.increfs.push_back(RefOp({p.first, rt->vars[p.first].nullable, p.second, NULL, &BB, NULL})); state.increfs.push_back(
RefOp({ p.first, rt->vars.lookup(p.first).nullable, p.second, NULL, &BB, NULL }));
} }
state.ending_refs.clear(); state.ending_refs.clear();
} }
...@@ -949,6 +956,10 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -949,6 +956,10 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
dumpPrettyIR(f); dumpPrettyIR(f);
fprintf(stderr, "\033[0m"); fprintf(stderr, "\033[0m");
} }
long us = _t.end();
static StatCounter us_refcounting("us_compiling_irgen_refcounting");
us_refcounting.log(us);
} }
} // namespace pyston } // namespace pyston
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment