@@ -74,8 +74,8 @@ chooseLaterInsertPointInBlock(OpBuilder::InsertPoint a,
7474// TODO: Extend DominanceInfo API to work with block iterators.
7575static OpBuilder::InsertPoint chooseLaterInsertPoint (OpBuilder::InsertPoint a,
7676 OpBuilder::InsertPoint b) {
77- // Case 1: Same block.
78- if (a.getBlock () == b.getBlock ())
77+ // Case 1: Fast path: Same block. This is the most common case .
78+ if (LLVM_LIKELY ( a.getBlock () == b.getBlock () ))
7979 return chooseLaterInsertPointInBlock (a, b);
8080
8181 // Case 2: Different block, but same region.
@@ -90,12 +90,12 @@ static OpBuilder::InsertPoint chooseLaterInsertPoint(OpBuilder::InsertPoint a,
9090 }
9191
9292 // Case 3: b's region contains a: choose a.
93- if (Operation *aParent = b.getBlock ()->getParent ()->findAncestorOpInRegion (
93+ if (b.getBlock ()->getParent ()->findAncestorOpInRegion (
9494 *a.getPoint ()->getParentOp ()))
9595 return a;
9696
9797 // Case 4: a's region contains b: choose b.
98- if (Operation *bParent = a.getBlock ()->getParent ()->findAncestorOpInRegion (
98+ if (a.getBlock ()->getParent ()->findAncestorOpInRegion (
9999 *b.getPoint ()->getParentOp ()))
100100 return b;
101101
@@ -249,6 +249,11 @@ ConversionValueMapping::lookupOrDefault(Value from,
249249 // Otherwise: Check if there is a mapping for the entire vector. Such
250250 // mappings are materializations. (N:M mapping are not supported for value
251251 // replacements.)
252+ //
253+ // Note: From a correctness point of view, materializations do not have to
254+ // be stored (and looked up) in the mapping. But for performance reasons,
255+ // we choose to reuse existing IR (when possible) instead of creating it
256+ // multiple times.
252257 auto it = mapping.find (current);
253258 if (it == mapping.end ()) {
254259 // No mapping found: The lookup stops here.
@@ -1514,6 +1519,18 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
15141519 // `applySignatureConversion`.)
15151520 return Value ();
15161521 }
1522+
1523+ // Note: `computeInsertPoint` computes the "earliest" insertion point at
1524+ // which all values in `repl` are defined. It is important to emit the
1525+ // materialization at that location because the same materialization may be
1526+ // reused in a different context. (That's because materializations are cached
1527+ // in the conversion value mapping.) The insertion point of the
1528+ // materialization must be valid for all future users that may be created
1529+ // later in the conversion process.
1530+ //
1531+ // Note: Instead of creating new IR, `buildUnresolvedMaterialization` may
1532+ // return an already existing, cached materialization from the conversion
1533+ // value mapping.
15171534 Value castValue =
15181535 buildUnresolvedMaterialization (MaterializationKind::Source,
15191536 computeInsertPoint (repl), value.getLoc (),
0 commit comments