Skip to content

[CodeGen][NewPM] Port "FixupStatepointCallerSaved" pass to NPM #129541

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
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions llvm/include/llvm/CodeGen/FixupStatepointCallerSaved.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- llvm/CodeGen/FixupStatepointCallerSaved.h ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_FIXUPSTATEPOINTCALLERSAVED_H
#define LLVM_CODEGEN_FIXUPSTATEPOINTCALLERSAVED_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {

class FixupStatepointCallerSavedPass
: public PassInfoMixin<FixupStatepointCallerSavedPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
};

} // namespace llvm

#endif // LLVM_CODEGEN_FIXUPSTATEPOINTCALLERSAVED_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void initializeFEntryInserterPass(PassRegistry &);
void initializeFinalizeISelPass(PassRegistry &);
void initializeFinalizeMachineBundlesPass(PassRegistry &);
void initializeFixIrreduciblePass(PassRegistry &);
void initializeFixupStatepointCallerSavedPass(PassRegistry &);
void initializeFixupStatepointCallerSavedLegacyPass(PassRegistry &);
void initializeFlattenCFGLegacyPassPass(PassRegistry &);
void initializeFuncletLayoutPass(PassRegistry &);
void initializeGCEmptyBasicBlocksPass(PassRegistry &);
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/Passes/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "llvm/CodeGen/ExpandMemCmp.h"
#include "llvm/CodeGen/ExpandReductions.h"
#include "llvm/CodeGen/FinalizeISel.h"
#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/CodeGen/GlobalMerge.h"
#include "llvm/CodeGen/GlobalMergeFunctions.h"
Expand Down Expand Up @@ -935,6 +936,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::addMachinePasses(
derived().addPostRegAlloc(addPass);

addPass(RemoveRedundantDebugValuesPass());
addPass(FixupStatepointCallerSavedPass());

// Insert prolog/epilog code. Eliminate abstract frame index references...
if (getOptLevel() != CodeGenOptLevel::None) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass())
MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass())
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass())
MACHINE_FUNCTION_PASS("machine-cse", MachineCSEPass())
Expand Down Expand Up @@ -231,7 +232,6 @@ DUMMY_MACHINE_FUNCTION_PASS("cfi-instr-inserter", CFIInstrInserterPass)
DUMMY_MACHINE_FUNCTION_PASS("detect-dead-lanes", DetectDeadLanesPass)
DUMMY_MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinter)
DUMMY_MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass)
DUMMY_MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass)
DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass)
DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass)
DUMMY_MACHINE_FUNCTION_PASS("gc-empty-basic-blocks", GCEmptyBasicBlocksPass)
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeFEntryInserterPass(Registry);
initializeFinalizeISelPass(Registry);
initializeFinalizeMachineBundlesPass(Registry);
initializeFixupStatepointCallerSavedPass(Registry);
initializeFixupStatepointCallerSavedLegacyPass(Registry);
initializeFuncletLayoutPass(Registry);
initializeGCMachineCodeAnalysisPass(Registry);
initializeGCModuleInfoPass(Registry);
Expand Down
46 changes: 34 additions & 12 deletions llvm/lib/CodeGen/FixupStatepointCallerSaved.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
///
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
Expand Down Expand Up @@ -58,14 +59,18 @@ static cl::opt<unsigned> MaxStatepointsWithRegs(

namespace {

class FixupStatepointCallerSaved : public MachineFunctionPass {
struct FixupStatepointCallerSavedImpl {
bool run(MachineFunction &MF);
};

class FixupStatepointCallerSavedLegacy : public MachineFunctionPass {
public:
static char ID;

FixupStatepointCallerSaved() : MachineFunctionPass(ID) {
initializeFixupStatepointCallerSavedPass(*PassRegistry::getPassRegistry());
FixupStatepointCallerSavedLegacy() : MachineFunctionPass(ID) {
initializeFixupStatepointCallerSavedLegacyPass(
*PassRegistry::getPassRegistry());
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
Expand All @@ -80,12 +85,12 @@ class FixupStatepointCallerSaved : public MachineFunctionPass {

} // End anonymous namespace.

char FixupStatepointCallerSaved::ID = 0;
char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSaved::ID;
char FixupStatepointCallerSavedLegacy::ID = 0;
char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSavedLegacy::ID;

INITIALIZE_PASS_BEGIN(FixupStatepointCallerSaved, DEBUG_TYPE,
INITIALIZE_PASS_BEGIN(FixupStatepointCallerSavedLegacy, DEBUG_TYPE,
"Fixup Statepoint Caller Saved", false, false)
INITIALIZE_PASS_END(FixupStatepointCallerSaved, DEBUG_TYPE,
INITIALIZE_PASS_END(FixupStatepointCallerSavedLegacy, DEBUG_TYPE,
"Fixup Statepoint Caller Saved", false, false)

// Utility function to get size of the register.
Expand Down Expand Up @@ -588,10 +593,7 @@ class StatepointProcessor {
};
} // namespace

bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;

bool FixupStatepointCallerSavedImpl::run(MachineFunction &MF) {
const Function &F = MF.getFunction();
if (!F.hasGC())
return false;
Expand All @@ -618,3 +620,23 @@ bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) {
}
return Changed;
}

bool FixupStatepointCallerSavedLegacy::runOnMachineFunction(
MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;

return FixupStatepointCallerSavedImpl().run(MF);
}

PreservedAnalyses
FixupStatepointCallerSavedPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {

if (!FixupStatepointCallerSavedImpl().run(MF))
return PreservedAnalyses::all();

auto PA = getMachineFunctionPassPreservedAnalyses();
PA.preserveSet<CFGAnalyses>();
return PA;
}
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
#include "llvm/CodeGen/ExpandMemCmp.h"
#include "llvm/CodeGen/FinalizeISel.h"
#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/CodeGen/GlobalMerge.h"
#include "llvm/CodeGen/GlobalMergeFunctions.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-call.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -run-pass fixup-statepoint-caller-saved | FileCheck %s
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -passes='fixup-statepoint-caller-saved' | FileCheck %s
--- |
; ModuleID = 'test/CodeGen/X86/statepoint-fixup-call.ll'
source_filename = "test/CodeGen/X86/statepoint-fixup-call.ll"
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-copy-prop-neg.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -o - %s -run-pass fixup-statepoint-caller-saved -verify-machineinstrs | FileCheck %s
# RUN: llc -o - %s -passes='fixup-statepoint-caller-saved' | FileCheck %s

# Check that COPY is not eliminated if dest register is used in
# non-meta arguments of statepoint.
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-copy-prop.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# RUN: llc -o - %s -run-pass fixup-statepoint-caller-saved -verify-machineinstrs | FileCheck %s
# RUN: llc -o - %s -passes='fixup-statepoint-caller-saved' | FileCheck %s

--- |
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-invoke.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -run-pass fixup-statepoint-caller-saved | FileCheck %s
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -passes='fixup-statepoint-caller-saved' | FileCheck %s

--- |
; ModuleID = 'test/CodeGen/X86/statepoint-fixup-invoke.mir'
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-shared-ehpad.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -run-pass fixup-statepoint-caller-saved | FileCheck %s
# RUN: llc -o - %s -fixup-allow-gcptr-in-csr=false -passes='fixup-statepoint-caller-saved' | FileCheck %s

# NOTE: MIR in this test was hand edited to have two statepoints share single landing pad.
# This is forbidden in IR, but is allowed in MIR. I just was unable to reproduce conditions
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-undef-def.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -x mir -run-pass fixup-statepoint-caller-saved -verify-machineinstrs < %s | FileCheck %s
# RUN: llc -x mir -passes='fixup-statepoint-caller-saved' < %s | FileCheck %s

--- |
; ModuleID = 'undef.ll'
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/statepoint-fixup-undef.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -x mir -run-pass fixup-statepoint-caller-saved -verify-machineinstrs < %s | FileCheck %s
# RUN: llc -x mir -passes='fixup-statepoint-caller-saved' < %s | FileCheck %s
# RUN: llc -x mir -start-before fixup-statepoint-caller-saved -verify-machineinstrs < %s | FileCheck %s -check-prefix=STACKMAP

--- |
Expand Down