Skip to content

JIT: Handle remainder accesses more precisely in physical promotion liveness #92651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/coreclr/jit/jitstd/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class vector

// cctors
vector(const vector& vec);
vector(vector&& vec);

template <typename Alt, typename AltAllocator>
explicit vector(const vector<Alt, AltAllocator>& vec);
Expand Down Expand Up @@ -195,6 +196,8 @@ class vector
template <typename Alt, typename AltAllocator>
vector<T, Allocator>& operator=(const vector<Alt, AltAllocator>& vec);

vector& operator=(vector&& vec);

reference operator[](size_type n);
const_reference operator[](size_type n) const;

Expand Down Expand Up @@ -328,6 +331,18 @@ vector<T, Allocator>::vector(const vector<T, Allocator>& vec)
}
}

template <typename T, typename Allocator>
vector<T, Allocator>::vector(vector<T, Allocator>&& vec)
: m_allocator(vec.m_allocator)
, m_pArray(vec.m_pArray)
, m_nSize(vec.m_nSize)
, m_nCapacity(vec.m_nCapacity)
{
vec.m_pArray = nullptr;
vec.m_nSize = 0;
vec.m_nCapacity = 0;
}

template <typename T, typename Allocator>
vector<T, Allocator>::~vector()
{
Expand Down Expand Up @@ -578,6 +593,20 @@ vector<T, Allocator>& vector<T, Allocator>::operator=(const vector<T, Allocator>
return *this;
}

template <typename T, typename Allocator>
vector<T, Allocator>& vector<T, Allocator>::operator=(vector<T, Allocator>&& vec)
{
m_allocator = vec.m_allocator;
m_pArray = vec.m_pArray;
m_nSize = vec.m_nSize;
m_nCapacity = vec.m_nCapacity;

vec.m_pArray = nullptr;
vec.m_nSize = 0;
vec.m_nCapacity = 0;

return *this;
}

template <typename T, typename Allocator>
typename vector<T, Allocator>::reference vector<T, Allocator>::operator[](size_type n)
Expand Down
76 changes: 70 additions & 6 deletions src/coreclr/jit/promotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,19 +1229,18 @@ class LocalsUseVisitor : public GenTreeVisitor<LocalsUseVisitor>
}
#endif

StructSegments unpromotedParts =
m_prom->SignificantSegments(m_compiler->lvaGetDesc(agg->LclNum)->GetLayout());
agg->Unpromoted = m_prom->SignificantSegments(m_compiler->lvaGetDesc(agg->LclNum)->GetLayout());
for (Replacement& rep : reps)
{
unpromotedParts.Subtract(StructSegments::Segment(rep.Offset, rep.Offset + genTypeSize(rep.AccessType)));
agg->Unpromoted.Subtract(StructSegments::Segment(rep.Offset, rep.Offset + genTypeSize(rep.AccessType)));
}

JITDUMP(" Unpromoted remainder: ");
DBEXEC(m_compiler->verbose, unpromotedParts.Dump());
DBEXEC(m_compiler->verbose, agg->Unpromoted.Dump());
JITDUMP("\n\n");

StructSegments::Segment unpromotedSegment;
if (unpromotedParts.CoveringSegment(&unpromotedSegment))
if (agg->Unpromoted.CoveringSegment(&unpromotedSegment))
{
agg->UnpromotedMin = unpromotedSegment.Start;
agg->UnpromotedMax = unpromotedSegment.End;
Expand Down Expand Up @@ -1495,6 +1494,31 @@ bool StructSegments::Segment::IntersectsOrAdjacent(const Segment& other) const
return true;
}

//------------------------------------------------------------------------
// Intersects:
// Check if this segment intersects another segment.
//
// Parameters:
// other - The other segment.
//
// Returns:
// True if so.
//
bool StructSegments::Segment::Intersects(const Segment& other) const
{
if (End <= other.Start)
{
return false;
}

if (other.End <= Start)
{
return false;
}

return true;
}

//------------------------------------------------------------------------
// Contains:
// Check if this segment contains another segment.
Expand Down Expand Up @@ -1586,7 +1610,7 @@ void StructSegments::Subtract(const Segment& segment)
return;
}

