| 1 | //===-- AMDGPUTargetMachine.h - AMDGPU TargetMachine Interface --*- 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 | /// The AMDGPU TargetMachine interface definition for hw codegen targets. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETMACHINE_H |
| 15 | #define LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETMACHINE_H |
| 16 | |
| 17 | #include "GCNSubtarget.h" |
| 18 | #include "llvm/CodeGen/CodeGenTargetMachineImpl.h" |
| 19 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 20 | #include "llvm/MC/MCStreamer.h" |
| 21 | #include "llvm/Target/LastSubtargetCache.h" |
| 22 | #include <optional> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // AMDGPU Target Machine (R600+) |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | namespace AMDGPU { |
| 32 | StringRef getSchedStrategy(const AMDGPUOptions &CLOpts, const Function &F); |
| 33 | } |
| 34 | |
| 35 | class AMDGPUTargetMachine : public CodeGenTargetMachineImpl { |
| 36 | protected: |
| 37 | const AMDGPUOptions &CLOpts; |
| 38 | std::unique_ptr<TargetLoweringObjectFile> TLOF; |
| 39 | |
| 40 | public: |
| 41 | static bool EnableFunctionCalls; |
| 42 | static bool EnableObjectLinking; |
| 43 | static bool EnableLowerModuleLDS; |
| 44 | |
| 45 | AMDGPUTargetMachine(const Target &T, const Triple &TT, StringRef CPU, |
| 46 | StringRef FS, const TargetOptions &Options, |
| 47 | std::optional<Reloc::Model> RM, |
| 48 | std::optional<CodeModel::Model> CM, CodeGenOptLevel OL); |
| 49 | ~AMDGPUTargetMachine() override; |
| 50 | |
| 51 | const AMDGPUOptions &getCLOpts() const { return CLOpts; } |
| 52 | |
| 53 | const TargetSubtargetInfo * |
| 54 | getSubtargetImpl(const Function &) const override = 0; |
| 55 | |
| 56 | TargetLoweringObjectFile *getObjFileLowering() const override { |
| 57 | return TLOF.get(); |
| 58 | } |
| 59 | |
| 60 | void registerPassBuilderCallbacks(PassBuilder &PB) override; |
| 61 | void registerDefaultAliasAnalyses(AAManager &) override; |
| 62 | |
| 63 | bool isNoopAddrSpaceCast(const DataLayout &DL, unsigned SrcAS, |
| 64 | unsigned DestAS) const override; |
| 65 | |
| 66 | unsigned getAssumedAddrSpace(const Value *V) const override; |
| 67 | |
| 68 | std::pair<const Value *, unsigned> |
| 69 | getPredicatedAddrSpace(const Value *V) const override; |
| 70 | |
| 71 | unsigned getAddressSpaceForPseudoSourceKind(unsigned Kind) const override; |
| 72 | |
| 73 | bool splitModule(Module &M, unsigned NumParts, |
| 74 | function_ref<void(std::unique_ptr<Module> MPart)> |
| 75 | ModuleCallback) override; |
| 76 | ScheduleDAGInstrs * |
| 77 | createMachineScheduler(MachineSchedContext *C) const override; |
| 78 | }; |
| 79 | |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | // GCN Target Machine (SI+) |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
| 84 | class GCNTargetMachine final : public AMDGPUTargetMachine { |
| 85 | private: |
| 86 | mutable StringMap<std::unique_ptr<GCNSubtarget>> SubtargetMap; |
| 87 | |
| 88 | /// The attributes that select a subtarget. |
| 89 | struct SubtargetAttrs { |
| 90 | Attribute GPU, FS; |
| 91 | bool BufRelaxed = false; |
| 92 | bool TBufRelaxed = false; |
| 93 | AMDGPU::TargetIDSetting Xnack = AMDGPU::TargetIDSetting::Any; |
| 94 | AMDGPU::TargetIDSetting SramEcc = AMDGPU::TargetIDSetting::Any; |
| 95 | |
| 96 | bool operator==(const SubtargetAttrs &RHS) const { |
| 97 | return GPU == RHS.GPU && FS == RHS.FS && BufRelaxed == RHS.BufRelaxed && |
| 98 | TBufRelaxed == RHS.TBufRelaxed && Xnack == RHS.Xnack && |
| 99 | SramEcc == RHS.SramEcc; |
| 100 | } |
| 101 | |
| 102 | bool operator!=(const SubtargetAttrs &RHS) const { return !(*this == RHS); } |
| 103 | }; |
| 104 | mutable LastSubtargetCache<GCNSubtarget, SubtargetAttrs> LastSubtarget; |
| 105 | |
| 106 | public: |
| 107 | GCNTargetMachine(const Target &T, const Triple &TT, StringRef CPU, |
| 108 | StringRef FS, const TargetOptions &Options, |
| 109 | std::optional<Reloc::Model> RM, |
| 110 | std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, |
| 111 | bool JIT); |
| 112 | |
| 113 | TargetPassConfig *createPassConfig(PassManagerBase &PM) override; |
| 114 | |
| 115 | const TargetSubtargetInfo *getSubtargetImpl(const Function &) const override; |
| 116 | |
| 117 | TargetTransformInfo getTargetTransformInfo(const Function &F) const override; |
| 118 | |
| 119 | bool useIPRA() const override { return true; } |
| 120 | |
| 121 | Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM, |
| 122 | raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, |
| 123 | CodeGenFileType FileType, |
| 124 | const CGPassBuilderOption &Opts, MCContext &Ctx, |
| 125 | PassInstrumentationCallbacks *PIC) override; |
| 126 | |
| 127 | void registerMachineRegisterInfoCallback(MachineFunction &MF) const override; |
| 128 | |
| 129 | /// Get xnack/sramecc setting from module flag or command line option (for |
| 130 | /// testing). |
| 131 | /// Returns Any if not specified. |
| 132 | AMDGPU::TargetIDSetting |
| 133 | getTargetIDSettingFromModuleFlag(const Module &M, StringRef FlagName) const; |
| 134 | |
| 135 | MachineFunctionInfo * |
| 136 | createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F, |
| 137 | const TargetSubtargetInfo *STI) const override; |
| 138 | |
| 139 | yaml::MachineFunctionInfo *createDefaultFuncInfoYAML() const override; |
| 140 | yaml::MachineFunctionInfo * |
| 141 | convertFuncInfoToYAML(const MachineFunction &MF) const override; |
| 142 | bool parseMachineFunctionInfo(const yaml::MachineFunctionInfo &, |
| 143 | PerFunctionMIParsingState &PFS, |
| 144 | SMDiagnostic &Error, |
| 145 | SMRange &SourceRange) const override; |
| 146 | ScheduleDAGInstrs * |
| 147 | createMachineScheduler(MachineSchedContext *C) const override; |
| 148 | ScheduleDAGInstrs * |
| 149 | createPostMachineScheduler(MachineSchedContext *C) const override; |
| 150 | }; |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // AMDGPU Pass Setup - For Legacy Pass Manager. |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | |
| 156 | class AMDGPUPassConfig : public TargetPassConfig { |
| 157 | protected: |
| 158 | const AMDGPUOptions &CLOpts; |
| 159 | |
| 160 | public: |
| 161 | AMDGPUPassConfig(TargetMachine &TM, PassManagerBase &PM); |
| 162 | |
| 163 | AMDGPUTargetMachine &getAMDGPUTargetMachine() const { |
| 164 | return getTM<AMDGPUTargetMachine>(); |
| 165 | } |
| 166 | void addEarlyCSEOrGVNPass(); |
| 167 | void addStraightLineScalarOptimizationPasses(); |
| 168 | void addIRPasses() override; |
| 169 | void addCodeGenPrepare() override; |
| 170 | bool addPreISel() override; |
| 171 | bool addInstSelector() override; |
| 172 | bool addGCPasses() override; |
| 173 | |
| 174 | std::unique_ptr<CSEConfigBase> getCSEConfig() const override; |
| 175 | |
| 176 | /// Check if a pass is enabled given \p Opt option. The option always |
| 177 | /// overrides defaults if explicitly used. Otherwise the pass is enabled at an |
| 178 | /// optimization \p Level minimum. |
| 179 | bool isPassEnabled(BoolOrDefault Opt, |
| 180 | CodeGenOptLevel Level = CodeGenOptLevel::Default) const { |
| 181 | return valueOr(X: Opt, Default: TM->getOptLevel() >= Level); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | } // end namespace llvm |
| 186 | |
| 187 | #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUTARGETMACHINE_H |
| 188 | |