-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Framework for copy-on-write visitors (WIP)
Signed-off-by: Chris Dodd <[email protected]>
- Loading branch information
Showing
9 changed files
with
482 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
Oops, something went wrong.