Skip to content

Commit

Permalink
Merge branch 'master' into logg
Browse files Browse the repository at this point in the history
merge logg
  • Loading branch information
meetesh06 committed Aug 25, 2021
2 parents 1b7d60f + 6080a13 commit 1cc4750
Show file tree
Hide file tree
Showing 22 changed files with 374 additions and 307 deletions.
2 changes: 1 addition & 1 deletion rir/src/compiler/analysis/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class TheVerifier {
}
if (auto assume = Assume::Cast(i)) {
if (IsType::Cast(assume->arg(0).val())) {
if (assume->feedbackOrigin.empty()) {
if (!assume->reason.pc()) {
std::cerr << "Error: instruction '";
i->print(std::cerr);
std::cerr << "' typecheck without origin information\n";
Expand Down
2 changes: 1 addition & 1 deletion rir/src/compiler/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static void lower(Code* code) {
next = it + 1;

} else if (auto expect = Assume::Cast(*it)) {
if (expect->arg(0).val() == True::instance()) {
if (expect->triviallyHolds()) {
next = bb->remove(it);
} else {
auto expectation = expect->assumeTrue;
Expand Down
35 changes: 18 additions & 17 deletions rir/src/compiler/native/lower_function_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class NativeAllocator : public SSAAllocator {
// Ensure we preserve slots for variables with typefeedback to make them
// accessible to the runtime profiler.
// TODO: this needs to be replaced by proper mapping of slots.
if (a != b && (a->typeFeedback().origin || b->typeFeedback().origin))
if (a != b && (a->typeFeedback().feedbackOrigin.pc() ||
b->typeFeedback().feedbackOrigin.pc()))
return true;
return SSAAllocator::interfere(a, b);
}
Expand Down Expand Up @@ -1927,14 +1928,15 @@ llvm::Value* LowerFunctionLLVM::fastVeceltOkNative(llvm::Value* v) {
auto attrs = attr(v);
auto isNil = builder.CreateICmpEQ(attrs, constant(R_NilValue, t::SEXP));
auto ok = builder.CreateAnd(builder.CreateNot(isObj(v)), isNil);
return createSelect2(ok, [&]() { return builder.getTrue(); },
[&]() {
auto isMatr1 = builder.CreateICmpEQ(
tag(attrs), constant(R_DimSymbol, t::SEXP));
auto isMatr2 = builder.CreateICmpEQ(
cdr(attrs), constant(R_NilValue, t::SEXP));
return builder.CreateAnd(isMatr1, isMatr2);
});
return createSelect2(
ok, [&]() { return builder.getTrue(); },
[&]() {
auto isMatr1 = builder.CreateICmpEQ(tag(attrs),
constant(R_DimSymbol, t::SEXP));
auto isMatr2 =
builder.CreateICmpEQ(cdr(attrs), constant(R_NilValue, t::SEXP));
return builder.CreateAnd(isMatr1, isMatr2);
});
};

llvm::Value* LowerFunctionLLVM::isAltrep(llvm::Value* v) {
Expand Down Expand Up @@ -2291,15 +2293,14 @@ void LowerFunctionLLVM::compile() {
llvm::ConstantInt::get(
PirJitLLVM::getContext(),
llvm::APInt(
64, reinterpret_cast<uint64_t>(rec->reason.srcCode),
64,
reinterpret_cast<uint64_t>(rec->reason.srcCode()),
false)),
t::voidPtr);
auto reason = llvm::ConstantStruct::get(
t::DeoptReason, {
c(rec->reason.reason, 32),
srcAddr,
c(rec->reason.originOffset),
});
t::DeoptReason,
{c(rec->reason.reason, 32),
c(rec->reason.origin.offset(), 32), srcAddr});
call(NativeBuiltins::get(NativeBuiltins::Id::recordDeopt),
{loadSxp(rec->arg<0>().val()), globalConst(reason)});
break;
Expand Down Expand Up @@ -5896,12 +5897,12 @@ void LowerFunctionLLVM::compile() {
auto i = var.first;
if (Representation::Of(i) != Representation::Sexp)
continue;
if (!i->typeFeedback().origin)
if (!i->typeFeedback().feedbackOrigin.pc())
continue;
if (!var.second.initialized)
continue;
if (var.second.stackSlot < PirTypeFeedback::MAX_SLOT_IDX) {
codes.insert(i->typeFeedback().srcCode);
codes.insert(i->typeFeedback().feedbackOrigin.srcCode());
variableMapping.emplace(var.second.stackSlot, i->typeFeedback());
#ifdef DEBUG_REGISTER_MAP
assert(!usedSlots.count(var.second.stackSlot));
Expand Down
2 changes: 1 addition & 1 deletion rir/src/compiler/native/types_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void initializeTypes(LLVMContext& context) {
t::RCNTXT->setBody(fields);

t::DeoptReason = StructType::create(context, "DeoptReason");
fields = {t::i32, t::voidPtr, t::i32};
fields = {t::i32, t::i32, t::voidPtr};
t::DeoptReason->setBody(fields, true);

#define DECLARE(name, ret, ...) \
Expand Down
78 changes: 38 additions & 40 deletions rir/src/compiler/opt/assumptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,44 @@ struct AAssumption {
}
explicit AAssumption(Assume* a) : yesNo(a->assumeTrue) {
auto cond = a->condition();
if (auto t = IsType::Cast(cond)) {
kind = Typecheck;
c.typecheck = {t->arg(0).val(), t->typeTest};
} else if (auto t = Identical::Cast(cond)) {
kind = Equality;
c.equality = {t->arg(0).val(), t->arg(1).val()};
} else if (auto t = IsEnvStub::Cast(cond)) {
kind = IsEnvStub;
c.env = t->env();
} else {
kind = Other;
c.misc = cond;
switch (a->reason.reason) {
case DeoptReason::Typecheck: {
if (auto t = IsType::Cast(cond)) {
c.typecheck = {t->arg(0).val(), t->typeTest};
kind = Typecheck;
return;
}
break;
}
}
explicit AAssumption(Branch* b, bool yesNo) : yesNo(yesNo) {
auto cond = b->arg(0).val();
if (auto t = IsType::Cast(cond)) {
kind = Typecheck;
c.typecheck = {t->arg(0).val(), t->typeTest};
} else if (auto t = Identical::Cast(cond)) {
kind = Equality;
c.equality = {t->arg(0).val(), t->arg(1).val()};
} else if (auto t = IsEnvStub::Cast(cond)) {
kind = IsEnvStub;
c.env = t->env();
} else {
kind = Other;
c.misc = cond;
case DeoptReason::Calltarget: {
if (auto t = Identical::Cast(cond)) {
c.equality = {t->arg(0).val(), t->arg(1).val()};
kind = Equality;
return;
}
break;
}
case DeoptReason::EnvStubMaterialized: {
if (auto t = IsEnvStub::Cast(cond)) {
c.env = t->env();
kind = IsEnvStub;
return;
}
break;
}
case DeoptReason::DeadBranchReached:
break;
case DeoptReason::DeadCall:
assert(false);
break;
}

// Fallthrough generic case. Assumptions are identified by the concrete
// condition alone.
kind = Other;
c.misc = cond;
}

AAssumption& operator=(const AAssumption& o) {
yesNo = o.yesNo;
kind = o.kind;
Expand Down Expand Up @@ -253,14 +261,6 @@ bool OptimizeAssumptions::apply(Compiler&, ClosureVersion* vers, Code* code,
return false;
};

if (auto br = Branch::Cast(instr)) {
if (assumptionsIncludes(AAssumption(br, true))) {
br->arg(0).val() = True::instance();
} else if (assumptionsIncludes(AAssumption(br, false))) {
br->arg(0).val() = False::instance();
}
}

if (auto assume = Assume::Cast(instr)) {
if (assumptionsIncludes(AAssumption(assume))) {
anyChange = true;
Expand Down Expand Up @@ -327,11 +327,9 @@ bool OptimizeAssumptions::apply(Compiler&, ClosureVersion* vers, Code* code,
if (h != hoistAssume.end()) {
auto g = h->second;
ip++;
auto assume = new Assume(std::get<0>(g), std::get<1>(g));
assume->feedbackOrigin.insert(
assume->feedbackOrigin.end(),
std::get<2>(g)->feedbackOrigin.begin(),
std::get<2>(g)->feedbackOrigin.end());
auto assume = new Assume(std::get<Instruction*>(g),
std::get<Checkpoint*>(g),
std::get<Assume*>(g)->reason);
ip = bb->insert(ip, assume);
anyChange = true;
}
Expand Down
2 changes: 1 addition & 1 deletion rir/src/compiler/opt/constantfold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ bool Constantfold::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
auto next = ip + 1;

auto killUnreachable = [&]() {
if (ip == bb->end() || Unreachable::Cast(*(ip + 1)))
if (ip == bb->end() || Unreachable::Cast(bb->last()))
return;
ip = bb->insert(ip + 1, new Unreachable()) + 1;
while (ip != bb->end())
Expand Down
31 changes: 20 additions & 11 deletions rir/src/compiler/opt/eager_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
LogStream& log) const {
AvailableCheckpoints checkpoint(cls, code, log);

struct Speculation {
SEXP builtin;
Checkpoint* cp;
FeedbackOrigin origin;
};

auto replaceLdFunBuiltinWithDeopt = [&](BB* bb, BB::Instrs::iterator ip,
Checkpoint* cp, SEXP builtin,
const Speculation& speculation,
LdFun* ldfun) {
assert(LdFun::Cast(*ip));
assert(cp);
assert(speculation.cp);

// skip ldfun
++ip;

auto expected = new LdConst(builtin);
auto expected = new LdConst(speculation.builtin);
ip = bb->insert(ip, expected);
++ip;
Instruction* given = ldfun;
Expand All @@ -54,7 +60,9 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
ip = bb->insert(ip, test);
++ip;

auto assume = new Assume(test, cp);
auto assume = new Assume(
test, speculation.cp,
DeoptReason(speculation.origin, DeoptReason::Calltarget));
ip = bb->insert(ip, assume);
++ip;

Expand Down Expand Up @@ -105,7 +113,8 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
};

// Search for calls that likely point to a builtin.
std::unordered_map<LdFun*, std::pair<SEXP, Checkpoint*>> needsGuard;
std::unordered_map<LdFun*, Speculation> needsGuard;

Visitor::run(code->entry, [&](BB* bb) {
auto ip = bb->begin();
while (ip != bb->end()) {
Expand Down Expand Up @@ -144,8 +153,8 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
Effect::DependsOnAssume));
}
} else if (auto ldfun = LdFun::Cast(call->cls())) {
if (ldfun->hint && !ldfun->hintIsInnerFunction) {
auto kind = TYPEOF(ldfun->hint);
if (ldfun->hint() && !ldfun->hintIsInnerFunction) {
auto kind = TYPEOF(ldfun->hint());
// We also speculate on calls to CLOSXPs, these will
// be picked up by MatchArgs opt pass and turned
// into a static call. TODO, for inner functions we
Expand All @@ -157,9 +166,10 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
if (auto cp = checkpoint.at(ldfun)) {
if (kind == BUILTINSXP) {
ip = replaceCallWithCallBuiltin(
bb, ip, call, ldfun->hint, true);
bb, ip, call, ldfun->hint(), true);
}
needsGuard[ldfun] = {ldfun->hint, cp};
needsGuard[ldfun] = {ldfun->hint(), cp,
ldfun->hintOrigin()};
}
}
} else {
Expand Down Expand Up @@ -218,8 +228,7 @@ bool EagerCalls::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
if (auto ldfun = LdFun::Cast(*ip)) {
auto r = needsGuard.find(ldfun);
if (r != needsGuard.end()) {
ip = replaceLdFunBuiltinWithDeopt(bb, ip, r->second.second,
r->second.first, ldfun);
ip = replaceLdFunBuiltinWithDeopt(bb, ip, r->second, ldfun);
needsGuard.erase(r);
continue;
}
Expand Down
16 changes: 12 additions & 4 deletions rir/src/compiler/opt/elide_env_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ bool ElideEnvSpec::apply(Compiler&, ClosureVersion* cls, Code* code,
arg, seen, suggested, required,
[&](TypeTest::Info info) {
BBTransform::insertAssume(
info.test, cp, bb, ip, info.expectation,
info.srcCode, info.origin);
info.test, info.expectation, cp,
info.feedbackOrigin, DeoptReason::Typecheck,
bb, ip);

if (argi) {
auto cast = new CastType(
Expand Down Expand Up @@ -129,6 +130,7 @@ bool ElideEnvSpec::apply(Compiler&, ClosureVersion* cls, Code* code,
!arg->type.maybeMissing()) {
force->replaceUsesWith(arg);
next = bb->remove(ip);
anyChange = true;
}
}
}
Expand Down Expand Up @@ -281,8 +283,10 @@ bool ElideEnvSpec::apply(Compiler&, ClosureVersion* cls, Code* code,
if (!bannedEnvs.count(env)) {
auto condition = new IsEnvStub(env);
BBTransform::insertAssume(
condition, cp, true,
env->typeFeedback().srcCode, nullptr);
condition, true, cp,
env->typeFeedback().feedbackOrigin,
DeoptReason::EnvStubMaterialized);
anyChange = true;
assert(cp->bb()->trueBranch() != bb);
}
}
Expand All @@ -301,6 +305,7 @@ bool ElideEnvSpec::apply(Compiler&, ClosureVersion* cls, Code* code,
for (auto n : additionalEntries[env]) {
env->varName.push_back(n);
env->pushArg(UnboundValue::instance(), PirType::any());
anyChange = true;
}
// After eliding an env we must ensure to add a
// materialization before every usage in deopt branches
Expand Down Expand Up @@ -331,17 +336,20 @@ bool ElideEnvSpec::apply(Compiler&, ClosureVersion* cls, Code* code,
!i->effects.contains(Effect::DependsOnAssume) &&
MkEnv::Cast(i->env()) && MkEnv::Cast(i->env())->stub) {
i->effects.set(Effect::DependsOnAssume);
anyChange = true;
}
if (auto is = IsEnvStub::Cast(i)) {
if (!materializableStubs.count(i->env())) {
is->replaceUsesWith(True::instance());
is->effects.reset();
anyChange = true;
}
}
if (auto mk = MkArg::Cast(i)) {
if (materializableStubs.count(mk->env())) {
if (auto e = mk->prom()->env()) {
e->stub = true;
anyChange = true;
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions rir/src/compiler/opt/match_call_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ bool MatchCallArgs::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
false,
[&](ClosureVersion* fun) { target = fun; },
[]() {}, {});
} else if (auto cnst = LdConst::Cast(calli->tryGetClsArg())) {
} else if (auto cnst =
LdConst::Cast(calli->tryGetClsArg())) {
if (auto dt = DispatchTable::check(BODY(cnst->c()))) {
if (dt->baseline()->body()->codeSize <
Parameter::RECOMPILE_THRESHOLD)
Expand All @@ -222,14 +223,14 @@ bool MatchCallArgs::apply(Compiler& cmp, ClosureVersion* cls, Code* code,
auto srcRef = mk->srcRef;
if (dt->baseline()->body()->codeSize <
Parameter::RECOMPILE_THRESHOLD)
cmp.compileFunction(dt, "unknown--fromMkFunCls",
formals, srcRef, asmpt,
[&](ClosureVersion* fun) {
mk->setCls(
fun->owner());
target = fun;
},
[]() {}, {});
cmp.compileFunction(
dt, "unknown--fromMkFunCls", formals,
srcRef, asmpt,
[&](ClosureVersion* fun) {
mk->setCls(fun->owner());
target = fun;
},
[]() {}, {});
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions rir/src/compiler/opt/type_speculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ bool TypeSpeculation::apply(Compiler&, ClosureVersion* cls, Code* code,
auto cp = sp.second.first;
TypeTest::Info& info = sp.second.second;

BBTransform::insertAssume(info.test, cp, bb, ip, info.expectation,
info.srcCode, info.origin);
BBTransform::insertAssume(info.test, info.expectation, cp,
info.feedbackOrigin,
DeoptReason::Typecheck, bb, ip);

auto cast = new CastType(i, CastType::Downcast, PirType::any(),
info.result);
Expand Down
Loading

0 comments on commit 1cc4750

Please sign in to comment.