Skip to content

[CodeGen][NPM] Port FEntryInserter to NPM #129857

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 17, 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/FEntryInserter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===- llvm/CodeGen/FEntryInserter.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_FENTRYINSERTER_H
#define LLVM_CODEGEN_FENTRYINSERTER_H

#include "llvm/CodeGen/MachinePassManager.h"

namespace llvm {

class FEntryInserterPass : public PassInfoMixin<FEntryInserterPass> {
public:
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM);
static bool isRequired() { return true; }
};

} // namespace llvm

#endif // LLVM_CODEGEN_FENTRYINSERTER_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void initializeExpandPostRALegacyPass(PassRegistry &);
void initializeExpandReductionsPass(PassRegistry &);
void initializeExpandVariadicsPass(PassRegistry &);
void initializeExternalAAWrapperPassPass(PassRegistry &);
void initializeFEntryInserterPass(PassRegistry &);
void initializeFEntryInserterLegacyPass(PassRegistry &);
void initializeFinalizeISelPass(PassRegistry &);
void initializeFinalizeMachineBundlesPass(PassRegistry &);
void initializeFixIrreduciblePass(PassRegistry &);
Expand Down
1 change: 1 addition & 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/ExpandPostRAPseudos.h"
#include "llvm/CodeGen/ExpandReductions.h"
#include "llvm/CodeGen/FEntryInserter.h"
#include "llvm/CodeGen/FinalizeISel.h"
#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
#include "llvm/CodeGen/GCMetadata.h"
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("dead-mi-elimination", DeadMachineInstructionElimPass())
MACHINE_FUNCTION_PASS("early-ifcvt", EarlyIfConverterPass())
MACHINE_FUNCTION_PASS("early-machinelicm", EarlyMachineLICMPass())
MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass())
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass())
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
Expand Down Expand Up @@ -251,7 +252,6 @@ DUMMY_MACHINE_FUNCTION_PASS("cfi-fixup", CFIFixupPass)
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("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 @@ -43,7 +43,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeExpandLargeFpConvertLegacyPassPass(Registry);
initializeExpandMemCmpLegacyPassPass(Registry);
initializeExpandPostRALegacyPass(Registry);
initializeFEntryInserterPass(Registry);
initializeFEntryInserterLegacyPass(Registry);
initializeFinalizeISelPass(Registry);
initializeFinalizeMachineBundlesPass(Registry);
initializeFixupStatepointCallerSavedLegacyPass(Registry);
Expand Down
33 changes: 24 additions & 9 deletions llvm/lib/CodeGen/FEntryInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/FEntryInserter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/Function.h"
Expand All @@ -21,17 +23,30 @@
using namespace llvm;

namespace {
struct FEntryInserter : public MachineFunctionPass {
struct FEntryInserter {
bool run(MachineFunction &MF);
};

struct FEntryInserterLegacy : public MachineFunctionPass {
static char ID; // Pass identification, replacement for typeid
FEntryInserter() : MachineFunctionPass(ID) {
initializeFEntryInserterPass(*PassRegistry::getPassRegistry());
FEntryInserterLegacy() : MachineFunctionPass(ID) {
initializeFEntryInserterLegacyPass(*PassRegistry::getPassRegistry());
}

bool runOnMachineFunction(MachineFunction &F) override;
bool runOnMachineFunction(MachineFunction &F) override {
return FEntryInserter().run(F);
}
};
}

bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
PreservedAnalyses FEntryInserterPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &AM) {
if (!FEntryInserter().run(MF))
return PreservedAnalyses::all();
return getMachineFunctionPassPreservedAnalyses();
}

bool FEntryInserter::run(MachineFunction &MF) {
const std::string FEntryName = std::string(
MF.getFunction().getFnAttribute("fentry-call").getValueAsString());
if (FEntryName != "true")
Expand All @@ -44,7 +59,7 @@ bool FEntryInserter::runOnMachineFunction(MachineFunction &MF) {
return true;
}

char FEntryInserter::ID = 0;
char &llvm::FEntryInserterID = FEntryInserter::ID;
INITIALIZE_PASS(FEntryInserter, "fentry-insert", "Insert fentry calls", false,
false)
char FEntryInserterLegacy::ID = 0;
char &llvm::FEntryInserterID = FEntryInserterLegacy::ID;
INITIALIZE_PASS(FEntryInserterLegacy, "fentry-insert", "Insert fentry calls",
false, false)
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/ExpandPostRAPseudos.h"
#include "llvm/CodeGen/FEntryInserter.h"
#include "llvm/CodeGen/FinalizeISel.h"
#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
#include "llvm/CodeGen/GCMetadata.h"
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/CodeGen/X86/fentry.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# RUN: llc -mtriple=x86_64-- -passes=fentry-insert %s -o - | FileCheck %s

--- |
target triple = "x86_64--"
define i32 @with_fentry() #1 {
entry:
ret i32 0
}

attributes #1 = { "fentry-call"="true" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attributes #1 = { "fentry-call"="true" }
attributes #1 = { "fentry-call"="true" }
...

I'm not sure why the parses

...

---
name: with_fentry
body: |
bb.0:
; CHECK-LABEL: name: with_fentry
; CHECK: FENTRY_CALL
; CHECK-NEXT: RET 0
RET 0
...