File: LLVMUtils.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (806 lines) | stat: -rw-r--r-- 31,180 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/PassTimer.hpp"
#include "Compiler/CISACodeGen/TimeStatsCounter.h"
#include "common/Stats.hpp"
#include "common/debug/Dump.hpp"
#include "common/shaderOverride.hpp"
#include "common/IntrinsicAnnotator.hpp"
#include "common/LLVMUtils.h"
#include <iomanip>
#include <iostream>
#include <unordered_map>
#include <string>

#include "common/LLVMWarningsPush.hpp"
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include <llvmWrapper/ADT/StringRef.h>
#include "common/LLVMWarningsPop.hpp"
#include <llvm/IR/IRPrintingPasses.h>

using namespace IGC;
using namespace IGC::Debug;
using namespace llvm;

bool getPassToggles(std::bitset<1024>& toggles)
{
    const char* passToggles = IGC_GET_REGKEYSTRING(DisablePassToggles);
    if (passToggles != nullptr && strlen(passToggles) > 0)
    {
        std::string szBin;
        std::string szHexLL;
        unsigned int len = 0;
        unsigned long long x = 0;
        std::string szHex = passToggles;
        for (size_t i = 0; i < szHex.size(); i += 16)
        {
            szHexLL = szHex.substr(i, 16);
            len = szHexLL.size() * 4;
            x = std::stoull(szHexLL, nullptr, 16);
            szBin += std::bitset<64>(x).to_string().substr(64 - len, len);
        }

        toggles = std::bitset<1024>(szBin);
        return true;
    }

    return false;
}

llvm::Pass* createFlushPass(llvm::Pass* pass, Dump& dump)
{
    // Choose an appropriate pass to preserve the original order of pass execution in the pass manager
    switch (pass->getPassKind())
    {
    case PT_Function:
        return new FunctionFlushDumpPass(dump);
    case PT_Module:
        return new ModuleFlushDumpPass(dump);
    case PT_Region:
    case PT_Loop:
    case PT_CallGraphSCC:
        // flushing for analysis passes is not required
        break;
    case PT_PassManager:
    default:
        // internal pass managers are not considered in the context
        break;
    }
    return nullptr;
}

/*
ShaderPassDisable
The syntax is as follows:
ShaderPassDisable="TOKEN1;TOKEN2;..."

TOKEN:
* Pass number                           PASS_NUMBER                                 example: 14
* Range                                 PASS_NUMBER_START-PASS_NUMBER_STOP          example: 10-50
* Open range                            PASS_NUMBER_START-                          example: 60-
* Pass name                             PASS_NAME                                   example: BreakConstantExprPass
* Specific pass occurrence              PASS_NAME:occurrence                        example: BreakConstantExprPass:0
* Specific pass occurrences range       PASS_NAME:OCCURRANCE_START-OCCURRANCE_STOP  example: BreakConstantExprPass:2-4
* Specific pass occurrences open range  PASS_NAME:OCCURRANCE_START-                 example: BreakConstantExprPass:3-

Example:
IGC_ShaderPassDisable="9;17-19;239-;Error Check;ResolveOCLAtomics:2;Dead Code Elimination:3-5;BreakConstantExprPass:7-"

You want to skip pass ID 9
You want to skip passes from 17 to 19
You want to skip passes from 239 to end
You want to skip all occurrence of Error Check
You want to skip pass ResolveOCLAtomics occurrence 2
You want to skip pass Dead Code Elimination from occurrence 3 to occurrence 5
You want to skip pass BreakConstantExprPass from occurrence 7 to end

Pass number    Pass name                                         Pass occurrence     skippedBy
9              LowerInvokeSIMD                                   1                   PassNumber
17             CorrectlyRoundedDivSqrt                           1                   Range
18             CodeAssumption                                    1                   Range
19             JointMatrixFuncsResolutionPass                    1                   Range
32             Error Check                                       1                   PassName
65             ResolveOCLAtomics                                 2                   SpecificPassOccurrence
75             Dead Code Elimination                             3                   SpecificPassOccurrenceRange
77             Error Check                                       2                   PassName
121            Dead Code Elimination                             4                   SpecificPassOccurrenceRange
143            Dead Code Elimination                             5                   SpecificPassOccurrenceRange
145            BreakConstantExprPass                             7                   SpecificPassOccurrenceOpenRange
181            BreakConstantExprPass                             8                   SpecificPassOccurrenceOpenRange
183            BreakConstantExprPass                             9                   SpecificPassOccurrenceOpenRange
189            BreakConstantExprPass                             10                  SpecificPassOccurrenceOpenRange
214            BreakConstantExprPass                             11                  SpecificPassOccurrenceOpenRange
221            BreakConstantExprPass                             12                  SpecificPassOccurrenceOpenRange
239            Layout                                            1                   OpenRange
240            TimeStatsCounter Start/Stop                       6                   OpenRange
241            EmitPass                                          1                   OpenRange
242            EmitPass                                          2                   OpenRange
243            EmitPass                                          3                   OpenRange
244            DebugInfoPass                                     1                   OpenRange
*/
enum TokenSkipCase { PassNumber, Range, OpenRange, PassName, SpecificPassOccurrence, SpecificPassOccurrenceRange, SpecificPassOccurrenceOpenRange, WrongFormat };

struct SkipCommand
{
    std::string name;
    unsigned int start, end;
    TokenSkipCase skippedBy;

    SkipCommand(std::string _name, unsigned _start, unsigned _end, TokenSkipCase _skippedBy)
        : name(_name), start(_start), end(_end), skippedBy(_skippedBy)
    {}

    SkipCommand(std::string _name) : name(_name)
    {
        start = std::numeric_limits<unsigned int>::max();
        end = std::numeric_limits<unsigned int>::max();
        skippedBy = TokenSkipCase::PassName;
    }

    SkipCommand(unsigned _start, unsigned _end) : start(_start), end(_end)
    {
        name = "";
        skippedBy = TokenSkipCase::Range;
    }

    SkipCommand(unsigned _start) : start(_start), end(_start)
    {
        name = "";
        skippedBy = TokenSkipCase::PassNumber;
    }
};

struct PassDisableConfig
{
    std::unordered_map<std::string, unsigned int>        passesOccuranceCount;
    std::vector<SkipCommand>                             skipCommands;
    unsigned int                                         passCount = 0;

    PassDisableConfig();
};

static bool tryParsePassNumber(PassDisableConfig& pdc, const std::string& Token)
{
    // remove spaces to check if other chars is only digit
    std::string tempTokenWithoutSpaces = Token;
    tempTokenWithoutSpaces.erase(remove(tempTokenWithoutSpaces.begin(), tempTokenWithoutSpaces.end(), ' '), tempTokenWithoutSpaces.end());
    if (std::all_of(tempTokenWithoutSpaces.cbegin(), tempTokenWithoutSpaces.cend(), ::isdigit))
    {
        std::cerr << "You want to skip pass ID " << Token << std::endl;
        unsigned int passNumber = std::stoi(Token);
        auto localSkipComand = SkipCommand(passNumber);
        pdc.skipCommands.push_back(localSkipComand);
        // move to next Token
        return true;
    }
    // continue to parse this Token
    return false;
}

static bool checkSkipCommandBounds(unsigned int from, unsigned int to)
{
    if (from > to)
    {
        std::cerr << "You want to skip passes from " << from << " to " << to
            << " but start of skipping is bigger than end of skipping" << std::endl;
        return false;
    }
    return true;
}

static SkipCommand* getFirstCommand(PassDisableConfig& pdc, TokenSkipCase commandCase, const std::string& passName = "")
{
    auto it = find_if(pdc.skipCommands.begin(), pdc.skipCommands.end(),
        [&passName, &commandCase](SkipCommand& sc)
        {
            return sc.skippedBy == commandCase && (passName.empty() || sc.name == passName);
        });

    return it == pdc.skipCommands.end() ? nullptr : &(*it);
}

static bool tryParsePassRange(PassDisableConfig& pdc, const std::string& Token)
{
    // remove '-' and spaces to check if other chars is only digit
    std::string tempTokenWithoutDashAndSpaces = Token;
    tempTokenWithoutDashAndSpaces.erase(remove(tempTokenWithoutDashAndSpaces.begin(), tempTokenWithoutDashAndSpaces.end(), '-'), tempTokenWithoutDashAndSpaces.end());
    tempTokenWithoutDashAndSpaces.erase(remove(tempTokenWithoutDashAndSpaces.begin(), tempTokenWithoutDashAndSpaces.end(), ' '), tempTokenWithoutDashAndSpaces.end());
    if (!std::all_of(tempTokenWithoutDashAndSpaces.cbegin(), tempTokenWithoutDashAndSpaces.cend(), ::isdigit))
    {
        // continue to parse this Token
        return false;
    }

    std::istringstream tokenAsStringStream(Token);
    std::string subToken;
    std::vector<std::string> vectorOfSubTokens;

    while (std::getline(tokenAsStringStream, subToken, '-'))
    {
        vectorOfSubTokens.push_back(subToken);
    }

    switch (vectorOfSubTokens.size())
    {
        // Parse cases for Token: Range
        case 2:
        {
            unsigned int skippingFrom = std::stoi(vectorOfSubTokens[0]);
            unsigned int skippingTo = std::stoi(vectorOfSubTokens[1]);
            if (checkSkipCommandBounds(skippingFrom, skippingTo)) {
                std::cerr << "You want to skip passes from " << skippingFrom << " to " << skippingTo << std::endl;
                auto localSkipComand = SkipCommand(skippingFrom, skippingTo);
                pdc.skipCommands.push_back(localSkipComand);
            }
            break;
        }
        // Parse cases for Token: Open range
        case 1:
        {
            unsigned int skippingFrom = std::stoi(vectorOfSubTokens[0]);
            std::cerr << "You want to skip passes from " << skippingFrom << " to end" << std::endl;

            if (SkipCommand* sc = getFirstCommand(pdc, TokenSkipCase::OpenRange))
            {
                sc->start = std::min(sc->start, skippingFrom);
            }
            else
            {
                auto localSkipComand = SkipCommand("", skippingFrom, std::numeric_limits<unsigned int>::max(), TokenSkipCase::OpenRange);
                pdc.skipCommands.push_back(localSkipComand);
            }
            break;
        }
        // Wrong Token format
        default:
        {
            std::cerr << "Wrong format TOKEN, TOKEN should have one '-'. Your Token: " << Token << std::endl;
        }
    }
    // move to next Token
    return true;
}

static bool tryParsePassNameSpecificOccurrence(PassDisableConfig& pdc, const std::string& nameOfPassToSkip, const std::string& occurrenceOfPass)
{
    // remove spaces to check if other chars is only digit
    std::string tempOccurrenceOfPassWithoutSpaces = occurrenceOfPass;
    tempOccurrenceOfPassWithoutSpaces.erase(remove(tempOccurrenceOfPassWithoutSpaces.begin(), tempOccurrenceOfPassWithoutSpaces.end(), ' '), tempOccurrenceOfPassWithoutSpaces.end());
    if (!std::all_of(tempOccurrenceOfPassWithoutSpaces.cbegin(), tempOccurrenceOfPassWithoutSpaces.cend(), ::isdigit))
    {
        // continue to parse this Token
        return false;
    }
    std::cerr << "You want to skip pass " << nameOfPassToSkip << " occurrence " << occurrenceOfPass << std::endl;
    unsigned int passOccurrence = std::stoi(occurrenceOfPass);
    auto localSkipComand = SkipCommand(nameOfPassToSkip, passOccurrence, passOccurrence, TokenSkipCase::SpecificPassOccurrence);
    pdc.skipCommands.push_back(localSkipComand);
    // move to next Token
    return true;
}

