Skip to content

[BPF][GlobalISel] add initial gisel support for BPF #74999

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 1 commit into from
Dec 11, 2023
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
7 changes: 7 additions & 0 deletions llvm/lib/Target/BPF/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#include "llvm/Target/TargetMachine.h"

namespace llvm {
class BPFRegisterBankInfo;
class BPFSubtarget;
class BPFTargetMachine;
class InstructionSelector;
class PassRegistry;

ModulePass *createBPFCheckAndAdjustIR();
Expand All @@ -27,6 +30,10 @@ FunctionPass *createBPFMIPeepholePass();
FunctionPass *createBPFMIPreEmitPeepholePass();
FunctionPass *createBPFMIPreEmitCheckingPass();

InstructionSelector *createBPFInstructionSelector(const BPFTargetMachine &,
const BPFSubtarget &,
const BPFRegisterBankInfo &);

void initializeBPFCheckAndAdjustIRPass(PassRegistry&);
void initializeBPFDAGToDAGISelPass(PassRegistry &);
void initializeBPFMIPeepholePass(PassRegistry &);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/BPF/BPF.td
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include "llvm/Target/Target.td"
include "BPFRegisterInfo.td"
include "BPFCallingConv.td"
include "BPFInstrInfo.td"
include "GISel/BPFRegisterBanks.td"

def BPFInstrInfo : InstrInfo;

Expand Down
28 changes: 28 additions & 0 deletions llvm/lib/Target/BPF/BPFSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#include "BPFSubtarget.h"
#include "BPF.h"
#include "BPFTargetMachine.h"
#include "GISel/BPFCallLowering.h"
#include "GISel/BPFLegalizerInfo.h"
#include "GISel/BPFRegisterBankInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/TargetParser/Host.h"

Expand Down Expand Up @@ -95,4 +99,28 @@ BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU,
FrameLowering(initializeSubtargetDependencies(CPU, FS)),
TLInfo(TM, *this) {
IsLittleEndian = TT.isLittleEndian();

CallLoweringInfo.reset(new BPFCallLowering(*getTargetLowering()));
Legalizer.reset(new BPFLegalizerInfo(*this));
auto *RBI = new BPFRegisterBankInfo(*getRegisterInfo());
RegBankInfo.reset(RBI);

InstSelector.reset(createBPFInstructionSelector(
*static_cast<const BPFTargetMachine *>(&TM), *this, *RBI));
}

const CallLowering *BPFSubtarget::getCallLowering() const {
return CallLoweringInfo.get();
}

InstructionSelector *BPFSubtarget::getInstructionSelector() const {
return InstSelector.get();
}

const LegalizerInfo *BPFSubtarget::getLegalizerInfo() const {
return Legalizer.get();
}

const RegisterBankInfo *BPFSubtarget::getRegBankInfo() const {
return RegBankInfo.get();
}
17 changes: 16 additions & 1 deletion llvm/lib/Target/BPF/BPFSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
#include "BPFFrameLowering.h"
#include "BPFISelLowering.h"
#include "BPFInstrInfo.h"
#include "BPFRegisterInfo.h"
#include "BPFSelectionDAGInfo.h"
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
#include "llvm/CodeGen/RegisterBankInfo.h"
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DataLayout.h"
Expand Down Expand Up @@ -61,6 +66,11 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
// whether cpu v4 insns are enabled.
bool HasLdsx, HasMovsx, HasBswap, HasSdivSmod, HasGotol, HasStoreImm;

std::unique_ptr<CallLowering> CallLoweringInfo;
std::unique_ptr<InstructionSelector> InstSelector;
std::unique_ptr<LegalizerInfo> Legalizer;
std::unique_ptr<RegisterBankInfo> RegBankInfo;

public:
// This constructor initializes the data members to match that
// of the specified triple.
Expand Down Expand Up @@ -95,9 +105,14 @@ class BPFSubtarget : public BPFGenSubtargetInfo {
const BPFSelectionDAGInfo *getSelectionDAGInfo() const override {
return &TSInfo;
}
const TargetRegisterInfo *getRegisterInfo() const override {
const BPFRegisterInfo *getRegisterInfo() const override {
return &InstrInfo.getRegisterInfo();
}

const CallLowering *getCallLowering() const override;
InstructionSelector *getInstructionSelector() const override;
const LegalizerInfo *getLegalizerInfo() const override;
const RegisterBankInfo *getRegBankInfo() const override;
};
} // End llvm namespace

Expand Down
31 changes: 31 additions & 0 deletions llvm/lib/Target/BPF/BPFTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
#include "BPFTargetTransformInfo.h"
#include "MCTargetDesc/BPFMCAsmInfo.h"
#include "TargetInfo/BPFTargetInfo.h"
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
#include "llvm/CodeGen/GlobalISel/Legalizer.h"
#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/FormattedStream.h"
Expand All @@ -40,6 +45,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());

