Skip to content

IROutliner: Use ValueMapper to remap constants in a function #134850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Apr 8, 2025

Stop relying on the uselist of constants, which also required filtering
the uses that are in the correct function. I'm not sure why this pass is
doing its own cloning instead of building the value map while doing its
cloning.

Copy link
Contributor Author

arsenm commented Apr 8, 2025

@arsenm arsenm added the ipo Interprocedural optimizations label Apr 8, 2025 — with Graphite App
@arsenm arsenm marked this pull request as ready for review April 8, 2025 12:38
@llvmbot
Copy link
Member

llvmbot commented Apr 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Stop relying on the uselist of constants, which also required filtering
the uses that are in the correct function. I'm not sure why this pass is
doing its own cloning instead of building the value map while doing its
cloning.


Full diff: https://github.com/llvm/llvm-project/pull/134850.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/IROutliner.cpp (+9-8)
diff --git a/llvm/lib/Transforms/IPO/IROutliner.cpp b/llvm/lib/Transforms/IPO/IROutliner.cpp
index fd76d3a32049c..7e9ec4a04d5e3 100644
--- a/llvm/lib/Transforms/IPO/IROutliner.cpp
+++ b/llvm/lib/Transforms/IPO/IROutliner.cpp
@@ -24,6 +24,7 @@
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Utils/ValueMapper.h"
 #include <optional>
 #include <vector>
 
@@ -1930,10 +1931,12 @@ replaceArgumentUses(OutlinableRegion &Region,
 /// \param Region [in] - The region of extracted code to be changed.
 void replaceConstants(OutlinableRegion &Region) {
   OutlinableGroup &Group = *Region.Parent;
+  Function *OutlinedFunction = Group.OutlinedFunction;
+  ValueToValueMapTy VMap;
+
   // Iterate over the constants that need to be elevated into arguments
   for (std::pair<unsigned, Constant *> &Const : Region.AggArgToConstant) {
     unsigned AggArgIdx = Const.first;
-    Function *OutlinedFunction = Group.OutlinedFunction;
     assert(OutlinedFunction && "Overall Function is not defined?");
     Constant *CST = Const.second;
     Argument *Arg = Group.OutlinedFunction->getArg(AggArgIdx);
@@ -1943,16 +1946,14 @@ void replaceConstants(OutlinableRegion &Region) {
     // TODO: If in the future constants do not have one global value number,
     // i.e. a constant 1 could be mapped to several values, this check will
     // have to be more strict.  It cannot be using only replaceUsesWithIf.
-
+    VMap[CST] = Arg;
     LLVM_DEBUG(dbgs() << "Replacing uses of constant " << *CST
                       << " in function " << *OutlinedFunction << " with "
-                      << *Arg << "\n");
-    CST->replaceUsesWithIf(Arg, [OutlinedFunction](Use &U) {
-      if (Instruction *I = dyn_cast<Instruction>(U.getUser()))
-        return I->getFunction() == OutlinedFunction;
-      return false;
-    });
+                      << *Arg << '\n');
   }
+
+  RemapFunction(*OutlinedFunction, VMap,
+                RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
 }
 
 /// It is possible that there is a basic block that already performs the same

arsenm added 2 commits April 9, 2025 16:02
Stop relying on the uselist of constants, which also required filtering
the uses that are in the correct function. I'm not sure why this pass is
doing its own cloning instead of building the value map while doing its
cloning.
@arsenm arsenm force-pushed the users/arsenm/ir-outliner/use-valuemapper-to-replace-constants branch from 96a4097 to 34bf25a Compare April 9, 2025 14:03
Copy link
Contributor Author

arsenm commented Apr 10, 2025

Merge activity

  • Apr 10, 4:29 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Apr 10, 4:31 PM EDT: A user merged this pull request with Graphite.

@arsenm arsenm merged commit a80a802 into main Apr 10, 2025
11 checks passed
@arsenm arsenm deleted the users/arsenm/ir-outliner/use-valuemapper-to-replace-constants branch April 10, 2025 20:31
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…4850)

Stop relying on the uselist of constants, which also required filtering
the uses that are in the correct function. I'm not sure why this pass is
doing its own cloning instead of building the value map while doing its
cloning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ipo Interprocedural optimizations llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants