Skip to content

Commit

Permalink
Merge branch 'frontend'
Browse files Browse the repository at this point in the history
  • Loading branch information
Snektron committed Aug 27, 2021
2 parents 365e0ae + 25000e9 commit 71382ab
Show file tree
Hide file tree
Showing 17 changed files with 1,014 additions and 57 deletions.
4 changes: 3 additions & 1 deletion include/pareas/compiler/frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <chrono>
#include <stdexcept>
#include <iosfwd>
#include <cstdio>
#include <cstdint>

namespace frontend {
enum class Error : uint8_t {
Expand All @@ -33,7 +35,7 @@ namespace frontend {
std::runtime_error(error_name(e)) {}
};

DeviceAst compile(futhark_context* ctx, const std::string& input, pareas::Profiler& p);
DeviceAst compile(futhark_context* ctx, const std::string& input, bool verbose_tree, pareas::Profiler& p, std::FILE* debug_log);
}

#endif
24 changes: 11 additions & 13 deletions include/pareas/compiler/futhark_interop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <memory>
#include <string>
#include <stdexcept>
#include <vector>
#include <utility>
#include <cstdint>
#include <cassert>
Expand Down Expand Up @@ -74,6 +73,13 @@ namespace futhark {
}
}

void clear() {
if (this->data) {
free_fn(this->ctx, this->data);
this->data = nullptr;
}
}

Array* get() {
return this->data;
}
Expand Down Expand Up @@ -124,6 +130,10 @@ namespace futhark {
throw Error(this->handle.ctx);
}

void clear() {
this->handle.clear();
}

Array* get() {
return this->handle.get();
}
Expand All @@ -150,18 +160,6 @@ namespace futhark {
throw Error(this->handle.ctx);
}

std::vector<T> download() const {
auto* shape = this->shape();
int64_t total_size = 1;
for (size_t i = 0; i < N; ++i) {
total_size *= shape[i];
}

auto result = std::vector<T>(total_size);
this->values(result.data());
return result;
}

const int64_t* shape() const {
return ArrayTraits<T, N>::shape_fn(this->handle.ctx, this->handle.data);
}
Expand Down
213 changes: 213 additions & 0 deletions include/pareas/json/futhark_interop.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#ifndef _PAREAS_COMPILER_FUTHARK_INTEROP_HPP
#define _PAREAS_COMPILER_FUTHARK_INTEROP_HPP

#include "json_futhark_generated.h"

#include <memory>
#include <string>
#include <stdexcept>
#include <utility>
#include <cstdint>
#include <cassert>
#include <cstdio>

namespace futhark {
template <typename T, void(*deleter)(T*)>
struct Deleter {
void operator()(T* t) const {
deleter(t);
}
};

template <typename T, void(*deleter)(T*)>
using Unique = std::unique_ptr<T, Deleter<T, deleter>>;

using ContextConfig = Unique<futhark_context_config, futhark_context_config_free>;
using Context = Unique<futhark_context, futhark_context_free>;

inline std::string get_error_str(futhark_context* ctx) {
auto err = futhark_context_get_error(ctx);
if (err) {
auto err_str = std::string(err);
free(err); // leak if the string constructor throws, but whatever.
return err_str;
}

return "(no diagnostic)";
}

struct Error: std::runtime_error {
Error(futhark_context* ctx):
std::runtime_error(get_error_str(ctx)) {}
};

template <typename Array, int (*free_fn)(futhark_context* ctx, Array*)>
struct UniqueOpaqueArray {
futhark_context* ctx;
Array* data;

UniqueOpaqueArray(futhark_context* ctx, Array* data):
ctx(ctx), data(data) {
}

explicit UniqueOpaqueArray(futhark_context* ctx):
ctx(ctx), data(nullptr) {
}

UniqueOpaqueArray(UniqueOpaqueArray&& other):
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;
UniqueOpaqueArray& operator=(const UniqueOpaqueArray&) = delete;

~UniqueOpaqueArray() {
if (this->data) {
free_fn(this->ctx, this->data);
}
}

void clear() {
if (this->data) {
free_fn(this->ctx, this->data);
this->data = nullptr;
}
}

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

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

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

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

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

using UniqueLexTable = UniqueOpaqueArray<futhark_opaque_lex_table, futhark_free_opaque_lex_table>;
using UniqueParseTable = UniqueOpaqueArray<futhark_opaque_parse_table, futhark_free_opaque_parse_table>;
using UniqueStackChangeTable = UniqueOpaqueArray<futhark_opaque_stack_change_table, futhark_free_opaque_stack_change_table>;

template <typename T, size_t N>
struct ArrayTraits;

template <typename T, size_t N>
struct UniqueArray {
using Array = typename ArrayTraits<T, N>::Array;

UniqueOpaqueArray<Array, ArrayTraits<T, N>::free_fn> handle;

UniqueArray(futhark_context* ctx, Array* data):
handle(ctx, data) {
}

explicit UniqueArray(futhark_context* ctx):
handle(ctx, nullptr) {
}

template <typename... Sizes>
UniqueArray(futhark_context* ctx, const T* data, Sizes... dims):
handle(ctx, ArrayTraits<T, N>::new_fn(ctx, data, dims...)) {
if (!this->handle.data)
throw Error(this->handle.ctx);
}

void clear() {
this->handle.clear();
}

Array* get() {
return this->handle.get();
}

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

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

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

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

void values(T* out) const {
int err = ArrayTraits<T, N>::values_fn(this->handle.ctx, this->handle.data, out);
if (err != 0)
throw Error(this->handle.ctx);
}

const int64_t* shape() const {
return ArrayTraits<T, N>::shape_fn(this->handle.ctx, this->handle.data);
}
};

template <>
struct ArrayTraits<uint8_t, 1> {
using Array = futhark_u8_1d;
constexpr static const auto new_fn = futhark_new_u8_1d;
constexpr static const auto free_fn = futhark_free_u8_1d;
constexpr static const auto shape_fn = futhark_shape_u8_1d;
constexpr static const auto values_fn = futhark_values_u8_1d;
};

template <>
struct ArrayTraits<uint16_t, 1> {
using Array = futhark_u16_1d;
constexpr static const auto new_fn = futhark_new_u16_1d;
constexpr static const auto free_fn = futhark_free_u16_1d;
constexpr static const auto shape_fn = futhark_shape_u16_1d;
constexpr static const auto values_fn = futhark_values_u16_1d;
};

template <>
struct ArrayTraits<uint16_t, 2> {
using Array = futhark_u16_2d;
constexpr static const auto new_fn = futhark_new_u16_2d;
constexpr static const auto free_fn = futhark_free_u16_2d;
constexpr static const auto shape_fn = futhark_shape_u16_2d;
constexpr static const auto values_fn = futhark_values_u16_2d;
};

template <>
struct ArrayTraits<int32_t, 1> {
using Array = futhark_i32_1d;
constexpr static const auto new_fn = futhark_new_i32_1d;
constexpr static const auto free_fn = futhark_free_i32_1d;
constexpr static const auto shape_fn = futhark_shape_i32_1d;
constexpr static const auto values_fn = futhark_values_i32_1d;
};

template <>
struct ArrayTraits<int32_t, 2> {
using Array = futhark_i32_2d;
constexpr static const auto new_fn = futhark_new_i32_2d;
constexpr static const auto free_fn = futhark_free_i32_2d;
constexpr static const auto shape_fn = futhark_shape_i32_2d;
constexpr static const auto values_fn = futhark_values_i32_2d;
};
}

#endif
Loading

0 comments on commit 71382ab

Please sign in to comment.