Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"cfenv": "cpp",
"csignal": "cpp",
"__functional_base_03": "cpp",
"__memory": "cpp"
"__memory": "cpp",
"version": "cpp"
}
}
1 change: 1 addition & 0 deletions rir/src/compiler/pir/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ struct CallFeedback {
SEXP monomorphic = nullptr;
SEXPTYPE type = NILSXP;
bool stableEnv = false;
bool stableEnvHint = false;
};

class DominanceGraph;
Expand Down
42 changes: 39 additions & 3 deletions rir/src/compiler/rir2pir/rir2pir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,33 @@ bool Rir2Pir::compileBC(const BC& bc, Opcode* pos, Opcode* nextPos,
f.taken = feedback.taken;
f.feedbackOrigin = FeedbackOrigin(srcCode, pos);
if (feedback.numTargets == 1) {
f.monomorphic = feedback.getTarget(srcCode, 0);

// if (auto ld = LdFun::Cast(i)) {
// // if ( ld->varName == Rf_install("f")) {
// // int a=3;
// // //assert(false && "f!");

// // }

// }

auto target = feedback.getTarget(srcCode, 0);

if (TYPEOF(target) == CLOSXP) {
if (!isValidClosureSEXP(target)) {

rir::Compiler::compileClosure(target);
assert(isValidClosureSEXP(target));
f.stableEnv = false;
}
} else {
f.stableEnv = true;
}
f.stableEnvHint = true;
// f.stableEnv =true;
f.monomorphic = target;
f.type = TYPEOF(f.monomorphic);
f.stableEnv = true;

} else if (feedback.numTargets > 1) {
SEXP first = nullptr;
bool stableType = true;
Expand Down Expand Up @@ -519,6 +543,8 @@ bool Rir2Pir::compileBC(const BC& bc, Opcode* pos, Opcode* nextPos,
f.monomorphic = first;
if (stableEnv)
f.stableEnv = true;

f.stableEnv = false;
}
}
break;
Expand Down Expand Up @@ -602,8 +628,18 @@ bool Rir2Pir::compileBC(const BC& bc, Opcode* pos, Opcode* nextPos,
if (ldfun) {
if (ti.monomorphic) {
ldfun->hint(ti.monomorphic, ti.feedbackOrigin);
if (ti.stableEnv)
if (ti.stableEnvHint) {
ldfun->hintHasStableEnv = true;

if (TYPEOF(ti.monomorphic) == CLOSXP) {

if (!isValidClosureSEXP(ti.monomorphic)) {

rir::Compiler::compileClosure(ti.monomorphic);
assert(isValidClosureSEXP(ti.monomorphic));
}
}
}
} else {
ldfun->hint(symbol::ambiguousCallTarget, {});
}
Expand Down
8 changes: 7 additions & 1 deletion rir/src/compiler/test/PirCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ static bool testNoPromise(ClosureVersion* f) {

static bool testNoExternalCalls(ClosureVersion* f) {
return Visitor::check(f->entry, [&](Instruction* i) {
return !CallInstruction::CastCall(i) || CallSafeBuiltin::Cast(i);
auto res = !CallInstruction::CastCall(i) || CallSafeBuiltin::Cast(i);
if (!res && f->owner()) {
i->print(std::cout, true);
std::cerr << i;
// assert(false);
}
return res;
});
}

Expand Down
3 changes: 3 additions & 0 deletions rir/src/compiler/util/bb_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ Value* BBTransform::insertCalleeGuard(Compiler& compiler,
: (stableEnv ? compiler.module->c(fb.monomorphic)
: compiler.module->c(BODY(fb.monomorphic)));

// if (expected == nullptr || expected == Nil::instance() )
// assert(false && "pir nil");

auto t = new Identical(calleeForGuard, expected, PirType::any());
pos = bb->insert(pos, t) + 1;

Expand Down
10 changes: 10 additions & 0 deletions rir/src/runtime/TypeFeedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ void ObservedCallees::record(Code* caller, SEXP callee) {
if (taken < CounterOverflow)
taken++;
if (numTargets < MaxTargets) {

if (TYPEOF(callee) != CLOSXP && TYPEOF(callee) != BUILTINSXP &&
TYPEOF(callee) != SPECIALSXP) {

// Rf_PrintValue(callee);
// assert(false && "aa");
return;
}

int i = 0;
for (; i < numTargets; ++i)
if (caller->getExtraPoolEntry(targets[i]) == callee)
break;

if (i == numTargets) {
auto idx = caller->addExtraPoolEntry(callee);
targets[numTargets++] = idx;
Expand Down