File: PromoteResourceToDirectAS.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 (780 lines) | stat: -rw-r--r-- 30,917 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "PromoteResourceToDirectAS.h"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Constants.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvmWrapper/Support/Alignment.h>
#include "common/LLVMWarningsPop.hpp"
#include "common/IGCIRBuilder.h"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/CodeGenPublicEnums.h"
#include "common/igc_regkeys.hpp"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;
using namespace GenISAIntrinsic;

// Register pass to igc-opt
#define PASS_FLAG "igc-promote-resources-to-direct-addrspace"
#define PASS_DESCRIPTION "Pass promotes indirect addrspace resource access to direct addrspace"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(PromoteResourceToDirectAS, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(PromoteResourceToDirectAS, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char PromoteResourceToDirectAS::ID = 0;

PromoteResourceToDirectAS::PromoteResourceToDirectAS()
    : FunctionPass(ID)
{
    initializePromoteResourceToDirectASPass(*PassRegistry::getPassRegistry());
}

bool PromoteResourceToDirectAS::runOnFunction(Function& F)
{
    m_pCodeGenContext = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
    m_pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
    visit(F);

    return true;
}

// Determine the new buffer type
Type* GetBufferAccessType(Instruction* inst)
{
    if (LoadInst * load = dyn_cast<LoadInst>(inst))
    {
        return load->getType();
    }
    else if (StoreInst * store = dyn_cast<StoreInst>(inst))
    {
        return store->getOperand(0)->getType();
    }
    else if (GenIntrinsicInst * pIntr = dyn_cast<GenIntrinsicInst>(inst))
    {
        switch (pIntr->getIntrinsicID())
        {
        case GenISAIntrinsic::GenISA_storeraw_indexed:
        case GenISAIntrinsic::GenISA_storerawvector_indexed:
            return pIntr->getOperand(2)->getType();
        case GenISAIntrinsic::GenISA_ldrawvector_indexed:
        case GenISAIntrinsic::GenISA_ldraw_indexed:
        case GenISAIntrinsic::GenISA_intatomicraw:
        case GenISAIntrinsic::GenISA_intatomictyped:
        case GenISAIntrinsic::GenISA_icmpxchgatomictyped:
        case GenISAIntrinsic::GenISA_floatatomicraw:
        case GenISAIntrinsic::GenISA_icmpxchgatomicraw:
        case GenISAIntrinsic::GenISA_fcmpxchgatomicraw:
        case GenISAIntrinsic::GenISA_intatomicrawA64:
        case GenISAIntrinsic::GenISA_floatatomicrawA64:
        case GenISAIntrinsic::GenISA_icmpxchgatomicrawA64:
        case GenISAIntrinsic::GenISA_fcmpxchgatomicrawA64:
            return pIntr->getType();
        default:
            break;
        }
    }

    IGC_ASSERT_MESSAGE(0, "Unsupported buffer access intrinsic");
    return inst->getType();
}

Argument* FindArrayBaseArg(AllocaInst* alloca)
{
    // Search for argument that is first element of this local array, starting from alloca.
    // This is pattern match and is relying on a way array is lowered.

    // First, find GEP taking first element of an array. It is assumed to be immediate user of alloca.
    Argument* arg = nullptr;
    GetElementPtrInst* baseGep = nullptr;
    for (Value::user_iterator use_it = alloca->user_begin(), use_e = alloca->user_end(); use_it != use_e; ++use_it)
    {
        if (auto gep = dyn_cast<GetElementPtrInst>(*use_it))
        {
            if (gep->getNumIndices() == 2)
            {
                if (auto gepIndexValue = dyn_cast<llvm::ConstantInt>(gep->getOperand(2)))
                {
                    if (gepIndexValue->getZExtValue() == 0)
                    {
                        // Pointer to first element found.
                        baseGep = gep;
                    }
                }
            }
        }
    }

    // The only user for this GEP should be a store, which is storing function argument to an array.
    // Note that this is assuming OCL approach, which is using KernelArgs.
    if (baseGep && baseGep->hasOneUse())
    {
        if (auto store = dyn_cast<StoreInst>(*baseGep->user_begin()))
        {
            if (auto elem = dyn_cast<Argument>(store->getValueOperand()))
            {
                arg = elem;
            }
        }
    }

    return arg;
}

