From 0ec894ddab97740f0ba351ea5ccc61595ca66bd8 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 9 Nov 2021 02:26:17 +0100 Subject: [PATCH] Export riscv binary --- include/pareas/compiler/module.hpp | 20 ++++++++++++ src/compiler/main.cpp | 22 ++++++++++++- src/compiler/module.cpp | 52 ++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/include/pareas/compiler/module.hpp b/include/pareas/compiler/module.hpp index 80371b8..0213d0a 100644 --- a/include/pareas/compiler/module.hpp +++ b/include/pareas/compiler/module.hpp @@ -3,8 +3,23 @@ #include "futhark_generated.h" +#include +#include #include +struct HostModule { + size_t num_functions; + size_t num_instructions; + + std::unique_ptr func_id; + std::unique_ptr func_start; + std::unique_ptr func_size; + + std::unique_ptr instructions; + + void dump(std::ostream& os) const; +}; + struct DeviceModule { futhark_context* ctx; @@ -22,6 +37,11 @@ struct DeviceModule { DeviceModule& operator=(DeviceModule&& other); ~DeviceModule(); + + size_t num_functions() const; + size_t num_instructions() const; + + HostModule download() const; }; #endif diff --git a/src/compiler/main.cpp b/src/compiler/main.cpp index 4391c09..4131fec 100644 --- a/src/compiler/main.cpp +++ b/src/compiler/main.cpp @@ -30,6 +30,7 @@ struct Options { bool dump_dot; unsigned profile; bool verbose_tree; + bool verbose_mod; bool futhark_verbose; bool futhark_debug; bool futhark_debug_extra; @@ -52,6 +53,8 @@ void print_usage(char* progname) { "-p --profile Record (non-futhark) profiling information.\n" "--verbose-tree Dump some information about the tree to stderr.\n" " (default: 0, =disabled)\n" + "--verbose-mod Dump some information about the final module to\n" + " stderr.\n" "--futhark-verbose Enable Futhark logging.\n" "--futhark-debug Enable Futhark debug logging.\n" "--futhark-debug-extra Futhark debug logging with extra information.\n" @@ -83,6 +86,7 @@ bool parse_options(Options* opts, int argc, char* argv[]) { .dump_dot = false, .profile = 0, .verbose_tree = false, + .verbose_mod = false, .futhark_verbose = false, .futhark_debug = false, .futhark_debug_extra = false, @@ -141,6 +145,8 @@ bool parse_options(Options* opts, int argc, char* argv[]) { profile_arg = argv[i]; } else if (arg == "--verbose-tree") { opts->verbose_tree = true; + } else if (arg == "--verbose-mod") { + opts->verbose_mod = true; } else if (arg == "--futhark-verbose") { opts->futhark_verbose = true; } else if (arg == "--futhark-debug") { @@ -255,7 +261,7 @@ int main(int argc, char* argv[]) { p.end("frontend"); p.begin(); - backend::compile(ctx.get(), ast, p); + auto module = backend::compile(ctx.get(), ast, p); p.end("backend"); if (opts.profile > 0) @@ -268,6 +274,20 @@ int main(int argc, char* argv[]) { host_ast.dump_dot(std::cout); } + auto host_mod = module.download(); + + if (opts.verbose_mod) { + host_mod.dump(std::cerr); + } + + auto out = std::ofstream(opts.output_path, std::ios::binary); + if (!out) { + fmt::print(std::cerr, "Failed to open output file '{}'\n", opts.output_path); + return EXIT_FAILURE; + } + + out.write(reinterpret_cast(host_mod.instructions.get()), host_mod.num_instructions * sizeof(uint32_t)); + if (opts.futhark_profile) { auto report = MallocPtr(futhark_context_report(ctx.get())); fmt::print(std::cerr, "Futhark profile report:\n{}", report); diff --git a/src/compiler/module.cpp b/src/compiler/module.cpp index beba5ce..e137382 100644 --- a/src/compiler/module.cpp +++ b/src/compiler/module.cpp @@ -1,7 +1,21 @@ #include "pareas/compiler/module.hpp" +#include "pareas/compiler/futhark_interop.hpp" + +#include +#include #include +void HostModule::dump(std::ostream& os) const { + fmt::print("Total instructions: {}\n", this->num_instructions); + for (size_t i = 0; i < this->num_functions; ++i) { + fmt::print(os, "function {}:\n", i); + fmt::print(os, " id: {}\n", this->func_id[i]); + fmt::print(os, " start: {}\n", this->func_start[i]); + fmt::print(os, " size: {}\n", this->func_size[i]); + } +} + DeviceModule::DeviceModule(futhark_context* ctx): ctx(ctx), func_id(nullptr), @@ -43,3 +57,41 @@ DeviceModule::~DeviceModule() { if (this->instructions) futhark_free_u32_1d(this->ctx, this->instructions); } + +size_t DeviceModule::num_functions() const { + if (!this->func_id) + return 0; + return futhark_shape_u32_1d(this->ctx, this->func_id)[0]; +} + + +size_t DeviceModule::num_instructions() const { + if (!this->instructions) + return 0; + return futhark_shape_u32_1d(this->ctx, this->instructions)[0]; +} + + +HostModule DeviceModule::download() const { + size_t num_functions = this->num_functions(); + size_t num_instructions = this->num_instructions(); + + auto mod = HostModule{ + .num_functions = num_functions, + .num_instructions = num_instructions, + .func_id = std::make_unique(num_functions), + .func_start = std::make_unique(num_functions), + .func_size = std::make_unique(num_functions), + .instructions = std::make_unique(num_instructions) + }; + + int err = futhark_values_u32_1d(this->ctx, this->func_id, mod.func_id.get()); + err |= futhark_values_u32_1d(this->ctx, this->func_start, mod.func_start.get()); + err |= futhark_values_u32_1d(this->ctx, this->func_size, mod.func_size.get()); + err |= futhark_values_u32_1d(this->ctx, this->instructions, mod.instructions.get()); + + if (err) + throw futhark::Error(this->ctx); + + return mod; +}