LLVM 24.0.0git
AMDGPUTargetStreamer.cpp
Go to the documentation of this file.
1//===-- AMDGPUTargetStreamer.cpp - Mips Target Streamer Methods -----------===//
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// This file provides AMDGPU specific target streamer methods.
10//
11//===----------------------------------------------------------------------===//
12
14#include "AMDGPUMCExpr.h"
16#include "AMDGPUMCTargetDesc.h"
17#include "AMDGPUPTNote.h"
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCAssembler.h"
24#include "llvm/MC/MCContext.h"
35
36using namespace llvm;
37using namespace llvm::AMDGPU;
38
39//===----------------------------------------------------------------------===//
40// AMDGPUTargetStreamer
41//===----------------------------------------------------------------------===//
42
44 ForceGenericVersion("amdgpu-force-generic-version",
45 cl::desc("Force a specific generic_v<N> flag to be "
46 "added. For testing purposes only."),
48
50 bool ApplyFeatureString) {
51 assert(TargetID == std::nullopt && "TargetID can only be initialized once");
52 // Apply xnack/sramecc from subtarget features only in MC contexts
53 // (assembler), not in codegen where they come from module flags
55 STI, ApplyFeatureString ? STI.getFeatureString() : "");
56}
57
59 msgpack::Document HSAMetadataDoc;
60 if (!HSAMetadataDoc.fromYAML(HSAMetadataString))
61 return false;
62 return EmitHSAMetadata(HSAMetadataDoc, false);
63}
64
68 AK = parseArchR600(GPU);
69
70 // clang-format off
71 switch (AK) {
72 case GK_R600: return ELF::EF_AMDGPU_MACH_R600_R600;
73 case GK_R630: return ELF::EF_AMDGPU_MACH_R600_R630;
74 case GK_RS880: return ELF::EF_AMDGPU_MACH_R600_RS880;
75 case GK_RV670: return ELF::EF_AMDGPU_MACH_R600_RV670;
76 case GK_RV710: return ELF::EF_AMDGPU_MACH_R600_RV710;
77 case GK_RV730: return ELF::EF_AMDGPU_MACH_R600_RV730;
78 case GK_RV770: return ELF::EF_AMDGPU_MACH_R600_RV770;
79 case GK_CEDAR: return ELF::EF_AMDGPU_MACH_R600_CEDAR;
80 case GK_CYPRESS: return ELF::EF_AMDGPU_MACH_R600_CYPRESS;
81 case GK_JUNIPER: return ELF::EF_AMDGPU_MACH_R600_JUNIPER;
82 case GK_REDWOOD: return ELF::EF_AMDGPU_MACH_R600_REDWOOD;
83 case GK_SUMO: return ELF::EF_AMDGPU_MACH_R600_SUMO;
84 case GK_BARTS: return ELF::EF_AMDGPU_MACH_R600_BARTS;
85 case GK_CAICOS: return ELF::EF_AMDGPU_MACH_R600_CAICOS;
86 case GK_CAYMAN: return ELF::EF_AMDGPU_MACH_R600_CAYMAN;
87 case GK_TURKS: return ELF::EF_AMDGPU_MACH_R600_TURKS;
88 case GK_GFX600: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX600;
89 case GK_GFX601: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX601;
90 case GK_GFX602: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX602;
91 case GK_GFX700: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX700;
92 case GK_GFX701: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX701;
93 case GK_GFX702: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX702;
94 case GK_GFX703: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX703;
95 case GK_GFX704: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX704;
96 case GK_GFX705: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX705;
97 case GK_GFX801: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX801;
98 case GK_GFX802: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX802;
99 case GK_GFX803: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX803;
100 case GK_GFX805: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX805;
101 case GK_GFX810: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX810;
102 case GK_GFX900: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX900;
103 case GK_GFX902: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX902;
104 case GK_GFX904: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX904;
105 case GK_GFX906: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX906;
106 case GK_GFX908: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX908;
107 case GK_GFX909: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX909;
108 case GK_GFX90A: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A;
109 case GK_GFX90C: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C;
110 case GK_GFX942: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX942;
111 case GK_GFX950: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX950;
112 case GK_GFX1010: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010;
113 case GK_GFX1011: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011;
114 case GK_GFX1012: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012;
115 case GK_GFX1013: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013;
116 case GK_GFX1030: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030;
117 case GK_GFX1031: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031;
118 case GK_GFX1032: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032;
119 case GK_GFX1033: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033;
120 case GK_GFX1034: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034;
121 case GK_GFX1035: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035;
122 case GK_GFX1036: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036;
123 case GK_GFX1100: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100;
124 case GK_GFX1101: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101;
125 case GK_GFX1102: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102;
126 case GK_GFX1103: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103;
127 case GK_GFX1150: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1150;
128 case GK_GFX1151: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1151;
129 case GK_GFX1152: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1152;
130 case GK_GFX1153: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1153;
131 case GK_GFX1154: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1154;
132 case GK_GFX1170: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1170;
133 case GK_GFX1171: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1171;
134 case GK_GFX1172: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1172;
135 case GK_GFX1200: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1200;
136 case GK_GFX1201: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1201;
137 case GK_GFX1250_STRICT: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1250_STRICT;
138 case GK_GFX1250: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1250;
139 case GK_GFX1251: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1251;
140 case GK_GFX1310: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX1310;
141 case GK_GFX9_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC;
142 case GK_GFX9_4_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX9_4_GENERIC;
143 case GK_GFX10_1_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC;
144 case GK_GFX10_3_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC;
145 case GK_GFX11_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC;
146 case GK_GFX11_7_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX11_7_GENERIC;
147 case GK_GFX12_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX12_GENERIC;
148 case GK_GFX12_5_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX12_5_GENERIC;
149 case GK_GFX13_GENERIC: return ELF::EF_AMDGPU_MACH_AMDGCN_GFX13_GENERIC;
150 case GK_GENERIC:
151 case GK_GENERIC_HSA:
153 }
154 // clang-format on
155
156 llvm_unreachable("unknown GPU");
157}
158
159//===----------------------------------------------------------------------===//
160// AMDGPUTargetAsmStreamer
161//===----------------------------------------------------------------------===//
162
166
167// A hook for emitting stuff at the end.
168// We use it for emitting the accumulated PAL metadata as directives.
169// The PAL metadata is reset after it is emitted.
171 std::string S;
173 OS << S;
174
175 // Reset the pal metadata so its data will not affect a compilation that
176 // reuses this object.
178}
179
181 OS << "\t.amdgcn_target \"" << *getTargetID() << "\"\n";
182}
183
185 unsigned COV) {
187 OS << "\t.amdhsa_code_object_version " << COV << '\n';
188}
189
191 auto FoldAndPrint = [&](const MCExpr *Expr, raw_ostream &OS,
192 const MCAsmInfo *MAI) {
194 };
195
196 OS << "\t.amd_kernel_code_t\n";
197 Header.EmitKernelCodeT(OS, getContext(), FoldAndPrint);
198 OS << "\t.end_amd_kernel_code_t\n";
199}
200
202 unsigned Type) {
203 switch (Type) {
204 default:
205 llvm_unreachable("Invalid AMDGPU symbol type");
207 OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n';
208 break;
209 }
210}
211
213 Align Alignment) {
214 OS << "\t.amdgpu_lds " << Symbol->getName() << ", " << Size << ", "
215 << Alignment.value() << '\n';
216}
217
219 const MCSymbol *NumVGPR, const MCSymbol *NumAGPR,
220 const MCSymbol *NumExplicitSGPR, const MCSymbol *NumNamedBarrier,
221 const MCSymbol *PrivateSegmentSize, const MCSymbol *UsesVCC,
222 const MCSymbol *UsesFlatScratch, const MCSymbol *HasDynamicallySizedStack,
223 const MCSymbol *HasRecursion, const MCSymbol *HasIndirectCall) {
224#define PRINT_RES_INFO(ARG) \
225 OS << "\t.set "; \
226 ARG->print(OS, &getContext().getAsmInfo()); \
227 OS << ", "; \
228 getContext().getAsmInfo().printExpr(OS, *ARG->getVariableValue()); \
229 Streamer.addBlankLine();
230
231 PRINT_RES_INFO(NumVGPR);
232 PRINT_RES_INFO(NumAGPR);
233 PRINT_RES_INFO(NumExplicitSGPR);
234 PRINT_RES_INFO(NumNamedBarrier);
235 PRINT_RES_INFO(PrivateSegmentSize);
236 PRINT_RES_INFO(UsesVCC);
237 PRINT_RES_INFO(UsesFlatScratch);
238 PRINT_RES_INFO(HasDynamicallySizedStack);
239 PRINT_RES_INFO(HasRecursion);
240 PRINT_RES_INFO(HasIndirectCall);
241#undef PRINT_RES_INFO
242}
243
245 const MCSymbol *MaxVGPR, const MCSymbol *MaxAGPR, const MCSymbol *MaxSGPR,
246 const MCSymbol *MaxNamedBarrier) {
247#define PRINT_RES_INFO(ARG) \
248 OS << "\t.set "; \
249 ARG->print(OS, &getContext().getAsmInfo()); \
250 OS << ", "; \
251 getContext().getAsmInfo().printExpr(OS, *ARG->getVariableValue()); \
252 Streamer.addBlankLine();
253
254 PRINT_RES_INFO(MaxVGPR);
255 PRINT_RES_INFO(MaxAGPR);
256 PRINT_RES_INFO(MaxSGPR);
257 PRINT_RES_INFO(MaxNamedBarrier);
258#undef PRINT_RES_INFO
259}
260
262 OS << "\t.amd_amdgpu_isa \"" << getTargetID() << "\"\n";
263 return true;
264}
265
267 bool Strict) {
269 if (!Verifier.verify(HSAMetadataDoc.getRoot()))
270 return false;
271
272 std::string HSAMetadataString;
273 raw_string_ostream StrOS(HSAMetadataString);
274 HSAMetadataDoc.toYAML(StrOS);
275
276 OS << '\t' << HSAMD::V3::AssemblerDirectiveBegin << '\n';
277 OS << StrOS.str() << '\n';
278 OS << '\t' << HSAMD::V3::AssemblerDirectiveEnd << '\n';
279 return true;
280}
281
283 const uint32_t Encoded_s_code_end = 0xbf9f0000;
284 const uint32_t Encoded_s_nop = 0xbf800000;
285 uint32_t Encoded_pad = Encoded_s_code_end;
286
287 // Instruction cache line size in bytes.
288 const unsigned Log2CacheLineSize = AMDGPU::isGFX11Plus(STI) ? 7 : 6;
289 const unsigned CacheLineSize = 1u << Log2CacheLineSize;
290
291 // Extra padding amount in bytes to support prefetch mode 3.
292 unsigned FillSize = 3 * CacheLineSize;
293
294 if (AMDGPU::isGFX90A(STI)) {
295 Encoded_pad = Encoded_s_nop;
296 FillSize = 16 * CacheLineSize;
297 }
298
299 OS << "\t.p2alignl " << Log2CacheLineSize << ", " << Encoded_pad << '\n';
300 OS << "\t.fill " << (FillSize / 4) << ", 4, " << Encoded_pad << '\n';
301 return true;
302}
303
305 const MCSubtargetInfo &STI, StringRef KernelName,
306 const MCKernelDescriptor &KD, const MCExpr *NextVGPR,
307 const MCExpr *NextSGPR, const MCExpr *ReserveVCC,
308 const MCExpr *ReserveFlatScr) {
309 IsaVersion IVersion = getIsaVersion(STI.getCPU());
310 const MCAsmInfo &MAI = getContext().getAsmInfo();
311
312 OS << "\t.amdhsa_kernel " << KernelName << '\n';
313
314 auto PrintField = [&](const MCExpr *Expr, uint32_t Shift, uint32_t Mask,
316 OS << "\t\t" << Directive << ' ';
317 const MCExpr *ShiftedAndMaskedExpr =
318 MCKernelDescriptor::bits_get(Expr, Shift, Mask, getContext());
319 const MCExpr *New = foldAMDGPUMCExpr(ShiftedAndMaskedExpr, getContext());
320 printAMDGPUMCExpr(New, OS, &MAI);
321 OS << '\n';
322 };
323
324 auto EmitMCExpr = [&](const MCExpr *Value) {
326 printAMDGPUMCExpr(NewExpr, OS, &MAI);
327 };
328
329 OS << "\t\t.amdhsa_group_segment_fixed_size ";
330 EmitMCExpr(KD.group_segment_fixed_size);
331 OS << '\n';
332
333 OS << "\t\t.amdhsa_private_segment_fixed_size ";
334 EmitMCExpr(KD.private_segment_fixed_size);
335 OS << '\n';
336
337 OS << "\t\t.amdhsa_kernarg_size ";
338 EmitMCExpr(KD.kernarg_size);
339 OS << '\n';
340
341 if (isGFX1250Plus(STI)) {
343 amdhsa::COMPUTE_PGM_RSRC2_GFX125_USER_SGPR_COUNT_SHIFT,
344 amdhsa::COMPUTE_PGM_RSRC2_GFX125_USER_SGPR_COUNT,
345 ".amdhsa_user_sgpr_count");
346 } else {
348 amdhsa::COMPUTE_PGM_RSRC2_GFX6_GFX120_USER_SGPR_COUNT_SHIFT,
349 amdhsa::COMPUTE_PGM_RSRC2_GFX6_GFX120_USER_SGPR_COUNT,
350 ".amdhsa_user_sgpr_count");
351 }
352
356 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER_SHIFT,
357 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER,
358 ".amdhsa_user_sgpr_private_segment_buffer");
360 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR_SHIFT,
361 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR,
362 ".amdhsa_user_sgpr_dispatch_ptr");
364 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR_SHIFT,
365 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR,
366 ".amdhsa_user_sgpr_queue_ptr");
368 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR_SHIFT,
369 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR,
370 ".amdhsa_user_sgpr_kernarg_segment_ptr");
372 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID_SHIFT,
373 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID,
374 ".amdhsa_user_sgpr_dispatch_id");
377 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT_SHIFT,
378 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT,
379 ".amdhsa_user_sgpr_flat_scratch_init");
380 if (hasKernargPreload(STI)) {
381 PrintField(KD.kernarg_preload, amdhsa::KERNARG_PRELOAD_SPEC_LENGTH_SHIFT,
382 amdhsa::KERNARG_PRELOAD_SPEC_LENGTH,
383 ".amdhsa_user_sgpr_kernarg_preload_length");
384 PrintField(KD.kernarg_preload, amdhsa::KERNARG_PRELOAD_SPEC_OFFSET_SHIFT,
385 amdhsa::KERNARG_PRELOAD_SPEC_OFFSET,
386 ".amdhsa_user_sgpr_kernarg_preload_offset");
387 }
390 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE_SHIFT,
391 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE,
392 ".amdhsa_user_sgpr_private_segment_size");
393 if (STI.getFeatureBits().test(AMDGPU::FeatureSupportsWave32) &&
394 STI.getFeatureBits().test(AMDGPU::FeatureSupportsWave64)) {
396 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32_SHIFT,
397 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32,
398 ".amdhsa_wavefront_size32");
399 }
402 amdhsa::KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK_SHIFT,
403 amdhsa::KERNEL_CODE_PROPERTY_USES_DYNAMIC_STACK,
404 ".amdhsa_uses_dynamic_stack");
406 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT_SHIFT,
407 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_PRIVATE_SEGMENT,
409 ? ".amdhsa_enable_private_segment"
410 : ".amdhsa_system_sgpr_private_segment_wavefront_offset"));
412 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X_SHIFT,
413 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X,
414 ".amdhsa_system_sgpr_workgroup_id_x");
416 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Y_SHIFT,
417 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Y,
418 ".amdhsa_system_sgpr_workgroup_id_y");
420 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Z_SHIFT,
421 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_Z,
422 ".amdhsa_system_sgpr_workgroup_id_z");
424 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_INFO_SHIFT,
425 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_INFO,
426 ".amdhsa_system_sgpr_workgroup_info");
428 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_VGPR_WORKITEM_ID_SHIFT,
429 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_VGPR_WORKITEM_ID,
430 ".amdhsa_system_vgpr_workitem_id");
431
432 // These directives are required.
433 OS << "\t\t.amdhsa_next_free_vgpr ";
434 EmitMCExpr(NextVGPR);
435 OS << '\n';
436
437 OS << "\t\t.amdhsa_next_free_sgpr ";
438 EmitMCExpr(NextSGPR);
439 OS << '\n';
440
441 if (AMDGPU::isGFX90A(STI)) {
442 // MCExpr equivalent of taking the (accum_offset + 1) * 4.
443 const MCExpr *accum_bits = MCKernelDescriptor::bits_get(
445 amdhsa::COMPUTE_PGM_RSRC3_GFX90A_ACCUM_OFFSET_SHIFT,
446 amdhsa::COMPUTE_PGM_RSRC3_GFX90A_ACCUM_OFFSET, getContext());
447 accum_bits = MCBinaryExpr::createAdd(
448 accum_bits, MCConstantExpr::create(1, getContext()), getContext());
449 accum_bits = MCBinaryExpr::createMul(
450 accum_bits, MCConstantExpr::create(4, getContext()), getContext());
451 OS << "\t\t.amdhsa_accum_offset ";
452 const MCExpr *New = foldAMDGPUMCExpr(accum_bits, getContext());
453 printAMDGPUMCExpr(New, OS, &MAI);
454 OS << '\n';
455 }
456
457 if (isGFX1250Plus(STI))
459 amdhsa::COMPUTE_PGM_RSRC3_GFX125_NAMED_BAR_CNT_SHIFT,
460 amdhsa::COMPUTE_PGM_RSRC3_GFX125_NAMED_BAR_CNT,
461 ".amdhsa_named_barrier_count");
462
463 OS << "\t\t.amdhsa_reserve_vcc ";
464 EmitMCExpr(ReserveVCC);
465 OS << '\n';
466
467 if (IVersion.Major >= 7 && !hasArchitectedFlatScratch(STI)) {
468 OS << "\t\t.amdhsa_reserve_flat_scratch ";
469 EmitMCExpr(ReserveFlatScr);
470 OS << '\n';
471 }
472
473 switch (CodeObjectVersion) {
474 default:
475 break;
478 if (STI.hasFeature(AMDGPU::FeatureSupportsXNACK)) {
479 bool XnackOn = getTargetID()->isXnackOnOrAny();
480 OS << "\t\t.amdhsa_reserve_xnack_mask " << XnackOn << '\n';
481 }
482 break;
483 }
484
486 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_32_SHIFT,
487 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_32,
488 ".amdhsa_float_round_mode_32");
490 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_16_64_SHIFT,
491 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_ROUND_MODE_16_64,
492 ".amdhsa_float_round_mode_16_64");
494 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_32_SHIFT,
495 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_32,
496 ".amdhsa_float_denorm_mode_32");
498 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64_SHIFT,
499 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64,
500 ".amdhsa_float_denorm_mode_16_64");
501 if (STI.hasFeature(AMDGPU::FeatureDX10ClampAndIEEEMode)) {
503 amdhsa::COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_DX10_CLAMP_SHIFT,
504 amdhsa::COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_DX10_CLAMP,
505 ".amdhsa_dx10_clamp");
507 amdhsa::COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_IEEE_MODE_SHIFT,
508 amdhsa::COMPUTE_PGM_RSRC1_GFX6_GFX11_ENABLE_IEEE_MODE,
509 ".amdhsa_ieee_mode");
510 }
511 if (IVersion.Major >= 9) {
513 amdhsa::COMPUTE_PGM_RSRC1_GFX9_PLUS_FP16_OVFL_SHIFT,
514 amdhsa::COMPUTE_PGM_RSRC1_GFX9_PLUS_FP16_OVFL,
515 ".amdhsa_fp16_overflow");
516 }
517 if (AMDGPU::isGFX90A(STI))
519 amdhsa::COMPUTE_PGM_RSRC3_GFX90A_TG_SPLIT_SHIFT,
520 amdhsa::COMPUTE_PGM_RSRC3_GFX90A_TG_SPLIT, ".amdhsa_tg_split");
521 if (AMDGPU::supportsWGP(STI))
523 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_WGP_MODE_SHIFT,
524 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_WGP_MODE,
525 ".amdhsa_workgroup_processor_mode");
526 if (IVersion.Major >= 10) {
528 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_MEM_ORDERED_SHIFT,
529 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_MEM_ORDERED,
530 ".amdhsa_memory_ordered");
532 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_FWD_PROGRESS_SHIFT,
533 amdhsa::COMPUTE_PGM_RSRC1_GFX10_PLUS_FWD_PROGRESS,
534 ".amdhsa_forward_progress");
535 }
536 if (IVersion.Major >= 10 && IVersion.Major < 12) {
538 amdhsa::COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT_SHIFT,
539 amdhsa::COMPUTE_PGM_RSRC3_GFX10_GFX11_SHARED_VGPR_COUNT,
540 ".amdhsa_shared_vgpr_count");
541 }
542 if (IVersion.Major == 11) {
544 amdhsa::COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE_SHIFT,
545 amdhsa::COMPUTE_PGM_RSRC3_GFX11_INST_PREF_SIZE,
546 ".amdhsa_inst_pref_size");
547 }
548 if (IVersion.Major >= 12) {
550 amdhsa::COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE_SHIFT,
551 amdhsa::COMPUTE_PGM_RSRC3_GFX12_PLUS_INST_PREF_SIZE,
552 ".amdhsa_inst_pref_size");
554 amdhsa::COMPUTE_PGM_RSRC1_GFX12_PLUS_ENABLE_WG_RR_EN_SHIFT,
555 amdhsa::COMPUTE_PGM_RSRC1_GFX12_PLUS_ENABLE_WG_RR_EN,
556 ".amdhsa_round_robin_scheduling");
557 }
560 amdhsa::
561 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION_SHIFT,
562 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION,
563 ".amdhsa_exception_fp_ieee_invalid_op");
566 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_FP_DENORMAL_SOURCE_SHIFT,
567 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_FP_DENORMAL_SOURCE,
568 ".amdhsa_exception_fp_denorm_src");
571 amdhsa::
572 COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO_SHIFT,
573 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO,
574 ".amdhsa_exception_fp_ieee_div_zero");
577 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW_SHIFT,
578 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW,
579 ".amdhsa_exception_fp_ieee_overflow");
582 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW_SHIFT,
583 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW,
584 ".amdhsa_exception_fp_ieee_underflow");
587 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INEXACT_SHIFT,
588 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_IEEE_754_FP_INEXACT,
589 ".amdhsa_exception_fp_ieee_inexact");
592 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO_SHIFT,
593 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO,
594 ".amdhsa_exception_int_div_zero");
595
596 OS << "\t.end_amdhsa_kernel\n";
597}
598
599namespace {
600/// Callback type invoked by \c forEachInfoScope for each function scope in
601/// the canonical iteration order. The scope is emitted exactly once per
602/// unique \p Sym regardless of how many flat entries reference it.
603using InfoScopeEmitter = function_ref<void(
605 ArrayRef<MCSymbol *> Calls, ArrayRef<StringRef> IndirectCallTypeIds,
606 ArrayRef<StringRef> TypeIds)>;
607
608/// Group the flat edge lists in \p Data by source function symbol and drive
609/// per-scope emission. A scope is opened for every function with attached
610/// info and for every function that appears only as an edge source; each
611/// scope is emitted exactly once. Both the asm and ELF streamers share this
612/// iteration logic and only differ in the per-scope emission callback.
613static void forEachInfoScope(const AMDGPU::InfoSectionData &Data,
614 InfoScopeEmitter Emit) {
619 for (const auto &[Func, Res] : Data.Uses)
620 FuncUses[Func].push_back(Res);
621 for (const auto &[Src, Dst] : Data.Calls)
622 FuncCalls[Src].push_back(Dst);
623 for (const auto &[Func, TypeId] : Data.IndirectCalls)
624 FuncIndirectCalls[Func].push_back(TypeId);
625 for (const auto &[Sym, TypeId] : Data.TypeIds)
626 FuncTypeIds[Sym].push_back(TypeId);
627
628 DenseSet<MCSymbol *> Emitted;
629 auto EmitIfNew = [&](MCSymbol *Sym, const AMDGPU::FuncInfo *Info) {
630 if (!Emitted.insert(Sym).second)
631 return;
633 ArrayRef<StringRef> IndirectCallTypeIds, TypeIds;
634 if (auto It = FuncUses.find(Sym); It != FuncUses.end())
635 Uses = It->second;
636 if (auto It = FuncCalls.find(Sym); It != FuncCalls.end())
637 Calls = It->second;
638 if (auto It = FuncIndirectCalls.find(Sym); It != FuncIndirectCalls.end())
639 IndirectCallTypeIds = It->second;
640 if (auto It = FuncTypeIds.find(Sym); It != FuncTypeIds.end())
641 TypeIds = It->second;
642 Emit(Sym, Info, Uses, Calls, IndirectCallTypeIds, TypeIds);
643 };
644
645 for (const AMDGPU::FuncInfo &Func : Data.Funcs)
646 EmitIfNew(Func.Sym, &Func);
647 // Emit scopes for functions that only appear as edge sources (e.g. typeid
648 // tags on address-taken declarations, or callers of external functions).
649 for (const auto &[Sym, TypeId] : Data.TypeIds)
650 EmitIfNew(Sym, nullptr);
651 for (const auto &[Sym, Res] : Data.Uses)
652 EmitIfNew(Sym, nullptr);
653 for (const auto &[Sym, Dst] : Data.Calls)
654 EmitIfNew(Sym, nullptr);
655 for (const auto &[Sym, TypeId] : Data.IndirectCalls)
656 EmitIfNew(Sym, nullptr);
657}
658} // namespace
659
662 forEachInfoScope(Data, [&](MCSymbol *Sym, const AMDGPU::FuncInfo *Info,
665 ArrayRef<StringRef> IndirectCallTypeIds,
666 ArrayRef<StringRef> TypeIds) {
667 OS << "\t.amdgpu_info " << Sym->getName() << '\n';
668 if (Info) {
669 AMDGPU::FuncInfoFlags Flags{};
670 if (Info->UsesVCC)
672 if (Info->UsesFlatScratch)
674 if (Info->HasDynStack)
676 OS << "\t\t.amdgpu_flags " << llvm::to_underlying(Flags) << '\n';
677 OS << "\t\t.amdgpu_num_sgpr " << Info->NumSGPR << '\n';
678 OS << "\t\t.amdgpu_num_vgpr " << Info->NumArchVGPR << '\n';
679 if (Info->NumAccVGPR)
680 OS << "\t\t.amdgpu_num_agpr " << Info->NumAccVGPR << '\n';
681 OS << "\t\t.amdgpu_private_segment_size " << Info->PrivateSegmentSize
682 << '\n';
683 }
684 for (MCSymbol *Res : Uses)
685 OS << "\t\t.amdgpu_use " << Res->getName() << '\n';
686 for (MCSymbol *Dst : Calls)
687 OS << "\t\t.amdgpu_call " << Dst->getName() << '\n';
688 for (StringRef TypeId : IndirectCallTypeIds)
689 OS << "\t\t.amdgpu_indirect_call \"" << TypeId << "\"\n";
690 for (StringRef TypeId : TypeIds)
691 OS << "\t\t.amdgpu_typeid \"" << TypeId << "\"\n";
692 OS << "\t.end_amdgpu_info\n\n";
693 });
694}
695
696//===----------------------------------------------------------------------===//
697// AMDGPUTargetELFStreamer
698//===----------------------------------------------------------------------===//
699
703
705 return static_cast<MCELFStreamer &>(Streamer);
706}
707
708// A hook for emitting stuff at the end.
709// We use it for emitting the accumulated PAL metadata as a .note record.
710// The PAL metadata is reset after it is emitted.
713 W.setELFHeaderEFlags(getEFlags());
714 W.setOverrideABIVersion(
715 getELFABIVersion(STI.getTargetTriple(), CodeObjectVersion));
716
717 std::string Blob;
718 const char *Vendor = getPALMetadata()->getVendor();
719 unsigned Type = getPALMetadata()->getType();
720 getPALMetadata()->toBlob(Type, Blob);
721 if (Blob.empty())
722 return;
723 EmitNote(Vendor, MCConstantExpr::create(Blob.size(), getContext()), Type,
724 [&](MCELFStreamer &OS) { OS.emitBytes(Blob); });
725
726 // Reset the pal metadata so its data will not affect a compilation that
727 // reuses this object.
729}
730
731void AMDGPUTargetELFStreamer::EmitNote(
732 StringRef Name, const MCExpr *DescSZ, unsigned NoteType,
733 function_ref<void(MCELFStreamer &)> EmitDesc) {
734 auto &S = getStreamer();
735 auto &Context = S.getContext();
736
737 auto NameSZ = Name.size() + 1;
738
739 unsigned NoteFlags = 0;
740 // TODO Apparently, this is currently needed for OpenCL as mentioned in
741 // https://reviews.llvm.org/D74995
742 if (isHsaAbi(STI))
743 NoteFlags = ELF::SHF_ALLOC;
744
745 S.pushSection();
746 S.switchSection(
747 Context.getELFSection(ElfNote::SectionName, ELF::SHT_NOTE, NoteFlags));
748 S.emitInt32(NameSZ); // namesz
749 S.emitValue(DescSZ, 4); // descz
750 S.emitInt32(NoteType); // type
751 S.emitBytes(Name); // name
752 S.emitInt8(0); // null terminator
753 S.emitValueToAlignment(Align(4), 0, 1, 0); // padding 0
754 EmitDesc(S); // desc
755 S.emitValueToAlignment(Align(4), 0, 1, 0); // padding 0
756 S.popSection();
757}
758
759unsigned AMDGPUTargetELFStreamer::getEFlags() {
760 switch (STI.getTargetTriple().getArch()) {
761 default:
762 llvm_unreachable("Unsupported Arch");
763 case Triple::r600:
764 return getEFlagsR600();
765 case Triple::amdgpu:
766 return getEFlagsAMDGCN();
767 }
768}
769
770unsigned AMDGPUTargetELFStreamer::getEFlagsR600() {
771 assert(STI.getTargetTriple().getArch() == Triple::r600);
772
773 return getElfMach(STI.getCPU());
774}
775
776unsigned AMDGPUTargetELFStreamer::getEFlagsAMDGCN() {
777 assert(STI.getTargetTriple().isAMDGCN());
778
779 switch (STI.getTargetTriple().getOS()) {
780 default:
781 // TODO: Why are some tests have "mingw" listed as OS?
782 // llvm_unreachable("Unsupported OS");
784 return getEFlagsUnknownOS();
785 case Triple::AMDHSA:
786 return getEFlagsAMDHSA();
787 case Triple::AMDPAL:
788 return getEFlagsAMDPAL();
789 case Triple::Mesa3D:
790 return getEFlagsMesa3D();
791 }
792}
793
794unsigned AMDGPUTargetELFStreamer::getEFlagsUnknownOS() {
795 // TODO: Why are some tests have "mingw" listed as OS?
796 // assert(STI.getTargetTriple().getOS() == Triple::UnknownOS);
797
798 return getEFlagsV3();
799}
800
801unsigned AMDGPUTargetELFStreamer::getEFlagsAMDHSA() {
802 assert(isHsaAbi(STI));
803
804 if (CodeObjectVersion >= 6)
805 return getEFlagsV6();
806 return getEFlagsV4();
807}
808
809unsigned AMDGPUTargetELFStreamer::getEFlagsAMDPAL() {
810 assert(STI.getTargetTriple().getOS() == Triple::AMDPAL);
811
812 return getEFlagsV3();
813}
814
815unsigned AMDGPUTargetELFStreamer::getEFlagsMesa3D() {
816 assert(STI.getTargetTriple().getOS() == Triple::Mesa3D);
817
818 return getEFlagsV3();
819}
820
821unsigned AMDGPUTargetELFStreamer::getEFlagsV3() {
822 unsigned EFlagsV3 = 0;
823
824 // mach.
825 EFlagsV3 |= getElfMach(STI.getCPU());
826
827 // xnack.
828 if (getTargetID()->isXnackOnOrAny())
830 // sramecc.
831 if (getTargetID()->isSramEccOnOrAny())
833
834 return EFlagsV3;
835}
836
837unsigned AMDGPUTargetELFStreamer::getEFlagsV4() {
838 unsigned EFlagsV4 = 0;
839
840 // mach.
841 EFlagsV4 |= getElfMach(STI.getCPU());
842
843 // xnack.
844 // Hardwired-on XNACK is implied by the processor, not an ELF mode selection.
846 STI.hasFeature(AMDGPU::FeatureXNACKOnOffModes)
847 ? getTargetID()->getXnackSetting()
848 : AMDGPU::TargetIDSetting::Unsupported;
849 switch (XnackSetting) {
850 case AMDGPU::TargetIDSetting::Unsupported:
851 break;
852 case AMDGPU::TargetIDSetting::Any:
854 break;
855 case AMDGPU::TargetIDSetting::Off:
857 break;
858 case AMDGPU::TargetIDSetting::On:
860 break;
861 }
862 // sramecc.
863 // Hardwired-on SRAMECC is implied by the processor, not an ELF mode.
865 STI.hasFeature(AMDGPU::FeatureSRAMECCOnOffModes)
866 ? getTargetID()->getSramEccSetting()
867 : AMDGPU::TargetIDSetting::Unsupported;
868 switch (SramEccSetting) {
869 case AMDGPU::TargetIDSetting::Unsupported:
870 break;
871 case AMDGPU::TargetIDSetting::Any:
873 break;
874 case AMDGPU::TargetIDSetting::Off:
876 break;
877 case AMDGPU::TargetIDSetting::On:
879 break;
880 }
881
882 return EFlagsV4;
883}
884
885unsigned AMDGPUTargetELFStreamer::getEFlagsV6() {
886 unsigned Flags = getEFlagsV4();
887
888 unsigned Version = ForceGenericVersion;
889 if (!Version) {
890 switch (parseArchAMDGCN(STI.getCPU())) {
891 case AMDGPU::GK_GFX9_GENERIC:
893 break;
894 case AMDGPU::GK_GFX9_4_GENERIC:
896 break;
897 case AMDGPU::GK_GFX10_1_GENERIC:
899 break;
900 case AMDGPU::GK_GFX10_3_GENERIC:
902 break;
903 case AMDGPU::GK_GFX11_GENERIC:
905 break;
906 case AMDGPU::GK_GFX11_7_GENERIC:
908 break;
909 case AMDGPU::GK_GFX12_GENERIC:
911 break;
912 case AMDGPU::GK_GFX12_5_GENERIC:
914 break;
915 case AMDGPU::GK_GFX13_GENERIC:
917 break;
918 default:
919 break;
920 }
921 }
922
923 // Versions start at 1.
924 if (Version) {
926 report_fatal_error("Cannot encode generic code object version " +
927 Twine(Version) +
928 " - no ELF flag can represent this version!");
930 }
931
932 return Flags;
933}
934
936
938 MCStreamer &OS = getStreamer();
939 OS.pushSection();
940 Header.EmitKernelCodeT(OS, getContext());
941 OS.popSection();
942}
943
945 unsigned Type) {
946 auto *Symbol = static_cast<MCSymbolELF *>(
948 Symbol->setType(Type);
949}
950
952 Align Alignment) {
953 auto *SymbolELF = static_cast<MCSymbolELF *>(Symbol);
954 SymbolELF->setType(ELF::STT_OBJECT);
955
956 if (!SymbolELF->isBindingSet())
957 SymbolELF->setBinding(ELF::STB_GLOBAL);
958
959 if (SymbolELF->declareCommon(Size, Alignment)) {
960 report_fatal_error("Symbol: " + Symbol->getName() +
961 " redeclared as different type");
962 }
963
964 SymbolELF->setIndex(ELF::SHN_AMDGPU_LDS);
965 SymbolELF->setSize(MCConstantExpr::create(Size, getContext()));
966}
967
969 // Create two labels to mark the beginning and end of the desc field
970 // and a MCExpr to calculate the size of the desc field.
971 auto &Context = getContext();
972 auto *DescBegin = Context.createTempSymbol();
973 auto *DescEnd = Context.createTempSymbol();
974 auto *DescSZ = MCBinaryExpr::createSub(
975 MCSymbolRefExpr::create(DescEnd, Context),
976 MCSymbolRefExpr::create(DescBegin, Context), Context);
977
979 [&](MCELFStreamer &OS) {
980 OS.emitLabel(DescBegin);
981
982 SmallString<32> Str;
983 raw_svector_ostream StrOS(Str);
984 StrOS << *getTargetID();
985
986 OS.emitBytes(StrOS.str());
987 OS.emitLabel(DescEnd);
988 });
989 return true;
990}
991
993 bool Strict) {
995 if (!Verifier.verify(HSAMetadataDoc.getRoot()))
996 return false;
997
998 std::string HSAMetadataString;
999 HSAMetadataDoc.writeToBlob(HSAMetadataString);
1000
1001 // Create two labels to mark the beginning and end of the desc field
1002 // and a MCExpr to calculate the size of the desc field.
1003 auto &Context = getContext();
1004 auto *DescBegin = Context.createTempSymbol();
1005 auto *DescEnd = Context.createTempSymbol();
1006 auto *DescSZ = MCBinaryExpr::createSub(
1007 MCSymbolRefExpr::create(DescEnd, Context),
1008 MCSymbolRefExpr::create(DescBegin, Context), Context);
1009
1011 [&](MCELFStreamer &OS) {
1012 OS.emitLabel(DescBegin);
1013 OS.emitBytes(HSAMetadataString);
1014 OS.emitLabel(DescEnd);
1015 });
1016 return true;
1017}
1018
1020 const uint32_t Encoded_s_code_end = 0xbf9f0000;
1021 const uint32_t Encoded_s_nop = 0xbf800000;
1022 uint32_t Encoded_pad = Encoded_s_code_end;
1023
1024 // Instruction cache line size in bytes.
1025 const unsigned Log2CacheLineSize = AMDGPU::isGFX11Plus(STI) ? 7 : 6;
1026 const unsigned CacheLineSize = 1u << Log2CacheLineSize;
1027
1028 // Extra padding amount in bytes to support prefetch mode 3.
1029 unsigned FillSize = 3 * CacheLineSize;
1030
1031 if (AMDGPU::isGFX90A(STI)) {
1032 Encoded_pad = Encoded_s_nop;
1033 FillSize = 16 * CacheLineSize;
1034 }
1035
1036 MCStreamer &OS = getStreamer();
1037 OS.pushSection();
1038 OS.emitValueToAlignment(Align(CacheLineSize), Encoded_pad, 4);
1039 for (unsigned I = 0; I < FillSize; I += 4)
1040 OS.emitInt32(Encoded_pad);
1041 OS.popSection();
1042 return true;
1043}
1044
1046 const MCSubtargetInfo &STI, StringRef KernelName,
1047 const MCKernelDescriptor &KernelDescriptor, const MCExpr *NextVGPR,
1048 const MCExpr *NextSGPR, const MCExpr *ReserveVCC,
1049 const MCExpr *ReserveFlatScr) {
1050 auto &Streamer = getStreamer();
1051 auto &Context = Streamer.getContext();
1052
1053 auto *KernelCodeSymbol =
1054 static_cast<MCSymbolELF *>(Context.getOrCreateSymbol(Twine(KernelName)));
1055 auto *KernelDescriptorSymbol = static_cast<MCSymbolELF *>(
1056 Context.getOrCreateSymbol(Twine(KernelName) + Twine(".kd")));
1057
1058 // Copy kernel descriptor symbol's binding, other and visibility from the
1059 // kernel code symbol.
1060 KernelDescriptorSymbol->setBinding(KernelCodeSymbol->getBinding());
1061 KernelDescriptorSymbol->setOther(KernelCodeSymbol->getOther());
1062 KernelDescriptorSymbol->setVisibility(KernelCodeSymbol->getVisibility());
1063 // Kernel descriptor symbol's type and size are fixed.
1064 KernelDescriptorSymbol->setType(ELF::STT_OBJECT);
1065 KernelDescriptorSymbol->setSize(
1067
1068 // The visibility of the kernel code symbol must be protected or less to allow
1069 // static relocations from the kernel descriptor to be used.
1070 if (KernelCodeSymbol->getVisibility() == ELF::STV_DEFAULT)
1071 KernelCodeSymbol->setVisibility(ELF::STV_PROTECTED);
1072
1073 Streamer.emitLabel(KernelDescriptorSymbol);
1074 Streamer.emitValue(
1075 KernelDescriptor.group_segment_fixed_size,
1077 Streamer.emitValue(
1078 KernelDescriptor.private_segment_fixed_size,
1080 Streamer.emitValue(KernelDescriptor.kernarg_size,
1082
1083 for (uint32_t i = 0; i < sizeof(amdhsa::kernel_descriptor_t::reserved0); ++i)
1084 Streamer.emitInt8(0u);
1085
1086 // FIXME: Remove the use of VK_AMDGPU_REL64 in the expression below. The
1087 // expression being created is:
1088 // (start of kernel code) - (start of kernel descriptor)
1089 // It implies R_AMDGPU_REL64, but ends up being R_AMDGPU_ABS64.
1090 Streamer.emitValue(
1093 Context),
1094 MCSymbolRefExpr::create(KernelDescriptorSymbol, Context), Context),
1096 for (uint32_t i = 0; i < sizeof(amdhsa::kernel_descriptor_t::reserved1); ++i)
1097 Streamer.emitInt8(0u);
1098 Streamer.emitValue(KernelDescriptor.compute_pgm_rsrc3,
1100 Streamer.emitValue(KernelDescriptor.compute_pgm_rsrc1,
1102 Streamer.emitValue(KernelDescriptor.compute_pgm_rsrc2,
1104 Streamer.emitValue(
1105 KernelDescriptor.kernel_code_properties,
1107 Streamer.emitValue(KernelDescriptor.kernarg_preload,
1109 for (uint32_t i = 0; i < sizeof(amdhsa::kernel_descriptor_t::reserved3); ++i)
1110 Streamer.emitInt8(0u);
1111}
1112
1116 MCContext &Context = S.getContext();
1117
1119 auto getOrAddString = [&](StringRef Str) -> uint32_t {
1120 if (Str.empty())
1121 return UINT32_MAX;
1122 return StrTab.add(Str);
1123 };
1124
1125 auto EmitU32Entry = [&](AMDGPU::InfoKind Kind, uint32_t Val) {
1126 S.emitInt8(static_cast<uint8_t>(Kind));
1127 S.emitInt8(4);
1128 S.emitInt32(Val);
1129 };
1130 auto EmitSymEntry = [&](AMDGPU::InfoKind Kind, MCSymbol *Sym) {
1131 S.emitInt8(static_cast<uint8_t>(Kind));
1132 S.emitInt8(8);
1133 S.emitValue(MCSymbolRefExpr::create(Sym, Context), 8);
1134 };
1135
1136 S.pushSection();
1137 MCSectionELF *InfoSec = Context.getELFSection(
1138 ".amdgpu.info", ELF::SHT_PROGBITS, ELF::SHF_EXCLUDE);
1139 S.switchSection(InfoSec);
1140
1141 forEachInfoScope(Data, [&](MCSymbol *Sym, const AMDGPU::FuncInfo *Info,
1144 ArrayRef<StringRef> IndirectCallTypeIds,
1145 ArrayRef<StringRef> TypeIds) {
1146 EmitSymEntry(AMDGPU::InfoKind::INFO_FUNC, Sym);
1147
1148 if (Info) {
1149 AMDGPU::FuncInfoFlags Flags{};
1150 if (Info->UsesVCC)
1152 if (Info->UsesFlatScratch)
1154 if (Info->HasDynStack)
1157 EmitU32Entry(AMDGPU::InfoKind::INFO_NUM_SGPR, Info->NumSGPR);
1158 EmitU32Entry(AMDGPU::InfoKind::INFO_NUM_VGPR, Info->NumArchVGPR);
1159 // INFO_NUM_AGPR is only emitted when the function actually uses AGPRs,
1160 // since AGPRs are not available on all architectures.
1161 if (Info->NumAccVGPR)
1162 EmitU32Entry(AMDGPU::InfoKind::INFO_NUM_AGPR, Info->NumAccVGPR);
1164 Info->PrivateSegmentSize);
1165 }
1166
1167 for (MCSymbol *Res : Uses)
1168 EmitSymEntry(AMDGPU::InfoKind::INFO_USE, Res);
1169 for (MCSymbol *Dst : Calls)
1170 EmitSymEntry(AMDGPU::InfoKind::INFO_CALL, Dst);
1171 for (StringRef TypeId : IndirectCallTypeIds) {
1173 getOrAddString(TypeId));
1174 }
1175 for (StringRef TypeId : TypeIds)
1176 EmitU32Entry(AMDGPU::InfoKind::INFO_TYPEID, getOrAddString(TypeId));
1177 });
1178
1179 if (!StrTab.empty()) {
1180 StrTab.finalizeInOrder();
1181 MCSectionELF *Sec = Context.getELFSection(".amdgpu.strtab", ELF::SHT_STRTAB,
1183 S.switchSection(Sec);
1184 SmallString<128> Buf;
1185 raw_svector_ostream OS(Buf);
1186 StrTab.write(OS);
1187 S.emitBytes(Buf);
1188 }
1189
1190 S.popSection();
1191}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDHSA kernel descriptor MCExpr struct for use in MC layer.
Provides AMDGPU specific target descriptions.
This is a verifier for AMDGPU HSA metadata, which can verify both well-typed metadata and untyped met...
AMDGPU metadata definitions and in-memory representations.
Enums shared between the AMDGPU backend (LLVM) and the ELF linker (LLD) for the .amdgpu....
Enums and constants for AMDGPU PT_NOTE sections.
static cl::opt< bool > SramEccSetting("amdgpu-sramecc", cl::desc("Force amdgpu.sramecc for testing"), cl::ReallyHidden)
static cl::opt< bool > XnackSetting("amdgpu-xnack", cl::desc("Force amdgpu.xnack value for testing"), cl::ReallyHidden)
static cl::opt< unsigned > ForceGenericVersion("amdgpu-force-generic-version", cl::desc("Force a specific generic_v<N> flag to be " "added. For testing purposes only."), cl::ReallyHidden, cl::init(0))
#define PRINT_RES_INFO(ARG)
AMDHSA kernel descriptor definitions.
MC layer struct for AMDGPUMCKernelCodeT, provides MCExpr functionality where required.
#define I(x, y, z)
Definition MD5.cpp:57
Remove Loads Into Fake Uses
verify safepoint Safepoint IR Verifier
static cl::opt< unsigned > CacheLineSize("cache-line-size", cl::init(0), cl::Hidden, cl::desc("Use this to override the target cache line size when " "specified by the user."))
const char * getVendor() const
void toBlob(unsigned Type, std::string &S)
void toString(std::string &S)
void emitAMDGPUInfo(const AMDGPU::InfoSectionData &Data) override
AMDGPUTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override
void EmitDirectiveAMDHSACodeObjectVersion(unsigned COV) override
void EmitMCResourceMaximums(const MCSymbol *MaxVGPR, const MCSymbol *MaxAGPR, const MCSymbol *MaxSGPR, const MCSymbol *MaxNamedBarrier) override
void EmitAMDKernelCodeT(AMDGPU::AMDGPUMCKernelCodeT &Header) override
void EmitAmdhsaKernelDescriptor(const MCSubtargetInfo &STI, StringRef KernelName, const AMDGPU::MCKernelDescriptor &KernelDescriptor, const MCExpr *NextVGPR, const MCExpr *NextSGPR, const MCExpr *ReserveVCC, const MCExpr *ReserveFlatScr) override
void EmitMCResourceInfo(const MCSymbol *NumVGPR, const MCSymbol *NumAGPR, const MCSymbol *NumExplicitSGPR, const MCSymbol *NumNamedBarrier, const MCSymbol *PrivateSegmentSize, const MCSymbol *UsesVCC, const MCSymbol *UsesFlatScratch, const MCSymbol *HasDynamicallySizedStack, const MCSymbol *HasRecursion, const MCSymbol *HasIndirectCall) override
bool EmitCodeEnd(const MCSubtargetInfo &STI) override
void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override
bool EmitCodeEnd(const MCSubtargetInfo &STI) override
void EmitAMDKernelCodeT(AMDGPU::AMDGPUMCKernelCodeT &Header) override
bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict) override
AMDGPUTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
void emitAMDGPULDS(MCSymbol *Sym, unsigned Size, Align Alignment) override
void EmitAmdhsaKernelDescriptor(const MCSubtargetInfo &STI, StringRef KernelName, const AMDGPU::MCKernelDescriptor &KernelDescriptor, const MCExpr *NextVGPR, const MCExpr *NextSGPR, const MCExpr *ReserveVCC, const MCExpr *ReserveFlatScr) override
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override
void emitAMDGPUInfo(const AMDGPU::InfoSectionData &Data) override
virtual bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict)
Emit HSA Metadata.
AMDGPUPALMetadata * getPALMetadata()
void initializeTargetID(const MCSubtargetInfo &STI, bool ApplyFeatureString=false)
virtual void EmitDirectiveAMDHSACodeObjectVersion(unsigned COV)
virtual bool EmitHSAMetadataV3(StringRef HSAMetadataString)
static unsigned getElfMach(StringRef GPU)
const std::optional< AMDGPU::TargetID > & getTargetID() const
std::optional< AMDGPU::TargetID > TargetID
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
constexpr bool test(unsigned I) const
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
static const MCBinaryExpr * createMul(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:397
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition MCExpr.h:427
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
const MCAsmInfo & getAsmInfo() const
Definition MCContext.h:411
ELFObjectWriter & getWriter()
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
This represents a section on linux, lots of unix variants and some bare metal systems.
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
Definition MCStreamer.h:326
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void pushSection()
Save the current and previous section on the section stack.
Definition MCStreamer.h:460
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
Definition MCStreamer.h:769
void emitInt8(uint64_t Value)
Definition MCStreamer.h:767
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
StringRef getFeatureString() const
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
StringRef getCPU() const
LLVM_ABI void setBinding(unsigned Binding) const
LLVM_ABI void setType(unsigned Type) const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Utility for building string tables with deduplicated suffixes.
LLVM_ABI void finalizeInOrder()
Finalize the string table without reording it.
LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority=0)
Add a string to the builder.
LLVM_ABI void write(raw_ostream &OS) const
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:515
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
An efficient, type-erasing, non-owning reference to a callable.
Simple in-memory representation of a document of msgpack objects with ability to find and create arra...
DocNode & getRoot()
Get ref to the document's root element.
LLVM_ABI void toYAML(raw_ostream &OS)
Convert MsgPack Document to YAML text.
LLVM_ABI void writeToBlob(std::string &Blob)
Write a MsgPack document to a binary MsgPack blob.
LLVM_ABI bool fromYAML(StringRef S)
Read YAML text into the MsgPack document. Returns false on failure.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const char NoteNameV2[]
const char SectionName[]
const char NoteNameV3[]
static constexpr unsigned GFX12_5
static constexpr unsigned GFX9_4
static constexpr unsigned GFX10_1
static constexpr unsigned GFX10_3
static constexpr unsigned GFX11
static constexpr unsigned GFX9
static constexpr unsigned GFX12
static constexpr unsigned GFX13
static constexpr unsigned GFX11_7
constexpr char AssemblerDirectiveBegin[]
HSA metadata beginning assembler directive.
constexpr char AssemblerDirectiveEnd[]
HSA metadata ending assembler directive.
FuncInfoFlags
Per-function flags packed into INFO_FLAGS entries.
void printAMDGPUMCExpr(const MCExpr *Expr, raw_ostream &OS, const MCAsmInfo *MAI)
bool isHsaAbi(const MCSubtargetInfo &STI)
TargetID createAMDGPUTargetID(const MCSubtargetInfo &STI, StringRef FeatureString)
Construct TargetID from MCSubtargetInfo.
LLVM_ABI IsaVersion getIsaVersion(StringRef GPU)
GPUKind
GPU kinds supported by the AMDGPU target.
bool isGFX90A(const MCSubtargetInfo &STI)
LLVM_ABI GPUKind parseArchAMDGCN(StringRef CPU)
bool hasArchitectedFlatScratch(const MCSubtargetInfo &STI)
bool isGFX11Plus(const MCSubtargetInfo &STI)
const MCExpr * foldAMDGPUMCExpr(const MCExpr *Expr, MCContext &Ctx)
InfoKind
Entry kind values for the .amdgpu.info section.
@ INFO_INDIRECT_CALL
Indirect call edge: the function contains an indirect call whose callee is expected to match the type...
@ INFO_FLAGS
Bitfield of FuncInfoFlags properties for the function. [u32].
@ INFO_FUNC
Opens a new function scope.
@ INFO_NUM_SGPR
Number of SGPRs explicitly used by the function. [u32].
@ INFO_NUM_VGPR
Number of architectural VGPRs used by the function. [u32].
@ INFO_CALL
Direct call edge: the function calls the callee identified by the 8-byte relocated symbol.
@ INFO_NUM_AGPR
Number of accumulator VGPRs (AGPRs) used by the function. [u32].
@ INFO_TYPEID
Function type ID: tags an address-taken function with a type-ID string (at the given ....
@ INFO_PRIVATE_SEGMENT_SIZE
Private (scratch) memory size in bytes required by the function. [u32].
@ INFO_USE
Dependency edge: the function uses the resource identified by the 8-byte relocated symbol (e....
unsigned hasKernargPreload(const MCSubtargetInfo &STI)
bool supportsWGP(const MCSubtargetInfo &STI)
bool isGFX1250Plus(const MCSubtargetInfo &STI)
uint8_t getELFABIVersion(const Triple &T, unsigned CodeObjectVersion)
LLVM_ABI GPUKind parseArchR600(StringRef CPU)
@ NT_AMDGPU_METADATA
Definition ELF.h:1813
@ SHN_AMDGPU_LDS
Definition ELF.h:1796
@ SHF_EXCLUDE
Definition ELF.h:1101
@ SHF_ALLOC
Definition ELF.h:1067
@ SHT_STRTAB
Definition ELF.h:967
@ SHT_PROGBITS
Definition ELF.h:965
@ SHT_NOTE
Definition ELF.h:971
@ STB_GLOBAL
Definition ELF.h:1230
@ STT_AMDGPU_HSA_KERNEL
Definition ELF.h:1255
@ STT_OBJECT
Definition ELF.h:1242
@ EF_AMDGPU_GENERIC_VERSION_MAX
Definition ELF.h:741
@ EF_AMDGPU_FEATURE_XNACK_ANY_V4
Definition ELF.h:718
@ EF_AMDGPU_FEATURE_SRAMECC_V3
Definition ELF.h:709
@ EF_AMDGPU_GENERIC_VERSION_OFFSET
Definition ELF.h:739
@ EF_AMDGPU_FEATURE_SRAMECC_OFF_V4
Definition ELF.h:733
@ EF_AMDGPU_FEATURE_XNACK_OFF_V4
Definition ELF.h:720
@ EF_AMDGPU_FEATURE_XNACK_V3
Definition ELF.h:704
@ EF_AMDGPU_FEATURE_XNACK_ON_V4
Definition ELF.h:722
@ EF_AMDGPU_MACH_NONE
Definition ELF.h:663
@ EF_AMDGPU_FEATURE_SRAMECC_ANY_V4
Definition ELF.h:731
@ EF_AMDGPU_FEATURE_SRAMECC_ON_V4
Definition ELF.h:735
@ NT_AMD_HSA_ISA_NAME
Definition ELF.h:1806
@ STV_PROTECTED
Definition ELF.h:1262
@ STV_DEFAULT
Definition ELF.h:1259
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
Instruction set architecture version.
static const MCExpr * bits_get(const MCExpr *Src, uint32_t Shift, uint32_t Mask, MCContext &Ctx)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39