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/AMDGPUDelayedMCExpr.h"
19 #include "llvm/BinaryFormat/MsgPackDocument.h"
20 #include "llvm/Support/AMDGPUMetadata.h"
21 #include "llvm/Support/Alignment.h"
22 #include "llvm/Support/Compiler.h"
26 class AMDGPUTargetStreamer
;
30 class MachineFunction
;
44 class MetadataStreamer
{
46 virtual ~MetadataStreamer() = default;
48 virtual bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) = 0;
50 virtual void begin(const Module
&Mod
,
51 const IsaInfo::AMDGPUTargetID
&TargetID
) = 0;
53 virtual void end() = 0;
55 virtual void emitKernel(const MachineFunction
&MF
,
56 const SIProgramInfo
&ProgramInfo
) = 0;
59 virtual void emitVersion() = 0;
60 virtual void emitHiddenKernelArgs(const MachineFunction
&MF
, unsigned &Offset
,
61 msgpack::ArrayDocNode Args
) = 0;
62 virtual void emitKernelAttrs(const Function
&Func
,
63 msgpack::MapDocNode Kern
) = 0;
66 class LLVM_EXTERNAL_VISIBILITY MetadataStreamerMsgPackV4
67 : public MetadataStreamer
{
69 std::unique_ptr
<DelayedMCExprs
> DelayedExprs
=
70 std::make_unique
<DelayedMCExprs
>();
72 std::unique_ptr
<msgpack::Document
> HSAMetadataDoc
=
73 std::make_unique
<msgpack::Document
>();
75 void dump(StringRef HSAMetadataString
) const;
77 void verify(StringRef HSAMetadataString
) const;
79 std::optional
<StringRef
> getAccessQualifier(StringRef AccQual
) const;
81 std::optional
<StringRef
>
82 getAddressSpaceQualifier(unsigned AddressSpace
) const;
84 StringRef
getValueKind(Type
*Ty
, StringRef TypeQual
,
85 StringRef BaseTypeName
) const;
87 std::string
getTypeName(Type
*Ty
, bool Signed
) const;
89 msgpack::ArrayDocNode
getWorkGroupDimensions(MDNode
*Node
) const;
91 msgpack::MapDocNode
getHSAKernelProps(const MachineFunction
&MF
,
92 const SIProgramInfo
&ProgramInfo
,
93 unsigned CodeObjectVersion
) const;
95 void emitVersion() override
;
97 void emitTargetID(const IsaInfo::AMDGPUTargetID
&TargetID
);
99 void emitPrintf(const Module
&Mod
);
101 void emitKernelLanguage(const Function
&Func
, msgpack::MapDocNode Kern
);
103 void emitKernelAttrs(const Function
&Func
, msgpack::MapDocNode Kern
) override
;
105 void emitKernelArgs(const MachineFunction
&MF
, msgpack::MapDocNode Kern
);
107 void emitKernelArg(const Argument
&Arg
, unsigned &Offset
,
108 msgpack::ArrayDocNode Args
);
110 void emitKernelArg(const DataLayout
&DL
, Type
*Ty
, Align Alignment
,
111 StringRef ValueKind
, unsigned &Offset
,
112 msgpack::ArrayDocNode Args
,
113 MaybeAlign PointeeAlign
= std::nullopt
,
114 StringRef Name
= "", StringRef TypeName
= "",
115 StringRef BaseTypeName
= "", StringRef ActAccQual
= "",
116 StringRef AccQual
= "", StringRef TypeQual
= "");
118 void emitHiddenKernelArgs(const MachineFunction
&MF
, unsigned &Offset
,
119 msgpack::ArrayDocNode Args
) override
;
121 msgpack::DocNode
&getRootMetadata(StringRef Key
) {
122 return HSAMetadataDoc
->getRoot().getMap(/*Convert=*/true)[Key
];
125 msgpack::DocNode
&getHSAMetadataRoot() {
126 return HSAMetadataDoc
->getRoot();
130 MetadataStreamerMsgPackV4() = default;
131 ~MetadataStreamerMsgPackV4() = default;
133 bool emitTo(AMDGPUTargetStreamer
&TargetStreamer
) override
;
135 void begin(const Module
&Mod
,
136 const IsaInfo::AMDGPUTargetID
&TargetID
) override
;
140 void emitKernel(const MachineFunction
&MF
,
141 const SIProgramInfo
&ProgramInfo
) override
;
144 class MetadataStreamerMsgPackV5
: public MetadataStreamerMsgPackV4
{
146 void emitVersion() override
;
147 void emitHiddenKernelArgs(const MachineFunction
&MF
, unsigned &Offset
,
148 msgpack::ArrayDocNode Args
) override
;
149 void emitKernelAttrs(const Function
&Func
, msgpack::MapDocNode Kern
) override
;
152 MetadataStreamerMsgPackV5() = default;
153 ~MetadataStreamerMsgPackV5() = default;
156 class MetadataStreamerMsgPackV6 final
: public MetadataStreamerMsgPackV5
{
158 void emitVersion() override
;
161 MetadataStreamerMsgPackV6() = default;
162 ~MetadataStreamerMsgPackV6() = default;
165 } // end namespace HSAMD
166 } // end namespace AMDGPU
167 } // end namespace llvm
169 #endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H