[llvm] [LoopInterchange] Reject if loops contain indirectbr instruction (PR #203836)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 15 00:03:52 PDT 2026
https://github.com/kasuga-fj created https://github.com/llvm/llvm-project/pull/203836
In the transformation phase of LoopInterchange, it may call `InsertPreheaderForLoop` with loop headers to create new loop preheaders. However, if any of their predecessors branch to them via an `indirectbr`, the insertion of a preheader will fail, causing the entire program to crash.
This patch adds an additional check in the legality check so that the loops containing an `indirectbr` are now rejected. This validation would be too conservative, especially it would be sufficientto ensure that the predecessors of outer and inner loop headers don't contain an `indirectbr`. However, since I believe the use of this instruction is rare in the practical cases where loop-interchange is beneficial, I chose a simpler implementation for now.
Fixes #203664.
>From d219f33a8fb5c2c52eac5335d10a01d622a67b67 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Mon, 15 Jun 2026 06:50:36 +0000
Subject: [PATCH] [LoopInterchange] Reject if loops contain indirectbr
instruction
---
.../lib/Transforms/Scalar/LoopInterchange.cpp | 18 +++
.../Transforms/LoopInterchange/indirectbr.ll | 114 ++++++++++++++++++
2 files changed, 132 insertions(+)
create mode 100644 llvm/test/Transforms/LoopInterchange/indirectbr.ll
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index ca278ee565a20..8db420b1c18f7 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1517,6 +1517,24 @@ bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
if (isa<LoadInst, StoreInst, PseudoProbeInst>(&I))
continue;
+ // We currently do not support interchanging loops with indirectbr
+ // instructions. This is because indirectbr can prevent inserting new loop
+ // preheaders, which is required in the transformation phase.
+ //
+ // TODO: This is conservative, we may be able to support some cases, e.g.,
+ // when the indirectbr is in the inner loop body, not in the outer/inner
+ // loop header.
+ if (isa<IndirectBrInst>(&I)) {
+ LLVM_DEBUG(dbgs() << "Loops contain indirect branch instructions.\n");
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "IndirectBranch",
+ I.getDebugLoc(), I.getParent())
+ << "Cannot interchange loops due to indirect branch "
+ "instruction.";
+ });
+ return false;
+ }
+
// We cannot ignore potential memory reads, e.g., loads inside the called
// function.
if (!I.mayHaveSideEffects() && !I.mayReadFromMemory())
diff --git a/llvm/test/Transforms/LoopInterchange/indirectbr.ll b/llvm/test/Transforms/LoopInterchange/indirectbr.ll
new file mode 100644
index 0000000000000..522c8738cf1e8
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/indirectbr.ll
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore -S | FileCheck %s
+
+; When transforming loops, loop-interchange will insert new prehader blocks for
+; the outer/inner loop, but in this case it will fail due to their predecessor
+; blocks contain indirectbr instructions. Therefore we canoot interchange the
+; loops.
+;
+define void @indirect0(ptr %A) {
+; CHECK-LABEL: define void @indirect0(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: indirectbr ptr blockaddress(@indirect0, %[[OUTER_HEADER:.*]]), [label %[[OUTER_HEADER]]]
+; CHECK: [[OUTER_HEADER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_INC:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT: indirectbr ptr blockaddress(@indirect0, %[[INNER:.*]]), [label %[[INNER]]]
+; CHECK: [[INNER]]:
+; CHECK-NEXT: [[J:%.*]] = phi i64 [ 0, %[[OUTER_HEADER]] ], [ [[J_INC:%.*]], %[[INNER]] ]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i8], ptr [[A]], i64 [[J]], i64 [[I]]
+; CHECK-NEXT: store i8 0, ptr [[GEP]], align 1
+; CHECK-NEXT: [[J_INC]] = add i64 [[J]], 1
+; CHECK-NEXT: [[EC_J:%.*]] = icmp eq i64 [[J_INC]], 4
+; CHECK-NEXT: br i1 [[EC_J]], label %[[OUTER_LATCH]], label %[[INNER]]
+; CHECK: [[OUTER_LATCH]]:
+; CHECK-NEXT: [[I_INC]] = add i64 [[I]], 1
+; CHECK-NEXT: [[EC_I:%.*]] = icmp eq i64 [[I_INC]], 4
+; CHECK-NEXT: br i1 [[EC_I]], label %[[EXIT:.*]], label %[[OUTER_HEADER]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ indirectbr ptr blockaddress(@indirect0, %outer.header), [ label %outer.header ]
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.inc, %outer.latch ]
+ indirectbr ptr blockaddress(@indirect0, %inner), [ label %inner ]
+
+inner:
+ %j = phi i64 [ 0, %outer.header ], [ %j.inc, %inner ]
+ %gep = getelementptr inbounds [4 x i8], ptr %A, i64 %j, i64 %i
+ store i8 0, ptr %gep
+ %j.inc = add i64 %j, 1
+ %ec.j = icmp eq i64 %j.inc, 4
+ br i1 %ec.j, label %outer.latch, label %inner
+
+outer.latch:
+ %i.inc = add i64 %i, 1
+ %ec.i = icmp eq i64 %i.inc, 4
+ br i1 %ec.i, label %exit, label %outer.header
+
+exit:
+ ret void
+}
+
+; This is semantically equivalent to the above case, but the outer loop header
+; uses a br instruction so theoretically we can perform loop interchange.
+; However, we now check the presence of indirectbr instrucction conservatively
+; and reject these loops as well.
+;
+define void @indirect1(ptr %A) {
+; CHECK-LABEL: define void @indirect1(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[OUTER_HEADER:.*]]
+; CHECK: [[OUTER_HEADER]]:
+; CHECK-NEXT: [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_INC:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT: br label %[[INNER_HEADER:.*]]
+; CHECK: [[INNER_HEADER]]:
+; CHECK-NEXT: [[J:%.*]] = phi i64 [ 0, %[[OUTER_HEADER]] ], [ [[J_INC:%.*]], %[[INNER_LATCH:.*]] ]
+; CHECK-NEXT: indirectbr ptr blockaddress(@indirect1, %[[INNER_BODY:.*]]), [label %[[INNER_BODY]]]
+; CHECK: [[INNER_BODY]]:
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i8], ptr [[A]], i64 [[J]], i64 [[I]]
+; CHECK-NEXT: store i8 0, ptr [[GEP]], align 1
+; CHECK-NEXT: indirectbr ptr blockaddress(@indirect1, %[[INNER_LATCH]]), [label %[[INNER_LATCH]]]
+; CHECK: [[INNER_LATCH]]:
+; CHECK-NEXT: [[J_INC]] = add i64 [[J]], 1
+; CHECK-NEXT: [[EC_J:%.*]] = icmp eq i64 [[J_INC]], 4
+; CHECK-NEXT: br i1 [[EC_J]], label %[[OUTER_LATCH]], label %[[INNER_HEADER]]
+; CHECK: [[OUTER_LATCH]]:
+; CHECK-NEXT: [[I_INC]] = add i64 [[I]], 1
+; CHECK-NEXT: [[EC_I:%.*]] = icmp eq i64 [[I_INC]], 4
+; CHECK-NEXT: br i1 [[EC_I]], label %[[EXIT:.*]], label %[[OUTER_HEADER]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %outer.header
+
+outer.header:
+ %i = phi i64 [ 0, %entry ], [ %i.inc, %outer.latch ]
+ br label %inner.header
+
+inner.header:
+ %j = phi i64 [ 0, %outer.header ], [ %j.inc, %inner.latch ]
+ indirectbr ptr blockaddress(@indirect1, %inner.body), [ label %inner.body ]
+
+inner.body:
+ %gep = getelementptr inbounds [4 x i8], ptr %A, i64 %j, i64 %i
+ store i8 0, ptr %gep
+ indirectbr ptr blockaddress(@indirect1, %inner.latch), [ label %inner.latch ]
+
+inner.latch:
+ %j.inc = add i64 %j, 1
+ %ec.j = icmp eq i64 %j.inc, 4
+ br i1 %ec.j, label %outer.latch, label %inner.header
+
+outer.latch:
+ %i.inc = add i64 %i, 1
+ %ec.i = icmp eq i64 %i.inc, 4
+ br i1 %ec.i, label %exit, label %outer.header
+
+exit:
+ ret void
+}
More information about the llvm-commits
mailing list