[llvm] [OpenMPOpt] Analyse loop-runtime callbacks, bound specialization, ask the runtime for the worker count (PR #219323)

Larry Meadows via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 5 07:15:13 PDT 2026


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

>From 7578f6adade182801e56acfd35965bdbe4a0e7a5 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] [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}
+;.



More information about the llvm-commits mailing list