xref: /aosp_15_r20/external/executorch/backends/apple/mps/runtime/MPSDelegateHeader.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 //
2 //  Copyright (c) 2024 Apple Inc. All rights reserved.
3 //  Provided subject to the LICENSE file in the top level directory.
4 //
5 
6 #pragma once
7 
8 #include <executorch/runtime/core/result.h>
9 
10 namespace executorch {
11 namespace backends {
12 namespace mps {
13 namespace delegate {
14 
15 /**
16  * MPS-header that is embedded before the flatbuffer payload
17  *
18  */
19 struct MPSDelegateHeader {
20   /**
21    * The minimum size of the MPSDelegateHeader. The caller should provide at
22    * least this many bytes of the head of the serialized MPS Data
23    */
24   static constexpr size_t kMinSize = 30;
25 
26   /**
27    * The magic offset. This offset is the same as the offset for flatbuffer
28    * header so we will be able to check if the header is is either the
29    * flatbuffer head or the wrapper header we introduce here
30    */
31   static constexpr size_t kMagicOffset = 4;
32 
33   /**
34    * The magic bytes that identify the header.
35    *
36    * This is the canonical definition of the expected value. If the header
37    * layout ever changes in a compatibility-breaking way, increment the digits
38    * in the magic. But, doing so will prevent older binaries from recognizing
39    * the presence of the header. The compatibility-preserving way to make
40    * changes is to increase the header's length field and add new fields at the
41    * end.
42    */
43   static constexpr size_t kMagicSize = 4;
44   static constexpr char kMagic[kMagicSize] = {'M', 'P', '0', '0'};
45 
46   /**
47    * The size in bytes of the header length. We store 2 bytes for the header
48    * length
49    */
50   static constexpr size_t kHeaderLengthSize = 2;
51 
52   /**
53    * The expected location of the header length field relative to the beginning
54    * of the header.
55    */
56   static constexpr size_t kHeaderLengthOffset =
57       MPSDelegateHeader::kMagicOffset + MPSDelegateHeader::kMagicSize;
58 
59   /*
60    * The expected location of the constant data offset field relative to the
61    * beginning of the header.
62    */
63   static constexpr size_t kConstantDataSegmentOffset = kHeaderLengthOffset;
64 
65   /*
66    * The expected location of the constant data size field relative to the
67    * beginning of the header.
68    */
69   static constexpr size_t kConstantDataSizeOffset =
70       kConstantDataSegmentOffset + sizeof(uint64_t);
71 
72   /**
73    * The expected location of the flatbuffer data offset field relative to the
74    * beginning of the header.
75    */
76   static constexpr size_t kFlatbufferDataOffsetOffset =
77       kConstantDataSizeOffset + sizeof(uint64_t);
78 
79   /**
80    * Look for and parse an ExtendedHeader in the provided data.
81    *
82    * @param[in] data The contents of the beginning of the serialized binary
83    *     Program data, starting at offset 0 (i.e., the head of the file).
84    * @param[in] size Length of `data` in bytes.
85    *
86    * @returns an MPSHeader if the header was found and is valid. Returns an
87    *     error if size was too short, if the header was not found, or if the
88    *     header appeared to be corrupt.
89    */
90   static executorch::runtime::Result<MPSDelegateHeader> Parse(
91       const void* data,
92       size_t size);
93 
94   /**
95    * The offset in bytes to the beginning of the constant data.
96    */
97   uint64_t constant_data_offset;
98   /**
99    * The size in bytes of the constant data.
100    */
101   uint64_t constant_data_size;
102   /**
103    * The offset in bytes to the beginning of the flatbuffer data.
104    */
105   uint64_t flatbuffer_offset;
106   /**
107    * The size in bytes of the flatbuffer data.
108    */
109   uint64_t flatbuffer_size;
110 };
111 
112 } // namespace delegate
113 } // namespace mps
114 } // namespace backends
115 } // namespace executorch
116