[llvm-branch-commits] [RISCV] Port PromoteConstant (PR #235406)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Oct 24 23:32:18 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

Standard NewPM pass porting. Also rename the legacy pass to match the
pass class name.


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


5 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCV.h (+12-2) 
- (modified) llvm/lib/Target/RISCV/RISCVPassRegistry.def (+1) 
- (modified) llvm/lib/Target/RISCV/RISCVPromoteConstant.cpp (+44-31) 
- (modified) llvm/lib/Target/RISCV/RISCVTargetMachine.cpp (+2-2) 
- (modified) llvm/test/CodeGen/RISCV/riscv-promote-constant.ll (+2-1) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCV.h b/llvm/lib/Target/RISCV/RISCV.h
index 22f25b331b6ec7..88017ff92327ad 100644
--- a/llvm/lib/Target/RISCV/RISCV.h
+++ b/llvm/lib/Target/RISCV/RISCV.h
@@ -244,8 +244,18 @@ void initializeRISCVO0PreLegalizerCombinerPass(PassRegistry &);
 FunctionPass *createRISCVPreLegalizerCombiner();
 void initializeRISCVPreLegalizerCombinerPass(PassRegistry &);
 
-ModulePass *createRISCVPromoteConstantPass();
-void initializeRISCVPromoteConstantPass(PassRegistry &);
+class RISCVPromoteConstantPass
+    : public OptionalPassInfoMixin<RISCVPromoteConstantPass> {
+private:
+  const RISCVTargetMachine *TM;
+
+public:
+  RISCVPromoteConstantPass(const RISCVTargetMachine *TM) : TM(TM) {}
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+};
+
+ModulePass *createRISCVPromoteConstantLegacyPass();
+void initializeRISCVPromoteConstantLegacyPass(PassRegistry &);
 
 class RISCVVLOptimizerPass
     : public OptionalPassInfoMixin<RISCVVLOptimizerPass> {
diff --git a/llvm/lib/Target/RISCV/RISCVPassRegistry.def b/llvm/lib/Target/RISCV/RISCVPassRegistry.def
index 09da40b7d3bc58..85427b64dd8c59 100644
--- a/llvm/lib/Target/RISCV/RISCVPassRegistry.def
+++ b/llvm/lib/Target/RISCV/RISCVPassRegistry.def
@@ -18,6 +18,7 @@
 #endif
 MODULE_PASS("riscv-asm-printer-begin", RISCVAsmPrinterBeginPass())
 MODULE_PASS("riscv-asm-printer-end", RISCVAsmPrinterEndPass())
+MODULE_PASS("riscv-promote-constant", RISCVPromoteConstantPass(this))
 #undef MODULE_PASS
 
 #ifndef FUNCTION_PASS
diff --git a/llvm/lib/Target/RISCV/RISCVPromoteConstant.cpp b/llvm/lib/Target/RISCV/RISCVPromoteConstant.cpp
index 2cee794602edd7..057ec4adf0d7e9 100644
--- a/llvm/lib/Target/RISCV/RISCVPromoteConstant.cpp
+++ b/llvm/lib/Target/RISCV/RISCVPromoteConstant.cpp
@@ -8,11 +8,13 @@
 
 #include "RISCV.h"
 #include "RISCVSubtarget.h"
+#include "RISCVTargetMachine.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/CodeGen/TargetLowering.h"
 #include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/IR/Analysis.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/Constants.h"
@@ -25,6 +27,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/IR/Type.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Casting.h"
@@ -32,7 +35,7 @@
 
 using namespace llvm;
 
-#define DEBUG_TYPE "riscv-promote-const"
+#define DEBUG_TYPE "riscv-promote-constant"
 #define RISCV_PROMOTE_CONSTANT_NAME "RISC-V Promote Constants"
 
 STATISTIC(NumPromoted, "Number of constant literals promoted to globals");
@@ -40,10 +43,10 @@ STATISTIC(NumPromotedUses, "Number of uses of promoted literal constants");
 
 namespace {
 
-class RISCVPromoteConstant : public ModulePass {
+class RISCVPromoteConstantLegacy : public ModulePass {
 public:
   static char ID;
-  RISCVPromoteConstant() : ModulePass(ID) {}
+  RISCVPromoteConstantLegacy() : ModulePass(ID) {}
 
   StringRef getPassName() const override { return RISCV_PROMOTE_CONSTANT_NAME; }
 
@@ -52,41 +55,21 @@ class RISCVPromoteConstant : public ModulePass {
     AU.setPreservesCFG();
   }
 
-  /// Iterate over the functions and promote the double fp constants that
-  /// would otherwise go into the constant pool to a constant array.
-  bool runOnModule(Module &M) override {
-    if (skipModule(M))
-      return false;
-    // TargetMachine and Subtarget are needed to query isFPImmlegal.
-    const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
-    const TargetMachine &TM = TPC.getTM<TargetMachine>();
-    bool Changed = false;
-    for (Function &F : M) {
-      if (F.isDeclaration())
-        continue;
-      const RISCVSubtarget &ST = TM.getSubtarget<RISCVSubtarget>(F);
-      const RISCVTargetLowering *TLI = ST.getTargetLowering();
-      Changed |= runOnFunction(F, TLI);
-    }
-    return Changed;
-  }
-
-private:
-  bool runOnFunction(Function &F, const RISCVTargetLowering *TLI);
+  bool runOnModule(Module &M) override;
 };
 } // end anonymous namespace
 
-char RISCVPromoteConstant::ID = 0;
+char RISCVPromoteConstantLegacy::ID = 0;
 
-INITIALIZE_PASS(RISCVPromoteConstant, DEBUG_TYPE, RISCV_PROMOTE_CONSTANT_NAME,
-                false, false)
+INITIALIZE_PASS(RISCVPromoteConstantLegacy, DEBUG_TYPE,
+                RISCV_PROMOTE_CONSTANT_NAME, false, false)
 
-ModulePass *llvm::createRISCVPromoteConstantPass() {
-  return new RISCVPromoteConstant();
+ModulePass *llvm::createRISCVPromoteConstantLegacyPass() {
+  return new RISCVPromoteConstantLegacy();
 }
 
-bool RISCVPromoteConstant::runOnFunction(Function &F,
-                                         const RISCVTargetLowering *TLI) {
+static bool promotConstantsInFunction(Function &F,
+                                      const RISCVTargetLowering *TLI) {
   if (F.hasOptNone() || F.hasOptSize())
     return false;
 
@@ -212,3 +195,33 @@ bool RISCVPromoteConstant::runOnFunction(Function &F,
 
   return true;
 }
+
+/// Iterate over the functions and promote the double fp constants that
+/// would otherwise go into the constant pool to a constant array.
+static bool promoteConstantsInModule(Module &M, const TargetMachine &TM) {
+  bool Changed = false;
+  for (Function &F : M) {
+    if (F.isDeclaration())
+      continue;
+    const RISCVSubtarget &ST = TM.getSubtarget<RISCVSubtarget>(F);
+    const RISCVTargetLowering *TLI = ST.getTargetLowering();
+    Changed |= promotConstantsInFunction(F, TLI);
+  }
+  return Changed;
+}
+
+bool RISCVPromoteConstantLegacy::runOnModule(Module &M) {
+  if (skipModule(M))
+    return false;
+  // TargetMachine and Subtarget are needed to query isFPImmlegal.
+  const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
+  const TargetMachine &TM = TPC.getTM<TargetMachine>();
+  return promoteConstantsInModule(M, TM);
+}
+
+PreservedAnalyses RISCVPromoteConstantPass::run(Module &M,
+                                                ModuleAnalysisManager &MAM) {
+  return promoteConstantsInModule(M, *TM)
+             ? PreservedAnalyses::none().preserveSet<CFGAnalyses>()
+             : PreservedAnalyses::all();
+}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 59ad1c2e21516b..c3905b82f229b5 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -81,7 +81,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
   initializeRISCVExpandPseudoAtomicsLegacyPass(*PR);
   initializeRISCVRedundantCopyEliminationPass(*PR);
   initializeRISCVAsmPrinterPass(*PR);
-  initializeRISCVPromoteConstantPass(*PR);
+  initializeRISCVPromoteConstantLegacyPass(*PR);
 }
 
 static Reloc::Model getEffectiveRelocModel(const Triple &TT,
@@ -418,7 +418,7 @@ void RISCVPassConfig::addIRPasses() {
 
 bool RISCVPassConfig::addPreISel() {
   if (TM->getOptLevel() != CodeGenOptLevel::None)
-    addPass(createRISCVPromoteConstantPass());
+    addPass(createRISCVPromoteConstantLegacyPass());
   if (TM->getOptLevel() != CodeGenOptLevel::None) {
     // Add a barrier before instruction selection so that we will not get
     // deleted block address after enabling default outlining. See D99707 for
diff --git a/llvm/test/CodeGen/RISCV/riscv-promote-constant.ll b/llvm/test/CodeGen/RISCV/riscv-promote-constant.ll
index 821b23cc6ebfab..173495122c6636 100644
--- a/llvm/test/CodeGen/RISCV/riscv-promote-constant.ll
+++ b/llvm/test/CodeGen/RISCV/riscv-promote-constant.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt %s -S -riscv-promote-const -mtriple=riscv64 -mattr=+d | FileCheck %s
+; RUN: opt %s -S -riscv-promote-constant -mtriple=riscv64 -mattr=+d | FileCheck %s
+; RUN: opt %s -S -passes=riscv-promote-constant -mtriple=riscv64 -mattr=+d | FileCheck %s
 
 ; No promotion should take place, as the pass skips floats.
 define float @multiple_floats(float %a, float %b) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/235406


More information about the llvm-branch-commits mailing list