Value* FindArrayIndex(const std::vector<Value*>& instList, IGCIRBuilder<>& builder)
{
    // Find GEP instruction in the list and get arrayIndex from it, depending on GEP type.
    Value* arrayIndex = nullptr;
    for (Value* V : instList)
    {
        if (auto gep = dyn_cast<GetElementPtrInst>(V))
        {
            if (arrayIndex != nullptr || gep->getPointerAddressSpace() != 0)
            {
                // It's not expected to see multiple GEPs on this path or GEPs to addrspace other than 0.
                arrayIndex = nullptr;
                break;
            }
            auto pointerElementTy = gep->getPointerOperandType()->getPointerElementType();
            if (pointerElementTy->isStructTy())
            {
                // Example: %1 = getelementptr inbounds %"struct.texture", %"struct.texture"* %aot, i64 %arrayIndex, i32 0
                arrayIndex = gep->getOperand(1);
            }
            else if (pointerElementTy->isArrayTy() && gep->getOperand(1) == builder.getInt64(0))
            {
                // Example: %2 = getelementptr inbounds [8 x %"struct.texture"], [8 x %"struct.texture"]* %aot, i64 0, i64 %arrayIndex, i32 0
                arrayIndex = gep->getOperand(2);
            }
            else if (pointerElementTy->isPointerTy() && pointerElementTy->getPointerElementType()->isStructTy() && gep->getOperand(1) == builder.getInt64(0))
            {
                // Example: %3 = getelementptr inbounds %"struct.texture", %"struct.texture"** %aot, i64 0, i64 %arrayIndex, i32 0
                arrayIndex = gep->getOperand(2);
            }
        }
    }
    return arrayIndex;
}

