From b7f396d3368f5ea1fe2462d300660da2dd25e273 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: Mon, 1 Oct 2018 19:31:58 +0900 Subject: [PATCH] [enco] Introduce IdenticalObjectReduction pass (#1694) This commit implements Identical Object Reduction optimization as an enco pass. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/Backend.cpp | 2 + .../src/Transforms/IdenticalObjectReduction.cpp | 71 ++++++++++++++++++++++ .../core/src/Transforms/IdenticalObjectReduction.h | 58 ++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp create mode 100644 contrib/enco/core/src/Transforms/IdenticalObjectReduction.h diff --git a/contrib/enco/core/src/Backend.cpp b/contrib/enco/core/src/Backend.cpp index 617650e..645b048 100644 --- a/contrib/enco/core/src/Backend.cpp +++ b/contrib/enco/core/src/Backend.cpp @@ -24,6 +24,7 @@ #include "Transforms/Rewrite.h" #include "Transforms/Normalize.h" #include "Transforms/IndirectCopyElimination.h" +#include "Transforms/IdenticalObjectReduction.h" #include "Transforms/DeadObjectElimination.h" #include "Transforms/Optimizations.h" #include "Transforms/Split.h" @@ -103,6 +104,7 @@ void BackendImpl::compile(coco::Module *m, coco::Data *d) normalize.runOnCode(&code); eliminate_indirect_copy(&code); + reduce_identical_object(&code); // Eliminate dead object // diff --git a/contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp new file mode 100644 index 0000000..a6cd8ac --- /dev/null +++ b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "IdenticalObjectReduction.h" +#include "IRUtils.h" + +#include + +namespace enco +{ + +void reduce_identical_object(enco::Code *code) +{ + auto m = code->module(); + + std::set detached; + + for (uint32_t n = 0; n < m->entity()->instr()->size(); ++n) + { + auto ins = m->entity()->instr()->at(n); + assert(ins != nullptr); + assert(ins->parent() != nullptr); + + auto copy = ins->asCopy(); + + if (copy == nullptr) + { + // Skip if instruction is not a copy + continue; + } + + // TODO Support non-Feature Objects + auto ifm = copy->from()->asFeature(); + auto ofm = copy->into()->asFeature(); + + if (ifm->layout()->id() != ofm->layout()->id()) + { + continue; + } + + if (ifm->layout()->id() != coco::FeatureLayouts::BHWC::uid()) + { + continue; + } + + subst(copy->into(), copy->from()); + + copy->detach(); + detached.insert(copy); + } + + for (auto copy : detached) + { + m->entity()->instr()->destroy(copy); + } +} + +} // namespace enco diff --git a/contrib/enco/core/src/Transforms/IdenticalObjectReduction.h b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.h new file mode 100644 index 0000000..bbf624f --- /dev/null +++ b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__ +#define __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__ + +#include "Code.h" + +namespace enco +{ + +/** + * @brief Reduce identically copied objects as its original object + * + * >>> BEFORE <<< + * %bag_0 = Bag(size: N) + * %bag_1 = Bag(size: N) + * + * %obj_0 = Feature(layout: BHWC) at %bag_0 + * %obj_1 = Feature(layout: BHWC) at %bag_1 + * + * copy(from: %obj_0, into: %obj_1) + * ... + * Use(%obj_0) + * Use(%obj_1) + * ... + * + * >>> AFTER <<< + * %bag_0 = Bag(size: N) + * %bag_1 = Bag(size: N) + * + * %obj_0 = Feature(layout: BHWC) at %bag_0 + * %obj_1 = Feature(layout: BHWC) at %bag_1 + * + * copy(from: %obj_0, into: %obj_1) + * ... + * Use(%obj_0) + * Use(%obj_0) <- %obj_1 is replaced + * ... + */ +void reduce_identical_object(enco::Code *code); + +} // namespace enco + +#endif // __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__ -- 2.7.4