[llvm-branch-commits] [flang] [flang] Fuse chained character concatenations into one buffer (PR #239331)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 16 01:07:49 PST 2026
https://github.com/jeanPerier updated https://github.com/llvm/llvm-project/pull/239331
>From bb3fe9a2d459cf7ddfb9c194c47300ccc7f72d34 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Thu, 5 Nov 2026 07:54:08 -0800
Subject: [PATCH 1/2] [flang] Fuse chained character concatenations into one
buffer
Lowering creates one binary hlfir.concat per // operator, so a // b // c
was evaluated into an intermediate buffer that was then copied into the
result buffer. Add a canonicalization that folds a single-use hlfir.concat
into its user when no operation in between may modify or release its
operands, and bufferize hlfir.concat with any number of operands. The
buffer is now sized with the hlfir.concat length operand, which can be
computed outside of elemental loops, instead of the sum of the operand
lengths computed in the loop body.
---
.../flang/Optimizer/Builder/Character.h | 5 +-
.../include/flang/Optimizer/HLFIR/HLFIROps.td | 1 +
flang/lib/Optimizer/Builder/Character.cpp | 4 +-
flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp | 59 +++++++++
.../HLFIR/Transforms/BufferizeHLFIR.cpp | 37 +++---
flang/test/HLFIR/associate-codegen.fir | 37 +++---
.../HLFIR/character-expression-lifetimes.fir | 51 ++++++++
flang/test/HLFIR/concat-bufferization.fir | 117 +++++++++---------
flang/test/HLFIR/elemental-codegen.fir | 1 +
flang/test/HLFIR/get_length_codegen.fir | 2 +-
.../test/Integration/concat-chain-buffer.f90 | 14 +++
.../character-expression-storage.f90 | 20 +--
12 files changed, 243 insertions(+), 105 deletions(-)
create mode 100644 flang/test/Integration/concat-chain-buffer.f90
diff --git a/flang/include/flang/Optimizer/Builder/Character.h b/flang/include/flang/Optimizer/Builder/Character.h
index 613cb80e5ba9b5..3f0aa05492a538 100644
--- a/flang/include/flang/Optimizer/Builder/Character.h
+++ b/flang/include/flang/Optimizer/Builder/Character.h
@@ -63,10 +63,9 @@ class CharacterExprHelper {
void createAssign(const fir::ExtendedValue &lhs,
const fir::ExtendedValue &rhs);
- /// Create lhs // rhs in a stack or heap temporary.
+ /// Create lhs // rhs in temp obtained with fir.alloca
fir::CharBoxValue createConcatenate(const fir::CharBoxValue &lhs,
- const fir::CharBoxValue &rhs,
- bool useHeap = false);
+ const fir::CharBoxValue &rhs);
/// Create {max,min}(lhs,rhs) in a stack or heap temporary.
fir::CharBoxValue createCharExtremum(bool predIsMin,
diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
index 2d5e8e8c2618e7..1b371dcf285149 100644
--- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
+++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
@@ -379,6 +379,7 @@ def hlfir_ConcatOp : hlfir_Op<"concat",
let builders = [OpBuilder<(ins "mlir::ValueRange":$strings,"mlir::Value":$len)>];
+ let hasCanonicalizer = 1;
let hasVerifier = 1;
}
diff --git a/flang/lib/Optimizer/Builder/Character.cpp b/flang/lib/Optimizer/Builder/Character.cpp
index 670417b8887463..24d10be865932d 100644
--- a/flang/lib/Optimizer/Builder/Character.cpp
+++ b/flang/lib/Optimizer/Builder/Character.cpp
@@ -487,13 +487,13 @@ void fir::factory::CharacterExprHelper::createAssign(
}
fir::CharBoxValue fir::factory::CharacterExprHelper::createConcatenate(
- const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs, bool useHeap) {
+ const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs) {
auto lhsLen = builder.createConvert(loc, builder.getCharacterLengthType(),
lhs.getLen());
auto rhsLen = builder.createConvert(loc, builder.getCharacterLengthType(),
rhs.getLen());
mlir::Value len = mlir::arith::AddIOp::create(builder, loc, lhsLen, rhsLen);
- auto temp = createCharacterTemp(getCharacterType(rhs), len, useHeap);
+ auto temp = createCharacterTemp(getCharacterType(rhs), len);
createCopy(temp, lhs, lhsLen);
auto one = builder.createIntegerConstant(loc, len.getType(), 1);
auto upperBound = mlir::arith::SubIOp::create(builder, loc, len, one);
diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
index e40036d1a402a5..87929e2e0f04e6 100644
--- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -22,6 +22,7 @@
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/OpImplementation.h"
+#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/CommandLine.h"
@@ -794,6 +795,64 @@ getCharacterLengthIfStatic(mlir::Type t) {
return std::nullopt;
}
+namespace {
+/// Fuse single-use concatenations before allocating their buffers. The reads
+/// of the inner operands move to the outer concatenation, so do not cross any
+/// operation that may change or release their storage.
+struct InlineConcat : public mlir::OpRewritePattern<hlfir::ConcatOp> {
+ using OpRewritePattern::OpRewritePattern;
+ llvm::LogicalResult
+ matchAndRewrite(hlfir::ConcatOp concat,
+ mlir::PatternRewriter &rewriter) const override {
+ for (auto [index, string] : llvm::enumerate(concat.getStrings())) {
+ auto inner = string.getDefiningOp<hlfir::ConcatOp>();
+ if (!inner || inner->getBlock() != concat->getBlock())
+ continue;
+ unsigned uses = 0;
+ llvm::SmallVector<hlfir::DestroyOp> destroys;
+ for (mlir::OpOperand &use : string.getUses()) {
+ if (auto destroy = mlir::dyn_cast<hlfir::DestroyOp>(use.getOwner()))
+ destroys.push_back(destroy);
+ else
+ ++uses;
+ }
+ if (uses != 1)
+ continue;
+ bool canMove = true;
+ for (mlir::Operation *op = inner->getNextNode(); op != concat;
+ op = op->getNextNode()) {
+ auto effects = mlir::getEffectsRecursively(op);
+ if (!effects || llvm::any_of(*effects, [](const auto &effect) {
+ return !mlir::isa<mlir::MemoryEffects::Read>(effect.getEffect());
+ })) {
+ canMove = false;
+ break;
+ }
+ }
+ if (!canMove)
+ continue;
+ llvm::SmallVector<mlir::Value> strings(concat.getStrings());
+ strings.erase(strings.begin() + index);
+ strings.insert(strings.begin() + index, inner.getStrings().begin(),
+ inner.getStrings().end());
+ rewriter.modifyOpInPlace(
+ concat, [&]() { concat.getStringsMutable().assign(strings); });
+ for (auto destroy : destroys)
+ rewriter.eraseOp(destroy);
+ rewriter.eraseOp(inner);
+ return mlir::success();
+ }
+ return mlir::failure();
+ }
+};
+
+} // namespace
+
+void hlfir::ConcatOp::getCanonicalizationPatterns(
+ mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
+ patterns.add<InlineConcat>(context);
+}
+
llvm::LogicalResult hlfir::ConcatOp::verify() {
if (getStrings().size() < 2)
return emitOpError("must be provided at least two string operands");
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index 88f1fe796eaa4d..9276d586404ad9 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -284,20 +284,29 @@ struct ConcatOpConversion : public mlir::OpConversionPattern<hlfir::ConcatOp> {
fir::FirOpBuilder builder(rewriter, concat.getOperation());
assert(adaptor.getStrings().size() >= 2 &&
"must have at least two strings operands");
- if (adaptor.getStrings().size() > 2)
- TODO(loc, "codegen of optimized chained concatenation of more than two "
- "strings");
- hlfir::Entity lhs = getBufferizedExprStorage(adaptor.getStrings()[0]);
- hlfir::Entity rhs = getBufferizedExprStorage(adaptor.getStrings()[1]);
- auto [lhsExv, c1] = hlfir::translateToExtendedValue(loc, builder, lhs);
- auto [rhsExv, c2] = hlfir::translateToExtendedValue(loc, builder, rhs);
- assert(!c1 && !c2 && "expected variables");
- bool useHeap = !mlir::cast<fir::CharacterType>(
- hlfir::getFortranElementType(concat.getType()))
- .hasConstantLen();
- fir::CharBoxValue temp =
- fir::factory::CharacterExprHelper{builder, loc}.createConcatenate(
- *lhsExv.getCharBox(), *rhsExv.getCharBox(), useHeap);
+ fir::factory::CharacterExprHelper helper{builder, loc};
+ auto charType = hlfir::getFortranElementType(concat.getType());
+ auto temp = helper.createCharacterTemp(
+ charType, adaptor.getLength(),
+ /*useHeap=*/!fir::getIntIfConstant(adaptor.getLength()));
+ mlir::Value offset =
+ builder.createIntegerConstant(loc, builder.getIndexType(), 0);
+ mlir::Value one =
+ builder.createIntegerConstant(loc, builder.getIndexType(), 1);
+ for (mlir::Value string : adaptor.getStrings()) {
+ auto [source, cleanup] = hlfir::translateToExtendedValue(
+ loc, builder, getBufferizedExprStorage(string));
+ assert(!cleanup && "expected variable");
+ auto charBox = *source.getCharBox();
+ auto lower = mlir::arith::AddIOp::create(builder, loc, offset, one);
+ auto address = helper.genSubstringBase(temp.getBuffer(), lower,
+ builder.getRefType(charType), one);
+ helper.createCopy(fir::CharBoxValue{address, charBox.getLen()}, charBox,
+ charBox.getLen());
+ auto length =
+ builder.createConvert(loc, builder.getIndexType(), charBox.getLen());
+ offset = mlir::arith::AddIOp::create(builder, loc, offset, length);
+ }
fir::ExtendedValue res = temp;
// Ensure the memory type is the same as the result type.
mlir::Type addrType = fir::ReferenceType::get(
diff --git a/flang/test/HLFIR/associate-codegen.fir b/flang/test/HLFIR/associate-codegen.fir
index e1b5152d74c4d2..1485a66006a8a6 100644
--- a/flang/test/HLFIR/associate-codegen.fir
+++ b/flang/test/HLFIR/associate-codegen.fir
@@ -75,27 +75,34 @@ func.func @associate_char(%arg0: !fir.boxchar<1> ) {
// CHECK: %[[VAL_4:.*]] = arith.addi %[[VAL_2]]#1, %[[VAL_1]] : index
// CHECK: %[[VAL_5:.*]] = fir.undefined !fir.ref<!fir.char<1>>
// CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] typeparams %[[VAL_1]] uniq_name("char_literal") fortran_attrs<parameter> : (!fir.ref<!fir.char<1>>, index) -> (!fir.ref<!fir.char<1>>, !fir.ref<!fir.char<1>>)
-// CHECK: %[[VAL_7:.*]] = arith.addi %[[VAL_2]]#1, %[[VAL_1]] : index
-// CHECK: %[[VAL_8:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_7]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[VAL_8:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_4]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[VAL_35:.*]] = arith.constant 0 : index
+// CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
+// CHECK: %[[VAL_36:.*]] = arith.addi %[[VAL_35]], %[[VAL_15]] : index
+// CHECK: %[[VAL_37:.*]] = arith.subi %[[VAL_36]], %[[VAL_15]] : index
+// CHECK: %[[VAL_38:.*]] = fir.convert %[[VAL_8]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_39:.*]] = fir.coordinate_of %[[VAL_38]], %[[VAL_37]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_40:.*]] = fir.convert %[[VAL_39]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
// CHECK: %[[VAL_9:.*]] = arith.constant 1 : i64
// CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_2]]#1 : (index) -> i64
// CHECK: %[[VAL_11:.*]] = arith.muli %[[VAL_9]], %[[VAL_10]] : i64
-// CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_8]] : (!fir.heap<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_40]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: "llvm.intr.memmove"(%[[VAL_13]], %[[VAL_14]], %[[VAL_11]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
-// CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_16:.*]] = arith.subi %[[VAL_7]], %[[VAL_15]] : index
-// CHECK: fir.do_loop %[[VAL_17:.*]] = %[[VAL_2]]#1 to %[[VAL_16]] step %[[VAL_15]] {
-// CHECK: %[[VAL_18:.*]] = arith.subi %[[VAL_17]], %[[VAL_2]]#1 : index
-// CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_6]]#0 : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.array<1x!fir.char<1>>>
-// CHECK: %[[VAL_20:.*]] = fir.coordinate_of %[[VAL_19]], %[[VAL_18]] : (!fir.ref<!fir.array<1x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_21:.*]] = fir.load %[[VAL_20]] : !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_8]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_23:.*]] = fir.coordinate_of %[[VAL_22]], %[[VAL_17]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: fir.store %[[VAL_21]] to %[[VAL_23]] : !fir.ref<!fir.char<1>>
-// CHECK: }
+// CHECK: %[[VAL_41:.*]] = arith.addi %[[VAL_35]], %[[VAL_2]]#1 : index
+// CHECK: %[[VAL_42:.*]] = arith.addi %[[VAL_41]], %[[VAL_15]] : index
+// CHECK: %[[VAL_43:.*]] = arith.subi %[[VAL_42]], %[[VAL_15]] : index
+// CHECK: %[[VAL_44:.*]] = fir.convert %[[VAL_8]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_45:.*]] = fir.coordinate_of %[[VAL_44]], %[[VAL_43]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_46:.*]] = fir.convert %[[VAL_45]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+// CHECK: %[[VAL_47:.*]] = arith.constant 1 : i64
+// CHECK: %[[VAL_48:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
+// CHECK: %[[VAL_49:.*]] = arith.muli %[[VAL_47]], %[[VAL_48]] : i64
+// CHECK: %[[VAL_50:.*]] = fir.convert %[[VAL_46]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_51:.*]] = fir.convert %[[VAL_6]]#0 : (!fir.ref<!fir.char<1>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%[[VAL_50]], %[[VAL_51]], %[[VAL_49]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
// CHECK: %[[VAL_8_REF:.*]] = fir.convert %[[VAL_8]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,?>>
-// CHECK: %[[VAL_24:.*]]:2 = hlfir.declare %[[VAL_8_REF]] typeparams %[[VAL_7]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[VAL_24:.*]]:2 = hlfir.declare %[[VAL_8_REF]] typeparams %[[VAL_4]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_25:.*]] = arith.constant true
// CHECK: %[[VAL_26:.*]] = fir.undefined tuple<!fir.boxchar<1>, i1>
// CHECK: %[[VAL_27:.*]] = fir.insert_value %[[VAL_26]], %[[VAL_25]], [1 : index] : (tuple<!fir.boxchar<1>, i1>, i1) -> tuple<!fir.boxchar<1>, i1>
diff --git a/flang/test/HLFIR/character-expression-lifetimes.fir b/flang/test/HLFIR/character-expression-lifetimes.fir
index 003899d91d1ece..cb6f64f98ced08 100644
--- a/flang/test/HLFIR/character-expression-lifetimes.fir
+++ b/flang/test/HLFIR/character-expression-lifetimes.fir
@@ -1,5 +1,56 @@
+// RUN: fir-opt %s --canonicalize="region-simplify=disabled" | FileCheck %s --check-prefix=CANON
// RUN: fir-opt %s --canonicalize="region-simplify=disabled" --bufferize-hlfir --canonicalize="region-simplify=disabled" | FileCheck %s
+func.func private @change(!fir.boxchar<1>)
+
+// A single-use concatenation chain, including its destroys, is replaced by
+// one buffer. The buffer must be freed after assignment, not before it.
+// CANON-LABEL: func.func @chain
+// CANON: %[[JOINED:.*]] = hlfir.concat %arg0, %arg1, %arg2
+// CANON-NOT: hlfir.concat
+// CANON: hlfir.assign %[[JOINED]]
+// CANON: hlfir.destroy %[[JOINED]]
+// CANON-NOT: hlfir.destroy
+// CANON: return
+// CHECK-LABEL: func.func @chain
+// CHECK: fir.allocmem !fir.char<1,?>
+// CHECK-NOT: fir.allocmem
+// CHECK: hlfir.assign
+// CHECK-NOT: fir.allocmem
+// CHECK: fir.freemem
+// CHECK-NOT: fir.freemem
+// CHECK: return
+func.func @chain(%a: !fir.boxchar<1>, %b: !fir.boxchar<1>, %c: !fir.boxchar<1>, %out: !fir.boxchar<1>, %n: index, %m: index) {
+ %x = hlfir.concat %a, %b len %n : (!fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ %y = hlfir.concat %x, %c len %m : (!hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ hlfir.assign %y to %out : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
+ hlfir.destroy %y : !hlfir.expr<!fir.char<1,?>>
+ hlfir.destroy %x : !hlfir.expr<!fir.char<1,?>>
+ return
+}
+
+// Fusion must not postpone reading A past a call that may modify A.
+// CANON-LABEL: func.func @snapshot
+// CANON: hlfir.concat
+// CANON: fir.call @change
+// CANON: hlfir.concat
+// CHECK-LABEL: func.func @snapshot
+// CHECK: fir.allocmem !fir.char<1,?>
+// CHECK: fir.call @change
+// CHECK: fir.allocmem !fir.char<1,?>
+// CHECK: hlfir.assign
+// CHECK-COUNT-2: fir.freemem
+// CHECK: return
+func.func @snapshot(%a: !fir.boxchar<1>, %b: !fir.boxchar<1>, %out: !fir.boxchar<1>, %n: index, %m: index) {
+ %x = hlfir.concat %a, %b len %n : (!fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ fir.call @change(%a) : (!fir.boxchar<1>) -> ()
+ %y = hlfir.concat %x, %b len %m : (!hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ hlfir.assign %y to %out : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
+ hlfir.destroy %y : !hlfir.expr<!fir.char<1,?>>
+ hlfir.destroy %x : !hlfir.expr<!fir.char<1,?>>
+ return
+}
+
// Explicit cleanup of an intermediate expression survives association through
// no_reassoc and releases the buffer after the comparison.
// CHECK-LABEL: func.func @intermediate
diff --git a/flang/test/HLFIR/concat-bufferization.fir b/flang/test/HLFIR/concat-bufferization.fir
index 234fb95a589cc7..b99af4351e08bb 100644
--- a/flang/test/HLFIR/concat-bufferization.fir
+++ b/flang/test/HLFIR/concat-bufferization.fir
@@ -27,27 +27,34 @@ func.func @concat(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2: !fir.bo
// CHECK: %[[VAL_7:.*]]:2 = fir.unboxchar %[[VAL_2]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
// CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_7]]#0 typeparams %[[VAL_7]]#1 uniq_name("c3") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_9:.*]] = arith.addi %[[VAL_5]]#1, %[[VAL_7]]#1 : index
-// CHECK: %[[VAL_10:.*]] = arith.addi %[[VAL_5]]#1, %[[VAL_7]]#1 : index
-// CHECK: %[[VAL_11:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_10]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[VAL_11:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_9]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[VAL_58:.*]] = arith.constant 0 : index
+// CHECK: %[[VAL_59:.*]] = arith.constant 1 : index
+// CHECK: %[[VAL_60:.*]] = arith.addi %[[VAL_58]], %[[VAL_59]] : index
+// CHECK: %[[VAL_61:.*]] = arith.subi %[[VAL_60]], %[[VAL_59]] : index
+// CHECK: %[[VAL_62:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_63:.*]] = fir.coordinate_of %[[VAL_62]], %[[VAL_61]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_64:.*]] = fir.convert %[[VAL_63]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
// CHECK: %[[VAL_12:.*]] = arith.constant 1 : i64
// CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_5]]#1 : (index) -> i64
// CHECK: %[[VAL_14:.*]] = arith.muli %[[VAL_12]], %[[VAL_13]] : i64
-// CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_64]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_6]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: "llvm.intr.memmove"(%[[VAL_15]], %[[VAL_16]], %[[VAL_14]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
-// CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_18:.*]] = arith.subi %[[VAL_10]], %[[VAL_17]] : index
-// CHECK: fir.do_loop %[[VAL_19:.*]] = %[[VAL_5]]#1 to %[[VAL_18]] step %[[VAL_17]] {
-// CHECK: %[[VAL_20:.*]] = arith.subi %[[VAL_19]], %[[VAL_5]]#1 : index
-// CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_8]]#1 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_22:.*]] = fir.coordinate_of %[[VAL_21]], %[[VAL_20]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_22]] : !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_24:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_25:.*]] = fir.coordinate_of %[[VAL_24]], %[[VAL_19]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: fir.store %[[VAL_23]] to %[[VAL_25]] : !fir.ref<!fir.char<1>>
-// CHECK: }
+// CHECK: %[[VAL_65:.*]] = arith.addi %[[VAL_58]], %[[VAL_5]]#1 : index
+// CHECK: %[[VAL_66:.*]] = arith.addi %[[VAL_65]], %[[VAL_59]] : index
+// CHECK: %[[VAL_67:.*]] = arith.subi %[[VAL_66]], %[[VAL_59]] : index
+// CHECK: %[[VAL_68:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_69:.*]] = fir.coordinate_of %[[VAL_68]], %[[VAL_67]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_70:.*]] = fir.convert %[[VAL_69]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+// CHECK: %[[VAL_71:.*]] = arith.constant 1 : i64
+// CHECK: %[[VAL_72:.*]] = fir.convert %[[VAL_7]]#1 : (index) -> i64
+// CHECK: %[[VAL_73:.*]] = arith.muli %[[VAL_71]], %[[VAL_72]] : i64
+// CHECK: %[[VAL_74:.*]] = fir.convert %[[VAL_70]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_75:.*]] = fir.convert %[[VAL_8]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%[[VAL_74]], %[[VAL_75]], %[[VAL_73]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
// CHECK: %[[VAL_11_REF:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,?>>
-// CHECK: %[[VAL_26:.*]]:2 = hlfir.declare %[[VAL_11_REF]] typeparams %[[VAL_10]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
+// CHECK: %[[VAL_26:.*]]:2 = hlfir.declare %[[VAL_11_REF]] typeparams %[[VAL_9]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_27:.*]] = arith.constant true
// CHECK: %[[VAL_28:.*]] = fir.undefined tuple<!fir.boxchar<1>, i1>
// CHECK: %[[VAL_29:.*]] = fir.insert_value %[[VAL_28]], %[[VAL_27]], [1 : index] : (tuple<!fir.boxchar<1>, i1>, i1) -> tuple<!fir.boxchar<1>, i1>
@@ -69,12 +76,10 @@ func.func @concat_chained(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2:
%6:2 = fir.unboxchar %arg3 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
%7:2 = hlfir.declare %6#0 typeparams %6#1 uniq_name("c4") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
%8 = arith.addi %2#1, %4#1 : index
- %9 = hlfir.concat %3#0, %5#0 len %8 : (!fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
%10 = arith.addi %8, %6#1 : index
- %11 = hlfir.concat %9, %7#0 len %10 : (!hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ %11 = hlfir.concat %3#0, %5#0, %7#0 len %10 : (!fir.boxchar<1>, !fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
hlfir.assign %11 to %1#0 : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
hlfir.destroy %11 : !hlfir.expr<!fir.char<1,?>>
- hlfir.destroy %9 : !hlfir.expr<!fir.char<1,?>>
return
}
@@ -92,60 +97,52 @@ func.func @concat_chained(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2:
// CHECK: %[[VAL_10:.*]]:2 = fir.unboxchar %[[VAL_3]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
// CHECK: %[[VAL_11:.*]]:2 = hlfir.declare %[[VAL_10]]#0 typeparams %[[VAL_10]]#1 uniq_name("c4") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_6]]#1, %[[VAL_8]]#1 : index
-// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_6]]#1, %[[VAL_8]]#1 : index
+// CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_12]], %[[VAL_10]]#1 : index
// CHECK: %[[VAL_14:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_13]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[VAL_58:.*]] = arith.constant 0 : index
+// CHECK: %[[VAL_59:.*]] = arith.constant 1 : index
+// CHECK: %[[VAL_60:.*]] = arith.addi %[[VAL_58]], %[[VAL_59]] : index
+// CHECK: %[[VAL_61:.*]] = arith.subi %[[VAL_60]], %[[VAL_59]] : index
+// CHECK: %[[VAL_62:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_63:.*]] = fir.coordinate_of %[[VAL_62]], %[[VAL_61]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_64:.*]] = fir.convert %[[VAL_63]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
// CHECK: %[[VAL_15:.*]] = arith.constant 1 : i64
// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_6]]#1 : (index) -> i64
// CHECK: %[[VAL_17:.*]] = arith.muli %[[VAL_15]], %[[VAL_16]] : i64
-// CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_64]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_7]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
// CHECK: "llvm.intr.memmove"(%[[VAL_18]], %[[VAL_19]], %[[VAL_17]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
-// CHECK: %[[VAL_20:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_21:.*]] = arith.subi %[[VAL_13]], %[[VAL_20]] : index
-// CHECK: fir.do_loop %[[VAL_22:.*]] = %[[VAL_6]]#1 to %[[VAL_21]] step %[[VAL_20]] {
-// CHECK: %[[VAL_23:.*]] = arith.subi %[[VAL_22]], %[[VAL_6]]#1 : index
-// CHECK: %[[VAL_24:.*]] = fir.convert %[[VAL_9]]#1 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_25:.*]] = fir.coordinate_of %[[VAL_24]], %[[VAL_23]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_26:.*]] = fir.load %[[VAL_25]] : !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_27:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_28:.*]] = fir.coordinate_of %[[VAL_27]], %[[VAL_22]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: fir.store %[[VAL_26]] to %[[VAL_28]] : !fir.ref<!fir.char<1>>
-// CHECK: }
+// CHECK: %[[VAL_65:.*]] = arith.addi %[[VAL_58]], %[[VAL_6]]#1 : index
+// CHECK: %[[VAL_66:.*]] = arith.addi %[[VAL_65]], %[[VAL_59]] : index
+// CHECK: %[[VAL_67:.*]] = arith.subi %[[VAL_66]], %[[VAL_59]] : index
+// CHECK: %[[VAL_68:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_69:.*]] = fir.coordinate_of %[[VAL_68]], %[[VAL_67]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_70:.*]] = fir.convert %[[VAL_69]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+// CHECK: %[[VAL_71:.*]] = arith.constant 1 : i64
+// CHECK: %[[VAL_72:.*]] = fir.convert %[[VAL_8]]#1 : (index) -> i64
+// CHECK: %[[VAL_73:.*]] = arith.muli %[[VAL_71]], %[[VAL_72]] : i64
+// CHECK: %[[VAL_74:.*]] = fir.convert %[[VAL_70]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_75:.*]] = fir.convert %[[VAL_9]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%[[VAL_74]], %[[VAL_75]], %[[VAL_73]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
+// CHECK: %[[VAL_76:.*]] = arith.addi %[[VAL_65]], %[[VAL_8]]#1 : index
+// CHECK: %[[VAL_77:.*]] = arith.addi %[[VAL_76]], %[[VAL_59]] : index
+// CHECK: %[[VAL_78:.*]] = arith.subi %[[VAL_77]], %[[VAL_59]] : index
+// CHECK: %[[VAL_79:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
+// CHECK: %[[VAL_80:.*]] = fir.coordinate_of %[[VAL_79]], %[[VAL_78]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
+// CHECK: %[[VAL_81:.*]] = fir.convert %[[VAL_80]] : (!fir.ref<!fir.char<1>>) -> !fir.ref<!fir.char<1,?>>
+// CHECK: %[[VAL_82:.*]] = arith.constant 1 : i64
+// CHECK: %[[VAL_83:.*]] = fir.convert %[[VAL_10]]#1 : (index) -> i64
+// CHECK: %[[VAL_84:.*]] = arith.muli %[[VAL_82]], %[[VAL_83]] : i64
+// CHECK: %[[VAL_85:.*]] = fir.convert %[[VAL_81]] : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: %[[VAL_86:.*]] = fir.convert %[[VAL_11]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%[[VAL_85]], %[[VAL_86]], %[[VAL_84]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
// CHECK: %[[VAL_14_REF:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,?>>
// CHECK: %[[VAL_29:.*]]:2 = hlfir.declare %[[VAL_14_REF]] typeparams %[[VAL_13]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_30:.*]] = arith.constant true
// CHECK: %[[VAL_31:.*]] = fir.undefined tuple<!fir.boxchar<1>, i1>
// CHECK: %[[VAL_32:.*]] = fir.insert_value %[[VAL_31]], %[[VAL_30]], [1 : index] : (tuple<!fir.boxchar<1>, i1>, i1) -> tuple<!fir.boxchar<1>, i1>
// CHECK: %[[VAL_33:.*]] = fir.insert_value %[[VAL_32]], %[[VAL_29]]#0, [0 : index] : (tuple<!fir.boxchar<1>, i1>, !fir.boxchar<1>) -> tuple<!fir.boxchar<1>, i1>
-// CHECK: %[[VAL_34:.*]] = arith.addi %[[VAL_12]], %[[VAL_10]]#1 : index
-// CHECK: %[[VAL_35:.*]] = arith.addi %[[VAL_13]], %[[VAL_10]]#1 : index
-// CHECK: %[[VAL_36:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_35]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
-// CHECK: %[[VAL_37:.*]] = arith.constant 1 : i64
-// CHECK: %[[VAL_38:.*]] = fir.convert %[[VAL_13]] : (index) -> i64
-// CHECK: %[[VAL_39:.*]] = arith.muli %[[VAL_37]], %[[VAL_38]] : i64
-// CHECK: %[[VAL_40:.*]] = fir.convert %[[VAL_36]] : (!fir.heap<!fir.char<1,?>>) -> !llvm.ptr
-// CHECK: %[[VAL_41:.*]] = fir.convert %[[VAL_29]]#1 : (!fir.ref<!fir.char<1,?>>) -> !llvm.ptr
-// CHECK: "llvm.intr.memmove"(%[[VAL_40]], %[[VAL_41]], %[[VAL_39]]) <{isVolatile = false}> : (!llvm.ptr, !llvm.ptr, i64) -> ()
-// CHECK: %[[VAL_42:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_43:.*]] = arith.subi %[[VAL_35]], %[[VAL_42]] : index
-// CHECK: fir.do_loop %[[VAL_44:.*]] = %[[VAL_13]] to %[[VAL_43]] step %[[VAL_42]] {
-// CHECK: %[[VAL_45:.*]] = arith.subi %[[VAL_44]], %[[VAL_13]] : index
-// CHECK: %[[VAL_46:.*]] = fir.convert %[[VAL_11]]#1 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_47:.*]] = fir.coordinate_of %[[VAL_46]], %[[VAL_45]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_48:.*]] = fir.load %[[VAL_47]] : !fir.ref<!fir.char<1>>
-// CHECK: %[[VAL_49:.*]] = fir.convert %[[VAL_36]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.array<?x!fir.char<1>>>
-// CHECK: %[[VAL_50:.*]] = fir.coordinate_of %[[VAL_49]], %[[VAL_44]] : (!fir.ref<!fir.array<?x!fir.char<1>>>, index) -> !fir.ref<!fir.char<1>>
-// CHECK: fir.store %[[VAL_48]] to %[[VAL_50]] : !fir.ref<!fir.char<1>>
-// CHECK: }
-// CHECK: %[[VAL_36_REF:.*]] = fir.convert %[[VAL_36]] : (!fir.heap<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,?>>
-// CHECK: %[[VAL_51:.*]]:2 = hlfir.declare %[[VAL_36_REF]] typeparams %[[VAL_35]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
-// CHECK: %[[VAL_52:.*]] = arith.constant true
-// CHECK: %[[VAL_53:.*]] = fir.undefined tuple<!fir.boxchar<1>, i1>
-// CHECK: %[[VAL_54:.*]] = fir.insert_value %[[VAL_53]], %[[VAL_52]], [1 : index] : (tuple<!fir.boxchar<1>, i1>, i1) -> tuple<!fir.boxchar<1>, i1>
-// CHECK: %[[VAL_55:.*]] = fir.insert_value %[[VAL_54]], %[[VAL_51]]#0, [0 : index] : (tuple<!fir.boxchar<1>, i1>, !fir.boxchar<1>) -> tuple<!fir.boxchar<1>, i1>
-// CHECK: hlfir.assign %[[VAL_51]]#0 to %[[VAL_5]]#0 : !fir.boxchar<1>, !fir.boxchar<1>
-// CHECK: %[[VAL_56:.*]] = fir.box_addr %[[VAL_51]]#0 : (!fir.boxchar<1>) -> !fir.heap<!fir.char<1,?>>
-// CHECK: fir.freemem %[[VAL_56]] : !fir.heap<!fir.char<1,?>>
+// CHECK: hlfir.assign %[[VAL_29]]#0 to %[[VAL_5]]#0 : !fir.boxchar<1>, !fir.boxchar<1>
// CHECK: %[[VAL_57:.*]] = fir.box_addr %[[VAL_29]]#0 : (!fir.boxchar<1>) -> !fir.heap<!fir.char<1,?>>
// CHECK: fir.freemem %[[VAL_57]] : !fir.heap<!fir.char<1,?>>
// CHECK: return
diff --git a/flang/test/HLFIR/elemental-codegen.fir b/flang/test/HLFIR/elemental-codegen.fir
index d7984fe43e1986..19833d3388771b 100644
--- a/flang/test/HLFIR/elemental-codegen.fir
+++ b/flang/test/HLFIR/elemental-codegen.fir
@@ -66,6 +66,7 @@ func.func @char_type(%arg0: !fir.box<!fir.array<?x!fir.char<1,?>>>, %arg1: index
// CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
// CHECK: fir.do_loop %[[VAL_9:.*]] = %[[VAL_8]] to %[[VAL_1]] step %[[VAL_8]] {
// CHECK: %[[VAL_10:.*]] = hlfir.designate %[[VAL_0]] (%[[VAL_9]]) typeparams %[[VAL_3]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index, index) -> !fir.boxchar<1>
+// CHECK: %[[VAL_57:.*]] = fir.allocmem !fir.char<1,?>(%[[VAL_2]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
// concatenation
// CHECK: %[[VAL_30:.*]]:2 = hlfir.declare %[[VAL_14:.*]] typeparams %[[VAL_13:.*]] uniq_name("tmp") : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
// CHECK: %[[VAL_31:.*]] = arith.constant true
diff --git a/flang/test/HLFIR/get_length_codegen.fir b/flang/test/HLFIR/get_length_codegen.fir
index c9e007b3593e02..547b6e7a9f87e5 100644
--- a/flang/test/HLFIR/get_length_codegen.fir
+++ b/flang/test/HLFIR/get_length_codegen.fir
@@ -26,7 +26,7 @@ fir.global linkonce @_QQclX616263 constant : !fir.char<1,3> {
// CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1> {fir.bindc_name = "ch"}) -> index {
// CHECK: %[[VAL_1:.*]] = arith.constant 3 : index
// CHECK: %[[VAL_2:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
-// CHECK: %[[VAL_9:.*]] = arith.addi %[[VAL_1]], %[[VAL_2]]#1 : index
+// CHECK: %[[VAL_9:.*]] = arith.addi %[[VAL_2]]#1, %[[VAL_1]] : index
// CHECK: %[[VAL_33:.*]]:2 = hlfir.declare %[[VAL_31:.*]] typeparams %[[VAL_9]] uniq_name(".tmp") : (!fir.heap<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.heap<!fir.char<1,?>>)
// CHECK: return %[[VAL_9]] : index
// CHECK: }
diff --git a/flang/test/Integration/concat-chain-buffer.f90 b/flang/test/Integration/concat-chain-buffer.f90
new file mode 100644
index 00000000000000..2130b3ce88d022
--- /dev/null
+++ b/flang/test/Integration/concat-chain-buffer.f90
@@ -0,0 +1,14 @@
+! Test that a chain of concatenations is evaluated into a single buffer.
+! RUN: %flang_fc1 -emit-llvm -O2 -o - %s | FileCheck %s
+
+subroutine test(a, b, c)
+ character(*) :: a, b, c
+ call foo(a // b // c)
+end subroutine
+! CHECK-LABEL: define void @test_(
+! CHECK: alloca i8, i64
+! CHECK-NOT: alloca i8, i64
+! CHECK-NOT: malloc
+! CHECK: call void @foo_(
+! CHECK-NOT: free
+! CHECK: ret void
diff --git a/flang/test/Transforms/character-expression-storage.f90 b/flang/test/Transforms/character-expression-storage.f90
index eb406eda9c0244..c65974c7019e8d 100644
--- a/flang/test/Transforms/character-expression-storage.f90
+++ b/flang/test/Transforms/character-expression-storage.f90
@@ -1,4 +1,5 @@
! RUN: %flang_fc1 -emit-fir %s -o - | fir-opt --canonicalize --allocation-placement | FileCheck %s
+! RUN: %flang_fc1 -O1 -emit-fir %s -o - | fir-opt --canonicalize --allocation-placement | FileCheck %s --check-prefix=FUSED
! RUN: %flang_fc1 -emit-hlfir %s -o - | fir-opt --inline-elementals | FileCheck %s --check-prefix=INLINE
! RUN: %flang_fc1 -emit-hlfir %s -o - | fir-opt --lower-hlfir-ordered-assignments | FileCheck %s --check-prefix=WHERE
! RUN: %flang_fc1 -emit-hlfir %s -o - | fir-opt --inline-elementals --opt-bufferization | FileCheck %s --check-prefix=OPT
@@ -22,20 +23,19 @@ subroutine scalar_loop(s, n)
! CHECK-NOT: fir.allocmem !fir.char
! CHECK: return
-! After elemental fusion, the scalar scratch buffers of an element are
-! reclaimed before the next iteration.
+! Elemental fusion and concatenation fusion must retain one scalar scratch
+! buffer, whose invariant length allows allocation outside the element loop.
subroutine concat_array(a, b, c)
character(*) :: a(:), b(:), c(:)
c = a // b // '!'
end subroutine
-! CHECK-LABEL: func.func @_QPconcat_array
-! CHECK: fir.do_loop
-! CHECK: llvm.intr.stacksave
-! CHECK: fir.alloca !fir.char<1,?>
-! CHECK: fir.alloca !fir.char<1,?>
-! CHECK: llvm.intr.stackrestore
-! CHECK-NOT: fir.allocmem !fir.char
-! CHECK: return
+! FUSED-LABEL: func.func @_QPconcat_array
+! FUSED: fir.alloca !fir.char<1,?>
+! FUSED-NOT: fir.alloca !fir.char
+! FUSED: fir.do_loop
+! FUSED-NOT: fir.alloca !fir.char
+! FUSED-NOT: fir.allocmem !fir.char
+! FUSED: return
! The concatenation becomes an intermediate scalar expression when the
! elementals are fused. It needs cleanup even though it is not yielded.
>From 2dbd5820f09ead06269b646a16160488ad4edadd Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Thu, 12 Nov 2026 10:31:15 -0800
Subject: [PATCH 2/2] [flang] Address review comments on chained concatenation
fusion
Let concatenation fusion cross operations that only allocate, like the
hlfir.declare of a character literal operand, so that chains ending with a
literal are fused into one buffer. Only operations that may write or free
memory, or whose effects are unknown, now prevent the fusion.
Remove CharacterExprHelper::createConcatenate, which no longer has users.
Add bufferization tests for concatenations whose operands all have a
compile-time length, and for character kind 4.
---
.../flang/Optimizer/Builder/Character.h | 4 --
flang/lib/Optimizer/Builder/Character.cpp | 25 -------
flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp | 6 +-
.../HLFIR/character-expression-lifetimes.fir | 27 ++++++++
flang/test/HLFIR/concat-bufferization.fir | 68 +++++++++++++++++++
5 files changed, 99 insertions(+), 31 deletions(-)
diff --git a/flang/include/flang/Optimizer/Builder/Character.h b/flang/include/flang/Optimizer/Builder/Character.h
index 3f0aa05492a538..a3185146629535 100644
--- a/flang/include/flang/Optimizer/Builder/Character.h
+++ b/flang/include/flang/Optimizer/Builder/Character.h
@@ -63,10 +63,6 @@ class CharacterExprHelper {
void createAssign(const fir::ExtendedValue &lhs,
const fir::ExtendedValue &rhs);
- /// Create lhs // rhs in temp obtained with fir.alloca
- fir::CharBoxValue createConcatenate(const fir::CharBoxValue &lhs,
- const fir::CharBoxValue &rhs);
-
/// Create {max,min}(lhs,rhs) in a stack or heap temporary.
fir::CharBoxValue createCharExtremum(bool predIsMin,
llvm::ArrayRef<fir::CharBoxValue> opCBVs,
diff --git a/flang/lib/Optimizer/Builder/Character.cpp b/flang/lib/Optimizer/Builder/Character.cpp
index 24d10be865932d..57277baaf29d53 100644
--- a/flang/lib/Optimizer/Builder/Character.cpp
+++ b/flang/lib/Optimizer/Builder/Character.cpp
@@ -486,31 +486,6 @@ void fir::factory::CharacterExprHelper::createAssign(
}
}
-fir::CharBoxValue fir::factory::CharacterExprHelper::createConcatenate(
- const fir::CharBoxValue &lhs, const fir::CharBoxValue &rhs) {
- auto lhsLen = builder.createConvert(loc, builder.getCharacterLengthType(),
- lhs.getLen());
- auto rhsLen = builder.createConvert(loc, builder.getCharacterLengthType(),
- rhs.getLen());
- mlir::Value len = mlir::arith::AddIOp::create(builder, loc, lhsLen, rhsLen);
- auto temp = createCharacterTemp(getCharacterType(rhs), len);
- createCopy(temp, lhs, lhsLen);
- auto one = builder.createIntegerConstant(loc, len.getType(), 1);
- auto upperBound = mlir::arith::SubIOp::create(builder, loc, len, one);
- auto lhsLenIdx = builder.createConvert(loc, builder.getIndexType(), lhsLen);
- auto fromBuff = getCharBoxBuffer(rhs);
- auto toBuff = getCharBoxBuffer(temp);
- fir::factory::DoLoopHelper{builder, loc}.createLoop(
- lhsLenIdx, upperBound, one,
- [&](fir::FirOpBuilder &bldr, mlir::Value index) {
- auto rhsIndex =
- mlir::arith::SubIOp::create(bldr, loc, index, lhsLenIdx);
- auto charVal = createLoadCharAt(fromBuff, rhsIndex);
- createStoreCharAt(toBuff, index, charVal);
- });
- return temp;
-}
-
mlir::Value fir::factory::CharacterExprHelper::genSubstringBase(
mlir::Value stringRawAddr, mlir::Value lowerBound,
mlir::Type substringAddrType, mlir::Value one) {
diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
index 87929e2e0f04e6..96e9bcd394d7ed 100644
--- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -798,7 +798,8 @@ getCharacterLengthIfStatic(mlir::Type t) {
namespace {
/// Fuse single-use concatenations before allocating their buffers. The reads
/// of the inner operands move to the outer concatenation, so do not cross any
-/// operation that may change or release their storage.
+/// operation that may change or release their storage. Allocations, like the
+/// hlfir.declare of a literal operand, are fine to cross.
struct InlineConcat : public mlir::OpRewritePattern<hlfir::ConcatOp> {
using OpRewritePattern::OpRewritePattern;
llvm::LogicalResult
@@ -823,7 +824,8 @@ struct InlineConcat : public mlir::OpRewritePattern<hlfir::ConcatOp> {
op = op->getNextNode()) {
auto effects = mlir::getEffectsRecursively(op);
if (!effects || llvm::any_of(*effects, [](const auto &effect) {
- return !mlir::isa<mlir::MemoryEffects::Read>(effect.getEffect());
+ return mlir::isa<mlir::MemoryEffects::Write,
+ mlir::MemoryEffects::Free>(effect.getEffect());
})) {
canMove = false;
break;
diff --git a/flang/test/HLFIR/character-expression-lifetimes.fir b/flang/test/HLFIR/character-expression-lifetimes.fir
index cb6f64f98ced08..0c358087cde2fe 100644
--- a/flang/test/HLFIR/character-expression-lifetimes.fir
+++ b/flang/test/HLFIR/character-expression-lifetimes.fir
@@ -29,6 +29,33 @@ func.func @chain(%a: !fir.boxchar<1>, %b: !fir.boxchar<1>, %c: !fir.boxchar<1>,
return
}
+// The declaration of a literal operand between the concatenations does not
+// prevent fusion.
+// CANON-LABEL: func.func @chain_literal
+// CANON: %[[LIT:.*]]:2 = hlfir.declare
+// CANON: %[[JOINED:.*]] = hlfir.concat %arg0, %arg1, %[[LIT]]#0
+// CANON-NOT: hlfir.concat
+// CANON: hlfir.assign %[[JOINED]]
+// CHECK-LABEL: func.func @chain_literal
+// CHECK: fir.allocmem !fir.char<1,?>
+// CHECK-NOT: fir.allocmem
+// CHECK: return
+func.func @chain_literal(%a: !fir.boxchar<1>, %b: !fir.boxchar<1>, %out: !fir.boxchar<1>, %n: index, %m: index) {
+ %c1 = arith.constant 1 : index
+ %x = hlfir.concat %a, %b len %n : (!fir.boxchar<1>, !fir.boxchar<1>, index) -> !hlfir.expr<!fir.char<1,?>>
+ %addr = fir.address_of(@_QQclX21) : !fir.ref<!fir.char<1>>
+ %lit:2 = hlfir.declare %addr typeparams %c1 uniq_name("_QQclX21") fortran_attrs<parameter> : (!fir.ref<!fir.char<1>>, index) -> (!fir.ref<!fir.char<1>>, !fir.ref<!fir.char<1>>)
+ %y = hlfir.concat %x, %lit#0 len %m : (!hlfir.expr<!fir.char<1,?>>, !fir.ref<!fir.char<1>>, index) -> !hlfir.expr<!fir.char<1,?>>
+ hlfir.assign %y to %out : !hlfir.expr<!fir.char<1,?>>, !fir.boxchar<1>
+ hlfir.destroy %y : !hlfir.expr<!fir.char<1,?>>
+ hlfir.destroy %x : !hlfir.expr<!fir.char<1,?>>
+ return
+}
+fir.global linkonce @_QQclX21 constant : !fir.char<1> {
+ %0 = fir.string_lit "!"(1) : !fir.char<1>
+ fir.has_value %0 : !fir.char<1>
+}
+
// Fusion must not postpone reading A past a call that may modify A.
// CANON-LABEL: func.func @snapshot
// CANON: hlfir.concat
diff --git a/flang/test/HLFIR/concat-bufferization.fir b/flang/test/HLFIR/concat-bufferization.fir
index b99af4351e08bb..bfb58e7fc5aa0b 100644
--- a/flang/test/HLFIR/concat-bufferization.fir
+++ b/flang/test/HLFIR/concat-bufferization.fir
@@ -147,3 +147,71 @@ func.func @concat_chained(%arg0: !fir.boxchar<1>, %arg1: !fir.boxchar<1>, %arg2:
// CHECK: fir.freemem %[[VAL_57]] : !fir.heap<!fir.char<1,?>>
// CHECK: return
// CHECK: }
+
+// When every operand has a compile-time length, the buffer is on the stack.
+func.func @concat_constant_len(%arg0: !fir.ref<!fir.char<1,3>>, %arg1: !fir.ref<!fir.char<1,2>>, %arg2: !fir.ref<!fir.char<1,5>>) {
+ %c5 = arith.constant 5 : index
+ %0 = hlfir.concat %arg0, %arg1 len %c5 : (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,2>>, index) -> !hlfir.expr<!fir.char<1,5>>
+ hlfir.assign %0 to %arg2 : !hlfir.expr<!fir.char<1,5>>, !fir.ref<!fir.char<1,5>>
+ hlfir.destroy %0 : !hlfir.expr<!fir.char<1,5>>
+ return
+}
+// CHECK-LABEL: func.func @concat_constant_len(
+// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.char<1,3>>, %[[ARG1:.*]]: !fir.ref<!fir.char<1,2>>, %[[ARG2:.*]]: !fir.ref<!fir.char<1,5>>) {
+// CHECK-NOT: fir.allocmem
+// CHECK: %[[TEMP:.*]] = fir.alloca !fir.char<1,5> <{bindc_name = ".chrtmp"}>
+// CHECK: %[[C3:.*]] = arith.constant 3 : index
+// CHECK: %[[LEN0:.*]] = fir.convert %[[C3]] : (index) -> i64
+// CHECK: %[[SIZE0:.*]] = arith.muli %{{.*}}, %[[LEN0]] : i64
+// CHECK: %[[SRC0:.*]] = fir.convert %[[ARG0]] : (!fir.ref<!fir.char<1,3>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%{{.*}}, %[[SRC0]], %[[SIZE0]])
+// CHECK: %[[OFFSET:.*]] = arith.addi %{{.*}}, %[[C3]] : index
+// CHECK: %[[C2:.*]] = arith.constant 2 : index
+// CHECK: %[[LOWER:.*]] = arith.addi %[[OFFSET]], %{{.*}} : index
+// CHECK: %[[INDEX:.*]] = arith.subi %[[LOWER]], %{{.*}} : index
+// CHECK: %[[ARRAY:.*]] = fir.convert %[[TEMP]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<!fir.array<5x!fir.char<1>>>
+// CHECK: fir.coordinate_of %[[ARRAY]], %[[INDEX]]
+// CHECK: %[[LEN1:.*]] = fir.convert %[[C2]] : (index) -> i64
+// CHECK: %[[SIZE1:.*]] = arith.muli %{{.*}}, %[[LEN1]] : i64
+// CHECK: %[[SRC1:.*]] = fir.convert %[[ARG1]] : (!fir.ref<!fir.char<1,2>>) -> !llvm.ptr
+// CHECK: "llvm.intr.memmove"(%{{.*}}, %[[SRC1]], %[[SIZE1]])
+// CHECK: %[[RES:.*]]:2 = hlfir.declare %[[TEMP]] typeparams %{{.*}} uniq_name("tmp") : (!fir.ref<!fir.char<1,5>>, index) -> (!fir.ref<!fir.char<1,5>>, !fir.ref<!fir.char<1,5>>)
+// CHECK: %[[FALSE:.*]] = arith.constant false
+// CHECK: fir.insert_value %{{.*}}, %[[FALSE]], [1 : index]
+// CHECK: hlfir.assign %[[RES]]#0 to %[[ARG2]] : !fir.ref<!fir.char<1,5>>, !fir.ref<!fir.char<1,5>>
+// CHECK-NOT: fir.freemem
+// CHECK: return
+
+// Offsets are in characters, copy sizes are in bytes.
+func.func @concat_kind4(%arg0: !fir.boxchar<4>, %arg1: !fir.boxchar<4>, %arg2: !fir.boxchar<4>) {
+ %0:2 = fir.unboxchar %arg0 : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+ %1:2 = fir.unboxchar %arg1 : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+ %2 = arith.addi %0#1, %1#1 : index
+ %3 = hlfir.concat %arg0, %arg1 len %2 : (!fir.boxchar<4>, !fir.boxchar<4>, index) -> !hlfir.expr<!fir.char<4,?>>
+ hlfir.assign %3 to %arg2 : !hlfir.expr<!fir.char<4,?>>, !fir.boxchar<4>
+ hlfir.destroy %3 : !hlfir.expr<!fir.char<4,?>>
+ return
+}
+// CHECK-LABEL: func.func @concat_kind4(
+// CHECK: %[[LEN:.*]] = arith.addi %{{.*}}, %{{.*}} : index
+// CHECK: %[[TEMP:.*]] = fir.allocmem !fir.char<4,?>(%[[LEN]] : index) <{bindc_name = ".chrtmp", uniq_name = ""}>
+// CHECK: %[[STR0:.*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+// CHECK: %[[ARRAY0:.*]] = fir.convert %[[TEMP]] : (!fir.heap<!fir.char<4,?>>) -> !fir.ref<!fir.array<?x!fir.char<4>>>
+// CHECK: fir.coordinate_of %[[ARRAY0]], %{{.*}} : (!fir.ref<!fir.array<?x!fir.char<4>>>, index) -> !fir.ref<!fir.char<4>>
+// CHECK: %[[KIND0:.*]] = arith.constant 4 : i64
+// CHECK: %[[LEN0:.*]] = fir.convert %[[STR0]]#1 : (index) -> i64
+// CHECK: %[[SIZE0:.*]] = arith.muli %[[KIND0]], %[[LEN0]] : i64
+// CHECK: "llvm.intr.memmove"(%{{.*}}, %{{.*}}, %[[SIZE0]])
+// CHECK: %[[OFFSET:.*]] = arith.addi %{{.*}}, %[[STR0]]#1 : index
+// CHECK: %[[STR1:.*]]:2 = fir.unboxchar %{{.*}} : (!fir.boxchar<4>) -> (!fir.ref<!fir.char<4,?>>, index)
+// CHECK: %[[LOWER:.*]] = arith.addi %[[OFFSET]], %{{.*}} : index
+// CHECK: %[[INDEX:.*]] = arith.subi %[[LOWER]], %{{.*}} : index
+// CHECK: %[[ARRAY1:.*]] = fir.convert %[[TEMP]] : (!fir.heap<!fir.char<4,?>>) -> !fir.ref<!fir.array<?x!fir.char<4>>>
+// CHECK: fir.coordinate_of %[[ARRAY1]], %[[INDEX]] : (!fir.ref<!fir.array<?x!fir.char<4>>>, index) -> !fir.ref<!fir.char<4>>
+// CHECK: %[[KIND1:.*]] = arith.constant 4 : i64
+// CHECK: %[[LEN1:.*]] = fir.convert %[[STR1]]#1 : (index) -> i64
+// CHECK: %[[SIZE1:.*]] = arith.muli %[[KIND1]], %[[LEN1]] : i64
+// CHECK: "llvm.intr.memmove"(%{{.*}}, %{{.*}}, %[[SIZE1]])
+// CHECK: hlfir.declare %{{.*}} typeparams %[[LEN]] uniq_name("tmp") : (!fir.ref<!fir.char<4,?>>, index) -> (!fir.boxchar<4>, !fir.ref<!fir.char<4,?>>)
+// CHECK: fir.freemem
+// CHECK: return
More information about the llvm-branch-commits
mailing list