[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
Thu Nov 12 10:46:37 PST 2026
================
@@ -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());
+ })) {
----------------
jeanPerier wrote:
Good catch, thanks. The scan now only rejects Write and Free effects, and still bails out on ops with unknown effects, so the literal's hlfir.declare no longer blocks fusion. `a // "-" // b // "!"` now becomes a single four-operand hlfir.concat, and I added a @chain_literal test in character-expression-lifetimes.fir.
https://github.com/llvm/llvm-project/pull/239331
More information about the llvm-branch-commits
mailing list