File: cm_program.cpp

package info (click to toggle)
intel-media-driver 18.4.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 77,144 kB
  • sloc: cpp: 784,288; ansic: 95,944; asm: 42,125; python: 353; sh: 156; makefile: 15
file content (960 lines) | stat: -rw-r--r-- 33,415 bytes parent folder | download | duplicates (2)
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
* Copyright (c) 2017, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//!
//! \file      cm_program.cpp 
//! \brief     Contains Class CmProgram definitions 
//!

#include "cm_program.h"

#include "cm_device_rt.h"
#include "cm_mem.h"
#include "cm_hal.h"

#if USE_EXTENSION_CODE
#include "cm_hw_debugger.h"
#endif

#define READ_FIELD_FROM_BUF( dst, type ) \
    dst = *((type *) &buf[bytePos]); \
    bytePos += sizeof(type);

#define CM_RT_JITTER_DEBUG_FLAG "-debug"
#define CM_RT_JITTER_NCSTATELESS_FLAG "-ncstateless"

#define CM_RT_JITTER_MAX_NUM_FLAGS      30
#define CM_RT_JITTER_NUM_RESERVED_FLAGS 3  // one for gtpin;  two for hw stepping info.
#define CM_RT_JITTER_MAX_NUM_USER_FLAGS (CM_RT_JITTER_MAX_NUM_FLAGS - CM_RT_JITTER_NUM_RESERVED_FLAGS)

