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

Conversation

optimisan
Copy link
Contributor

No description provided.

Copy link
Contributor Author

optimisan commented Mar 5, 2025

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

test?

Copy link
Collaborator

@cdevadas cdevadas left a comment

Choose a reason for hiding this comment

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

No tests for this pass?

@optimisan optimisan force-pushed the users/optimisan/05-03-port-mblock-placement-stats branch 2 times, most recently from 4a2d253 to 10ddc68 Compare March 11, 2025 06:48
@optimisan optimisan force-pushed the users/optimisan/postbb/05-03-port-fentryinserter branch from 4b18d66 to 454f3e6 Compare March 11, 2025 08:25
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-backend-x86

Author: Akshat Oke (optimisan)

Changes

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

8 Files Affected:

  • (added) llvm/include/llvm/CodeGen/FEntryInserter.h (+25)
  • (modified) llvm/include/llvm/InitializePasses.h (+1-1)
  • (modified) llvm/include/llvm/Passes/CodeGenPassBuilder.h (+1)
  • (modified) llvm/include/llvm/Passes/MachinePassRegistry.def (+1-1)
  • (modified) llvm/lib/CodeGen/CodeGen.cpp (+1-1)
  • (modified) llvm/lib/CodeGen/FEntryInserter.cpp (+24-9)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (added) llvm/test/CodeGen/X86/fentry.mir (+18)
diff --git a/llvm/include/llvm/CodeGen/FEntryInserter.h b/llvm/include/llvm/CodeGen/FEntryInserter.h
new file mode 100644
index 0000000000000..4a82975cad879
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/FEntryInserter.h
@@ -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
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 62566492d0b87..6c5def57d9f05 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -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 &);
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index 1dfbb6c7eec2e..35d9b124bd859 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -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"
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 505dce9098254..416e5506dfd0d 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -142,6 +142,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())
@@ -259,7 +260,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)
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 8f9d9ff92d366..4a69e06d84a9a 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -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);
diff --git a/llvm/lib/CodeGen/FEntryInserter.cpp b/llvm/lib/CodeGen/FEntryInserter.cpp
index 68304dd41db04..79949dac51448 100644
--- a/llvm/lib/CodeGen/FEntryInserter.cpp
+++ b/llvm/lib/CodeGen/FEntryInserter.cpp
@@ -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"
@@ -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")
@@ -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)
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 555349cbe0398..4e623834e5e5d 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -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"
diff --git a/llvm/test/CodeGen/X86/fentry.mir b/llvm/test/CodeGen/X86/fentry.mir
new file mode 100644
index 0000000000000..5bbc3a0e0387d
--- /dev/null
+++ b/llvm/test/CodeGen/X86/fentry.mir
@@ -0,0 +1,18 @@
+# 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" }
+---
+
+name: with_fentry
+body: |
+  bb.0:
+   ; CHECK-LABEL: name: with_fentry
+   ; CHECK: FENTRY_CALL
+    RET 0
\ No newline at end of file

@optimisan
Copy link
Contributor Author

There were no existing tests, added one.

@optimisan optimisan force-pushed the users/optimisan/postbb/05-03-port-fentryinserter branch 2 times, most recently from 0712e45 to 192d2f2 Compare March 11, 2025 08:29
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

@optimisan optimisan force-pushed the users/optimisan/postbb/05-03-port-fentryinserter branch from 192d2f2 to c029a58 Compare March 12, 2025 05:44
@optimisan optimisan changed the base branch from users/optimisan/05-03-port-mblock-placement-stats to main March 12, 2025 05:46
@optimisan optimisan merged commit 687c9d3 into main Mar 17, 2025
10 of 11 checks passed
@optimisan optimisan deleted the users/optimisan/postbb/05-03-port-fentryinserter branch March 17, 2025 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants