blob: afc09baf952d6e7c869cd9435600da4a14bf3ed6 [file] [log] [blame]
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +00001//===--- AMDGPUHSAMetadataStreamer.h ----------------------------*- C++ -*-===//
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// AMDGPU HSA Metadata Streamer.
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000012///
13//
14//===----------------------------------------------------------------------===//
15
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000016#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
17#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000018
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000019#include "AMDGPU.h"
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +000020#include "AMDKernelCodeT.h"
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000021#include "llvm/ADT/StringRef.h"
Scott Linderf5b36e52018-12-12 19:39:27 +000022#include "llvm/BinaryFormat/MsgPackTypes.h"
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000023#include "llvm/Support/AMDGPUMetadata.h"
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000024
25namespace llvm {
26
Scott Linderf5b36e52018-12-12 19:39:27 +000027class AMDGPUTargetStreamer;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000028class Argument;
29class DataLayout;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000030class Function;
31class MDNode;
32class Module;
Richard Trieud5e57ed2018-07-10 22:09:33 +000033struct SIProgramInfo;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000034class Type;
35
36namespace AMDGPU {
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +000037namespace HSAMD {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +000038
Scott Linderf5b36e52018-12-12 19:39:27 +000039class MetadataStreamer {
40public:
41 virtual ~MetadataStreamer(){};
42
43 virtual bool emitTo(AMDGPUTargetStreamer &TargetStreamer) = 0;
44
45 virtual void begin(const Module &Mod) = 0;
46
47 virtual void end() = 0;
48
49 virtual void emitKernel(const MachineFunction &MF,
50 const SIProgramInfo &ProgramInfo) = 0;
51};
52
53class MetadataStreamerV3 final : public MetadataStreamer {
54private:
55 std::shared_ptr<msgpack::Node> HSAMetadataRoot =
56 std::make_shared<msgpack::MapNode>();
57
58 void dump(StringRef HSAMetadataString) const;
59
60 void verify(StringRef HSAMetadataString) const;
61
62 Optional<StringRef> getAccessQualifier(StringRef AccQual) const;
63
64 Optional<StringRef> getAddressSpaceQualifier(unsigned AddressSpace) const;
65
66 StringRef getValueKind(Type *Ty, StringRef TypeQual,
67 StringRef BaseTypeName) const;
68
69 StringRef getValueType(Type *Ty, StringRef TypeName) const;
70
71 std::string getTypeName(Type *Ty, bool Signed) const;
72
73 std::shared_ptr<msgpack::ArrayNode>
74 getWorkGroupDimensions(MDNode *Node) const;
75
76 std::shared_ptr<msgpack::MapNode>
77 getHSAKernelProps(const MachineFunction &MF,
78 const SIProgramInfo &ProgramInfo) const;
79
80 void emitVersion();
81
82 void emitPrintf(const Module &Mod);
83
84 void emitKernelLanguage(const Function &Func, msgpack::MapNode &Kern);
85
86 void emitKernelAttrs(const Function &Func, msgpack::MapNode &Kern);
87
88 void emitKernelArgs(const Function &Func, msgpack::MapNode &Kern);
89
90 void emitKernelArg(const Argument &Arg, unsigned &Offset,
91 msgpack::ArrayNode &Args);
92
93 void emitKernelArg(const DataLayout &DL, Type *Ty, StringRef ValueKind,
94 unsigned &Offset, msgpack::ArrayNode &Args,
95 unsigned PointeeAlign = 0, StringRef Name = "",
96 StringRef TypeName = "", StringRef BaseTypeName = "",
97 StringRef AccQual = "", StringRef TypeQual = "");
98
99 void emitHiddenKernelArgs(const Function &Func, unsigned &Offset,
100 msgpack::ArrayNode &Args);
101
102 std::shared_ptr<msgpack::Node> &getRootMetadata(StringRef Key) {
103 return (*cast<msgpack::MapNode>(HSAMetadataRoot.get()))[Key];
104 }
105
106 std::shared_ptr<msgpack::Node> &getHSAMetadataRoot() {
107 return HSAMetadataRoot;
108 }
109
110public:
111 MetadataStreamerV3() = default;
112 ~MetadataStreamerV3() = default;
113
114 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
115
116 void begin(const Module &Mod) override;
117
118 void end() override;
119
120 void emitKernel(const MachineFunction &MF,
121 const SIProgramInfo &ProgramInfo) override;
122};
123
124class MetadataStreamerV2 final : public MetadataStreamer {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000125private:
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000126 Metadata HSAMetadata;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000127
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000128 void dump(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000129
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000130 void verify(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000131
132 AccessQualifier getAccessQualifier(StringRef AccQual) const;
133
Scott Linderf5b36e52018-12-12 19:39:27 +0000134 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000135
136 ValueKind getValueKind(Type *Ty, StringRef TypeQual,
137 StringRef BaseTypeName) const;
138
139 ValueType getValueType(Type *Ty, StringRef TypeName) const;
140
141 std::string getTypeName(Type *Ty, bool Signed) const;
142
143 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
144
Scott Linder2ad2c182018-07-10 17:31:32 +0000145 Kernel::CodeProps::Metadata getHSACodeProps(
146 const MachineFunction &MF,
147 const SIProgramInfo &ProgramInfo) const;
148 Kernel::DebugProps::Metadata getHSADebugProps(
149 const MachineFunction &MF,
150 const SIProgramInfo &ProgramInfo) const;
151
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000152 void emitVersion();
153
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000154 void emitPrintf(const Module &Mod);
155
156 void emitKernelLanguage(const Function &Func);
157
158 void emitKernelAttrs(const Function &Func);
159
160 void emitKernelArgs(const Function &Func);
161
162 void emitKernelArg(const Argument &Arg);
163
164 void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind,
Matt Arsenault73eeb422018-06-25 14:29:04 +0000165 unsigned PointeeAlign = 0,
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000166 StringRef Name = "", StringRef TypeName = "",
167 StringRef BaseTypeName = "", StringRef AccQual = "",
168 StringRef TypeQual = "");
Konstantin Zhuravlyova780ffa2017-03-22 23:10:46 +0000169
Konstantin Zhuravlyovf0badd52018-07-10 16:12:51 +0000170 void emitHiddenKernelArgs(const Function &Func);
171
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000172 const Metadata &getHSAMetadata() const {
173 return HSAMetadata;
174 }
175
Scott Linderf5b36e52018-12-12 19:39:27 +0000176public:
177 MetadataStreamerV2() = default;
178 ~MetadataStreamerV2() = default;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000179
Scott Linderf5b36e52018-12-12 19:39:27 +0000180 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000181
Scott Linderf5b36e52018-12-12 19:39:27 +0000182 void begin(const Module &Mod) override;
183
184 void end() override;
185
186 void emitKernel(const MachineFunction &MF,
187 const SIProgramInfo &ProgramInfo) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000188};
189
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000190} // end namespace HSAMD
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000191} // end namespace AMDGPU
192} // end namespace llvm
193
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000194#endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H