[PATCH] D130952: [Inliner] Handle multiple `run` invocation of `ModuleInlinerWrapperPass` (NFC).

Michele Scandale via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 1 16:53:19 PDT 2022


michele.scandale created this revision.
michele.scandale added reviewers: mtrofin, aeubanks.
Herald added subscribers: ormris, hiraditya.
Herald added a project: All.
michele.scandale requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The current implementation `ModuleInlinerWrapperPass` uses three pass
managers. In the `run` function two of the three pass manager are
always moved into the third one. This lead to undefined behavior (e.g.
crash) if the `ModuleInlinerWrapperPass::run` is executed more than
once on the same instance of the pass because additional run perform
again the move operations on object whose state is unspecified.
This commit addresses the issue by ensuring that the move operations are
performed only on the first invocation of `ModuleInlinerWrapperPass::run`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130952

Files:
  llvm/include/llvm/Transforms/IPO/Inliner.h
  llvm/lib/Transforms/IPO/Inliner.cpp


Index: llvm/lib/Transforms/IPO/Inliner.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Inliner.cpp
+++ llvm/lib/Transforms/IPO/Inliner.cpp
@@ -1118,7 +1118,7 @@
                                                    InliningAdvisorMode Mode,
                                                    unsigned MaxDevirtIterations)
     : Params(Params), IC(IC), Mode(Mode),
-      MaxDevirtIterations(MaxDevirtIterations) {
+      MaxDevirtIterations(MaxDevirtIterations), MPMFinalized(false) {
   // Run the inliner first. The theory is that we are walking bottom-up and so
   // the callees have already been fully optimized, and we want to inline them
   // into the callers so that our optimizations can reflect that.
@@ -1149,20 +1149,23 @@
     return PreservedAnalyses::all();
   }
 
-  // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
-  // to detect when we devirtualize indirect calls and iterate the SCC passes
-  // in that case to try and catch knock-on inlining or function attrs
-  // opportunities. Then we add it to the module pipeline by walking the SCCs
-  // in postorder (or bottom-up).
-  // If MaxDevirtIterations is 0, we just don't use the devirtualization
-  // wrapper.
-  if (MaxDevirtIterations == 0)
-    MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(PM)));
-  else
-    MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
-        createDevirtSCCRepeatedPass(std::move(PM), MaxDevirtIterations)));
-
-  MPM.addPass(std::move(AfterCGMPM));
+  if (!MPMFinalized) {
+    // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
+    // to detect when we devirtualize indirect calls and iterate the SCC passes
+    // in that case to try and catch knock-on inlining or function attrs
+    // opportunities. Then we add it to the module pipeline by walking the SCCs
+    // in postorder (or bottom-up).
+    // If MaxDevirtIterations is 0, we just don't use the devirtualization
+    // wrapper.
+    if (MaxDevirtIterations == 0)
+      MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(PM)));
+    else
+      MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
+          createDevirtSCCRepeatedPass(std::move(PM), MaxDevirtIterations)));
+
+    MPM.addPass(std::move(AfterCGMPM));
+    MPMFinalized = true;
+  }
   MPM.run(M, MAM);
 
   // Discard the InlineAdvisor, a subsequent inlining session should construct
Index: llvm/include/llvm/Transforms/IPO/Inliner.h
===================================================================
--- llvm/include/llvm/Transforms/IPO/Inliner.h
+++ llvm/include/llvm/Transforms/IPO/Inliner.h
@@ -157,6 +157,7 @@
   CGSCCPassManager PM;
   ModulePassManager MPM;
   ModulePassManager AfterCGMPM;
+  bool MPMFinalized;
 };
 } // end namespace llvm
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130952.449156.patch
Type: text/x-patch
Size: 2830 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220801/e15fe665/attachment.bin>


More information about the llvm-commits mailing list