Skip to content

Commit

Permalink
JIT: Unspecialize handle types after arithmetic ops in VN (#70452)
Browse files Browse the repository at this point in the history
VN was retaining the precise handle types when applying arithmetic
operations to handles. This meant that we could not rely on handles of
types like GTF_ICON_METHOD_HDL actually containing an embedded method
handle after constant propagation.

This change generalizes the handle type to GTF_ICON_CONST_PTR or
GTF_ICON_GLOBAL_PTR whenever VN does anything that semantically
"unassociates" the icon from the specialized handle type.
  • Loading branch information
jakobbotsch committed Jun 9, 2022
1 parent 3d74b00 commit 9080239
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2869,13 +2869,15 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu
{
int resVal = EvalOp<int>(func, ConstantValue<int>(arg0VN));
// Unary op on a handle results in a handle.
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForIntCon(resVal);
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetFoldedArithOpResultHandleFlags(arg0VN))
: VNForIntCon(resVal);
}
case TYP_LONG:
{
INT64 resVal = EvalOp<INT64>(func, ConstantValue<INT64>(arg0VN));
// Unary op on a handle results in a handle.
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForLongCon(resVal);
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetFoldedArithOpResultHandleFlags(arg0VN))
: VNForLongCon(resVal);
}
case TYP_FLOAT:
{
Expand Down Expand Up @@ -3106,7 +3108,7 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu
ValueNum handleVN = IsVNHandle(arg0VN) ? arg0VN : IsVNHandle(arg1VN) ? arg1VN : NoVN;
if (handleVN != NoVN)
{
result = VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)); // Use VN for Handle
result = VNForHandle(ssize_t(resultVal), GetFoldedArithOpResultHandleFlags(handleVN));
}
else
{
Expand All @@ -3132,7 +3134,7 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu

if (handleVN != NoVN)
{
result = VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)); // Use VN for Handle
result = VNForHandle(ssize_t(resultVal), GetFoldedArithOpResultHandleFlags(handleVN));
}
else
{
Expand Down Expand Up @@ -5119,6 +5121,37 @@ GenTreeFlags ValueNumStore::GetHandleFlags(ValueNum vn)
return handle->m_flags;
}

GenTreeFlags ValueNumStore::GetFoldedArithOpResultHandleFlags(ValueNum vn)
{
GenTreeFlags flags = GetHandleFlags(vn);
assert((flags & GTF_ICON_HDL_MASK) == flags);

switch (flags)
{
case GTF_ICON_SCOPE_HDL:
case GTF_ICON_CLASS_HDL:
case GTF_ICON_METHOD_HDL:
case GTF_ICON_FIELD_HDL:
case GTF_ICON_TOKEN_HDL:
case GTF_ICON_STR_HDL:
case GTF_ICON_CONST_PTR:
case GTF_ICON_VARG_HDL:
case GTF_ICON_PINVKI_HDL:
case GTF_ICON_FTN_ADDR:
case GTF_ICON_CIDMID_HDL:
case GTF_ICON_TLS_HDL:
case GTF_ICON_STATIC_BOX_PTR:
return GTF_ICON_CONST_PTR;
case GTF_ICON_STATIC_HDL:
case GTF_ICON_GLOBAL_PTR:
case GTF_ICON_BBC_PTR:
return GTF_ICON_GLOBAL_PTR;
default:
assert(!"Unexpected handle type");
return flags;
}
}

bool ValueNumStore::IsVNHandle(ValueNum vn)
{
if (vn == NoVN)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/valuenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ class ValueNumStore
// returns true iff vn is known to be a constant int32 that is > 0
bool IsVNPositiveInt32Constant(ValueNum vn);

GenTreeFlags GetFoldedArithOpResultHandleFlags(ValueNum vn);

public:
// Initializes any static variables of ValueNumStore.
static void InitValueNumStoreStatics();
Expand Down

0 comments on commit 9080239

Please sign in to comment.