5void Schedule::place(Lam* curr,
const Def* def) {
6 if (def->isa_mut())
return;
8 auto insn = is_insn(def);
9 auto remat = is_remat(def);
10 if (!remat && !(insn ? placed_ : visited_[curr]).emplace(def).second)
return;
11 if (target_.is_mem(def)) mems_[scheduler_->early(def)->mut()->as<
Lam>()].emplace_back(def);
12 if (
auto node = insn && !remat ? scheduler_->smart(curr, def) :
nullptr) curr = node->mut()->as<
Lam>();
14 auto ops =
DefVec(def->ops().begin(), def->ops().end());
16 if (
auto app = def->isa<App>(); app && def == curr->body()) ops = {app->arg(), app->callee()};
18 if (!is_remat(op)) place(curr, op);
21 if (is_remat(op)) place(curr, op);
22 if (insn) insns_[curr].emplace_back(def);
27 scheduler_ = &scheduler;
29 placed_.clear(), threaded_.clear(), visited_.clear(), insns_.clear(), mems_.clear();
32 if (
auto lam = mut->isa<
Lam>(); lam && lam->
is_set()) place(lam, lam->body());
37 if (
auto mut = old_def->
isa_mut()) {
38 if (
auto lam = mut->isa<
Lam>(); lam && lam->
is_set() &&
nest()[lam] && threaded_.emplace(lam).second)
48 app->
blame(
"{} backend: instruction `{}` is not on the `mem.M` chain of a continuation", target_.name(), app)
53void Schedule::thread(
Lam* lam) {
56 for (
size_t j = 0; j != n && i == n; ++j)
57 if (target_.
is_mem(lam->
var(n, j))) i = j;
61 lam->
blame(
"{} backend: continuation `{}` has no `mem.M` to schedule on", target_.
name(), lam).bail();
63 lam->
blame(
"{} backend: direct-style function `{}` takes a `mem.M`, which only a continuation can schedule on",
66 if (i != n && !lam->
body()->isa<
App>())
67 lam->
blame(
"{} backend: the body of continuation `{}` is no jump: `{}`", target_.
name(), lam, lam->
body())
71 if (i != n) chain(lam, i);
77void Schedule::chain(
Lam* lam,
size_t i) {
80 auto m = lam->
var(n, i);
81 auto order = std::move(insns_[lam]);
82 auto mems = std::move(mems_[lam]);
86 auto cc = ex->index()->as<
App>();
87 if (is_remat(cc->arg())) order.emplace_back(cc->arg());
88 order.emplace_back(cc);
91 for (
auto old_def : order) {
92 auto app = old_def->as<
App>();
98 auto old_arg = app->arg();
99 auto k = old_arg->num_projs();
101 auto a = old_arg->proj(k, j);
105 if (target_.is_mem(new_app)) {
108 m = new_app->proj(2, 0);
109 map(old_def, new_app);
114 map(old_def, res->proj(2, 1));
118 for (
auto old_mem : mems)
121 if (n > 1)
map(lam->
var(),
w.tuple(
DefVec(n, [&](
size_t j) {
return j == i ? m : lam->
var(n, j); })));
const Def * callee() const
static auto isa(const Def *def)
T * isa_mut() const
If this is mutable, it will cast constness away and perform a dynamic_cast to T.
const Def * var(nat_t a, nat_t i) noexcept
nat_t num_vars() noexcept
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
const Var * has_var()
Only returns not nullptr, if Var of this mutable has ever been created.
const Def * filter() const
static const Lam * isa_cn(const Def *d)
Lam * set(Filter filter, const Def *body)
const Nest & nest() const
Builds a nesting tree for all mutables/binders.
const fe::Vector< std::string > & args()
Command-line arguments passed to this Phase's plugin via -X <plugin>:<arg>.
const Def * map(const Def *old_def, const Def *new_def)
virtual const Def * rewrite(const Def *)
virtual const Def * rewrite_imm_App(const App *)
Pop scoped(Vars old_vars={}, Vars new_vars={})
push()es a scope that lives as long as the returned Pop.
std::string_view name() const
The plugin name, used as the prefix of diagnostics.
void visit(const Nest &) final
const Def * rewrite(const Def *) final
const Def * rewrite_imm_App(const App *) final
static bool is_mem(const Def *def)
const Def * prev_mem(const App *app) const
The memory app continues, if app is an instruction on a mem.M 0 chain.
static const Def * sched_insn(const App *app)
fe::Vector< const Def * > DefVec
fe::Patricia< const Var, DefKey >::Set Vars