Skip to content

Commit

Permalink
Merge branch 'frontend'
Browse files Browse the repository at this point in the history
  • Loading branch information
Snektron committed Jul 9, 2021
2 parents b199766 + f6439c2 commit bf0ed45
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 499 deletions.
55 changes: 6 additions & 49 deletions include/pareas/compiler/frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
#include "futhark_generated.h"

#include "pareas/compiler/ast.hpp"
#include "pareas/compiler/profiler.hpp"

#include <chrono>
#include <stdexcept>
#include <iosfwd>

namespace frontend {
// Keep in sync with src/compiler/frontend.fut
enum class Status : uint8_t {
OK = 0,
enum class Error : uint8_t {
PARSE_ERROR = 1,
STRAY_ELSE_ERROR = 2,
INVALID_DECL = 3,
Expand All @@ -27,56 +26,14 @@ namespace frontend {
MISSING_RETURN = 12,
};

const char* status_name(Status s);
const char* error_name(Error e);

struct CompileError: std::runtime_error {
CompileError(Status status):
std::runtime_error(status_name(status)) {}
CompileError(Error e):
std::runtime_error(error_name(e)) {}
};

struct CombinedStatistics {
std::chrono::microseconds table_upload;
std::chrono::microseconds input_upload;
std::chrono::microseconds compile;
std::chrono::microseconds total;

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

DeviceAst compile_combined(futhark_context* ctx, const std::string& input, CombinedStatistics& stats);

struct SeparateStatistics {
std::chrono::microseconds table_upload;
std::chrono::microseconds input_upload;
std::chrono::microseconds tokenize;
std::chrono::microseconds parse;
std::chrono::microseconds build_parse_tree;
std::chrono::microseconds fix_bin_ops;
std::chrono::microseconds fix_if_else;
std::chrono::microseconds flatten_lists;
std::chrono::microseconds fix_names;
std::chrono::microseconds fix_ascriptions;
std::chrono::microseconds fix_fn_decls;
std::chrono::microseconds fix_args_and_params;
std::chrono::microseconds fix_decls;
std::chrono::microseconds remove_marker_nodes;
std::chrono::microseconds compute_prev_siblings;
std::chrono::microseconds check_assignments;
std::chrono::microseconds insert_derefs;
std::chrono::microseconds extract_lexemes;
std::chrono::microseconds resolve_vars;
std::chrono::microseconds resolve_fns;
std::chrono::microseconds resolve_args;
std::chrono::microseconds resolve_data_types;
std::chrono::microseconds check_return_types;
std::chrono::microseconds check_convergence;
std::chrono::microseconds build_ast;
std::chrono::microseconds total;

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

DeviceAst compile_separate(futhark_context* ctx, const std::string& input, SeparateStatistics& stats);
DeviceAst compile(futhark_context* ctx, const std::string& input, Profiler& p);
}

#endif
19 changes: 14 additions & 5 deletions include/pareas/compiler/futhark_interop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ namespace futhark {
}

UniqueOpaqueArray(UniqueOpaqueArray&& other):
ctx(std::exchange(other.ctx, nullptr)), data(std::exchange(other.data, nullptr)) {
ctx(other.ctx), data(std::exchange(other.data, nullptr)) {
}

UniqueOpaqueArray& operator=(UniqueOpaqueArray&& other) {
std::swap(this->data, other.data);
std::swap(this->ctx, other.ctx);
return *this;
}

UniqueOpaqueArray(const UniqueOpaqueArray&) = delete;
Expand All @@ -85,8 +86,12 @@ namespace futhark {
return &this->data;
}

Array* exchange(Array* other) {
return std::exchange(this->data, other);
operator Array*() {
return this->data;
}

operator const Array*() const {
return this->data;
}
};

Expand Down Expand Up @@ -131,8 +136,12 @@ namespace futhark {
return &this->handle;
}

Array* exchange(Array* other) {
return this->handle.exchange(other);
operator Array*() {
return this->handle;
}

operator const Array*() const {
return this->handle;
}

void values(T* out) const {
Expand Down
47 changes: 47 additions & 0 deletions include/pareas/compiler/profiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef _PAREAS_COMPILER_PROFILER_HPP
#define _PAREAS_COMPILER_PROFILER_HPP

#include "futhark_generated.h"

#include <iosfwd>
#include <chrono>
#include <vector>

struct Profiler {
using Clock = std::chrono::high_resolution_clock;

struct HistoryEntry {
unsigned level;
const char* name;
Clock::duration elapsed;
};

unsigned max_level;
unsigned level;

std::vector<Clock::time_point> starts;
std::vector<HistoryEntry> history;

Profiler(unsigned max_level);

void begin(futhark_context* ctx = nullptr);
void end(const char* name, futhark_context* ctx = nullptr);

void dump(std::ostream& os);

template <typename F>
void measure(const char* name, F f) {
this->begin();
f();
this->end(name);
}

template <typename F>
void measure(const char* name, futhark_context* ctx, F f) {
this->begin(ctx);
f();
this->end(name, ctx);
}
};

#endif
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ sources = [
'src/compiler/main.cpp',
'src/compiler/frontend.cpp',
'src/compiler/ast.cpp',
'src/compiler/profiler.cpp',
]

executable(
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,20 @@ DeviceAst::~DeviceAst() {
}

size_t DeviceAst::num_nodes() const {
if (!this->node_types)
return 0;
return futhark_shape_u8_1d(this->ctx, this->node_types)[0];
}

size_t DeviceAst::num_functions() const {
if (!this->fn_tab)
return 0;
return futhark_shape_i32_1d(this->ctx, this->fn_tab)[0];
}

HostAst DeviceAst::download() const {
size_t num_nodes = futhark_shape_u8_1d(this->ctx, this->node_types)[0];
size_t num_functions = futhark_shape_i32_1d(this->ctx, this->fn_tab)[0];
size_t num_nodes = this->num_nodes();
size_t num_functions = this->num_functions();

auto ast = HostAst{
.num_nodes = num_nodes,
Expand Down
Loading

0 comments on commit bf0ed45

Please sign in to comment.