#include "coco/IR/Object.forward.h"
#include "coco/IR/Instr.forward.h"
#include "coco/IR/Step.forward.h"
+#include "coco/IR/Part.forward.h"
#include "coco/IR/Entity.h"
#include "coco/ADT/PtrLink.h"
struct Op : public Entity
{
friend class Step;
+ friend class Part;
virtual ~Op() = default;
public:
Instr *parent(void) const;
+ // @brief Return a pointer to the parent Op
+ Op *up(void) const;
+
private:
// @brief A link to Instr from Op
//
// WARN Update this field only through Step
Step *_step = nullptr;
+
+ // @brief A link to a parent Op
+ //
+ // WARN Update this field only through Part
+ // NOTE An "Op" CANNOT have a link to a parent Op if it is linked to an "Instr"
+ Part *_part = nullptr;
};
} // namespace coco
--- /dev/null
+/*
+ * 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 __COCO_IR_PART_FORWARD_H__
+#define __COCO_IR_PART_FORWARD_H__
+
+namespace coco
+{
+
+class Part;
+
+} // namespace coco
+
+#endif // __COCO_IR_PART_FORWARD_H__
--- /dev/null
+/*
+ * 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 __COCO_IR_PART_H__
+#define __COCO_IR_PART_H__
+
+#include "coco/IR/Op.forward.h"
+
+namespace coco
+{
+
+/**
+ * @brief A Part represents the edge between a child Op and its parent Op
+ */
+class Part final
+{
+public:
+ Part(Op *parent) : _parent{parent}
+ {
+ // DO NOTHING
+ }
+
+public:
+ ~Part() { child(nullptr); }
+
+public:
+ Op *child(void) const { return _child; }
+ void child(Op *c);
+
+public:
+ Op *parent(void) const { return _parent; }
+
+private:
+ Op *_parent = nullptr;
+ Op *_child = nullptr;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_PART_H__
#include "coco/IR/Op.h"
#include "coco/IR/Step.h"
+#include "coco/IR/Part.h"
#include <cassert>
}
return nullptr;
}
+
+Op *Op::up(void) const
+{
+ if (_part)
+ {
+ assert(_part->parent() != nullptr);
+ return _part->parent();
+ }
+ return nullptr;
+}
} // namespace coco
--- /dev/null
+/*
+ * 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 "coco/IR/Part.h"
+#include "coco/IR/Op.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void Part::child(Op *c)
+{
+ if (_child != nullptr)
+ {
+ assert(_child->_part == this);
+ _child->_part = nullptr;
+ _child = nullptr;
+ }
+
+ assert(_child == nullptr);
+
+ if (c != nullptr)
+ {
+ assert(c->_part == nullptr);
+ assert(c->_step == nullptr);
+ _child = c;
+ _child->_part = this;
+ }
+}
+
+} // namespace coco
--- /dev/null
+/*
+ * 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 "coco/IR/Part.h"
+#include "coco/IR/Op.h"
+
+#include <nncc/foundation/Memory.h>
+
+#include <gtest/gtest.h>
+
+using nncc::foundation::make_unique;
+
+namespace
+{
+namespace mock
+{
+
+struct Op final : public coco::Op
+{
+public:
+ Op() : _arg{this}
+ {
+ // DO NOTHING
+ }
+
+public:
+ std::set<coco::Object *> uses() const override { throw std::runtime_error{"Not supported"}; }
+
+public:
+ ::coco::Op *arg(void) const { return _arg.child(); }
+ void arg(::coco::Op *child) { _arg.child(child); }
+
+private:
+ coco::Part _arg;
+};
+
+} // namespace mock
+} // namespace
+
+TEST(PartTest, destructor)
+{
+ auto parent = make_unique<::mock::Op>();
+ auto child = make_unique<::mock::Op>();
+
+ parent->arg(child.get());
+ ASSERT_EQ(parent->arg(), child.get());
+ ASSERT_EQ(child->up(), parent.get());
+
+ parent.reset();
+
+ // NOTE parent SHOULD unlink itself from child on destruction
+ ASSERT_EQ(child->up(), nullptr);
+}