Skip to content

Commit 5549edd

Browse files
committed
[BPF][GlobalISel] add initial gisel support for BPF
This adds initial codegen support for BPF backend. Only implemented ir-translator for "RET" (but not support isel). Depends on: #74998
1 parent 75bf5ff commit 5549edd

16 files changed

+404
-4
lines changed

llvm/lib/Target/BPF/BPF.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
#include "llvm/Target/TargetMachine.h"
1717

1818
namespace llvm {
19+
class BPFRegisterBankInfo;
20+
class BPFSubtarget;
1921
class BPFTargetMachine;
22+
class InstructionSelector;
2023
class PassRegistry;
2124

2225
ModulePass *createBPFCheckAndAdjustIR();
@@ -27,6 +30,10 @@ FunctionPass *createBPFMIPeepholePass();
2730
FunctionPass *createBPFMIPreEmitPeepholePass();
2831
FunctionPass *createBPFMIPreEmitCheckingPass();
2932

33+
InstructionSelector *createBPFInstructionSelector(const BPFTargetMachine &,
34+
const BPFSubtarget &,
35+
const BPFRegisterBankInfo &);
36+
3037
void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
3138
void initializeBPFDAGToDAGISelPass(PassRegistry &);
3239
void initializeBPFMIPeepholePass(PassRegistry &);

llvm/lib/Target/BPF/BPF.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ include "llvm/Target/Target.td"
1111
include "BPFRegisterInfo.td"
1212
include "BPFCallingConv.td"
1313
include "BPFInstrInfo.td"
14+
include "GISel/BPFRegisterBanks.td"
1415

1516
def BPFInstrInfo : InstrInfo;
1617

llvm/lib/Target/BPF/BPFSubtarget.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#include "BPFSubtarget.h"
1414
#include "BPF.h"
15+
#include "BPFTargetMachine.h"
16+
#include "GISel/BPFCallLowering.h"
17+
#include "GISel/BPFLegalizerInfo.h"
18+
#include "GISel/BPFRegisterBankInfo.h"
1519
#include "llvm/MC/TargetRegistry.h"
1620
#include "llvm/TargetParser/Host.h"
1721

@@ -95,4 +99,28 @@ BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU,
9599
FrameLowering(initializeSubtargetDependencies(CPU, FS)),
96100
TLInfo(TM, *this) {
97101
IsLittleEndian = TT.isLittleEndian();
102+
103+
CallLoweringInfo.reset(new BPFCallLowering(*getTargetLowering()));
104+
Legalizer.reset(new BPFLegalizerInfo(*this));
105+
auto *RBI = new BPFRegisterBankInfo(*getRegisterInfo());
106+
RegBankInfo.reset(RBI);
107+
108+
InstSelector.reset(createBPFInstructionSelector(
109+
*static_cast<const BPFTargetMachine *>(&TM), *this, *RBI));
110+
}
111+
112+
const CallLowering *BPFSubtarget::getCallLowering() const {
113+
return CallLoweringInfo.get();
114+
}
115+
116+
InstructionSelector *BPFSubtarget::getInstructionSelector() const {
117+
return InstSelector.get();
118+
}
119+
120+
const LegalizerInfo *BPFSubtarget::getLegalizerInfo() const {
121+
return Legalizer.get();
122+
}
123+
124+
const RegisterBankInfo *BPFSubtarget::getRegBankInfo() const {
125+
return RegBankInfo.get();
98126
}

llvm/lib/Target/BPF/BPFSubtarget.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
#include "BPFFrameLowering.h"
1717
#include "BPFISelLowering.h"
1818
#include "BPFInstrInfo.h"
19+
#include "BPFRegisterInfo.h"
1920
#include "BPFSelectionDAGInfo.h"
21+
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
22+
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
23+
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
24+
#include "llvm/CodeGen/RegisterBankInfo.h"
2025
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
2126
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2227
#include "llvm/IR/DataLayout.h"
@@ -61,6 +66,11 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
6166
// whether cpu v4 insns are enabled.
6267
bool HasLdsx, HasMovsx, HasBswap, HasSdivSmod, HasGotol, HasStoreImm;
6368

69+
std::unique_ptr<CallLowering> CallLoweringInfo;
70+
std::unique_ptr<InstructionSelector> InstSelector;
71+
std::unique_ptr<LegalizerInfo> Legalizer;
72+
std::unique_ptr<RegisterBankInfo> RegBankInfo;
73+
6474
public:
6575
// This constructor initializes the data members to match that
6676
// of the specified triple.
@@ -95,9 +105,14 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
95105
const BPFSelectionDAGInfo *getSelectionDAGInfo() const override {
96106
return &TSInfo;
97107
}
98-
const TargetRegisterInfo *getRegisterInfo() const override {
108+
const BPFRegisterInfo *getRegisterInfo() const override {
99109
return &InstrInfo.getRegisterInfo();
100110
}
111+
112+
const CallLowering *getCallLowering() const override;
113+
InstructionSelector *getInstructionSelector() const override;
114+
const LegalizerInfo *getLegalizerInfo() const override;
115+
const RegisterBankInfo *getRegBankInfo() const override;
101116
};
102117
} // End llvm namespace
103118