static bool tryParsePassNameOccurrenceRange(PassDisableConfig& pdc, const std::string& Token, const std::string& nameOfPassToSkip, const std::string& occurrenceOfPass)
{
    // remove '-' to check if other chars is only digit
    std::string tempOccurrenceOfPassWithoutDashAndSpaces = occurrenceOfPass;
    tempOccurrenceOfPassWithoutDashAndSpaces.erase(remove(tempOccurrenceOfPassWithoutDashAndSpaces.begin(), tempOccurrenceOfPassWithoutDashAndSpaces.end(), '-'), tempOccurrenceOfPassWithoutDashAndSpaces.end());
    tempOccurrenceOfPassWithoutDashAndSpaces.erase(remove(tempOccurrenceOfPassWithoutDashAndSpaces.begin(), tempOccurrenceOfPassWithoutDashAndSpaces.end(), ' '), tempOccurrenceOfPassWithoutDashAndSpaces.end());
    if (!std::all_of(tempOccurrenceOfPassWithoutDashAndSpaces.cbegin(), tempOccurrenceOfPassWithoutDashAndSpaces.cend(), ::isdigit))
    {
        std::cerr << "Wrong format TOKEN " << Token << " should have digit or '-' after ':'" << std::endl;
        // move to next Token anyway
        return true;
    }

    std::istringstream occurrenceOfPassAsStringStream(occurrenceOfPass);
    std::string occurrenceOfPassSubToken;
    std::vector<std::string> occurrenceOfPassVectorOfSubTokens;

    while (std::getline(occurrenceOfPassAsStringStream, occurrenceOfPassSubToken, '-'))
    {
        occurrenceOfPassVectorOfSubTokens.push_back(occurrenceOfPassSubToken);
    }

    switch (occurrenceOfPassVectorOfSubTokens.size())
    {
        // Parse cases for Token: Specific pass occurrences range
        case 2:
        {
            unsigned int skippingFrom = std::stoi(occurrenceOfPassVectorOfSubTokens[0]);
            unsigned int skippingTo = std::stoi(occurrenceOfPassVectorOfSubTokens[1]);
            if (checkSkipCommandBounds(skippingFrom, skippingTo)) {
                std::cerr << "You want to skip pass " << nameOfPassToSkip << " from occurrence " << skippingFrom << " to occurrence " << skippingTo << std::endl;
                auto localSkipComand = SkipCommand(nameOfPassToSkip, skippingFrom, skippingTo, TokenSkipCase::SpecificPassOccurrenceRange);
                pdc.skipCommands.push_back(localSkipComand);
            }
            break;
        }
        // Parse cases for Token: Specific pass occurrences open range
        case 1:
        {
            unsigned int skippingFrom = std::stoi(occurrenceOfPassVectorOfSubTokens[0]);
            std::cerr << "You want to skip pass " << nameOfPassToSkip << " from occurrence " << skippingFrom << " to end" << std::endl;

            if (SkipCommand* sc = getFirstCommand(pdc, TokenSkipCase::SpecificPassOccurrenceOpenRange, nameOfPassToSkip))
            {
                sc->start = std::min(sc->start, skippingFrom);
            }
            else
            {
                auto localSkipComand = SkipCommand(nameOfPassToSkip, skippingFrom, std::numeric_limits<unsigned int>::max(), TokenSkipCase::SpecificPassOccurrenceOpenRange);
                pdc.skipCommands.push_back(localSkipComand);
            }
            break;
        }
        // Wrong Token format
        default:
        {
            std::cerr << "Wrong format TOKEN, TOKEN should have one '-'. Your Token: " << Token << std::endl;
        }
    }
    // move to next Token
    return true;
}