void PromoteResourceToDirectAS::PromoteSamplerTextureToDirectAS(GenIntrinsicInst*& pIntr, Value* resourcePtr)
{
    IGCIRBuilder<> builder(pIntr);

    unsigned addrSpace = resourcePtr->getType()->getPointerAddressSpace();

    if (addrSpace != 1 && addrSpace != 2 && IGC::IsDirectIdx(addrSpace))
    {
        // Already direct addrspace, no need to promote
        // Only try to promote bindless pointers ( as(1) or as(2) ), or indirect buffer access
        return;
    }
    unsigned bufID = 0;
    BufferType bufTy = BufferType::BUFFER_TYPE_UNKNOWN;
    BufferAccessType accTy;
    bool needBufferOffset;  // Unused
    bool canPromote = false;
    Value* arrayIndex = nullptr;

    std::vector<Value*> instList;
    Value* srcPtr = IGC::TracePointerSource(resourcePtr, false, false, true, instList);

    if (srcPtr)
    {
        if (auto alloca = llvm::dyn_cast<AllocaInst>(srcPtr))
        {
            arrayIndex = FindArrayIndex(instList, builder);
            if (arrayIndex != nullptr)
            {
                // TODO: We could read igc.read_only_array metadata attached to alloca.
                // If not -1, it should contain base index of this array. In this case,
                // FindArrayBaseArg would not be needed.

                // Find input argument for the first element in this array.
                srcPtr = FindArrayBaseArg(alloca);
            }
        }
    }

    if (srcPtr)
    {
        // Trace the resource pointer.
        // If we can find it, we can promote the indirect access to direct access
        // by encoding the BTI as a direct addrspace
        if (srcPtr->getType()->isPointerTy() &&
            IGC::GetResourcePointerInfo(srcPtr, bufID, bufTy, accTy, needBufferOffset))
        {
            canPromote = true;
        }
        else if (Argument * argPtr = dyn_cast<Argument>(srcPtr))
        {
            // Source comes from kernel arguments
            // We only promote if the argument comes from the entry function.
            // Default to bindless if sampler called from subroutine.
            Function* function = argPtr->getParent();
            if (isEntryFunc(m_pMdUtils, function))
            {
                IGC_ASSERT(m_pCodeGenContext->type == ShaderType::OPENCL_SHADER);
                ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
                if (modMD->FuncMD.find(function) != modMD->FuncMD.end())
                {
                    FunctionMetaData* funcMD = &modMD->FuncMD[function];
                    ResourceAllocMD* resAllocMD = &funcMD->resAllocMD;
                    ArgAllocMD* argInfo = &resAllocMD->argAllocMDList[argPtr->getArgNo()];
                    IGC_ASSERT_MESSAGE((size_t)argPtr->getArgNo() < resAllocMD->argAllocMDList.size(), "ArgAllocMD List Out of Bounds Error");

                    if (argInfo->type == ResourceTypeEnum::BindlessUAVResourceType)
                    {
                        bufID = (unsigned)argInfo->indexType;
                        bufTy = BufferType::UAV;
                        canPromote = true;
                    }
                    else if (argInfo->type == ResourceTypeEnum::BindlessSamplerResourceType)
                    {
                        bufID = (unsigned)argInfo->indexType;
                        bufTy = BufferType::SAMPLER;
                        canPromote = true;
                    }
                }
            }
        }
        else if (GlobalVariable * pGlobal = dyn_cast<GlobalVariable>(srcPtr))
        {
            // Can still promote if we traced to an inline sampler with samplerID metadata attached
            if (MDNode * md = pGlobal->getMetadata("ConstSampler"))
            {
                if (ConstantInt * C = mdconst::extract<ConstantInt>(md->getOperand(0)))
                {
                    bufID = (unsigned)C->getZExtValue();
                    bufTy = BufferType::SAMPLER;
                    canPromote = true;
                }
            }
        }
    }

    if (canPromote)
    {
        Value* bufferId = builder.getInt32(bufID);
        if (arrayIndex != nullptr)
        {
            // Add base array index:
            if (arrayIndex->getType() != bufferId->getType())
            {
                arrayIndex = builder.CreateZExtOrTrunc(arrayIndex, bufferId->getType());
            }
            bufferId = builder.CreateAdd(bufferId, arrayIndex);
        }

        addrSpace = IGC::EncodeAS4GFXResource(*bufferId, bufTy);
        PointerType* newptrType = PointerType::get(resourcePtr->getType()->getPointerElementType(), addrSpace);

        Value* mutePtr = nullptr;
        if (llvm::isa<llvm::ConstantInt>(bufferId))
        {
            mutePtr = ConstantPointerNull::get(newptrType);
        }
        else
        {
            // Index is not a constant:
            mutePtr = builder.CreateIntToPtr(builder.CreateZExt(bufferId, builder.getInt64Ty()), newptrType);
        }
        IGC::ChangePtrTypeInIntrinsic(pIntr, resourcePtr, mutePtr);
    }
}

