b4403432f3fc2458841ff42a8798ef293006d7c6
[oota-llvm.git] / lib / Target / AMDGPU / AMDGPUAsmPrinter.cpp
1 //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer  --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 ///
12 /// The AMDGPUAsmPrinter is used to print both assembly string and also binary
13 /// code.  When passed an MCAsmStreamer it prints assembly and when passed
14 /// an MCObjectStreamer it outputs binary code.
15 //
16 //===----------------------------------------------------------------------===//
17 //
18
19 #include "AMDGPUAsmPrinter.h"
20 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
21 #include "InstPrinter/AMDGPUInstPrinter.h"
22 #include "Utils/AMDGPUBaseInfo.h"
23 #include "AMDGPU.h"
24 #include "AMDKernelCodeT.h"
25 #include "AMDGPUSubtarget.h"
26 #include "R600Defines.h"
27 #include "R600MachineFunctionInfo.h"
28 #include "R600RegisterInfo.h"
29 #include "SIDefines.h"
30 #include "SIMachineFunctionInfo.h"
31 #include "SIRegisterInfo.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/MC/MCContext.h"
34 #include "llvm/MC/MCSectionELF.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/Support/ELF.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Target/TargetLoweringObjectFile.h"
40
41 using namespace llvm;
42
43 // TODO: This should get the default rounding mode from the kernel. We just set
44 // the default here, but this could change if the OpenCL rounding mode pragmas
45 // are used.
46 //
47 // The denormal mode here should match what is reported by the OpenCL runtime
48 // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
49 // can also be override to flush with the -cl-denorms-are-zero compiler flag.
50 //
51 // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
52 // precision, and leaves single precision to flush all and does not report
53 // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
54 // CL_FP_DENORM for both.
55 //
56 // FIXME: It seems some instructions do not support single precision denormals
57 // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
58 // and sin_f32, cos_f32 on most parts).
59
60 // We want to use these instructions, and using fp32 denormals also causes
61 // instructions to run at the double precision rate for the device so it's
62 // probably best to just report no single precision denormals.
63 static uint32_t getFPMode(const MachineFunction &F) {
64   const AMDGPUSubtarget& ST = F.getSubtarget<AMDGPUSubtarget>();
65   // TODO: Is there any real use for the flush in only / flush out only modes?
66
67   uint32_t FP32Denormals =
68     ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
69
70   uint32_t FP64Denormals =
71     ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
72
73   return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
74          FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
75          FP_DENORM_MODE_SP(FP32Denormals) |
76          FP_DENORM_MODE_DP(FP64Denormals);
77 }
78
79 static AsmPrinter *
80 createAMDGPUAsmPrinterPass(TargetMachine &tm,
81                            std::unique_ptr<MCStreamer> &&Streamer) {
82   return new AMDGPUAsmPrinter(tm, std::move(Streamer));
83 }
84
85 extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
86   TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
87   TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
88 }
89
90 AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
91                                    std::unique_ptr<MCStreamer> Streamer)
92     : AsmPrinter(TM, std::move(Streamer)) {}
93
94 void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
95   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
96   SIProgramInfo KernelInfo;
97   if (STM.isAmdHsaOS()) {
98     getSIProgramInfo(KernelInfo, *MF);
99     EmitAmdKernelCodeT(*MF, KernelInfo);
100   }
101 }
102
103 void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
104   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
105   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
106   if (MFI->isKernel() && STM.isAmdHsaOS()) {
107     AMDGPUTargetStreamer *TS =
108         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
109     TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
110                              ELF::STT_AMDGPU_HSA_KERNEL);
111   }
112
113   AsmPrinter::EmitFunctionEntryLabel();
114 }
115
116 static bool isModuleLinkage(const GlobalValue *GV) {
117   switch (GV->getLinkage()) {
118   case GlobalValue::InternalLinkage:
119   case GlobalValue::CommonLinkage:
120    return true;
121   case GlobalValue::ExternalLinkage:
122    return false;
123   default: llvm_unreachable("unknown linkage type");
124   }
125 }
126
127 void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
128
129   if (TM.getTargetTriple().getOS() != Triple::AMDHSA ||
130       GV->isDeclaration()) {
131     AsmPrinter::EmitGlobalVariable(GV);
132     return;
133   }
134
135   // Group segment variables aren't emitted in HSA.
136   if (AMDGPU::isGroupSegment(GV))
137     return;
138
139   AMDGPUTargetStreamer *TS =
140       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
141   if (isModuleLinkage(GV)) {
142     TS->EmitAMDGPUHsaModuleScopeGlobal(GV->getName());
143   } else {
144     TS->EmitAMDGPUHsaProgramScopeGlobal(GV->getName());
145   }
146
147   const DataLayout &DL = getDataLayout();
148   OutStreamer->PushSection();
149   OutStreamer->SwitchSection(
150       getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
151   MCSymbol *GVSym = getSymbol(GV);
152   const Constant *C = GV->getInitializer();
153   OutStreamer->EmitLabel(GVSym);
154   EmitGlobalConstant(DL, C);
155   OutStreamer->PopSection();
156 }
157
158 bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
159
160   // The starting address of all shader programs must be 256 bytes aligned.
161   MF.setAlignment(8);
162
163   SetupMachineFunction(MF);
164
165   MCContext &Context = getObjFileLowering().getContext();
166   MCSectionELF *ConfigSection =
167       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
168   OutStreamer->SwitchSection(ConfigSection);
169
170   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
171   SIProgramInfo KernelInfo;
172   if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
173     getSIProgramInfo(KernelInfo, MF);
174     if (!STM.isAmdHsaOS()) {
175       EmitProgramInfoSI(MF, KernelInfo);
176     }
177     // Emit directives
178     AMDGPUTargetStreamer *TS =
179         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
180     TS->EmitDirectiveHSACodeObjectVersion(1, 0);
181     AMDGPU::IsaVersion ISA = STM.getIsaVersion();
182     TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
183                                       "AMD", "AMDGPU");
184   } else {
185     EmitProgramInfoR600(MF);
186   }
187
188   DisasmLines.clear();
189   HexLines.clear();
190   DisasmLineMaxLen = 0;
191
192   EmitFunctionBody();
193
194   if (isVerbose()) {
195     MCSectionELF *CommentSection =
196         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
197     OutStreamer->SwitchSection(CommentSection);
198
199     if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
200       OutStreamer->emitRawComment(" Kernel info:", false);
201       OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
202                                   false);
203       OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
204                                   false);
205       OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
206                                   false);
207       OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
208                                   false);
209       OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
210                                   false);
211       OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
212                                   false);
213
214       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
215                                   Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
216                                   false);
217       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
218                                   Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
219                                   false);
220       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
221                                   Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
222                                   false);
223       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
224                                   Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
225                                   false);
226       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
227                                   Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
228                                   false);
229
230     } else {
231       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
232       OutStreamer->emitRawComment(
233         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
234     }
235   }
236
237   if (STM.dumpCode()) {
238
239     OutStreamer->SwitchSection(
240         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
241
242     for (size_t i = 0; i < DisasmLines.size(); ++i) {
243       std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
244       Comment += " ; " + HexLines[i] + "\n";
245
246       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
247       OutStreamer->EmitBytes(StringRef(Comment));
248     }
249   }
250
251   return false;
252 }
253
254 void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
255   unsigned MaxGPR = 0;
256   bool killPixel = false;
257   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
258   const R600RegisterInfo *RI =
259       static_cast<const R600RegisterInfo *>(STM.getRegisterInfo());
260   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
261
262   for (const MachineBasicBlock &MBB : MF) {
263     for (const MachineInstr &MI : MBB) {
264       if (MI.getOpcode() == AMDGPU::KILLGT)
265         killPixel = true;
266       unsigned numOperands = MI.getNumOperands();
267       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
268         const MachineOperand &MO = MI.getOperand(op_idx);
269         if (!MO.isReg())
270           continue;
271         unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
272
273         // Register with value > 127 aren't GPR
274         if (HWReg > 127)
275           continue;
276         MaxGPR = std::max(MaxGPR, HWReg);
277       }
278     }
279   }
280
281   unsigned RsrcReg;
282   if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
283     // Evergreen / Northern Islands
284     switch (MFI->getShaderType()) {
285     default: // Fall through
286     case ShaderType::COMPUTE:  RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
287     case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
288     case ShaderType::PIXEL:    RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
289     case ShaderType::VERTEX:   RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
290     }
291   } else {
292     // R600 / R700
293     switch (MFI->getShaderType()) {
294     default: // Fall through
295     case ShaderType::GEOMETRY: // Fall through
296     case ShaderType::COMPUTE:  // Fall through
297     case ShaderType::VERTEX:   RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
298     case ShaderType::PIXEL:    RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
299     }
300   }
301
302   OutStreamer->EmitIntValue(RsrcReg, 4);
303   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
304                            S_STACK_SIZE(MFI->StackSize), 4);
305   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
306   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
307
308   if (MFI->getShaderType() == ShaderType::COMPUTE) {
309     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
310     OutStreamer->EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
311   }
312 }
313
314 void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
315                                         const MachineFunction &MF) const {
316   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
317   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
318   uint64_t CodeSize = 0;
319   unsigned MaxSGPR = 0;
320   unsigned MaxVGPR = 0;
321   bool VCCUsed = false;
322   bool FlatUsed = false;
323   const SIRegisterInfo *RI =
324       static_cast<const SIRegisterInfo *>(STM.getRegisterInfo());
325
326   for (const MachineBasicBlock &MBB : MF) {
327     for (const MachineInstr &MI : MBB) {
328       // TODO: CodeSize should account for multiple functions.
329
330       // TODO: Should we count size of debug info?
331       if (MI.isDebugValue())
332         continue;
333
334       // FIXME: This is reporting 0 for many instructions.
335       CodeSize += MI.getDesc().Size;
336
337       unsigned numOperands = MI.getNumOperands();
338       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
339         const MachineOperand &MO = MI.getOperand(op_idx);
340         unsigned width = 0;
341         bool isSGPR = false;
342
343         if (!MO.isReg())
344           continue;
345
346         unsigned reg = MO.getReg();
347         switch (reg) {
348         case AMDGPU::EXEC:
349         case AMDGPU::SCC:
350         case AMDGPU::M0:
351           continue;
352
353         case AMDGPU::VCC:
354         case AMDGPU::VCC_LO:
355         case AMDGPU::VCC_HI:
356           VCCUsed = true;
357           continue;
358
359         case AMDGPU::FLAT_SCR:
360         case AMDGPU::FLAT_SCR_LO:
361         case AMDGPU::FLAT_SCR_HI:
362           FlatUsed = true;
363           continue;
364
365         default:
366           break;
367         }
368
369         if (AMDGPU::SReg_32RegClass.contains(reg)) {
370           isSGPR = true;
371           width = 1;
372         } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
373           isSGPR = false;
374           width = 1;
375         } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
376           isSGPR = true;
377           width = 2;
378         } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
379           isSGPR = false;
380           width = 2;
381         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
382           isSGPR = false;
383           width = 3;
384         } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
385           isSGPR = true;
386           width = 4;
387         } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
388           isSGPR = false;
389           width = 4;
390         } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
391           isSGPR = true;
392           width = 8;
393         } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
394           isSGPR = false;
395           width = 8;
396         } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
397           isSGPR = true;
398           width = 16;
399         } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
400           isSGPR = false;
401           width = 16;
402         } else {
403           llvm_unreachable("Unknown register class");
404         }
405         unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
406         unsigned maxUsed = hwReg + width - 1;
407         if (isSGPR) {
408           MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
409         } else {
410           MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
411         }
412       }
413     }
414   }
415
416   if (VCCUsed)
417     MaxSGPR += 2;
418
419   if (FlatUsed)
420     MaxSGPR += 2;
421
422   // We found the maximum register index. They start at 0, so add one to get the
423   // number of registers.
424   ProgInfo.NumVGPR = MaxVGPR + 1;
425   ProgInfo.NumSGPR = MaxSGPR + 1;
426
427   if (STM.hasSGPRInitBug()) {
428     if (ProgInfo.NumSGPR > AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
429       LLVMContext &Ctx = MF.getFunction()->getContext();
430       Ctx.emitError("too many SGPRs used with the SGPR init bug");
431     }
432
433     ProgInfo.NumSGPR = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
434   }
435
436   if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
437     LLVMContext &Ctx = MF.getFunction()->getContext();
438     Ctx.emitError("too many user SGPRs used");
439   }
440
441   ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
442   ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
443   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
444   // register.
445   ProgInfo.FloatMode = getFPMode(MF);
446
447   // XXX: Not quite sure what this does, but sc seems to unset this.
448   ProgInfo.IEEEMode = 0;
449
450   // Do not clamp NAN to 0.
451   ProgInfo.DX10Clamp = 0;
452
453   const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
454   ProgInfo.ScratchSize = FrameInfo->estimateStackSize(MF);
455
456   ProgInfo.FlatUsed = FlatUsed;
457   ProgInfo.VCCUsed = VCCUsed;
458   ProgInfo.CodeLen = CodeSize;
459
460   unsigned LDSAlignShift;
461   if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
462     // LDS is allocated in 64 dword blocks.
463     LDSAlignShift = 8;
464   } else {
465     // LDS is allocated in 128 dword blocks.
466     LDSAlignShift = 9;
467   }
468
469   unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
470                           MFI->getMaximumWorkGroupSize(MF);
471
472   ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
473   ProgInfo.LDSBlocks =
474      RoundUpToAlignment(ProgInfo.LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
475
476   // Scratch is allocated in 256 dword blocks.
477   unsigned ScratchAlignShift = 10;
478   // We need to program the hardware with the amount of scratch memory that
479   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
480   // scratch memory used per thread.
481   ProgInfo.ScratchBlocks =
482     RoundUpToAlignment(ProgInfo.ScratchSize * STM.getWavefrontSize(),
483                        1 << ScratchAlignShift) >> ScratchAlignShift;
484
485   ProgInfo.ComputePGMRSrc1 =
486       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
487       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
488       S_00B848_PRIORITY(ProgInfo.Priority) |
489       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
490       S_00B848_PRIV(ProgInfo.Priv) |
491       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
492       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
493       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
494
495   // 0 = X, 1 = XY, 2 = XYZ
496   unsigned TIDIGCompCnt = 0;
497   if (MFI->hasWorkItemIDZ())
498     TIDIGCompCnt = 2;
499   else if (MFI->hasWorkItemIDY())
500     TIDIGCompCnt = 1;
501
502   ProgInfo.ComputePGMRSrc2 =
503       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
504       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
505       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
506       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
507       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
508       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
509       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
510       S_00B84C_EXCP_EN_MSB(0) |
511       S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
512       S_00B84C_EXCP_EN(0);
513 }
514
515 static unsigned getRsrcReg(unsigned ShaderType) {
516   switch (ShaderType) {
517   default: // Fall through
518   case ShaderType::COMPUTE:  return R_00B848_COMPUTE_PGM_RSRC1;
519   case ShaderType::GEOMETRY: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
520   case ShaderType::PIXEL:    return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
521   case ShaderType::VERTEX:   return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
522   }
523 }
524
525 void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
526                                          const SIProgramInfo &KernelInfo) {
527   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
528   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
529   unsigned RsrcReg = getRsrcReg(MFI->getShaderType());
530
531   if (MFI->getShaderType() == ShaderType::COMPUTE) {
532     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
533
534     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
535
536     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
537     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
538
539     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
540     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
541
542     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
543     // 0" comment but I don't see a corresponding field in the register spec.
544   } else {
545     OutStreamer->EmitIntValue(RsrcReg, 4);
546     OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
547                               S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
548     if (STM.isVGPRSpillingEnabled(MFI)) {
549       OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
550       OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
551     }
552   }
553
554   if (MFI->getShaderType() == ShaderType::PIXEL) {
555     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
556     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
557     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
558     OutStreamer->EmitIntValue(MFI->PSInputAddr, 4);
559   }
560 }
561
562 void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
563                                          const SIProgramInfo &KernelInfo) const {
564   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
565   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
566   amd_kernel_code_t header;
567
568   AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
569
570   header.compute_pgm_resource_registers =
571       KernelInfo.ComputePGMRSrc1 |
572       (KernelInfo.ComputePGMRSrc2 << 32);
573   header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
574
575   if (MFI->hasPrivateSegmentBuffer()) {
576     header.code_properties |=
577       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
578   }
579
580   if (MFI->hasDispatchPtr())
581     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
582
583   if (MFI->hasQueuePtr())
584     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
585
586   if (MFI->hasKernargSegmentPtr())
587     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
588
589   if (MFI->hasDispatchID())
590     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
591
592   if (MFI->hasFlatScratchInit())
593     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
594
595   // TODO: Private segment size
596
597   if (MFI->hasGridWorkgroupCountX()) {
598     header.code_properties |=
599       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
600   }
601
602   if (MFI->hasGridWorkgroupCountY()) {
603     header.code_properties |=
604       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
605   }
606
607   if (MFI->hasGridWorkgroupCountZ()) {
608     header.code_properties |=
609       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
610   }
611
612   if (MFI->hasDispatchPtr())
613     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
614
615   header.kernarg_segment_byte_size = MFI->ABIArgOffset;
616   header.wavefront_sgpr_count = KernelInfo.NumSGPR;
617   header.workitem_vgpr_count = KernelInfo.NumVGPR;
618
619   AMDGPUTargetStreamer *TS =
620       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
621   TS->EmitAMDKernelCodeT(header);
622 }
623
624 bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
625                                        unsigned AsmVariant,
626                                        const char *ExtraCode, raw_ostream &O) {
627   if (ExtraCode && ExtraCode[0]) {
628     if (ExtraCode[1] != 0)
629       return true; // Unknown modifier.
630
631     switch (ExtraCode[0]) {
632     default:
633       // See if this is a generic print operand
634       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
635     case 'r':
636       break;
637     }
638   }
639
640   AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
641                    *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
642   return false;
643 }