static bool tryParsePassNames(PassDisableConfig& pdc, const std::string& Token)
{
    std::istringstream tokenAsStringStream(Token);
    std::string subToken;
    std::vector<std::string> vectorOfSubTokens;

    while (std::getline(tokenAsStringStream, subToken, ':'))
    {
        vectorOfSubTokens.push_back(subToken);
    }

    switch (vectorOfSubTokens.size())
    {
        // Parse cases for Token: Specific pass occurrence AND
        // Specific pass occurrences range AND
        // Specific pass occurrences open range
        case 2:
        {
            std::string nameOfPassToSkip = vectorOfSubTokens[0];
            std::string occurrenceOfPass = vectorOfSubTokens[1];

            // Parse cases for Token: Specific pass occurrence
            if (tryParsePassNameSpecificOccurrence(pdc, nameOfPassToSkip, occurrenceOfPass))
                return true; // move to next Token

            // Parse cases for Token: Specific pass occurrences range AND Specific pass occurrences open range
            if (tryParsePassNameOccurrenceRange(pdc, Token, nameOfPassToSkip, occurrenceOfPass))
                return true; // move to next Token

            return true; // move to next Token anyway
        }
        // Parse cases for Token: Pass name: PASS_NAME
        case 1:
        {
            std::string nameOfPassToSkip = vectorOfSubTokens[0];
            std::cerr << "You want to skip all occurrence of " << nameOfPassToSkip << std::endl;
            auto localSkipComand = SkipCommand(nameOfPassToSkip);
            pdc.skipCommands.push_back(localSkipComand);
            // move to next Token
            return true;
        }
        // Wrong Token format
        default:
        {
            std::cerr << "Wrong format TOKEN, TOKEN should have one ':'. Your Token: " << Token << std::endl;
            // move to next Token anyway
            return true;
        }
    }
}

static void parseShaderPassDisableFlag(PassDisableConfig& pdc)
{
    std::string passesToSkip = std::string(IGC_GET_REGKEYSTRING(ShaderPassDisable));
    std::cerr << "Your input to ShaderPassDisable: " << passesToSkip << std::endl;

    std::string Token;
    std::istringstream passesToSkipAsStringStream(passesToSkip);
    // Split to Tokens
    while (std::getline(passesToSkipAsStringStream, Token, ';'))
    {
        // Parse case for Token: Pass number
        if (tryParsePassNumber(pdc, Token))
            continue;

        // Parse cases for Token: Range AND Open range
        if (tryParsePassRange(pdc, Token))
            continue;

        // Parse cases for Token: Pass name AND Specific pass occurrence AND
        // Specific pass occurrences range AND
        // Specific pass occurrences open range
        if (tryParsePassNames(pdc, Token))
            continue;
    }
}

PassDisableConfig::PassDisableConfig()
{
    parseShaderPassDisableFlag(*this);
}

const SkipCommand* findApplicableSkipCommand(const PassDisableConfig& pdc, const std::string& currentPassName)
{
    for (auto sc = pdc.skipCommands.cbegin(); sc != pdc.skipCommands.cend(); sc++)
    {
        if (sc->name.empty() && pdc.passCount >= sc->start && pdc.passCount <= sc->end)
        {
            // Pass number OR Range OR Open range
            return &(*sc);
        }

        if (sc->name != currentPassName)
            continue;

        auto currentPass = pdc.passesOccuranceCount.find(currentPassName);
        if (currentPass == pdc.passesOccuranceCount.end())
        {
            continue;
        }
        else
        {
            if (sc->name == currentPassName && currentPass->second >= sc->start && currentPass->second <= sc->end)
            {
                // Specific pass occurrence OR Specific pass occurrences range OR Specific pass occurrences open range
                return &(*sc);
            }
            if (sc->name == currentPassName && sc->start == std::numeric_limits<unsigned int>::max() && sc->end == std::numeric_limits<unsigned int>::max())
            {
                // Pass name
                return &(*sc);
            }
        }
    }
    return nullptr;
}

