Skip to content

Commit

Permalink
Introduce HandleKindDataIsInvariant helper (dotnet#99744)
Browse files Browse the repository at this point in the history
* Introduce `HandleKindDataIsInvariant` helper

Small helper to identify if a handle kind is invariant.

* Update src/coreclr/jit/gentree.cpp

Co-authored-by: Filip Navara <[email protected]>

---------

Co-authored-by: Filip Navara <[email protected]>
  • Loading branch information
BruceForstall and filipnavara authored Mar 14, 2024
1 parent 33d737a commit e8118ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
13 changes: 5 additions & 8 deletions src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3411,20 +3411,17 @@ void Compiler::fgDebugCheckFlags(GenTree* tree, BasicBlock* block)
GenTreeFlags handleKind = op1->GetIconHandleFlag();

// Some of these aren't handles to invariant data...
if ((handleKind == GTF_ICON_STATIC_HDL) || // Pointer to a mutable class Static variable
(handleKind == GTF_ICON_BBC_PTR) || // Pointer to a mutable basic block count value
(handleKind == GTF_ICON_FTN_ADDR) || // Pointer to a potentially mutable VM slot
(handleKind == GTF_ICON_GLOBAL_PTR)) // Pointer to mutable data from the VM state
if (GenTree::HandleKindDataIsInvariant(handleKind) && (handleKind != GTF_ICON_FTN_ADDR))
{
expectedFlags |= GTF_IND_INVARIANT;
}
else
{
// For statics, we expect the GTF_GLOB_REF to be set. However, we currently
// fail to set it in a number of situations, and so this check is disabled.
// TODO: enable checking of GTF_GLOB_REF.
// expectedFlags |= GTF_GLOB_REF;
}
else // All the other handle indirections are considered invariant
{
expectedFlags |= GTF_IND_INVARIANT;
}

// Currently we expect all indirections with constant addresses to be nonfaulting.
expectedFlags |= GTF_IND_NONFAULTING;
Expand Down
29 changes: 24 additions & 5 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7726,8 +7726,8 @@ GenTreeFlags Compiler::gtTokenToIconFlags(unsigned token)
// Returns a GT_IND node representing value at the address provided by 'addr'
//
// Notes:
// The GT_IND node is marked as non-faulting
// If the indType is GT_REF we also mark the indNode as GTF_GLOB_REF
// The GT_IND node is marked as non-faulting.
// If the indirection is not invariant, we also mark the indNode as GTF_GLOB_REF.
//
GenTree* Compiler::gtNewIndOfIconHandleNode(var_types indType, size_t addr, GenTreeFlags iconFlags, bool isInvariant)
{
Expand All @@ -7736,9 +7736,7 @@ GenTree* Compiler::gtNewIndOfIconHandleNode(var_types indType, size_t addr, GenT

if (isInvariant)
{
assert(iconFlags != GTF_ICON_STATIC_HDL); // Pointer to a mutable class Static variable
assert(iconFlags != GTF_ICON_BBC_PTR); // Pointer to a mutable basic block count value
assert(iconFlags != GTF_ICON_GLOBAL_PTR); // Pointer to mutable data from the VM state
assert(GenTree::HandleKindDataIsInvariant(iconFlags));

// This indirection also is invariant.
indirFlags |= GTF_IND_INVARIANT;
Expand Down Expand Up @@ -10814,6 +10812,27 @@ void GenTree::SetIndirExceptionFlags(Compiler* comp)
}
}

//------------------------------------------------------------------------------
// HandleKindDataIsInvariant: Returns true if the data referred to by a handle
// address is guaranteed to be invariant. Note that GTF_ICON_FTN_ADDR handles may
// or may not point to invariant data.
//
// Arguments:
// flags - GenTree flags for handle.
//
/* static */
bool GenTree::HandleKindDataIsInvariant(GenTreeFlags flags)
{
GenTreeFlags handleKind = flags & GTF_ICON_HDL_MASK;
assert(handleKind != GTF_EMPTY);

// All handle types are assumed invariant except those specifically listed here.

return (handleKind != GTF_ICON_STATIC_HDL) && // Pointer to a mutable class Static variable
(handleKind != GTF_ICON_BBC_PTR) && // Pointer to a mutable basic block count value
(handleKind != GTF_ICON_GLOBAL_PTR); // Pointer to mutable data from the VM state
}

#ifdef DEBUG

/* static */ int GenTree::gtDispFlags(GenTreeFlags flags, GenTreeDebugFlags debugFlags)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,7 @@ struct GenTree
}

#if defined(TARGET_XARCH) && defined(FEATURE_HW_INTRINSICS)

bool IsEmbMaskOp()
{
return OperIsHWIntrinsic() && ((gtFlags & GTF_HW_EM_OP) != 0);
Expand All @@ -2263,6 +2264,8 @@ struct GenTree

#endif // TARGET_XARCH && FEATURE_HW_INTRINSICS

static bool HandleKindDataIsInvariant(GenTreeFlags flags);

bool IsCall() const
{
return OperGet() == GT_CALL;
Expand Down

0 comments on commit e8118ba

Please sign in to comment.