| 1 | //===-- R600TargetMachine.cpp - TargetMachine for hw codegen targets-------===// |
| 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 contains both AMDGPU-R600 target machine and the CodeGen pass |
| 11 | /// builder. The target machine contains all of the hardware specific |
| 12 | /// information needed to emit code for R600 GPUs and the CodeGen pass builder |
| 13 | /// handles the pass pipeline for new pass manager. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "R600TargetMachine.h" |
| 18 | #include "R600.h" |
| 19 | #include "R600MachineFunctionInfo.h" |
| 20 | #include "R600MachineScheduler.h" |
| 21 | #include "R600TargetTransformInfo.h" |
| 22 | #include "llvm/Passes/CodeGenPassBuilder.h" |
| 23 | #include "llvm/Transforms/Scalar.h" |
| 24 | #include <optional> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static cl::opt<bool, true> EnableAMDGPUFunctionCallsOpt( |
| 29 | "amdgpu-function-calls" , cl::desc("Enable AMDGPU function call support" ), |
| 30 | cl::location(L&: AMDGPUTargetMachine::EnableFunctionCalls), cl::init(Val: true), |
| 31 | cl::Hidden); |
| 32 | |
| 33 | static ScheduleDAGInstrs *createR600MachineScheduler(MachineSchedContext *C) { |
| 34 | return new ScheduleDAGMILive(C, std::make_unique<R600SchedStrategy>()); |
| 35 | } |
| 36 | |
| 37 | static MachineSchedRegistry R600SchedRegistry("r600" , |
| 38 | "Run R600's custom scheduler" , |
| 39 | createR600MachineScheduler); |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // R600 CodeGen Pass Builder interface. |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | class R600CodeGenPassBuilder : public CodeGenPassBuilder { |
| 46 | public: |
| 47 | R600CodeGenPassBuilder(R600TargetMachine &TM, const CGPassBuilderOption &Opts, |
| 48 | PassInstrumentationCallbacks *PIC); |
| 49 | |
| 50 | void addPreISel(PassManagerWrapper &PMW) override; |
| 51 | void addAsmPrinterBegin(PassManagerWrapper &PMW) override; |
| 52 | void addAsmPrinter(PassManagerWrapper &PMW) override; |
| 53 | void addAsmPrinterEnd(PassManagerWrapper &PMW) override; |
| 54 | Error addInstSelector(PassManagerWrapper &PMW) override; |
| 55 | }; |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // R600 Target Machine (R600 -> Cayman) |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | R600TargetMachine::R600TargetMachine(const Target &T, const Triple &TT, |
| 62 | StringRef CPU, StringRef FS, |
| 63 | const TargetOptions &Options, |
| 64 | std::optional<Reloc::Model> RM, |
| 65 | std::optional<CodeModel::Model> CM, |
| 66 | CodeGenOptLevel OL, bool JIT) |
| 67 | : AMDGPUTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) { |
| 68 | setRequiresStructuredCFG(true); |
| 69 | |
| 70 | // Override the default since calls aren't supported for r600. |
| 71 | if (EnableFunctionCalls && |
| 72 | EnableAMDGPUFunctionCallsOpt.getNumOccurrences() == 0) |
| 73 | EnableFunctionCalls = false; |
| 74 | } |
| 75 | |
| 76 | const TargetSubtargetInfo * |
| 77 | R600TargetMachine::getSubtargetImpl(const Function &F) const { |
| 78 | AttributeSet FnAttrs = F.getAttributes().getFnAttrs(); |
| 79 | const Module *M = F.getParent(); |
| 80 | if (const R600Subtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M)) |
| 81 | return ST; |
| 82 | |
| 83 | SubtargetAttrs Attrs; |
| 84 | Attrs.GPU = FnAttrs.getAttribute(Kind: "target-cpu" ); |
| 85 | Attrs.FS = FnAttrs.getAttribute(Kind: "target-features" ); |
| 86 | |
| 87 | if (const R600Subtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs)) |
| 88 | return ST; |
| 89 | |
| 90 | StringRef GPU = |
| 91 | Attrs.GPU.isValid() ? Attrs.GPU.getValueAsString() : getTargetCPU(); |
| 92 | StringRef FS = Attrs.FS.isValid() ? Attrs.FS.getValueAsString() |
| 93 | : getTargetFeatureString(); |
| 94 | |
| 95 | SmallString<128> SubtargetKey(GPU); |
| 96 | SubtargetKey.append(RHS: FS); |
| 97 | |
| 98 | auto &I = SubtargetMap[SubtargetKey]; |
| 99 | if (!I) |
| 100 | I = std::make_unique<R600Subtarget>(args: TargetTriple, args&: GPU, args&: FS, args: *this); |
| 101 | |
| 102 | LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: I.get()); |
| 103 | return I.get(); |
| 104 | } |
| 105 | |
| 106 | TargetTransformInfo |
| 107 | R600TargetMachine::getTargetTransformInfo(const Function &F) const { |
| 108 | return TargetTransformInfo(std::make_unique<R600TTIImpl>(args: this, args: F)); |
| 109 | } |
| 110 | |
| 111 | ScheduleDAGInstrs * |
| 112 | R600TargetMachine::createMachineScheduler(MachineSchedContext *C) const { |
| 113 | return createR600MachineScheduler(C); |
| 114 | } |
| 115 | |
| 116 | namespace { |
| 117 | class R600PassConfig final : public AMDGPUPassConfig { |
| 118 | public: |
| 119 | R600PassConfig(TargetMachine &TM, PassManagerBase &PM) |
| 120 | : AMDGPUPassConfig(TM, PM) {} |
| 121 | |
| 122 | bool addPreISel() override; |
| 123 | bool addInstSelector() override; |
| 124 | void addPreRegAlloc() override; |
| 125 | void addPreSched2() override; |
| 126 | void addPreEmitPass() override; |
| 127 | }; |
| 128 | } // namespace |
| 129 | |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | // R600 Pass Setup |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | bool R600PassConfig::addPreISel() { |
| 135 | AMDGPUPassConfig::addPreISel(); |
| 136 | |
| 137 | if (CLOpts.r600_ir_structurize) |
| 138 | addPass(P: createStructurizeCFGPass()); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool R600PassConfig::addInstSelector() { |
| 143 | addPass(P: createR600ISelDag(TM&: getAMDGPUTargetMachine(), OptLevel: getOptLevel())); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | void R600PassConfig::addPreRegAlloc() { addPass(P: createR600VectorRegMerger()); } |
| 148 | |
| 149 | void R600PassConfig::addPreSched2() { |
| 150 | addPass(P: createR600EmitClauseMarkers()); |
| 151 | if (CLOpts.r600_if_convert) |
| 152 | addPass(PassID: &IfConverterID); |
| 153 | addPass(P: createR600ClauseMergePass()); |
| 154 | } |
| 155 | |
| 156 | void R600PassConfig::addPreEmitPass() { |
| 157 | addPass(P: createR600MachineCFGStructurizerPass()); |
| 158 | addPass(P: createR600ExpandSpecialInstrsPass()); |
| 159 | addPass(P: createR600Packetizer()); |
| 160 | addPass(P: createR600ControlFlowFinalizer()); |
| 161 | } |
| 162 | |
| 163 | TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) { |
| 164 | return new R600PassConfig(*this, PM); |
| 165 | } |
| 166 | |
| 167 | Error R600TargetMachine::buildCodeGenPipeline( |
| 168 | ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out, |
| 169 | raw_pwrite_stream *DwoOut, CodeGenFileType FileType, |
| 170 | const CGPassBuilderOption &Opts, MCContext &Ctx, |
| 171 | PassInstrumentationCallbacks *PIC) { |
| 172 | R600CodeGenPassBuilder CGPB(*this, Opts, PIC); |
| 173 | return CGPB.buildPipeline(MPM, MAM, Out, DwoOut, FileType, Ctx); |
| 174 | } |
| 175 | |
| 176 | MachineFunctionInfo *R600TargetMachine::createMachineFunctionInfo( |
| 177 | BumpPtrAllocator &Allocator, const Function &F, |
| 178 | const TargetSubtargetInfo *STI) const { |
| 179 | return R600MachineFunctionInfo::create<R600MachineFunctionInfo>( |
| 180 | Allocator, F, STI: static_cast<const R600Subtarget *>(STI)); |
| 181 | } |
| 182 | |
| 183 | //===----------------------------------------------------------------------===// |
| 184 | // R600 CodeGen Pass Builder interface. |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | |
| 187 | R600CodeGenPassBuilder::R600CodeGenPassBuilder( |
| 188 | R600TargetMachine &TM, const CGPassBuilderOption &Opts, |
| 189 | PassInstrumentationCallbacks *PIC) |
| 190 | : CodeGenPassBuilder(TM, Opts, PIC) { |
| 191 | Opt.RequiresCodeGenSCCOrder = true; |
| 192 | } |
| 193 | |
| 194 | void R600CodeGenPassBuilder::addPreISel(PassManagerWrapper &PMW) { |
| 195 | // TODO: Add passes pre instruction selection. |
| 196 | } |
| 197 | |
| 198 | void R600CodeGenPassBuilder::addAsmPrinterBegin(PassManagerWrapper &PMW) { |
| 199 | // TODO: Add AsmPrinterBegin |
| 200 | } |
| 201 | |
| 202 | void R600CodeGenPassBuilder::addAsmPrinter(PassManagerWrapper &PMW) { |
| 203 | // TODO: Add AsmPrinter. |
| 204 | } |
| 205 | |
| 206 | void R600CodeGenPassBuilder::addAsmPrinterEnd(PassManagerWrapper &PMW) { |
| 207 | // TODO: Add AsmPrinterEnd |
| 208 | } |
| 209 | |
| 210 | Error R600CodeGenPassBuilder::addInstSelector(PassManagerWrapper &PMW) { |
| 211 | // TODO: Add instruction selector. |
| 212 | return Error::success(); |
| 213 | } |
| 214 | |