From ce5e9a3997944433d05c1a7c57e524d552fd9388 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 7 Aug 2019 17:22:09 +0900 Subject: [PATCH] [loco] Introduce GraphBuilder helper class (#6308) * [loco] Introduce GraphBuilder helper class This commit introduces GraphBuilder helper class which faciliates testcase implementation. Signed-off-by: Jonghyun Park * Update comment --- compiler/loco/src/Service/GraphBuilder.h | 114 ++++++++++++++++++++++++ compiler/loco/src/Service/GraphBuilder.test.cpp | 47 ++++++++++ 2 files changed, 161 insertions(+) create mode 100644 compiler/loco/src/Service/GraphBuilder.h create mode 100644 compiler/loco/src/Service/GraphBuilder.test.cpp diff --git a/compiler/loco/src/Service/GraphBuilder.h b/compiler/loco/src/Service/GraphBuilder.h new file mode 100644 index 0000000..49cd58f --- /dev/null +++ b/compiler/loco/src/Service/GraphBuilder.h @@ -0,0 +1,114 @@ +/* + * 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 __GRAPH_BUILDER_H__ +#define __GRAPH_BUILDER_H__ + +// loco-internal headers +#include "loco/IR/Graph.h" + +// repo-internal headers +#include + +// C++ standard headers +#include + +// +// This file includes a stack-based loco graph builder +// +// HOW TO USE +// +// loco::Graph *g = ... +// auto builder = make_graph_builder(g); +// +// builder->push(...); +// + +class GraphBuilder final +{ +public: + class Stack final + { + public: + Stack() = default; + + public: + loco::Node *top(void) const { return _content.top(); } + + public: + loco::Node *pop(void) + { + auto ret = top(); + _content.pop(); + return ret; + } + + public: + void push(loco::Node *node) { _content.push(node); } + + private: + std::stack _content; + }; + + class Context final + { + public: + Context(loco::Graph *graph) : _graph{graph} + { + // DO NOTHING + } + + public: + loco::Graph *graph(void) { return _graph; } + Stack *stack(void) { return &_stack; } + + private: + loco::Graph *_graph = nullptr; + Stack _stack; + }; + +public: + GraphBuilder(loco::Graph *graph) : _context{graph} + { + // DO NOTHING + } + +public: + // "Layer" is in theory a subgraph builder. + template + auto push(Args &&... args) + -> decltype(static_cast(nullptr)->operator()(static_cast(nullptr))) + { + Layer layer{std::forward(args)...}; + return layer(ctx()); + } + +public: + loco::Node *pop(void) { return ctx()->stack()->pop(); } + +private: + Context *ctx(void) { return &_context; } + +private: + Context _context; +}; + +static inline std::unique_ptr make_graph_builder(loco::Graph *g) +{ + return stdex::make_unique(g); +} + +#endif // __GRAPH_BUILDER_H__ diff --git a/compiler/loco/src/Service/GraphBuilder.test.cpp b/compiler/loco/src/Service/GraphBuilder.test.cpp new file mode 100644 index 0000000..7b2ea51 --- /dev/null +++ b/compiler/loco/src/Service/GraphBuilder.test.cpp @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#include "GraphBuilder.h" + +#include "loco/IR/Nodes.h" +#include "loco/IR/CanonicalDialect.h" +#include "loco/IR/CanonicalOpcode.h" + +#include + +TEST(GraphBuilderTest, Usecase_000) +{ + struct SampleLayer final + { + loco::Node *operator()(GraphBuilder::Context *ctx) + { + auto node = ctx->graph()->nodes()->create(); + ctx->stack()->push(node); + return node; + } + }; + + auto g = loco::make_graph(); + auto gbuilder = make_graph_builder(g.get()); + + gbuilder->push(); + + auto node = gbuilder->pop(); + + ASSERT_EQ(g->nodes()->size(), 1); + ASSERT_EQ(node->dialect(), loco::CanonicalDialect::get()); + ASSERT_EQ(node->opnum(), static_cast(loco::CanonicalOpcode::ConstGen)); +} -- 2.7.4