void displaySkippedPass(const PassDisableConfig& pdc, const std::string& currentPassName, const SkipCommand* skippedByCommand)
{
    static bool displayHeader = true;
    std::string skippedByString = "Invalid skip pass case";
    TokenSkipCase skippedBy = WrongFormat;
    if (skippedByCommand)
        skippedBy = skippedByCommand->skippedBy;

    switch (skippedBy) {
#define CMD_KIND_CASE(x) case x: skippedByString = #x ; break;
        CMD_KIND_CASE(Range)
        CMD_KIND_CASE(OpenRange)
        CMD_KIND_CASE(PassNumber)
        CMD_KIND_CASE(PassName)
        CMD_KIND_CASE(SpecificPassOccurrence)
        CMD_KIND_CASE(SpecificPassOccurrenceRange)
        CMD_KIND_CASE(SpecificPassOccurrenceOpenRange)
        CMD_KIND_CASE(WrongFormat)
#undef CMD_KIND_CASE
    }

    if (displayHeader)
    {
        std::cerr << std::left << std::endl
            << setw(15) << "Pass number"
            << setw(50) << "Pass name"
            << setw(20) << "Pass occurrence"
            << setw(75) << "skippedBy"
            << std::endl;
        displayHeader = false;
    }
    auto singlePass = pdc.passesOccuranceCount.find(currentPassName);
    if (singlePass != pdc.passesOccuranceCount.end())
    {
        std::cerr << std::left
            << setw(15) << pdc.passCount
            << setw(50) << currentPassName
            << setw(20) << singlePass->second
            << setw(75) << skippedByString
            << std::endl;
    }
    else
    {
        std::cerr << std::left << "You are trying to skip a pass that is not available" << std::endl;
    }
}

/*
ShaderDisplayAllPassesNames

Example:
IGC_ShaderDisplayAllPassesNames=1

Pass number Pass name                                         Pass occurrence
0           CheckInstrTypes                                   1
1           Types Legalization Pass                           1
2           Target Library Information                        1
3           Dead Code Elimination                             1
...
...
...
225         Layout                                            1
226         TimeStatsCounter Start/Stop                       6
227         EmitPass                                          1
228         EmitPass                                          2
229         EmitPass                                          3
230         DebugInfoPass                                     1
Build succeeded.
*/
void displayAllPasses(const Pass* P)
{
    static unsigned int countPass = 0;
    const std::string currentPassName = P->getPassName().str();
    static std::unordered_map<std::string, int> passesOccuranceCountToDisplay;

    auto result = passesOccuranceCountToDisplay.try_emplace(currentPassName, 1);
    if (!result.second) {
        result.first->second += 1;
    }
    if (!countPass)
    {
        std::cerr << std::left << std::endl
            << setw(12) << "Pass number"
            << setw(50) << "Pass name"
            << setw(20) << "Pass occurrence"
            << std::endl;
    }
    std::cerr << std::left
        << setw(12) << countPass
        << setw(50) << currentPassName
        << setw(20) << passesOccuranceCountToDisplay[currentPassName]
        << std::endl;

    countPass++;
}

