Skip to content

Commit

Permalink
towards compiling gnur bc to pir
Browse files Browse the repository at this point in the history
  • Loading branch information
o- committed Apr 8, 2021
1 parent e991ba3 commit bd2bb2b
Show file tree
Hide file tree
Showing 35 changed files with 738 additions and 209 deletions.
37 changes: 25 additions & 12 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/native/pir_debug_info.h"
#include "compiler/parameter.h"
Expand Down Expand Up @@ -287,26 +288,14 @@ 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;
pir::StreamLogger logger(debug);
logger.title("Compiling " + name);
pir::Compiler cmp(m, logger);
#ifdef PIR_GDB_SUPPORT
pir::Backend backend(logger, name);
#else
pir::Backend backend(logger);
#endif
cmp.compileClosure(what, name, assumptions, true,
[&](pir::ClosureVersion* c) {
logger.flush();
Expand Down Expand Up @@ -370,6 +359,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
4 changes: 0 additions & 4 deletions rir/src/compiler/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ namespace pir {

class Backend {
public:
#ifdef PIR_GDB_SUPPORT
Backend(StreamLogger& logger, const std::string& name)
: jit(name), logger(logger) {}
#else
explicit Backend(StreamLogger& logger) : logger(logger) {}
#endif
Backend(const Backend&) = delete;
Backend& operator=(const Backend&) = delete;

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

0 comments on commit bd2bb2b

Please sign in to comment.