Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] towards a gnur BC to PIR compiler #1002

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
33 changes: 25 additions & 8 deletions rir/src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "R/Serialize.h"
#include "compiler/backend.h"
#include "compiler/compiler.h"
#include "compiler/gnur2pir/gnur2pir.h"
#include "compiler/log/debug.h"
#include "compiler/parameter.h"
#include "compiler/test/PirCheck.h"
Expand Down Expand Up @@ -286,15 +287,7 @@ REXPORT SEXP pirSetDebugFlags(SEXP debugFlags) {

SEXP pirCompile(SEXP what, const Context& assumptions, const std::string& name,
const pir::DebugOptions& debug) {
if (!isValidClosureSEXP(what)) {
Rf_error("not a compiled closure");
}
if (!DispatchTable::check(BODY(what))) {
Rf_error("Cannot optimize compiled expression, only closure");
}

PROTECT(what);

bool dryRun = debug.includes(pir::DebugFlag::DryRun);
// compile to pir
pir::Module* m = new pir::Module;
Expand Down Expand Up @@ -365,6 +358,30 @@ REXPORT SEXP pirCompileWrapper(SEXP what, SEXP name, SEXP debugFlags,
return pirCompile(what, rir::pir::Compiler::defaultContext, n, opts);
}

REXPORT SEXP gnur2pir(SEXP what) {
// compile to pir
pir::StreamLogger logger(PirDebug);
pir::Module m;
pir::Gnur2Pir g2p(m);
pir::ClosureVersion* c = g2p.compile(what, "");
if (!c)
return R_NilValue;

c->printCode(std::cout, true, false);
pir::Compiler cmp(&m, logger);
pir::Backend backend(logger, "");
auto f = backend.getOrCompile(c);

// TODO the rest of the system really wants the dispatch table to only
// contain rir::Functions... So we need to compile the baseline anyway to be
// able to call the opt version...
what = rirCompile(what, nullptr);
auto dt = DispatchTable::unpack(BODY(what));
dt->insert(f);

return what;
}

REXPORT SEXP pirTests() {
PirTests::run();
return R_NilValue;
Expand Down
5 changes: 3 additions & 2 deletions rir/src/compiler/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ rir::Function* Backend::doCompile(ClosureVersion* cls,
approximateRefcount(cls, c, refcount, log);
std::unordered_set<Instruction*> needsLdVarForUpdate;
approximateNeedsLdVarForUpdate(c, needsLdVarForUpdate);
auto res = done[c] = rir::Code::New(c->rirSrc()->src);
auto res = done[c] = rir::Code::New(c->expression());
// Can we do better?
preserve(res->container());
jit.compile(res, c, promMap.at(c), refcount, needsLdVarForUpdate, log);
Expand All @@ -375,7 +375,8 @@ rir::Function* Backend::doCompile(ClosureVersion* cls,
log.finalPIR(cls);
function.finalize(body, signature, cls->context());

function.function()->inheritFlags(cls->owner()->rirFunction());
cls->owner()->rirFunction(
[&](rir::Function* f) { function.function()->inheritFlags(f); });
return function.function();
}

Expand Down
24 changes: 13 additions & 11 deletions rir/src/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ void Compiler::compileClosure(SEXP closure, const std::string& name,
auto pirClosure = module->getOrDeclareRirClosure(closureName, closure, fun,
tbl->userDefinedContext());
Context context(assumptions);
compileClosure(pirClosure, tbl->dispatch(assumptions), context, root,
success, fail, outerFeedback);
compileClosure(pirClosure, tbl->baseline(), tbl->dispatch(assumptions),
context, root, success, fail, outerFeedback);
}

void Compiler::compileFunction(rir::DispatchTable* src, const std::string& name,
Expand All @@ -68,13 +68,13 @@ void Compiler::compileFunction(rir::DispatchTable* src, const std::string& name,
Context context(assumptions);
auto closure = module->getOrDeclareRirFunction(
name, srcFunction, formals, srcRef, src->userDefinedContext());
compileClosure(closure, src->dispatch(assumptions), context, false, success,
fail, outerFeedback);
compileClosure(closure, srcFunction, src->dispatch(assumptions), context,
false, success, fail, outerFeedback);
}

void Compiler::compileClosure(Closure* closure, rir::Function* optFunction,
const Context& ctx, bool root, MaybeCls success,
Maybe fail,
void Compiler::compileClosure(Closure* closure, rir::Function* srcCode,
rir::Function* optFunction, const Context& ctx,
bool root, MaybeCls success, Maybe fail,
std::list<PirTypeFeedback*> outerFeedback) {

if (!ctx.includes(minimalContext)) {
Expand All @@ -99,8 +99,10 @@ void Compiler::compileClosure(Closure* closure, rir::Function* optFunction,
return fail();
}

if (closure->rirFunction()->body()->codeSize > Parameter::MAX_INPUT_SIZE) {
closure->rirFunction()->flags.set(Function::NotOptimizable);
auto sz = closure->bodySize();
if (sz > Parameter::MAX_INPUT_SIZE) {
closure->rirFunction(
[&](rir::Function* f) { f->flags.set(Function::NotOptimizable); });
logger.warn("skipping huge function");
return fail();
}
Expand All @@ -125,7 +127,7 @@ void Compiler::compileClosure(Closure* closure, rir::Function* optFunction,
auto arg = closure->formals().defaultArgs()[idx];
assert(rir::Code::check(arg) && "Default arg not compiled");
auto code = rir::Code::unpack(arg);
auto res = rir2pir.tryCreateArg(code, builder, false);
auto res = rir2pir.tryCreateArg(code, builder, false, -1);
if (!res) {
failedToCompileDefaultArgs = true;
return;
Expand Down Expand Up @@ -186,7 +188,7 @@ void Compiler::compileClosure(Closure* closure, rir::Function* optFunction,
return fail();
}

if (rir2pir.tryCompile(builder)) {
if (rir2pir.tryCompile(srcCode->body(), builder)) {
log.compilationEarlyPir(version);
#ifdef FULLVERIFIER
Verify::apply(version, "Error after initial translation", true);
Expand Down
7 changes: 4 additions & 3 deletions rir/src/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ class Compiler {
Module* module;
StreamLogger& logger;

void compileClosure(Closure* closure, rir::Function* optFunction,
const Context& ctx, bool root, MaybeCls success,
Maybe fail, std::list<PirTypeFeedback*> outerFeedback);
void compileClosure(Closure* closure, rir::Function* srcCode,
rir::Function* optFunction, const Context& ctx,
bool root, MaybeCls success, Maybe fail,
std::list<PirTypeFeedback*> outerFeedback);

Preserve preserve_;
};
Expand Down
Loading