bool PatchGetElementPtr(const std::vector<Value*>& instList, Type* dstTy, unsigned directAS, Value* patchedSourcePtr, Value*& dstPtr)
{
    unsigned numInstructions = instList.size();
    Value* patchedInst = patchedSourcePtr;
    dstPtr = nullptr;
    Type* patchTy = nullptr;

    // Find all the instructions we need to patch, starting from the top.
    // If there is more than one GEP instruction, we need to patch all of them, as well
    // as any pointer casts. All other instructions are not supported.
    // %0 = getelementptr int, int addrspace(1)* %ptr, i32 3
    // %1 = bitcast int addrspace(1)* %0 to float addrspace(1)*
    // %2 = getelementptr float, float addrspace(1)* %1, i32 8
    // PROMOTED TO:
    // %0 = getelementptr int, int addrspace(131072)* null, i32 3
    // %1 = bitcast int addrspace(131072)* %0 to float addrspace(131072)*
    // %2 = getelementptr float, float addrspace(131072)* %1, i32 8
    std::vector<Value*> patchInstructions;
    for (int i = numInstructions - 1; i >= 0; i--)
    {
        Value* inst = instList[i];
        if (isa<GetElementPtrInst>(inst))
        {
            patchInstructions.push_back(inst);
        }
        else if (BitCastInst * cast = dyn_cast<BitCastInst>(inst))
        {
            // Bitcast from pointer to pointer
            if (cast->getType()->isPointerTy() && cast->getOperand(0)->getType()->isPointerTy())
                patchInstructions.push_back(inst);
        }
    }

    if (!patchedInst)
    {
        if (patchInstructions.size() > 0)
        {
            // Get the original pointer type before any GEPs or bitcasts modifies it
            patchTy = cast<Instruction>(patchInstructions[0])->getOperand(0)->getType()->getPointerElementType();
        }
        else
        {
            // If there is nothing to patch, set the pointer type to the same type as the buffer access type
            patchTy = dstTy;
        }
        PointerType* newptrType = PointerType::get(patchTy, directAS);
        patchedInst = ConstantPointerNull::get(newptrType);
    }

    for (unsigned i = 0; i < (unsigned)patchInstructions.size(); i++)
    {
        Value* inst = patchInstructions[i];
        if (GetElementPtrInst * gepInst = dyn_cast<GetElementPtrInst>(inst))
        {
            llvm::SmallVector<llvm::Value*, 4> gepArgs(gepInst->idx_begin(), gepInst->idx_end());
            // Create the new GEP instruction
            if (gepInst->isInBounds())
                patchedInst = GetElementPtrInst::CreateInBounds(patchTy, patchedInst, gepArgs, "", gepInst);
            else
                patchedInst = GetElementPtrInst::Create(patchTy, patchedInst, gepArgs, "", gepInst);

            if (GetElementPtrInst* gepPatchedInst = dyn_cast<GetElementPtrInst>(patchedInst))
            {
                gepPatchedInst->setDebugLoc(gepInst->getDebugLoc());
            }
        }
        else if (BitCastInst * cast = dyn_cast<BitCastInst>(inst))
        {
            PointerType* newptrType = PointerType::get(cast->getType()->getPointerElementType(), directAS);
            patchedInst = BitCastInst::Create(Instruction::BitCast, patchedInst, newptrType, "", cast);
            if (BitCastInst* castPathedInst = dyn_cast<BitCastInst>(patchedInst))
            {
                castPathedInst->setDebugLoc(cast->getDebugLoc());
            }
        }
        else
        {
            IGC_ASSERT_MESSAGE(0, "Can not patch unsupported instruction!");
            return false;
        }
    }

    dstPtr = patchedInst;

    // Final types must match
    return (dstPtr->getType()->getPointerElementType() == dstTy);
}

bool PatchInstructionAddressSpace(const std::vector<Value*>& instList, Type* dstTy, unsigned directAS, Value*& dstPtr)
{
    unsigned numInstructions = instList.size();
    dstPtr = nullptr;
    bool success = false;

    // Find the first PHI node or select we need to patch, if any.
    // In the most simple case, we assume only one branching instruction. If there are multiple selects, phis, or any
    // combination of the two, we won't be able to handle it.

    // First, we find the phi/select instruction. We patch all the GEP and ptrcast instructions for each branch, then
    // finally any GEP and ptrcast instructions that comes after the phi/select, but before the load
    PHINode* phiNode = nullptr;
    SelectInst* selectInst = nullptr;
    std::vector<Value*> remainingInst;
    for (unsigned i = 0; i < numInstructions; i++)
    {
        Value* inst = instList[i];
        if (PHINode * phi = dyn_cast<PHINode>(inst))
        {
            phiNode = phi;
            break;
        }
        else if (SelectInst * select = dyn_cast<SelectInst>(inst))
        {
            selectInst = select;
            break;
        }
        else
        {
            remainingInst.push_back(inst);
        }
    }

    if (selectInst)
    {
        Value* newSelectInst = nullptr;
        Value* bufferPtr0 = nullptr;
        Value* bufferPtr1 = nullptr;
        std::vector<Value*> tempList0, tempList1;
        // Call trace again to get the instructions list for each branch of the select
        if (IGC::TracePointerSource(selectInst->getOperand(1), true, false, true, tempList0) &&
            IGC::TracePointerSource(selectInst->getOperand(2), true, false, true, tempList1))
        {
            IGC_ASSERT(selectInst->getOperand(1)->getType()->isPointerTy() && selectInst->getOperand(2)->getType()->isPointerTy());
            Type* srcType0 = selectInst->getOperand(1)->getType()->getPointerElementType();
            Type* srcType1 = selectInst->getOperand(1)->getType()->getPointerElementType();

            // Patch both branches, then patch the select instruction
            if (PatchGetElementPtr(tempList0, srcType0, directAS, nullptr, bufferPtr0) &&
                PatchGetElementPtr(tempList1, srcType1, directAS, nullptr, bufferPtr1))
            {
                newSelectInst = SelectInst::Create(selectInst->getOperand(0), bufferPtr0, bufferPtr1, "", selectInst);
            }
            // If there are any remaining GEP/bitcast instructions after the select, patch them as well
            if (newSelectInst)
            {
                success = PatchGetElementPtr(remainingInst, dstTy, directAS, newSelectInst, dstPtr);
            }
        }
    }
    else if (phiNode)
    {
        PointerType* newPhiTy = PointerType::get(phiNode->getType()->getPointerElementType(), directAS);
        PHINode* pNewPhi = PHINode::Create(newPhiTy, phiNode->getNumIncomingValues(), "", phiNode);
        for (unsigned int i = 0; i < phiNode->getNumIncomingValues(); ++i)
        {
            Value* incomingVal = phiNode->getIncomingValue(i);
            IGC_ASSERT(incomingVal->getType()->isPointerTy());

            std::vector<Value*> tempList;
            Value* srcPtr = IGC::TracePointerSource(incomingVal, true, false, true, tempList);

            // We know srcPtr is trace-able, since it's been traced already, we just need to get the
            // list of instructions we need to patch
            IGC_ASSERT(srcPtr);

            // Patch the GEPs for each phi node path
            Value* bufferPtr = nullptr;
            Type* incomingTy = incomingVal->getType()->getPointerElementType();
            if (!PatchGetElementPtr(tempList, incomingTy, directAS, nullptr, bufferPtr))
            {
                // Patching must succeed for all paths
                pNewPhi->eraseFromParent();
                return false;
            }
            pNewPhi->addIncoming(bufferPtr, phiNode->getIncomingBlock(i));
        }

        // If there are any remaining GEP/bitcast instructions after the PHI node, patch them as well
        success = PatchGetElementPtr(remainingInst, dstTy, directAS, pNewPhi, dstPtr);
    }
    else
    {
        // If there are no PHI nodes or selects, we can just patch the GEPs
        success = PatchGetElementPtr(instList, dstTy, directAS, nullptr, dstPtr);
    }

    if (!dstPtr || !dstPtr->getType()->isPointerTy())
        return false;
    if (dstPtr->getType()->getPointerElementType() != dstTy)
        return false;

    return success;
}

Value* PromoteResourceToDirectAS::getOffsetValue(Value* srcPtr, int& bufferOffsetHandle)
{
    auto offsetEntry = m_SrcPtrToBufferOffsetMap.find(srcPtr);
    if (offsetEntry != m_SrcPtrToBufferOffsetMap.end())
    {
        GenIntrinsicInst* runtimevalue = dyn_cast<GenIntrinsicInst>(offsetEntry->second);
        IGC_ASSERT_MESSAGE(runtimevalue, "Buffer offset must be a runtime value");
        bufferOffsetHandle = (int)llvm::cast<llvm::ConstantInt>(runtimevalue->getOperand(0))->getZExtValue();
        return offsetEntry->second;
    }
    else
    {
        Instruction* srcPtrInst;
        srcPtrInst = dyn_cast<Instruction>(srcPtr);
        IGC_ASSERT_MESSAGE(srcPtrInst, "source pointer must have been an instruction");
        IGCIRBuilder<> builder(srcPtrInst);

        Instruction* bufferOffset;
        ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
        // Create runtime value for buffer offset
        Function* pFunc = GenISAIntrinsic::getDeclaration(srcPtrInst->getParent()->getParent()->getParent(), GenISAIntrinsic::GenISA_RuntimeValue, builder.getInt32Ty());
        bufferOffset = builder.CreateCall(pFunc, builder.getInt32(modMD->MinNOSPushConstantSize));
        bufferOffsetHandle = modMD->MinNOSPushConstantSize;
        modMD->MinNOSPushConstantSize++;
        m_SrcPtrToBufferOffsetMap[srcPtr] = bufferOffset;
        return bufferOffset;
    }
}

