diff options
Diffstat (limited to 'backend/include/rcc')
| -rw-r--r-- | backend/include/rcc/Config/Targets.def | 7 | ||||
| -rw-r--r-- | backend/include/rcc/MC/TargetRegistry.h | 89 | ||||
| -rw-r--r-- | backend/include/rcc/Support/TargetSelect.h | 18 | ||||
| -rw-r--r-- | backend/include/rcc/Target/RISCV/RISCVPipeline.h | 14 | ||||
| -rw-r--r-- | backend/include/rcc/Target/RISCV/RISCVSubtarget.h | 8 | ||||
| -rw-r--r-- | backend/include/rcc/Target/TargetMachine.h | 69 | ||||
| -rw-r--r-- | backend/include/rcc/Target/TargetOptions.h | 49 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/BackendDriver.h | 14 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/BackendInvocation.h | 18 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/PipelineDriver.h | 6 |
10 files changed, 254 insertions, 38 deletions
diff --git a/backend/include/rcc/Config/Targets.def b/backend/include/rcc/Config/Targets.def new file mode 100644 index 0000000..e6cdd96 --- /dev/null +++ b/backend/include/rcc/Config/Targets.def @@ -0,0 +1,7 @@ +#ifndef RCC_TARGET +#define RCC_TARGET(TargetName) +#endif + +RCC_TARGET(RISCV) + +#undef RCC_TARGET diff --git a/backend/include/rcc/MC/TargetRegistry.h b/backend/include/rcc/MC/TargetRegistry.h new file mode 100644 index 0000000..3e1de7c --- /dev/null +++ b/backend/include/rcc/MC/TargetRegistry.h @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_MC_TARGETREGISTRY_H +#define RCC_MC_TARGETREGISTRY_H + +#include "rcc/Support/Error.h" + +#include <memory> +#include <span> +#include <string> +#include <string_view> + +namespace rcc { + +class TargetMachine; +struct TargetMachineOptions; + +class Target final { +public: + using MatchQualityFn = unsigned (*)(std::string_view Triple); + using NormalizeTripleFn = std::string (*)(std::string_view Triple); + using DefaultOptionsFn = TargetMachineOptions (*)(); + using DefaultABIFn = std::string (*)(std::span<const std::string> Features); + using TargetMachineCtorFn = Expected<std::unique_ptr<TargetMachine>> (*)( + const Target &RegisteredTarget, TargetMachineOptions Options); + + constexpr Target(std::string_view Name, std::string_view Description, bool IsDefault, + MatchQualityFn MatchQuality, NormalizeTripleFn NormalizeTriple, + DefaultOptionsFn DefaultOptions, DefaultABIFn DefaultABI, + std::span<const std::string_view> BaselineFeatures, + TargetMachineCtorFn TargetMachineCtor) noexcept + : Name(Name), Description(Description), IsDefault(IsDefault), MatchQuality(MatchQuality), + NormalizeTriple(NormalizeTriple), DefaultOptions(DefaultOptions), DefaultABI(DefaultABI), + BaselineFeatures(BaselineFeatures), TargetMachineCtor(TargetMachineCtor) {} + + [[nodiscard]] std::string_view name() const noexcept { + return Name; + } + + [[nodiscard]] std::string_view description() const noexcept { + return Description; + } + + [[nodiscard]] bool isDefault() const noexcept { + return IsDefault; + } + + [[nodiscard]] unsigned matchQuality(std::string_view Triple) const noexcept { + return MatchQuality(Triple); + } + + [[nodiscard]] std::string normalizeTriple(std::string_view Triple) const { + return NormalizeTriple(Triple); + } + + [[nodiscard]] TargetMachineOptions defaultMachineOptions() const; + [[nodiscard]] std::string defaultABI(std::span<const std::string> Features) const { + return DefaultABI(Features); + } + + [[nodiscard]] std::span<const std::string_view> baselineFeatures() const noexcept { + return BaselineFeatures; + } + + [[nodiscard]] Expected<std::unique_ptr<TargetMachine>> + createTargetMachine(TargetMachineOptions Options) const; + +private: + std::string_view Name; + std::string_view Description; + bool IsDefault = false; + MatchQualityFn MatchQuality = nullptr; + NormalizeTripleFn NormalizeTriple = nullptr; + DefaultOptionsFn DefaultOptions = nullptr; + DefaultABIFn DefaultABI = nullptr; + std::span<const std::string_view> BaselineFeatures; + TargetMachineCtorFn TargetMachineCtor = nullptr; +}; + +class TargetRegistry final { +public: + static void registerTarget(const Target &RegisteredTarget); + [[nodiscard]] static Expected<const Target *> lookupTarget(std::string_view Triple); + [[nodiscard]] static Expected<const Target *> defaultTarget(); +}; + +} // namespace rcc + +#endif // RCC_MC_TARGETREGISTRY_H diff --git a/backend/include/rcc/Support/TargetSelect.h b/backend/include/rcc/Support/TargetSelect.h new file mode 100644 index 0000000..f56090b --- /dev/null +++ b/backend/include/rcc/Support/TargetSelect.h @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_SUPPORT_TARGETSELECT_H +#define RCC_SUPPORT_TARGETSELECT_H + +namespace rcc { + +#define RCC_TARGET(TargetName) void initialize##TargetName##Target(); +#include "rcc/Config/Targets.def" + +inline void initializeAllTargets() { +#define RCC_TARGET(TargetName) initialize##TargetName##Target(); +#include "rcc/Config/Targets.def" +} + +} // namespace rcc + +#endif // RCC_SUPPORT_TARGETSELECT_H diff --git a/backend/include/rcc/Target/RISCV/RISCVPipeline.h b/backend/include/rcc/Target/RISCV/RISCVPipeline.h index 3e8ea3d..f556ded 100644 --- a/backend/include/rcc/Target/RISCV/RISCVPipeline.h +++ b/backend/include/rcc/Target/RISCV/RISCVPipeline.h @@ -3,10 +3,10 @@ #ifndef RCC_TARGET_RISCV_RISCVPIPELINE_H #define RCC_TARGET_RISCV_RISCVPIPELINE_H -#include "rcc/CodeGen/CommandFlags.h" #include "rcc/CodeGen/MachinePassManager.h" #include "rcc/Support/Error.h" #include "rcc/Target/RISCV/RISCVSubtarget.h" +#include "rcc/Target/TargetMachine.h" #include <memory> #include <string> @@ -21,21 +21,13 @@ class TargetMachineOutlinerInfo; namespace riscv { -struct RISCVPipelineOptions final { - OptimizationLevel Optimization = OptimizationLevel::O2; - bool UseTargetCopyInstructions = false; - GlobalISelAbortMode GlobalISelAbort = GlobalISelAbortMode::Enable; - bool DebugMachineScheduler = false; - bool DebugPrologueEpilogue = false; -}; - [[nodiscard]] Expected<void> addCanonicalPipeline(MachineFunctionPassManager &Pipeline, const MachineFunction &Function, const RISCVSubtarget &Subtarget, - RISCVPipelineOptions Options = {}); + TargetPipelineOptions Options = {}); [[nodiscard]] Expected<std::unique_ptr<MachineFunctionPass>> createCanonicalPipelinePass(std::string_view Name, const RISCVSubtarget &Subtarget, - RISCVPipelineOptions Options = {}); + TargetPipelineOptions Options = {}); [[nodiscard]] Expected<std::vector<std::string>> canonicalPipelinePassNames(const MachineFunction &Function, const RISCVSubtarget &Subtarget); [[nodiscard]] Expected<std::unique_ptr<TargetMachineOutlinerInfo>> diff --git a/backend/include/rcc/Target/RISCV/RISCVSubtarget.h b/backend/include/rcc/Target/RISCV/RISCVSubtarget.h index 52e4b03..a5ba01a 100644 --- a/backend/include/rcc/Target/RISCV/RISCVSubtarget.h +++ b/backend/include/rcc/Target/RISCV/RISCVSubtarget.h @@ -142,10 +142,10 @@ private: CodeModel Code; RISCVABI ABI; std::unique_ptr<TargetFrameLowering> FrameLoweringInfo; - mutable std::unique_ptr<TargetCallLowering> CallLoweringInfo; - mutable std::unique_ptr<InstructionSelector> InstSelector; - mutable std::unique_ptr<LegalizerInfo> Legalizer; - mutable std::unique_ptr<RISCVRegisterBankInfo> RegBankInfo; + std::unique_ptr<TargetCallLowering> CallLoweringInfo; + std::unique_ptr<InstructionSelector> InstSelector; + std::unique_ptr<LegalizerInfo> Legalizer; + std::unique_ptr<RISCVRegisterBankInfo> RegBankInfo; std::unique_ptr<TargetSchedulingHooks> SchedulingHooks; std::unique_ptr<TargetSchedulingModel> SchedulingModel; }; diff --git a/backend/include/rcc/Target/TargetMachine.h b/backend/include/rcc/Target/TargetMachine.h new file mode 100644 index 0000000..e45b8ca --- /dev/null +++ b/backend/include/rcc/Target/TargetMachine.h @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_TARGET_TARGETMACHINE_H +#define RCC_TARGET_TARGETMACHINE_H + +#include "rcc/CodeGen/MachineOutliner.h" +#include "rcc/CodeGen/MachinePassManager.h" +#include "rcc/CodeGen/TargetDescription.h" +#include "rcc/Support/Error.h" +#include "rcc/Target/TargetOptions.h" + +#include <cstdint> +#include <memory> +#include <string> +#include <string_view> +#include <vector> + +namespace rcc { + +class CompilationUnit; +class MachineFunction; +class Target; + +class TargetMachine { +public: + TargetMachine(const TargetMachine &) = delete; + TargetMachine &operator=(const TargetMachine &) = delete; + TargetMachine(TargetMachine &&) = delete; + TargetMachine &operator=(TargetMachine &&) = delete; + virtual ~TargetMachine(); + + [[nodiscard]] const Target &target() const noexcept { + return RegisteredTarget; + } + + [[nodiscard]] const TargetDescription &targetDescription() const noexcept { + return Description; + } + + [[nodiscard]] const TargetMachineOptions &options() const noexcept { + return Options; + } + + [[nodiscard]] virtual Expected<void> + addCodeGenPasses(MachineFunctionPassManager &Pipeline, const MachineFunction &Function, + TargetPipelineOptions PipelineOptions) const = 0; + [[nodiscard]] virtual Expected<std::unique_ptr<MachineFunctionPass>> + createMachinePass(std::string_view Name, TargetPipelineOptions PipelineOptions) const = 0; + [[nodiscard]] virtual Expected<std::vector<std::string>> + codeGenPassNames(const MachineFunction &Function) const = 0; + [[nodiscard]] virtual Expected<std::unique_ptr<TargetMachineOutlinerInfo>> + createOutlinerInfo() const = 0; + [[nodiscard]] virtual Expected<std::string> emitAssembly(const CompilationUnit &Unit) const = 0; + [[nodiscard]] virtual Expected<std::vector<std::uint8_t>> + emitObject(const CompilationUnit &Unit) const = 0; + +protected: + TargetMachine(const Target &RegisteredTarget, TargetMachineOptions Options, + TargetDescription Description); + +private: + const Target &RegisteredTarget; + TargetMachineOptions Options; + TargetDescription Description; +}; + +} // namespace rcc + +#endif // RCC_TARGET_TARGETMACHINE_H diff --git a/backend/include/rcc/Target/TargetOptions.h b/backend/include/rcc/Target/TargetOptions.h new file mode 100644 index 0000000..1db183c --- /dev/null +++ b/backend/include/rcc/Target/TargetOptions.h @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_TARGET_TARGETOPTIONS_H +#define RCC_TARGET_TARGETOPTIONS_H + +#include "rcc/CodeGen/CommandFlags.h" + +#include <string> +#include <vector> + +namespace rcc { + +struct TargetOption final { + std::string Name; + std::string Value; + + friend bool operator==(const TargetOption &, const TargetOption &) = default; +}; + +struct TargetMachineOptions final { + std::string Triple; + std::string CPU; + std::string TuneCPU; + std::vector<std::string> Features; + std::string ABI; + std::string DataLayout; + RelocationModel Relocation = RelocationModel::Static; + CodeModel Code = CodeModel::Small; + OptimizationLevel Optimization = OptimizationLevel::O2; + std::vector<TargetOption> Options; + std::string ABILoweringVersion; + std::string AssemblerFlavor; + std::string AssemblerVersion; + std::vector<TargetOption> AssemblerOptions; + + friend bool operator==(const TargetMachineOptions &, const TargetMachineOptions &) = default; +}; + +struct TargetPipelineOptions final { + OptimizationLevel Optimization = OptimizationLevel::O2; + bool UseTargetCopyInstructions = false; + GlobalISelAbortMode GlobalISelAbort = GlobalISelAbortMode::Enable; + bool DebugMachineScheduler = false; + bool DebugPrologueEpilogue = false; +}; + +} // namespace rcc + +#endif // RCC_TARGET_TARGETOPTIONS_H diff --git a/backend/include/rcc/Tooling/BackendDriver.h b/backend/include/rcc/Tooling/BackendDriver.h index 8117a0c..2003983 100644 --- a/backend/include/rcc/Tooling/BackendDriver.h +++ b/backend/include/rcc/Tooling/BackendDriver.h @@ -13,11 +13,7 @@ #include <string_view> #include <vector> -namespace rcc { - -class TargetDescription; - -namespace tooling { +namespace rcc::tooling { struct BackendRunResult final { ArtifactKind Kind = ArtifactKind::FinalMIR; @@ -29,11 +25,9 @@ struct BackendRunResult final { }; [[nodiscard]] Expected<BackendRunResult> -runBackend(CanonicalMIR Input, const TargetDescription &Target, const BackendInvocation &Invocation, - ArtifactKind Output, PipelineControls Controls = {}, - MachinePassInstrumentation *Instrumentation = nullptr); +runBackend(CanonicalMIR Input, const BackendInvocation &Invocation, ArtifactKind Output, + PipelineControls Controls = {}, MachinePassInstrumentation *Instrumentation = nullptr); -} // namespace tooling -} // namespace rcc +} // namespace rcc::tooling #endif // RCC_TOOLING_BACKENDDRIVER_H diff --git a/backend/include/rcc/Tooling/BackendInvocation.h b/backend/include/rcc/Tooling/BackendInvocation.h index 504dc3a..c50148a 100644 --- a/backend/include/rcc/Tooling/BackendInvocation.h +++ b/backend/include/rcc/Tooling/BackendInvocation.h @@ -5,8 +5,10 @@ #include "rcc/CodeGen/CommandFlags.h" #include "rcc/Support/Error.h" +#include "rcc/Target/TargetOptions.h" #include <cstdint> +#include <memory> #include <optional> #include <span> #include <string> @@ -16,6 +18,8 @@ namespace rcc { class TargetDescription; +class Target; +class TargetMachine; namespace tooling { @@ -23,7 +27,6 @@ inline constexpr std::uint32_t BackendInvocationSchemaVersion = 1; inline constexpr std::string_view LLVMReferenceRevision = "2b40c303e79f64c5c7642032554eda026594d604"; inline constexpr std::string_view GenericOpcodeReferenceVersion = "rcc-generic-opcode-v1"; -inline constexpr std::string_view RV64GCTargetABILoweringVersion = "rcc-rv64gc-lp64d-v1"; inline constexpr std::string_view CompilerBuildFingerprint = "rcc-backend-0.1.0-stage17"; enum class LayoutOwnership : std::uint8_t { @@ -32,12 +35,7 @@ enum class LayoutOwnership : std::uint8_t { ExternalRelaxation, }; -struct BackendOption final { - std::string Name; - std::string Value; - - friend bool operator==(const BackendOption &, const BackendOption &) = default; -}; +using BackendOption = TargetOption; struct DeterminismOptions final { std::uint64_t RandomSeed = 0; @@ -69,7 +67,7 @@ struct BackendInvocation final { std::vector<std::string> EnabledActions; std::string MIRReferenceRevision{LLVMReferenceRevision}; std::string GenericOpcodeVersion{GenericOpcodeReferenceVersion}; - std::string TargetABILoweringVersion{RV64GCTargetABILoweringVersion}; + std::string TargetABILoweringVersion; std::string BuildFingerprint{CompilerBuildFingerprint}; [[nodiscard]] Expected<void> canonicalizeAndVerify(); @@ -78,7 +76,9 @@ struct BackendInvocation final { friend bool operator==(const BackendInvocation &, const BackendInvocation &) = default; }; -[[nodiscard]] BackendInvocation defaultRV64GCInvocation(); +[[nodiscard]] Expected<BackendInvocation> defaultBackendInvocation(const Target &Target); +[[nodiscard]] Expected<std::unique_ptr<TargetMachine>> +createTargetMachine(const BackendInvocation &Invocation); [[nodiscard]] Expected<std::string> computeTargetDescriptionFingerprint(const TargetDescription &Target); [[nodiscard]] Expected<std::string> computeCacheKey(std::string_view CanonicalMIR, diff --git a/backend/include/rcc/Tooling/PipelineDriver.h b/backend/include/rcc/Tooling/PipelineDriver.h index c01650c..ce88e64 100644 --- a/backend/include/rcc/Tooling/PipelineDriver.h +++ b/backend/include/rcc/Tooling/PipelineDriver.h @@ -5,6 +5,7 @@ #include "rcc/CodeGen/MIRParser/MIRParser.h" #include "rcc/CodeGen/MachinePassManager.h" +#include "rcc/Target/TargetMachine.h" #include "rcc/Tooling/BackendInvocation.h" #include <cstdint> @@ -15,8 +16,6 @@ namespace rcc { -class TargetDescription; - namespace tooling { enum class BackendStage : std::uint8_t { @@ -72,9 +71,8 @@ struct PipelineRunResult final { std::vector<PipelineSnapshot> Snapshots; }; -[[nodiscard]] Expected<void> verifyRV64GCInvocation(const BackendInvocation &Invocation); [[nodiscard]] Expected<PipelineRunResult> -runRV64GCPipeline(mir::MIRDocument &Document, const TargetDescription &Target, +runTargetPipeline(mir::MIRDocument &Document, const TargetMachine &TargetMachine, const BackendInvocation &Invocation, PipelineControls Controls = {}, MachinePassInstrumentation *Instrumentation = nullptr); [[nodiscard]] BackendStage stageForFunction(const MachineFunction &Function) noexcept; |
