[llvm] [NewPM][BPF] Add `BPFCodeGenPassBuilder` (PR #94158)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 2 19:03:22 PDT 2024


https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/94158

>From 72e2ca8b2f4b0855c4c3b31c05138e2e6484c4f8 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Sun, 2 Jun 2024 21:00:00 +0800
Subject: [PATCH] [NewPM][BPF] Add `BPFCodeGenPassBuilder` We need a simple
 enough target to make codegen pipeline as short as possible.

---
 llvm/lib/Target/BPF/BPFCodeGenPassBuilder.cpp | 30 ++++++++++++++++++
 llvm/lib/Target/BPF/BPFCodeGenPassBuilder.h   | 31 +++++++++++++++++++
 llvm/lib/Target/BPF/BPFTargetMachine.cpp      |  9 ++++++
 llvm/lib/Target/BPF/BPFTargetMachine.h        |  5 +++
 llvm/lib/Target/BPF/CMakeLists.txt            |  1 +
 5 files changed, 76 insertions(+)
 create mode 100644 llvm/lib/Target/BPF/BPFCodeGenPassBuilder.cpp
 create mode 100644 llvm/lib/Target/BPF/BPFCodeGenPassBuilder.h

diff --git a/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.cpp b/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.cpp
new file mode 100644
index 0000000000000..5dbd3911a68fb
--- /dev/null
+++ b/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.cpp
@@ -0,0 +1,30 @@
+//===-- BPFCodeGenPassBuilder.cpp -----------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements BPFCodeGenPassBuilder class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "BPFCodeGenPassBuilder.h"
+#include "BPFTargetMachine.h"
+
+using namespace llvm;
+
+BPFCodeGenPassBuilder::BPFCodeGenPassBuilder(BPFTargetMachine &TM,
+                                             const CGPassBuilderOption &Opts,
+                                             PassInstrumentationCallbacks *PIC)
+    : CodeGenPassBuilder(TM, Opts, PIC) {}
+
+void BPFCodeGenPassBuilder::addPreISel(AddIRPass &addPass) const {
+  // TODO: Add passes pre instruction selection.
+}
+
+void BPFCodeGenPassBuilder::addAsmPrinter(AddMachinePass &addPass,
+                                          CreateMCStreamer) const {
+  // TODO: Add AsmPrinter.
+}
diff --git a/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.h b/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.h
new file mode 100644
index 0000000000000..c8e2c18a845f4
--- /dev/null
+++ b/llvm/lib/Target/BPF/BPFCodeGenPassBuilder.h
@@ -0,0 +1,31 @@
+//===- BPFCodeGenPassBuilder.h - Build BPF codegen pipeline -----*- 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_LIB_TARGET_BPF_BPFCODEGENPASSBUILDER_H
+#define LLVM_LIB_TARGET_BPF_BPFCODEGENPASSBUILDER_H
+
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/Passes/CodeGenPassBuilder.h"
+
+namespace llvm {
+
+class BPFTargetMachine;
+
+class BPFCodeGenPassBuilder
+    : public CodeGenPassBuilder<BPFCodeGenPassBuilder, BPFTargetMachine> {
+public:
+  BPFCodeGenPassBuilder(BPFTargetMachine &TM, const CGPassBuilderOption &Opts,
+                        PassInstrumentationCallbacks *PIC);
+
+  void addPreISel(AddIRPass &addPass) const;
+  void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const;
+};
+
+} // namespace llvm
+
+#endif // LLVM_LIB_TARGET_BPF_BPFCODEGENPASSBUILDER_H
diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
index a7bed69b0f2ab..32b79976633f6 100644
--- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp
+++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
@@ -12,6 +12,7 @@
 
 #include "BPFTargetMachine.h"
 #include "BPF.h"
+#include "BPFCodeGenPassBuilder.h"
 #include "BPFTargetTransformInfo.h"
 #include "MCTargetDesc/BPFMCAsmInfo.h"
 #include "TargetInfo/BPFTargetInfo.h"
@@ -108,6 +109,14 @@ TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
   return new BPFPassConfig(*this, PM);
 }
 
+Error BPFTargetMachine::buildCodeGenPipeline(
+    ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
+    CodeGenFileType FileType, const CGPassBuilderOption &Opts,
+    PassInstrumentationCallbacks *PIC) {
+  BPFCodeGenPassBuilder CGPB(*this, Opts, PIC);
+  return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
+}
+
 static Expected<bool> parseBPFPreserveStaticOffsetOptions(StringRef Params) {
   return PassBuilder::parseSinglePassOption(Params, "allow-partial",
                                             "BPFPreserveStaticOffsetPass");
diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.h b/llvm/lib/Target/BPF/BPFTargetMachine.h
index 0a28394463b26..b735796ed6393 100644
--- a/llvm/lib/Target/BPF/BPFTargetMachine.h
+++ b/llvm/lib/Target/BPF/BPFTargetMachine.h
@@ -36,6 +36,11 @@ class BPFTargetMachine : public LLVMTargetMachine {
 
   TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
 
+  Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &,
+                             raw_pwrite_stream *, CodeGenFileType,
+                             const CGPassBuilderOption &,
+                             PassInstrumentationCallbacks *) override;
+
   TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
 
   TargetLoweringObjectFile *getObjFileLowering() const override {
diff --git a/llvm/lib/Target/BPF/CMakeLists.txt b/llvm/lib/Target/BPF/CMakeLists.txt
index eade4cacb7100..98bf34813f985 100644
--- a/llvm/lib/Target/BPF/CMakeLists.txt
+++ b/llvm/lib/Target/BPF/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_target(BPFCodeGen
   BPFAsmPrinter.cpp
   BPFASpaceCastSimplifyPass.cpp
   BPFCheckAndAdjustIR.cpp
+  BPFCodeGenPassBuilder.cpp
   BPFFrameLowering.cpp
   BPFInstrInfo.cpp
   BPFIRPeephole.cpp



More information about the llvm-commits mailing list