1 //===- AMDGPUMetadataVerifier.cpp - MsgPack Types ---------------*- 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 /// Implements a verifier for AMDGPU HSA metadata.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Support/AMDGPUMetadata.h"
23 bool MetadataVerifier::verifyScalar(
24 msgpack::DocNode
&Node
, msgpack::Type SKind
,
25 function_ref
<bool(msgpack::DocNode
&)> verifyValue
) {
28 if (Node
.getKind() != SKind
) {
31 // If we are not strict, we interpret string values as "implicitly typed"
32 // and attempt to coerce them to the expected type here.
33 if (Node
.getKind() != msgpack::Type::String
)
35 StringRef StringValue
= Node
.getString();
36 Node
.fromString(StringValue
);
37 if (Node
.getKind() != SKind
)
41 return verifyValue(Node
);
45 bool MetadataVerifier::verifyInteger(msgpack::DocNode
&Node
) {
46 if (!verifyScalar(Node
, msgpack::Type::UInt
))
47 if (!verifyScalar(Node
, msgpack::Type::Int
))
52 bool MetadataVerifier::verifyArray(
53 msgpack::DocNode
&Node
, function_ref
<bool(msgpack::DocNode
&)> verifyNode
,
54 Optional
<size_t> Size
) {
57 auto &Array
= Node
.getArray();
58 if (Size
&& Array
.size() != *Size
)
60 for (auto &Item
: Array
)
61 if (!verifyNode(Item
))
67 bool MetadataVerifier::verifyEntry(
68 msgpack::MapDocNode
&MapNode
, StringRef Key
, bool Required
,
69 function_ref
<bool(msgpack::DocNode
&)> verifyNode
) {
70 auto Entry
= MapNode
.find(Key
);
71 if (Entry
== MapNode
.end())
73 return verifyNode(Entry
->second
);
76 bool MetadataVerifier::verifyScalarEntry(
77 msgpack::MapDocNode
&MapNode
, StringRef Key
, bool Required
,
79 function_ref
<bool(msgpack::DocNode
&)> verifyValue
) {
80 return verifyEntry(MapNode
, Key
, Required
, [=](msgpack::DocNode
&Node
) {
81 return verifyScalar(Node
, SKind
, verifyValue
);
85 bool MetadataVerifier::verifyIntegerEntry(msgpack::MapDocNode
&MapNode
,
86 StringRef Key
, bool Required
) {
87 return verifyEntry(MapNode
, Key
, Required
, [this](msgpack::DocNode
&Node
) {
88 return verifyInteger(Node
);
92 bool MetadataVerifier::verifyKernelArgs(msgpack::DocNode
&Node
) {
95 auto &ArgsMap
= Node
.getMap();
97 if (!verifyScalarEntry(ArgsMap
, ".name", false,
98 msgpack::Type::String
))
100 if (!verifyScalarEntry(ArgsMap
, ".type_name", false,
101 msgpack::Type::String
))
103 if (!verifyIntegerEntry(ArgsMap
, ".size", true))
105 if (!verifyIntegerEntry(ArgsMap
, ".offset", true))
107 if (!verifyScalarEntry(ArgsMap
, ".value_kind", true,
108 msgpack::Type::String
,
109 [](msgpack::DocNode
&SNode
) {
110 return StringSwitch
<bool>(SNode
.getString())
111 .Case("by_value", true)
112 .Case("global_buffer", true)
113 .Case("dynamic_shared_pointer", true)
114 .Case("sampler", true)
118 .Case("hidden_global_offset_x", true)
119 .Case("hidden_global_offset_y", true)
120 .Case("hidden_global_offset_z", true)
121 .Case("hidden_none", true)
122 .Case("hidden_printf_buffer", true)
123 .Case("hidden_hostcall_buffer", true)
124 .Case("hidden_default_queue", true)
125 .Case("hidden_completion_action", true)
126 .Case("hidden_multigrid_sync_arg", true)
130 if (!verifyIntegerEntry(ArgsMap
, ".pointee_align", false))
132 if (!verifyScalarEntry(ArgsMap
, ".address_space", false,
133 msgpack::Type::String
,
134 [](msgpack::DocNode
&SNode
) {
135 return StringSwitch
<bool>(SNode
.getString())
136 .Case("private", true)
137 .Case("global", true)
138 .Case("constant", true)
140 .Case("generic", true)
141 .Case("region", true)
145 if (!verifyScalarEntry(ArgsMap
, ".access", false,
146 msgpack::Type::String
,
147 [](msgpack::DocNode
&SNode
) {
148 return StringSwitch
<bool>(SNode
.getString())
149 .Case("read_only", true)
150 .Case("write_only", true)
151 .Case("read_write", true)
155 if (!verifyScalarEntry(ArgsMap
, ".actual_access", false,
156 msgpack::Type::String
,
157 [](msgpack::DocNode
&SNode
) {
158 return StringSwitch
<bool>(SNode
.getString())
159 .Case("read_only", true)
160 .Case("write_only", true)
161 .Case("read_write", true)
165 if (!verifyScalarEntry(ArgsMap
, ".is_const", false,
166 msgpack::Type::Boolean
))
168 if (!verifyScalarEntry(ArgsMap
, ".is_restrict", false,
169 msgpack::Type::Boolean
))
171 if (!verifyScalarEntry(ArgsMap
, ".is_volatile", false,
172 msgpack::Type::Boolean
))
174 if (!verifyScalarEntry(ArgsMap
, ".is_pipe", false,
175 msgpack::Type::Boolean
))
181 bool MetadataVerifier::verifyKernel(msgpack::DocNode
&Node
) {
184 auto &KernelMap
= Node
.getMap();
186 if (!verifyScalarEntry(KernelMap
, ".name", true,
187 msgpack::Type::String
))
189 if (!verifyScalarEntry(KernelMap
, ".symbol", true,
190 msgpack::Type::String
))
192 if (!verifyScalarEntry(KernelMap
, ".language", false,
193 msgpack::Type::String
,
194 [](msgpack::DocNode
&SNode
) {
195 return StringSwitch
<bool>(SNode
.getString())
196 .Case("OpenCL C", true)
197 .Case("OpenCL C++", true)
200 .Case("OpenMP", true)
201 .Case("Assembler", true)
206 KernelMap
, ".language_version", false, [this](msgpack::DocNode
&Node
) {
209 [this](msgpack::DocNode
&Node
) { return verifyInteger(Node
); }, 2);
212 if (!verifyEntry(KernelMap
, ".args", false, [this](msgpack::DocNode
&Node
) {
213 return verifyArray(Node
, [this](msgpack::DocNode
&Node
) {
214 return verifyKernelArgs(Node
);
218 if (!verifyEntry(KernelMap
, ".reqd_workgroup_size", false,
219 [this](msgpack::DocNode
&Node
) {
220 return verifyArray(Node
,
221 [this](msgpack::DocNode
&Node
) {
222 return verifyInteger(Node
);
227 if (!verifyEntry(KernelMap
, ".workgroup_size_hint", false,
228 [this](msgpack::DocNode
&Node
) {
229 return verifyArray(Node
,
230 [this](msgpack::DocNode
&Node
) {
231 return verifyInteger(Node
);
236 if (!verifyScalarEntry(KernelMap
, ".vec_type_hint", false,
237 msgpack::Type::String
))
239 if (!verifyScalarEntry(KernelMap
, ".device_enqueue_symbol", false,
240 msgpack::Type::String
))
242 if (!verifyIntegerEntry(KernelMap
, ".kernarg_segment_size", true))
244 if (!verifyIntegerEntry(KernelMap
, ".group_segment_fixed_size", true))
246 if (!verifyIntegerEntry(KernelMap
, ".private_segment_fixed_size", true))
248 if (!verifyIntegerEntry(KernelMap
, ".kernarg_segment_align", true))
250 if (!verifyIntegerEntry(KernelMap
, ".wavefront_size", true))
252 if (!verifyIntegerEntry(KernelMap
, ".sgpr_count", true))
254 if (!verifyIntegerEntry(KernelMap
, ".vgpr_count", true))
256 if (!verifyIntegerEntry(KernelMap
, ".max_flat_workgroup_size", true))
258 if (!verifyIntegerEntry(KernelMap
, ".sgpr_spill_count", false))
260 if (!verifyIntegerEntry(KernelMap
, ".vgpr_spill_count", false))
266 bool MetadataVerifier::verify(msgpack::DocNode
&HSAMetadataRoot
) {
267 if (!HSAMetadataRoot
.isMap())
269 auto &RootMap
= HSAMetadataRoot
.getMap();
272 RootMap
, "amdhsa.version", true, [this](msgpack::DocNode
&Node
) {
275 [this](msgpack::DocNode
&Node
) { return verifyInteger(Node
); }, 2);
279 RootMap
, "amdhsa.printf", false, [this](msgpack::DocNode
&Node
) {
280 return verifyArray(Node
, [this](msgpack::DocNode
&Node
) {
281 return verifyScalar(Node
, msgpack::Type::String
);
285 if (!verifyEntry(RootMap
, "amdhsa.kernels", true,
286 [this](msgpack::DocNode
&Node
) {
287 return verifyArray(Node
, [this](msgpack::DocNode
&Node
) {
288 return verifyKernel(Node
);
296 } // end namespace V3
297 } // end namespace HSAMD
298 } // end namespace AMDGPU
299 } // end namespace llvm