[llvm] [AMDGPU][GIsel][NPM] Port "AMDGPUPreLegalizerCombiner" to NPM (PR #217222)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 08:09:10 PDT 2026


================
@@ -238,54 +270,74 @@ void AMDGPUPreLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
   MachineFunctionPass::getAnalysisUsage(AU);
 }
 
-AMDGPUPreLegalizerCombiner::AMDGPUPreLegalizerCombiner(bool IsOptNone)
+AMDGPUPreLegalizerCombinerLegacy::AMDGPUPreLegalizerCombinerLegacy(
+    bool IsOptNone)
     : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
   if (!RuleConfig.parseCommandLineOption())
     report_fatal_error("Invalid rule identifier");
 }
 
-bool AMDGPUPreLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
-  if (MF.getProperties().hasFailedISel())
-    return false;
-  auto *TPC = &getAnalysis<TargetPassConfig>();
+bool AMDGPUPreLegalizerCombinerLegacy::runOnMachineFunction(
+    MachineFunction &MF) {
   const Function &F = MF.getFunction();
   bool EnableOpt =
       MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
-  GISelValueTracking *VT =
-      &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
-
-  // Enable CSE.
-  GISelCSEAnalysisWrapper &Wrapper =
-      getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
-  auto *CSEInfo = &Wrapper.get(TPC->getCSEConfig());
-
-  const GCNSubtarget &STI = MF.getSubtarget<GCNSubtarget>();
-  MachineDominatorTree *MDT =
-      IsOptNone ? nullptr
-                : &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
-  CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
-                     nullptr, EnableOpt, F.hasOptSize(), F.hasMinSize());
-  // Disable fixed-point iteration to reduce compile-time
-  CInfo.MaxIterations = 1;
-  CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass;
-  // This is the first Combiner, so the input IR might contain dead
-  // instructions.
-  CInfo.EnableFullDCE = true;
-  AMDGPUPreLegalizerCombinerImpl Impl(MF, CInfo, *VT, CSEInfo, RuleConfig, STI,
-                                      MDT, STI.getLegalizerInfo());
-  return Impl.combineMachineInstrs();
+  return runCombiner(
+      MF,
+      [&]() {
+        // Enable CSE.
+        GISelCSEAnalysisWrapper &Wrapper =
+            getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
+        return &Wrapper.get(getAnalysis<TargetPassConfig>().getCSEConfig());
+      },
+      [&]() {
+        return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
+      },
+      [&]() -> MachineDominatorTree * {
+        return IsOptNone ? nullptr
+                         : &getAnalysis<MachineDominatorTreeWrapperPass>()
+                                .getDomTree();
+      },
+      RuleConfig, EnableOpt);
 }
 
-char AMDGPUPreLegalizerCombiner::ID = 0;
-INITIALIZE_PASS_BEGIN(AMDGPUPreLegalizerCombiner, DEBUG_TYPE,
+char AMDGPUPreLegalizerCombinerLegacy::ID = 0;
+INITIALIZE_PASS_BEGIN(AMDGPUPreLegalizerCombinerLegacy, DEBUG_TYPE,
                       "Combine AMDGPU machine instrs before legalization",
                       false, false)
 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
 INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
-INITIALIZE_PASS_END(AMDGPUPreLegalizerCombiner, DEBUG_TYPE,
+INITIALIZE_PASS_END(AMDGPUPreLegalizerCombinerLegacy, DEBUG_TYPE,
                     "Combine AMDGPU machine instrs before legalization", false,
                     false)
 
-FunctionPass *llvm::createAMDGPUPreLegalizeCombiner(bool IsOptNone) {
-  return new AMDGPUPreLegalizerCombiner(IsOptNone);
+FunctionPass *llvm::createAMDGPUPreLegalizeCombinerLegacy(bool IsOptNone) {
+  return new AMDGPUPreLegalizerCombinerLegacy(IsOptNone);
+}
+
+PreservedAnalyses
+AMDGPUPreLegalizerCombinerPass::run(MachineFunction &MF,
+                                    MachineFunctionAnalysisManager &MFAM) {
+  AMDGPUPreLegalizerCombinerImplRuleConfig RuleConfig;
+  if (!RuleConfig.parseCommandLineOption())
----------------
boomanaiden154 wrote:

Can we just parse the rule config in the static impl function to avoid the separate path between the Legacy and NewPM?

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


More information about the llvm-commits mailing list