Skip to content

Commit 9080239

Browse files
authored
JIT: Unspecialize handle types after arithmetic ops in VN (#70452)
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.
1 parent 3d74b00 commit 9080239

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/coreclr/jit/valuenum.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,13 +2869,15 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu
28692869
{
28702870
int resVal = EvalOp<int>(func, ConstantValue<int>(arg0VN));
28712871
// Unary op on a handle results in a handle.
2872-
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForIntCon(resVal);
2872+
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetFoldedArithOpResultHandleFlags(arg0VN))
2873+
: VNForIntCon(resVal);
28732874
}
28742875
case TYP_LONG:
28752876
{
28762877
INT64 resVal = EvalOp<INT64>(func, ConstantValue<INT64>(arg0VN));
28772878
// Unary op on a handle results in a handle.
2878-
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetHandleFlags(arg0VN)) : VNForLongCon(resVal);
2879+
return IsVNHandle(arg0VN) ? VNForHandle(ssize_t(resVal), GetFoldedArithOpResultHandleFlags(arg0VN))
2880+
: VNForLongCon(resVal);
28792881
}
28802882
case TYP_FLOAT:
28812883
{
@@ -3106,7 +3108,7 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu
31063108
ValueNum handleVN = IsVNHandle(arg0VN) ? arg0VN : IsVNHandle(arg1VN) ? arg1VN : NoVN;
31073109
if (handleVN != NoVN)
31083110
{
3109-
result = VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)); // Use VN for Handle
3111+
result = VNForHandle(ssize_t(resultVal), GetFoldedArithOpResultHandleFlags(handleVN));
31103112
}
31113113
else
31123114
{
@@ -3132,7 +3134,7 @@ ValueNum ValueNumStore::EvalFuncForConstantArgs(var_types typ, VNFunc func, Valu
31323134

31333135
if (handleVN != NoVN)
31343136
{
3135-
result = VNForHandle(ssize_t(resultVal), GetHandleFlags(handleVN)); // Use VN for Handle
3137+
result = VNForHandle(ssize_t(resultVal), GetFoldedArithOpResultHandleFlags(handleVN));
31363138
}
31373139
else
31383140
{
@@ -5119,6 +5121,37 @@ GenTreeFlags ValueNumStore::GetHandleFlags(ValueNum vn)
51195121
return handle->m_flags;
51205122
}
51215123

5124+
GenTreeFlags ValueNumStore::GetFoldedArithOpResultHandleFlags(ValueNum vn)
5125+
{
5126+
GenTreeFlags flags = GetHandleFlags(vn);
5127+
assert((flags & GTF_ICON_HDL_MASK) == flags);
5128+
5129+
switch (flags)
5130+
{
5131+
case GTF_ICON_SCOPE_HDL:
5132+
case GTF_ICON_CLASS_HDL:
5133+
case GTF_ICON_METHOD_HDL:
5134+
case GTF_ICON_FIELD_HDL:
5135+
case GTF_ICON_TOKEN_HDL:
5136+
case GTF_ICON_STR_HDL:
5137+
case GTF_ICON_CONST_PTR:
5138+
case GTF_ICON_VARG_HDL:
5139+
case GTF_ICON_PINVKI_HDL:
5140+
case GTF_ICON_FTN_ADDR:
5141+
case GTF_ICON_CIDMID_HDL:
5142+
case GTF_ICON_TLS_HDL:
5143+
case GTF_ICON_STATIC_BOX_PTR:
5144+
return GTF_ICON_CONST_PTR;
5145+
case GTF_ICON_STATIC_HDL:
5146+
case GTF_ICON_GLOBAL_PTR:
5147+
case GTF_ICON_BBC_PTR:
5148+
return GTF_ICON_GLOBAL_PTR;
5149+
default:
5150+
assert(!"Unexpected handle type");
5151+
return flags;
5152+
}
5153+
}
5154+
51225155
bool ValueNumStore::IsVNHandle(ValueNum vn)
51235156
{
51245157
if (vn == NoVN)

src/coreclr/jit/valuenum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ class ValueNumStore
379379
// returns true iff vn is known to be a constant int32 that is > 0
380380
bool IsVNPositiveInt32Constant(ValueNum vn);
381381

382+
GenTreeFlags GetFoldedArithOpResultHandleFlags(ValueNum vn);
383+
382384
public:
383385
// Initializes any static variables of ValueNumStore.
384386
static void InitValueNumStoreStatics();

0 commit comments

Comments
 (0)