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

Assign VN for COMMA in gtWrapWithSideEffects #108955

Merged
merged 10 commits into from
Oct 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 3 additions & 59 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3102,18 +3102,7 @@ GenTree* Compiler::optVNBasedFoldConstExpr(BasicBlock* block, GenTree* parent, G

// Were able to optimize.
conValTree->gtVNPair = vnPair;
GenTree* sideEffList = optExtractSideEffListFromConst(tree);
if (sideEffList != nullptr)
{
// Replace as COMMA(side_effects, const value tree);
assert((sideEffList->gtFlags & GTF_SIDE_EFFECT) != 0);
return gtNewOperNode(GT_COMMA, conValTree->TypeGet(), sideEffList, conValTree);
}
else
{
// No side effects, replace as const value tree.
return conValTree;
}
return gtWrapWithSideEffects(conValTree, tree, GTF_SIDE_EFFECT, true);
}
else
{
Expand Down Expand Up @@ -6228,52 +6217,6 @@ struct VNAssertionPropVisitorInfo
}
};

//------------------------------------------------------------------------------
// optExtractSideEffListFromConst
// Extracts side effects from a tree so it can be replaced with a comma
// separated list of side effects + a const tree.
//
// Note:
// The caller expects that the root of the tree has no side effects and it
// won't be extracted. Otherwise the resulting comma tree would be bigger
// than the tree before optimization.
//
// Arguments:
// tree - The tree node with constant value to extrace side-effects from.
//
// Return Value:
// 1. Returns the extracted side-effects from "tree"
// 2. When no side-effects are present, returns null.
//
//
GenTree* Compiler::optExtractSideEffListFromConst(GenTree* tree)
{
assert(vnStore->IsVNConstant(vnStore->VNConservativeNormalValue(tree->gtVNPair)));

GenTree* sideEffList = nullptr;

// If we have side effects, extract them.
if ((tree->gtFlags & GTF_SIDE_EFFECT) != 0)
{
// Do a sanity check to ensure persistent side effects aren't discarded and
// tell gtExtractSideEffList to ignore the root of the tree.
// We are relying here on an invariant that VN will only fold non-throwing expressions.
assert(!gtNodeHasSideEffects(tree, GTF_PERSISTENT_SIDE_EFFECTS));

// Exception side effects may be ignored because the root is known to be a constant
// (e.g. VN may evaluate a DIV/MOD node to a constant and the node may still
// have GTF_EXCEPT set, even if it does not actually throw any exceptions).
bool ignoreRoot = true;

gtExtractSideEffList(tree, &sideEffList, GTF_SIDE_EFFECT, ignoreRoot);

JITDUMP("Extracted side effects from a constant tree [%06u]:\n", tree->gtTreeID);
DISPTREE(sideEffList);
}

return sideEffList;
}

//------------------------------------------------------------------------------
// optVNConstantPropOnJTrue
// Constant propagate on the JTrue node by extracting side effects and moving
Expand Down Expand Up @@ -6330,7 +6273,8 @@ GenTree* Compiler::optVNConstantPropOnJTrue(BasicBlock* block, GenTree* test)
}

// Prepare the tree for replacement so any side effects can be extracted.
GenTree* sideEffList = optExtractSideEffListFromConst(relop);
GenTree* sideEffList = nullptr;
gtExtractSideEffList(relop, &sideEffList, GTF_SIDE_EFFECT, true);

// Transform the relop's operands to be both zeroes.
ValueNum vnZero = vnStore->VNZeroForType(TYP_INT);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8047,7 +8047,6 @@ class Compiler
GenTree* optVNBasedFoldExpr(BasicBlock* block, GenTree* parent, GenTree* tree);
GenTree* optVNBasedFoldExpr_Call(BasicBlock* block, GenTree* parent, GenTreeCall* call);
GenTree* optVNBasedFoldExpr_Call_Memmove(GenTreeCall* call);
GenTree* optExtractSideEffListFromConst(GenTree* tree);

AssertionIndex GetAssertionCount()
{
Expand Down
8 changes: 7 additions & 1 deletion src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17223,7 +17223,13 @@ GenTree* Compiler::gtWrapWithSideEffects(GenTree* tree,
// It should be possible to be smarter here and allow such cases by extracting the side effects
// properly for this particular case. For now, caller is responsible for avoiding such cases.

tree = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree);
GenTree* comma = gtNewOperNode(GT_COMMA, tree->TypeGet(), sideEffects, tree);
if ((vnStore != nullptr) && tree->gtVNPair.BothDefined() && sideEffectsSource->gtVNPair.BothDefined())
{
comma->gtVNPair =
vnStore->VNPWithExc(tree->gtVNPair, vnStore->VNPExceptionSet(sideEffectsSource->gtVNPair));
}
return comma;
}
return tree;
}
Expand Down
Loading