PassRegistry &PR = *PassRegistry::getPassRegistry();
initializeGlobalISel(PR);
initializeBPFCheckAndAdjustIRPass(PR);
initializeBPFMIPeepholePass(PR);
initializeBPFDAGToDAGISelPass(PR);
Expand Down Expand Up @@ -90,6 +96,11 @@ class BPFPassConfig : public TargetPassConfig {
bool addInstSelector() override;
void addMachineSSAOptimization() override;
void addPreEmitPass() override;

bool addIRTranslator() override;
bool addLegalizeMachineIR() override;
bool addRegBankSelect() override;
bool addGlobalInstructionSelect() override;
};
}

Expand Down Expand Up @@ -174,3 +185,23 @@ void BPFPassConfig::addPreEmitPass() {
if (!DisableMIPeephole)
addPass(createBPFMIPreEmitPeepholePass());
}

bool BPFPassConfig::addIRTranslator() {
addPass(new IRTranslator());
return false;
}

bool BPFPassConfig::addLegalizeMachineIR() {
addPass(new Legalizer());
return false;
}

bool BPFPassConfig::addRegBankSelect() {
addPass(new RegBankSelect());
return false;
}

bool BPFPassConfig::addGlobalInstructionSelect() {
addPass(new InstructionSelect(getOptLevel()));
return false;
}
7 changes: 7 additions & 0 deletions llvm/lib/Target/BPF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ tablegen(LLVM BPFGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM BPFGenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM BPFGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM BPFGenSubtargetInfo.inc -gen-subtarget)
tablegen(LLVM BPFGenGlobalISel.inc -gen-global-isel)
tablegen(LLVM BPFGenRegisterBank.inc -gen-register-bank)

add_public_tablegen_target(BPFCommonTableGen)

add_llvm_target(BPFCodeGen
GISel/BPFCallLowering.cpp
GISel/BPFInstructionSelector.cpp
GISel/BPFRegisterBankInfo.cpp
GISel/BPFLegalizerInfo.cpp
BPFAbstractMemberAccess.cpp
BPFAdjustOpt.cpp
BPFAsmPrinter.cpp
Expand Down Expand Up @@ -44,6 +50,7 @@ add_llvm_target(BPFCodeGen
CodeGen
CodeGenTypes
Core
GlobalISel
IPO
MC
Scalar
Expand Down
46 changes: 46 additions & 0 deletions llvm/lib/Target/BPF/GISel/BPFCallLowering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===-- BPFCallLowering.cpp - Call lowering for GlobalISel ------*- 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the lowering of LLVM calls to machine code calls for
/// GlobalISel.
///
//===----------------------------------------------------------------------===//

#include "BPFCallLowering.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "bpf-call-lowering"

using namespace llvm;

BPFCallLowering::BPFCallLowering(const BPFTargetLowering &TLI)
: CallLowering(&TLI) {}

bool BPFCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
const Value *Val, ArrayRef<Register> VRegs,
FunctionLoweringInfo &FLI,
Register SwiftErrorVReg) const {
if (!VRegs.empty())
return false;
MIRBuilder.buildInstr(BPF::RET);
return true;
}

bool BPFCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
const Function &F,
ArrayRef<ArrayRef<Register>> VRegs,
FunctionLoweringInfo &FLI) const {
return VRegs.empty();
}

bool BPFCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
CallLoweringInfo &Info) const {
return false;
}
39 changes: 39 additions & 0 deletions llvm/lib/Target/BPF/GISel/BPFCallLowering.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===-- BPFCallLowering.h - Call lowering for GlobalISel --------*- 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file describes how to lower LLVM calls to machine code calls.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_BPF_GISEL_BPFCALLLOWERING_H
#define LLVM_LIB_TARGET_BPF_GISEL_BPFCALLLOWERING_H

#include "BPFISelLowering.h"
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
#include "llvm/IR/CallingConv.h"

namespace llvm {

class BPFTargetLowering;

class BPFCallLowering : public CallLowering {
public:
BPFCallLowering(const BPFTargetLowering &TLI);
bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
ArrayRef<Register> VRegs, FunctionLoweringInfo &FLI,
Register SwiftErrorVReg) const override;
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F,
ArrayRef<ArrayRef<Register>> VRegs,
FunctionLoweringInfo &FLI) const override;
bool lowerCall(MachineIRBuilder &MIRBuilder,
CallLoweringInfo &Info) const override;
};
} // namespace llvm

#endif
91 changes: 91 additions & 0 deletions llvm/lib/Target/BPF/GISel/BPFInstructionSelector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//===- BPFInstructionSelector.cpp --------------------------------*- 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
//
//===----------------------------------------------------------------------===//
/// \file
/// This file implements the targeting of the InstructionSelector class for BPF.
//===----------------------------------------------------------------------===//

#include "BPFInstrInfo.h"
#include "BPFRegisterBankInfo.h"
#include "BPFSubtarget.h"
#include "BPFTargetMachine.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/IR/IntrinsicsBPF.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "bpf-gisel"

using namespace llvm;

namespace {

#define GET_GLOBALISEL_PREDICATE_BITSET
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATE_BITSET

class BPFInstructionSelector : public InstructionSelector {
public:
BPFInstructionSelector(const BPFTargetMachine &TM, const BPFSubtarget &STI,
const BPFRegisterBankInfo &RBI);

bool select(MachineInstr &I) override;
static const char *getName() { return DEBUG_TYPE; }

private:
/// tblgen generated 'select' implementation that is used as the initial
/// selector for the patterns that do not require complex C++.
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;

const BPFInstrInfo &TII;
const BPFRegisterInfo &TRI;
const BPFRegisterBankInfo &RBI;

#define GET_GLOBALISEL_PREDICATES_DECL
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATES_DECL

#define GET_GLOBALISEL_TEMPORARIES_DECL
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_TEMPORARIES_DECL
};

} // namespace

#define GET_GLOBALISEL_IMPL
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_IMPL

BPFInstructionSelector::BPFInstructionSelector(const BPFTargetMachine &TM,
const BPFSubtarget &STI,
const BPFRegisterBankInfo &RBI)
: TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()), RBI(RBI),
#define GET_GLOBALISEL_PREDICATES_INIT
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_PREDICATES_INIT
#define GET_GLOBALISEL_TEMPORARIES_INIT
#include "BPFGenGlobalISel.inc"
#undef GET_GLOBALISEL_TEMPORARIES_INIT
{
}

bool BPFInstructionSelector::select(MachineInstr &I) {
if (selectImpl(I, *CoverageInfo))
return true;
return false;
}

namespace llvm {
InstructionSelector *
createBPFInstructionSelector(const BPFTargetMachine &TM,
const BPFSubtarget &Subtarget,
const BPFRegisterBankInfo &RBI) {
return new BPFInstructionSelector(TM, Subtarget, RBI);
}
} // namespace llvm
22 changes: 22 additions & 0 deletions llvm/lib/Target/BPF/GISel/BPFLegalizerInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===- BPFLegalizerInfo.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
//
//===----------------------------------------------------------------------===//
/// \file
/// This file implements the targeting of the Machinelegalizer class for BPF
//===----------------------------------------------------------------------===//

#include "BPFLegalizerInfo.h"
#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "bpf-legalinfo"

using namespace llvm;
using namespace LegalizeActions;

BPFLegalizerInfo::BPFLegalizerInfo(const BPFSubtarget &ST) {
getLegacyLegalizerInfo().computeTables();
}
Loading