84c71e0cc9cfdb39e9be7e516367e70511e328ad
[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::EmitEndOfAsmFile(Module &M) {
104
105   // This label is used to mark the end of the .text section.
106   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
107   OutStreamer->SwitchSection(TLOF.getTextSection());
108   MCSymbol *EndOfTextLabel =
109       OutContext.getOrCreateSymbol(StringRef(END_OF_TEXT_LABEL_NAME));
110   OutStreamer->EmitLabel(EndOfTextLabel);
111 }
112
113 void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
114   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
115   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
116   if (MFI->isKernel() && STM.isAmdHsaOS()) {
117     AMDGPUTargetStreamer *TS =
118         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
119     TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
120                              ELF::STT_AMDGPU_HSA_KERNEL);
121   }
122
123   AsmPrinter::EmitFunctionEntryLabel();
124 }
125
126 void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
127
128   if (TM.getTargetTriple().getOS() != Triple::AMDHSA ||
129       !AMDGPU::isGroupSegment(GV))
130     return AsmPrinter::EmitGlobalVariable(GV);
131 }
132
133 bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
134
135   // The starting address of all shader programs must be 256 bytes aligned.
136   MF.setAlignment(8);
137
138   SetupMachineFunction(MF);
139
140   MCContext &Context = getObjFileLowering().getContext();
141   MCSectionELF *ConfigSection =
142       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
143   OutStreamer->SwitchSection(ConfigSection);
144
145   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
146   SIProgramInfo KernelInfo;
147   if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
148     getSIProgramInfo(KernelInfo, MF);
149     if (!STM.isAmdHsaOS()) {
150       EmitProgramInfoSI(MF, KernelInfo);
151     }
152     // Emit directives
153     AMDGPUTargetStreamer *TS =
154         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
155     TS->EmitDirectiveHSACodeObjectVersion(1, 0);
156     AMDGPU::IsaVersion ISA = STM.getIsaVersion();
157     TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
158                                       "AMD", "AMDGPU");
159   } else {
160     EmitProgramInfoR600(MF);
161   }
162
163   DisasmLines.clear();
164   HexLines.clear();
165   DisasmLineMaxLen = 0;
166
167   EmitFunctionBody();
168
169   if (isVerbose()) {
170     MCSectionELF *CommentSection =
171         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
172     OutStreamer->SwitchSection(CommentSection);
173
174     if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
175       OutStreamer->emitRawComment(" Kernel info:", false);
176       OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
177                                   false);
178       OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
179                                   false);
180       OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
181                                   false);
182       OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
183                                   false);
184       OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
185                                   false);
186       OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
187                                   false);
188
189       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
190                                   Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
191                                   false);
192       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
193                                   Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
194                                   false);
195       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
196                                   Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
197                                   false);
198       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
199                                   Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
200                                   false);
201       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
202                                   Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
203                                   false);
204
205     } else {
206       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
207       OutStreamer->emitRawComment(
208         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
209     }
210   }
211
212   if (STM.dumpCode()) {
213
214     OutStreamer->SwitchSection(
215         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
216
217     for (size_t i = 0; i < DisasmLines.size(); ++i) {
218       std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
219       Comment += " ; " + HexLines[i] + "\n";
220
221       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
222       OutStreamer->EmitBytes(StringRef(Comment));
223     }
224   }
225
226   return false;
227 }
228
229 void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
230   unsigned MaxGPR = 0;
231   bool killPixel = false;
232   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
233   const R600RegisterInfo *RI =
234       static_cast<const R600RegisterInfo *>(STM.getRegisterInfo());
235   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
236
237   for (const MachineBasicBlock &MBB : MF) {
238     for (const MachineInstr &MI : MBB) {
239       if (MI.getOpcode() == AMDGPU::KILLGT)
240         killPixel = true;
241       unsigned numOperands = MI.getNumOperands();
242       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
243         const MachineOperand &MO = MI.getOperand(op_idx);
244         if (!MO.isReg())
245           continue;
246         unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
247
248         // Register with value > 127 aren't GPR
249         if (HWReg > 127)
250           continue;
251         MaxGPR = std::max(MaxGPR, HWReg);
252       }
253     }
254   }
255
256   unsigned RsrcReg;
257   if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
258     // Evergreen / Northern Islands
259     switch (MFI->getShaderType()) {
260     default: // Fall through
261     case ShaderType::COMPUTE:  RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
262     case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
263     case ShaderType::PIXEL:    RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
264     case ShaderType::VERTEX:   RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
265     }
266   } else {
267     // R600 / R700
268     switch (MFI->getShaderType()) {
269     default: // Fall through
270     case ShaderType::GEOMETRY: // Fall through
271     case ShaderType::COMPUTE:  // Fall through
272     case ShaderType::VERTEX:   RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
273     case ShaderType::PIXEL:    RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
274     }
275   }
276
277   OutStreamer->EmitIntValue(RsrcReg, 4);
278   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
279                            S_STACK_SIZE(MFI->StackSize), 4);
280   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
281   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
282
283   if (MFI->getShaderType() == ShaderType::COMPUTE) {
284     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
285     OutStreamer->EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
286   }
287 }
288
289 void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
290                                         const MachineFunction &MF) const {
291   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
292   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
293   uint64_t CodeSize = 0;
294   unsigned MaxSGPR = 0;
295   unsigned MaxVGPR = 0;
296   bool VCCUsed = false;
297   bool FlatUsed = false;
298   const SIRegisterInfo *RI =
299       static_cast<const SIRegisterInfo *>(STM.getRegisterInfo());
300
301   for (const MachineBasicBlock &MBB : MF) {
302     for (const MachineInstr &MI : MBB) {
303       // TODO: CodeSize should account for multiple functions.
304
305       // TODO: Should we count size of debug info?
306       if (MI.isDebugValue())
307         continue;
308
309       // FIXME: This is reporting 0 for many instructions.
310       CodeSize += MI.getDesc().Size;
311
312       unsigned numOperands = MI.getNumOperands();
313       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
314         const MachineOperand &MO = MI.getOperand(op_idx);
315         unsigned width = 0;
316         bool isSGPR = false;
317
318         if (!MO.isReg())
319           continue;
320
321         unsigned reg = MO.getReg();
322         switch (reg) {
323         case AMDGPU::EXEC:
324         case AMDGPU::SCC:
325         case AMDGPU::M0:
326           continue;
327
328         case AMDGPU::VCC:
329         case AMDGPU::VCC_LO:
330         case AMDGPU::VCC_HI:
331           VCCUsed = true;
332           continue;
333
334         case AMDGPU::FLAT_SCR:
335         case AMDGPU::FLAT_SCR_LO:
336         case AMDGPU::FLAT_SCR_HI:
337           FlatUsed = true;
338           continue;
339
340         default:
341           break;
342         }
343
344         if (AMDGPU::SReg_32RegClass.contains(reg)) {
345           isSGPR = true;
346           width = 1;
347         } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
348           isSGPR = false;
349           width = 1;
350         } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
351           isSGPR = true;
352           width = 2;
353         } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
354           isSGPR = false;
355           width = 2;
356         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
357           isSGPR = false;
358           width = 3;
359         } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
360           isSGPR = true;
361           width = 4;
362         } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
363           isSGPR = false;
364           width = 4;
365         } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
366           isSGPR = true;
367           width = 8;
368         } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
369           isSGPR = false;
370           width = 8;
371         } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
372           isSGPR = true;
373           width = 16;
374         } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
375           isSGPR = false;
376           width = 16;
377         } else {
378           llvm_unreachable("Unknown register class");
379         }
380         unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
381         unsigned maxUsed = hwReg + width - 1;
382         if (isSGPR) {
383           MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
384         } else {
385           MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
386         }
387       }
388     }
389   }
390
391   if (VCCUsed)
392     MaxSGPR += 2;
393
394   if (FlatUsed)
395     MaxSGPR += 2;
396
397   // We found the maximum register index. They start at 0, so add one to get the
398   // number of registers.
399   ProgInfo.NumVGPR = MaxVGPR + 1;
400   ProgInfo.NumSGPR = MaxSGPR + 1;
401
402   if (STM.hasSGPRInitBug()) {
403     if (ProgInfo.NumSGPR > AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
404       LLVMContext &Ctx = MF.getFunction()->getContext();
405       Ctx.emitError("too many SGPRs used with the SGPR init bug");
406     }
407
408     ProgInfo.NumSGPR = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
409   }
410
411   if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
412     LLVMContext &Ctx = MF.getFunction()->getContext();
413     Ctx.emitError("too many user SGPRs used");
414   }
415
416   ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
417   ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
418   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
419   // register.
420   ProgInfo.FloatMode = getFPMode(MF);
421
422   // XXX: Not quite sure what this does, but sc seems to unset this.
423   ProgInfo.IEEEMode = 0;
424
425   // Do not clamp NAN to 0.
426   ProgInfo.DX10Clamp = 0;
427
428   const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
429   ProgInfo.ScratchSize = FrameInfo->estimateStackSize(MF);
430
431   ProgInfo.FlatUsed = FlatUsed;
432   ProgInfo.VCCUsed = VCCUsed;
433   ProgInfo.CodeLen = CodeSize;
434
435   unsigned LDSAlignShift;
436   if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
437     // LDS is allocated in 64 dword blocks.
438     LDSAlignShift = 8;
439   } else {
440     // LDS is allocated in 128 dword blocks.
441     LDSAlignShift = 9;
442   }
443
444   unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
445                           MFI->getMaximumWorkGroupSize(MF);
446
447   ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
448   ProgInfo.LDSBlocks =
449      RoundUpToAlignment(ProgInfo.LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
450
451   // Scratch is allocated in 256 dword blocks.
452   unsigned ScratchAlignShift = 10;
453   // We need to program the hardware with the amount of scratch memory that
454   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
455   // scratch memory used per thread.
456   ProgInfo.ScratchBlocks =
457     RoundUpToAlignment(ProgInfo.ScratchSize * STM.getWavefrontSize(),
458                        1 << ScratchAlignShift) >> ScratchAlignShift;
459
460   ProgInfo.ComputePGMRSrc1 =
461       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
462       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
463       S_00B848_PRIORITY(ProgInfo.Priority) |
464       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
465       S_00B848_PRIV(ProgInfo.Priv) |
466       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
467       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
468       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
469
470   // 0 = X, 1 = XY, 2 = XYZ
471   unsigned TIDIGCompCnt = 0;
472   if (MFI->hasWorkItemIDZ())
473     TIDIGCompCnt = 2;
474   else if (MFI->hasWorkItemIDY())
475     TIDIGCompCnt = 1;
476
477   ProgInfo.ComputePGMRSrc2 =
478       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
479       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
480       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
481       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
482       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
483       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
484       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
485       S_00B84C_EXCP_EN_MSB(0) |
486       S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
487       S_00B84C_EXCP_EN(0);
488 }
489
490 static unsigned getRsrcReg(unsigned ShaderType) {
491   switch (ShaderType) {
492   default: // Fall through
493   case ShaderType::COMPUTE:  return R_00B848_COMPUTE_PGM_RSRC1;
494   case ShaderType::GEOMETRY: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
495   case ShaderType::PIXEL:    return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
496   case ShaderType::VERTEX:   return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
497   }
498 }
499
500 void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
501                                          const SIProgramInfo &KernelInfo) {
502   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
503   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
504   unsigned RsrcReg = getRsrcReg(MFI->getShaderType());
505
506   if (MFI->getShaderType() == ShaderType::COMPUTE) {
507     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
508
509     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
510
511     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
512     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
513
514     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
515     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
516
517     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
518     // 0" comment but I don't see a corresponding field in the register spec.
519   } else {
520     OutStreamer->EmitIntValue(RsrcReg, 4);
521     OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
522                               S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
523     if (STM.isVGPRSpillingEnabled(MFI)) {
524       OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
525       OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
526     }
527   }
528
529   if (MFI->getShaderType() == ShaderType::PIXEL) {
530     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
531     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
532     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
533     OutStreamer->EmitIntValue(MFI->PSInputAddr, 4);
534   }
535 }
536
537 void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
538                                          const SIProgramInfo &KernelInfo) const {
539   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
540   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
541   amd_kernel_code_t header;
542
543   AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
544
545   header.compute_pgm_resource_registers =
546       KernelInfo.ComputePGMRSrc1 |
547       (KernelInfo.ComputePGMRSrc2 << 32);
548   header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
549
550   if (MFI->hasPrivateSegmentBuffer()) {
551     header.code_properties |=
552       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
553   }
554
555   if (MFI->hasDispatchPtr())
556     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
557
558   if (MFI->hasQueuePtr())
559     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
560
561   if (MFI->hasKernargSegmentPtr())
562     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
563
564   if (MFI->hasDispatchID())
565     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
566
567   if (MFI->hasFlatScratchInit())
568     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
569
570   // TODO: Private segment size
571
572   if (MFI->hasGridWorkgroupCountX()) {
573     header.code_properties |=
574       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
575   }
576
577   if (MFI->hasGridWorkgroupCountY()) {
578     header.code_properties |=
579       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
580   }
581
582   if (MFI->hasGridWorkgroupCountZ()) {
583     header.code_properties |=
584       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
585   }
586
587   if (MFI->hasDispatchPtr())
588     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
589
590   header.kernarg_segment_byte_size = MFI->ABIArgOffset;
591   header.wavefront_sgpr_count = KernelInfo.NumSGPR;
592   header.workitem_vgpr_count = KernelInfo.NumVGPR;
593
594   AMDGPUTargetStreamer *TS =
595       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
596   TS->EmitAMDKernelCodeT(header);
597 }
598
599 bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
600                                        unsigned AsmVariant,
601                                        const char *ExtraCode, raw_ostream &O) {
602   if (ExtraCode && ExtraCode[0]) {
603     if (ExtraCode[1] != 0)
604       return true; // Unknown modifier.
605
606     switch (ExtraCode[0]) {
607     default:
608       // See if this is a generic print operand
609       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
610     case 'r':
611       break;
612     }
613   }
614
615   AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
616                    *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
617   return false;
618 }