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

Refactor/driver #186

Merged
merged 23 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 22 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
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ endif()
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/thorin-config-version.cmake"
VERSION ${Thorin_VERSION}
COMPATIBILITY SameMajorVersion
"${PROJECT_BINARY_DIR}/thorin-config-version.cmake"
VERSION ${Thorin_VERSION}
COMPATIBILITY SameMajorVersion
)

set(THORIN_CMAKE_INSTALL_DIR lib/cmake/thorin)
Expand Down
60 changes: 29 additions & 31 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@ int main(int argc, char** argv) {
static const auto version = "thorin command-line utility version " THORIN_VER "\n";

Driver driver;
bool show_help = false;
bool show_version = false;
bool show_help = false;
bool show_version = false;
bool list_dialect_paths = false;
std::string input, prefix;
std::string clang = sys::find_cmd("clang");
std::vector<std::string> dialect_plugins, dialect_paths;
std::vector<std::string> plugins, dialect_paths;
std::vector<size_t> breakpoints;
std::array<std::string, Num_Backends> output;
int verbose = 0;
int opt = 2;
auto inc_verbose = [&](bool) { ++verbose; };
auto& flags = driver.flags;
auto& flags = driver.flags();

// clang-format off
auto cli = lyra::cli()
| lyra::help(show_help)
| lyra::opt(show_version )["-v"]["--version" ]("Display version info and exit.")
| lyra::opt(list_dialect_paths )["-l"]["--list-dialect-paths"]("List search paths in order and exit.")
| lyra::opt(clang, "clang" )["-c"]["--clang" ]("Path to clang executable (default: '" THORIN_WHICH " clang').")
| lyra::opt(dialect_plugins,"dialect")["-d"]["--dialect" ]("Dynamically load dialect [WIP].")
| lyra::opt(plugins, "dialect")["-d"]["--dialect" ]("Dynamically load dialect [WIP].")
| lyra::opt(dialect_paths, "path" )["-D"]["--dialect-path" ]("Path to search dialects in.")
| lyra::opt(inc_verbose )["-V"]["--verbose" ]("Verbose mode. Multiple -V options increase the verbosity. The maximum is 4.").cardinality(0, 4)
| lyra::opt(opt, "level" )["-O"]["--optimize" ]("Optimization level (default: 2).")
Expand Down Expand Up @@ -78,12 +80,19 @@ int main(int argc, char** argv) {
std::exit(EXIT_SUCCESS);
}

for (auto&& path : dialect_paths) driver.add_search_path(path);

if (list_dialect_paths) {
for (auto&& path : driver.search_paths()) std::cout << path << std::endl;
std::exit(EXIT_SUCCESS);
}

World& world = driver.world();
#if THORIN_ENABLE_CHECKS
for (auto b : breakpoints) driver.breakpoints.emplace(b);
for (auto b : breakpoints) world.breakpoint(b);
#endif
World& world = driver.world;
world.log().ostream = &std::cerr;
world.log().level = (Log::Level)verbose;
world.log().set(&std::cerr).set((Log::Level)verbose);

// prepare output files and streams
std::array<std::ofstream, Num_Backends> ofs;
std::array<std::ostream*, Num_Backends> os;
Expand All @@ -99,20 +108,10 @@ int main(int argc, char** argv) {
}

// we always need core and mem, as long as we are not in bootstrap mode..
if (!os[H]) dialect_plugins.insert(dialect_plugins.end(), {"core", "mem", "compile", "opt"});

std::vector<Dialect> dialects;
thorin::Backends backends;
thorin::Normalizers normalizers;
thorin::Passes passes;
if (!dialect_plugins.empty()) {
for (const auto& dialect : dialect_plugins) {
dialects.push_back(Dialect::load(dialect, dialect_paths));
dialects.back().register_backends(backends);
dialects.back().register_normalizers(normalizers);
dialects.back().register_passes(passes);
}
}
if (!os[H]) plugins.insert(plugins.end(), {"core", "mem", "compile", "opt"});

if (!plugins.empty())
for (const auto& plugin : plugins) driver.load(plugin);

if (input.empty()) throw std::invalid_argument("error: no input given");
if (input[0] == '-' || input.substr(0, 2) == "--")
Expand All @@ -124,12 +123,11 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

for (const auto& dialect : dialects)
fe::Parser::import_module(world, world.sym(dialect.name()), dialect_paths, &normalizers);
for (const auto& plugin : plugins) fe::Parser::import_module(world, world.sym(plugin));

auto sym = world.sym(std::move(input));
world.set(sym);
fe::Parser parser(world, sym, ifs, dialect_paths, &normalizers, os[Md]);
fe::Parser parser(world, sym, ifs, os[Md]);
parser.parse_module();

if (os[H]) {
Expand All @@ -141,7 +139,7 @@ int main(int argc, char** argv) {
switch (opt) {
case 0: break;
case 1: Phase::run<Cleanup>(world); break;
case 2: optimize(world, passes, dialects); break;
case 2: optimize(world); break;
default: errln("error: illegal optimization level '{}'", opt);
}
// clang-format on
Expand All @@ -150,10 +148,10 @@ int main(int argc, char** argv) {
if (os[Dot]) dot::emit(world, *os[Dot]);

if (os[LL]) {
if (auto it = backends.find("ll"); it != backends.end()) {
it->second(world, *os[LL]);
} else
errln("error: 'll' emitter not loaded. Try loading 'mem' dialect.");
if (auto backend = driver.backend("ll"))
backend(world, *os[LL]);
else
err("'ll' emitter not loaded. Try loading 'mem' dialect.");
}
} catch (const std::exception& e) {
errln("{}", e.what());
Expand Down
5 changes: 3 additions & 2 deletions dialects/affine/affine.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "dialects/affine/affine.h"

#include <thorin/analyses/scope.h>
#include <thorin/config.h>
#include <thorin/pass/pass.h>

#include "thorin/dialects.h"
#include <thorin/pass/pipelinebuilder.h>
#include <thorin/rewrite.h>

#include "dialects/affine/passes/lower_for.h"

Expand Down
3 changes: 1 addition & 2 deletions dialects/autodiff/autodiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <thorin/config.h>
#include <thorin/pass/pass.h>

#include "thorin/dialects.h"
#include <thorin/pass/pipelinebuilder.h>

#include "dialects/autodiff/passes/autodiff_eval.h"
#include "dialects/autodiff/passes/autodiff_ext_cleanup.h"
Expand Down
8 changes: 4 additions & 4 deletions dialects/clos/clos.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "dialects/clos/clos.h"

#include <thorin/config.h>
#include <thorin/pass/fp/eta_exp.h>
#include <thorin/pass/fp/eta_red.h>
#include <thorin/pass/pass.h>
#include <thorin/pass/pipelinebuilder.h>
#include <thorin/pass/rw/scalarize.h>

#include "thorin/dialects.h"

#include "thorin/pass/fp/eta_exp.h"
#include "thorin/pass/fp/eta_red.h"
#include "thorin/pass/rw/scalarize.h"

#include "dialects/clos/pass/fp/lower_typed_clos_prep.h"
#include "dialects/clos/pass/rw/branch_clos_elim.h"
#include "dialects/clos/pass/rw/clos2sjlj.h"
Expand Down
3 changes: 2 additions & 1 deletion dialects/clos/pass/rw/clos_conv_prep.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "thorin/pass/pass.h"
#include <thorin/analyses/scope.h>
#include <thorin/pass/pass.h>

#include "dialects/clos/clos.h"

Expand Down
14 changes: 6 additions & 8 deletions dialects/compile/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using namespace thorin;

void add_phases(DefVec& phases, World& world, Passes& passes, PipelineBuilder& builder) {
for (auto phase : phases) { compile::handle_optimization_part(phase, world, passes, builder); }
for (auto phase : phases) compile::handle_optimization_part(phase, world, passes, builder);
}

void add_passes(World& world, PipelineBuilder& builder, Passes& passes, DefVec& pass_list) {
Expand All @@ -31,7 +31,7 @@ void add_passes(World& world, PipelineBuilder& builder, Passes& passes, DefVec&
// We create a new dummy phase in which the passes should be inserted.
// builder.append_phase_end([](Pipeline&) {});
builder.begin_pass_phase();
for (auto pass : pass_list) { compile::handle_optimization_part(pass, world, passes, builder); }
for (auto pass : pass_list) compile::handle_optimization_part(pass, world, passes, builder);
builder.end_pass_phase();
}

Expand All @@ -50,15 +50,15 @@ extern "C" THORIN_EXPORT thorin::DialectInfo thorin_get_dialect_info() {
[&](World& world, PipelineBuilder& builder, const Def* app) {
auto pass_array = app->as<App>()->arg()->projs();
DefVec pass_list;
for (auto pass : pass_array) { pass_list.push_back(pass); }
for (auto pass : pass_array) pass_list.push_back(pass);
add_passes(world, builder, passes, pass_list);
};

passes[flags_t(Axiom::Base<thorin::compile::phases_to_phase>)] =
[&](World& world, PipelineBuilder& builder, const Def* app) {
auto phase_array = app->as<App>()->arg()->projs();
DefVec phase_list;
for (auto phase : phase_array) { phase_list.push_back(phase); }
for (auto phase : phase_array) phase_list.push_back(phase);
add_phases(phase_list, world, passes, builder);
};

Expand All @@ -67,10 +67,8 @@ extern "C" THORIN_EXPORT thorin::DialectInfo thorin_get_dialect_info() {
auto [ax, phases] = collect_args(app);
add_phases(phases, world, passes, builder);
};
passes[flags_t(Axiom::Base<thorin::compile::nullptr_pass>)] = [&](World&, PipelineBuilder& builder,
const Def* def) {
builder.remember_pass_instance(nullptr, def);
};
passes[flags_t(Axiom::Base<thorin::compile::nullptr_pass>)] =
[&](World&, PipelineBuilder& builder, const Def* def) { builder.def2pass(def, nullptr); };

register_pass<compile::partial_eval_pass, PartialEval>(passes);
register_pass<compile::beta_red_pass, BetaRed>(passes);
Expand Down
11 changes: 1 addition & 10 deletions dialects/direct/direct.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
#include "dialects/direct/direct.h"

#include <thorin/config.h>
#include <thorin/dialects.h>
#include <thorin/pass/pass.h>

#include "thorin/pass/fp/beta_red.h"
#include "thorin/pass/fp/eta_exp.h"
#include "thorin/pass/fp/eta_red.h"
#include "thorin/pass/fp/tail_rec_elim.h"
#include "thorin/pass/rw/partial_eval.h"
#include "thorin/pass/rw/ret_wrap.h"
#include "thorin/pass/rw/scalarize.h"
#include <thorin/pass/pipelinebuilder.h>

#include "dialects/direct/passes/cps2ds.h"
#include "dialects/direct/passes/ds2cps.h"
Expand Down
3 changes: 2 additions & 1 deletion dialects/direct/direct.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "thorin/world.h"
#include <thorin/analyses/scope.h>
#include <thorin/rewrite.h>

#include "dialects/direct/autogen.h"

Expand Down
23 changes: 11 additions & 12 deletions dialects/mem/mem.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "dialects/mem/mem.h"

#include <thorin/config.h>
#include <thorin/dialects.h>
#include <thorin/pass/fp/beta_red.h>
#include <thorin/pass/fp/eta_exp.h>
#include <thorin/pass/fp/eta_red.h>
#include <thorin/pass/fp/tail_rec_elim.h>
#include <thorin/pass/pass.h>

#include "thorin/dialects.h"

#include "thorin/pass/fp/beta_red.h"
#include "thorin/pass/fp/eta_exp.h"
#include "thorin/pass/fp/eta_red.h"
#include "thorin/pass/fp/tail_rec_elim.h"
#include "thorin/pass/rw/partial_eval.h"
#include "thorin/pass/rw/ret_wrap.h"
#include "thorin/pass/rw/scalarize.h"
#include <thorin/pass/pipelinebuilder.h>
#include <thorin/pass/rw/partial_eval.h>
#include <thorin/pass/rw/ret_wrap.h>
#include <thorin/pass/rw/scalarize.h>

#include "dialects/mem/autogen.h"
#include "dialects/mem/passes/fp/copy_prop.h"
Expand All @@ -35,8 +34,8 @@ extern "C" THORIN_EXPORT DialectInfo thorin_get_dialect_info() {
const Def* app) {
auto [br, ee, bb] = app->as<App>()->args<3>();
// TODO: let get_pass do the casts
auto br_pass = (BetaRed*)builder.get_pass_instance(br);
auto ee_pass = (EtaExp*)builder.get_pass_instance(ee);
auto br_pass = (BetaRed*)builder.def2pass(br);
auto ee_pass = (EtaExp*)builder.def2pass(ee);
auto bb_only = bb->as<Lit>()->get<u64>();
world.DLOG("registering copy_prop with br = {}, ee = {}, bb_only = {}", br, ee, bb_only);
builder.add_pass<mem::CopyProp>(app, br_pass, ee_pass, bb_only);
Expand Down
13 changes: 6 additions & 7 deletions dialects/opt/opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <string_view>

#include <thorin/config.h>
#include <thorin/dialects.h>
#include <thorin/driver.h>
#include <thorin/pass/pass.h>

#include "dialects/compile/compile.h"
Expand All @@ -15,7 +14,8 @@ extern "C" THORIN_EXPORT thorin::DialectInfo thorin_get_dialect_info() {
return {"opt",
[](Passes& passes) {
passes[flags_t(Axiom::Base<compile::dialect_select>)] = [&](World& world, PipelineBuilder& builder,
const Def* app) {
const Def* app) {
auto& driver = builder.world().driver();
auto [ax, args] = collect_args(app);
auto dialect_axiom = args[1]->as<Axiom>();
auto then_phase = args[2];
Expand All @@ -27,10 +27,9 @@ extern "C" THORIN_EXPORT thorin::DialectInfo thorin_get_dialect_info() {
auto name = dialect_axiom->sym();
auto [_, tag, __] = Axiom::split(world, name);
assert(tag->find('_') != std::string_view::npos && "dialect_phase: invalid dialect name");
auto dialect = tag->substr(0, tag->find('_'));
auto dialect_str = std::string(dialect);
world.DLOG("dialect: {}", dialect_str);
auto is_loaded = builder.is_registered_dialect(dialect_str);
auto dialect = driver.sym(tag->substr(0, tag->find('_')));
world.DLOG("dialect: {}", dialect);
bool is_loaded = driver.sym2plugin(dialect);
world.DLOG("contained: {}", is_loaded);

compile::handle_optimization_part(is_loaded ? then_phase : else_phase, world, passes, builder);
Expand Down
1 change: 1 addition & 0 deletions dialects/refly/refly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thorin/config.h>
#include <thorin/dialects.h>
#include <thorin/pass/pass.h>
#include <thorin/pass/pipelinebuilder.h>

#include "dialects/refly/passes/remove_perm.h"

Expand Down
Loading