From ac5aef188b9c57a63524f2b97585ac24fa60f85c Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=94=D0=B8=D0=BB=D1=88=D0=BE=D0=B4=D0=B6=D0=BE=D0=BD=20?= =?utf8?q?=D0=9F=D0=BE=D1=88=D1=88=D0=BE=D0=B5=D0=B2/AI=20Tools=20Lab=20/S?= =?utf8?q?RR/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 6 Aug 2019 22:49:39 +0300 Subject: [PATCH] Add default fallback for Visitor::visit (#5889) This will help to escape overriding a lot of visit() with default body: empty body or `throw std::runtime_error("NYI")`: just override this new method additional changes: * added file with all mir operation headers * refined IVisitor Signed-off-by: Dilshodzhon Poshshoev --- compiler/mir/include/mir/OpDefs.h | 53 ++++++++++++++++++++++++++++++++++++++ compiler/mir/include/mir/Visitor.h | 27 +++++++------------ compiler/mir/src/Visitor.cpp | 4 ++- 3 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 compiler/mir/include/mir/OpDefs.h diff --git a/compiler/mir/include/mir/OpDefs.h b/compiler/mir/include/mir/OpDefs.h new file mode 100644 index 0000000..92b5b3b --- /dev/null +++ b/compiler/mir/include/mir/OpDefs.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 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 _MIR_OPDEFS_H_ +#define _MIR_OPDEFS_H_ + +#include "mir/ops/BatchNormOp.h" +#include "mir/ops/BiasAddOp.h" +#include "mir/ops/CappedReluOp.h" +#include "mir/ops/CommonProps.h" +#include "mir/ops/ConcatOp.h" +#include "mir/ops/ConstantOp.h" +#include "mir/ops/Conv2DOp.h" +#include "mir/ops/Deconv2DOp.h" +#include "mir/ops/DepthwiseConv2DOp.h" +#include "mir/ops/DropoutOp.h" +#include "mir/ops/ElementwiseOp.h" +#include "mir/ops/EluOp.h" +#include "mir/ops/FullyConnectedOp.h" +#include "mir/ops/GatherOp.h" +#include "mir/ops/GemmOp.h" +#include "mir/ops/InputOp.h" +#include "mir/ops/LeakyReluOp.h" +#include "mir/ops/OutputOp.h" +#include "mir/ops/PadOp.h" +#include "mir/ops/PoolOp.h" +#include "mir/ops/ReduceOp.h" +#include "mir/ops/ReluOp.h" +#include "mir/ops/ReshapeOp.h" +#include "mir/ops/ResizeOp.h" +#include "mir/ops/ScaleOp.h" +#include "mir/ops/SigmoidOp.h" +#include "mir/ops/SliceOp.h" +#include "mir/ops/SoftmaxOp.h" +#include "mir/ops/SqrtOp.h" +#include "mir/ops/SqueezeOp.h" +#include "mir/ops/TanhOp.h" +#include "mir/ops/TransposeOp.h" + +#endif // _MIR_OPDEFS_H_ diff --git a/compiler/mir/include/mir/Visitor.h b/compiler/mir/include/mir/Visitor.h index aa15f8e..3a4738c 100644 --- a/compiler/mir/include/mir/Visitor.h +++ b/compiler/mir/include/mir/Visitor.h @@ -28,36 +28,27 @@ namespace ops #undef HANDLE_OP } // namespace ops +class Operation; + /** - * @brief Visitor Interface declaration + * @brief Base visitor with empty falback function */ class IVisitor { public: -#define HANDLE_OP(OpType, OpClass) virtual void visit(ops::OpClass &) = 0; +#define HANDLE_OP(OpType, OpClass) virtual void visit(ops::OpClass &); #include "mir/ops/operations.lst.h" #undef HANDLE_OP virtual ~IVisitor() = default; -}; - -/** - * @brief Non Pure Virtual implementation of IVisitor - * It is used to facilitate adding new operations, - * so that we don't have to add more declarations and - * only need to define an implementation of `visit` for a subset of operations in the graph, - * while not doing anything for all others. - */ -class Visitor : public IVisitor -{ -public: -#define HANDLE_OP(OpType, OpClass) virtual void visit(ops::OpClass &) override; -#include "mir/ops/operations.lst.h" -#undef HANDLE_OP - ~Visitor() override = default; +protected: + virtual void visit_fallback(Operation &) {} }; +// TODO Remove this using when all uses of Visitor are eliminated. +using Visitor = IVisitor; + } // namespace mir #endif //_MIR_VISITOR_H_ diff --git a/compiler/mir/src/Visitor.cpp b/compiler/mir/src/Visitor.cpp index 5a9c1b9..7c195bc 100644 --- a/compiler/mir/src/Visitor.cpp +++ b/compiler/mir/src/Visitor.cpp @@ -16,11 +16,13 @@ #include "mir/Visitor.h" +#include "mir/OpDefs.h" + namespace mir { #define HANDLE_OP(OpType, OpClass) \ - void Visitor::visit(ops::OpClass &) {} + void IVisitor::visit(ops::OpClass &op) { visit_fallback(op); } #include "mir/ops/operations.lst.h" #undef HANDLE_OP -- 2.7.4