|
| 1 | +#pragma once |
| 2 | +#include "cinn/ir/buffer.h" |
| 3 | +#include "cinn/ir/node.h" |
| 4 | + |
| 5 | +namespace cinn { |
| 6 | +namespace ir { |
| 7 | + |
| 8 | +class _LoweredFunc_; |
| 9 | + |
| 10 | +/** |
| 11 | + * A struct representing an argument to a lowered function. Used for specifying the function signature of generated |
| 12 | + * code. |
| 13 | + */ |
| 14 | +struct Argument { |
| 15 | + //! The name of the argument. |
| 16 | + std::string name; |
| 17 | + |
| 18 | + enum class Kind { kScalar = 0, kBuffer } kind{Kind::kScalar}; |
| 19 | + |
| 20 | + //! Number of the dimensions of buffer. |
| 21 | + uint32_t ndims{0}; |
| 22 | + |
| 23 | + //! The type of the buffer or scalar. |
| 24 | + Type type; |
| 25 | + |
| 26 | + bool is_buffer() const { return kind == Kind::kBuffer; } |
| 27 | + bool is_scalar() const { return kind == Kind::kScalar; } |
| 28 | + |
| 29 | + Argument() {} |
| 30 | + Argument(const std::string& name, Kind kind, const Type& type, int ndims) |
| 31 | + : name(name), kind(kind), type(type), ndims(ndims) {} |
| 32 | + |
| 33 | + explicit Argument(const ir::Buffer& buffer) : name(buffer->name), type(buffer->type()), ndims(buffer->shape.size()) {} |
| 34 | +}; |
| 35 | + |
| 36 | +//! Wrapper for _LoweredFunc_ |
| 37 | +class LoweredFunc : public IrNodeRef { |
| 38 | + public: |
| 39 | + LoweredFunc() = default; |
| 40 | + explicit LoweredFunc(IrNode* n) : IrNodeRef(n) {} |
| 41 | + |
| 42 | + const _LoweredFunc_* operator->() const; |
| 43 | + _LoweredFunc_* operator->(); |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Definition of a lowered function. Note that, it should be functional. |
| 48 | + */ |
| 49 | +struct _LoweredFunc_ : ExprNode<_LoweredFunc_> { |
| 50 | + //! The name of this function. |
| 51 | + std::string name; |
| 52 | + |
| 53 | + //! The Arguments used in the body of the function. |
| 54 | + std::vector<Argument> args; |
| 55 | + |
| 56 | + //! Body of this function. |
| 57 | + Expr body; |
| 58 | + |
| 59 | + static LoweredFunc Make(const std::string& name, const std::vector<Argument>& args, const Expr& body); |
| 60 | + |
| 61 | + static LoweredFunc Make(const std::string& name, const std::vector<Argument>& args, const std::vector<Expr>& body); |
| 62 | + |
| 63 | + std::vector<Expr*> expr_fields() override; |
| 64 | + std::vector<const Expr*> expr_fields() const override; |
| 65 | + |
| 66 | + static const IrNodeTy _node_type_ = IrNodeTy::_LoweredFunc_; |
| 67 | +}; |
| 68 | + |
| 69 | +} // namespace ir |
| 70 | +} // namespace cinn |
0 commit comments