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