Skip to content

Commit

Permalink
Framework for copy-on-write visitors (WIP)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Dodd <[email protected]>
  • Loading branch information
ChrisDodd committed Nov 11, 2024
1 parent 6f7353f commit ba6a9f8
Show file tree
Hide file tree
Showing 9 changed files with 482 additions and 21 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ endif()

project (P4C)

# set (CMAKE_CXX_EXTENSIONS OFF) # prefer using -std=c++17 rather than -std=gnu++17
set (CMAKE_CXX_STANDARD 17)
# set (CMAKE_CXX_EXTENSIONS OFF) # prefer using -std=c++20 rather than -std=gnu++20
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED ON)

set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
Expand Down
14 changes: 7 additions & 7 deletions frontends/p4/removeReturns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.

namespace P4 {

bool MoveToElseAfterBranch::preorder(IR::BlockStatement *block) {
bool MoveToElseAfterBranch::preorder(IR::COWptr<IR::BlockStatement> block) {
movedToIfBranch = false;
for (auto it = block->components.begin(); it != block->components.end();) {
if (movedToIfBranch)
Expand All @@ -32,7 +32,7 @@ bool MoveToElseAfterBranch::preorder(IR::BlockStatement *block) {
return false;
}

bool MoveToElseAfterBranch::moveFromParentTo(const IR::Statement *&child) {
template<class T> bool MoveToElseAfterBranch::moveFromParentTo(T &child) {
auto parent = getParent<IR::BlockStatement>();
size_t next = getContext()->child_index + 1;
if (!parent || next >= parent->components.size()) {
Expand All @@ -43,9 +43,9 @@ bool MoveToElseAfterBranch::moveFromParentTo(const IR::Statement *&child) {
IR::BlockStatement *modified = nullptr;
if (!child)
modified = new IR::BlockStatement;
else if (auto *t = child->to<IR::BlockStatement>())
else if (auto *t = child->template to<IR::BlockStatement>())
modified = t->clone();
else if (child->is<IR::EmptyStatement>())
else if (child->template is<IR::EmptyStatement>())
modified = new IR::BlockStatement;
else
modified = new IR::BlockStatement({child});
Expand All @@ -55,7 +55,7 @@ bool MoveToElseAfterBranch::moveFromParentTo(const IR::Statement *&child) {
return true;
}

bool MoveToElseAfterBranch::preorder(IR::IfStatement *ifStmt) {
bool MoveToElseAfterBranch::preorder(IR::COWptr<IR::IfStatement> ifStmt) {
hasJumped = false;
bool movedCode = false;
visit(ifStmt->ifTrue, "ifTrue", 1);
Expand All @@ -74,7 +74,7 @@ bool MoveToElseAfterBranch::preorder(IR::IfStatement *ifStmt) {
return false;
}

bool MoveToElseAfterBranch::preorder(IR::SwitchStatement *swch) {
bool MoveToElseAfterBranch::preorder(IR::COWptr<IR::SwitchStatement> swch) {
// TBD: if there is exactly one case that falls through (all others end with a branch)
// then we could move subsequent code into that case, as it done with 'if'
bool canFallThrough = false;
Expand All @@ -87,7 +87,7 @@ bool MoveToElseAfterBranch::preorder(IR::SwitchStatement *swch) {
return false;
}

void MoveToElseAfterBranch::postorder(IR::LoopStatement *) {
void MoveToElseAfterBranch::postorder(IR::COWptr<IR::LoopStatement>) {
// after a loop body is never unreachable
hasJumped = false;
}
Expand Down
22 changes: 11 additions & 11 deletions frontends/p4/removeReturns.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ introduce a boolean flag and extra tests to remove those branches.
precondition:
switchAddDefault pass has run to ensure switch statements cover all cases
*/
class MoveToElseAfterBranch : public Modifier {
class MoveToElseAfterBranch : public COWModifier {
/* This pass does not use (inherit from) ControlFlowVisitor, even though it is doing
* control flow analysis, as it turns out to be more efficient to do it directly here
* by overloading the branching constructs (if/switch/loops) and not cloning the visitor,
Expand All @@ -80,22 +80,22 @@ class MoveToElseAfterBranch : public Modifier {
* indicating that it needs to be removed from the BlockStatment */
bool movedToIfBranch = false;

bool preorder(IR::BlockStatement *) override;
bool moveFromParentTo(const IR::Statement *&child);
bool preorder(IR::IfStatement *) override;
bool preorder(IR::SwitchStatement *) override;
void postorder(IR::LoopStatement *) override;
bool preorder(IR::COWptr<IR::BlockStatement>) override;
template<class T> bool moveFromParentTo(T &child);
bool preorder(IR::COWptr<IR::IfStatement>) override;
bool preorder(IR::COWptr<IR::SwitchStatement>) override;
void postorder(IR::COWptr<IR::LoopStatement>) override;
bool branch() {
hasJumped = true;
// no need to visit children
return false;
}
bool preorder(IR::BreakStatement *) override { return branch(); }
bool preorder(IR::ContinueStatement *) override { return branch(); }
bool preorder(IR::ExitStatement *) override { return branch(); }
bool preorder(IR::ReturnStatement *) override { return branch(); }
bool preorder(IR::COWptr<IR::BreakStatement>) override { return branch(); }
bool preorder(IR::COWptr<IR::ContinueStatement>) override { return branch(); }
bool preorder(IR::COWptr<IR::ExitStatement>) override { return branch(); }
bool preorder(IR::COWptr<IR::ReturnStatement>) override { return branch(); }
// Only visit statements, skip all expressions
bool preorder(IR::Expression *) override { return false; }
bool preorder(IR::COWptr<IR::Expression>) override { return false; }

public:
MoveToElseAfterBranch() {}
Expand Down
256 changes: 256 additions & 0 deletions ir/copy_on_write_inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/*
Copyright 2024 NVIDIA CORPORATION.
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 IR_COPY_ON_WRITE_INL_H_
#define IR_COPY_ON_WRITE_INL_H_

/* template methods declared in "copy_on_write_ptr.h" that can't be defined there
* due to order-of-declaration issues. */

namespace P4::IR {

template <class T, typename U, U T::*field>
const U &COWfieldref<T, U, field>::get() const {
if (info->clone) return info->clone->checkedTo<T>()->*field;
return info->orig->checkedTo<T>()->*field;
}

template <class T, typename U, U T::*field>
U &COWfieldref<T, U, field>::modify() const {
return info->mk_clone()->checkedTo<T>()->*field;
}

template <class T, typename U, U T::*field>
const U &COWfieldref<T, U, field>::operator=(const U &val) const {
if (!info->clone) {
if (info->orig->checkedTo<T>()->*field == val) return val;
info->clone = info->orig->clone();
}
return info->clone->checkedTo<T>()->*field = val;
}

template <class T, typename U, U T::*field>
const U &COWfieldref<T, U, field>::operator=(U &&val) const {
if (!info->clone) {
if (info->orig->checkedTo<T>()->*field == val) return val;
info->clone = info->orig->clone();
}
return info->clone->checkedTo<T>()->*field = std::move(val);
}

/* specializations for IR::Node pointers */
template <class T, typename U, const U *T::*field>
requires std::derived_from<U, Node>
struct COWfieldref<T, const U *, field> {
COWNode_info *info;

const U * const &get() const;
const U *&modify() const;
operator const U * const&() const { return get(); }
const U *operator=(const U *val) const {
if (!info->clone) {
if (info->orig->checkedTo<T>()->*field == val) return val;
info->clone = info->orig->clone();
}
return info->clone->checkedTo<T>()->*field = val;
}
void set(const U *val) const { *this = val; }
void set(const Node *val) const { *this = val ? val->checkedTo<U>() : (U *)nullptr; }
};

/* specializations for IR::Vector */
template <class T, typename U, Vector<U> T::*field>
struct COWfieldref<T, Vector<U>, field> {
COWNode_info *info;

struct element_ref {
COWNode_info *info;
bool is_const;
union {
typename Vector<U>::const_iterator ci;
typename Vector<U>::iterator ni;
};
element_ref(COWNode_info *inf, typename Vector<U>::const_iterator i)
: info(inf), is_const(true) { ci = i; }
element_ref(COWNode_info *inf, typename Vector<U>::iterator i)
: info(inf), is_const(false) { ni = i; }
void clone_fixup() {
if (is_const) {
// messy problem -- need to clone (iff not yet cloned) and then find the
// corresponding iterator in the clone
auto i = (info->mk_clone()->checkedTo<T>()->*field).begin();
auto &orig_vec = info->orig->checkedTo<T>()->*field;
for (auto oi = orig_vec.begin(); oi != ci; ++oi, ++i)
BUG_CHECK(oi != orig_vec.end(), "Invalid iterator in clone_fixup");
ni = i;
is_const = false;
}
}
const U *get() const {
if (is_const && info->clone) clone_fixup();
return *ci;
}
operator const U *() const {
if (is_const && info->clone) clone_fixup();
return *ci;
}
const U *operator=(const U *val) const {
clone_fixup();
return *ni = val;
}
void set(const U *val) const {
clone_fixup();
*ni = val;
}
void set(const Node *val) const {
set(val ? val->checkedTo<U>() : (U *)nullptr);
}
};

struct iterator {
element_ref ref;
iterator(COWNode_info *inf, typename Vector<U>::const_iterator i) : ref(inf, i) {}
iterator(COWNode_info *inf, typename Vector<U>::iterator i) : ref(inf, i) {}
iterator &operator++() { ++ref.ci; return *this; }
iterator operator++(int) { iterator rv = *this; ++ref.ci; return rv; }
iterator &operator--() { --ref.ci; return *this; }
iterator operator--(int) { iterator rv = *this; --ref.ci; return rv; }
bool operator==(const iterator &i) const { return ref.ci == i.ref.ci; }
bool operator!=(const iterator &i) const { return ref.ci != i.ref.ci; }
element_ref operator *() { return ref; }
};

const Vector<U> &get() const {
if (info->clone) return info->clone->checkedTo<T>()->*field;
return info->orig->checkedTo<T>()->*field;
}
Vector<U> &modify() const { return info->mk_clone()->checkedTo<T>()->*field; }
operator const Vector<U>&() const { return get(); }
Vector<U> &operator=(const Vector<U> &val) const { return modify() = val; }
Vector<U> &operator=(Vector<U> &&val) const { return modify() = std::move(val); }
iterator begin() {
if (info->clone)
return iterator(info, (info->clone->checkedTo<T>()->*field).begin());
else
return iterator(info, (info->orig->checkedTo<T>()->*field).begin());
}
iterator end() {
if (info->clone)
return iterator(info, (info->clone->checkedTo<T>()->*field).begin());
else
return iterator(info, (info->orig->checkedTo<T>()->*field).begin());
}
iterator erase(iterator i) {
i.ref.clone_fixup();
Vector<U> &vec = info->clone->checkedTo<T>()->*field;
return vec.erase(i.ref.ni);
}
// FIXME need to add insert/appeand/prepend/emplace_back specializations
};

/* specializations for IR::IndexedVector */
template <class T, typename U, IndexedVector<U> T::*field>
struct COWfieldref<T, IndexedVector<U>, field> {
COWNode_info *info;

struct element_ref {
COWNode_info *info;
bool is_const;
union {
typename IndexedVector<U>::const_iterator ci;
typename IndexedVector<U>::iterator ni;
};
element_ref(COWNode_info *inf, typename IndexedVector<U>::const_iterator i)
: info(inf), is_const(true) { ci = i; }
element_ref(COWNode_info *inf, typename IndexedVector<U>::iterator i)
: info(inf), is_const(false) { ni = i; }
void clone_fixup() {
if (is_const) {
// messy problem -- need to clone (iff not yet cloned) and then find the
// corresponding iterator in the clone
auto i = (info->mk_clone()->checkedTo<T>()->*field).begin();
auto &orig_vec = info->orig->checkedTo<T>()->*field;
for (auto oi = orig_vec.begin(); oi != ci; ++oi, ++i)
BUG_CHECK(oi != orig_vec.end(), "Invalid iterator in clone_fixup");
ni = i;
is_const = false;
}
}
const U *get() const {
if (is_const && info->clone) clone_fixup();
return *ci;
}
operator const U *() const {
if (is_const && info->clone) clone_fixup();
return *ci;
}
const U *operator=(const U *val) const {
clone_fixup();
return *ni = val;
}
void set(const U *val) const {
clone_fixup();
*ni = val;
}
void set(const Node *val) const {
set(val ? val->checkedTo<U>() : (U *)nullptr);
}
};

struct iterator {
element_ref ref;
iterator(COWNode_info *inf, typename IndexedVector<U>::const_iterator i) : ref(inf, i) {}
iterator(COWNode_info *inf, typename IndexedVector<U>::iterator i) : ref(inf, i) {}
iterator &operator++() { ++ref.ci; return *this; }
iterator operator++(int) { iterator rv = *this; ++ref.ci; return rv; }
iterator &operator--() { --ref.ci; return *this; }
iterator operator--(int) { iterator rv = *this; --ref.ci; return rv; }
bool operator==(const iterator &i) const { return ref.ci == i.ref.ci; }
bool operator!=(const iterator &i) const { return ref.ci != i.ref.ci; }
element_ref operator *() { return ref; }
};

const IndexedVector<U> &get() const {
if (info->clone) return info->clone->checkedTo<T>()->*field;
return info->orig->checkedTo<T>()->*field;
}
IndexedVector<U> &modify() const { return info->mk_clone()->checkedTo<T>()->*field; }
operator const IndexedVector<U>&() const { return get(); }
IndexedVector<U> &operator=(const IndexedVector<U> &val) const { return modify() = val; }
IndexedVector<U> &operator=(IndexedVector<U> &&val) const { return modify() = std::move(val); }
iterator begin() {
if (info->clone)
return iterator(info, (info->clone->checkedTo<T>()->*field).begin());
else
return iterator(info, (info->orig->checkedTo<T>()->*field).begin());
}
iterator end() {
if (info->clone)
return iterator(info, (info->clone->checkedTo<T>()->*field).begin());
else
return iterator(info, (info->orig->checkedTo<T>()->*field).begin());
}
iterator erase(iterator i) {
i.ref.clone_fixup();
IndexedVector<U> &vec = info->clone->checkedTo<T>()->*field;
return vec.erase(i.ref.ni);
}
// FIXME need to add insert/appeand/prepend/emplace_back/removeByName specializations
};

} // namespace P4::IR

#endif /* IR_COPY_ON_WRITE_INL_H_ */
Loading

0 comments on commit ba6a9f8

Please sign in to comment.