Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,27 @@ TypeConverter::convertTypeAttribute(Type type, Attribute attr) const {
// FunctionOpInterfaceSignatureConversion
//===----------------------------------------------------------------------===//

static SmallVector<Attribute>
convertFuncOpAttrs(FunctionOpInterface funcOp,
TypeConverter::SignatureConversion &sigConv,
FunctionType newType) {
if (newType.getNumInputs() == funcOp.getNumArguments()) {
return {};
}
ArrayAttr allArgAttrs = funcOp.getAllArgAttrs();
if (!allArgAttrs)
return {};

SmallVector<Attribute> newAttrs(newType.getNumInputs());
for (auto i : llvm::seq(allArgAttrs.size())) {
auto mapping = sigConv.getInputMapping(i);
assert(mapping.has_value());
auto outIdx = mapping->inputNo;
Copy link
Member

Choose a reason for hiding this comment

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

What if an argument is mapped to multiple block arguments? (I.e., mapping.size > 1.)

newAttrs[outIdx] = allArgAttrs[i];
}
return newAttrs;
}

static LogicalResult convertFuncOpTypes(FunctionOpInterface funcOp,
const TypeConverter &typeConverter,
ConversionPatternRewriter &rewriter) {
Expand All @@ -3809,7 +3830,16 @@ static LogicalResult convertFuncOpTypes(FunctionOpInterface funcOp,
auto newType = FunctionType::get(rewriter.getContext(),
result.getConvertedTypes(), newResults);

rewriter.modifyOpInPlace(funcOp, [&] { funcOp.setType(newType); });
// If using 1-to-n type conversion, we must re-map argument attributes
// to the corresponding new argument index.
auto newArgAttrs = convertFuncOpAttrs(funcOp, result, newType);

rewriter.modifyOpInPlace(funcOp, [&] {
funcOp.setType(newType);
if (!newArgAttrs.empty()) {
funcOp.setAllArgAttrs(newArgAttrs);
}
});

return success();
}
Expand Down