Skip to content

Commit

Permalink
Merge pull request #29 from AnyDSL/development
Browse files Browse the repository at this point in the history
Merge recent changes from development branch
  • Loading branch information
Hugobros3 authored Nov 22, 2024
2 parents fe4eeb5 + 45a32b3 commit 130e2e2
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: build-and-test

on:
push:
branches: [ master ]
branches: [ master, development ]
pull_request:
branches: [ master ]
branches: [ master, development ]

jobs:
build-and-test:
Expand Down
11 changes: 11 additions & 0 deletions include/artic/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ struct TypeApp : public Type {
void print(Printer&) const override;
};

/// The codomain of functions that don't return anything.
struct NoCodomType : public Type {
NoCodomType(const Loc& loc)
: Type(loc)
{}

const artic::Type* infer(TypeChecker&) override;
void bind(NameBinder&) override;
void print(Printer&) const override;
};

/// Type resulting from a parsing error.
struct ErrorType : public Type {
ErrorType(const Loc& loc)
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ if (Thorin_HAS_LLVM_SUPPORT)
target_compile_definitions(artic PUBLIC -DENABLE_LLVM)
llvm_config(artic ${AnyDSL_LLVM_LINK_SHARED} core support)
endif ()
if (Thorin_HAS_SPIRV_SUPPORT)
target_compile_definitions(artic PUBLIC -DENABLE_SPIRV)
endif ()
2 changes: 2 additions & 0 deletions src/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ void TypeApp::bind(NameBinder& binder) {
binder.bind(path);
}

void NoCodomType::bind(NameBinder&) {}

void ErrorType::bind(NameBinder&) {}

// Statements ----------------------------------------------------------------------
Expand Down
13 changes: 12 additions & 1 deletion src/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,13 @@ bool TypeChecker::check_filter(const ast::Expr& expr) {
is_mutable = true;
else
return true;
} else if (expr.isa<ast::LiteralExpr>())
} else if (expr.isa<ast::LiteralExpr>()) {
return true;
} else if (auto proj = expr.isa<ast::ProjExpr>()) {
//This needs to be supported to inspect struct and tuple members.
//TODO: Not sure if this check coveres all possible problematic cases.
return check_filter(*proj->expr);
}

error(expr.loc, "unsupported expression in filter");
if (is_logic_or)
Expand Down Expand Up @@ -885,6 +890,8 @@ const artic::Type* UnsizedArrayType::infer(TypeChecker& checker) {
}

const artic::Type* FnType::infer(TypeChecker& checker) {
if (to->isa<ast::NoCodomType>())
return checker.type_table.cn_type(checker.infer(*from));
return checker.type_table.fn_type(checker.infer(*from), checker.infer(*to));
}

Expand All @@ -901,6 +908,10 @@ const artic::Type* TypeApp::infer(TypeChecker& checker) {
return path.type = path.infer(checker, false);
}

const artic::Type* NoCodomType::infer(TypeChecker& checker) {
return checker.type_table.no_ret_type();
}

// Statements ----------------------------------------------------------------------

