Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PgoFormat.cs « Pgo « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa4fe3c1a31809d0f04e782375084d18b6a6598c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace Internal.Pgo
{
    public enum PgoInstrumentationKind
    {
        // This must be kept in sync with PgoInstrumentationKind in corjit.h
        // New InstrumentationKinds should receive corresponding merging logic in
        // PgoSchemeMergeComparer and the MergeInSchemaElem functions below

        // Schema data types
        None = 0,
        FourByte = 1,
        EightByte = 2,
        TypeHandle = 3,
        MethodHandle = 4,

        // Mask of all schema data types
        MarshalMask = 0xF,

        // ExcessAlignment
        Align4Byte = 0x10,
        Align8Byte = 0x20,
        AlignPointer = 0x30,

        // Mask of all schema data types
        AlignMask = 0x30,

        DescriptorMin = 0x40,
        DescriptorMask = ~(MarshalMask | AlignMask),

        Done = None, // All instrumentation schemas must end with a record which is "Done"
        BasicBlockIntCount = (DescriptorMin * 1) | FourByte, // basic block counter using unsigned 4 byte int
        BasicBlockLongCount = (DescriptorMin * 1) | EightByte, // basic block counter using unsigned 8 byte int
        HandleHistogramIntCount = (DescriptorMin * 2) | FourByte | AlignPointer, // 4 byte counter that is part of a type histogram. Aligned to match ClassProfile32's alignment.
        HandleHistogramLongCount = (DescriptorMin * 2) | EightByte, // 8 byte counter that is part of a type histogram
        HandleHistogramTypes = (DescriptorMin * 3) | TypeHandle, // TypeHandle that is part of a type histogram
        HandleHistogramMethods = (DescriptorMin * 3) | MethodHandle, // TypeHandle that is part of a type histogram
        Version = (DescriptorMin * 4) | None, // Version is encoded in the Other field of the schema
        NumRuns = (DescriptorMin * 5) | None, // Number of runs is encoded in the Other field of the schema
        EdgeIntCount = (DescriptorMin * 6) | FourByte, // edge counter using unsigned 4 byte int
        EdgeLongCount = (DescriptorMin * 6) | EightByte, // edge counter using unsigned 8 byte int
        GetLikelyClass = (DescriptorMin * 7) | TypeHandle, // Compressed get likely class data
        GetLikelyMethod = (DescriptorMin * 7) | MethodHandle, // Compressed get likely method data
    }

    public interface IPgoSchemaDataLoader<TType, TMethod>
    {
        TType TypeFromLong(long input);
        TMethod MethodFromLong(long input);
    }

    public interface IPgoEncodedValueEmitter<TType, TMethod>
    {
        void EmitType(TType type, TType previousValue);
        void EmitMethod(TMethod method, TMethod previousValue);
        void EmitLong(long value, long previousValue);
        bool EmitDone();
    }

    public struct PgoSchemaElem
    {
        public PgoInstrumentationKind InstrumentationKind;
        public int ILOffset;
        public int Count;
        public int Other;

        // Data is normally stored as a long
        // but for Types/Method/Fields/conditions where Count > 1
        // the DataObject field is used instead
        public long DataLong;
        public Array DataObject;

        public bool DataHeldInDataLong => (Count == 1 &&
                            (((InstrumentationKind & PgoInstrumentationKind.MarshalMask) == PgoInstrumentationKind.FourByte) ||
                            ((InstrumentationKind & PgoInstrumentationKind.MarshalMask) == PgoInstrumentationKind.EightByte)));

        public override string ToString() => $"{InstrumentationKind} @ {ILOffset} Count = {Count} DataLong = {DataLong}";
    }

    // Flags stored in 'Other' field of TypeHandleHistogram*Count entries.
    [Flags]
    public enum ClassProfileFlags : uint
    {
        IsInterface = 0x40000000,
        IsClass = 0x80000000,
    }

    public class PgoProcessor
    {
        private enum InstrumentationDataProcessingState
        {
            Done = 0,
            ILOffset = 0x1,
            Type = 0x2,
            Count = 0x4,
            Other = 0x8,
            UpdateProcessMask = 0xF,
            UpdateProcessMaskFlag = 0x100,
        }

        private const long SIGN_MASK_ONEBYTE_64BIT = unchecked((long)0xffffffffffffffc0L);
        private const long SIGN_MASK_TWOBYTE_64BIT = unchecked((long)0xffffffffffffe000L);
        private const long SIGN_MASK_FOURBYTE_64BIT = unchecked((long)0xffffffff80000000L);

        public class PgoEncodedCompressedIntParser : IEnumerable<long>, IEnumerator<long>
        {
            private long _current;
            private byte[] _bytes;

            public PgoEncodedCompressedIntParser(byte[] bytes, int startOffset)
            {
                _bytes = bytes;
                Offset = startOffset;
            }

            public int Offset { get; private set; }

            public long Current => _current;

            object IEnumerator.Current => throw new NotImplementedException();

            public PgoEncodedCompressedIntParser GetEnumerator() => this;

            public bool MoveNext()
            {
                byte[] bytes = _bytes;
                int offset = Offset;
                if (offset >= _bytes.Length)
                    return false;

                // This logic is a variant on CorSigUncompressSignedInt which allows for the full range of an int64_t
                long signedInt;
                if ((bytes[offset] & 0x80) == 0x0) // 0??? ????
                {
                    int shiftedInt = bytes[offset];
                    signedInt = shiftedInt >> 1;
                    if ((shiftedInt & 1) != 0)
                    {
                        signedInt |= SIGN_MASK_ONEBYTE_64BIT;
                    }
                    offset += 1;
                }
                else if ((bytes[offset] & 0xC0) == 0x80) // 10?? ????
                {
                    int shiftedInt = (bytes[offset] & 0x3f) << 8 | bytes[offset + 1];
                    signedInt = shiftedInt >> 1;
                    if ((shiftedInt & 1) != 0)
                        signedInt |= SIGN_MASK_TWOBYTE_64BIT;

                    offset += 2;
                }
                else if ((bytes[offset]) == 0xC1) // 8 byte specifier
                {
                    signedInt = (((long)bytes[offset + 1]) << 56) |
                                (((long)bytes[offset + 2]) << 48) |
                                (((long)bytes[offset + 3]) << 40) |
                                (((long)bytes[offset + 4]) << 32) |
                                (((long)bytes[offset + 5]) << 24) |
                                (((long)bytes[offset + 6]) << 16) |
                                (((long)bytes[offset + 7]) << 8) |
                                ((long)bytes[offset + 8]);
                    offset += 9;
                }
                else
                {
                    signedInt = (((int)bytes[offset + 1]) << 24) |
                                (((int)bytes[offset + 2]) << 16) |
                                (((int)bytes[offset + 3]) << 8) |
                                ((int)bytes[offset + 4]);
                    offset += 5;
                }

                Offset = offset;
                _current = signedInt;
                return true;
            }

            void IDisposable.Dispose() { }
            IEnumerator<long> IEnumerable<long>.GetEnumerator() => this;
            IEnumerator IEnumerable.GetEnumerator() => this;
            void IEnumerator.Reset() => throw new NotImplementedException();
        }

        public static IEnumerable<byte> PgoEncodedCompressedLongGenerator(IEnumerable<long> input)
        {
            foreach (long value in input)
            {
                byte isSigned = 0;
                // This function is modeled on CorSigCompressSignedInt, but differs in that
                // it handles arbitrary int64 values, not just a subset
                if (value < 0)
                    isSigned = 1;

                if ((value & SIGN_MASK_ONEBYTE_64BIT) == 0 || (value & SIGN_MASK_ONEBYTE_64BIT) == SIGN_MASK_ONEBYTE_64BIT)
                {
                    yield return (byte)((byte)((value & ~SIGN_MASK_ONEBYTE_64BIT) << 1 | isSigned));
                }
                else if ((value & SIGN_MASK_TWOBYTE_64BIT) == 0 || (value & SIGN_MASK_TWOBYTE_64BIT) == SIGN_MASK_TWOBYTE_64BIT)
                {
                    int iData = (int)((value & ~SIGN_MASK_TWOBYTE_64BIT) << 1 | isSigned);
                    yield return (byte)((iData >> 8) | 0x80);
                    yield return (byte)(iData & 0xff);
                }
                else if ((value & SIGN_MASK_FOURBYTE_64BIT) == 0 || (value & SIGN_MASK_FOURBYTE_64BIT) == SIGN_MASK_FOURBYTE_64BIT)
                {
                    // Unlike CorSigCompressSignedInt, this just writes a header byte
                    // then 4 bytes, ignoring the whole signed bit detail
                    yield return 0xC0;
                    yield return (byte)((value >> 24) & 0xff);
                    yield return (byte)((value >> 16) & 0xff);
                    yield return (byte)((value >> 8) & 0xff);
                    yield return (byte)((value >> 0) & 0xff);
                }
                else
                {
                    // Unlike CorSigCompressSignedInt, this just writes a header byte
                    // then 8 bytes, ignoring the whole signed bit detail
                    yield return 0xC1;
                    yield return (byte)((value >> 56) & 0xff);
                    yield return (byte)((value >> 48) & 0xff);
                    yield return (byte)((value >> 40) & 0xff);
                    yield return (byte)((value >> 32) & 0xff);
                    yield return (byte)((value >> 24) & 0xff);
                    yield return (byte)((value >> 16) & 0xff);
                    yield return (byte)((value >> 8) & 0xff);
                    yield return (byte)((value >> 0) & 0xff);
                }
            }
        }

        public static IEnumerable<PgoSchemaElem> ParsePgoData<TType, TMethod>(IPgoSchemaDataLoader<TType, TMethod> dataProvider, IEnumerable<long> inputDataStream, bool longsAreCompressed)
        {
            int dataCountToRead = 0;
            PgoSchemaElem curSchema = default(PgoSchemaElem);
            InstrumentationDataProcessingState processingState = InstrumentationDataProcessingState.UpdateProcessMaskFlag;
            long lastDataValue = 0;
            long lastTypeValue = 0;
            long lastMethodValue = 0;

            foreach (long value in inputDataStream)
            {
                if (dataCountToRead > 0)
                {
                    if (curSchema.DataHeldInDataLong)
                    {
                        if (longsAreCompressed)
                            lastDataValue += value;
                        else
                            lastDataValue = value;
                        curSchema.DataLong = lastDataValue;
                    }
                    else
                    {
                        int dataIndex = curSchema.Count - dataCountToRead;
                        switch (curSchema.InstrumentationKind & PgoInstrumentationKind.MarshalMask)
                        {
                            case PgoInstrumentationKind.FourByte:
                                if (longsAreCompressed)
                                    lastDataValue += value;
                                else
                                    lastDataValue = value;
                                ((int[])curSchema.DataObject)[dataIndex] = checked((int)lastDataValue);
                                break;

                            case PgoInstrumentationKind.EightByte:
                                if (longsAreCompressed)
                                    lastDataValue += value;
                                else
                                    lastDataValue = value;
                                ((long[])curSchema.DataObject)[dataIndex] = lastDataValue;
                                break;

                            case PgoInstrumentationKind.TypeHandle:
                                if (longsAreCompressed)
                                    lastTypeValue += value;
                                else
                                    lastTypeValue = value;
                                ((TType[])curSchema.DataObject)[dataIndex] = dataProvider.TypeFromLong(lastTypeValue);
                                break;

                            case PgoInstrumentationKind.MethodHandle:
                                if (longsAreCompressed)
                                    lastMethodValue += value;
                                else
                                    lastMethodValue = value;
                                ((TMethod[])curSchema.DataObject)[dataIndex] = dataProvider.MethodFromLong(lastMethodValue);
                                break;
                        }
                    }
                    dataCountToRead--;
                    if (dataCountToRead == 0)
                    {
                        yield return curSchema;
                        curSchema.DataLong = 0;
                        curSchema.DataObject = null;
                    }
                    continue;
                }

                if (processingState == InstrumentationDataProcessingState.UpdateProcessMaskFlag)
                {
                    processingState = (InstrumentationDataProcessingState)value;
                    continue;
                }

                if ((processingState & InstrumentationDataProcessingState.ILOffset) == InstrumentationDataProcessingState.ILOffset)
                {
                    if (longsAreCompressed)
                        curSchema.ILOffset = checked((int)(value + (long)curSchema.ILOffset));
                    else
                        curSchema.ILOffset = checked((int)value);

                    processingState &= ~InstrumentationDataProcessingState.ILOffset;
                }
                else if ((processingState & InstrumentationDataProcessingState.Type) == InstrumentationDataProcessingState.Type)
                {
                    if (longsAreCompressed)
                        curSchema.InstrumentationKind = (PgoInstrumentationKind)(((int)(curSchema.InstrumentationKind)) + checked((int)value));
                    else
                        curSchema.InstrumentationKind = (PgoInstrumentationKind)value;

                    processingState &= ~InstrumentationDataProcessingState.Type;
                }
                else if ((processingState & InstrumentationDataProcessingState.Count) == InstrumentationDataProcessingState.Count)
                {
                    if (longsAreCompressed)
                        curSchema.Count = checked((int)(value + (long)curSchema.Count));
                    else
                        curSchema.Count = checked((int)value);
                    processingState &= ~InstrumentationDataProcessingState.Count;
                }
                else if ((processingState & InstrumentationDataProcessingState.Other) == InstrumentationDataProcessingState.Other)
                {
                    if (longsAreCompressed)
                        curSchema.Other = checked((int)(value + (long)curSchema.Other));
                    else
                        curSchema.Other = checked((int)value);
                    processingState &= ~InstrumentationDataProcessingState.Other;
                }

                if (processingState == InstrumentationDataProcessingState.Done)
                {
                    processingState = InstrumentationDataProcessingState.UpdateProcessMaskFlag;

                    if (curSchema.InstrumentationKind == PgoInstrumentationKind.Done)
                    {
                        yield break;
                    }

                    switch (curSchema.InstrumentationKind & PgoInstrumentationKind.MarshalMask)
                    {
                        case PgoInstrumentationKind.None:
                            yield return curSchema;
                            break;

                        case PgoInstrumentationKind.FourByte:
                            if (curSchema.Count > 1)
                            {
                                curSchema.DataObject = new int[curSchema.Count];
                            }
                            dataCountToRead = curSchema.Count;
                            break;
                        case PgoInstrumentationKind.EightByte:
                            if (curSchema.Count > 1)
                            {
                                curSchema.DataObject = new long[curSchema.Count];
                            }
                            dataCountToRead = curSchema.Count;
                            break;
                        case PgoInstrumentationKind.TypeHandle:
                            curSchema.DataObject = new TType[curSchema.Count];
                            dataCountToRead = curSchema.Count;
                            break;
                        case PgoInstrumentationKind.MethodHandle:
                            curSchema.DataObject = new TMethod[curSchema.Count];
                            dataCountToRead = curSchema.Count;
                            break;
                        default:
                            throw new Exception("Unknown Type");
                    }
                }
            }

            throw new Exception("Partial Instrumentation Data");
        }

        public static void EncodePgoData<TType, TMethod>(IEnumerable<PgoSchemaElem> schemas, IPgoEncodedValueEmitter<TType, TMethod> valueEmitter, bool emitAllElementsUnconditionally)
        {
            PgoSchemaElem prevSchema = default(PgoSchemaElem);
            TType prevEmittedType = default(TType);
            TMethod prevEmittedMethod = default(TMethod);
            long prevEmittedIntData = 0;

            foreach (PgoSchemaElem schema in schemas)
            {
                int ilOffsetDiff = schema.ILOffset - prevSchema.ILOffset;
                int OtherDiff = schema.Other - prevSchema.Other;
                int CountDiff = schema.Count - prevSchema.Count;
                int TypeDiff = (int)schema.InstrumentationKind - (int)prevSchema.InstrumentationKind;

                InstrumentationDataProcessingState modifyMask = (InstrumentationDataProcessingState)0;

                if (!emitAllElementsUnconditionally)
                {
                    if (ilOffsetDiff != 0)
                        modifyMask |= InstrumentationDataProcessingState.ILOffset;
                    if (TypeDiff != 0)
                        modifyMask |= InstrumentationDataProcessingState.Type;
                    if (CountDiff != 0)
                        modifyMask |= InstrumentationDataProcessingState.Count;
                    if (OtherDiff != 0)
                        modifyMask |= InstrumentationDataProcessingState.Other;
                }
                else
                {
                    modifyMask = InstrumentationDataProcessingState.ILOffset |
                                 InstrumentationDataProcessingState.Type |
                                 InstrumentationDataProcessingState.Count |
                                 InstrumentationDataProcessingState.Other;
                }

                Debug.Assert(modifyMask != InstrumentationDataProcessingState.Done);

                valueEmitter.EmitLong((long)modifyMask, 0);

                if ((modifyMask & InstrumentationDataProcessingState.ILOffset) == InstrumentationDataProcessingState.ILOffset)
                    valueEmitter.EmitLong(schema.ILOffset, prevSchema.ILOffset);
                if ((modifyMask & InstrumentationDataProcessingState.Type) == InstrumentationDataProcessingState.Type)
                    valueEmitter.EmitLong((long)schema.InstrumentationKind, (long)prevSchema.InstrumentationKind);
                if ((modifyMask & InstrumentationDataProcessingState.Count) == InstrumentationDataProcessingState.Count)
                    valueEmitter.EmitLong(schema.Count, prevSchema.Count);
                if ((modifyMask & InstrumentationDataProcessingState.Other) == InstrumentationDataProcessingState.Other)
                    valueEmitter.EmitLong(schema.Other, prevSchema.Other);

                for (int i = 0; i < schema.Count; i++)
                {
                    PgoInstrumentationKind marshal = schema.InstrumentationKind & PgoInstrumentationKind.MarshalMask;
                    switch (marshal)
                    {
                        case PgoInstrumentationKind.None:
                            break;
                        case PgoInstrumentationKind.FourByte:
                            {
                                long valueToEmit;
                                if (schema.Count == 1)
                                {
                                    valueToEmit = schema.DataLong;
                                }
                                else
                                {
                                    valueToEmit = ((int[])schema.DataObject)[i];
                                }
                                valueEmitter.EmitLong(valueToEmit, prevEmittedIntData);
                                prevEmittedIntData = valueToEmit;
                                break;
                            }
                        case PgoInstrumentationKind.EightByte:
                            {
                                long valueToEmit;
                                if (schema.Count == 1)
                                {
                                    valueToEmit = schema.DataLong;
                                }
                                else
                                {
                                    valueToEmit = ((long[])schema.DataObject)[i];
                                }
                                valueEmitter.EmitLong(valueToEmit, prevEmittedIntData);
                                prevEmittedIntData = valueToEmit;
                                break;
                            }
                        case PgoInstrumentationKind.TypeHandle:
                            {
                                TType typeToEmit = ((TType[])schema.DataObject)[i];
                                valueEmitter.EmitType(typeToEmit, prevEmittedType);
                                prevEmittedType = typeToEmit;
                                break;
                            }
                        case PgoInstrumentationKind.MethodHandle:
                            {
                                TMethod methodToEmit = ((TMethod[])schema.DataObject)[i];
                                valueEmitter.EmitMethod(methodToEmit, prevEmittedMethod);
                                prevEmittedMethod = methodToEmit;
                                break;
                            }
                        default:
                            throw new ArgumentException("Unknown schema marshal " + marshal);
                    }
                }

                prevSchema = schema;
            }

            // Emit a "done" schema
            if (!valueEmitter.EmitDone())
            {
                // If EmitDone returns true, no further data needs to be encoded.
                // Otherwise, emit a "Done" schema
                valueEmitter.EmitLong((long)InstrumentationDataProcessingState.Type, 0);
                valueEmitter.EmitLong((long)PgoInstrumentationKind.Done, (long)prevSchema.InstrumentationKind);
            }
        }


        private sealed class PgoSchemaMergeComparer : IComparer<PgoSchemaElem>, IEqualityComparer<PgoSchemaElem>
        {
            public static PgoSchemaMergeComparer Singleton = new PgoSchemaMergeComparer();

            public int Compare(PgoSchemaElem x, PgoSchemaElem y)
            {
                if (x.ILOffset != y.ILOffset)
                {
                    return x.ILOffset.CompareTo(y.ILOffset);
                }
                PgoInstrumentationKind xdescr = x.InstrumentationKind & PgoInstrumentationKind.DescriptorMask;
                PgoInstrumentationKind ydescr = y.InstrumentationKind & PgoInstrumentationKind.DescriptorMask;
                if (xdescr != ydescr)
                {
                    return xdescr.CompareTo(ydescr);
                }
                // We usually merge the Other field, except for edges, where we take care only to merge
                // edges with equal ILOffset _and_ equal Other fields.
                if ((x.InstrumentationKind == PgoInstrumentationKind.EdgeIntCount || x.InstrumentationKind == PgoInstrumentationKind.EdgeLongCount)
                    && x.Other != y.Other)
                {
                    return x.Other.CompareTo(y.Other);
                }

                return 0;
            }

            public bool Equals(PgoSchemaElem x, PgoSchemaElem y)
                => Compare(x, y) == 0;
            int IEqualityComparer<PgoSchemaElem>.GetHashCode(PgoSchemaElem obj) => obj.ILOffset ^ ((int)(obj.InstrumentationKind & PgoInstrumentationKind.DescriptorMask) << 20);
        }

        public static PgoSchemaElem[] Merge<TType, TMethod>(ReadOnlySpan<PgoSchemaElem[]> schemasToMerge)
        {
            {
                // The merging algorithm will sort the schema data by iloffset, then by InstrumentationKind
                // From there each individual instrumentation kind shall have a specific merging rule
                Dictionary<PgoSchemaElem, PgoSchemaElem> dataMerger = new Dictionary<PgoSchemaElem, PgoSchemaElem>(PgoSchemaMergeComparer.Singleton);

                foreach (PgoSchemaElem[] schemaSet in schemasToMerge)
                {
                    bool foundNumRuns = false;

                    foreach (PgoSchemaElem schema in schemaSet)
                    {
                        if (schema.InstrumentationKind == PgoInstrumentationKind.NumRuns)
                            foundNumRuns = true;
                        MergeInSchemaElem(dataMerger, schema);
                    }

                    if (!foundNumRuns)
                    {
                        PgoSchemaElem oneRunSchema = default(PgoSchemaElem);
                        oneRunSchema.InstrumentationKind = PgoInstrumentationKind.NumRuns;
                        oneRunSchema.ILOffset = 0;
                        oneRunSchema.Other = 1;
                        oneRunSchema.Count = 1;
                        MergeInSchemaElem(dataMerger, oneRunSchema);
                    }
                }

                PgoSchemaElem[] result = new PgoSchemaElem[dataMerger.Count];
                dataMerger.Values.CopyTo(result, 0);
                Array.Sort(result, PgoSchemaMergeComparer.Singleton);
                return result;
            }

            static void MergeInSchemaElem(Dictionary<PgoSchemaElem, PgoSchemaElem> dataMerger, PgoSchemaElem schema)
            {
                if (dataMerger.TryGetValue(schema, out var existingSchemaItem))
                {
                    // Actually merge two schema items
                    PgoSchemaElem mergedElem = existingSchemaItem;

                    switch (existingSchemaItem.InstrumentationKind)
                    {
                        case PgoInstrumentationKind.BasicBlockIntCount:
                        case PgoInstrumentationKind.BasicBlockLongCount:
                        case PgoInstrumentationKind.EdgeIntCount:
                        case PgoInstrumentationKind.EdgeLongCount:
                        case PgoInstrumentationKind.HandleHistogramIntCount:
                        case PgoInstrumentationKind.HandleHistogramLongCount:
                            if ((existingSchemaItem.Count != 1) || (schema.Count != 1))
                            {
                                throw new Exception("Unable to merge pgo data. Invalid format");
                            }
                            mergedElem.DataLong = existingSchemaItem.DataLong + schema.DataLong;
                            break;

                        case PgoInstrumentationKind.HandleHistogramTypes:
                            {
                                mergedElem.Count = existingSchemaItem.Count + schema.Count;
                                TType[] newMergedTypeArray = new TType[mergedElem.Count];
                                mergedElem.DataObject = newMergedTypeArray;
                                int i = 0;
                                foreach (TType type in (TType[])existingSchemaItem.DataObject)
                                {
                                    newMergedTypeArray[i++] = type;
                                }
                                foreach (TType type in (TType[])schema.DataObject)
                                {
                                    newMergedTypeArray[i++] = type;
                                }
                                break;
                            }

                        case PgoInstrumentationKind.HandleHistogramMethods:
                            {
                                mergedElem.Count = existingSchemaItem.Count + schema.Count;
                                TMethod[] newMergedMethodArray = new TMethod[mergedElem.Count];
                                mergedElem.DataObject = newMergedMethodArray;
                                int i = 0;
                                foreach (TMethod meth in (TMethod[])existingSchemaItem.DataObject)
                                {
                                    newMergedMethodArray[i++] = meth;
                                }
                                foreach (TMethod meth in (TMethod[])schema.DataObject)
                                {
                                    newMergedMethodArray[i++] = meth;
                                }
                                break;
                            }

                        case PgoInstrumentationKind.Version:
                            {
                                mergedElem.Other = Math.Max(existingSchemaItem.Other, schema.Other);
                                break;
                            }

                        case PgoInstrumentationKind.NumRuns:
                            {
                                mergedElem.Other = existingSchemaItem.Other + schema.Other;
                                break;
                            }
                    }

                    Debug.Assert(PgoSchemaMergeComparer.Singleton.Compare(schema, mergedElem) == 0);
                    Debug.Assert(PgoSchemaMergeComparer.Singleton.Equals(schema, mergedElem));
                    dataMerger[mergedElem] = mergedElem;
                }
                else
                {
                    dataMerger.Add(schema, schema);
                }
            }
        }
    }
}