Skip to content

Commit

Permalink
Add ExprBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
chlict committed Sep 26, 2020
1 parent 727f633 commit 1517548
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
60 changes: 60 additions & 0 deletions src/includes/ExprBlock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <iostream>
#include <boost/yap/yap.hpp>
#include <boost/yap/print.hpp>
#include <boost/yap/expression.hpp>
#include <boost/hana.hpp>
#include "xforms/AllocTensor.hpp"
#include "xforms/CodeGen.hpp"

template <typename ...Exprs>
struct ExprBlock {
// static_assert(all_are yap_expr_type)
boost::hana::tuple<Exprs...> expr_block_;

constexpr ExprBlock(Exprs const &... exprs) : expr_block_(exprs...) {}

constexpr ExprBlock(ExprBlock const& other) : expr_block_(other.expr_block_) {}

constexpr ExprBlock(ExprBlock&& other) noexcept : expr_block_(other.expr_block_) {}

// template <typename... BodyExprs>
// constexpr auto operator[] (BodyExprs &&... body_exprs) const {
// auto body_list = hana::tuple(body_exprs...);
// }

template <typename ...Args>
constexpr auto gen_code(Args &&... args) const {
namespace yap = boost::yap;

// First replace all the placeholders in expr with args
auto ir_list = hana::transform(expr_block_, [&args...](auto const& expr) {
return yap::replace_placeholders(expr, static_cast<Args &&>(args)...);
});
// auto constexpr dumping = Dumping();
// if constexpr (need_dump(dumping)) {
// yap::print(std::cout, ast);
// }

// Go through a set of transforms
auto xforms = hana::make_tuple(
CodeGen()
);

// Call each xform's transform() method. Each xform's output serves as input of next xform.
auto codes = hana::fold_left(xforms, ir_list, [](auto &&ir, auto &&xform) {
return xform.transform(ir, with_dump{});
});

return codes;
}

friend std::ostream& operator<< (std::ostream &os, ExprBlock const& L) {
namespace hana = boost::hana;
hana::for_each(L.expr_block_, [&os](auto const &expr) {
boost::yap::print(os, expr);
});
return os;
}
};
3 changes: 0 additions & 3 deletions src/includes/xforms/GenIR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#include "Placeholder.hpp"
#include "xforms/XformUtils.hpp"

namespace yap = boost::yap;
namespace hana = boost::hana;

template <typename Sequence, typename Stack, long long I = 1>
struct GenIRXform {
Sequence mIRList;
Expand Down
7 changes: 5 additions & 2 deletions src/includes/xforms/Placeholder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

#include <boost/yap/expression.hpp>
#include <iostream>
#include "Utils.hpp"

struct temp_placeholder_tag;

template <typename T>
constexpr bool is_temp = is_a<temp_placeholder_tag, T>;

// A placeholder for temporary variables
template <long long I>
struct temp_placeholder : boost::hana::llong<I> {
Expand All @@ -20,6 +24,5 @@ struct temp_placeholder : boost::hana::llong<I> {

auto const _0 = boost::yap::make_terminal(temp_placeholder<0>{});
auto const _1 = boost::yap::make_terminal(temp_placeholder<1>{});
auto const _2 = boost::yap::make_terminal(temp_placeholder<1>{});

template <typename T>
constexpr bool is_temp = is_a<temp_placeholder_tag, T>;
3 changes: 3 additions & 0 deletions src/includes/xforms/XformUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "Utils.hpp"
#include "xforms/PrintIR.hpp"

namespace yap = boost::yap;
namespace hana = boost::hana;

struct xform_pass_tag;

// Transform at compile time
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

# adding the run_tests target
add_executable(run_tests test.cpp TestDims.cpp TestLayout.cpp TestTensorFormat.cpp TestLiterals.cpp TestUtils.cpp
TestTensor.cpp TestTensorOps.cpp TestGenIR.cpp TestTensorOps2.cpp TestAllocTensor.cpp TestCodeGen.cpp TestECompiler.cpp TestTiling.cpp TestOperator.cpp TestTilingService.cpp)
TestTensor.cpp TestTensorOps.cpp TestGenIR.cpp TestTensorOps2.cpp TestAllocTensor.cpp TestCodeGen.cpp TestECompiler.cpp TestTiling.cpp TestOperator.cpp TestTilingService.cpp TestExprList.cpp)

target_link_libraries(run_tests gtest gtest_main)
45 changes: 45 additions & 0 deletions test/TestExprList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "gtest/gtest.h"
#include "ExprBlock.hpp"
using namespace boost::yap::literals;


TEST(TestExprList, Test1) {
auto list1 = ExprBlock {
1_p + 2_p,
2_p * 3_p
};
std::cout << list1 << std::endl;

list1.gen_code(1, 2, 3);
}

TEST(TestExprBlock, Test2) {
auto format1 = make_format(Dim2(2, 4), RowMajorLayout());
auto tensor1 = TensorE(float(), format1, MemSpace::GM(), 0x10);
auto tensor2 = TensorE(float(), format1, MemSpace::GM(), 0x20);
auto tensor3 = TensorE(float(), format1, MemSpace::GM(), 0x30);
auto temp_1 = TensorE(float(), format1, MemSpace::GM(), 0x40);

auto add_mul = ExprBlock {
temp_1 = tensor1 + tensor2,
tensor3 = temp_1 * tensor3
};

add_mul.gen_code()();
}

//TEST(TestExprBlock, Test3) {
// auto format1 = make_format(Dim2(2, 4), RowMajorLayout());
// auto tensor1 = Tensor(float(), format1, MemSpace::GM(), 0x10);
// auto tensor2 = Tensor(float(), format1, MemSpace::GM(), 0x20);
// auto tensor3 = Tensor(float(), format1, MemSpace::GM(), 0x30);
// auto temp_1 = Tensor(float(), format1, MemSpace::GM(), 0x40);
//
// auto add_mul = ExprBlock {
// _1 = 1_p + 2_p,
// // sync(),
// _1 * 3_p
// };
//
// add_mul.gen_code(tensor1, tensor2, tensor3)();
//}
2 changes: 1 addition & 1 deletion test/TestOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST(TestOperator, Test3) {
auto mul_add = [](auto &&... args) {
using namespace boost::yap::literals;
auto op = TOperator(1_p = 2_p + 3_p, args...);
op.gen_code()();
op.gen_code()(); // with execution
};

mul_add(dest, src1, src2);
Expand Down

0 comments on commit 1517548

Please sign in to comment.