LLVM 19.0.0git
AMDGPUHSAMetadataStreamer.cpp
Go to the documentation of this file.
1//===--- AMDGPUHSAMetadataStreamer.cpp --------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// AMDGPU HSA Metadata Streamer.
11///
12//
13//===----------------------------------------------------------------------===//
14
16#include "AMDGPU.h"
17#include "GCNSubtarget.h"
20#include "SIProgramInfo.h"
21#include "llvm/IR/Module.h"
22using namespace llvm;
23
24static std::pair<Type *, Align> getArgumentTypeAlign(const Argument &Arg,
25 const DataLayout &DL) {
26 Type *Ty = Arg.getType();
27 MaybeAlign ArgAlign;
28 if (Arg.hasByRefAttr()) {
29 Ty = Arg.getParamByRefType();
30 ArgAlign = Arg.getParamAlign();
31 }
32
33 if (!ArgAlign)
34 ArgAlign = DL.getABITypeAlign(Ty);
35
36 return std::pair(Ty, *ArgAlign);
37}
38
39namespace llvm {
40
42 "amdgpu-dump-hsa-metadata",
43 cl::desc("Dump AMDGPU HSA Metadata"));
45 "amdgpu-verify-hsa-metadata",
46 cl::desc("Verify AMDGPU HSA Metadata"));
47
48namespace AMDGPU {
49namespace HSAMD {
50
51//===----------------------------------------------------------------------===//
52// HSAMetadataStreamerV4
53//===----------------------------------------------------------------------===//
54
55void MetadataStreamerMsgPackV4::dump(StringRef HSAMetadataString) const {
56 errs() << "AMDGPU HSA Metadata:\n" << HSAMetadataString << '\n';
57}
58
59void MetadataStreamerMsgPackV4::verify(StringRef HSAMetadataString) const {
60 errs() << "AMDGPU HSA Metadata Parser Test: ";
61
62 msgpack::Document FromHSAMetadataString;
63
64 if (!FromHSAMetadataString.fromYAML(HSAMetadataString)) {
65 errs() << "FAIL\n";
66 return;
67 }
68
69 std::string ToHSAMetadataString;
70 raw_string_ostream StrOS(ToHSAMetadataString);
71 FromHSAMetadataString.toYAML(StrOS);
72
73 errs() << (HSAMetadataString == StrOS.str() ? "PASS" : "FAIL") << '\n';
74 if (HSAMetadataString != ToHSAMetadataString) {
75 errs() << "Original input: " << HSAMetadataString << '\n'
76 << "Produced output: " << StrOS.str() << '\n';
77 }
78}
79
80std::optional<StringRef>
83 .Case("read_only", StringRef("read_only"))
84 .Case("write_only", StringRef("write_only"))
85 .Case("read_write", StringRef("read_write"))
86 .Default(std::nullopt);
87}
88
90 unsigned AddressSpace) const {
91 switch (AddressSpace) {
93 return StringRef("private");
95 return StringRef("global");
97 return StringRef("constant");
99 return StringRef("local");
101 return StringRef("generic");
103 return StringRef("region");
104 default:
105 return std::nullopt;
106 }
107}
108
111 StringRef BaseTypeName) const {
112 if (TypeQual.contains("pipe"))
113 return "pipe";
114
115 return StringSwitch<StringRef>(BaseTypeName)
116 .Case("image1d_t", "image")
117 .Case("image1d_array_t", "image")
118 .Case("image1d_buffer_t", "image")
119 .Case("image2d_t", "image")
120 .Case("image2d_array_t", "image")
121 .Case("image2d_array_depth_t", "image")
122 .Case("image2d_array_msaa_t", "image")
123 .Case("image2d_array_msaa_depth_t", "image")
124 .Case("image2d_depth_t", "image")
125 .Case("image2d_msaa_t", "image")
126 .Case("image2d_msaa_depth_t", "image")
127 .Case("image3d_t", "image")
128 .Case("sampler_t", "sampler")
129 .Case("queue_t", "queue")
130 .Default(isa<PointerType>(Ty)
132 ? "dynamic_shared_pointer"
133 : "global_buffer")
134 : "by_value");
135}
136
138 bool Signed) const {
139 switch (Ty->getTypeID()) {
140 case Type::IntegerTyID: {
141 if (!Signed)
142 return (Twine('u') + getTypeName(Ty, true)).str();
143
144 auto BitWidth = Ty->getIntegerBitWidth();
145 switch (BitWidth) {
146 case 8:
147 return "char";
148 case 16:
149 return "short";
150 case 32:
151 return "int";
152 case 64:
153 return "long";
154 default:
155 return (Twine('i') + Twine(BitWidth)).str();
156 }
157 }
158 case Type::HalfTyID:
159 return "half";
160 case Type::FloatTyID:
161 return "float";
162 case Type::DoubleTyID:
163 return "double";
165 auto VecTy = cast<FixedVectorType>(Ty);
166 auto ElTy = VecTy->getElementType();
167 auto NumElements = VecTy->getNumElements();
168 return (Twine(getTypeName(ElTy, Signed)) + Twine(NumElements)).str();
169 }
170 default:
171 return "unknown";
172 }
173}
174
177 auto Dims = HSAMetadataDoc->getArrayNode();
178 if (Node->getNumOperands() != 3)
179 return Dims;
180
181 for (auto &Op : Node->operands())
182 Dims.push_back(Dims.getDocument()->getNode(
183 uint64_t(mdconst::extract<ConstantInt>(Op)->getZExtValue())));
184 return Dims;
185}
186
188 auto Version = HSAMetadataDoc->getArrayNode();
189 Version.push_back(Version.getDocument()->getNode(VersionMajorV4));
190 Version.push_back(Version.getDocument()->getNode(VersionMinorV4));
191 getRootMetadata("amdhsa.version") = Version;
192}
193
195 const IsaInfo::AMDGPUTargetID &TargetID) {
196 getRootMetadata("amdhsa.target") =
197 HSAMetadataDoc->getNode(TargetID.toString(), /*Copy=*/true);
198}
199
201 auto Node = Mod.getNamedMetadata("llvm.printf.fmts");
202 if (!Node)
203 return;
204
205 auto Printf = HSAMetadataDoc->getArrayNode();
206 for (auto *Op : Node->operands())
207 if (Op->getNumOperands())
208 Printf.push_back(Printf.getDocument()->getNode(
209 cast<MDString>(Op->getOperand(0))->getString(), /*Copy=*/true));
210 getRootMetadata("amdhsa.printf") = Printf;
211}
212
214 msgpack::MapDocNode Kern) {
215 // TODO: What about other languages?
216 auto Node = Func.getParent()->getNamedMetadata("opencl.ocl.version");
217 if (!Node || !Node->getNumOperands())
218 return;
219 auto Op0 = Node->getOperand(0);
220 if (Op0->getNumOperands() <= 1)
221 return;
222
223 Kern[".language"] = Kern.getDocument()->getNode("OpenCL C");
225 LanguageVersion.push_back(Kern.getDocument()->getNode(
226 mdconst::extract<ConstantInt>(Op0->getOperand(0))->getZExtValue()));
227 LanguageVersion.push_back(Kern.getDocument()->getNode(
228 mdconst::extract<ConstantInt>(Op0->getOperand(1))->getZExtValue()));
229 Kern[".language_version"] = LanguageVersion;
230}
231
233 msgpack::MapDocNode Kern) {
234
235 if (auto Node = Func.getMetadata("reqd_work_group_size"))
236 Kern[".reqd_workgroup_size"] = getWorkGroupDimensions(Node);
237 if (auto Node = Func.getMetadata("work_group_size_hint"))
238 Kern[".workgroup_size_hint"] = getWorkGroupDimensions(Node);
239 if (auto Node = Func.getMetadata("vec_type_hint")) {
240 Kern[".vec_type_hint"] = Kern.getDocument()->getNode(
242 cast<ValueAsMetadata>(Node->getOperand(0))->getType(),
243 mdconst::extract<ConstantInt>(Node->getOperand(1))->getZExtValue()),
244 /*Copy=*/true);
245 }
246 if (Func.hasFnAttribute("runtime-handle")) {
247 Kern[".device_enqueue_symbol"] = Kern.getDocument()->getNode(
248 Func.getFnAttribute("runtime-handle").getValueAsString().str(),
249 /*Copy=*/true);
250 }
251 if (Func.hasFnAttribute("device-init"))
252 Kern[".kind"] = Kern.getDocument()->getNode("init");
253 else if (Func.hasFnAttribute("device-fini"))
254 Kern[".kind"] = Kern.getDocument()->getNode("fini");
255}
256
258 msgpack::MapDocNode Kern) {
259 auto &Func = MF.getFunction();
260 unsigned Offset = 0;
261 auto Args = HSAMetadataDoc->getArrayNode();
262 for (auto &Arg : Func.args())
263 emitKernelArg(Arg, Offset, Args);
264
265 emitHiddenKernelArgs(MF, Offset, Args);
266
267 Kern[".args"] = Args;
268}
269
271 unsigned &Offset,
273 auto Func = Arg.getParent();
274 auto ArgNo = Arg.getArgNo();
275 const MDNode *Node;
276
278 Node = Func->getMetadata("kernel_arg_name");
279 if (Node && ArgNo < Node->getNumOperands())
280 Name = cast<MDString>(Node->getOperand(ArgNo))->getString();
281 else if (Arg.hasName())
282 Name = Arg.getName();
283
284 StringRef TypeName;
285 Node = Func->getMetadata("kernel_arg_type");
286 if (Node && ArgNo < Node->getNumOperands())
287 TypeName = cast<MDString>(Node->getOperand(ArgNo))->getString();
288
289 StringRef BaseTypeName;
290 Node = Func->getMetadata("kernel_arg_base_type");
291 if (Node && ArgNo < Node->getNumOperands())
292 BaseTypeName = cast<MDString>(Node->getOperand(ArgNo))->getString();
293
294 StringRef ActAccQual;
295 // Do we really need NoAlias check here?
296 if (Arg.getType()->isPointerTy() && Arg.hasNoAliasAttr()) {
297 if (Arg.onlyReadsMemory())
298 ActAccQual = "read_only";
299 else if (Arg.hasAttribute(Attribute::WriteOnly))
300 ActAccQual = "write_only";
301 }
302
303 StringRef AccQual;
304 Node = Func->getMetadata("kernel_arg_access_qual");
305 if (Node && ArgNo < Node->getNumOperands())
306 AccQual = cast<MDString>(Node->getOperand(ArgNo))->getString();
307
308 StringRef TypeQual;
309 Node = Func->getMetadata("kernel_arg_type_qual");
310 if (Node && ArgNo < Node->getNumOperands())
311 TypeQual = cast<MDString>(Node->getOperand(ArgNo))->getString();
312
313 const DataLayout &DL = Func->getParent()->getDataLayout();
314
315 MaybeAlign PointeeAlign;
316 Type *Ty = Arg.hasByRefAttr() ? Arg.getParamByRefType() : Arg.getType();
317
318 // FIXME: Need to distinguish in memory alignment from pointer alignment.
319 if (auto PtrTy = dyn_cast<PointerType>(Ty)) {
320 if (PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS)
321 PointeeAlign = Arg.getParamAlign().valueOrOne();
322 }
323
324 // There's no distinction between byval aggregates and raw aggregates.
325 Type *ArgTy;
326 Align ArgAlign;
327 std::tie(ArgTy, ArgAlign) = getArgumentTypeAlign(Arg, DL);
328
329 emitKernelArg(DL, ArgTy, ArgAlign,
330 getValueKind(ArgTy, TypeQual, BaseTypeName), Offset, Args,
331 PointeeAlign, Name, TypeName, BaseTypeName, ActAccQual,
332 AccQual, TypeQual);
333}
334
336 const DataLayout &DL, Type *Ty, Align Alignment, StringRef ValueKind,
337 unsigned &Offset, msgpack::ArrayDocNode Args, MaybeAlign PointeeAlign,
338 StringRef Name, StringRef TypeName, StringRef BaseTypeName,
339 StringRef ActAccQual, StringRef AccQual, StringRef TypeQual) {
340 auto Arg = Args.getDocument()->getMapNode();
341
342 if (!Name.empty())
343 Arg[".name"] = Arg.getDocument()->getNode(Name, /*Copy=*/true);
344 if (!TypeName.empty())
345 Arg[".type_name"] = Arg.getDocument()->getNode(TypeName, /*Copy=*/true);
346 auto Size = DL.getTypeAllocSize(Ty);
347 Arg[".size"] = Arg.getDocument()->getNode(Size);
348 Offset = alignTo(Offset, Alignment);
349 Arg[".offset"] = Arg.getDocument()->getNode(Offset);
350 Offset += Size;
351 Arg[".value_kind"] = Arg.getDocument()->getNode(ValueKind, /*Copy=*/true);
352 if (PointeeAlign)
353 Arg[".pointee_align"] = Arg.getDocument()->getNode(PointeeAlign->value());
354
355 if (auto PtrTy = dyn_cast<PointerType>(Ty))
356 if (auto Qualifier = getAddressSpaceQualifier(PtrTy->getAddressSpace()))
357 // Limiting address space to emit only for a certain ValueKind.
358 if (ValueKind == "global_buffer" || ValueKind == "dynamic_shared_pointer")
359 Arg[".address_space"] = Arg.getDocument()->getNode(*Qualifier,
360 /*Copy=*/true);
361
362 if (auto AQ = getAccessQualifier(AccQual))
363 Arg[".access"] = Arg.getDocument()->getNode(*AQ, /*Copy=*/true);
364
365 if (auto AAQ = getAccessQualifier(ActAccQual))
366 Arg[".actual_access"] = Arg.getDocument()->getNode(*AAQ, /*Copy=*/true);
367
368 SmallVector<StringRef, 1> SplitTypeQuals;
369 TypeQual.split(SplitTypeQuals, " ", -1, false);
370 for (StringRef Key : SplitTypeQuals) {
371 if (Key == "const")
372 Arg[".is_const"] = Arg.getDocument()->getNode(true);
373 else if (Key == "restrict")
374 Arg[".is_restrict"] = Arg.getDocument()->getNode(true);
375 else if (Key == "volatile")
376 Arg[".is_volatile"] = Arg.getDocument()->getNode(true);
377 else if (Key == "pipe")
378 Arg[".is_pipe"] = Arg.getDocument()->getNode(true);
379 }
380
381 Args.push_back(Arg);
382}
383
385 const MachineFunction &MF, unsigned &Offset, msgpack::ArrayDocNode Args) {
386 auto &Func = MF.getFunction();
387 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
388
389 unsigned HiddenArgNumBytes = ST.getImplicitArgNumBytes(Func);
390 if (!HiddenArgNumBytes)
391 return;
392
393 const Module *M = Func.getParent();
394 auto &DL = M->getDataLayout();
395 auto Int64Ty = Type::getInt64Ty(Func.getContext());
396
397 Offset = alignTo(Offset, ST.getAlignmentForImplicitArgPtr());
398
399 if (HiddenArgNumBytes >= 8)
400 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_x", Offset,
401 Args);
402 if (HiddenArgNumBytes >= 16)
403 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_y", Offset,
404 Args);
405 if (HiddenArgNumBytes >= 24)
406 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_z", Offset,
407 Args);
408
409 auto Int8PtrTy =
410 PointerType::get(Func.getContext(), AMDGPUAS::GLOBAL_ADDRESS);
411
412 if (HiddenArgNumBytes >= 32) {
413 // We forbid the use of features requiring hostcall when compiling OpenCL
414 // before code object V5, which makes the mutual exclusion between the
415 // "printf buffer" and "hostcall buffer" here sound.
416 if (M->getNamedMetadata("llvm.printf.fmts"))
417 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_printf_buffer", Offset,
418 Args);
419 else if (!Func.hasFnAttribute("amdgpu-no-hostcall-ptr"))
420 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_hostcall_buffer", Offset,
421 Args);
422 else
423 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_none", Offset, Args);
424 }
425
426 // Emit "default queue" and "completion action" arguments if enqueue kernel is
427 // used, otherwise emit dummy "none" arguments.
428 if (HiddenArgNumBytes >= 40) {
429 if (!Func.hasFnAttribute("amdgpu-no-default-queue")) {
430 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_default_queue", Offset,
431 Args);
432 } else {
433 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_none", Offset, Args);
434 }
435 }
436
437 if (HiddenArgNumBytes >= 48) {
438 if (!Func.hasFnAttribute("amdgpu-no-completion-action")) {
439 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_completion_action", Offset,
440 Args);
441 } else {
442 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_none", Offset, Args);
443 }
444 }
445
446 // Emit the pointer argument for multi-grid object.
447 if (HiddenArgNumBytes >= 56) {
448 if (!Func.hasFnAttribute("amdgpu-no-multigrid-sync-arg")) {
449 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_multigrid_sync_arg", Offset,
450 Args);
451 } else {
452 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_none", Offset, Args);
453 }
454 }
455}
456
459 const SIProgramInfo &ProgramInfo,
460 unsigned CodeObjectVersion) const {
461 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
463 const Function &F = MF.getFunction();
464
465 auto Kern = HSAMetadataDoc->getMapNode();
466
467 Align MaxKernArgAlign;
468 Kern[".kernarg_segment_size"] = Kern.getDocument()->getNode(
469 STM.getKernArgSegmentSize(F, MaxKernArgAlign));
470 Kern[".group_segment_fixed_size"] =
471 Kern.getDocument()->getNode(ProgramInfo.LDSSize);
472 Kern[".private_segment_fixed_size"] =
473 Kern.getDocument()->getNode(ProgramInfo.ScratchSize);
474 if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5)
475 Kern[".uses_dynamic_stack"] =
476 Kern.getDocument()->getNode(ProgramInfo.DynamicCallStack);
477
478 if (CodeObjectVersion >= AMDGPU::AMDHSA_COV5 && STM.supportsWGP())
479 Kern[".workgroup_processor_mode"] =
480 Kern.getDocument()->getNode(ProgramInfo.WgpMode);
481
482 // FIXME: The metadata treats the minimum as 16?
483 Kern[".kernarg_segment_align"] =
484 Kern.getDocument()->getNode(std::max(Align(4), MaxKernArgAlign).value());
485 Kern[".wavefront_size"] =
486 Kern.getDocument()->getNode(STM.getWavefrontSize());
487 Kern[".sgpr_count"] = Kern.getDocument()->getNode(ProgramInfo.NumSGPR);
488 Kern[".vgpr_count"] = Kern.getDocument()->getNode(ProgramInfo.NumVGPR);
489
490 // Only add AGPR count to metadata for supported devices
491 if (STM.hasMAIInsts()) {
492 Kern[".agpr_count"] = Kern.getDocument()->getNode(ProgramInfo.NumAccVGPR);
493 }
494
495 Kern[".max_flat_workgroup_size"] =
496 Kern.getDocument()->getNode(MFI.getMaxFlatWorkGroupSize());
497 unsigned NumWGX = MFI.getMaxNumWorkGroupsX();
498 unsigned NumWGY = MFI.getMaxNumWorkGroupsY();
499 unsigned NumWGZ = MFI.getMaxNumWorkGroupsZ();
500 if (NumWGX != 0 && NumWGY != 0 && NumWGZ != 0) {
501 Kern[".max_num_workgroups_x"] = Kern.getDocument()->getNode(NumWGX);
502 Kern[".max_num_workgroups_y"] = Kern.getDocument()->getNode(NumWGY);
503 Kern[".max_num_workgroups_z"] = Kern.getDocument()->getNode(NumWGZ);
504 }
505 Kern[".sgpr_spill_count"] =
506 Kern.getDocument()->getNode(MFI.getNumSpilledSGPRs());
507 Kern[".vgpr_spill_count"] =
508 Kern.getDocument()->getNode(MFI.getNumSpilledVGPRs());
509
510 return Kern;
511}
512
514 return TargetStreamer.EmitHSAMetadata(*HSAMetadataDoc, true);
515}
516
518 const IsaInfo::AMDGPUTargetID &TargetID) {
519 emitVersion();
520 emitTargetID(TargetID);
522 getRootMetadata("amdhsa.kernels") = HSAMetadataDoc->getArrayNode();
523}
524
526 std::string HSAMetadataString;
527 raw_string_ostream StrOS(HSAMetadataString);
528 HSAMetadataDoc->toYAML(StrOS);
529
530 if (DumpHSAMetadata)
531 dump(StrOS.str());
533 verify(StrOS.str());
534}
535
537 const SIProgramInfo &ProgramInfo) {
538 auto &Func = MF.getFunction();
539 if (Func.getCallingConv() != CallingConv::AMDGPU_KERNEL &&
540 Func.getCallingConv() != CallingConv::SPIR_KERNEL)
541 return;
542
543 auto CodeObjectVersion =
544 AMDGPU::getAMDHSACodeObjectVersion(*Func.getParent());
545 auto Kern = getHSAKernelProps(MF, ProgramInfo, CodeObjectVersion);
546
547 auto Kernels =
548 getRootMetadata("amdhsa.kernels").getArray(/*Convert=*/true);
549
550 {
551 Kern[".name"] = Kern.getDocument()->getNode(Func.getName());
552 Kern[".symbol"] = Kern.getDocument()->getNode(
553 (Twine(Func.getName()) + Twine(".kd")).str(), /*Copy=*/true);
554 emitKernelLanguage(Func, Kern);
555 emitKernelAttrs(Func, Kern);
556 emitKernelArgs(MF, Kern);
557 }
558
559 Kernels.push_back(Kern);
560}
561
562//===----------------------------------------------------------------------===//
563// HSAMetadataStreamerV5
564//===----------------------------------------------------------------------===//
565
567 auto Version = HSAMetadataDoc->getArrayNode();
568 Version.push_back(Version.getDocument()->getNode(VersionMajorV5));
569 Version.push_back(Version.getDocument()->getNode(VersionMinorV5));
570 getRootMetadata("amdhsa.version") = Version;
571}
572
574 const MachineFunction &MF, unsigned &Offset, msgpack::ArrayDocNode Args) {
575 auto &Func = MF.getFunction();
576 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
577
578 // No implicit kernel argument is used.
579 if (ST.getImplicitArgNumBytes(Func) == 0)
580 return;
581
582 const Module *M = Func.getParent();
583 auto &DL = M->getDataLayout();
585
586 auto Int64Ty = Type::getInt64Ty(Func.getContext());
587 auto Int32Ty = Type::getInt32Ty(Func.getContext());
588 auto Int16Ty = Type::getInt16Ty(Func.getContext());
589
590 Offset = alignTo(Offset, ST.getAlignmentForImplicitArgPtr());
591 emitKernelArg(DL, Int32Ty, Align(4), "hidden_block_count_x", Offset, Args);
592 emitKernelArg(DL, Int32Ty, Align(4), "hidden_block_count_y", Offset, Args);
593 emitKernelArg(DL, Int32Ty, Align(4), "hidden_block_count_z", Offset, Args);
594
595 emitKernelArg(DL, Int16Ty, Align(2), "hidden_group_size_x", Offset, Args);
596 emitKernelArg(DL, Int16Ty, Align(2), "hidden_group_size_y", Offset, Args);
597 emitKernelArg(DL, Int16Ty, Align(2), "hidden_group_size_z", Offset, Args);
598
599 emitKernelArg(DL, Int16Ty, Align(2), "hidden_remainder_x", Offset, Args);
600 emitKernelArg(DL, Int16Ty, Align(2), "hidden_remainder_y", Offset, Args);
601 emitKernelArg(DL, Int16Ty, Align(2), "hidden_remainder_z", Offset, Args);
602
603 // Reserved for hidden_tool_correlation_id.
604 Offset += 8;
605
606 Offset += 8; // Reserved.
607
608 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_x", Offset, Args);
609 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_y", Offset, Args);
610 emitKernelArg(DL, Int64Ty, Align(8), "hidden_global_offset_z", Offset, Args);
611
612 emitKernelArg(DL, Int16Ty, Align(2), "hidden_grid_dims", Offset, Args);
613
614 Offset += 6; // Reserved.
615 auto Int8PtrTy =
616 PointerType::get(Func.getContext(), AMDGPUAS::GLOBAL_ADDRESS);
617
618 if (M->getNamedMetadata("llvm.printf.fmts")) {
619 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_printf_buffer", Offset,
620 Args);
621 } else {
622 Offset += 8; // Skipped.
623 }
624
625 if (!Func.hasFnAttribute("amdgpu-no-hostcall-ptr")) {
626 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_hostcall_buffer", Offset,
627 Args);
628 } else {
629 Offset += 8; // Skipped.
630 }
631
632 if (!Func.hasFnAttribute("amdgpu-no-multigrid-sync-arg")) {
633 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_multigrid_sync_arg", Offset,
634 Args);
635 } else {
636 Offset += 8; // Skipped.
637 }
638
639 if (!Func.hasFnAttribute("amdgpu-no-heap-ptr"))
640 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_heap_v1", Offset, Args);
641 else
642 Offset += 8; // Skipped.
643
644 if (!Func.hasFnAttribute("amdgpu-no-default-queue")) {
645 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_default_queue", Offset,
646 Args);
647 } else {
648 Offset += 8; // Skipped.
649 }
650
651 if (!Func.hasFnAttribute("amdgpu-no-completion-action")) {
652 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_completion_action", Offset,
653 Args);
654 } else {
655 Offset += 8; // Skipped.
656 }
657
658 // Emit argument for hidden dynamic lds size
659 if (MFI.isDynamicLDSUsed()) {
660 emitKernelArg(DL, Int32Ty, Align(4), "hidden_dynamic_lds_size", Offset,
661 Args);
662 } else {
663 Offset += 4; // skipped
664 }
665
666 Offset += 68; // Reserved.
667
668 // hidden_private_base and hidden_shared_base are only when the subtarget has
669 // ApertureRegs.
670 if (!ST.hasApertureRegs()) {
671 emitKernelArg(DL, Int32Ty, Align(4), "hidden_private_base", Offset, Args);
672 emitKernelArg(DL, Int32Ty, Align(4), "hidden_shared_base", Offset, Args);
673 } else {
674 Offset += 8; // Skipped.
675 }
676
677 if (MFI.getUserSGPRInfo().hasQueuePtr())
678 emitKernelArg(DL, Int8PtrTy, Align(8), "hidden_queue_ptr", Offset, Args);
679}
680
682 msgpack::MapDocNode Kern) {
684
685 if (Func.getFnAttribute("uniform-work-group-size").getValueAsBool())
686 Kern[".uniform_work_group_size"] = Kern.getDocument()->getNode(1);
687}
688
689//===----------------------------------------------------------------------===//
690// HSAMetadataStreamerV6
691//===----------------------------------------------------------------------===//
692
694 auto Version = HSAMetadataDoc->getArrayNode();
695 Version.push_back(Version.getDocument()->getNode(VersionMajorV6));
696 Version.push_back(Version.getDocument()->getNode(VersionMinorV6));
697 getRootMetadata("amdhsa.version") = Version;
698}
699
700} // end namespace HSAMD
701} // end namespace AMDGPU
702} // end namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static std::pair< Type *, Align > getArgumentTypeAlign(const Argument &Arg, const DataLayout &DL)
AMDGPU HSA Metadata Streamer.
Given that RA is a live value
std::string Name
uint64_t Size
AMD GCN specific subclass of TargetSubtarget.
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
IntegerType * Int32Ty
ppc ctr loops verify
Defines struct to track resource usage and hardware flags for kernels and entry functions.
unsigned getKernArgSegmentSize(const Function &F, Align &MaxAlign) const
unsigned getWavefrontSize() const
virtual bool EmitHSAMetadata(msgpack::Document &HSAMetadata, bool Strict)
Emit HSA Metadata.
std::optional< StringRef > getAddressSpaceQualifier(unsigned AddressSpace) const
msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const
std::optional< StringRef > getAccessQualifier(StringRef AccQual) const
void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern)
msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF, const SIProgramInfo &ProgramInfo, unsigned CodeObjectVersion) const
void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override
std::unique_ptr< msgpack::Document > HSAMetadataDoc
bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override
void emitKernelArgs(const MachineFunction &MF, msgpack::MapDocNode Kern)
std::string getTypeName(Type *Ty, bool Signed) const
void dump(StringRef HSAMetadataString) const
void begin(const Module &Mod, const IsaInfo::AMDGPUTargetID &TargetID) override
void emitKernelArg(const Argument &Arg, unsigned &Offset, msgpack::ArrayDocNode Args)
void emitTargetID(const IsaInfo::AMDGPUTargetID &TargetID)
void verify(StringRef HSAMetadataString) const
StringRef getValueKind(Type *Ty, StringRef TypeQual, StringRef BaseTypeName) const
void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, msgpack::ArrayDocNode Args) override
void emitKernel(const MachineFunction &MF, const SIProgramInfo &ProgramInfo) override
void emitHiddenKernelArgs(const MachineFunction &MF, unsigned &Offset, msgpack::ArrayDocNode Args) override
void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern) override
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
Type * getParamByRefType() const
If this is a byref argument, return its type.
Definition: Function.cpp:240
bool hasNoAliasAttr() const
Return true if this argument has the noalias attribute.
Definition: Function.cpp:278
bool hasByRefAttr() const
Return true if this argument has the byref attribute.
Definition: Function.cpp:144
bool onlyReadsMemory() const
Return true if this argument has the readonly or readnone attribute.
Definition: Function.cpp:314
bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition: Function.cpp:344
const Function * getParent() const
Definition: Argument.h:43
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition: Argument.h:49
MaybeAlign getParamAlign() const
If this is a byval or inalloca argument, return its alignment.
Definition: Function.cpp:221
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool hasMAIInsts() const
Definition: GCNSubtarget.h:804
bool supportsWGP() const
Definition: GCNSubtarget.h:341
Metadata node.
Definition: Metadata.h:1067
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
GCNUserSGPRUsageInfo & getUserSGPRInfo()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:696
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:420
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:137
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
A DocNode that is an array.
ArrayDocNode & getArray(bool Convert=false)
Get an ArrayDocNode for an array node.
Document * getDocument() const
Simple in-memory representation of a document of msgpack objects with ability to find and create arra...
DocNode getNode()
Create a nil node associated with this Document.
ArrayDocNode getArrayNode()
Create an empty Array node associated with this Document.
void toYAML(raw_ostream &OS)
Convert MsgPack Document to YAML text.
bool fromYAML(StringRef S)
Read YAML text into the MsgPack document. Returns false on failure.
A DocNode that is a map.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:678
unsigned LanguageVersion(SourceLanguage L)
Definition: Dwarf.cpp:380
@ REGION_ADDRESS
Address space for region memory. (GDS)
@ LOCAL_ADDRESS
Address space for local memory.
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
@ FLAT_ADDRESS
Address space for flat memory.
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
@ PRIVATE_ADDRESS
Address space for private memory.
constexpr uint32_t VersionMajorV5
HSA metadata major version for code object V5.
constexpr uint32_t VersionMinorV4
HSA metadata minor version for code object V4.
ValueKind
Value kinds.
constexpr uint32_t VersionMinorV5
HSA metadata minor version for code object V5.
constexpr uint32_t VersionMinorV6
HSA metadata minor version for code object V6.
constexpr uint32_t VersionMajorV6
HSA metadata major version for code object V6.
constexpr uint32_t VersionMajorV4
HSA metadata major version for code object V4.
unsigned getAMDHSACodeObjectVersion(const Module &M)
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:200
@ SPIR_KERNEL
Used for SPIR kernel functions.
Definition: CallingConv.h:144
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
static cl::opt< bool > VerifyHSAMetadata("amdgpu-verify-hsa-metadata", cl::desc("Verify AMDGPU HSA Metadata"))
AddressSpace
Definition: NVPTXBaseInfo.h:21
static cl::opt< bool > DumpHSAMetadata("amdgpu-dump-hsa-metadata", cl::desc("Dump AMDGPU HSA Metadata"))
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition: TypeName.h:27
@ Mod
The access may modify the value stored in memory.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
Track resource usage for kernels / entry functions.
Definition: SIProgramInfo.h:27