This is an archive of the discontinued LLVM Phabricator instance.

Avoid unnecessary uses of `MDNode::getTemporary`, NFC
ClosedPublic

Authored by dexonsmith on Oct 23 2020, 3:19 PM.

Details

Summary

TempMDNode includes a bunch of machinery for RAUW, and should only be
used when necessary. RAUW wasn't being used in any of these cases... it
was just a placeholder for a self-reference.

Where the real node was using MDNode::getDistinct, just replace the
temporary argument with nullptr.

Where the real node was using MDNode::get, the replaceOperandWith
call was "promoting" the node to a distinct one implicitly due to
self-reference detection in MDNode::handleChangedOperand. The
TempMDNode was serving a purpose by delaying uniquing, but it's way
simpler to just call MDNode::getDistinct in the first place.

Note that using a self-reference at all in these places is a hold-over
from before distinct metadata existed. It was an old trick to create
distinct nodes. It would be intrusive to change, including bitcode
upgrades, etc., and it's harmless so I'm not sure there's much value in
removing it from existing schemas. After this commit it still has a tiny
memory cost (in the extra metadata operand) but no more overhead in
construction.

Diff Detail

Event Timeline

dexonsmith created this revision.Oct 23 2020, 3:19 PM
Herald added a project: Restricted Project. · View Herald Transcript
dexonsmith requested review of this revision.Oct 23 2020, 3:19 PM

https://github.com/llvm/llvm-project/blob/master/llvm/lib/IR/Metadata.cpp#L716 is where the auto-detection of self-references is:

// Drop uniquing for self-reference cycles and deleted constants.
if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
  if (!isResolved())
    resolve();
  storeDistinctInContext();
  return;
}

BTW, this is a long-delayed follow-up to 5e5b85098dbeaea2cfa5d01695b5d2982634d7dd.

dexonsmith retitled this revision from CGLoopInfo: Avoid unnecessary uses of `MDNode::getTemporary`, NFC to Avoid unnecessary uses of `MDNode::getTemporary`, NFC.Oct 23 2020, 3:53 PM
aprantl accepted this revision.Oct 26 2020, 9:40 AM

The push_back(nullptr) reads weird to me, but it is an accurate reflection of what the IR will look like.

This revision is now accepted and ready to land.Oct 26 2020, 9:40 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptOct 26 2020, 2:03 PM