forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#34 from Superjomn/fea/init-codegen-c
fea/init codegen c
- Loading branch information
Showing
10 changed files
with
358 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
cc_library( | ||
backends | ||
SRCS outputs.cc | ||
codegen_c.cc | ||
DEPS ir | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include "cinn/backends/codegen_c.h" | ||
|
||
namespace cinn { | ||
namespace backends { | ||
|
||
CodeGenC::CodeGenC(std::ostream &os, Target target) : ir::IrPrinter(os), target_(target) {} | ||
|
||
void CodeGenC::Compile(const lang::Module &module) {} | ||
void CodeGenC::Compile(const lang::LoweredFunc &function) { | ||
os() << "void " << function.name; | ||
|
||
// output arguments | ||
os() << "("; | ||
|
||
auto print_arg = [&](const lang::Argument &arg) { | ||
if (arg.is_buffer()) { | ||
os() << "struct cinn_buffer_t *"; | ||
} else if (arg.is_scalar()) { | ||
os() << PrintType(arg.type) << " "; | ||
os() << arg.name; | ||
} | ||
os() << arg.name; | ||
}; | ||
|
||
for (int i = 0; i < function.args.size() - 1; i++) { | ||
print_arg(function.args[i]); | ||
os() << ", "; | ||
} | ||
if (function.args.size() >= 1) { | ||
print_arg(function.args.back()); | ||
} | ||
|
||
os() << ")"; | ||
|
||
DoIndent(); | ||
os() << "{\n"; | ||
|
||
Print(function.body); | ||
|
||
DoIndent(); | ||
os() << "}"; | ||
} | ||
void CodeGenC::Compile(const ir::Buffer &buffer) {} | ||
std::string CodeGenC::PrintType(Type type) { return std::__cxx11::string(); } | ||
void CodeGenC::Visit(const ir::IntImm *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::UIntImm *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::FloatImm *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Add *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Sub *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Mul *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Div *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Mod *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::EQ *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::NE *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::LT *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::LE *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::GT *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::GE *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::And *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Or *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Min *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Max *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Minus *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Not *op) { | ||
os() << "(!"; | ||
IrPrinter::Print(op->v); | ||
os() << ")"; | ||
} | ||
void CodeGenC::Visit(const ir::Cast *op) { PrintCastExpr(op->type(), op->v); } | ||
void CodeGenC::Visit(const ir::For *op) { LOG(FATAL) << "Not Implemented"; } | ||
void CodeGenC::Visit(const ir::PolyFor *op) { | ||
os() << "for ("; | ||
Print(op->init); | ||
os() << "; "; | ||
Print(op->condition); | ||
os() << "; "; | ||
Print(op->inc); | ||
os() << ")"; | ||
|
||
Print(op->body); | ||
} | ||
void CodeGenC::Visit(const ir::Select *op) { | ||
os() << "("; | ||
os() << "("; | ||
Print(op->condition); | ||
os() << ") ? "; | ||
Print(op->true_value); | ||
os() << " : "; | ||
Print(op->false_value); | ||
os() << ")"; | ||
} | ||
void CodeGenC::Visit(const ir::IfThenElse *op) { | ||
os() << "if ("; | ||
Print(op->condition); | ||
os() << ")"; | ||
Print(op->true_case); | ||
|
||
if (op->false_case.defined()) { | ||
os() << "else\n"; | ||
Print(op->false_case); | ||
} | ||
} | ||
void CodeGenC::Visit(const ir::Block *op) { | ||
os() << "{\n"; | ||
|
||
IncIndent(); | ||
|
||
for (int i = 0; i < op->stmts.size() - 1; i++) { | ||
DoIndent(); | ||
Print(op->stmts[i]); | ||
os() << ";\n"; | ||
} | ||
if (op->stmts.size() >= 1) { | ||
DoIndent(); | ||
Print(op->stmts.back()); | ||
os() << ";"; | ||
} | ||
|
||
DecIndent(); | ||
os() << "\n"; | ||
DoIndent(); | ||
os() << "}"; | ||
} | ||
void CodeGenC::Visit(const ir::Call *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Module *op) { NOT_IMPLEMENTED } | ||
void CodeGenC::Visit(const ir::_Var_ *op) { os() << op->name; } | ||
void CodeGenC::Visit(const ir::Load *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Store *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Alloc *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::Free *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::_Range_ *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::_IterVar_ *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::_Buffer_ *op) { IrPrinter::Visit(op); } | ||
void CodeGenC::Visit(const ir::_Tensor_ *op) { IrPrinter::Visit(op); } | ||
|
||
void CodeGenC::PrintCastExpr(const Type &type, Expr e) { | ||
os() << PrintType(type) << "("; | ||
Print(e); | ||
os() << ")"; | ||
} | ||
|
||
} // namespace backends | ||
} // namespace cinn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "cinn/common/common.h" | ||
#include "cinn/ir/function.h" | ||
#include "cinn/ir/ir.h" | ||
#include "cinn/ir/ir_printer.h" | ||
#include "cinn/lang/module.h" | ||
|
||
namespace cinn { | ||
|
||
namespace lang { | ||
class Module; | ||
} // namespace lang | ||
|
||
namespace backends { | ||
|
||
class CodeGenC : public ir::IrPrinter { | ||
public: | ||
CodeGenC(std::ostream& os, Target target); | ||
|
||
void Compile(const lang::Module& module); | ||
|
||
protected: | ||
void Compile(const lang::LoweredFunc& function); | ||
void Compile(const ir::Buffer& buffer); | ||
|
||
std::string PrintType(Type type); | ||
void PrintCastExpr(const Type& type, Expr e); | ||
|
||
#define __DEFINE_VISIT(op__) void Visit(const ir::op__* op) override; | ||
NODETY_FORALL(__DEFINE_VISIT) | ||
#undef __DEFINE_VISIT | ||
|
||
private: | ||
Target target_; | ||
}; | ||
|
||
} // namespace backends | ||
} // namespace cinn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
cc_library(runtime SRCS | ||
intrinsic.cc | ||
buffer.cc | ||
cinn_runtime.cc | ||
DEPS common ir) |
Empty file.
Oops, something went wrong.