assert(m_segments[index].IntersectsOrAdjacent(segment));
assert(m_segments[index].Intersects(segment));

if (m_segments[index].Contains(segment))
{
Expand Down Expand Up @@ -1679,6 +1703,46 @@ bool StructSegments::CoveringSegment(Segment* result)
return true;
}

//------------------------------------------------------------------------
// Intersects:
// Check if a segment intersects with any segment in this segment tree.
//
// Parameters:
// segment - The segment.
//
// Returns:
// True if the input segment intersects with any segment in the tree;
// otherwise false.
//
bool StructSegments::Intersects(const Segment& segment)
{
size_t index = Promotion::BinarySearch<Segment, &Segment::End>(m_segments, segment.Start);
if ((ssize_t)index < 0)
{
index = ~index;
}
else
{
// Start == segment[index].End, which makes it non-interesting.
index++;
}

if (index >= m_segments.size())
{
return false;
}

// Here we know Start < segment[index].End. Do they not intersect at all?
if (m_segments[index].Start >= segment.End)
{
// Does not intersect any segment.
return false;
}

assert(m_segments[index].Intersects(segment));
return true;
}

#ifdef DEBUG
//------------------------------------------------------------------------
// Dump:
Expand Down
8 changes: 6 additions & 2 deletions src/coreclr/jit/promotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class StructSegments
}

bool IntersectsOrAdjacent(const Segment& other) const;
bool Intersects(const Segment& other) const;
bool Contains(const Segment& other) const;
void Merge(const Segment& other);
};
Expand All @@ -68,14 +69,15 @@ class StructSegments
jitstd::vector<Segment> m_segments;

public:
StructSegments(CompAllocator allocator) : m_segments(allocator)
explicit StructSegments(CompAllocator allocator) : m_segments(allocator)
{
}

void Add(const Segment& segment);
void Subtract(const Segment& segment);
bool IsEmpty();
bool CoveringSegment(Segment* result);
bool Intersects(const Segment& segment);

#ifdef DEBUG
void Dump();
Expand All @@ -87,12 +89,14 @@ struct AggregateInfo
{
jitstd::vector<Replacement> Replacements;
unsigned LclNum;
// Unpromoted parts of the struct local.
StructSegments Unpromoted;
// Min offset in the struct local of the unpromoted part.
unsigned UnpromotedMin = 0;
// Max offset in the struct local of the unpromoted part.
unsigned UnpromotedMax = 0;

AggregateInfo(CompAllocator alloc, unsigned lclNum) : Replacements(alloc), LclNum(lclNum)
AggregateInfo(CompAllocator alloc, unsigned lclNum) : Replacements(alloc), LclNum(lclNum), Unpromoted(alloc)
{
}

Expand Down
9 changes: 3 additions & 6 deletions src/coreclr/jit/promotionliveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ void PromotionLiveness::MarkUseDef(GenTreeLclVarCommon* lcl, BitVec& useSet, Bit
}

bool isFullDefOfRemainder = isDef && (agg->UnpromotedMin >= offs) && (agg->UnpromotedMax <= (offs + size));
// TODO-CQ: We could also try to figure out if a use actually touches the remainder, e.g. in some cases
// a struct use may consist only of promoted fields and does not actually use the remainder.
MarkIndex(baseIndex, isUse, isFullDefOfRemainder, useSet, defSet);
bool isUseOfRemainder = isUse && agg->Unpromoted.Intersects(StructSegments::Segment(offs, offs + size));
MarkIndex(baseIndex, isUseOfRemainder, isFullDefOfRemainder, useSet, defSet);
}
}
else
Expand Down Expand Up @@ -609,11 +608,9 @@ void PromotionLiveness::FillInLiveness(BitVec& life, BitVec volatileVars, GenTre
}
else
{
// TODO-CQ: We could also try to figure out if a use actually touches the remainder, e.g. in some cases
// a struct use may consist only of promoted fields and does not actually use the remainder.
BitVecOps::AddElemD(&aggTraits, aggDeaths, 0);

if (isUse)
if (isUse && agg->Unpromoted.Intersects(StructSegments::Segment(offs, offs + size)))
{
BitVecOps::AddElemD(m_bvTraits, life, baseIndex);
}
Expand Down