Skip to content
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

Enable constant folding in Tier0 #82412

Merged
merged 2 commits into from Feb 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 4 additions & 26 deletions src/coreclr/jit/fgprofile.cpp
Expand Up @@ -1448,34 +1448,12 @@ void EfficientEdgeCountInstrumentor::SplitCriticalEdges()
JITDUMP("Could not find " FMT_BB " -> " FMT_BB " edge to instrument\n", block->bbNum,
target->bbNum);

// If we're optimizing, assume this edge got folded away
//
if (m_comp->opts.IsInstrumentedOptimized())
{
JITDUMP(" -- assuming this edge was folded away by the importer\n");
JITDUMP(" -- assuming this edge was folded away by the importer\n");

// Placate the asserts below
//
instrumentedBlock = source;
edgesIgnored++;
}
// If the source block is a partial compilation patchpoint
// then the edge will have been removed.
// Placate the asserts below
//
else if ((block->bbFlags & BBF_PARTIAL_COMPILATION_PATCHPOINT) ==
BBF_PARTIAL_COMPILATION_PATCHPOINT)
{
JITDUMP(" -- source block is partial compilation patchpoint\n");

// Placate the asserts below
//
instrumentedBlock = source;
edgesIgnored++;
}
else
{
assert(found);
}
instrumentedBlock = source;
edgesIgnored++;
}

// Delete the critical edge probe
Expand Down
20 changes: 13 additions & 7 deletions src/coreclr/jit/gentree.cpp
Expand Up @@ -6085,9 +6085,6 @@ unsigned GenTree::GetScaleIndexShf()

