blob: 5835ed715a66caf2894505795022e53a3224d08f [file] [log] [blame]
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +00001//===--- AMDGPUHSAMetadataStreamer.h ----------------------------*- C++ -*-===//
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// AMDGPU HSA Metadata Streamer.
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000011///
12//
13//===----------------------------------------------------------------------===//
14
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000015#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
16#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000017
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000018#include "AMDGPU.h"
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +000019#include "AMDKernelCodeT.h"
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000020#include "llvm/ADT/StringRef.h"
Scott Linderf5b36e52018-12-12 19:39:27 +000021#include "llvm/BinaryFormat/MsgPackTypes.h"
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000022#include "llvm/Support/AMDGPUMetadata.h"
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000023
24namespace llvm {
25
Scott Linderf5b36e52018-12-12 19:39:27 +000026class AMDGPUTargetStreamer;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000027class Argument;
28class DataLayout;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000029class Function;
30class MDNode;
31class Module;
Richard Trieud5e57ed2018-07-10 22:09:33 +000032struct SIProgramInfo;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000033class Type;
34
35namespace AMDGPU {
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000036namespace HSAMD {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000037
Scott Linderf5b36e52018-12-12 19:39:27 +000038class MetadataStreamer {
39public:
40 virtual ~MetadataStreamer(){};
41
42 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
43
44 virtual void begin(const Module &Mod) = 0;
45
46 virtual void end() = 0;
47
48 virtual void emitKernel(const MachineFunction &MF,
49 const SIProgramInfo &ProgramInfo) = 0;
50};
51
52class MetadataStreamerV3 final : public MetadataStreamer {
53private:
54 std::shared_ptr<msgpack::Node> HSAMetadataRoot =
55 std::make_shared<msgpack::MapNode>();
56
57 void dump(StringRef HSAMetadataString) const;
58
59 void verify(StringRef HSAMetadataString) const;
60
61 Optional<StringRef> getAccessQualifier(StringRef AccQual) const;
62
63 Optional<StringRef> getAddressSpaceQualifier(unsigned AddressSpace) const;
64
65 StringRef getValueKind(Type *Ty, StringRef TypeQual,
66 StringRef BaseTypeName) const;
67
68 StringRef getValueType(Type *Ty, StringRef TypeName) const;
69
70 std::string getTypeName(Type *Ty, bool Signed) const;
71
72 std::shared_ptr<msgpack::ArrayNode>
73 getWorkGroupDimensions(MDNode *Node) const;
74
75 std::shared_ptr<msgpack::MapNode>
76 getHSAKernelProps(const MachineFunction &MF,
77 const SIProgramInfo &ProgramInfo) const;
78
79 void emitVersion();
80
81 void emitPrintf(const Module &Mod);
82
83 void emitKernelLanguage(const Function &Func, msgpack::MapNode &Kern);
84
85 void emitKernelAttrs(const Function &Func, msgpack::MapNode &Kern);
86
87 void emitKernelArgs(const Function &Func, msgpack::MapNode &Kern);
88
89 void emitKernelArg(const Argument &Arg, unsigned &Offset,
90 msgpack::ArrayNode &Args);
91
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 = "");
97
98 void emitHiddenKernelArgs(const Function &Func, unsigned &Offset,
99 msgpack::ArrayNode &Args);
100
101 std::shared_ptr<msgpack::Node> &getRootMetadata(StringRef Key) {
102 return (*cast<msgpack::MapNode>(HSAMetadataRoot.get()))[Key];
103 }
104
105 std::shared_ptr<msgpack::Node> &getHSAMetadataRoot() {
106 return HSAMetadataRoot;
107 }
108
109public:
110 MetadataStreamerV3() = default;
111 ~MetadataStreamerV3() = default;
112
113 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
114
115 void begin(const Module &Mod) override;
116
117 void end() override;
118
119 void emitKernel(const MachineFunction &MF,
120 const SIProgramInfo &ProgramInfo) override;
121};
122
123class MetadataStreamerV2 final : public MetadataStreamer {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000124private:
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000125 Metadata HSAMetadata;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000126
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000127 void dump(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000128
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000129 void verify(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000130
131 AccessQualifier getAccessQualifier(StringRef AccQual) const;
132
Scott Linderf5b36e52018-12-12 19:39:27 +0000133 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000134
135 ValueKind getValueKind(Type *Ty, StringRef TypeQual,
136 StringRef BaseTypeName) const;
137
138 ValueType getValueType(Type *Ty, StringRef TypeName) const;
139
140 std::string getTypeName(Type *Ty, bool Signed) const;
141
142 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
143
Scott Linder2ad2c182018-07-10 17:31:32 +0000144 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;
150
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000151 void emitVersion();
152
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000153 void emitPrintf(const Module &Mod);
154
155 void emitKernelLanguage(const Function &Func);
156
157 void emitKernelAttrs(const Function &Func);
158
159 void emitKernelArgs(const Function &Func);
160
161 void emitKernelArg(const Argument &Arg);
162
163 void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind,
Matt Arsenault73eeb422018-06-25 14:29:04 +0000164 unsigned PointeeAlign = 0,
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000165 StringRef Name = "", StringRef TypeName = "",
166 StringRef BaseTypeName = "", StringRef AccQual = "",
167 StringRef TypeQual = "");
Konstantin Zhuravlyova780ffa2017-03-22 23:10:46 +0000168
Konstantin Zhuravlyovf0badd52018-07-10 16:12:51 +0000169 void emitHiddenKernelArgs(const Function &Func);
170
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000171 const Metadata &getHSAMetadata() const {
172 return HSAMetadata;
173 }
174
Scott Linderf5b36e52018-12-12 19:39:27 +0000175public:
176 MetadataStreamerV2() = default;
177 ~MetadataStreamerV2() = default;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000178
Scott Linderf5b36e52018-12-12 19:39:27 +0000179 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000180
Scott Linderf5b36e52018-12-12 19:39:27 +0000181 void begin(const Module &Mod) override;
182
183 void end() override;
184
185 void emitKernel(const MachineFunction &MF,
186 const SIProgramInfo &ProgramInfo) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000187};
188
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000189} // end namespace HSAMD
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000190} // end namespace AMDGPU
191} // end namespace llvm
192
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000193#endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H