From 0bb35fa2306a66f1f0baf118619b5c1640ed9c01 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Wed, 12 Aug 2026 21:56:37 +0800 Subject: [GlobalISel] Split the generic pipeline components --- .../rcc/CodeGen/GlobalISel/InstructionSelect.h | 31 +++++++ .../rcc/CodeGen/GlobalISel/InstructionSelector.h | 35 ++++++++ backend/include/rcc/CodeGen/GlobalISel/Legalizer.h | 36 ++++++++ .../include/rcc/CodeGen/GlobalISel/LegalizerInfo.h | 54 ++++++++++++ .../include/rcc/CodeGen/GlobalISel/RegBankSelect.h | 27 ++++++ .../include/rcc/CodeGen/MachinePipelinePasses.h | 68 ---------------- backend/include/rcc/CodeGen/RegisterBankInfo.h | 39 +++++++++ backend/include/rcc/CodeGen/TargetCodeGen.h | 95 ---------------------- backend/include/rcc/Target/RISCV/RISCVCodeGen.h | 13 +-- 9 files changed, 229 insertions(+), 169 deletions(-) create mode 100644 backend/include/rcc/CodeGen/GlobalISel/InstructionSelect.h create mode 100644 backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h create mode 100644 backend/include/rcc/CodeGen/GlobalISel/Legalizer.h create mode 100644 backend/include/rcc/CodeGen/GlobalISel/LegalizerInfo.h create mode 100644 backend/include/rcc/CodeGen/GlobalISel/RegBankSelect.h delete mode 100644 backend/include/rcc/CodeGen/MachinePipelinePasses.h create mode 100644 backend/include/rcc/CodeGen/RegisterBankInfo.h delete mode 100644 backend/include/rcc/CodeGen/TargetCodeGen.h (limited to 'backend/include') diff --git a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelect.h b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelect.h new file mode 100644 index 0000000..0c034cd --- /dev/null +++ b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelect.h @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECT_H +#define RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECT_H + +#include "rcc/CodeGen/GlobalISel/InstructionSelector.h" +#include "rcc/CodeGen/MachinePass.h" + +namespace rcc { + +class InstructionSelect final : public MachineFunctionPass { +public: + explicit InstructionSelect(const InstructionSelector *Info) : Info(Info) {} + explicit InstructionSelect(const InstructionSelector &Info) : Info(&Info) {} + + [[nodiscard]] const MachinePassContract &contract() const noexcept override; + [[nodiscard]] Expected run(MachineFunction &Function, + const TargetDescription &Target, + MachineAnalysisManager &Analyses) override; + +private: + [[nodiscard]] Expected runImplementation(MachineFunction &Function, + const TargetDescription &Target, + MachineActionReplay &Actions); + + const InstructionSelector *Info; +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECT_H diff --git a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h new file mode 100644 index 0000000..4c69606 --- /dev/null +++ b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H +#define RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H + +#include "rcc/Support/Error.h" + +namespace rcc { + +class MachineFunction; +class MachineInstr; +class TargetDescription; + +class InstructionSelector { +public: + virtual ~InstructionSelector() = default; + + [[nodiscard]] virtual Expected select(MachineInstr &Instruction, MachineFunction &Function, + const TargetDescription &Target) const = 0; + + // Selection matching is read-only and completes before select() mutates MachineIR. + [[nodiscard]] virtual Expected canSelect(const MachineInstr &, const MachineFunction &, + const TargetDescription &) const { + return {}; + } + + [[nodiscard]] virtual Expected + constrainSelectedInstruction(MachineInstr &, MachineFunction &, const TargetDescription &) const { + return {}; + } +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H diff --git a/backend/include/rcc/CodeGen/GlobalISel/Legalizer.h b/backend/include/rcc/CodeGen/GlobalISel/Legalizer.h new file mode 100644 index 0000000..7f86d16 --- /dev/null +++ b/backend/include/rcc/CodeGen/GlobalISel/Legalizer.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_GLOBALISEL_LEGALIZER_H +#define RCC_CODEGEN_GLOBALISEL_LEGALIZER_H + +#include "rcc/CodeGen/GlobalISel/LegalizerInfo.h" +#include "rcc/CodeGen/MachinePass.h" + +#include + +namespace rcc { + +class Legalizer final : public MachineFunctionPass { +public: + explicit Legalizer(const LegalizerInfo *Info) : Info(Info) {} + explicit Legalizer(const LegalizerInfo &Info) : Info(&Info) {} + explicit Legalizer(std::unique_ptr Info) + : OwnedInfo(std::move(Info)), Info(OwnedInfo.get()) {} + + [[nodiscard]] const MachinePassContract &contract() const noexcept override; + [[nodiscard]] Expected run(MachineFunction &Function, + const TargetDescription &Target, + MachineAnalysisManager &Analyses) override; + +private: + [[nodiscard]] Expected runImplementation(MachineFunction &Function, + const TargetDescription &Target, + MachineActionReplay &Actions); + + std::unique_ptr OwnedInfo; + const LegalizerInfo *Info = nullptr; +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_GLOBALISEL_LEGALIZER_H diff --git a/backend/include/rcc/CodeGen/GlobalISel/LegalizerInfo.h b/backend/include/rcc/CodeGen/GlobalISel/LegalizerInfo.h new file mode 100644 index 0000000..218a4c6 --- /dev/null +++ b/backend/include/rcc/CodeGen/GlobalISel/LegalizerInfo.h @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_GLOBALISEL_LEGALIZERINFO_H +#define RCC_CODEGEN_GLOBALISEL_LEGALIZERINFO_H + +#include "rcc/Support/Error.h" + +#include + +namespace rcc { + +class MachineFunction; +class MachineInstr; +class TargetDescription; + +enum class LegalizationAction : std::uint8_t { + Legal, + Unsupported, + Custom, + Lower, + Libcall, + WidenScalar, + NarrowScalar, + MoreElements, + FewerElements, + Bitcast, + ChangeElementType, + ChangeAddressSpace, +}; + +class LegalizerInfo { +public: + virtual ~LegalizerInfo() = default; + + [[nodiscard]] virtual Expected + classify(const MachineInstr &Instruction, const MachineFunction &Function, + const TargetDescription &Target) const = 0; + + [[nodiscard]] virtual Expected lower(MachineInstr &Instruction, MachineFunction &Function, + const TargetDescription &Target, + LegalizationAction Action) const = 0; + + // This hook performs read-only target checks before a lowering mutation is committed. + [[nodiscard]] virtual Expected validateLowering(const MachineInstr &, + const MachineFunction &, + const TargetDescription &, + LegalizationAction) const { + return {}; + } +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_GLOBALISEL_LEGALIZERINFO_H diff --git a/backend/include/rcc/CodeGen/GlobalISel/RegBankSelect.h b/backend/include/rcc/CodeGen/GlobalISel/RegBankSelect.h new file mode 100644 index 0000000..256d748 --- /dev/null +++ b/backend/include/rcc/CodeGen/GlobalISel/RegBankSelect.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_GLOBALISEL_REGBANKSELECT_H +#define RCC_CODEGEN_GLOBALISEL_REGBANKSELECT_H + +#include "rcc/CodeGen/MachinePass.h" +#include "rcc/CodeGen/RegisterBankInfo.h" + +namespace rcc { + +class RegBankSelect final : public MachineFunctionPass { +public: + explicit RegBankSelect(const RegisterBankInfo *Info) : Info(Info) {} + explicit RegBankSelect(const RegisterBankInfo &Info) : Info(&Info) {} + + [[nodiscard]] const MachinePassContract &contract() const noexcept override; + [[nodiscard]] Expected run(MachineFunction &Function, + const TargetDescription &Target, + MachineAnalysisManager &Analyses) override; + +private: + const RegisterBankInfo *Info; +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_GLOBALISEL_REGBANKSELECT_H diff --git a/backend/include/rcc/CodeGen/MachinePipelinePasses.h b/backend/include/rcc/CodeGen/MachinePipelinePasses.h deleted file mode 100644 index 1187e4a..0000000 --- a/backend/include/rcc/CodeGen/MachinePipelinePasses.h +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef RCC_CODEGEN_MACHINEPIPELINEPASSES_H -#define RCC_CODEGEN_MACHINEPIPELINEPASSES_H - -#include "rcc/CodeGen/MachinePass.h" -#include "rcc/CodeGen/TargetCodeGen.h" - -#include - -namespace rcc { - -class LegalizerPass final : public MachineFunctionPass { -public: - explicit LegalizerPass(const TargetLegalizerInfo *Info) : Info(Info) {} - explicit LegalizerPass(const TargetLegalizerInfo &Info) : Info(&Info) {} - explicit LegalizerPass(std::unique_ptr Info) - : OwnedInfo(std::move(Info)), Info(OwnedInfo.get()) {} - - [[nodiscard]] const MachinePassContract &contract() const noexcept override; - [[nodiscard]] Expected run(MachineFunction &Function, - const TargetDescription &Target, - MachineAnalysisManager &Analyses) override; - -private: - [[nodiscard]] Expected runImplementation(MachineFunction &Function, - const TargetDescription &Target, - MachineActionReplay &Actions); - - std::unique_ptr OwnedInfo; - const TargetLegalizerInfo *Info = nullptr; -}; - -class RegisterBankSelectPass final : public MachineFunctionPass { -public: - explicit RegisterBankSelectPass(const TargetRegisterBankInfo *Info) : Info(Info) {} - explicit RegisterBankSelectPass(const TargetRegisterBankInfo &Info) : Info(&Info) {} - - [[nodiscard]] const MachinePassContract &contract() const noexcept override; - [[nodiscard]] Expected run(MachineFunction &Function, - const TargetDescription &Target, - MachineAnalysisManager &Analyses) override; - -private: - const TargetRegisterBankInfo *Info; -}; - -class InstructionSelectPass final : public MachineFunctionPass { -public: - explicit InstructionSelectPass(const TargetInstructionSelector *Info) : Info(Info) {} - explicit InstructionSelectPass(const TargetInstructionSelector &Info) : Info(&Info) {} - - [[nodiscard]] const MachinePassContract &contract() const noexcept override; - [[nodiscard]] Expected run(MachineFunction &Function, - const TargetDescription &Target, - MachineAnalysisManager &Analyses) override; - -private: - [[nodiscard]] Expected runImplementation(MachineFunction &Function, - const TargetDescription &Target, - MachineActionReplay &Actions); - - const TargetInstructionSelector *Info; -}; - -} // namespace rcc - -#endif // RCC_CODEGEN_MACHINEPIPELINEPASSES_H diff --git a/backend/include/rcc/CodeGen/RegisterBankInfo.h b/backend/include/rcc/CodeGen/RegisterBankInfo.h new file mode 100644 index 0000000..70b7340 --- /dev/null +++ b/backend/include/rcc/CodeGen/RegisterBankInfo.h @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_REGISTERBANKINFO_H +#define RCC_CODEGEN_REGISTERBANKINFO_H + +#include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/CodeGen/Register.h" +#include "rcc/Support/Error.h" + +namespace rcc { + +class MachineFunction; +class MachineInstr; +class TargetDescription; +struct VirtualRegisterInfo; + +class RegisterBankInfo { +public: + virtual ~RegisterBankInfo() = default; + + [[nodiscard]] virtual Expected + selectBank(const MachineFunction &Function, Register Reg, const VirtualRegisterInfo &Info, + const MachineInstr *Context, const TargetDescription &Target) const = 0; + + [[nodiscard]] virtual Expected + constrainInstruction(MachineInstr &Instruction, MachineFunction &Function, + const TargetDescription &Target) const = 0; + + // This hook rejects invalid mappings before bank or register-class state is published. + [[nodiscard]] virtual Expected validateInstruction(const MachineInstr &, + const MachineFunction &, + const TargetDescription &) const { + return {}; + } +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_REGISTERBANKINFO_H diff --git a/backend/include/rcc/CodeGen/TargetCodeGen.h b/backend/include/rcc/CodeGen/TargetCodeGen.h deleted file mode 100644 index 65f65a4..0000000 --- a/backend/include/rcc/CodeGen/TargetCodeGen.h +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef RCC_CODEGEN_TARGETCODEGEN_H -#define RCC_CODEGEN_TARGETCODEGEN_H - -#include "rcc/Support/Error.h" - -#include - -namespace rcc { - -class MachineFunction; -class MachineInstr; -class TargetDescription; -struct VirtualRegisterInfo; - -enum class LegalizationAction : std::uint8_t { - Legal, - Unsupported, - Custom, - Lower, - Libcall, - WidenScalar, - NarrowScalar, - MoreElements, - FewerElements, - Bitcast, - ChangeElementType, - ChangeAddressSpace, -}; - -class TargetLegalizerInfo { -public: - virtual ~TargetLegalizerInfo() = default; - - [[nodiscard]] virtual Expected - classify(const MachineInstr &Instruction, const MachineFunction &Function, - const TargetDescription &Target) const = 0; - - [[nodiscard]] virtual Expected lower(MachineInstr &Instruction, MachineFunction &Function, - const TargetDescription &Target, - LegalizationAction Action) const = 0; - - // This hook performs read-only target checks before a lowering mutation is committed. - [[nodiscard]] virtual Expected validateLowering(const MachineInstr &, - const MachineFunction &, - const TargetDescription &, - LegalizationAction) const { - return {}; - } -}; - -class TargetRegisterBankInfo { -public: - virtual ~TargetRegisterBankInfo() = default; - - [[nodiscard]] virtual Expected - selectBank(const MachineFunction &Function, Register Reg, const VirtualRegisterInfo &Info, - const MachineInstr *Context, const TargetDescription &Target) const = 0; - - [[nodiscard]] virtual Expected - constrainInstruction(MachineInstr &Instruction, MachineFunction &Function, - const TargetDescription &Target) const = 0; - - // This hook must not mutate MachineIR. It allows the pass to reject a bad mapping - // before publishing any bank or class assignment. - [[nodiscard]] virtual Expected validateInstruction(const MachineInstr &, - const MachineFunction &, - const TargetDescription &) const { - return {}; - } -}; - -class TargetInstructionSelector { -public: - virtual ~TargetInstructionSelector() = default; - - [[nodiscard]] virtual Expected select(MachineInstr &Instruction, MachineFunction &Function, - const TargetDescription &Target) const = 0; - - // Selection matching is read-only and must complete before select() mutates MachineIR. - [[nodiscard]] virtual Expected canSelect(const MachineInstr &, const MachineFunction &, - const TargetDescription &) const { - return {}; - } - - [[nodiscard]] virtual Expected - constrainSelectedInstruction(MachineInstr &, MachineFunction &, const TargetDescription &) const { - return {}; - } -}; - -} // namespace rcc - -#endif // RCC_CODEGEN_TARGETCODEGEN_H diff --git a/backend/include/rcc/Target/RISCV/RISCVCodeGen.h b/backend/include/rcc/Target/RISCV/RISCVCodeGen.h index 6cda815..37f1c97 100644 --- a/backend/include/rcc/Target/RISCV/RISCVCodeGen.h +++ b/backend/include/rcc/Target/RISCV/RISCVCodeGen.h @@ -3,9 +3,11 @@ #ifndef RCC_TARGET_RISCV_RISCVCODEGEN_H #define RCC_TARGET_RISCV_RISCVCODEGEN_H +#include "rcc/CodeGen/GlobalISel/InstructionSelector.h" +#include "rcc/CodeGen/GlobalISel/LegalizerInfo.h" #include "rcc/CodeGen/MachineCombinerPattern.h" #include "rcc/CodeGen/MachineOptimizationPipeline.h" -#include "rcc/CodeGen/TargetCodeGen.h" +#include "rcc/CodeGen/RegisterBankInfo.h" #include "rcc/CodeGen/TargetFrameLowering.h" #include "rcc/CodeGen/TargetMemoryFolding.h" #include "rcc/CodeGen/TargetPseudoExpansion.h" @@ -17,11 +19,10 @@ namespace rcc::riscv { -[[nodiscard]] const TargetLegalizerInfo &legalizerInfo() noexcept; -[[nodiscard]] std::unique_ptr -createLegalizerInfo(RISCVSubtargetFeatures Features); -[[nodiscard]] const TargetRegisterBankInfo ®isterBankInfo() noexcept; -[[nodiscard]] const TargetInstructionSelector &instructionSelector() noexcept; +[[nodiscard]] const LegalizerInfo &legalizerInfo() noexcept; +[[nodiscard]] std::unique_ptr createLegalizerInfo(RISCVSubtargetFeatures Features); +[[nodiscard]] const RegisterBankInfo ®isterBankInfo() noexcept; +[[nodiscard]] const InstructionSelector &instructionSelector() noexcept; [[nodiscard]] const TargetRegisterAllocationInfo ®isterAllocationInfo() noexcept; [[nodiscard]] const TargetFrameLowering &frameLowering() noexcept; [[nodiscard]] const TargetSchedulingModel &schedulingModel() noexcept; -- cgit v1.2.3