Skip to content

Propagate doNotProfile state when cloning nodes - #7615

Merged
vijaysun-omr merged 1 commit into
eclipse-omr:masterfrom
ymanton:block-cloner-do-not-profile
Feb 5, 2025
Merged

vijaysun-omr merged 1 commit into
eclipse-omr:masterfrom
ymanton:block-cloner-do-not-profile

Conversation

@ymanton

@ymanton ymanton commented Jan 21, 2025

Copy link
Copy Markdown
Contributor

BlockSplitter uses BlockCloner to split blocks along hot paths, with the duplicated blocks forming the new hot path (that can then be straightened) and the original blocks being pushed out to cold paths.

BlockCloner clones nodes using one of TR::Node's copy constructors, which sets the doNotProfile on the clone's bytecode info. The copy constructors are used all over the place, not just to create copies of the original node but to also create new nodes. Setting the doNotProfile flag avoids inadvertantly proflining semantically unrelated nodes that happen to share the same bytecode, thereby polluting the profiling data.

Because of the above, when BlockSplitter runs, nothing on the hot path will be profiled, and in cases where the cold paths are never executed, we will completely miss out on profiling information. Because BlockSplitter and BlockCloner are duplicating nodes they don't need to worry about polluting the profiling data and should propagate the doNotProfile flag state to the nodes they duplicate.

@ymanton
ymanton requested a review from vijaysun-omr as a code owner January 21, 2025 19:22
@vijaysun-omr vijaysun-omr self-assigned this Jan 21, 2025
@ymanton

ymanton commented Jan 21, 2025

Copy link
Copy Markdown
Contributor Author

Patch currently makes the change in TR_BlockCloner::cloneNode():

omr/compiler/il/OMRBlock.cpp

Lines 2750 to 2785 in 1c2b08d

TR::Node *
TR_BlockCloner::cloneNode(TR::Node * from)
{
TR::Node * to;
if (from->getReferenceCount() > 1 && (to = _nodeMappings.getTo(from)))
return to;
int32_t n = from->getNumChildren();
to = TR::Node::copy(from);
if (from->getOpCode().getOpCodeValue() == TR::allocationFence)
{
if (from->getAllocation())
{
TR::Node *dupAllocNode = _nodeMappings.getTo(from->getAllocation());
if (dupAllocNode)
to->setAllocation(dupAllocNode);
}
}
if (from->getOpCode().isBranch())
{
if (_cloneBranchesExactly)
to->setBranchDestination(from->getBranchDestination());
else
to->setBranchDestination(getToBlock(from->getBranchDestination()->getNode()->getBlock())->getEntry());
}
for (int32_t i = 0; i < n; ++i)
to->setChild(i, cloneNode(from->getChild(i)));
if (from->getReferenceCount() > 1)
_nodeMappings.add(from, to, _cfg->comp()->trMemory());
return to;
}

Alternatively, we could do this in Node::copy(), which would have a wider reach:

omr/compiler/il/OMRNode.cpp

Lines 310 to 346 in 1c2b08d

TR::Node *
OMR::Node::copy(TR::Node * from)
{
TR::Compilation *comp = TR::comp();
int32_t numChildren = from->getNumChildren();
TR::ILOpCode opCode = from->getOpCode();
TR::ILOpCodes opCodeValue = opCode.getOpCodeValue();
if (opCode.isIf() ||
opCodeValue == TR::anewarray ||
opCodeValue == TR::newarray ||
opCodeValue == TR::arraycopy ||
opCodeValue == TR::tstart ||
opCodeValue == TR::arrayset)
++numChildren;
// note arraystorechk node has 1 or 2 children but it allocates spaces for 3 children
// we need to make sure we have room for 3
if (opCodeValue == TR::ArrayStoreCHK)
numChildren = 3;
TR::Node * n = new (comp->getNodePool()) TR::Node(from);
from->copyVirtualGuardInfoTo(n, comp);
return n;
}
TR::Node *
OMR::Node::copy(TR::Node * from, int32_t numChildren)
{
TR::Compilation *comp = TR::comp();
TR_ASSERT(numChildren >= from->getNumChildren(), "copy must have at least as many children as original");
TR::Node *clone = new (comp->getNodePool()) TR::Node(from, numChildren);
from->copyVirtualGuardInfoTo(clone, comp);
return clone;
}