void IGCPassManager::add(Pass *P)
{
    //check only once
    static bool checkedToggles = false;
    static bool hasToggles = false;
    static std::bitset<1024> toggles;
    if (!checkedToggles)
    {
        checkedToggles = true;
        hasToggles = getPassToggles(toggles);
    }
    if (hasToggles && m_pContext->m_numPasses < 1024 && toggles[m_pContext->m_numPasses])
    {
        errs() << "Skipping pass: '" << P->getPassName() << "\n";
        m_pContext->m_numPasses++;
        return;
    }

    if (IGC_IS_FLAG_ENABLED(ShaderDisableOptPassesAfter)
            && m_pContext->m_numPasses > IGC_GET_FLAG_VALUE(ShaderDisableOptPassesAfter)
            && m_name == "OPT") {
        errs() << "Skipping optimization pass: '" << P->getPassName()
               << "' (threshold: " << IGC_GET_FLAG_VALUE(ShaderDisableOptPassesAfter) << ").\n";
        return;
    }

    if (IGC_IS_FLAG_ENABLED(ShaderPassDisable))
    {
        // Parse Flag Input once in PassDisableConfig constructor
        static PassDisableConfig pdc;
        const std::string currentPassName = P->getPassName().str();

        // Fill the passesOccuranceCount
        auto result = pdc.passesOccuranceCount.try_emplace(currentPassName, 1);
        if (!result.second) {
            result.first->second += 1;
        }

        // Check if current pass should by skipped
        const SkipCommand* skippedByCommand = findApplicableSkipCommand(pdc, currentPassName);
        if (skippedByCommand)
        {
            displaySkippedPass(pdc, currentPassName, skippedByCommand);
            m_pContext->m_numPasses++;
            pdc.passCount++;
            return;
        }
        pdc.passCount++;
    }

    if (IGC_IS_FLAG_ENABLED(ShaderDisplayAllPassesNames) && !IGC_IS_FLAG_ENABLED(ShaderPassDisable))
    {
        displayAllPasses(P);
    }

    // Skip adding a printer pass for analysis passes.
    const PassInfo* PI = Pass::lookupPassInfo(P->getPassID());
    bool isAnalysisPass = PI && PI->isAnalysis();

    if (isPrintBefore(P) && !isAnalysisPass)
    {
        addPrintPass(P, true);
    }

    if (IGC_REGKEY_OR_FLAG_ENABLED(DumpTimeStatsPerPass, TIME_STATS_PER_PASS))
    {
        PassManager::add(createTimeStatsIGCPass(m_pContext, m_name + '_' + std::string(P->getPassName()), STATS_COUNTER_START));
    }

    PassManager::add(P);

    if (IGC_REGKEY_OR_FLAG_ENABLED(DumpTimeStatsPerPass, TIME_STATS_PER_PASS))
    {
        PassManager::add(createTimeStatsIGCPass(m_pContext, m_name + '_' + std::string(P->getPassName()), STATS_COUNTER_END));
    }

    if (isPrintAfter(P) && !isAnalysisPass)
    {
        addPrintPass(P, false);
    }
}

// List: a comma/semicolon-separated list of pass names.
//    N: a pass name
// return true if N is in List.
bool IGCPassManager::isInList(const StringRef& N, const StringRef& List) const
{
    StringRef Separators(",;");
    size_t startPos = 0;
    while (startPos != StringRef::npos)
    {
        size_t endPos = List.find_first_of(Separators, startPos);
        size_t len = (endPos != StringRef::npos ? endPos - startPos : endPos);
        StringRef Name = List.substr(startPos, len);
        if (IGCLLVM::equals_insensitive(Name,N))
        {
            return true;
        }
        startPos = (endPos != StringRef::npos ? endPos + 1 : StringRef::npos);
    }
    return false;
}

bool IGCPassManager::isPrintBefore(Pass* P)
{
    if (IGC_IS_FLAG_ENABLED(PrintBefore))
    {
        // PrintBefore=N0,N1,N2  : comma-separate list of pass names
        //                         or pass command args registered in passInfo.
        StringRef  passNameList(IGC_GET_REGKEYSTRING(PrintBefore));
        StringRef PN = P->getPassName();
        if (IGCLLVM::equals_insensitive(passNameList, "all") || isInList(PN, passNameList))
            return true;

        // further check passInfo
        if (const PassInfo* PI = Pass::lookupPassInfo(P->getPassID()))
        {
            return isInList(PI->getPassArgument(), passNameList);
        }
    }
    return false;
}

bool IGCPassManager::isPrintAfter(Pass* P)
{
    if (IGC_IS_FLAG_ENABLED(ShaderDumpEnableAll))
    {
        return true;
    }
    if (IGC_IS_FLAG_ENABLED(PrintAfter))
    {
        // PrintAfter=N0,N1,N2  : comma-separate list of pass names or
        //                         or pass command args registered in passInfo.
        StringRef  passNameList(IGC_GET_REGKEYSTRING(PrintAfter));
        StringRef PN = P->getPassName();
        if (IGCLLVM::equals_insensitive(passNameList, "all") || isInList(PN, passNameList))
            return true;

        // further check passInfo
        if (const PassInfo* PI = Pass::lookupPassInfo(P->getPassID()))
        {
            return isInList(PI->getPassArgument(), passNameList);
        }
    }
    return false;
}

