[llvm] [OpenMPOpt] Bound indirect-call specialization instead of paying for all of it (PR #219323)

Larry Meadows via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 5 18:06:11 PDT 2026


https://github.com/lfmeadow updated https://github.com/llvm/llvm-project/pull/219323

>From 707ba18cbcb294d6d364b60c844eafe501130d34 Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Fri, 21 Aug 2026 22:21:29 -0500
Subject: [PATCH 1/2] [OpenMPOpt] Bound indirect-call specialization instead of
 paying for all of it

AAIndirectCallInfo replaces an indirect call with an if-cascade of direct
calls, one arm per assumed callee. Device code routes every parallel region
through a single runtime dispatcher, so one call site there can see every
outlined region in the module, and the cascade it builds is quadratic in a way
nothing later undoes. On a module with 64 target regions the device LTO
pipeline takes 30.6s, of which 24s is this; capping the cascade at three
callees brings it to 6.6s, and the call sites that keep their indirect call
drop from 392 to 203.

The cap is on the number of possible callees, so it is a property of the call
site rather than the module: a dispatcher that could go anywhere keeps its
indirect call and a call site with one or two known callees still gets direct
ones. A single known callee is promoted either way, which is the case that
pays for itself.

ROCm carries this region under `#if 0` for the same reason. A bound keeps the
specialization that is worth having and lets that go.

Co-authored-by: Cursor <cursoragent at cursor.com>
---
 llvm/lib/Transforms/IPO/OpenMPOpt.cpp         |  16 +++
 .../indirect_call_specialization_cap.ll       | 123 ++++++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100644 llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll

diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
index f02a41eeb3c6b..acbd2972a06a8 100644
--- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
+++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
@@ -5835,6 +5835,20 @@ AAFoldRuntimeCall &AAFoldRuntimeCall::createForPosition(const IRPosition &IRP,
   return *AA;
 }
 
+/// Bound the if-cascade AAIndirectCallInfo builds for an indirect call. Device
+/// code routes many parallel regions through a single runtime dispatcher, so a
+/// call site there can see every outlined region in the module; specializing
+/// all of them costs more in code size and compile time than the direct calls
+/// are worth.
+static constexpr unsigned MaxIndirectCallSpecializations = 3;
+
+static bool shouldSpecializeIndirectCallee(Attributor &,
+                                           const AbstractAttribute &,
+                                           CallBase &, Function &,
+                                           unsigned NumAssumedCallees) {
+  return NumAssumedCallees <= MaxIndirectCallSpecializations;
+}
+
 PreservedAnalyses OpenMPOptPass::run(Module &M, ModuleAnalysisManager &AM) {
   if (!containsOpenMP(M))
     return PreservedAnalyses::all();
@@ -5921,6 +5935,7 @@ PreservedAnalyses OpenMPOptPass::run(Module &M, ModuleAnalysisManager &AM) {
   AC.OREGetter = OREGetter;
   AC.PassName = DEBUG_TYPE;
   AC.InitializationCallback = OpenMPOpt::registerAAsForFunction;
+  AC.IndirectCalleeSpecializationCallback = shouldSpecializeIndirectCallee;
   AC.IPOAmendableCB = [](const Function &F) {
     return F.hasFnAttribute("kernel");
   };
@@ -6001,6 +6016,7 @@ PreservedAnalyses OpenMPOptCGSCCPass::run(LazyCallGraph::SCC &C,
   AC.OREGetter = OREGetter;
   AC.PassName = DEBUG_TYPE;
   AC.InitializationCallback = OpenMPOpt::registerAAsForFunction;
+  AC.IndirectCalleeSpecializationCallback = shouldSpecializeIndirectCallee;
 
   Attributor A(Functions, InfoCache, AC);
 
diff --git a/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll b/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll
new file mode 100644
index 0000000000000..862c4a815a57c
--- /dev/null
+++ b/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll
@@ -0,0 +1,123 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=openmp-opt -S < %s | FileCheck %s
+; The bound is on the number of possible callees, so it is a property of the call
+; site and not of the module: @three gets its if-cascade of direct calls and
+; @four keeps its indirect call.
+
+target triple = "amdgcn-amd-amdhsa"
+
+ at g = external global i32
+
+define internal void @c1() {
+;
+;
+;
+; CHECK-LABEL: define internal void @c1(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    store volatile i32 1, ptr @g, align 4
+; CHECK-NEXT:    ret void
+;
+  store volatile i32 1, ptr @g
+  ret void
+}
+
+define internal void @c2() {
+;
+;
+;
+; CHECK-LABEL: define internal void @c2(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT:    store volatile i32 2, ptr @g, align 4
+; CHECK-NEXT:    ret void
+;
+  store volatile i32 2, ptr @g
+  ret void
+}
+
+define internal void @c3() {
+;
+;
+;
+; CHECK-LABEL: define internal void @c3(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT:    store volatile i32 3, ptr @g, align 4
+; CHECK-NEXT:    ret void
+;
+  store volatile i32 3, ptr @g
+  ret void
+}
+
+define internal void @c4() {
+;
+;
+;
+; CHECK-LABEL: define internal void @c4(
+; CHECK-SAME: ) #[[ATTR0]] {
+; CHECK-NEXT:    store volatile i32 4, ptr @g, align 4
+; CHECK-NEXT:    ret void
+;
+  store volatile i32 4, ptr @g
+  ret void
+}
+
+define void @three(i1 %a, i1 %b) {
+;
+;
+;
+; CHECK-LABEL: define void @three(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT:    [[S0:%.*]] = select i1 [[A]], ptr @c1, ptr @c2
+; CHECK-NEXT:    [[S1:%.*]] = select i1 [[B]], ptr [[S0]], ptr @c3
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq ptr [[S1]], @c3
+; CHECK-NEXT:    br i1 [[TMP1]], label %[[BB2:.*]], label %[[BB3:.*]]
+; CHECK:       [[BB2]]:
+; CHECK-NEXT:    call void @c3()
+; CHECK-NEXT:    br label %[[BB9:.*]]
+; CHECK:       [[BB3]]:
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq ptr [[S1]], @c2
+; CHECK-NEXT:    br i1 [[TMP4]], label %[[BB5:.*]], label %[[BB6:.*]]
+; CHECK:       [[BB5]]:
+; CHECK-NEXT:    call void @c2()
+; CHECK-NEXT:    br label %[[BB9]]
+; CHECK:       [[BB6]]:
+; CHECK-NEXT:    br i1 true, label %[[BB7:.*]], label %[[BB8:.*]]
+; CHECK:       [[BB7]]:
+; CHECK-NEXT:    call void @c1()
+; CHECK-NEXT:    br label %[[BB9]]
+; CHECK:       [[BB8]]:
+; CHECK-NEXT:    unreachable
+; CHECK:       [[BB9]]:
+; CHECK-NEXT:    ret void
+;
+  %s0 = select i1 %a, ptr @c1, ptr @c2
+  %s1 = select i1 %b, ptr %s0, ptr @c3
+  call void %s1()
+  ret void
+}
+
+define void @four(i1 %a, i1 %b, i1 %c) {
+;
+;
+;
+; CHECK-LABEL: define void @four(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]], i1 [[C:%.*]]) {
+; CHECK-NEXT:    [[S0:%.*]] = select i1 [[A]], ptr @c1, ptr @c2
+; CHECK-NEXT:    [[S1:%.*]] = select i1 [[B]], ptr [[S0]], ptr @c3
+; CHECK-NEXT:    [[S2:%.*]] = select i1 [[C]], ptr [[S1]], ptr @c4
+; CHECK-NEXT:    call void [[S2]]() #[[ATTR0]], !callees [[META2:![0-9]+]]
+; CHECK-NEXT:    ret void
+;
+  %s0 = select i1 %a, ptr @c1, ptr @c2
+  %s1 = select i1 %b, ptr %s0, ptr @c3
+  %s2 = select i1 %c, ptr %s1, ptr @c4
+  call void %s2()
+  ret void
+}
+
+!llvm.module.flags = !{!0, !1}
+
+!0 = !{i32 7, !"openmp", i32 50}
+!1 = !{i32 7, !"openmp-device", i32 50}
+;.
+; CHECK: [[META2]] = !{ptr @c4, ptr @c3, ptr @c2, ptr @c1}
+;.

>From 4677ac696668ffff4a5d0507da4fb94b61db82b1 Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Sat, 5 Sep 2026 20:04:57 -0500
Subject: [PATCH 2/2] Say that the bound is a threshold on the call site

The Attributor asks about every callee with the same total, so the
predicate is all-or-nothing per call site: a site with four possible
callees keeps its indirect call rather than getting three direct ones and
a fallback. MaxIndirectCallSpecializations read like the latter.

Co-authored-by: Cursor <cursoragent at cursor.com>
---
 llvm/lib/Transforms/IPO/OpenMPOpt.cpp                    | 9 +++++++--
 .../OpenMP/indirect_call_specialization_cap.ll           | 6 +++---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
index acbd2972a06a8..0007c2f7bf8bd 100644
--- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
+++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp
@@ -5840,13 +5840,18 @@ AAFoldRuntimeCall &AAFoldRuntimeCall::createForPosition(const IRPosition &IRP,
 /// call site there can see every outlined region in the module; specializing
 /// all of them costs more in code size and compile time than the direct calls
 /// are worth.
-static constexpr unsigned MaxIndirectCallSpecializations = 3;
+///
+/// This is a threshold on the call site rather than a limit on how many callees
+/// get specialized: the Attributor asks about each callee with the same total,
+/// so a site above the threshold keeps its indirect call instead of getting
+/// this many direct ones plus a fallback.
+static constexpr unsigned MaxCalleesForSpecialization = 3;
 
 static bool shouldSpecializeIndirectCallee(Attributor &,
                                            const AbstractAttribute &,
                                            CallBase &, Function &,
                                            unsigned NumAssumedCallees) {
-  return NumAssumedCallees <= MaxIndirectCallSpecializations;
+  return NumAssumedCallees <= MaxCalleesForSpecialization;
 }
 
 PreservedAnalyses OpenMPOptPass::run(Module &M, ModuleAnalysisManager &AM) {
diff --git a/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll b/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll
index 862c4a815a57c..3626188db37f6 100644
--- a/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll
+++ b/llvm/test/Transforms/OpenMP/indirect_call_specialization_cap.ll
@@ -1,8 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt -passes=openmp-opt -S < %s | FileCheck %s
-; The bound is on the number of possible callees, so it is a property of the call
-; site and not of the module: @three gets its if-cascade of direct calls and
-; @four keeps its indirect call.
+; The bound is a threshold on the call site, not a limit on how many callees get
+; specialized: @three gets its if-cascade of direct calls, and @four keeps its
+; indirect call rather than getting three direct calls and a fallback.
 
 target triple = "amdgcn-amd-amdhsa"
 



More information about the llvm-commits mailing list