From cee952c3e427107445c94d14d5e16e3e8f7d8bcf Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 11 Oct 2018 12:59:29 +0900 Subject: [PATCH] [enco] Introduce PadInstrBuilder (#1825) This commit extracts PadF instruction generation routine as PadInstrBuilder class, and simplifies AvgPoolRewritePass using it. Signed-off-by: Jonghyun Park --- .../enco/core/src/Transforms/AvgPoolLowering.cpp | 55 +++++++++++++++++----- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/contrib/enco/core/src/Transforms/AvgPoolLowering.cpp b/contrib/enco/core/src/Transforms/AvgPoolLowering.cpp index afbad10..cb20aeb 100644 --- a/contrib/enco/core/src/Transforms/AvgPoolLowering.cpp +++ b/contrib/enco/core/src/Transforms/AvgPoolLowering.cpp @@ -74,6 +74,48 @@ ShapeTransform::Pad shape_xform(const coco::Padding2D *pad) { return ShapeTransf } // namespace +namespace +{ + +class PadInstrBuilder final +{ +public: + PadInstrBuilder(const coco::Padding2D *pad) : _pad{pad} + { + // DO NOTHING + } + +public: + coco::Instr *build(coco::FeatureObject *ifm_obj, coco::FeatureObject *ofm_obj) const + { + assert(ifm_obj->module() == ofm_obj->module()); + auto m = ifm_obj->module(); + assert(m != nullptr); + + auto pad_op = m->entity()->op()->create(); + + pad_op->pad()->top(_pad->top()); + pad_op->pad()->bottom(_pad->bottom()); + pad_op->pad()->left(_pad->left()); + pad_op->pad()->right(_pad->right()); + + auto pad_instr = m->entity()->instr()->create(); + + pad_instr->ifm(ifm_obj); + pad_instr->ofm(ofm_obj); + pad_instr->op(pad_op); + + return pad_instr; + } + +private: + const coco::Padding2D *_pad; +}; + +PadInstrBuilder pad_instr_builder(const coco::Padding2D *pad) { return PadInstrBuilder{pad}; } + +} // namespace + namespace enco { @@ -111,18 +153,7 @@ void AvgPoolRewritePass::runOnModule(coco::Module *m) const pad_obj->bag(pad_bag); pad_obj->layout(coco::FeatureLayouts::BHWC::create(pad_shape)); - auto pad_op = m->entity()->op()->create(); - - pad_op->pad()->top(avgpool->pad()->top()); - pad_op->pad()->bottom(avgpool->pad()->bottom()); - pad_op->pad()->left(avgpool->pad()->left()); - pad_op->pad()->right(avgpool->pad()->right()); - - auto pad_instr = m->entity()->instr()->create(); - - pad_instr->ifm(ifm_obj); - pad_instr->ofm(pad_obj); - pad_instr->op(pad_op); + auto pad_instr = pad_instr_builder(avgpool->pad()).build(ifm_obj, pad_obj); // Insert PadF before AvgPool2D pad_instr->insertBefore(unit); -- 2.7.4