1 //===--- AMDGPUHSAMetadataStreamer.h ----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// AMDGPU HSA Metadata Streamer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
16 #define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
19 #include "AMDKernelCodeT.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/MsgPackTypes.h"
22 #include "llvm/Support/AMDGPUMetadata.h"
26 class AMDGPUTargetStreamer
;
38 class MetadataStreamer
{
40 virtual ~MetadataStreamer(){};
42 virtual bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) = 0;
44 virtual void begin(const Module
&Mod
) = 0;
46 virtual void end() = 0;
48 virtual void emitKernel(const MachineFunction
&MF
,
49 const SIProgramInfo
&ProgramInfo
) = 0;
52 class MetadataStreamerV3 final
: public MetadataStreamer
{
54 std::shared_ptr
<msgpack::Node
> HSAMetadataRoot
=
55 std::make_shared
<msgpack::MapNode
>();
57 void dump(StringRef HSAMetadataString
) const;
59 void verify(StringRef HSAMetadataString
) const;
61 Optional
<StringRef
> getAccessQualifier(StringRef AccQual
) const;
63 Optional
<StringRef
> getAddressSpaceQualifier(unsigned AddressSpace
) const;
65 StringRef
getValueKind(Type
*Ty
, StringRef TypeQual
,
66 StringRef BaseTypeName
) const;
68 StringRef
getValueType(Type
*Ty
, StringRef TypeName
) const;
70 std::string
getTypeName(Type
*Ty
, bool Signed
) const;
72 std::shared_ptr
<msgpack::ArrayNode
>
73 getWorkGroupDimensions(MDNode
*Node
) const;
75 std::shared_ptr
<msgpack::MapNode
>
76 getHSAKernelProps(const MachineFunction
&MF
,
77 const SIProgramInfo
&ProgramInfo
) const;
81 void emitPrintf(const Module
&Mod
);
83 void emitKernelLanguage(const Function
&Func
, msgpack::MapNode
&Kern
);
85 void emitKernelAttrs(const Function
&Func
, msgpack::MapNode
&Kern
);
87 void emitKernelArgs(const Function
&Func
, msgpack::MapNode
&Kern
);
89 void emitKernelArg(const Argument
&Arg
, unsigned &Offset
,
90 msgpack::ArrayNode
&Args
);
92 void emitKernelArg(const DataLayout
&DL
, Type
*Ty
, StringRef ValueKind
,
93 unsigned &Offset
, msgpack::ArrayNode
&Args
,
94 unsigned PointeeAlign
= 0, StringRef Name
= "",
95 StringRef TypeName
= "", StringRef BaseTypeName
= "",
96 StringRef AccQual
= "", StringRef TypeQual
= "");
98 void emitHiddenKernelArgs(const Function
&Func
, unsigned &Offset
,
99 msgpack::ArrayNode
&Args
);
101 std::shared_ptr
<msgpack::Node
> &getRootMetadata(StringRef Key
) {
102 return (*cast
<msgpack::MapNode
>(HSAMetadataRoot
.get()))[Key
];
105 std::shared_ptr
<msgpack::Node
> &getHSAMetadataRoot() {
106 return HSAMetadataRoot
;
110 MetadataStreamerV3() = default;
111 ~MetadataStreamerV3() = default;
113 bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) override
;
115 void begin(const Module
&Mod
) override
;
119 void emitKernel(const MachineFunction
&MF
,
120 const SIProgramInfo
&ProgramInfo
) override
;
123 class MetadataStreamerV2 final
: public MetadataStreamer
{
125 Metadata HSAMetadata
;
127 void dump(StringRef HSAMetadataString
) const;
129 void verify(StringRef HSAMetadataString
) const;
131 AccessQualifier
getAccessQualifier(StringRef AccQual
) const;
133 AddressSpaceQualifier
getAddressSpaceQualifier(unsigned AddressSpace
) const;
135 ValueKind
getValueKind(Type
*Ty
, StringRef TypeQual
,
136 StringRef BaseTypeName
) const;
138 ValueType
getValueType(Type
*Ty
, StringRef TypeName
) const;
140 std::string
getTypeName(Type
*Ty
, bool Signed
) const;
142 std::vector
<uint32_t> getWorkGroupDimensions(MDNode
*Node
) const;
144 Kernel::CodeProps::Metadata
getHSACodeProps(
145 const MachineFunction
&MF
,
146 const SIProgramInfo
&ProgramInfo
) const;
147 Kernel::DebugProps::Metadata
getHSADebugProps(
148 const MachineFunction
&MF
,
149 const SIProgramInfo
&ProgramInfo
) const;
153 void emitPrintf(const Module
&Mod
);
155 void emitKernelLanguage(const Function
&Func
);
157 void emitKernelAttrs(const Function
&Func
);
159 void emitKernelArgs(const Function
&Func
);
161 void emitKernelArg(const Argument
&Arg
);
163 void emitKernelArg(const DataLayout
&DL
, Type
*Ty
, ValueKind ValueKind
,
164 unsigned PointeeAlign
= 0,
165 StringRef Name
= "", StringRef TypeName
= "",
166 StringRef BaseTypeName
= "", StringRef AccQual
= "",
167 StringRef TypeQual
= "");
169 void emitHiddenKernelArgs(const Function
&Func
);
171 const Metadata
&getHSAMetadata() const {
176 MetadataStreamerV2() = default;
177 ~MetadataStreamerV2() = default;
179 bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) override
;
181 void begin(const Module
&Mod
) override
;
185 void emitKernel(const MachineFunction
&MF
,
186 const SIProgramInfo
&ProgramInfo
) override
;
189 } // end namespace HSAMD
190 } // end namespace AMDGPU
191 } // end namespace llvm
193 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H