27 cl::desc(
"Dump AMDGPU Code Object Metadata"));
30 cl::desc(
"Verify AMDGPU Code Object Metadata"));
33 namespace CodeObject {
35 void MetadataStreamer::dump(StringRef YamlString)
const {
36 errs() <<
"AMDGPU Code Object Metadata:\n" << YamlString <<
'\n';
39 void MetadataStreamer::verify(StringRef YamlString)
const {
40 errs() <<
"AMDGPU Code Object Metadata Parser Test: ";
42 CodeObject::Metadata FromYamlString;
48 std::string ToYamlString;
54 errs() << (YamlString == ToYamlString ?
"PASS" :
"FAIL") <<
'\n';
55 if (YamlString != ToYamlString) {
56 errs() <<
"Original input: " << YamlString <<
'\n'
57 <<
"Produced output: " << ToYamlString <<
'\n';
65 return StringSwitch<AccessQualifier>(
AccQual)
90 ValueKind MetadataStreamer::getValueKind(
Type *Ty, StringRef TypeQual,
91 StringRef BaseTypeName)
const {
95 return StringSwitch<ValueKind>(BaseTypeName)
110 .Default(isa<PointerType>(Ty) ?
111 (Ty->getPointerAddressSpace() ==
119 switch (Ty->getTypeID()) {
121 auto Signed = !TypeName.startswith(
"u");
122 switch (Ty->getIntegerBitWidth()) {
142 return getValueType(Ty->getPointerElementType(),
TypeName);
144 return getValueType(Ty->getVectorElementType(),
TypeName);
150 std::string MetadataStreamer::getTypeName(
Type *Ty,
bool Signed)
const {
151 switch (Ty->getTypeID()) {
154 return (Twine(
'u') + getTypeName(Ty,
true)).str();
156 auto BitWidth = Ty->getIntegerBitWidth();
167 return (Twine(
'i') + Twine(BitWidth)).str();
177 auto VecTy = cast<VectorType>(Ty);
178 auto ElTy = VecTy->getElementType();
179 auto NumElements = VecTy->getVectorNumElements();
180 return (Twine(getTypeName(ElTy, Signed)) + Twine(NumElements)).str();
187 std::vector<uint32_t> MetadataStreamer::getWorkGroupDimensions(
188 MDNode *Node)
const {
189 std::vector<uint32_t> Dims;
190 if (Node->getNumOperands() != 3)
193 for (
auto &
Op : Node->operands())
194 Dims.push_back(mdconst::extract<ConstantInt>(
Op)->getZExtValue());
198 void MetadataStreamer::emitVersion() {
205 void MetadataStreamer::emitPrintf(
const Module &Mod) {
208 auto Node = Mod.getNamedMetadata(
"llvm.printf.fmts");
212 for (
auto Op : Node->operands())
213 if (
Op->getNumOperands())
214 Printf.push_back(cast<MDString>(
Op->getOperand(0))->getString());
217 void MetadataStreamer::emitKernelLanguage(
const Function &Func) {
221 auto Node = Func.getParent()->getNamedMetadata(
"opencl.ocl.version");
222 if (!Node || !Node->getNumOperands())
224 auto Op0 = Node->getOperand(0);
225 if (Op0->getNumOperands() <= 1)
228 Kernel.mLanguage =
"OpenCL C";
229 Kernel.mLanguageVersion.push_back(
230 mdconst::extract<ConstantInt>(Op0->getOperand(0))->getZExtValue());
231 Kernel.mLanguageVersion.push_back(
232 mdconst::extract<ConstantInt>(Op0->getOperand(1))->getZExtValue());
235 void MetadataStreamer::emitKernelAttrs(
const Function &Func) {
238 if (
auto Node = Func.getMetadata(
"reqd_work_group_size"))
239 Attrs.mReqdWorkGroupSize = getWorkGroupDimensions(Node);
240 if (
auto Node = Func.getMetadata(
"work_group_size_hint"))
241 Attrs.mWorkGroupSizeHint = getWorkGroupDimensions(Node);
242 if (
auto Node = Func.getMetadata(
"vec_type_hint")) {
243 Attrs.mVecTypeHint = getTypeName(
244 cast<ValueAsMetadata>(Node->getOperand(0))->
getType(),
245 mdconst::extract<ConstantInt>(Node->getOperand(1))->getZExtValue());
249 void MetadataStreamer::emitKernelArgs(
const Function &Func) {
250 for (
auto &Arg : Func.args())
254 if (!Func.getParent()->getNamedMetadata(
"opencl.ocl.version"))
257 auto &
DL = Func.getParent()->getDataLayout();
264 if (!Func.getParent()->getNamedMetadata(
"llvm.printf.fmts"))
272 void MetadataStreamer::emitKernelArg(
const Argument &Arg) {
273 auto Func = Arg.getParent();
274 auto ArgNo = Arg.getArgNo();
278 Node = Func->getMetadata(
"kernel_arg_type_qual");
279 if (Node && ArgNo < Node->getNumOperands())
280 TypeQual = cast<MDString>(Node->getOperand(ArgNo))->getString();
282 StringRef BaseTypeName;
283 Node = Func->getMetadata(
"kernel_arg_base_type");
284 if (Node && ArgNo < Node->getNumOperands())
285 BaseTypeName = cast<MDString>(Node->getOperand(ArgNo))->getString();
288 if (Arg.getType()->isPointerTy() && Arg.onlyReadsMemory() &&
289 Arg.hasNoAliasAttr()) {
290 AccQual =
"read_only";
292 Node = Func->getMetadata(
"kernel_arg_access_qual");
293 if (Node && ArgNo < Node->getNumOperands())
294 AccQual = cast<MDString>(Node->getOperand(ArgNo))->getString();
298 Node = Func->getMetadata(
"kernel_arg_name");
299 if (Node && ArgNo < Node->getNumOperands())
300 Name = cast<MDString>(Node->getOperand(ArgNo))->getString();
303 Node = Func->getMetadata(
"kernel_arg_type");
304 if (Node && ArgNo < Node->getNumOperands())
305 TypeName = cast<MDString>(Node->getOperand(ArgNo))->getString();
307 emitKernelArg(Func->getParent()->getDataLayout(), Arg.getType(),
308 getValueKind(Arg.getType(), TypeQual, BaseTypeName), TypeQual,
309 BaseTypeName, AccQual, Name, TypeName);
312 void MetadataStreamer::emitKernelArg(
const DataLayout &DL,
Type *Ty,
314 StringRef BaseTypeName, StringRef AccQual,
315 StringRef Name, StringRef TypeName) {
316 CodeObjectMetadata.
mKernels.back().mArgs.push_back(Kernel::Arg::Metadata());
317 auto &Arg = CodeObjectMetadata.
mKernels.back().mArgs.back();
319 Arg.mSize = DL.getTypeAllocSize(Ty);
320 Arg.mAlign = DL.getABITypeAlignment(Ty);
322 Arg.mValueType = getValueType(Ty, BaseTypeName);
324 if (
auto PtrTy = dyn_cast<PointerType>(Ty)) {
325 auto ElTy = PtrTy->getElementType();
326 if (PtrTy->getAddressSpace() == AMDGPUASI.
LOCAL_ADDRESS && ElTy->isSized())
327 Arg.mPointeeAlign = DL.getABITypeAlignment(ElTy);
330 Arg.mAccQual = getAccessQualifier(AccQual);
332 if (
auto PtrTy = dyn_cast<PointerType>(Ty))
333 Arg.mAddrSpaceQual = getAddressSpaceQualifer(PtrTy->getAddressSpace());
335 SmallVector<StringRef, 1> SplitTypeQuals;
336 TypeQual.split(SplitTypeQuals,
" ", -1,
false);
337 for (StringRef Key : SplitTypeQuals) {
338 auto P = StringSwitch<bool*>(Key)
339 .Case(
"const", &Arg.mIsConst)
340 .Case(
"pipe", &Arg.mIsPipe)
341 .Case(
"restrict", &Arg.mIsRestrict)
342 .Case(
"volatile", &Arg.mIsVolatile)
352 void MetadataStreamer::emitKernelCodeProps(
369 void MetadataStreamer::emitKernelDebugProps(
384 DebugProps.mWavefrontPrivateSegmentOffsetSGPR =
403 emitKernelLanguage(Func);
404 emitKernelAttrs(Func);
405 emitKernelArgs(Func);
406 emitKernelCodeProps(KernelCode);
407 emitKernelDebugProps(KernelCode);
411 std::string YamlString;
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static cl::opt< bool > VerifyCodeObjectMetadata("amdgpu-verify-comd", cl::desc("Verify AMDGPU Code Object Metadata"))
A Module instance is used to store all the information related to an LLVM module. ...
2: 32-bit floating point type
constexpr char Printf[]
Key for CodeObject::Metadata::mPrintf.
AMDGPUAS getAMDGPUAS(const Module &M)
1: 16-bit floating point type
static IntegerType * getInt64Ty(LLVMContext &C)
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
static const unsigned LOCAL_ADDRESS
Address space for local memory.
StringRef getName() const
Return a constant reference to the value's name.
uint32_t code_properties
Code properties.
AMD Kernel Code Object (amd_kernel_code_t).
uint8_t kernarg_segment_alignment
The maximum byte alignment of variables used by the kernel in the specified memory segment...
AccessQualifier
Access qualifiers.
uint8_t group_segment_alignment
11: Arbitrary bit width integers
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
uint16_t wavefront_sgpr_count
Number of scalar registers used by a wavefront.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint8_t private_segment_alignment
unsigned REGION_ADDRESS
Address space for region memory.
static wasm::ValType getType(const TargetRegisterClass *RC)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
constexpr char CodeProps[]
Key for Kernel::Metadata::mCodeProps.
16: SIMD 'packed' format, or other vector type
uint64_t kernarg_segment_byte_size
The size in bytes of the kernarg segment that holds the values of the arguments to the kernel...
Module.h This file contains the declarations for the Module class.
uint16_t debug_wavefront_private_segment_offset_sgpr
If is_debug_supported is 0 then must be 0.
uint16_t workitem_vgpr_count
Number of vector registers used by each work-item.
uint8_t wavefront_size
Wavefront size expressed as a power of two.
uint32_t workgroup_group_segment_byte_size
The amount of group segment memory required by a work-group in bytes.
static cl::opt< bool > DumpCodeObjectMetadata("amdgpu-dump-comd", cl::desc("Dump AMDGPU Code Object Metadata"))
llvm_unreachable("Unknown RegBank!")
constexpr char AccQual[]
Key for Kernel::Arg::Metadata::mAccQual.
constexpr uint32_t MetadataVersionMinor
Code object metadata minor version.
uint32_t workitem_private_segment_byte_size
The amount of memory required for the combined private, spill and arg segments for a work-item in byt...
unsigned FLAT_ADDRESS
Address space for flat memory.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
static const unsigned CONSTANT_ADDRESS
Address space for constant memory (VTX2)
static const unsigned GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
3: 64-bit floating point type
AddressSpaceQualifier
Address space qualifiers.
constexpr uint32_t MetadataVersionMajor
Code object metadata major version.
constexpr char DebugProps[]
Key for Kernel::Metadata::mDebugProps.
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
uint16_t debug_private_segment_buffer_sgpr
If is_debug_supported is 0 then must be 0.
uint16_t reserved_vgpr_count
The number of consecutive VGPRs reserved by the client.
Calling convention for AMDGPU code object kernels.
unsigned PRIVATE_ADDRESS
Address space for private memory.
uint16_t reserved_vgpr_first
If reserved_vgpr_count is 0 then must be 0.