void PromoteResourceToDirectAS::PromoteBufferToDirectAS(Instruction* inst, Value* resourcePtr)
{
    IGCIRBuilder<> builder(inst);

    unsigned addrSpace = resourcePtr->getType()->getPointerAddressSpace();

    if (addrSpace != 1 && addrSpace != 2 && IGC::IsDirectIdx(addrSpace))
    {
        // Already direct addrspace, no need to promote
        // Only try to promote stateless buffer pointers ( as(1) or as(2) ), or indirect buffer access
        return;
    }

    // Vulkan encodes address space differently, with the reserve bits set.
    // TODO: Investigate how addrspace is encoded in Vulkan,
    // for now skip promoting if it's an address space we dont recognize.
    if ((addrSpace & 0xFFC00000) != 0x0)
    {
        return;
    }

    std::vector<Value*> instructionList;
    Value* srcPtr = IGC::TracePointerSource(resourcePtr, false, false, true, instructionList);

    if (!srcPtr)
    {
        // Cannot trace the resource pointer back to it's source, cannot promote
        return;
    }

    unsigned bufferID;
    BufferType bufType;
    BufferAccessType accType;
    bool needBufferOffset;
    if (!IGC::GetResourcePointerInfo(srcPtr, bufferID, bufType, accType, needBufferOffset))
    {
        // Can't promote if we don't know the explicit buffer ID and type
        return;
    }

    // Get the new direct address space
    unsigned directAS = IGC::EncodeAS4GFXResource(*builder.getInt32(bufferID), bufType);

    Value* pBuffer = nullptr;
    Type* pBufferType = GetBufferAccessType(inst);

    if (!PatchInstructionAddressSpace(instructionList, pBufferType, directAS, pBuffer))
    {
        // Patching failed
        return;
    }

    // If needBufferOffset set, we need to adjust stateful buffer accesses with the buffer offset from payload
    Value* pointerValue;
    int bufferOffsetHandle = -1;
    if (needBufferOffset)
    {
        pointerValue = builder.CreatePtrToInt(pBuffer, builder.getInt32Ty());
        pointerValue = builder.CreateAdd(pointerValue, getOffsetValue(srcPtr, bufferOffsetHandle));
        pBuffer = builder.CreateIntToPtr(pointerValue, pBuffer->getType());
    }

    bool canpromote = false;
    if (LoadInst * load = dyn_cast<LoadInst>(inst))
    {
        LoadInst* newload = IGC::cloneLoad(load, pBuffer);
        load->replaceAllUsesWith(newload);
        load->eraseFromParent();
        canpromote = true;
    }
    else if (StoreInst * store = dyn_cast<StoreInst>(inst))
    {
        StoreInst* newstore = IGC::cloneStore(store, store->getOperand(0), pBuffer);
        store->replaceAllUsesWith(newstore);
        store->eraseFromParent();
        canpromote = true;
    }
    else if (GenIntrinsicInst * pIntr = dyn_cast<GenIntrinsicInst>(inst))
    {
        Value* pNewBufferAccessInst = nullptr;

        switch (pIntr->getIntrinsicID())
        {
            // TODO: ldraw and storeraw currently does not support non-aligned memory, if promote fails
            // then default alignment is 4. Need to implement support for ldraw and storeraw to support
            // non-aligned memory access, to preserve the alignment of the original load/store.

            // WA:
            // %522 = load <4 x i8> addrspace(131073)* %521
            // For this example instruction, InstructionCombining pass generates align4 if alignment
            // is not set. Forcing alignment to 1 generates the correct alignment value align2.
            // TODO: Why is no alignment and align1 treated differently by InstructionCombining?
        case GenISAIntrinsic::GenISA_ldraw_indexed:
        case GenISAIntrinsic::GenISA_ldrawvector_indexed:
        {
            Value* offsetVal = pIntr->getOperand(1);
            PointerType* ptrType = PointerType::get(pBufferType, directAS);
            pBuffer = builder.CreateIntToPtr(offsetVal, ptrType);

            const LdRawIntrinsic* const ldRawIntr = cast<LdRawIntrinsic>(pIntr);

            // Promote ldraw back to load
            pNewBufferAccessInst = builder.CreateAlignedLoad(
                pBuffer,
                IGCLLVM::getAlign(ldRawIntr->getAlignment()),
                ldRawIntr->isVolatile());
            break;
        }
        case GenISAIntrinsic::GenISA_storeraw_indexed:
        case GenISAIntrinsic::GenISA_storerawvector_indexed:
        {
            Value* offsetVal = pIntr->getOperand(1);
            PointerType* ptrType = PointerType::get(pBufferType, directAS);
            pBuffer = builder.CreateIntToPtr(offsetVal, ptrType);

            const StoreRawIntrinsic* const storeRawIntr = cast<StoreRawIntrinsic>(pIntr);

            // Promote storeraw back to store
            Value* storeVal = pIntr->getOperand(2);
            pNewBufferAccessInst = builder.CreateAlignedStore(
                storeVal,
                pBuffer,
                IGCLLVM::getAlign(storeRawIntr->getAlignment()),
                storeRawIntr->isVolatile());
            break;
        }
        default:
        {
            bool is64BitPtr = true;
            switch (pIntr->getIntrinsicID())
            {
            case GenISAIntrinsic::GenISA_intatomicraw:
            case GenISAIntrinsic::GenISA_floatatomicraw:
            case GenISAIntrinsic::GenISA_intatomictyped:
            case GenISAIntrinsic::GenISA_icmpxchgatomictyped:
            case GenISAIntrinsic::GenISA_icmpxchgatomicraw:
            case GenISAIntrinsic::GenISA_fcmpxchgatomicraw:
                is64BitPtr = false;
                break;
            case GenISAIntrinsic::GenISA_intatomicrawA64:
            case GenISAIntrinsic::GenISA_floatatomicrawA64:
            case GenISAIntrinsic::GenISA_icmpxchgatomicrawA64:
            case GenISAIntrinsic::GenISA_fcmpxchgatomicrawA64:
            default:
                is64BitPtr = true;
                break;
            }

            // clone atomicraw instructions
            llvm::SmallVector<llvm::Value*, 8> args;
            llvm::SmallVector<Type*, 3> types;

            PointerType* newptrType = PointerType::get(pBufferType, directAS);
            Value* sourcePointer = ConstantPointerNull::get(newptrType);
            Value* bufferAddress = nullptr;

            types.push_back(pIntr->getType());
            types.push_back(sourcePointer->getType());

            if (is64BitPtr)
            {
                if (!isa<ConstantPointerNull>(pBuffer))
                {
                    bufferAddress = pBuffer;
                }
                else
                {
                    bufferAddress = sourcePointer;
                }
                types.push_back(bufferAddress->getType());
            }
            else
            {
                bufferAddress = pIntr->getArgOperand(1);
                if (!isa<ConstantPointerNull>(pBuffer))
                {
                    IGC_ASSERT(isa<ConstantInt>(bufferAddress) && cast<ConstantInt>(bufferAddress)->getZExtValue() == 0);
                    IGC_ASSERT(pIntr->getIntrinsicID() != GenISAIntrinsic::GenISA_intatomictyped &&
                        pIntr->getIntrinsicID() != GenISAIntrinsic::GenISA_icmpxchgatomictyped);
                    bufferAddress = builder.CreatePtrToInt(pBuffer, builder.getInt32Ty());
                }
            }

            args.push_back(sourcePointer);
            args.push_back(bufferAddress);
            for (unsigned i = 2; i < IGCLLVM::getNumArgOperands(pIntr); i++)
            {
                args.push_back(pIntr->getArgOperand(i));
            }

            Module* module = pIntr->getParent()->getParent()->getParent();
            Function* pFunc = GenISAIntrinsic::getDeclaration(module, pIntr->getIntrinsicID(), types);
            pNewBufferAccessInst = builder.CreateCall(pFunc, args);
            break;
        }
        }

        if (pNewBufferAccessInst)
        {
            Instruction* oldInst = inst;
            Instruction* newInst = cast<Instruction>(pNewBufferAccessInst);

            // Clone metadata
            llvm::SmallVector<std::pair<unsigned, llvm::MDNode*>, 4> MDs;
            oldInst->getAllMetadata(MDs);
            for (llvm::SmallVectorImpl<std::pair<unsigned, llvm::MDNode*> >::iterator MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI)
            {
                newInst->setMetadata(MI->first, MI->second);
            }
            oldInst->replaceAllUsesWith(newInst);
            oldInst->eraseFromParent();
            canpromote = true;
        }
    }
    if (canpromote)
    {
        int handle = needBufferOffset ? bufferOffsetHandle : -1;
        if ((m_pCodeGenContext->m_buffersPromotedToDirectAS.find(bufferID) == m_pCodeGenContext->m_buffersPromotedToDirectAS.end()) ||
            (m_pCodeGenContext->m_buffersPromotedToDirectAS[bufferID] == -1))
        {
            m_pCodeGenContext->m_buffersPromotedToDirectAS[bufferID] = handle;
        }
        else
        {
            IGC_ASSERT((m_pCodeGenContext->m_buffersPromotedToDirectAS[bufferID] == handle));
        }
    }
}

void PromoteResourceToDirectAS::visitInstruction(Instruction& I)
{
    bool resourceAccessed = false;
    if (llvm::GenIntrinsicInst * pIntr = llvm::dyn_cast<llvm::GenIntrinsicInst>(&I))
    {
        // Figure out the intrinsic operands for texture & sampler
        llvm::Value* pTextureValue = nullptr, * pSamplerValue = nullptr;
        IGC::getTextureAndSamplerOperands(pIntr, pTextureValue, pSamplerValue);

        if (pTextureValue && pTextureValue->getType()->isPointerTy())
        {
            PromoteSamplerTextureToDirectAS(pIntr, pTextureValue);
            resourceAccessed = true;
        }
        if (pSamplerValue && pSamplerValue->getType()->isPointerTy())
        {
            PromoteSamplerTextureToDirectAS(pIntr, pSamplerValue);
            resourceAccessed = true;
        }
    }

    // Handle buffer access call instructions
    if (!resourceAccessed)
    {
        Value* bufptr = GetBufferOperand(&I);

        if (bufptr && bufptr->getType()->isPointerTy())
        {
            PromoteBufferToDirectAS(&I, bufptr);
            resourceAccessed = true;
        }
    }
}