[llvm] [LTO][Pipelines][Coro] Handle coroutines in LTO pipeline (PR #126168)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 7 00:05:55 PST 2025


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/126168

>From f44ca8d1569ec1c6025192555a7eb94b3a7d772a Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 6 Feb 2025 18:09:18 -0800
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 llvm/lib/Passes/PassBuilderPipelines.cpp | 33 ++++++++++++------------
 llvm/test/Other/new-pm-O0-defaults.ll    |  2 ++
 llvm/test/Other/new-pm-lto-defaults.ll   |  4 +++
 llvm/test/ThinLTO/X86/coro.ll            | 21 +++++++++++++++
 4 files changed, 44 insertions(+), 16 deletions(-)
 create mode 100644 llvm/test/ThinLTO/X86/coro.ll

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index c6cf6cdbe9390f8..e631e03c8384080 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -14,6 +14,7 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Analysis/BasicAliasAnalysis.h"
@@ -1816,6 +1817,17 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
   // in the current module.
   MPM.addPass(CrossDSOCFIPass());
 
+  MPM.addPass(CoroEarlyPass());
+
+  auto Exit = llvm::make_scope_exit([&]() {
+    MPM.addPass(CoroCleanupPass());
+
+    invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
+
+    // Emit annotation remarks.
+    addAnnotationRemarksPass(MPM);
+  });
+
   if (Level == OptimizationLevel::O0) {
     // The WPD and LowerTypeTest passes need to run at -O0 to lower type
     // metadata and intrinsics.
@@ -1826,11 +1838,6 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
     MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
                                    lowertypetests::DropTestKind::Assume));
 
-    invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
-
-    // Emit annotation remarks.
-    addAnnotationRemarksPass(MPM);
-
     return MPM;
   }
 
@@ -1910,11 +1917,6 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
     MPM.addPass(LowerTypeTestsPass(nullptr, nullptr,
                                    lowertypetests::DropTestKind::Assume));
 
-    invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
-
-    // Emit annotation remarks.
-    addAnnotationRemarksPass(MPM);
-
     return MPM;
   }
 
@@ -1983,7 +1985,11 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
 
   // If we didn't decide to inline a function, check to see if we can
   // transform it to pass arguments by value instead of by reference.
-  MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(ArgumentPromotionPass()));
+  CGSCCPassManager CGPM;
+  CGPM.addPass(ArgumentPromotionPass());
+  CGPM.addPass(CoroSplitPass(Level != OptimizationLevel::O0));
+  CGPM.addPass(CoroAnnotationElidePass());
+  MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
 
   FunctionPassManager FPM;
   // The IPO Passes may leave cruft around. Clean up after them.
@@ -2135,11 +2141,6 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
   if (PTO.CallGraphProfile)
     MPM.addPass(CGProfilePass(/*InLTOPostLink=*/true));
 
-  invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
-
-  // Emit annotation remarks.
-  addAnnotationRemarksPass(MPM);
-
   return MPM;
 }
 
diff --git a/llvm/test/Other/new-pm-O0-defaults.ll b/llvm/test/Other/new-pm-O0-defaults.ll
index e8131ac7fab45ac..db450269414e3ef 100644
--- a/llvm/test/Other/new-pm-O0-defaults.ll
+++ b/llvm/test/Other/new-pm-O0-defaults.ll
@@ -47,12 +47,14 @@
 ; CHECK-THINLTO-NEXT: Running pass: EliminateAvailableExternallyPass
 ; CHECK-THINLTO-NEXT: Running pass: GlobalDCEPass
 ; CHECK-LTO: Running pass: CrossDSOCFIPass on [module]
+; CHECK-LTO-NEXT: CoroEarlyPass
 ; CHECK-LTO-NEXT: Running pass: WholeProgramDevirtPass
 ; CHECK-LTO-NEXT: Running analysis: InnerAnalysisManagerProxy
 ; CHECK-LTO-NEXT: Running pass: LowerTypeTestsPass
 ; CHECK-LTO-NEXT: Running pass: LowerTypeTestsPass
 ; CHECK-CORO-NEXT: Running pass: AnnotationRemarksPass
 ; CHECK-CORO-NEXT: Running analysis: TargetLibraryAnalysis
+; CHECK-LTO-NEXT: CoroCleanupPass
 ; CHECK-LTO-NEXT: Running pass: AnnotationRemarksPass
 ; CHECK-LTO-NEXT: Running analysis: TargetLibraryAnalysis
 ; CHECK-NEXT: Running pass: PrintModulePass
