From: 박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 Date: Wed, 7 Aug 2019 08:22:09 +0000 (+0900) Subject: [loco] Introduce GraphBuilder helper class (#6308) X-Git-Tag: submit/tizen/20190809.050447~71 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ce5e9a3997944433d05c1a7c57e524d552fd9388;p=platform%2Fcore%2Fml%2Fnnfw.git [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 --- 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)); +}