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
18 #include "Utils/AMDGPUBaseInfo.h"
19 #include "llvm/BinaryFormat/MsgPackDocument.h"
20 #include "llvm/Support/AMDGPUMetadata.h"
21 #include "llvm/Support/Alignment.h"
25 class AMDGPUTargetStreamer
;
29 class MachineFunction
;
38 class MetadataStreamer
{
40 virtual ~MetadataStreamer(){};
42 virtual bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) = 0;
44 virtual void begin(const Module
&Mod
,
45 const IsaInfo::AMDGPUTargetID
&TargetID
) = 0;
47 virtual void end() = 0;
49 virtual void emitKernel(const MachineFunction
&MF
,
50 const SIProgramInfo
&ProgramInfo
) = 0;
53 // TODO: Rename MetadataStreamerV3 -> MetadataStreamerMsgPackV3.
54 class MetadataStreamerV3
: public MetadataStreamer
{
56 std::unique_ptr
<msgpack::Document
> HSAMetadataDoc
=
57 std::make_unique
<msgpack::Document
>();
59 void dump(StringRef HSAMetadataString
) const;
61 void verify(StringRef HSAMetadataString
) const;
63 Optional
<StringRef
> getAccessQualifier(StringRef AccQual
) const;
65 Optional
<StringRef
> getAddressSpaceQualifier(unsigned AddressSpace
) const;
67 StringRef
getValueKind(Type
*Ty
, StringRef TypeQual
,
68 StringRef BaseTypeName
) const;
70 std::string
getTypeName(Type
*Ty
, bool Signed
) const;
72 msgpack::ArrayDocNode
getWorkGroupDimensions(MDNode
*Node
) const;
74 msgpack::MapDocNode
getHSAKernelProps(const MachineFunction
&MF
,
75 const SIProgramInfo
&ProgramInfo
) const;
79 void emitPrintf(const Module
&Mod
);
81 void emitKernelLanguage(const Function
&Func
, msgpack::MapDocNode Kern
);
83 void emitKernelAttrs(const Function
&Func
, msgpack::MapDocNode Kern
);
85 void emitKernelArgs(const Function
&Func
, msgpack::MapDocNode Kern
);
87 void emitKernelArg(const Argument
&Arg
, unsigned &Offset
,
88 msgpack::ArrayDocNode Args
);
90 void emitKernelArg(const DataLayout
&DL
, Type
*Ty
, Align Alignment
,
91 StringRef ValueKind
, unsigned &Offset
,
92 msgpack::ArrayDocNode Args
, MaybeAlign PointeeAlign
= None
,
93 StringRef Name
= "", StringRef TypeName
= "",
94 StringRef BaseTypeName
= "", StringRef AccQual
= "",
95 StringRef TypeQual
= "");
97 void emitHiddenKernelArgs(const Function
&Func
, unsigned &Offset
,
98 msgpack::ArrayDocNode Args
);
100 msgpack::DocNode
&getRootMetadata(StringRef Key
) {
101 return HSAMetadataDoc
->getRoot().getMap(/*Convert=*/true)[Key
];
104 msgpack::DocNode
&getHSAMetadataRoot() {
105 return HSAMetadataDoc
->getRoot();
109 MetadataStreamerV3() = default;
110 ~MetadataStreamerV3() = default;
112 bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) override
;
114 void begin(const Module
&Mod
,
115 const IsaInfo::AMDGPUTargetID
&TargetID
) override
;
119 void emitKernel(const MachineFunction
&MF
,
120 const SIProgramInfo
&ProgramInfo
) override
;
123 // TODO: Rename MetadataStreamerV4 -> MetadataStreamerMsgPackV4.
124 class MetadataStreamerV4 final
: public MetadataStreamerV3
{
127 void emitTargetID(const IsaInfo::AMDGPUTargetID
&TargetID
);
130 MetadataStreamerV4() = default;
131 ~MetadataStreamerV4() = default;
133 void begin(const Module
&Mod
,
134 const IsaInfo::AMDGPUTargetID
&TargetID
) override
;
137 // TODO: Rename MetadataStreamerV2 -> MetadataStreamerYamlV2.
138 class MetadataStreamerV2 final
: public MetadataStreamer
{
140 Metadata HSAMetadata
;
142 void dump(StringRef HSAMetadataString
) const;
144 void verify(StringRef HSAMetadataString
) const;
146 AccessQualifier
getAccessQualifier(StringRef AccQual
) const;
148 AddressSpaceQualifier
getAddressSpaceQualifier(unsigned AddressSpace
) const;
150 ValueKind
getValueKind(Type
*Ty
, StringRef TypeQual
,
151 StringRef BaseTypeName
) const;
153 std::string
getTypeName(Type
*Ty
, bool Signed
) const;
155 std::vector
<uint32_t> getWorkGroupDimensions(MDNode
*Node
) const;
157 Kernel::CodeProps::Metadata
getHSACodeProps(
158 const MachineFunction
&MF
,
159 const SIProgramInfo
&ProgramInfo
) const;
160 Kernel::DebugProps::Metadata
getHSADebugProps(
161 const MachineFunction
&MF
,
162 const SIProgramInfo
&ProgramInfo
) const;
166 void emitPrintf(const Module
&Mod
);
168 void emitKernelLanguage(const Function
&Func
);
170 void emitKernelAttrs(const Function
&Func
);
172 void emitKernelArgs(const Function
&Func
);
174 void emitKernelArg(const Argument
&Arg
);
176 void emitKernelArg(const DataLayout
&DL
, Type
*Ty
, Align Alignment
,
177 ValueKind ValueKind
, MaybeAlign PointeeAlign
= None
,
178 StringRef Name
= "", StringRef TypeName
= "",
179 StringRef BaseTypeName
= "", StringRef AccQual
= "",
180 StringRef TypeQual
= "");
182 void emitHiddenKernelArgs(const Function
&Func
);
184 const Metadata
&getHSAMetadata() const {
189 MetadataStreamerV2() = default;
190 ~MetadataStreamerV2() = default;
192 bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) override
;
194 void begin(const Module
&Mod
,
195 const IsaInfo::AMDGPUTargetID
&TargetID
) override
;
199 void emitKernel(const MachineFunction
&MF
,
200 const SIProgramInfo
&ProgramInfo
) override
;
203 } // end namespace HSAMD
204 } // end namespace AMDGPU
205 } // end namespace llvm
207 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H