unsigned GenTree::GetScaledIndex()
{
// with (!opts.OptEnabled(CLFLG_CONSTANTFOLD) we can have
// CNS_INT * CNS_INT
//
if (AsOp()->gtOp1->IsCnsIntOrI())
{
return 0;
Expand Down Expand Up @@ -12853,6 +12850,14 @@ GenTree* Compiler::gtFoldExpr(GenTree* tree)
return tree;
}

// NOTE: MinOpts() is always true for Tier0 so we have to check explicit flags instead.
// To be fixed in https://github.com/dotnet/runtime/pull/77465
const bool tier0opts = !opts.compDbgCode && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an explicit Tier0 flag, why not check this?

if (!tier0opts)
{
return tree;
}

if (!(kind & GTK_SMPOP))
{
if (tree->OperIsConditional())
Expand Down Expand Up @@ -12886,9 +12891,7 @@ GenTree* Compiler::gtFoldExpr(GenTree* tree)
return gtFoldExprConst(tree);
}
}
else if ((kind & GTK_BINOP) && op1 && tree->AsOp()->gtOp2 &&
// Don't take out conditionals for debugging
(opts.OptimizationEnabled() || !tree->OperIsCompare()))
else if ((kind & GTK_BINOP) && op1 && tree->AsOp()->gtOp2)
{
GenTree* op2 = tree->AsOp()->gtOp2;

Expand Down Expand Up @@ -14552,7 +14555,10 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
GenTree* op1 = tree->gtGetOp1();
GenTree* op2 = tree->gtGetOp2IfPresent();

if (!opts.OptEnabled(CLFLG_CONSTANTFOLD))
// NOTE: MinOpts() is always true for Tier0 so we have to check explicit flags instead.
// To be fixed in https://github.com/dotnet/runtime/pull/77465
const bool tier0opts = !opts.compDbgCode && !opts.jitFlags->IsSet(JitFlags::JIT_FLAG_MIN_OPT);
if (!tier0opts)
{
return tree;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/importer.cpp
Expand Up @@ -8232,7 +8232,7 @@ void Compiler::impImportBlockCode(BasicBlock* block)
op1->gtFlags |= (GTF_OVERFLOW | GTF_EXCEPT);
}

if (op1->gtGetOp1()->OperIsConst() && opts.OptimizationEnabled())
if (op1->gtGetOp1()->OperIsConst())
{
// Try and fold the introduced cast
op1 = gtFoldExprConst(op1);
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/jit/jit.h
Expand Up @@ -701,7 +701,6 @@ class Histogram
#define CLFLG_QMARK 0x00080
#define CLFLG_TREETRANS 0x00100
#define CLFLG_INLINING 0x00200
#define CLFLG_CONSTANTFOLD 0x00800

#if FEATURE_STRUCTPROMOTE
#define CLFLG_STRUCTPROMOTE 0x00400
Expand All @@ -717,7 +716,7 @@ class Histogram

#define CLFLG_MAXOPT \
(CLFLG_CSE | CLFLG_REGVAR | CLFLG_RNGCHKOPT | CLFLG_DEADASGN | CLFLG_CODEMOTION | CLFLG_QMARK | CLFLG_TREETRANS | \
CLFLG_INLINING | CLFLG_STRUCTPROMOTE | CLFLG_CONSTANTFOLD)
CLFLG_INLINING | CLFLG_STRUCTPROMOTE)

#define CLFLG_MINOPT (CLFLG_TREETRANS)

Expand Down
28 changes: 21 additions & 7 deletions src/coreclr/jit/morph.cpp
Expand Up @@ -8404,6 +8404,11 @@ GenTreeOp* Compiler::fgMorphCommutative(GenTreeOp* tree)
assert(varTypeIsIntegralOrI(tree->TypeGet()));
assert(tree->OperIs(GT_ADD, GT_MUL, GT_OR, GT_AND, GT_XOR));

if (opts.OptimizationDisabled())
{
return nullptr;
}

// op1 can be GT_COMMA, in this case we're going to fold
// "(op (COMMA(... (op X C1))) C2)" to "(COMMA(... (op X C3)))"
GenTree* op1 = tree->gtGetOp1()->gtEffectiveVal(true);
Expand Down Expand Up @@ -8836,7 +8841,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
// then don't morph to a helper call - it can be done faster inline using idiv.

noway_assert(op2);
if ((typ == TYP_LONG) && opts.OptEnabled(CLFLG_CONSTANTFOLD))
if ((typ == TYP_LONG) && opts.OptimizationEnabled())
{
if (op2->OperIs(GT_CNS_NATIVELONG) && op2->AsIntConCommon()->LngValue() >= 2 &&
op2->AsIntConCommon()->LngValue() <= 0x3fffffff)
Expand Down Expand Up @@ -8961,7 +8966,10 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA

GenTree* oldTree = tree;

tree = gtFoldExpr(tree);
if (opts.OptimizationEnabled())
{
tree = gtFoldExpr(tree);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the gtFold... seems like we should have a consistent pattern we use -- either the caller or the callee should check if optimizations are enabled, but not both?

Maybe this is covered in your opt levels PR?

}

// Were we able to fold it ?
// Note that gtFoldExpr may return a non-leaf even if successful
Expand Down Expand Up @@ -9398,8 +9406,11 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
qmarkOp2 = oldTree->AsOp()->gtOp2->AsOp()->gtOp2;
}

// Try to fold it, maybe we get lucky,
tree = gtFoldExpr(tree);
if (opts.OptimizationEnabled())
{
// Try to fold it, maybe we get lucky,
tree = gtFoldExpr(tree);
}

if (oldTree != tree)
{
Expand Down Expand Up @@ -9769,7 +9780,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
}

/* Any constant cases should have been folded earlier */
noway_assert(!op1->OperIsConst() || !opts.OptEnabled(CLFLG_CONSTANTFOLD) || optValnumCSE_phase);
noway_assert(!op1->OperIsConst() || opts.OptimizationDisabled() || optValnumCSE_phase);
break;

case GT_CKFINITE:
Expand Down Expand Up @@ -12769,8 +12780,11 @@ GenTree* Compiler::fgMorphTree(GenTree* tree, MorphAddrContext* mac)
tree->gtFlags |= tree->AsConditional()->gtOp1->gtFlags & GTF_ALL_EFFECT;
tree->gtFlags |= tree->AsConditional()->gtOp2->gtFlags & GTF_ALL_EFFECT;

// Try to fold away any constants etc.
tree = gtFoldExpr(tree);
if (opts.OptimizationEnabled())
{
// Try to fold away any constants etc.
tree = gtFoldExpr(tree);
}

break;

Expand Down