void IGCPassManager::addPrintPass(Pass* P, bool isBefore)
{
    std::string passName =
        m_name + (isBefore ? "_before_" : "_after_") + std::string(P->getPassName());
    auto name =
        IGC::Debug::DumpName(IGC::Debug::GetShaderOutputName())
        .Type(m_pContext->type)
        .Hash(m_pContext->hash)
        .Pass(passName, m_pContext->m_numPasses++)
        .StagedInfo(m_pContext)
        .Extension("ll");

    if (!name.allow())
        return;

    // The dump object needs to be on the Heap because it owns the stream, and the stream
    // is taken by reference into the printer pass. If the Dump object had been on the
    // stack, then that reference would go bad as soon as we exit this scope, and then
    // the printer pass would access an invalid pointer later on when we call PassManager::run()
    m_irDumps.emplace_front(name, IGC::Debug::DumpType::PASS_IR_TEXT);
    auto printerPass = P->createPrinterPass(m_irDumps.front().stream(), "");
    if (printerPass->getPassKind() == PT_Function) //Enabling debug info for -O2
        printerPass = llvm::createPrintModulePass(m_irDumps.front().stream(), "");
    PassManager::add(printerPass);


    llvm::Pass* flushPass = createFlushPass(P, m_irDumps.front());
    if (nullptr != flushPass)
    {
        PassManager::add(flushPass);
    }
}

void DumpLLVMIR(IGC::CodeGenContext* pContext, const char* dumpName)
{
    SetCurrentDebugHash(pContext->hash);

    if (IGC_IS_FLAG_ENABLED(DumpLLVMIR))
    {
        pContext->getMetaDataUtils()->save(*pContext->getLLVMContext());
        serialize(*(pContext->getModuleMetaData()), pContext->getModule());
        using namespace IGC::Debug;
        auto name =
            DumpName(IGC::Debug::GetShaderOutputName())
            .Hash(pContext->hash)
            .Type(pContext->type)
            .Pass(dumpName)
            .Retry(pContext->m_retryManager.GetRetryId())
            .Extension("ll");
        auto new_annotator = IntrinsicAnnotator();
        auto annotator = (pContext->annotater != nullptr) ? pContext->annotater : &new_annotator;
        DumpLLVMIRText(
            pContext->getModule(),
            name,
            annotator);
    }
    if (IGC_IS_FLAG_ENABLED(ShaderOverride))
    {
        auto name =
            DumpName(IGC::Debug::GetShaderOutputName())
            .Hash(pContext->hash)
            .Type(pContext->type)
            .Pass(dumpName)
            .Extension("ll");
        SMDiagnostic Err;
        std::string fileName = name.overridePath();
        FILE* fp = fopen(fileName.c_str(), "r");
        if (fp != nullptr)
        {
            fclose(fp);
            errs() << "Override shader: " << fileName << "\n";
            Module* mod = parseIRFile(fileName, Err, *pContext->getLLVMContext()).release();
            if (mod)
            {
                pContext->deleteModule();
                pContext->setModule(mod);
                deserialize(*(pContext->getModuleMetaData()), mod);
                appendToShaderOverrideLogFile(fileName, "OVERRIDEN: ");
            }
            else
            {
                std::stringstream ss;
                ss << "Parse IR failed.\n";
                ss << Err.getLineNo() << ": "
                    << Err.getLineContents().str() << "\n"
                    << Err.getMessage().str() << "\n";

                std::string str = ss.str();
                errs() << str;
                appendToShaderOverrideLogFile(fileName, str.c_str());
            }
        }
    }
}