diff --git a/llvm/test/Other/new-pm-lto-defaults.ll b/llvm/test/Other/new-pm-lto-defaults.ll
index 86480c5115748d5..d6e88bd7b5c50a5 100644
--- a/llvm/test/Other/new-pm-lto-defaults.ll
+++ b/llvm/test/Other/new-pm-lto-defaults.ll
@@ -35,6 +35,7 @@
 
 ; CHECK-EP: Running pass: NoOpModulePass
 ; CHECK-O: Running pass: CrossDSOCFIPass
+; CHECK-O-NEXT: CoroEarlyPass
 ; CHECK-O-NEXT: Running pass: OpenMPOptPass
 ; CHECK-O-NEXT: Running pass: GlobalDCEPass
 ; CHECK-O-NEXT: Running pass: InferFunctionAttrsPass
@@ -86,6 +87,8 @@
 ; CHECK-O23SZ-NEXT: Running pass: OpenMPOptPass
 ; CHECK-O23SZ-NEXT: Running pass: GlobalDCEPass
 ; CHECK-O23SZ-NEXT: Running pass: ArgumentPromotionPass
+; CHECK-O23SZ-NEXT: CoroSplitPass on (foo)
+; CHECK-O23SZ-NEXT: CoroAnnotationElidePass on (foo)
 ; CHECK-O23SZ-NEXT: Running pass: InstCombinePass
 ; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
 ; CHECK-O23SZ-NEXT: Running pass: ConstraintEliminationPass
@@ -156,6 +159,7 @@
 ; CHECK-O23SZ-NEXT: Running pass: GlobalDCEPass
 ; CHECK-O23SZ-NEXT: Running pass: RelLookupTableConverterPass
 ; CHECK-O23SZ-NEXT: Running pass: CGProfilePass
+; CHECK-O-NEXT: CoroCleanupPass
 ; CHECK-EP-NEXT: Running pass: NoOpModulePass
 ; CHECK-O-NEXT: Running pass: AnnotationRemarksPass on foo
 ; CHECK-O-NEXT: Running pass: PrintModulePass
diff --git a/llvm/test/ThinLTO/X86/coro.ll b/llvm/test/ThinLTO/X86/coro.ll
new file mode 100644
index 000000000000000..9094062b2fe275f
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/coro.ll
@@ -0,0 +1,21 @@
+; RUN: llvm-as %s -o %t1.bc
+; RUN: llvm-lto2 run %t1.bc -o %t2.o -r=%t1.bc,test,plx -r=%t1.bc,extern_func,plx -save-temps
+; RUN: llvm-dis %t2.o.0.5.precodegen.bc -o - | FileCheck %s --implicit-check-not=llvm.coro
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-fuchsia"
+
+declare void @extern_func()
+
+; CHECK:      define {{.*}} void @test(
+define void @test(ptr %hdl) {
+  call void @llvm.coro.resume(ptr %hdl)
+  call void @llvm.coro.destroy(ptr %hdl)
+  call i1 @llvm.coro.done(ptr %hdl)
+  ret void
+}
+
+declare void @llvm.coro.resume(ptr)
+declare void @llvm.coro.destroy(ptr)
+declare i1 @llvm.coro.done(ptr)
+

>From 0233bcd6e5a0ecd5c614a7075443322880b96f59 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 6 Feb 2025 20:57:23 -0800
Subject: [PATCH 2/2] fix test

Created using spr 1.3.4
---
 llvm/test/ThinLTO/X86/coro.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/ThinLTO/X86/coro.ll b/llvm/test/ThinLTO/X86/coro.ll
index 9094062b2fe275f..cde398dd76d85a4 100644
--- a/llvm/test/ThinLTO/X86/coro.ll
+++ b/llvm/test/ThinLTO/X86/coro.ll
@@ -1,6 +1,6 @@
 ; RUN: llvm-as %s -o %t1.bc
 ; RUN: llvm-lto2 run %t1.bc -o %t2.o -r=%t1.bc,test,plx -r=%t1.bc,extern_func,plx -save-temps
-; RUN: llvm-dis %t2.o.0.5.precodegen.bc -o - | FileCheck %s --implicit-check-not=llvm.coro
+; RUN: llvm-dis %t2.o.0.5.precodegen.bc -o - | FileCheck %s --implicit-check-not="call void @llvm.coro"
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-fuchsia"



More information about the llvm-commits mailing list