llvm/lib/Target/BPF/BPFTargetMachine.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
#include "BPFTargetTransformInfo.h"
1616
#include "MCTargetDesc/BPFMCAsmInfo.h"
1717
#include "TargetInfo/BPFTargetInfo.h"
18+
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
19+
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
20+
#include "llvm/CodeGen/GlobalISel/Legalizer.h"
21+
#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
1822
#include "llvm/CodeGen/Passes.h"
1923
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
2024
#include "llvm/CodeGen/TargetPassConfig.h"
2125
#include "llvm/IR/PassManager.h"
26+
#include "llvm/InitializePasses.h"
2227
#include "llvm/MC/TargetRegistry.h"
2328
#include "llvm/Passes/PassBuilder.h"
2429
#include "llvm/Support/FormattedStream.h"
@@ -40,6 +45,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
4045
RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
4146

4247
PassRegistry &PR = *PassRegistry::getPassRegistry();
48+
initializeGlobalISel(PR);
4349
initializeBPFCheckAndAdjustIRPass(PR);
4450
initializeBPFMIPeepholePass(PR);
4551
initializeBPFDAGToDAGISelPass(PR);
@@ -90,6 +96,11 @@ class BPFPassConfig : public TargetPassConfig {
9096
bool addInstSelector() override;
9197
void addMachineSSAOptimization() override;
9298
void addPreEmitPass() override;
99+
100+
bool addIRTranslator() override;
101+
bool addLegalizeMachineIR() override;
102+
bool addRegBankSelect() override;
103+
bool addGlobalInstructionSelect() override;
93104
};
94105
}
95106

@@ -174,3 +185,23 @@ void BPFPassConfig::addPreEmitPass() {
174185
if (!DisableMIPeephole)
175186
addPass(createBPFMIPreEmitPeepholePass());
176187
}
188+
189+
bool BPFPassConfig::addIRTranslator() {
190+
addPass(new IRTranslator());
191+
return false;
192+
}
193+
194+
bool BPFPassConfig::addLegalizeMachineIR() {
195+
addPass(new Legalizer());
196+
return false;
197+
}
198+
199+
bool BPFPassConfig::addRegBankSelect() {
200+
addPass(new RegBankSelect());
201+
return false;
202+
}
203+
204+
bool BPFPassConfig::addGlobalInstructionSelect() {
205+
addPass(new InstructionSelect(getOptLevel()));
206+
return false;
207+
}

llvm/lib/Target/BPF/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ tablegen(LLVM BPFGenInstrInfo.inc -gen-instr-info)
1111
tablegen(LLVM BPFGenMCCodeEmitter.inc -gen-emitter)
1212
tablegen(LLVM BPFGenRegisterInfo.inc -gen-register-info)
1313
tablegen(LLVM BPFGenSubtargetInfo.inc -gen-subtarget)
14+
tablegen(LLVM BPFGenGlobalISel.inc -gen-global-isel)
15+
tablegen(LLVM BPFGenRegisterBank.inc -gen-register-bank)
1416

1517
add_public_tablegen_target(BPFCommonTableGen)
1618

