blob: 80ac8ca67bcd1eddd251e44b1e06e039201ec698 [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"
Tim Renoufed0b9af2019-03-13 18:55:50 +000021#include "llvm/BinaryFormat/MsgPackDocument.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:
Tim Renoufed0b9af2019-03-13 18:55:50 +000054 std::unique_ptr<msgpack::Document> HSAMetadataDoc =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000055 std::make_unique<msgpack::Document>();
Scott Linderf5b36e52018-12-12 19:39:27 +000056
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
Tim Renoufed0b9af2019-03-13 18:55:50 +000072 msgpack::ArrayDocNode getWorkGroupDimensions(MDNode *Node) const;
Scott Linderf5b36e52018-12-12 19:39:27 +000073
Tim Renoufed0b9af2019-03-13 18:55:50 +000074 msgpack::MapDocNode getHSAKernelProps(const MachineFunction &MF,
75 const SIProgramInfo &ProgramInfo) const;
Scott Linderf5b36e52018-12-12 19:39:27 +000076
77 void emitVersion();
78
79 void emitPrintf(const Module &Mod);
80
Tim Renoufed0b9af2019-03-13 18:55:50 +000081 void emitKernelLanguage(const Function &Func, msgpack::MapDocNode Kern);
Scott Linderf5b36e52018-12-12 19:39:27 +000082
Tim Renoufed0b9af2019-03-13 18:55:50 +000083 void emitKernelAttrs(const Function &Func, msgpack::MapDocNode Kern);
Scott Linderf5b36e52018-12-12 19:39:27 +000084
Tim Renoufed0b9af2019-03-13 18:55:50 +000085 void emitKernelArgs(const Function &Func, msgpack::MapDocNode Kern);
Scott Linderf5b36e52018-12-12 19:39:27 +000086
87 void emitKernelArg(const Argument &Arg, unsigned &Offset,
Tim Renoufed0b9af2019-03-13 18:55:50 +000088 msgpack::ArrayDocNode Args);
Scott Linderf5b36e52018-12-12 19:39:27 +000089
90 void emitKernelArg(const DataLayout &DL, Type *Ty, StringRef ValueKind,
Tim Renoufed0b9af2019-03-13 18:55:50 +000091 unsigned &Offset, msgpack::ArrayDocNode Args,
Scott Linderf5b36e52018-12-12 19:39:27 +000092 unsigned PointeeAlign = 0, StringRef Name = "",
93 StringRef TypeName = "", StringRef BaseTypeName = "",
94 StringRef AccQual = "", StringRef TypeQual = "");
95
96 void emitHiddenKernelArgs(const Function &Func, unsigned &Offset,
Tim Renoufed0b9af2019-03-13 18:55:50 +000097 msgpack::ArrayDocNode Args);
Scott Linderf5b36e52018-12-12 19:39:27 +000098
Tim Renoufed0b9af2019-03-13 18:55:50 +000099 msgpack::DocNode &getRootMetadata(StringRef Key) {
100 return HSAMetadataDoc->getRoot().getMap(/*Convert=*/true)[Key];
Scott Linderf5b36e52018-12-12 19:39:27 +0000101 }
102
Tim Renoufed0b9af2019-03-13 18:55:50 +0000103 msgpack::DocNode &getHSAMetadataRoot() {
104 return HSAMetadataDoc->getRoot();
Scott Linderf5b36e52018-12-12 19:39:27 +0000105 }
106
107public:
108 MetadataStreamerV3() = default;
109 ~MetadataStreamerV3() = default;
110
111 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
112
113 void begin(const Module &Mod) override;
114
115 void end() override;
116
117 void emitKernel(const MachineFunction &MF,
118 const SIProgramInfo &ProgramInfo) override;
119};
120
121class MetadataStreamerV2 final : public MetadataStreamer {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000122private:
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000123 Metadata HSAMetadata;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000124
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000125 void dump(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000126
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000127 void verify(StringRef HSAMetadataString) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000128
129 AccessQualifier getAccessQualifier(StringRef AccQual) const;
130
Scott Linderf5b36e52018-12-12 19:39:27 +0000131 AddressSpaceQualifier getAddressSpaceQualifier(unsigned AddressSpace) const;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000132
133 ValueKind getValueKind(Type *Ty, StringRef TypeQual,
134 StringRef BaseTypeName) const;
135
136 ValueType getValueType(Type *Ty, StringRef TypeName) const;
137
138 std::string getTypeName(Type *Ty, bool Signed) const;
139
140 std::vector<uint32_t> getWorkGroupDimensions(MDNode *Node) const;
141
Scott Linder2ad2c182018-07-10 17:31:32 +0000142 Kernel::CodeProps::Metadata getHSACodeProps(
143 const MachineFunction &MF,
144 const SIProgramInfo &ProgramInfo) const;
145 Kernel::DebugProps::Metadata getHSADebugProps(
146 const MachineFunction &MF,
147 const SIProgramInfo &ProgramInfo) const;
148
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000149 void emitVersion();
150
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000151 void emitPrintf(const Module &Mod);
152
153 void emitKernelLanguage(const Function &Func);
154
155 void emitKernelAttrs(const Function &Func);
156
157 void emitKernelArgs(const Function &Func);
158
159 void emitKernelArg(const Argument &Arg);
160
161 void emitKernelArg(const DataLayout &DL, Type *Ty, ValueKind ValueKind,
Matt Arsenault73eeb422018-06-25 14:29:04 +0000162 unsigned PointeeAlign = 0,
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000163 StringRef Name = "", StringRef TypeName = "",
164 StringRef BaseTypeName = "", StringRef AccQual = "",
165 StringRef TypeQual = "");
Konstantin Zhuravlyova780ffa2017-03-22 23:10:46 +0000166
Konstantin Zhuravlyovf0badd52018-07-10 16:12:51 +0000167 void emitHiddenKernelArgs(const Function &Func);
168
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000169 const Metadata &getHSAMetadata() const {
170 return HSAMetadata;
171 }
172
Scott Linderf5b36e52018-12-12 19:39:27 +0000173public:
174 MetadataStreamerV2() = default;
175 ~MetadataStreamerV2() = default;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000176
Scott Linderf5b36e52018-12-12 19:39:27 +0000177 bool emitTo(AMDGPUTargetStreamer &TargetStreamer) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000178
Scott Linderf5b36e52018-12-12 19:39:27 +0000179 void begin(const Module &Mod) override;
180
181 void end() override;
182
183 void emitKernel(const MachineFunction &MF,
184 const SIProgramInfo &ProgramInfo) override;
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000185};
186
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000187} // end namespace HSAMD
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000188} // end namespace AMDGPU
189} // end namespace llvm
190
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000191#endif // LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUHSAMETADATASTREAMER_H