Skip to content

FixOptRepeat without optAssertionProp_Update fix #100360

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

Closed
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: 28 additions & 1 deletion src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,13 @@ void Compiler::optPrintAssertion(AssertionDsc* curAssertion, AssertionIndex asse
printf("Exact Type MT(0x%p %s)", dspPtr(iconVal),
eeGetClassName((CORINFO_CLASS_HANDLE)iconVal));
}

// We might want to assert:
// assert(curAssertion->op2.HasIconFlag());
// However, if we run CSE with shared constant mode, we may end up with an expression instead
// of the original handle value. If we then use JitOptRepeat to re-build value numbers, we lose
// knowledge that the constant was ever a handle, as the expression creating the original value
// was not (and can't be) assigned a handle flag.
}
else if (curAssertion->op1.kind == O1K_SUBTYPE)
{
Expand Down Expand Up @@ -1876,7 +1883,6 @@ void Compiler::optDebugCheckAssertion(AssertionDsc* assertion)
{
case O1K_EXACT_TYPE:
case O1K_SUBTYPE:
assert(assertion->op2.HasIconFlag());
break;
case O1K_LCLVAR:
assert((lvaGetDesc(assertion->op1.lcl.lclNum)->lvType != TYP_REF) ||
Expand Down Expand Up @@ -5428,6 +5434,27 @@ GenTree* Compiler::optAssertionProp_Update(GenTree* newTree, GenTree* tree, Stat
if (parent != nullptr)
{
parent->ReplaceOperand(useEdge, newTree);

#if 0
// If the parent is a GT_IND and we replaced the child with a handle constant, we might need
// to mark the GT_IND as invariant. This is the same as what gtNewIndOfIconHandleNode() does.
// Review: should some kind of more general morphing take care of this?
// Should this share code with gtNewIndOfIconHandleNode()?

if (parent->OperIs(GT_IND) && newTree->IsIconHandle())
{
GenTreeFlags iconFlags = newTree->GetIconHandleFlag();
if (GenTree::HandleKindDataIsInvariant(iconFlags))
{
parent->gtFlags |= GTF_IND_INVARIANT;
if (iconFlags == GTF_ICON_STR_HDL)
{
// String literals are never null
parent->gtFlags |= GTF_IND_NONNULL;
}
}
}
#endif // 0
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,14 @@ void Compiler::compInitOptions(JitFlags* jitFlags)
}
}

////////////////// TESTING
if (JitConfig.JitEnableOptRepeat() != 0)
{
opts.optRepeat = true;
opts.optRepeatCount = 4;
JITDUMP("\n*************** JitOptRepeat FORCED; repetition count: %d\n\n", opts.optRepeatCount);
}
////////////////// END TESTING
#endif // OPT_CONFIG

#ifdef DEBUG
Expand Down
41 changes: 38 additions & 3 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19038,6 +19038,27 @@ GenTreeLclVarCommon* Compiler::gtCallGetDefinedRetBufLclAddr(GenTreeCall* call)
// Return Value:
// Will set "*pArr" to "nullptr" if this array address is not parseable.
//
// Notes:
// Instead of (or in addition to) parsing the GenTree, maybe we should be parsing the VN
// "trees": if optimization has replaced the index expression with a CSE def, it's harder
// to parse, but the VN tree for the CSE def GT_COMMA has all the same info. For example:
//
// \--* ARR_ADDR byref System.Collections.Hashtable+Bucket[] $80
// \--* ADD byref
// +--* LCL_VAR ref V01 arg1 u:1
// \--* COMMA long
// +--* STORE_LCL_VAR long V21 cse2 d:1
// | \--* ADD long
// | +--* MUL long
// | | +--* CAST long <- uint
// | | | \--* LCL_VAR int V07 loc2 u:2
// | | \--* CNS_INT long 24
// | \--* CNS_INT long 16
// \--* LCL_VAR long V21 cse2 u:1
//
// Here, the COMMA represents the index + offset VN, and we could pull out the index VN
// from the COMMA VN.
//
void GenTreeArrAddr::ParseArrayAddress(Compiler* comp, GenTree** pArr, ValueNum* pInxVN)
{
*pArr = nullptr;
Expand All @@ -19052,13 +19073,23 @@ void GenTreeArrAddr::ParseArrayAddress(Compiler* comp, GenTree** pArr, ValueNum*
}

// OK, new we have to figure out if any part of the "offset" is a constant contribution to the index.
target_ssize_t elemOffset = GetFirstElemOffset();
unsigned elemSizeUn = (GetElemType() == TYP_STRUCT) ? comp->typGetObjLayout(GetElemClassHandle())->GetSize()
target_ssize_t firstElemOffset = GetFirstElemOffset();
assert(firstElemOffset > 0);

// If we didn't parse any offset, or the offset we parsed doesn't make sense, then give up on
// parsing the array address. (This can happen with JitOptRepeat.)
if (offset < firstElemOffset)
{
*pArr = nullptr;
return;
}

unsigned elemSizeUn = (GetElemType() == TYP_STRUCT) ? comp->typGetObjLayout(GetElemClassHandle())->GetSize()
: genTypeSize(GetElemType());

assert(FitsIn<target_ssize_t>(elemSizeUn));
target_ssize_t elemSize = static_cast<target_ssize_t>(elemSizeUn);
target_ssize_t constIndexOffset = offset - elemOffset;
target_ssize_t constIndexOffset = offset - firstElemOffset;

// This should be divisible by the element size...
assert((constIndexOffset % elemSize) == 0);
Expand Down Expand Up @@ -19134,6 +19165,7 @@ void GenTreeArrAddr::ParseArrayAddress(Compiler* comp, GenTree** pArr, ValueNum*
if (tree->TypeIs(TYP_REF))
{
// This must be the array pointer.
assert(*pArr == nullptr);
*pArr = tree;
assert(inputMul == 1); // Can't multiply the array pointer by anything.
}
Expand Down Expand Up @@ -19229,7 +19261,10 @@ void GenTreeArrAddr::ParseArrayAddress(Compiler* comp, GenTree** pArr, ValueNum*
default:
break;
}

// If we didn't return above, must be a contribution to the non-constant part of the index VN.
// We don't get here for GT_CNS_INT, GT_ADD, or GT_SUB, or for GT_MUL by constant, or GT_LSH of
// constant shift. Thus, the generated index VN does not include the parsed constant offset.
ValueNum vn = comp->GetValueNumStore()->VNLiberalNormalValue(tree->gtVNPair);
if (inputMul != 1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/jitconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ CONFIG_STRING(JitEnableInductionVariableOptsRange, W("JitEnableInductionVariable
CONFIG_INTEGER(JitDoSsa, W("JitDoSsa"), 1) // Perform Static Single Assignment (SSA) numbering on the variables
CONFIG_INTEGER(JitDoValueNumber, W("JitDoValueNumber"), 1) // Perform value numbering on method expressions

CONFIG_INTEGER(JitEnableOptRepeat, W("JitEnableOptRepeat"), 0) // If zero, do not allow JitOptRepeat
CONFIG_INTEGER(JitEnableOptRepeat, W("JitEnableOptRepeat"), 1) // If zero, do not allow JitOptRepeat
CONFIG_METHODSET(JitOptRepeat, W("JitOptRepeat")) // Runs optimizer multiple times on specified methods
CONFIG_INTEGER(JitOptRepeatCount, W("JitOptRepeatCount"), 2) // Number of times to repeat opts when repeating
CONFIG_STRING(JitOptRepeatRange, W("JitOptRepeatRange")) // Enable JitOptRepeat based on method hash range
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12193,9 +12193,10 @@ ValueNum ValueNumStore::VNForCast(ValueNum srcVN,
bool hasOverflowCheck) /* = false */
{

if ((castFromType == TYP_I_IMPL) && (castToType == TYP_BYREF) && IsVNHandle(srcVN))
if ((castFromType == TYP_I_IMPL) && varTypeIsGC(castToType) && IsVNConstant(srcVN))
{
// Omit cast for (h)CNS_INT [TYP_I_IMPL -> TYP_BYREF]
// Omit cast for CNS_INT [TYP_I_IMPL -> TYP_BYREF/TYP_REF]
// We can't check `IsVNHandle(srcVN)` because we may have lost handle information with shared const CSEs.
return srcVN;
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/JIT/Directed/debugging/debuginfo/tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<CLRTestEnvironmentVariable Include="DOTNET_JitNoForwardSub" Value="1" />
<CLRTestEnvironmentVariable Include="DOTNET_JitEnableHeadTailMerge" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JitEnableCrossBlockLocalAssertionProp" Value="0" />
<CLRTestEnvironmentVariable Include="DOTNET_JitEnableOptRepeat" Value="0" />

<ProjectReference Include="tests_d.ilproj" Aliases="tests_d" />
<ProjectReference Include="tests_r.ilproj" Aliases="tests_r" />
Expand Down