xref: /freebsd/contrib/llvm-project/llvm/include/llvm/BinaryFormat/AMDGPUMetadataVerifier.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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/None.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLFunctionalExtras.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/BinaryFormat/MsgPackReader.h"
24 
25 #include <cstddef>
26 
27 namespace llvm {
28 
29 namespace msgpack {
30   class DocNode;
31   class MapDocNode;
32 }
33 
34 namespace AMDGPU {
35 namespace HSAMD {
36 namespace V3 {
37 
38 /// Verifier for AMDGPU HSA metadata.
39 ///
40 /// Operates in two modes:
41 ///
42 /// In strict mode, metadata must already be well-typed.
43 ///
44 /// In non-strict mode, metadata is coerced into expected types when possible.
45 class MetadataVerifier {
46   bool Strict;
47 
48   bool verifyScalar(msgpack::DocNode &Node, msgpack::Type SKind,
49                     function_ref<bool(msgpack::DocNode &)> verifyValue = {});
50   bool verifyInteger(msgpack::DocNode &Node);
51   bool verifyArray(msgpack::DocNode &Node,
52                    function_ref<bool(msgpack::DocNode &)> verifyNode,
53                    Optional<size_t> Size = None);
54   bool verifyEntry(msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
55                    function_ref<bool(msgpack::DocNode &)> verifyNode);
56   bool
57   verifyScalarEntry(msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
58                     msgpack::Type SKind,
59                     function_ref<bool(msgpack::DocNode &)> verifyValue = {});
60   bool verifyIntegerEntry(msgpack::MapDocNode &MapNode, StringRef Key,
61                           bool Required);
62   bool verifyKernelArgs(msgpack::DocNode &Node);
63   bool verifyKernel(msgpack::DocNode &Node);
64 
65 public:
66   /// Construct a MetadataVerifier, specifying whether it will operate in \p
67   /// Strict mode.
68   MetadataVerifier(bool Strict) : Strict(Strict) {}
69 
70   /// Verify given HSA metadata.
71   ///
72   /// \returns True when successful, false when metadata is invalid.
73   bool verify(msgpack::DocNode &HSAMetadataRoot);
74 };
75 
76 } // end namespace V3
77 } // end namespace HSAMD
78 } // end namespace AMDGPU
79 } // end namespace llvm
80 
81 #endif // LLVM_BINARYFORMAT_AMDGPUMETADATAVERIFIER_H
82