Skip to content

Commit

Permalink
Export riscv binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Snektron committed Nov 9, 2021
1 parent 4683a26 commit 0ec894d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
20 changes: 20 additions & 0 deletions include/pareas/compiler/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@

#include "futhark_generated.h"

#include <memory>
#include <iosfwd>
#include <cstdint>

struct HostModule {
size_t num_functions;
size_t num_instructions;

std::unique_ptr<uint32_t[]> func_id;
std::unique_ptr<uint32_t[]> func_start;
std::unique_ptr<uint32_t[]> func_size;

std::unique_ptr<uint32_t[]> instructions;

void dump(std::ostream& os) const;
};

struct DeviceModule {
futhark_context* ctx;

Expand All @@ -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
22 changes: 21 additions & 1 deletion src/compiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +53,8 @@ void print_usage(char* progname) {
"-p --profile <level> 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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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<const char*>(host_mod.instructions.get()), host_mod.num_instructions * sizeof(uint32_t));

if (opts.futhark_profile) {
auto report = MallocPtr<char>(futhark_context_report(ctx.get()));
fmt::print(std::cerr, "Futhark profile report:\n{}", report);
Expand Down
52 changes: 52 additions & 0 deletions src/compiler/module.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include "pareas/compiler/module.hpp"
#include "pareas/compiler/futhark_interop.hpp"

#include <fmt/format.h>
#include <fmt/ostream.h>

#include <utility>

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),
Expand Down Expand Up @@ -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<uint32_t[]>(num_functions),
.func_start = std::make_unique<uint32_t[]>(num_functions),
.func_size = std::make_unique<uint32_t[]>(num_functions),
.instructions = std::make_unique<uint32_t[]>(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;
}

0 comments on commit 0ec894d

Please sign in to comment.