const artic::Type* DeclStmt::infer(TypeChecker& checker) {
Expand Down
12 changes: 6 additions & 6 deletions src/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ const thorin::Def* StaticDecl::emit(Emitter& emitter) const {

const thorin::Def* FnDecl::emit(Emitter& emitter) const {
auto _ = emitter.save_state();
const thorin::FnType* cont_type = nullptr;
const artic::FnType* fn_type = nullptr;
Emitter::MonoFn mono_fn { this, {} };
if (type_params) {
for (auto& param : type_params->params)
Expand All @@ -1667,12 +1667,12 @@ const thorin::Def* FnDecl::emit(Emitter& emitter) const {
if (auto it = emitter.mono_fns.find(mono_fn); it != emitter.mono_fns.end())
return it->second;
emitter.poly_defs.emplace_back();
cont_type = type->as<artic::ForallType>()->body->convert(emitter)->as<thorin::FnType>();
fn_type = type->as<artic::ForallType>()->body->as<artic::FnType>();
} else {
cont_type = type->convert(emitter)->as<thorin::FnType>();
fn_type = type->as<artic::FnType>();
}

auto cont = emitter.world.continuation(cont_type, emitter.debug_info(*this));
auto cont = emitter.world.continuation(fn_type->convert(emitter)->as<thorin::FnType>(), emitter.debug_info(*this));
if (type_params)
emitter.mono_fns.emplace(std::move(mono_fn), cont);

Expand Down Expand Up @@ -1715,7 +1715,7 @@ const thorin::Def* FnDecl::emit(Emitter& emitter) const {
fn->def = def = cont;

emitter.enter(cont);
emitter.emit(*fn->param, emitter.tuple_from_params(cont, true));
emitter.emit(*fn->param, emitter.tuple_from_params(cont, !fn_type->codom->isa<artic::NoRetType>()));
if (fn->filter)
cont->set_filter(emitter.world.filter(thorin::Array<const thorin::Def*>(cont->num_params(), emitter.emit(*fn->filter))));
auto value = emitter.emit(*fn->body);
Expand Down Expand Up @@ -1893,7 +1893,7 @@ std::string PtrType::stringify(Emitter& emitter) const {
}

const thorin::Type* PtrType::convert(Emitter& emitter) const {
return emitter.world.ptr_type(pointee->convert(emitter), 1, -1, thorin::AddrSpace(addr_space));
return emitter.world.ptr_type(pointee->convert(emitter), 1, thorin::AddrSpace(addr_space));
}

std::string ImplicitParamType::stringify(Emitter& emitter) const {
Expand Down
76 changes: 49 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifdef ENABLE_LLVM
#include <thorin/be/llvm/cpu.h>
#endif
#ifdef ENABLE_SPIRV
#include <thorin/be/spirv/spirv.h>
#endif

using namespace artic;

Expand Down Expand Up @@ -94,9 +97,11 @@ struct ProgramOptions {
bool print_ast = false;
bool emit_thorin = false;
bool emit_c_int = false;
bool emit_host_code = false;
bool emit_c = false;
bool emit_json = false;
bool emit_llvm = false;
bool emit_spirv = false;
std::string host_triple;
std::string host_cpu;
std::string host_attr;
Expand Down Expand Up @@ -195,12 +200,22 @@ struct ProgramOptions {
tab_width = std::strtoull(argv[++i], NULL, 10);
} else if (matches(argv[i], "--emit-llvm")) {
#ifdef ENABLE_LLVM
emit_host_code = true;
emit_llvm = true;
#else
log::error("Thorin is built without LLVM support, use '--emit-c' instead");
return false;
#endif
} else if (matches(argv[i], "--emit-spirv")) {
#ifdef ENABLE_SPIRV
emit_host_code = true;
emit_spirv = true;
#else
log::error("Thorin is built without SPIR-V support");
return false;
#endif
} else if (matches(argv[i], "--emit-c")) {
emit_host_code = true;
emit_c = true;
} else if (matches(argv[i], "--host-triple")) {
if (!check_arg(argc, argv, i))
Expand Down Expand Up @@ -341,41 +356,48 @@ int main(int argc, char** argv) {
thorin::c::emit_c_int(thorin, stream);
}
}
if (opts.opt_level > 1 || opts.emit_c || opts.emit_llvm)
if (opts.opt_level > 1 || opts.emit_host_code)
thorin.opt();
if (opts.emit_thorin)
thorin.world().dump_scoped(!opts.no_color);
if (opts.emit_json || opts.emit_c || opts.emit_llvm) {
auto emit_to_file = [&] (thorin::CodeGen& cg) {
auto name = opts.module_name + cg.file_ext();
std::ofstream file(name);
if (!file)
log::error("cannot open '{}' for writing", name);
else
cg.emit_stream(file);
};

auto emit_to_file = [&] (thorin::CodeGen& cg) {
auto name = opts.module_name + cg.file_ext();
std::ofstream file(name);
if (!file)
log::error("cannot open '{}' for writing", name);
else
cg.emit_stream(file);
};
#ifdef ENABLE_JSON
if (opts.emit_json) {
thorin::json::CodeGen cg(thorin, opts.debug, opts.host_triple, opts.host_cpu, opts.host_attr);
if (opts.emit_json) {
thorin::json::CodeGen cg(thorin, opts.debug, opts.host_triple, opts.host_cpu, opts.host_attr);
emit_to_file(cg);
}
#endif
if (opts.emit_host_code) {
thorin::DeviceBackends backends(thorin.world(), opts.opt_level, opts.debug, opts.hls_flags);
if (opts.emit_c) {
thorin::Cont2Config kernel_configs;
thorin::c::CodeGen cg(thorin, kernel_configs, thorin::c::Lang::C99, opts.debug, opts.hls_flags);
emit_to_file(cg);
}
#endif
if (opts.emit_c || opts.emit_llvm) {
thorin::DeviceBackends backends(thorin.world(), opts.opt_level, opts.debug, opts.hls_flags);
if (opts.emit_c) {
thorin::Cont2Config kernel_configs;
thorin::c::CodeGen cg(thorin, kernel_configs, thorin::c::Lang::C99, opts.debug, opts.hls_flags);
emit_to_file(cg);
}
#ifdef ENABLE_LLVM
if (opts.emit_llvm) {
thorin::llvm::CPUCodeGen cg(thorin, opts.opt_level, opts.debug, opts.host_triple, opts.host_cpu, opts.host_attr);
emit_to_file(cg);
}
if (opts.emit_llvm) {
thorin::llvm::CPUCodeGen cg(thorin, opts.opt_level, opts.debug, opts.host_triple, opts.host_cpu, opts.host_attr);
emit_to_file(cg);
}
#endif
#ifdef ENABLE_SPIRV
if (opts.emit_spirv) {
thorin::spirv::Target target;

thorin::spirv::CodeGen cg(thorin, target, opts.debug);
emit_to_file(cg);
}
#endif
for (auto& cg : backends.cgs) {
if (cg) emit_to_file(*cg);
}
for (auto& cg : backends.cgs) {
if (cg) emit_to_file(*cg);
}
}
return EXIT_SUCCESS;
Expand Down
14 changes: 11 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ Ptr<ast::FnDecl> Parser::parse_fn_decl() {
error(ahead().loc(), "parameter list expected in function definition");

Ptr<ast::Type> ret_type;
if (accept(Token::Arrow))
ret_type = parse_type();
if (accept(Token::Arrow)) {
if (accept(Token::Not))
ret_type = make_ptr<ast::NoCodomType>(prev_);
else
ret_type = parse_type();
}

Ptr<ast::Expr> body;
if (ahead().tag() == Token::LBrace)
Expand Down Expand Up @@ -1087,7 +1091,11 @@ Ptr<ast::FnType> Parser::parse_fn_type() {
else
from = parse_error_type();
expect(Token::Arrow);
auto to = parse_type();
Ptr<ast::Type> to;
if (accept(Token::Not))
to = make_ptr<ast::NoCodomType>(prev_);
else
to = parse_type();
return make_ptr<ast::FnType>(tracker(), std::move(from), std::move(to));
}

Expand Down
10 changes: 6 additions & 4 deletions src/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,8 @@ void UnsizedArrayType::print(Printer& p) const {
void FnType::print(Printer& p) const {
p << log::keyword_style("fn") << ' ';
print_parens(p, from);
if (to) {
p << " -> ";
to->print(p);
}
p << " -> ";
to->print(p);
}

void PtrType::print(Printer& p) const {
Expand All @@ -698,6 +696,10 @@ void TypeApp::print(Printer& p) const {
path.print(p);
}

void NoCodomType::print(artic::Printer& p) const {
p << "!";
}

void ErrorType::print(Printer& p) const {
p << log::error_style("<invalid type>");
}
Expand Down
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ if (Thorin_HAS_LLVM_SUPPORT)
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

if (NOT TARGET clang)
find_package(Clang REQUIRED CONFIG PATHS ${LLVM_DIR}/../clang NO_DEFAULT_PATH)
find_package(Clang REQUIRED CONFIG PATHS ${LLVM_DIR}/../clang)
endif()

# Compile the helper functions into an object file
Expand Down Expand Up @@ -231,6 +231,8 @@ if (Thorin_HAS_LLVM_SUPPORT)
SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/codegen/compare.art)
endif ()

add_subdirectory(thorin)

if (CODE_COVERAGE AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(COVERAGE_EXCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/codegen/*")
setup_target_for_coverage_gcovr_html(
Expand Down
37 changes: 37 additions & 0 deletions test/thorin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# silly hack because CMake has no plain expressions in its grammar :(
# https://stackoverflow.com/questions/62487808/can-i-set-a-cmake-variable-to-the-result-of-a-boolean-expression
macro(assign_me_bool var)
if(${ARGN})
set(${var} ON)
else()
set(${var} OFF)
endif()
endmacro()

function(add_thorin_test)
cmake_parse_arguments(test "NO_C;NO_LLVM;NO_SPIRV" "NAME;SOURCE_FILE" "ARGS" ${ARGN})
assign_me_bool(TEST_USE_C NOT ${test_NO_C})
assign_me_bool(HAS_LLVM ${Thorin_HAS_LLVM_SUPPORT})
assign_me_bool(TEST_USE_LLVM ${HAS_LLVM} AND NOT ${test_NO_LLVM})
assign_me_bool(HAS_SPIRV ${Thorin_HAS_SPIRV_SUPPORT})
assign_me_bool(TEST_USE_SPIRV ${HAS_SPIRV} AND NOT ${test_NO_SPIRV})

add_test(NAME thorin_${test_NAME} COMMAND ${CMAKE_COMMAND}
-DCOMPILER=$<TARGET_FILE:artic>
-DC_COMPILER=${CMAKE_C_COMPILER}
-DT=${test_NAME}
"-DTARGS=${test_ARGS}"
-DSRC=${CMAKE_CURRENT_SOURCE_DIR}/${test_SOURCE_FILE}.art
-DDST=${CMAKE_CURRENT_BINARY_DIR}
-DC=${TEST_USE_C}
-DLLVM=${TEST_USE_LLVM}
-DSPIRV=${TEST_USE_SPIRV}
-DMSVC=${MSVC}
-P ${PROJECT_SOURCE_DIR}/test/thorin/oracle.cmake)
endfunction()

add_thorin_test(NAME hello_world SOURCE_FILE hello_world)
add_thorin_test(NAME llvm_intrinsic SOURCE_FILE llvm_intrinsic NO_C NO_SPIRV)
add_thorin_test(NAME spirv_builtin SOURCE_FILE spirv_builtin NO_C NO_LLVM)
add_thorin_test(NAME control_flow SOURCE_FILE control_flow)
add_thorin_test(NAME slot SOURCE_FILE slot)
17 changes: 17 additions & 0 deletions test/thorin/control_flow.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[export]
fn test_branch(x: i32) -> i32 {
if (x > 0) {
1 / x
} else {
0
}
}

#[export]
fn test_switch(x: i32) -> i32 {
match (x) {
0 => x,
1 => 1,
_ => 1 / x
}
}
4 changes: 4 additions & 0 deletions test/thorin/hello_world.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[export]
fn hello() -> i32 {
42
}
6 changes: 6 additions & 0 deletions test/thorin/llvm_intrinsic.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[import(cc = "device", name = "llvm.exp.f32")] fn cpu_expf(_: f32) -> f32;

#[export]
fn foo(f: f32) -> f32 {
cpu_expf(f - 0.5) + 0.5
}
Loading

0 comments on commit 130e2e2

Please sign in to comment.