[Mlir-commits] [mlir] [mlir][Func] Preserve attribute when converting CallOp/ReturnOp signature (PR #127772)

Hongren Zheng llvmlistbot at llvm.org
Wed Feb 19 05:16:38 PST 2025


================
@@ -126,8 +127,9 @@ class ReturnOpTypeConversion : public OpConversionPattern<ReturnOp> {
   LogicalResult
   matchAndRewrite(ReturnOp op, OneToNOpAdaptor adaptor,
                   ConversionPatternRewriter &rewriter) const final {
-    rewriter.replaceOpWithNewOp<ReturnOp>(op,
-                                          flattenValues(adaptor.getOperands()));
+    rewriter
+        .replaceOpWithNewOp<ReturnOp>(op, flattenValues(adaptor.getOperands()))
+        ->setAttrs(op->getAttrs());
----------------
ZenithalHourlyRate wrote:

> The API you mention don't exist, it is called setDiscardableAttrs() in the codebase.

https://github.com/llvm/llvm-project/blob/c6a907ac46869e3552f3a62eef08ba5548676d85/mlir/include/mlir/IR/Operation.h#L651-L660

It is indeed a helper that manipulates those discardable attrs.

Another point I want to make is that, `func::FuncOp` signature conversion happens to preserve all the attributes, argument attributes and result attributes because it uses `rewriter.modifyOpInplace`

https://github.com/llvm/llvm-project/blob/c6a907ac46869e3552f3a62eef08ba5548676d85/mlir/lib/Transforms/Utils/DialectConversion.cpp#L3055-L3059

Other code that use `rewriter.create`-like way to do type conversion just inherits the original attribute, like

https://github.com/llvm/llvm-project/blob/c6a907ac46869e3552f3a62eef08ba5548676d85/mlir/lib/Transforms/Utils/DialectConversion.cpp#L3110-L3112

So making CallOp/ReturnOp also preserve the attribute would be more consistent than the current situation where some preserve them yet others discard them.

Or another way around, we need to change funcOp/other conversions to discard attributes.

https://github.com/llvm/llvm-project/pull/127772


More information about the Mlir-commits mailing list