diff --git a/flang/lib/Optimizer/Transforms/AbstractResult.cpp b/flang/lib/Optimizer/Transforms/AbstractResult.cpp --- a/flang/lib/Optimizer/Transforms/AbstractResult.cpp +++ b/flang/lib/Optimizer/Transforms/AbstractResult.cpp @@ -263,13 +263,14 @@ // Convert function type itself if it has an abstract result. auto funcTy = func.getFunctionType().cast(); if (hasAbstractResult(funcTy)) { - func.setType(getNewFunctionType(funcTy, shouldBoxResult)); if (!func.empty()) { // Insert new argument. mlir::OpBuilder rewriter(context); auto resultType = funcTy.getResult(0); auto argTy = getResultArgumentType(resultType, shouldBoxResult); - mlir::Value newArg = func.front().insertArgument(0u, argTy, loc); + func.insertArgument(0u, argTy, {}, loc); + func.eraseResult(0u); + mlir::Value newArg = func.getArgument(0u); if (mustEmboxResult(resultType, shouldBoxResult)) { auto bufferType = fir::ReferenceType::get(resultType); rewriter.setInsertionPointToStart(&func.front()); @@ -278,6 +279,10 @@ patterns.insert(context, newArg); target.addDynamicallyLegalOp( [](mlir::func::ReturnOp ret) { return ret.operands().empty(); }); + assert(func.getFunctionType() == + getNewFunctionType(funcTy, shouldBoxResult)); + } else { + func.setType(getNewFunctionType(funcTy, shouldBoxResult)); } } } diff --git a/flang/test/Fir/abstract-result-2.fir b/flang/test/Fir/abstract-result-2.fir new file mode 100644 --- /dev/null +++ b/flang/test/Fir/abstract-result-2.fir @@ -0,0 +1,27 @@ +// RUN: fir-opt %s --abstract-result-on-func-opt | FileCheck %s + +// Check that the attributes are shifted along with their corresponding arguments + +//CHECK: func.func @_QMi8Pintrinsic_pack0(%arg0: !fir.ref>>>, %arg1: !fir.box> {fir.bindc_name = "array"}, %arg2: !fir.ref> {fir.bindc_name = "mask"}, %arg3: !fir.box> {fir.bindc_name = "vector", fir.optional}) + +func.func @_QMi8Pintrinsic_pack0(%arg0: !fir.box> {fir.bindc_name = "array"}, %arg1: !fir.ref> {fir.bindc_name = "mask"}, %arg2: !fir.box> {fir.bindc_name = "vector", fir.optional}) -> !fir.box>> { + %0 = fir.alloca !fir.box>> + %1 = fir.alloca !fir.box>> {bindc_name = "intrinsic_pack0", uniq_name = "_QMi8Fintrinsic_pack0Eintrinsic_pack0"} + %2 = fir.zero_bits !fir.heap> + %c0 = arith.constant 0 : index + %3 = fir.shape %c0 : (index) -> !fir.shape<1> + %4 = fir.embox %2(%3) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> + fir.store %4 to %1 : !fir.ref>>> + %5 = fir.embox %arg1 : (!fir.ref>) -> !fir.box> + %6 = fir.zero_bits !fir.heap> + %7 = fir.embox %6(%3) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> + fir.store %7 to %0 : !fir.ref>>> + %8 = fir.convert %0 : (!fir.ref>>>) -> !fir.ref> + %9 = fir.convert %arg0 : (!fir.box>) -> !fir.box + %10 = fir.convert %5 : (!fir.box>) -> !fir.box + %11 = fir.convert %arg2 : (!fir.box>) -> !fir.box + %12 = fir.call @_FortranAPack(%8, %9, %10, %11) : (!fir.ref>, !fir.box, !fir.box, !fir.box) -> none + %13 = fir.load %1 : !fir.ref>>> + return %13 : !fir.box>> +} +func.func private @_FortranAPack(!fir.ref>, !fir.box, !fir.box, !fir.box) -> none attributes {fir.runtime}