From a0ecdba5a220682dd678158d7ef91122259e7fc0 Mon Sep 17 00:00:00 2001 From: Philippe Tillet Date: Sat, 12 Jan 2019 23:24:25 -0500 Subject: [PATCH] [code generation] testing analysis passes --- examples/matrix.cpp | 10 +++++++++- include/codegen/allocation.h | 5 +++-- include/codegen/liveness.h | 3 --- include/codegen/selection.h | 10 ++++++++++ include/ir/builder.h | 5 +++-- include/ir/instructions.h | 20 +++++++++++++++++++- lib/codegen/allocation.cpp | 4 ++-- lib/codegen/liveness.cpp | 5 +---- lib/codegen/selection.cpp | 9 ++++++++- lib/ir/builder.cpp | 23 ++++++++++++++++++++--- lib/ir/instructions.cpp | 12 ++++++++++-- lib/ir/type.cpp | 2 +- 12 files changed, 86 insertions(+), 22 deletions(-) diff --git a/examples/matrix.cpp b/examples/matrix.cpp index e9d380f39..426294a63 100644 --- a/examples/matrix.cpp +++ b/examples/matrix.cpp @@ -5,6 +5,9 @@ #include "ir/module.h" #include "codegen/selection.h" #include "codegen/tune.h" +#include "codegen/shared_copy.h" +#include "codegen/allocation.h" +#include "codegen/liveness.h" #include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/Module.h" #include "llvm/IR/LLVMContext.h" @@ -53,9 +56,14 @@ int main() { llvm::LLVMContext llvm_context; llvm::Module llvm_module("test", llvm_context); // lowering passes - tdl::codegen::selection selection; + tdl::codegen::place_shared_copy shared; tdl::codegen::tune tune; + tdl::codegen::liveness liveness; + tdl::codegen::allocation allocation(&liveness); tune.run(module); + shared.run(module); + liveness.run(module); + allocation.run(); std::vector params; tune.get_params(module, params); std::cout << params.size() << std::endl; diff --git a/include/codegen/allocation.h b/include/codegen/allocation.h index b93322539..4b90cf46a 100644 --- a/include/codegen/allocation.h +++ b/include/codegen/allocation.h @@ -20,6 +20,9 @@ class loop_info; class allocation { public: + allocation(liveness *live) + : liveness_(live){ } + // accessors unsigned get_offset(ir::value *x) const { return offsets_.at(x); } unsigned get_allocated_size() const { return allocated_size_; } @@ -33,8 +36,6 @@ private: size_t allocated_size_; // dependences liveness *liveness_; - layout *layout_; - loop_info *loop_info_; }; } diff --git a/include/codegen/liveness.h b/include/codegen/liveness.h index c7cfb5959..11d377c62 100644 --- a/include/codegen/liveness.h +++ b/include/codegen/liveness.h @@ -13,8 +13,6 @@ namespace ir{ namespace codegen{ -class layout; - typedef unsigned slot_index; struct segment { @@ -54,7 +52,6 @@ private: has_storage_map_t has_dedicated_storage_; indices_map_t indices_; intervals_map_t intervals_; - layout* layouts_; }; } diff --git a/include/codegen/selection.h b/include/codegen/selection.h index b55725c05..a8d4aabce 100644 --- a/include/codegen/selection.h +++ b/include/codegen/selection.h @@ -20,6 +20,14 @@ namespace llvm{ namespace tdl{ namespace codegen{ +class allocation; + +struct distributed_axis { + +}; + + + class selection{ typedef std::map vmap_t; typedef std::map bmap_t; @@ -31,11 +39,13 @@ private: llvm::Constant* llvm_constant(ir::constant *cst, llvm::LLVMContext &ctx); public: + selection(allocation *alloc): alloc_(alloc){ } void run(ir::module &src, llvm::Module &dst); private: vmap_t vmap_; bmap_t bmap_; + allocation *alloc_; }; diff --git a/include/ir/builder.h b/include/ir/builder.h index c84ef02d8..438390940 100644 --- a/include/ir/builder.h +++ b/include/ir/builder.h @@ -27,6 +27,7 @@ public: builder(context &ctx); // Setters void set_insert_point(iterator instr); + void set_insert_point(instruction* i); void set_insert_point(basic_block* block); basic_block* get_insert_block() { return block_; } iterator get_insert_point() { return insert_point_;} @@ -42,7 +43,6 @@ public: block_->get_inst_list().insert(insert_point_, inst); inst->set_parent(block_); inst->set_name(name); - insert_point_ = block_->end(); return inst; } // terminator instructions @@ -116,7 +116,8 @@ public: // Built-in instruction value *create_get_global_range(unsigned axis, unsigned size, const std::string &name = ""); value *create_matmul(value *A, value *B, value *C, const std::string &name = ""); - + // Intrinsics + value *create_copy_to_shared(value *arg, const std::string &name = ""); private: context &ctx_; basic_block *block_; diff --git a/include/ir/instructions.h b/include/ir/instructions.h index fa7a0d6e3..0b3295658 100644 --- a/include/ir/instructions.h +++ b/include/ir/instructions.h @@ -347,7 +347,10 @@ public: }; -// built-in +//===----------------------------------------------------------------------===// +// builtin_inst classes +//===----------------------------------------------------------------------===// + class builtin_inst: public instruction{ protected: using instruction::instruction; @@ -374,6 +377,21 @@ public: instruction *next = nullptr); }; + +//===----------------------------------------------------------------------===// +// intrinsics classes +//===----------------------------------------------------------------------===// + + +class copy_to_shared_inst: public unary_inst{ + using unary_inst::unary_inst; + +public: + static copy_to_shared_inst* create(value *arg, const std::string &name = "", + instruction *next = nullptr); +}; + + } } diff --git a/lib/codegen/allocation.cpp b/lib/codegen/allocation.cpp index 1730371ae..7a5154280 100644 --- a/lib/codegen/allocation.cpp +++ b/lib/codegen/allocation.cpp @@ -10,7 +10,6 @@ namespace tdl{ namespace codegen{ - void allocation::run(){ using std::max; using std::min; @@ -108,8 +107,9 @@ void allocation::run(){ // Save maximum size of induced memory space allocated_size_ = 0; - for(auto &x: offsets_) + for(auto &x: offsets_){ allocated_size_ = std::max(allocated_size_, x.second + get_num_bytes(x.first)); + } } } diff --git a/lib/codegen/liveness.cpp b/lib/codegen/liveness.cpp index 824c95590..bf4c99be2 100644 --- a/lib/codegen/liveness.cpp +++ b/lib/codegen/liveness.cpp @@ -1,5 +1,4 @@ #include "codegen/liveness.h" -#include "codegen/layout.h" #include "ir/basic_block.h" #include "ir/function.h" #include "ir/module.h" @@ -24,9 +23,7 @@ for(ir::function *fn: mod.get_function_list()){ // Creates live intervals for(auto i: indices_){ ir::value *v = i.first; - if(!layouts_->get_num_shared_views(v)) - continue; - if(!layouts_->get_shared_view(v, 0).has_dedicated_storage) + if(!dynamic_cast(v)) continue; unsigned start = i.second; unsigned end = start; diff --git a/lib/codegen/selection.cpp b/lib/codegen/selection.cpp index edf48262c..4ce0d1b38 100644 --- a/lib/codegen/selection.cpp +++ b/lib/codegen/selection.cpp @@ -63,7 +63,7 @@ Constant *selection::llvm_constant(ir::constant *cst, LLVMContext &ctx) { } -/* convert ir::instruction to Instruction */ +/* convert ir::instruction to llvm::Instruction */ Instruction *selection::llvm_inst(ir::instruction *inst, LLVMContext & ctx) { auto value = [&](ir::value *x) { return llvm_value(x, ctx); }; auto block = [&](ir::basic_block *x) { return bmap_.at(x); }; @@ -125,7 +125,9 @@ Instruction *selection::llvm_inst(ir::instruction *inst, LLVMContext & ctx) { throw std::runtime_error("unknown conversion from ir::type to Type"); } +/* convert ir::value to llvm::Value */ Value* selection::llvm_value(ir::value *v, LLVMContext &ctx) { + assert(!v->get_type()->is_tile_ty()); if(vmap_.find(v) != vmap_.end()) return vmap_.at(v); // create operands @@ -141,6 +143,11 @@ Value* selection::llvm_value(ir::value *v, LLVMContext &ctx) { throw std::runtime_error("unknown conversion from ir::value to Value"); } +/* lower tile to a set of llvm::Value's */ +//void selection::lower_tile(ir::value *v) { + +//} + void selection::run(ir::module &src, Module &dst){ vmap_.clear(); bmap_.clear(); diff --git a/lib/ir/builder.cpp b/lib/ir/builder.cpp index 8d3f58792..96e52c1d8 100644 --- a/lib/ir/builder.cpp +++ b/lib/ir/builder.cpp @@ -3,6 +3,7 @@ #include "ir/builder.h" #include "ir/constant.h" #include "ir/instructions.h" +#include "ir/intrinsics.h" #include "ir/type.h" #include "llvm/IR/Instruction.h" @@ -16,11 +17,18 @@ builder::builder(context &ctx): // utilities //===----------------------------------------------------------------------===// -void builder::set_insert_point(basic_block::iterator instr){ - block_ = (*instr)->get_parent(); - insert_point_ = instr; +void builder::set_insert_point(basic_block::iterator it){ + block_ = (*it)->get_parent(); + insert_point_ = it; } +void builder::set_insert_point(instruction* i){ + block_ = i->get_parent(); + auto it = std::find(block_->begin(), block_->end(), i); + set_insert_point(it); +} + + void builder::set_insert_point(basic_block *block){ block_ = block; insert_point_ = block->end(); @@ -262,5 +270,14 @@ value *builder::create_matmul(value *A, value *B, value *C, const std::string &n return insert(matmul_inst::create(A, B, C, name)); } +//===----------------------------------------------------------------------===// +// intrinsic instructions +//===----------------------------------------------------------------------===// + + +value *builder::create_copy_to_shared(value *arg, const std::string &name) { + return insert(copy_to_shared_inst::create(arg, name)); +} + } } diff --git a/lib/ir/instructions.cpp b/lib/ir/instructions.cpp index 0a62e9a6d..679ee5bb2 100644 --- a/lib/ir/instructions.cpp +++ b/lib/ir/instructions.cpp @@ -360,8 +360,8 @@ matmul_inst::matmul_inst(value *A, value *B, value *C, const std::string &name, instruction *next) : builtin_inst(C->get_type(), 3, name, next) { set_operand(0, A); - set_operand(0, B); - set_operand(0, C); + set_operand(1, B); + set_operand(2, C); } instruction *matmul_inst::create(value *A, value *B, value *C, @@ -385,5 +385,13 @@ instruction* get_global_range_inst::create(context &ctx, unsigned axis, unsigned return new get_global_range_inst(tile_ty, axis, name, next); } +//===----------------------------------------------------------------------===// +// intrinsic instructions +//===----------------------------------------------------------------------===// +copy_to_shared_inst* copy_to_shared_inst::create(value *arg, const std::string &name, + instruction *next) { + return new copy_to_shared_inst(arg->get_type(), arg, name, next); +} + } } diff --git a/lib/ir/type.cpp b/lib/ir/type.cpp index 98ec78508..075bcd88b 100644 --- a/lib/ir/type.cpp +++ b/lib/ir/type.cpp @@ -160,7 +160,7 @@ bool tile_type::is_valid_elt_ty(type *ty) { unsigned tile_type::get_num_elements() const { unsigned res = 1; for(unsigned shape: shapes_) - shape *= res; + res *= shape; return res; }