namespace CMRT_UMD
{
//*-----------------------------------------------------------------------------
//| Purpose:    Create Cm Program
//| Arguments :
//|               device            [in]     Pointer to Cm Device
//|               commonISACode    [in]     Pointer to memory where common isa locates
//|               size              [in]     Size of memory
//|               pProgram          [in]     Reference to pointer to CmProgram
//|               options           [in]     jitter or non-jitter
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::Create( CmDeviceRT* device, void* cisaCode, const uint32_t cisaCodeSize, CmProgramRT*& pProgram,  const char* options, const uint32_t programId )
{
    int32_t result = CM_SUCCESS;
    pProgram = new (std::nothrow) CmProgramRT( device, programId );
    if( pProgram )
    {
        pProgram->Acquire();
        result = pProgram->Initialize( cisaCode, cisaCodeSize, options );
        if( result != CM_SUCCESS )
        {
            CmProgramRT::Destroy( pProgram);
        }
    }
    else
    {
        CM_ASSERTMESSAGE("Error: Failed to create CmProgram due to out of system memory.");
        result = CM_OUT_OF_HOST_MEMORY;
    }
    return result;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Destroy Cm Program
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::Destroy( CmProgramRT* &program )
{
    long refCount = program->SafeRelease(  );
    if( refCount == 0 )
    {
        program = nullptr;
    }
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Acquire: Increase refcount
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::Acquire( void  )
{
    m_refCount++;
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    SafeRelease:
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::SafeRelease( void )
{
    --m_refCount;
    if( m_refCount == 0 )
    {
        delete this;
        return 0;
    }
    return m_refCount;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Constructor of Cm Program
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
CmProgramRT::CmProgramRT( CmDeviceRT* device, uint32_t programId ):
    m_device( device ),
    m_programCodeSize( 0 ),
    m_programCode(nullptr),
    m_isaFile(nullptr),
    m_options( nullptr ),
    m_surfaceCount( 0 ),
    m_kernelCount( 0 ),
    m_kernelInfo( CM_INIT_KERNEL_PER_PROGRAM ),
    m_isJitterEnabled(false),
    m_isHwDebugEnabled(false),
    m_refCount(0),
    m_programIndex(programId),
    m_fJITCompile(nullptr),
    m_fJITCompile_v2(nullptr),
    m_fFreeBlock(nullptr),
    m_fJITVersion(nullptr),
    m_cisaMagicNumber(0),
    m_cisaMajorVersion(0),
    m_cisaMinorVersion(0)
{
    CmSafeMemSet(m_isaFileName,0,sizeof(char)*CM_MAX_ISA_FILE_NAME_SIZE_IN_BYTE);
}

//*-----------------------------------------------------------------------------
//| Purpose:    Destructor of Cm Program
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
CmProgramRT::~CmProgramRT( void )
{
    MosSafeDeleteArray( m_options );
    MosSafeDeleteArray( m_programCode );
    for( uint32_t i = 0; i < m_kernelCount; i ++ )
    {
        uint32_t refCount = this->ReleaseKernelInfo(i);
        CM_ASSERT(refCount == 0);

    }
    m_kernelInfo.Delete();
    CmSafeDelete(m_isaFile);
}

#if (_RELEASE_INTERNAL)
int32_t CmProgramRT::ReadUserFeatureValue(const char *pcMessageKey, uint32_t &value)
{
    MOS_USER_FEATURE        userFeature;
    MOS_USER_FEATURE_VALUE  userFeatureValue = __NULL_USER_FEATURE_VALUE__;
    CM_RETURN_CODE          hr = CM_SUCCESS;

    // Set the component's message level and asserts:
    userFeatureValue.u32Data    = __MOS_USER_FEATURE_KEY_MESSAGE_DEFAULT_VALUE;
    userFeature.Type            = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath           = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues         = &userFeatureValue;
    userFeature.uiNumValues     = 1;

    CM_CHK_MOSSTATUS_GOTOFINISH_CMERROR(MOS_UserFeature_ReadValue(
                                  nullptr,
                                  &userFeature,
                                  pcMessageKey,
                                  MOS_USER_FEATURE_VALUE_TYPE_UINT32));

    value = userFeature.pValues->u32Data;

finish:
    return hr;
}
#endif

//*-----------------------------------------------------------------------------
//| Purpose:    Initialize Cm Program
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::Initialize( void* cisaCode, const uint32_t cisaCodeSize, const char* options )
{
    bool loadingGPUCopyKernel = false;
    int32_t result = CM_SUCCESS;
    int32_t hr     = CM_FAILURE;

    m_isJitterEnabled = true; //by default jitter is ON

    int numJitFlags = 0;
    const char *jitFlags[CM_RT_JITTER_MAX_NUM_FLAGS];
    CmSafeMemSet(jitFlags, 0, sizeof(char *) * CM_RT_JITTER_MAX_NUM_FLAGS);

    char* flagStepInfo = nullptr;

    if( options )
    {
        size_t length = strnlen( options, CM_MAX_OPTION_SIZE_IN_BYTE );
        if(length >= CM_MAX_OPTION_SIZE_IN_BYTE)
        {
            CM_ASSERTMESSAGE("Error: option size is too long.");
            return CM_INVALID_ARG_VALUE;
        }
        else
        {
            m_options = MOS_NewArray(char, (length + 1));
            if( !m_options )
            {
                CM_ASSERTMESSAGE("Error: Out of system memory.");
                return CM_OUT_OF_HOST_MEMORY;

            }
            CmSafeMemCopy( m_options, options, length);
            m_options[ length ] = '\0';

            if(strstr(options, "nojitter"))
                m_isJitterEnabled = false;

            if(!strcmp(m_options, "PredefinedGPUKernel"))
                loadingGPUCopyKernel = true;
            if( (m_isJitterEnabled == true) && (loadingGPUCopyKernel == false))
            { // option: "nonjitter" and "PredefinedGPUCopyKernel" should not be passed to jitter
                char *token = nullptr;
                char *nextToken = nullptr;
                char *ptr =  m_options;

                while( nullptr != (token = strtok_s(ptr," ",&nextToken)))
                {
                    if(numJitFlags >= CM_RT_JITTER_MAX_NUM_USER_FLAGS)
                    {
                        CM_ASSERTMESSAGE("Error: Invalid jitter user flags number.");
                        MosSafeDeleteArray(m_options);
                        return CM_FAILURE;
                    }
                    if(!strcmp(token, CM_RT_JITTER_DEBUG_FLAG))
                    {
                        m_isHwDebugEnabled = true;
                    }
                    jitFlags[numJitFlags] = token;
                    numJitFlags++;
                    ptr = nextToken;
                }
            }
        }
    }

    uint8_t *buf = (uint8_t*)cisaCode;
    uint32_t bytePos = 0;

    READ_FIELD_FROM_BUF(m_cisaMagicNumber, uint32_t);
    READ_FIELD_FROM_BUF(m_cisaMajorVersion, uint8_t);
    READ_FIELD_FROM_BUF(m_cisaMinorVersion, uint8_t);

    bool useVisaApi = true;
    vISA::Header *header = nullptr;

    auto getVersionAsInt = [](int major, int minor) {return major * 100 + minor;};
    if (getVersionAsInt(m_cisaMajorVersion, m_cisaMinorVersion) < getVersionAsInt(3, 2))
    {
        useVisaApi = false;
    }
    else
    {
        m_isaFile = new vISA::ISAfile((uint8_t*)cisaCode, cisaCodeSize);
        if (!m_isaFile->readFile())
        {
            CM_ASSERTMESSAGE("Error: invalid VISA.");
            MosSafeDeleteArray(m_options);
            return CM_INVALID_COMMON_ISA;
        }
        header = m_isaFile->getHeader();
    }

    if (m_cisaMagicNumber != CISA_MAGIC_NUMBER)
    {
        CM_ASSERTMESSAGE("Error: Invalid CISA magic number.");
        MosSafeDeleteArray(m_options);
        return CM_INVALID_COMMON_ISA;
    }

    if(loadingGPUCopyKernel)//for predefined kernel(GPUCopy), forcely disable jitting
    {
        m_isJitterEnabled = false;
    }

    const char *platform = nullptr;

    PCM_HAL_STATE  cmHalState = \
        ((PCM_CONTEXT_DATA)m_device->GetAccelData())->cmHalState;
    CM_CHK_NULL_GOTOFINISH_CMERROR(cmHalState);

    cmHalState->cmHalInterface->GetGenPlatformInfo(nullptr, nullptr, &platform);

    if( m_isJitterEnabled )
    {
    //reg control for svm IA/GT cache coherence
#if (_RELEASE_INTERNAL)
        uint32_t value = 0;
        if (ReadUserFeatureValue(CM_RT_USER_FEATURE_FORCE_COHERENT_STATELESSBTI, value) == CM_SUCCESS && value == 1)
        {
            jitFlags[numJitFlags] = CM_RT_JITTER_NCSTATELESS_FLAG;
            numJitFlags++;
        }

#endif // (_RELEASE_INTERNAL)

        //Load jitter library and add function pointers to program
        // Get hmodule from CmDevice_RT or CmDevice_Sim, which is casted from CmDevice
        result = m_device->LoadJITDll();
        if(result != CM_SUCCESS)
        {
            CM_ASSERTMESSAGE("Error: Load jitter library failure.");
            MosSafeDeleteArray(m_options);
            return result;
        }

        m_device->GetJITCompileFnt(m_fJITCompile);
        m_device->GetJITCompileFntV2(m_fJITCompile_v2);
        m_device->GetFreeBlockFnt(m_fFreeBlock);
        m_device->GetJITVersionFnt(m_fJITVersion);

        uint32_t jitMajor = 0;
        uint32_t jitMinor = 0;
        m_fJITVersion(jitMajor, jitMinor);
        if((jitMajor < m_cisaMajorVersion) || (jitMajor == m_cisaMajorVersion && jitMinor < m_cisaMinorVersion))
            return CM_JITDLL_OLDER_THAN_ISA;
#if USE_EXTENSION_CODE
        if( m_device->CheckGTPinEnabled() && !loadingGPUCopyKernel)
        {
            hr = InitForGTPin(jitFlags, numJitFlags);
            if (hr != CM_SUCCESS)
            {
                goto finish;
            }
        }
#endif
        //Pass stepping info to JITTER..."
        char *stepstr = nullptr;
        m_device->GetGenStepInfo(stepstr);
        if(stepstr != nullptr)
        {
            flagStepInfo = MOS_NewArray(char, CM_JIT_FLAG_SIZE);
            if (flagStepInfo)
            {
                jitFlags[numJitFlags] = "-stepping";
                numJitFlags++;

                CmSafeMemSet(flagStepInfo, 0, CM_JIT_FLAG_SIZE);
                MOS_SecureStringPrint(flagStepInfo, CM_JIT_FLAG_SIZE, CM_JIT_FLAG_SIZE, "%s", stepstr);
                if (numJitFlags >= CM_RT_JITTER_MAX_NUM_FLAGS)
                {
                    CM_ASSERTMESSAGE("Error: Invalid jitter user flags number.");
                    hr = CM_FAILURE;
                    goto finish;
                }
                jitFlags[numJitFlags] = flagStepInfo;
                numJitFlags++;
            }
            else
            {
                CM_ASSERTMESSAGE("Error: Out of system memory.");
                MosSafeDeleteArray(m_options);
                return CM_OUT_OF_HOST_MEMORY;
            }
        }
    }

    if (useVisaApi)
    {
        m_kernelCount = header->getNumKernels();
    }
    else
    {
        bytePos = 4 + 1 + 1; // m_cisaMagicNumber:sizeof (uint32_t) + m_cisaMajorVersion:sizeof(uint8_t) + m_cisaMinorVersion:sizeof(uint8_t)
        unsigned short numKernels;
        READ_FIELD_FROM_BUF(numKernels, unsigned short);
        m_kernelCount = numKernels;
    }

#ifdef _DEBUG
    if(m_isJitterEnabled)
        CM_NORMALMESSAGE("Jitter Compiling...");
#endif

    for (uint32_t i = 0; i < m_kernelCount; i++)
    {
        CM_KERNEL_INFO* kernInfo = new (std::nothrow) CM_KERNEL_INFO;
        if (!kernInfo)
        {
            CM_ASSERTMESSAGE("Error: Out of system memory.");
            MosSafeDeleteArray(flagStepInfo);
            MosSafeDeleteArray(m_options);
            hr = CM_OUT_OF_HOST_MEMORY;
            goto finish;
        }
        CmSafeMemSet(kernInfo, 0, sizeof(CM_KERNEL_INFO));

        vISA::Kernel *kernel = nullptr;
        uint8_t nameLen = 0;
        if (useVisaApi)
        {
            kernel = header->getKernelInfo()[i];
            nameLen = kernel->getNameLen();
            CmSafeMemCopy(kernInfo->kernelName, kernel->getName(), nameLen);
        }
        else
        {
            READ_FIELD_FROM_BUF(nameLen, uint8_t);
            CmSafeMemCopy(kernInfo->kernelName, buf + bytePos, nameLen);
            // move bytePos to the right index
            bytePos += nameLen;
        }

        if(m_isJitterEnabled)
        {
            if (useVisaApi)
            {
                kernInfo->kernelIsaOffset = kernel->getOffset();
                kernInfo->kernelIsaSize = kernel->getSize();
                kernInfo->inputCountOffset = kernel->getInputOffset();
            }
            else
            {
                uint32_t kernelIsaOffset;
                uint32_t kernelIsaSize;
                uint32_t inputCountOffset;

                READ_FIELD_FROM_BUF(kernelIsaOffset, uint32_t); //read kernel isa offset
                READ_FIELD_FROM_BUF(kernelIsaSize, uint32_t); //read kernel isa size
                READ_FIELD_FROM_BUF(inputCountOffset, uint32_t);

                kernInfo->kernelIsaOffset = kernelIsaOffset;
                kernInfo->kernelIsaSize = kernelIsaSize;
                kernInfo->inputCountOffset = inputCountOffset;

                // read relocation symbols
                unsigned short numVarSyms = 0, num_func_syms = 0;
                unsigned char numGenBinaries = 0;

                READ_FIELD_FROM_BUF(numVarSyms, uint16_t);
                // CISA layout of var_syms is symbolic_index[0], resolved_index[0],
                // ...resolved_index[n-1]. Skip all.
                bytePos += sizeof(uint16_t) * numVarSyms;
                bytePos += sizeof(uint16_t) * numVarSyms;

                READ_FIELD_FROM_BUF(num_func_syms, uint16_t);
                // CISA layout of func_syms is symbolic_index[0], resolved_index[0],
                // ...resolved_index[n-1]. Skip all.
                bytePos += sizeof(uint16_t) * num_func_syms;
                bytePos += sizeof(uint16_t) * num_func_syms;

                // numGenBinaries
                READ_FIELD_FROM_BUF(numGenBinaries, uint8_t);
                // CISA layout of gen_binaries is genPlatform[0], binary offset[0],
                // binary size[0]...binary size[n-1]. Skip all.
                // skip all genPlatform
                bytePos += sizeof(uint8_t) * numGenBinaries;
                // skip all binary offset
                bytePos += sizeof(uint32_t) * numGenBinaries;
                // skip all binary size
                bytePos += sizeof(uint32_t) * numGenBinaries;

                // bytePos should point to nameLen of next kernel
            }
        }
        else // non jitting
        {
            uint8_t numGenBinaries = 0;
            if (useVisaApi)
            {
                kernInfo->kernelIsaOffset = kernel->getOffset();
                kernInfo->inputCountOffset = kernel->getInputOffset();
                numGenBinaries = kernel->getNumGenBinaries();
            }
            else
            {
                uint32_t kernelIsaOffset;
                READ_FIELD_FROM_BUF(kernelIsaOffset, uint32_t); //read kernel isa offset
                kernInfo->kernelIsaOffset = kernelIsaOffset;

                // skipping kernelIsaSize
                bytePos += 4;

                uint32_t inputCountOffset;
                READ_FIELD_FROM_BUF(inputCountOffset, uint32_t);
                kernInfo->inputCountOffset = inputCountOffset;

                // read relocation symbols
                unsigned short numVarSyms = 0, num_func_syms = 0;

                READ_FIELD_FROM_BUF(numVarSyms, uint16_t);
                // CISA layout of var_syms is symbolic_index[0], resolved_index[0],
                // ...resolved_index[n-1]. Skip all.
                bytePos += sizeof(uint16_t) * numVarSyms;
                bytePos += sizeof(uint16_t) * numVarSyms;

                READ_FIELD_FROM_BUF(num_func_syms, uint16_t);
                // CISA layout of func_syms is symbolic_index[0], resolved_index[0],
                // ...resolved_index[n-1]. Skip all.
                bytePos += sizeof(uint16_t) * num_func_syms;
                bytePos += sizeof(uint16_t) * num_func_syms;

                // numGenBinaries
                READ_FIELD_FROM_BUF(numGenBinaries, uint8_t);
            }

            uint32_t genxBinaryOffset = 0;
            uint32_t genxBinarySize = 0;
            for (int j = 0; j < numGenBinaries; j++)
            {
                vISA::GenBinary *genBinary = nullptr;
                uint8_t genPlatform = 0;
                uint32_t offset = 0;
                uint32_t size = 0;

                if (useVisaApi)
                {
                    genBinary = kernel->getGenBinaryInfo()[j];
                    genPlatform = genBinary->getGenPlatform();
                    offset = genBinary->getBinaryOffset();
                    size = genBinary->getBinarySize();
                }
                else
                {
                    // genPlatform
                    READ_FIELD_FROM_BUF(genPlatform, uint8_t);
                    // binary offset
                    READ_FIELD_FROM_BUF(offset, uint32_t);
                    // binary size
                    READ_FIELD_FROM_BUF(size, uint32_t);
                }

                if (cmHalState->cmHalInterface->IsCisaIDSupported((uint32_t)genPlatform))
                {
                    // assign correct offset/size based on platform
                    genxBinaryOffset = offset;
                    genxBinarySize = size;
                }
                else
                {
                    MosSafeDeleteArray(flagStepInfo);
                    MosSafeDeleteArray(m_options);
                    CmSafeDelete(kernInfo);
                    return CM_INVALID_GENX_BINARY;
                }
            }

            kernInfo->genxBinaryOffset = genxBinaryOffset;
            kernInfo->genxBinarySize = genxBinarySize;

            if ( kernInfo->genxBinarySize == 0 || kernInfo->genxBinaryOffset == 0 )
            {
                CM_ASSERTMESSAGE("Error: Invalid genx binary.");
                MosSafeDeleteArray(flagStepInfo);
                MosSafeDeleteArray(m_options);
                CmSafeDelete(kernInfo);
                return CM_INVALID_GENX_BINARY;
            }
        }

        if(m_isJitterEnabled)
        {
            kernInfo->jitBinaryCode = 0;
            void* jitBinary = 0;// = (void**)malloc(m_kernelCount*sizeof(void*));
            uint32_t jitBinarySize = 0;// = (uint32_t*)malloc(m_kernelCount*sizeof(uint32_t*));
            char* errorMsg = (char*)malloc(CM_JIT_ERROR_MESSAGE_SIZE);
            if (errorMsg == nullptr)
            {
                CM_ASSERTMESSAGE("Error: Out of system memory.");
                CmSafeDelete(kernInfo);
                hr = CM_OUT_OF_HOST_MEMORY;
                goto finish;
            }
            CmSafeMemSet( errorMsg, 0, CM_JIT_ERROR_MESSAGE_SIZE );

            FINALIZER_INFO *jitProfInfo = (FINALIZER_INFO *)malloc(CM_JIT_PROF_INFO_SIZE);
            if(jitProfInfo == nullptr)
            {
                CM_ASSERTMESSAGE("Error: Out of system memory.");
                free(errorMsg);
                CmSafeDelete(kernInfo);
                hr = CM_OUT_OF_HOST_MEMORY;
                goto finish;
            }
            CmSafeMemSet( jitProfInfo, 0, CM_JIT_PROF_INFO_SIZE );

            void *extra_info = nullptr;
            CmNotifierGroup *notifiers = m_device->GetNotifiers();
            if (notifiers)
            {
                notifiers->NotifyCallingJitter(&extra_info);
            }

            if (m_fJITCompile_v2)
            {
                result = m_fJITCompile_v2( kernInfo->kernelName, (uint8_t*)cisaCode, cisaCodeSize,
                                    jitBinary, jitBinarySize, platform, m_cisaMajorVersion, m_cisaMinorVersion, numJitFlags, jitFlags, errorMsg, jitProfInfo, extra_info );
            }
            else
            {
                result = m_fJITCompile( kernInfo->kernelName, (uint8_t*)cisaCode, cisaCodeSize,
                                    jitBinary, jitBinarySize, platform, m_cisaMajorVersion, m_cisaMinorVersion, numJitFlags, jitFlags, errorMsg, jitProfInfo );
            }

            //if error code returned or error message not nullptr
            if(result != CM_SUCCESS)// || errorMsg[0])
            {
                CM_NORMALMESSAGE("%s.", errorMsg);
                free(errorMsg);
                CmSafeDelete(kernInfo);
                hr = CM_JIT_COMPILE_FAILURE;
                goto finish;
            }

            // if spill code exists and scrach space disabled, return error to user
            if( jitProfInfo->isSpill &&  m_device->IsScratchSpaceDisabled())
            {
                CmSafeDelete(kernInfo);
                free(errorMsg);
                return CM_INVALID_KERNEL_SPILL_CODE;
            }

            free(errorMsg);

            kernInfo->jitBinaryCode = jitBinary;
            kernInfo->jitBinarySize = jitBinarySize;
            kernInfo->jitInfo = jitProfInfo;

#if USE_EXTENSION_CODE
            if ( m_isHwDebugEnabled )
            {
                NotifyKernelBinary(this->m_device,
                                   this,
                                   kernInfo->kernelName,
                                   jitBinary,
                                   jitBinarySize,
                                   jitProfInfo->genDebugInfo,
                                   jitProfInfo->genDebugInfoSize,
                                   nullptr,
                                   m_device->GetDriverStoreFlag());
            }
#endif
        }

        m_kernelInfo.SetElement( i, kernInfo );
        this->AcquireKernelInfo(i);
    }

#ifdef _DEBUG
    if(m_isJitterEnabled)
        CM_NORMALMESSAGE("Jitter Done.");
#endif

    // now bytePos index to the start of common isa body;
    // compute the code size for common isa
    m_programCodeSize = cisaCodeSize;
    m_programCode = MOS_NewArray(uint8_t, m_programCodeSize);
    if( !m_programCode )
    {
        CM_ASSERTMESSAGE("Error: Out of system memory.");
        hr = CM_OUT_OF_HOST_MEMORY;
        goto finish;
    }

    //Copy CISA content
    CmFastMemCopy((void *)m_programCode, cisaCode, cisaCodeSize);

    hr = CM_SUCCESS;

finish:
    MosSafeDeleteArray(flagStepInfo);
    if(hr != CM_SUCCESS )
    {
        MosSafeDeleteArray(m_options);
        MosSafeDeleteArray(m_programCode);
    }
    return hr;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get size and address of Common Isa
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::GetCommonISACode( void* & commonISACode, uint32_t & size )
{
    commonISACode = (void *)m_programCode;
    size = m_programCodeSize;

    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get the count of kernel
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::GetKernelCount( uint32_t& kernelCount )
{
    kernelCount = m_kernelCount;
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get the Kernel's Infomation
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::GetKernelInfo( uint32_t index, CM_KERNEL_INFO*& kernelInfo )
{
    if( index < m_kernelCount )
    {
        kernelInfo = (CM_KERNEL_INFO*)m_kernelInfo.GetElement( index ) ;
        return CM_SUCCESS;
    }
    else
    {
        kernelInfo = nullptr;
        return CM_FAILURE;
    }
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get the name of ISA file
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::GetIsaFileName( char* & isaFileName )
{
    isaFileName = m_isaFileName;
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get Kernel's options
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::GetKernelOptions( char* & kernelOptions )
{
    kernelOptions = m_options;
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Get the number of Surfaces
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
uint32_t CmProgramRT::GetSurfaceCount(void)
{
    return m_surfaceCount;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Set Program's surface count
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
int32_t CmProgramRT::SetSurfaceCount(uint32_t count)
{
    m_surfaceCount = count;
    return CM_SUCCESS;
}

//*-----------------------------------------------------------------------------
//| Purpose:    Acquire Kernel Info
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
uint32_t CmProgramRT::AcquireKernelInfo(uint32_t index)
{
    CM_KERNEL_INFO* kernelInfo = nullptr;

    if( index < m_kernelCount )
    {
        kernelInfo = (CM_KERNEL_INFO *)m_kernelInfo.GetElement( index ) ;
        if (kernelInfo)
        {
            CM_ASSERT( (int32_t)kernelInfo->kernelInfoRefCount >= 0 );
            CM_ASSERT( kernelInfo->kernelInfoRefCount < UINT_MAX );

            ++ kernelInfo->kernelInfoRefCount;
            return kernelInfo->kernelInfoRefCount;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

//*-----------------------------------------------------------------------------
//| Purpose:    Release Kernel Info
//| Returns:    Result of the operation.
//*-----------------------------------------------------------------------------
uint32_t CmProgramRT::ReleaseKernelInfo(uint32_t index)
{
    CM_KERNEL_INFO* kernelInfo = nullptr;

    if( index < m_kernelCount )
    {
        kernelInfo = (CM_KERNEL_INFO *)m_kernelInfo.GetElement( index ) ;
        if (kernelInfo)
        {
            CM_ASSERT( kernelInfo->kernelInfoRefCount > 0 );

            -- kernelInfo->kernelInfoRefCount;

            if (kernelInfo->kernelInfoRefCount == 1)
            {
                /////////////////////////////////////////////////////////////
                //Free global string memory space, Start
                for (int i = 0; i < (int) kernelInfo->globalStringCount; i ++)
                {
                    if (kernelInfo->globalStrings[i])
                    {
                        free((void *)kernelInfo->globalStrings[i]);
                    }
                }
                if (kernelInfo->globalStrings)
                {
                    free((void *)kernelInfo->globalStrings);
                    kernelInfo->globalStrings = nullptr;
                    kernelInfo->globalStringCount = 0;
                }
                //Free global string memory space, End
                /////////////////////////////////////////////////////////////

                for (uint32_t i = 0; i < kernelInfo->surfaceCount; i++) {
                    if (kernelInfo->surface[i].attributeCount && kernelInfo->surface[i].attributes) {
                        free(kernelInfo->surface[i].attributes);
                    }
                }
                if (kernelInfo->surface) {
                    free(kernelInfo->surface);
                    kernelInfo->surface = nullptr;
                    kernelInfo->surfaceCount = 0;
                }

                return 1;
            }

            else if (kernelInfo->kernelInfoRefCount == 0)
            {
                if(m_isJitterEnabled)
                {
                    if(kernelInfo->jitBinaryCode)
                        m_fFreeBlock(kernelInfo->jitBinaryCode);
                    if(kernelInfo->jitInfo)
                    {
                        if (kernelInfo->jitInfo->freeGRFInfo)
                        {
                            m_fFreeBlock(kernelInfo->jitInfo->freeGRFInfo);
                        }
                        free(kernelInfo->jitInfo);
                    }
                }

                /////////////////////////////////////////////////////////////
                //Free global string memory space, Start
                for (int i = 0; i < (int) kernelInfo->globalStringCount; i ++)
                {
                    if (kernelInfo->globalStrings[i])
                    {
                        free((void *)kernelInfo->globalStrings[i]);
                    }
                }
                if (kernelInfo->globalStrings)
                {
                    free((void *)kernelInfo->globalStrings);
                }
                //Free global string memory space, End
                /////////////////////////////////////////////////////////////

                for (uint32_t i = 0; i < kernelInfo->surfaceCount; i++) {
                    if (kernelInfo->surface[i].attributeCount && kernelInfo->surface[i].attributes) {
                        free(kernelInfo->surface[i].attributes);
                    }
                }
                if (kernelInfo->surface) {
                    free(kernelInfo->surface);
                }

                CmSafeDelete( kernelInfo );

                m_kernelInfo.SetElement(index, nullptr);

                return 0;
            }
            else
            {
                return kernelInfo->kernelInfoRefCount;
            }
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

int32_t CmProgramRT::GetKernelInfoRefCount(uint32_t index, uint32_t& refCount)
{
    CM_KERNEL_INFO* kernelInfo = nullptr;

    refCount = 0;

    if( index < m_kernelCount )
    {
        kernelInfo =(CM_KERNEL_INFO *) m_kernelInfo.GetElement( index ) ;
        if (kernelInfo)
        {
            refCount = kernelInfo->kernelInfoRefCount;
            return CM_SUCCESS;
        }
        else
        {
            return CM_FAILURE;
        }
    }
    else
    {
        return CM_FAILURE;
    }
}

int32_t CmProgramRT::GetCISAVersion(uint32_t& majorVersion, uint32_t& minorVersion)
{
    majorVersion = m_cisaMajorVersion;
    minorVersion = m_cisaMinorVersion;

    return CM_SUCCESS;
}

uint32_t CmProgramRT::GetProgramIndex()
{
    return m_programIndex;
}

vISA::ISAfile *CmProgramRT::getISAfile()
{
    return m_isaFile;
}
}