1 //===- AMDGPUMetadataVerifier.h - MsgPack Types -----------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This is a verifier for AMDGPU HSA metadata, which can verify both
11 /// well-typed metadata and untyped metadata. When verifying in the non-strict
12 /// mode, untyped metadata is coerced into the correct type if possible.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_BINARYFORMAT_AMDGPUMETADATAVERIFIER_H
17 #define LLVM_BINARYFORMAT_AMDGPUMETADATAVERIFIER_H
18 
19 #include "llvm/ADT/STLFunctionalExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/BinaryFormat/MsgPackReader.h"
22 
23 #include <cstddef>
24 #include <optional>
25 
26 namespace llvm {
27 
28 namespace msgpack {
29   class DocNode;
30   class MapDocNode;
31 }
32 
33 namespace AMDGPU {
34 namespace HSAMD {
35 namespace V3 {
36 
37 /// Verifier for AMDGPU HSA metadata.
38 ///
39 /// Operates in two modes:
40 ///
41 /// In strict mode, metadata must already be well-typed.
42 ///
43 /// In non-strict mode, metadata is coerced into expected types when possible.
44 class MetadataVerifier {
45   bool Strict;
46 
47   bool verifyScalar(msgpack::DocNode &Node, msgpack::Type SKind,
48                     function_ref<bool(msgpack::DocNode &)> verifyValue = {});
49   bool verifyInteger(msgpack::DocNode &Node);
50   bool verifyArray(msgpack::DocNode &Node,
51                    function_ref<bool(msgpack::DocNode &)> verifyNode,
52                    std::optional<size_t> Size = std::nullopt);
53   bool verifyEntry(msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
54                    function_ref<bool(msgpack::DocNode &)> verifyNode);
55   bool
56   verifyScalarEntry(msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
57                     msgpack::Type SKind,
58                     function_ref<bool(msgpack::DocNode &)> verifyValue = {});
59   bool verifyIntegerEntry(msgpack::MapDocNode &MapNode, StringRef Key,
60                           bool Required);
61   bool verifyKernelArgs(msgpack::DocNode &Node);
62   bool verifyKernel(msgpack::DocNode &Node);
63 
64 public:
65   /// Construct a MetadataVerifier, specifying whether it will operate in \p
66   /// Strict mode.
67   MetadataVerifier(bool Strict) : Strict(Strict) {}
68 
69   /// Verify given HSA metadata.
70   ///
71   /// \returns True when successful, false when metadata is invalid.
72   bool verify(msgpack::DocNode &HSAMetadataRoot);
73 };
74 
75 } // end namespace V3
76 } // end namespace HSAMD
77 } // end namespace AMDGPU
78 } // end namespace llvm
79 
80 #endif // LLVM_BINARYFORMAT_AMDGPUMETADATAVERIFIER_H
81