Alternatively, we could do this in the Node::Node() copy-constructor that doesn't take an opcode parameter, which would have an even wider reach:

omr/compiler/il/OMRNode.cpp

Lines 232 to 306 in 1c2b08d

/**
* Copy constructor, must allow for copying more than 2 children.
*/
OMR::Node::Node(TR::Node * from, uint16_t numChildren)
: _opCode(TR::BadILOp),
_numChildren(0),
_globalIndex(0),
_flags(0),
_visitCount(0),
_localIndex(0),
_referenceCount(0),
_knownObjectIndex(TR::KnownObjectTable::UNKNOWN),
_byteCodeInfo(),
_unionBase(),
_unionPropertyA()
{
TR::Compilation * comp = TR::comp();
memcpy(self(), from, sizeof(TR::Node));
if (self()->hasDataType())
self()->setDataType(TR::NoType);
self()->copyChildren(from, numChildren, true /* forNodeExtensionOnly */);
if (from->getOpCode().getOpCodeValue() == TR::allocationFence)
self()->setAllocation(NULL);
self()->setGlobalIndex(comp->getNodePool().getLastGlobalIndex());
// a memcpy is used above to copy fields from the argument "node" to "this", but
// opt attributes are separate, and need separate initialization.
self()->setReferenceCount(from->getReferenceCount());
self()->setVisitCount(from->getVisitCount());
self()->setLocalIndex(from->getLocalIndex());
self()->setKnownObjectIndex(from->getKnownObjectIndex());
_unionA = from->_unionA;
if (self()->getGlobalIndex() == MAX_NODE_COUNT)
{
TR_ASSERT(0, "getGlobalIndex() == MAX_NODE_COUNT");
comp->failCompilation<TR::ExcessiveComplexity>("Global index equal to max node count");
}
if(comp->getDebug())
comp->getDebug()->newNode(self());
TR_IlGenerator * ilGen = comp->getCurrentIlGenerator();
if (ilGen)
{
_byteCodeInfo.setDoNotProfile(0);
}
else
{
_byteCodeInfo.setDoNotProfile(1);
}
if (from->getOpCode().isBranch() || from->getOpCode().isSwitch())
_byteCodeInfo.setDoNotProfile(1);
if (from->getOpCode().isStoreReg() || from->getOpCode().isLoadReg())
{
if (from->requiresRegisterPair(comp))
{
self()->setLowGlobalRegisterNumber(from->getLowGlobalRegisterNumber());
self()->setHighGlobalRegisterNumber(from->getHighGlobalRegisterNumber());
}
else
{
self()->setGlobalRegisterNumber(from->getGlobalRegisterNumber());
}
}
if (from->hasDataType())
{
self()->setDataType(from->getDataType());
}
}

There is a Node::Node() copy-constructor that takes an opcode parameter, which suggests to me that it's only purpose is to copy the node's bytecode info, but not it's semantic meaning within the program, so it's not a good choice.

As far as I can tell, semantically TR_BlockCloner::cloneNode() is concerned with creating exact duplicates of nodes, and preserving the doNotProfile flag state is a sensible thing to do. The 2 Node::copy()s seem to also have that purpose, and I don't see anything that suggests it should be used for other purposes. The Node::Node() copy-constructor that doesn't take an opcode parameter is probably there for the same reason, but I can imagine someone wanting to create a node that has the same opcode and points to the same bytecode as another without having the same semantic meaning as the original node, though arguably that's a bad practice that should be cleaned up if it can be found.

@vijaysun-omr

Copy link
Copy Markdown
Contributor

I would prefer changing the copy constructors you mentioned for wider reach, but will ask @hzongaro opinion too.

@hzongaro

Copy link
Copy Markdown
Contributor

@vijaysun-omr wrote:

I would prefer changing the copy constructors you mentioned for wider reach

I agree with that assessment, but I'm trying to understand what led to code in OMR::Node::Node(TR::Node * from, uint16_t numChildren) setting the doNotProfile flag after IL Generation. It looks like it came about with some changes related to OSR in OMR pull request #3030.

It seems OSR support in the JIT is using that flag in its bookkeeping. I'd have to dig deeper to understand whether changing that would cause any problems, but perhaps @vijaysun-omr has some recollection.

@vijaysun-omr

Copy link
Copy Markdown
Contributor

I think if the IL opcode was possible to be different without changing the bytecode information (that seems possible with the other node constructor in the PR that @hzongaro linked to), then I can see potential OSR issues. But if a straight copy is done of the node passed in to the node constructor, I don't see an obvious OSR issue. Younes was only proposing changing the "do not profile" property for nodes that did not change the IL opcode. I don't have any history beyond this reasoning and the discussion in that other PR you linked to @hzongaro

@hzongaro

Copy link
Copy Markdown
Contributor

Thanks, @vijaysun-omr. Given what you've said, I think it would be fine for the copy constructor to copy the doNotProfile setting in situations where the opcode does not change.

@vijaysun-omr

Copy link
Copy Markdown
Contributor

@ymanton please make the requested change when you get a chance. @hzongaro thanks for the input.

@jdmpapin

Copy link
Copy Markdown
Contributor

OSR uses the doNotProfile flag to identify nodes that would normally be OSR points, but where OSR isn't actually possible because the node wasn't created by IL gen and therefore the corresponding OSR infrastructure was never generated. There might not even be any correct way to generate it

Transformations that create such a situation should be statically pretty rare, i.e. they shouldn't happen in many places in the compiler codebase

With involuntary OSR, such transformations can't be done at all, since there's no way to guarantee that OSR won't be attempted

With voluntary OSR, it's only possible before the fear analysis, which happens very early. After that point, we're not allowed to introduce any new invalidation points. And if before fear analysis we do something to interfere with OSR, then it restricts where fear points can be placed and therefore also restricts the possible transformations that can be made based on OSR assumptions

I think the type of transformation this is meant to notice is one that invents new calls, e.g. one that changes a call into a series of two or more calls. In that case, all of the calls are OSR invalidation points, but only the last one could be an OSR induction point

Reading some of the discussion on #3030, the change to set doNotProfile explicitly when constructing Node seems to have been introduced because previously it would have been uninitialized

So for OSR purposes, I think that copies created by block splitter (or similar, e.g. loop replicator) are equivalent to their respective originals. This kind of copying will generally happen after the fear analysis anyway, so it shouldn't be able to affect whether OSR can actually happen at the copy. It is important though that we're copying not just the node but the entire block with its related control flow, so that any following OSR guard is copied along with it. Copying a node in isolation could represent some kind of reordering, which could constitute a "new" node from an OSR point of view. However, regardless of how we treat doNotProfile, it would most likely be incorrect to do that with OSR enabled anyway

BlockSplitter uses BlockCloner to split blocks along hot paths,
with the duplicated blocks forming the new hot path (that can then
be straightened) and the original blocks being pushed out to cold paths.

BlockCloner clones nodes using one of TR::Node's copy constructors,
and all Node constructors (copy or otherwise) set doNotProfile on the
new node's bytecode info. Setting the doNotProfile flag avoids
inadvertantly profiling semantically unrelated nodes that happen to
share the same bytecode, thereby polluting the profiling data. The flag
also has implications for OSR.

Because of the above, when BlockSplitter runs, nothing on the hot
path will be profiled, and in cases where the cold paths are never
executed, we will completely miss out on profiling information. Because
the Node copy constructors are duplicating nodes they don't need to
worry about polluting the profiling data and should propagate the
doNotProfile flag state to the nodes they duplicate.

Signed-off-by: Younes Manton <ymanton@ca.ibm.com>
@ymanton
ymanton force-pushed the block-cloner-do-not-profile branch from c8a506a to 7ecce7b Compare February 3, 2025 22:57
@vijaysun-omr

Copy link
Copy Markdown
Contributor

@hzongaro I can run tests after your review (and mine) are done.

@hzongaro hzongaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Thanks!

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Jenkins build all

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Tests have passed. Reviews are done. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants