Add default fallback for Visitor::visit (#5889)
authorДилшоджон Пошшоев/AI Tools Lab /SRR/Engineer/삼성전자 <d.poshshoev@samsung.com>
Tue, 6 Aug 2019 19:49:39 +0000 (22:49 +0300)
committerAlexander Efimov/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Tue, 6 Aug 2019 19:49:39 +0000 (22:49 +0300)
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 <d.poshshoev@samsung.com>
compiler/mir/include/mir/OpDefs.h [new file with mode: 0644]
compiler/mir/include/mir/Visitor.h
compiler/mir/src/Visitor.cpp

diff --git a/compiler/mir/include/mir/OpDefs.h b/compiler/mir/include/mir/OpDefs.h
new file mode 100644 (file)
index 0000000..92b5b3b
--- /dev/null
@@ -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_
index aa15f8e..3a4738c 100644 (file)
@@ -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_
index 5a9c1b9..7c195bc 100644 (file)
 
 #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