1719
add_llvm_target(BPFCodeGen
20+
GISel/BPFCallLowering.cpp
21+
GISel/BPFInstructionSelector.cpp
22+
GISel/BPFRegisterBankInfo.cpp
23+
GISel/BPFLegalizerInfo.cpp
1824
BPFAbstractMemberAccess.cpp
1925
BPFAdjustOpt.cpp
2026
BPFAsmPrinter.cpp
@@ -44,6 +50,7 @@ add_llvm_target(BPFCodeGen
4450
CodeGen
4551
CodeGenTypes
4652
Core
53+
GlobalISel
4754
IPO
4855
MC
4956
Scalar
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- BPFCallLowering.cpp - Call lowering for GlobalISel ------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file implements the lowering of LLVM calls to machine code calls for
11+
/// GlobalISel.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "BPFCallLowering.h"
16+
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
17+
#include "llvm/Support/Debug.h"
18+
19+
#define DEBUG_TYPE "bpf-call-lowering"
20+
21+
using namespace llvm;
22+
23+
BPFCallLowering::BPFCallLowering(const BPFTargetLowering &TLI)
24+
: CallLowering(&TLI) {}
25+
26+
bool BPFCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
27+
const Value *Val, ArrayRef<Register> VRegs,
28+
FunctionLoweringInfo &FLI,
29+
Register SwiftErrorVReg) const {
30+
if (!VRegs.empty())
31+
return false;
32+
MIRBuilder.buildInstr(BPF::RET);
33+
return true;
34+
}
35+
36+
bool BPFCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
37+
const Function &F,
38+
ArrayRef<ArrayRef<Register>> VRegs,
39+
FunctionLoweringInfo &FLI) const {
40+
return VRegs.empty();
41+
}
42+
43+
bool BPFCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
44+
CallLoweringInfo &Info) const {
45+
return false;
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- BPFCallLowering.h - Call lowering for GlobalISel --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file describes how to lower LLVM calls to machine code calls.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_LIB_TARGET_BPF_GISEL_BPFCALLLOWERING_H
15+
#define LLVM_LIB_TARGET_BPF_GISEL_BPFCALLLOWERING_H
16+
17+
#include "BPFISelLowering.h"
18+
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
19+
#include "llvm/IR/CallingConv.h"
20+
21+
namespace llvm {
22+
23+
class BPFTargetLowering;
24+
25+
class BPFCallLowering : public CallLowering {
26+
public:
27+
BPFCallLowering(const BPFTargetLowering &TLI);
28+
bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
29+
ArrayRef<Register> VRegs, FunctionLoweringInfo &FLI,
30+
Register SwiftErrorVReg) const override;
31+
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F,
32+
ArrayRef<ArrayRef<Register>> VRegs,
33+
FunctionLoweringInfo &FLI) const override;
34+
bool lowerCall(MachineIRBuilder &MIRBuilder,
35+
CallLoweringInfo &Info) const override;
36+
};
37+
} // namespace llvm
38+
39+
#endif
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//===- BPFInstructionSelector.cpp --------------------------------*- C++ -*-==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements the targeting of the InstructionSelector class for BPF.
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "BPFInstrInfo.h"
13+
#include "BPFRegisterBankInfo.h"
14+
#include "BPFSubtarget.h"
15+
#include "BPFTargetMachine.h"
16+
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
17+
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
18+
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
19+
#include "llvm/CodeGen/MachineFunction.h"
20+
#include "llvm/IR/IntrinsicsBPF.h"
21+
#include "llvm/Support/Debug.h"
22+
23+
#define DEBUG_TYPE "bpf-gisel"
24+
25+
using namespace llvm;
26+
27+
namespace {
28+
29+
#define GET_GLOBALISEL_PREDICATE_BITSET
30+
#include "BPFGenGlobalISel.inc"
31+
#undef GET_GLOBALISEL_PREDICATE_BITSET
32+
33+
class BPFInstructionSelector : public InstructionSelector {
34+
public:
35+
BPFInstructionSelector(const BPFTargetMachine &TM, const BPFSubtarget &STI,
36+
const BPFRegisterBankInfo &RBI);
37+
38+
bool select(MachineInstr &I) override;
39+
static const char *getName() { return DEBUG_TYPE; }
40+
41+
private:
42+
/// tblgen generated 'select' implementation that is used as the initial
43+
/// selector for the patterns that do not require complex C++.
44+
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
45+
46+
const BPFInstrInfo &TII;
47+
const BPFRegisterInfo &TRI;
48+
const BPFRegisterBankInfo &RBI;
49+
50+
#define GET_GLOBALISEL_PREDICATES_DECL
51+
#include "BPFGenGlobalISel.inc"
52+
#undef GET_GLOBALISEL_PREDICATES_DECL
53+
54+
#define GET_GLOBALISEL_TEMPORARIES_DECL
55+
#include "BPFGenGlobalISel.inc"
56+
#undef GET_GLOBALISEL_TEMPORARIES_DECL
57+
};
58+
59+
} // namespace
60+
61+
#define GET_GLOBALISEL_IMPL
62+
#include "BPFGenGlobalISel.inc"
63+
#undef GET_GLOBALISEL_IMPL
64+
65+
BPFInstructionSelector::BPFInstructionSelector(const BPFTargetMachine &TM,
66+
const BPFSubtarget &STI,
67+
const BPFRegisterBankInfo &RBI)
68+
: TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI),
69+
#define GET_GLOBALISEL_PREDICATES_INIT
70+
#include "BPFGenGlobalISel.inc"
71+
#undef GET_GLOBALISEL_PREDICATES_INIT
72+
#define GET_GLOBALISEL_TEMPORARIES_INIT
73+
#include "BPFGenGlobalISel.inc"
74+
#undef GET_GLOBALISEL_TEMPORARIES_INIT
75+
{
76+
}
77+
78+
bool BPFInstructionSelector::select(MachineInstr &I) {
79+
if (selectImpl(I, *CoverageInfo))
80+
return true;
81+
return false;
82+
}
83+
84+
namespace llvm {
85+
InstructionSelector *
86+
createBPFInstructionSelector(const BPFTargetMachine &TM,
87+
const BPFSubtarget &Subtarget,
88+
const BPFRegisterBankInfo &RBI) {
89+
return new BPFInstructionSelector(TM, Subtarget, RBI);
90+
}
91+
} // namespace llvm
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- BPFLegalizerInfo.h ----------------------------------------*- C++ -*-==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements the targeting of the Machinelegalizer class for BPF
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "BPFLegalizerInfo.h"
13+
#include "llvm/Support/Debug.h"
14+
15+
#define DEBUG_TYPE "bpf-legalinfo"
16+
17+
using namespace llvm;
18+
using namespace LegalizeActions;
19+
20+
BPFLegalizerInfo::BPFLegalizerInfo(const BPFSubtarget &ST) {
21+
getLegacyLegalizerInfo().computeTables();
22+
}

0 commit comments

Comments
 (0)