diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp index 6c3863e4c7f66..94e61a255dd3b 100644 --- a/mlir/lib/Transforms/Utils/DialectConversion.cpp +++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp @@ -176,6 +176,8 @@ struct ConversionValueMapping { template std::enable_if_t::value && IsValueVector::value> map(OldVal &&oldVal, NewVal &&newVal) { + assert(!mapping.contains(oldVal) && + "attempting to overwrite existing mapping"); LLVM_DEBUG({ ValueVector next(newVal); while (true) { @@ -1412,6 +1414,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion( for (unsigned i = 0; i != origArgCount; ++i) { BlockArgument origArg = block->getArgument(i); Type origArgType = origArg.getType(); + ValueVector currentMapping = mapping.lookupOrDefault(origArg); std::optional inputMap = signatureConversion.getInputMapping(i); @@ -1421,7 +1424,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion( buildUnresolvedMaterialization( MaterializationKind::Source, OpBuilder::InsertPoint(newBlock, newBlock->begin()), origArg.getLoc(), - /*valuesToMap=*/{origArg}, /*inputs=*/ValueRange(), + /*valuesToMap=*/currentMapping, /*inputs=*/ValueRange(), /*outputType=*/origArgType, /*originalType=*/Type(), converter); appendRewrite(block, origArg, converter); continue; @@ -1432,7 +1435,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion( assert(inputMap->size == 0 && "invalid to provide a replacement value when the argument isn't " "dropped"); - mapping.map(origArg, repl); + mapping.map(currentMapping, repl); appendRewrite(block, origArg, converter); continue; } @@ -1441,7 +1444,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion( auto replArgs = newBlock->getArguments().slice(inputMap->inputNo, inputMap->size); ValueVector replArgVals = llvm::to_vector_of(replArgs); - mapping.map(origArg, std::move(replArgVals)); + mapping.map(currentMapping, std::move(replArgVals)); appendRewrite(block, origArg, converter); } @@ -1533,11 +1536,10 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue( Value castValue = buildUnresolvedMaterialization(MaterializationKind::Source, computeInsertPoint(repl), value.getLoc(), - /*valuesToMap=*/{value}, /*inputs=*/repl, + /*valuesToMap=*/repl, /*inputs=*/repl, /*outputType=*/value.getType(), /*originalType=*/Type(), converter) .front(); - mapping.map(value, castValue); return castValue; } @@ -1758,6 +1760,8 @@ void ConversionPatternRewriter::replaceUsesOfBlockArgument(BlockArgument from, << "'(in region of '" << parentOp->getName() << "'(" << from.getOwner()->getParentOp() << ")\n"; }); + llvm::errs() << "replaceUsesOfBlockArgument: " << from.getOwner() << "\n"; + impl->appendRewrite(from.getOwner(), from, impl->currentTypeConverter); impl->mapping.map(impl->mapping.lookupOrDefault(from), to);