Skip to content

Commit

Permalink
Pass the pointer of owning object in GCHermesValueBase::set() (#1512)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1512

This variant will be used by write barriers that support large
allocation in following diffs.

Differential Revision: D62196480
  • Loading branch information
lavenzg authored and facebook-github-bot committed Dec 22, 2024
1 parent 04bcd2d commit 03b0589
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
48 changes: 47 additions & 1 deletion include/hermes/VM/HermesValue-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,33 @@ template <typename HVType>
template <typename NeedsBarriers>
GCHermesValueBase<HVType>::GCHermesValueBase(HVType hv, GC &gc) : HVType{hv} {
assert(!hv.isPointer() || hv.getPointer());
if (NeedsBarriers::value)
if (NeedsBarriers::value) {
gc.constructorWriteBarrier(this, hv);
} else {
assert(
!gc.needsWriteBarrierInCtor(this, hv) &&
"Can't skip write barriers for this GCHermesValueBase and target value");
}
}

template <typename HVType>
template <typename NeedsBarriers>
GCHermesValueBase<HVType>::GCHermesValueBase(
HVType hv,
GC &gc,
const GCCell *owningObj)
: HVType{hv} {
assert(!hv.isPointer() || hv.getPointer());
if (NeedsBarriers::value) {
gc.constructorWriteBarrierForLargeObj(owningObj, this, hv);
} else {
// We don't allow skipping write barriers for pointers live in objects that
// support large allocation. This may be revisited if we do have a case that
// needs optimization.
assert(
!hv.isPointer() &&
"Initial value for GCHermesValueBase in a large object must not be pointer when NeedsBarriers is false");
}
}

template <typename HVType>
Expand All @@ -61,6 +86,27 @@ inline void GCHermesValueBase<HVType>::set(HVType hv, GC &gc) {
HVType::setNoBarrier(hv);
}

template <typename HVType>
template <typename NeedsBarriers>
inline void GCHermesValueBase<HVType>::setInLarge(
HVType hv,
GC &gc,
const GCCell *owningObj) {
if (hv.isPointer()) {
HERMES_SLOW_ASSERT(
gc.validPointer(hv.getPointer(gc.getPointerBase())) &&
"Setting an invalid pointer into a GCHermesValue");
}
if constexpr (NeedsBarriers::value) {
gc.writeBarrierForLargeObj(owningObj, this, hv);
} else {
assert(
!gc.needsWriteBarrierForLargeObj(this, hv) &&
"Can't skip write barriers when assigning pointer to GCHermesValueBase in a large object");
}
HVType::setNoBarrier(hv);
}

template <typename HVType>
void GCHermesValueBase<HVType>::setNonPtr(HVType hv, GC &gc) {
assert(!hv.isPointer());
Expand Down
20 changes: 17 additions & 3 deletions include/hermes/VM/HermesValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,21 +520,35 @@ template <typename HVType>
class GCHermesValueBase final : public HVType {
public:
GCHermesValueBase() : HVType(HVType::encodeUndefinedValue()) {}
/// Initialize a GCHermesValue from another HV. Performs a write barrier.
/// Initialize a GCHermesValue from another HV. Performs a write barrier. This
/// must not be used if it lives in an object that supports large allocation.
template <typename NeedsBarriers = std::true_type>
GCHermesValueBase(HVType hv, GC &gc);
/// Initialize a GCHermesValue from another HV. Performs a write barrier using
/// \p owningObj, which owns this GCHermesValue and may support large
/// allocation.
template <typename NeedsBarriers = std::true_type>
GCHermesValueBase(HVType hv, GC &gc, const GCCell *owningObj);
/// Initialize a GCHermesValue from a non-pointer HV. Might perform a write
/// barrier, depending on the GC.
/// NOTE: The last parameter is unused, but acts as an overload selector.
template <typename NeedsBarriers = std::true_type>
GCHermesValueBase(HVType hv, GC &gc, std::nullptr_t);
GCHermesValueBase(const HVType &) = delete;

/// The HermesValue \p hv may be an object pointer. Assign the
/// value, and perform any necessary write barriers.
/// The HermesValue \p hv may be an object pointer. Assign the value, and
/// perform any necessary write barriers. This must not be used if it lives in
/// an object that supports large allocation.
template <typename NeedsBarriers = std::true_type>
inline void set(HVType hv, GC &gc);

/// The HermesValue \p hv may be an object pointer. Assign the value, and
/// perform any necessary write barriers. \p owningObj is the object that
/// contains this GCHermesValueBase, and it may support large allocation.
/// for which the object pointer is needed by writer barriers.
template <typename NeedsBarriers = std::true_type>
inline void setInLarge(HVType hv, GC &gc, const GCCell *owningObj);

/// The HermesValue \p hv must not be an object pointer. Assign the
/// value.
/// Some GCs still need to do a write barrier though, so pass a GC parameter.
Expand Down

0 comments on commit 03b0589

Please sign in to comment.