@@ -1618,6 +1618,8 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
1618
1618
if (!inputMap) {
1619
1619
// This block argument was dropped and no replacement value was provided.
1620
1620
// Materialize a replacement value "out of thin air".
1621
+ // Note: Materialization must be built here because we cannot find a
1622
+ // valid insertion point in the new block. (Will point to the old block.)
1621
1623
Value mat =
1622
1624
buildUnresolvedMaterialization (
1623
1625
MaterializationKind::Source,
@@ -1725,29 +1727,29 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
1725
1727
// (regardless of the type) and build a source materialization to the
1726
1728
// original type.
1727
1729
repl = lookupOrNull (value);
1730
+
1731
+ // Compute the insertion point of the materialization.
1732
+ OpBuilder::InsertPoint ip;
1728
1733
if (repl.empty ()) {
1729
- // No replacement value is registered in the mapping. This means that the
1730
- // value is dropped and no longer needed. (If the value were still needed,
1731
- // a source materialization producing a replacement value "out of thin air"
1732
- // would have already been created during `replaceOp` or
1733
- // `applySignatureConversion`.)
1734
- return Value ();
1734
+ // The source materialization has no inputs. Insert it right before the
1735
+ // value that it is replacing.
1736
+ ip = computeInsertPoint (value);
1737
+ } else {
1738
+ // Compute the "earliest" insertion point at which all values in `repl` are
1739
+ // defined. It is important to emit the materialization at that location
1740
+ // because the same materialization may be reused in a different context.
1741
+ // (That's because materializations are cached in the conversion value
1742
+ // mapping.) The insertion point of the materialization must be valid for
1743
+ // all future users that may be created later in the conversion process.
1744
+ ip = computeInsertPoint (repl);
1735
1745
}
1736
-
1737
- // Note: `computeInsertPoint` computes the "earliest" insertion point at
1738
- // which all values in `repl` are defined. It is important to emit the
1739
- // materialization at that location because the same materialization may be
1740
- // reused in a different context. (That's because materializations are cached
1741
- // in the conversion value mapping.) The insertion point of the
1742
- // materialization must be valid for all future users that may be created
1743
- // later in the conversion process.
1744
- Value castValue =
1745
- buildUnresolvedMaterialization (MaterializationKind::Source,
1746
- computeInsertPoint (repl), value.getLoc (),
1747
- /* valuesToMap=*/ repl, /* inputs=*/ repl,
1748
- /* outputTypes=*/ value.getType (),
1749
- /* originalType=*/ Type (), converter)
1750
- .front ();
1746
+ Value castValue = buildUnresolvedMaterialization (
1747
+ MaterializationKind::Source, ip, value.getLoc (),
1748
+ /* valuesToMap=*/ repl, /* inputs=*/ repl,
1749
+ /* outputTypes=*/ value.getType (),
1750
+ /* originalType=*/ Type (), converter,
1751
+ /* isPureTypeConversion=*/ !repl.empty ())
1752
+ .front ();
1751
1753
return castValue;
1752
1754
}
1753
1755
@@ -1897,21 +1899,8 @@ void ConversionPatternRewriterImpl::replaceOp(
1897
1899
}
1898
1900
1899
1901
// Create mappings for each of the new result values.
1900
- for (auto [repl, result] : llvm::zip_equal (newValues, op->getResults ())) {
1901
- if (repl.empty ()) {
1902
- // This result was dropped and no replacement value was provided.
1903
- // Materialize a replacement value "out of thin air".
1904
- buildUnresolvedMaterialization (
1905
- MaterializationKind::Source, computeInsertPoint (result),
1906
- result.getLoc (), /* valuesToMap=*/ {result}, /* inputs=*/ ValueRange (),
1907
- /* outputTypes=*/ result.getType (), /* originalType=*/ Type (),
1908
- currentTypeConverter, /* isPureTypeConversion=*/ false );
1909
- continue ;
1910
- }
1911
-
1912
- // Remap result to replacement value.
1902
+ for (auto [repl, result] : llvm::zip_equal (newValues, op->getResults ()))
1913
1903
mapping.map (static_cast <Value>(result), std::move (repl));
1914
- }
1915
1904
1916
1905
appendRewrite<ReplaceOperationRewrite>(op, currentTypeConverter);
1917
1906
// Mark this operation and all nested ops as replaced.
0 commit comments