File: PayloadMapping.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 (730 lines) | stat: -rw-r--r-- 22,654 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

//===----------------------------------------------------------------------===//
//
// A helper (static) class that returns a mapping from message generating
// intrinsic (e.g. sample, load, urb_write) arguments to their respective
// positions in the payload message.
//
//===----------------------------------------------------------------------===//

#include "Compiler/CISACodeGen/PayloadMapping.hpp"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/Platform.hpp"
#include "Compiler/CISACodeGen/helper.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Function.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/BasicBlock.h>
#include "common/LLVMWarningsPop.hpp"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;

///
const Instruction* PayloadMapping::GetSupremumOfNonHomogeneousPart(
    const Instruction* inst1,
    const Instruction* inst2)
{
    const GenIntrinsicInst* intrinsicInst1 = dyn_cast<GenIntrinsicInst>(inst1);
    const GenIntrinsicInst* intrinsicInst2 = dyn_cast<GenIntrinsicInst>(inst2);

    IGC_ASSERT(intrinsicInst1);
    IGC_ASSERT(intrinsicInst2);
    IGC_ASSERT(intrinsicInst1->getIntrinsicID() == GenISAIntrinsic::GenISA_RTWrite);
    IGC_ASSERT(intrinsicInst2->getIntrinsicID() == GenISAIntrinsic::GenISA_RTWrite);

    switch (intrinsicInst1->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_RTWrite:
        return GetSupremumOfNonHomogeneousPart_RTWrite(inst1, inst2);
    default:
        IGC_ASSERT_MESSAGE(0, "should not be called at all for other intrinsics");
        return nullptr;
    }
}


/// Helper method.
/// compare rtwi1 and rtwi2 w.r.t. oMask
/// biased towards rtwi1:
/// X X -> return rtwi1
/// X 2 -> return rtwi2
/// 1 X -> return rtwi1
/// 1 2 -> return rtwi1
static const Instruction* CompareOnMask(
    const RTWritIntrinsic* rtwi1,
    const RTWritIntrinsic* rtwi2)
{
    if (rtwi1->hasMask() && rtwi2->hasMask())
    {
        return rtwi1;
    }
    /// rtwi1 S0 < rtwi2 S0
    else if (!rtwi1->hasMask() && rtwi2->hasMask())
    {
        return rtwi2;
    }
    /// rtwi1 S0 > rtwi2 S0
    else if (rtwi1->hasMask() && !rtwi2->hasMask())
    {
        return rtwi1;
    }

    return rtwi1;
}

///
const Instruction* PayloadMapping::GetSupremumOfNonHomogeneousPart_RTWrite(
    const Instruction* inst1,
    const Instruction* inst2)
{
    IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst1));
    IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst2));
    ModuleMetaData* modMD = m_CodeGenContext->getModuleMetaData();
    const RTWritIntrinsic* rtwi1 = cast<RTWritIntrinsic>(inst1);
    const RTWritIntrinsic* rtwi2 = cast<RTWritIntrinsic>(inst2);

    /// rtwi1 S0 == rtwi2 S0
    if (RTWriteHasSource0Alpha(rtwi1, modMD) && RTWriteHasSource0Alpha(rtwi2, modMD))
    {
        return CompareOnMask(rtwi1, rtwi2);
    }
    /// rtwi1 X < rtwi2 S0
    else if (!RTWriteHasSource0Alpha(rtwi1, modMD) && RTWriteHasSource0Alpha(rtwi2, modMD))
    {
        return CompareOnMask(rtwi2, rtwi1) == rtwi2 ? rtwi2 : nullptr;
    }
    /// rtwi1 S0 > rtwi2 S0
    else if (RTWriteHasSource0Alpha(rtwi1, m_CodeGenContext->getModuleMetaData()) && !RTWriteHasSource0Alpha(rtwi2, modMD))
    {
        return CompareOnMask(rtwi1, rtwi2) == rtwi1 ? rtwi1 : nullptr;
    }

    return nullptr;
}


/// Return the 'reserved' left offset of modeled payload (in bytes).
/// By default, homogeneous payload messages returns 0, but if one
/// wants to model the 'reserved' part of the payload (which cannot
/// be coalesced due to non-homogeneous) this method should handle
/// that intrinsic and return the offset (in units of bytes).
int PayloadMapping::GetLeftReservedOffset(const Instruction* inst, SIMDMode simdMode)
{
    const GenIntrinsicInst* intrinsicInst = dyn_cast<GenIntrinsicInst>(inst);
    IGC_ASSERT(intrinsicInst);

    switch (intrinsicInst->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_RTWrite:
        IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst));
        return GetLeftReservedOffset_RTWrite(cast<RTWritIntrinsic>(inst), simdMode);
    case GenISAIntrinsic::GenISA_RTDualBlendSource:
        IGC_ASSERT(llvm::isa<llvm::RTDualBlendSourceIntrinsic>(inst));
        return GetLeftReservedOffset_RTWrite(cast<RTDualBlendSourceIntrinsic>(inst), simdMode);
    default:
        return 0;
    }
}

///
template <typename T>
int PayloadMapping::GetLeftReservedOffset_RTWrite(const T* inst, SIMDMode simdMode)
{
    int offset = 0;

    if (inst->hasMask())
    {
        //Output mask is always fixed size regardless of SIMD mode.
        offset += m_CodeGenContext->platform.getGRFSize();
    }

    if (RTWriteHasSource0Alpha(inst, m_CodeGenContext->getModuleMetaData()))
    {
        IGC_ASSERT(simdMode == SIMDMode::SIMD8 || simdMode == SIMDMode::SIMD16);
        int multiplier = inst->getSource0Alpha()->getType()->isHalfTy() ? 1 : 2;
        if (simdMode == SIMDMode::SIMD8)
        {
            offset += m_CodeGenContext->platform.getGRFSize(); //always 1GRF, regardless of HF
        }
        else if (simdMode == SIMDMode::SIMD16)
        {
            offset += m_CodeGenContext->platform.getGRFSize() * multiplier; // 2GRFs normal precision, 1GRF HF precision
        }
    }

    return offset;
}

///
int PayloadMapping::GetRightReservedOffset(const Instruction* inst, SIMDMode simdMode)
{
    const GenIntrinsicInst* intrinsicInst = dyn_cast<GenIntrinsicInst>(inst);
    IGC_ASSERT(intrinsicInst);

    switch (intrinsicInst->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_RTWrite:
        IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst));
        return GetRightReservedOffset_RTWrite(cast<RTWritIntrinsic>(inst), simdMode);
    case GenISAIntrinsic::GenISA_RTDualBlendSource:
        IGC_ASSERT(llvm::isa<llvm::RTDualBlendSourceIntrinsic>(inst));
        return GetRightReservedOffset_RTWrite(cast<RTDualBlendSourceIntrinsic>(inst), simdMode);
    default:
        return 0;
    }
}

template <typename T>
int PayloadMapping::GetRightReservedOffset_RTWrite(const T* inst, SIMDMode simdMode)
{
    int offset = 0;

    if (inst->hasStencil())
    {
        IGC_ASSERT(m_CodeGenContext->platform.supportsStencil(simdMode));
        offset += m_CodeGenContext->platform.getGRFSize();
    }

    if (inst->hasDepth())
    {

        IGC_ASSERT(simdMode == SIMDMode::SIMD8 || simdMode == SIMDMode::SIMD16);

        //Depth always has normal precision, regardless of the rest of the fields.
        if (simdMode == SIMDMode::SIMD8)
        {
            offset += m_CodeGenContext->platform.getGRFSize(); //always 1GRF, 32 bytes
        }
        else if (simdMode == SIMDMode::SIMD16)
        {
            offset += m_CodeGenContext->platform.getGRFSize() * 2; // 2GRFs normal precision, 64 bytes
        }
    }

    return offset;
}

/// Returns true if a given intrinsic (and its specific configuration) will produce
/// non-homogeneous payload (the one in which some elements are not 'expanded' with simd mode)
/// e.g. RT writes with oMask
bool PayloadMapping::HasNonHomogeneousPayloadElements(const Instruction* inst)
{
    const GenIntrinsicInst* intrinsicInst = dyn_cast<GenIntrinsicInst>(inst);
    IGC_ASSERT(intrinsicInst);

    switch (intrinsicInst->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_RTWrite:
        IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst));
        return HasNonHomogeneousPayloadElements_RTWrite(cast<RTWritIntrinsic>(inst));
    case GenISAIntrinsic::GenISA_RTDualBlendSource:
        IGC_ASSERT(llvm::isa<llvm::RTDualBlendSourceIntrinsic>(inst));
        return HasNonHomogeneousPayloadElements_RTWrite(cast<RTDualBlendSourceIntrinsic>(inst));
    default:
        return false;
    }

}

///
template <typename T>
bool PayloadMapping::HasNonHomogeneousPayloadElements_RTWrite(const T* inst)
{
    if (inst->hasMask())
    {
        return true;
    }
    if (RTWriteHasSource0Alpha(inst, m_CodeGenContext->getModuleMetaData()))
    {
        return true;
    }
    if (inst->hasDepth())
    {
        return true;
    }
    if (inst->hasStencil())
    {
        return true;
    }

    return false;
}

/// \brief
bool PayloadMapping::IsUndefOrZeroImmediate(const Value* value)
{
    if (llvm::isa<llvm::UndefValue>(value))
    {
        return true;
    }

    if (const llvm::ConstantInt * CInt = llvm::dyn_cast<llvm::ConstantInt>(value)) {
        if (CInt->getZExtValue() == 0) {
            return true;
        }
    }

    if (const llvm::ConstantFP * CFP = llvm::dyn_cast<llvm::ConstantFP>(value))
    {
        APInt api = CFP->getValueAPF().bitcastToAPInt();
        if (api.getZExtValue() == 0)
        {
            return true;
        }
    }
    return false;
}

/// Return the number of payload elements that this instruction will
/// generate.
uint PayloadMapping::GetNumPayloadElements(const Instruction* inst)
{
    const GenIntrinsicInst* intrinsicInst = dyn_cast<GenIntrinsicInst>(inst);
    IGC_ASSERT(intrinsicInst);
    if (const SampleIntrinsic * sampleInst = dyn_cast<SampleIntrinsic>(inst))
    {
        return GetNumPayloadElements_Sample(sampleInst);
    }
    else if (const SamplerLoadIntrinsic * sampleInst = dyn_cast<SamplerLoadIntrinsic>(inst))
    {
        return GetNumPayloadElements_LDMS(sampleInst);
    }

    switch (intrinsicInst->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_URBWrite:
        return GetNumPayloadElements_URBWrite(intrinsicInst);
    case GenISAIntrinsic::GenISA_RTWrite:
        return GetNumPayloadElements_RTWrite(intrinsicInst);
    case GenISAIntrinsic::GenISA_RTDualBlendSource:
        return GetNumPayloadElements_DSRTWrite(intrinsicInst);
    default:
        break;
    }

    IGC_ASSERT(0);
    return 0;
}

/// \brief
uint PayloadMapping::GetNumPayloadElements_LDMS(const GenIntrinsicInst* inst)
{
    const uint numOperands = inst->getNumOperands();

    //Subtract the offsets, and texture resource, lod to get
    //the number of texture coordinates and index to texture source
    uint numSources = numOperands - 5;

    for (uint i = numSources - 1; i > 0; i--)
    {
        if (IsUndefOrZeroImmediate(inst->getOperand(i)))
        {
            numSources--;
        }
        else
        {
            break;
        }
    }

    return numSources;
}

/// \brief Adjusts the number of sources for a sampler, based on a sampler type.
void PayloadMapping::ValidateNumberofSources(EOPCODE opCode, bool isCube, uint& numberofSrcs)
{
    switch (opCode)
    {
    case llvm_sampleptr:
    case llvm_sample_killpix:
    case llvm_lodptr:
    {
        if (m_CodeGenContext->platform.getWATable().Wa_14012688258 && isCube)
        {
            numberofSrcs = numberofSrcs >= 3 ? numberofSrcs : 3;
        }
    }
    break;

    case llvm_sample_bptr:
    case llvm_sample_cptr:
    case llvm_sample_lptr:
    {
        if (m_CodeGenContext->platform.getWATable().Wa_14012688258 && isCube)
        {
            numberofSrcs = numberofSrcs >= 4 ? numberofSrcs : 4;
        }

        switch (numberofSrcs)
        {
        case 1:
            numberofSrcs++;
            break;
        }
    }
    break;

    case llvm_sample_dptr:
    {
        if (m_CodeGenContext->platform.getWATable().Wa_14012688258 && isCube)
        {
            numberofSrcs = numberofSrcs >= 7 ? numberofSrcs : 7;
        }

        switch (numberofSrcs)
        {
        case 1:
            numberofSrcs++;
        case 2:
            numberofSrcs++;
            break;
        case 5:
            numberofSrcs++;
            break;
        case 8:
            if (!m_CodeGenContext->platform.supports3DAndCubeSampleD())
            {
                break;
            }
            numberofSrcs++;
            break;
        }
    }
    break;
    case llvm_sample_dcptr:
    {
        if (m_CodeGenContext->platform.getWATable().Wa_14012688258 && isCube)
        {
            numberofSrcs = numberofSrcs >= 8 ? numberofSrcs : 8;
        }

        switch (numberofSrcs)
        {
        case 2:
            numberofSrcs++;
        case 3:
            numberofSrcs++;
            break;
        case 5:
            numberofSrcs++;
        case 6:
            numberofSrcs++;
            break;
        case 8:
            numberofSrcs++;
        case 9:
            numberofSrcs++;
            break;
        }
    }
    break;

    case llvm_sample_lcptr:
    case llvm_sample_bcptr:
    {
        if (m_CodeGenContext->platform.getWATable().Wa_14012688258 && isCube)
        {
            numberofSrcs = numberofSrcs >= 5 ? numberofSrcs : 5;
        }

        switch (numberofSrcs)
        {
        case 1:
            numberofSrcs++;
        case 2:
            numberofSrcs++;
            break;
        }
    }
    break;
    default:
        break;
    }

}

/// \brief Provides the total number of payload elements for a sample.
///
/// Does not consider 'peeling' of the first element for split-send.
/// The peeling itself is supposed to be done by a wrapper method.
uint PayloadMapping::GetNonAdjustedNumPayloadElements_Sample(const SampleIntrinsic* inst)
{
    const unsigned int  numOperands = inst->getNumOperands();
    unsigned int numSources = numOperands - 6;

    if (inst->IsLODInst())
    {
        //Subtract resource and sampler sources to get
        //the number of texture coordinates and index to texture source
        numSources = numOperands - 3;
    }

    //Check for valid number of sources from the end of the list
    for (uint i = (numSources - 1); i >= 1; i--)
    {
        if (IsUndefOrZeroImmediate(inst->getOperand(i)))
        {
            numSources--;
        }
        else
        {
            break;
        }
    }

    //temp solution to send valid sources but having 0 as their values.
    EOPCODE opCode = GetOpCode(inst);
    llvm::Type* cubeTextureType = GetResourceDimensionType(*inst->getModule(), RESOURCE_DIMENSION_TYPE::DIM_CUBE_TYPE);
    llvm::Type* cubeArrayTextureType = GetResourceDimensionType(*inst->getModule(), RESOURCE_DIMENSION_TYPE::DIM_CUBE_ARRAY_TYPE);
    llvm::Type* textureType = inst->getTextureValue()->getType()->getPointerElementType();
    bool isCube = (textureType == cubeTextureType || textureType == cubeArrayTextureType);
    ValidateNumberofSources(opCode, isCube, numSources);

    if (IsZeroLOD(inst))
    {
        numSources--;
    }

    return numSources;
}

/// \brief Gets the adjusted number of payload elements for sampler.
///
/// Whether to peel is determined by the target GEN architecture.
uint PayloadMapping::GetNumPayloadElements_Sample(const SampleIntrinsic* inst)
{
    unsigned int numSources = GetNonAdjustedNumPayloadElements_Sample(inst);
    return numSources;
}

/// \brief Determines whether sample instruction has LOD of zero
bool PayloadMapping::IsZeroLOD(const SampleIntrinsic* inst)
{
    const CPlatform& platform = m_CodeGenContext->platform;

    if (platform.supportSampleAndLd_lz() && !platform.WaDisableSampleLz())
    {
        if (const SampleIntrinsic * sampleInst = dyn_cast<SampleIntrinsic>(inst))
        {
            return sampleInst->ZeroLOD();
        }
    }
    return false;
}

///
uint PayloadMapping::GetNumPayloadElements_URBWrite(const GenIntrinsicInst* inst)
{

    //FIXME: this was copied from EmitPass::emitURBWrite.
    //find a way to unify this, so not to cause sudden troubles if it is
    //modified there

    uint size = 0;
    if (llvm::isa<llvm::ConstantInt>(inst->getOperand(1)))
    {
        uint mask = (uint)llvm::cast<llvm::ConstantInt>(inst->getOperand(1))->getZExtValue();
        size = iSTD::bsr(mask) + 1;
    }
    else
    {
        // All 4 elements will be send - we don't know which are masked out.
        size = 4;
    }

    return size;
}

/// \brief Returns the number of homogeneous slots in payload.
///
/// RT write complete format: s0Alpha oM [R G B A] sZ oS
/// Only [R G B A] part forms the homogeneous sequence. (Though one can
/// also include s0Alpha in the homogeneous part if oM is not present, but that
/// seemed not to give good results in terms of coalescing efficiency).
uint PayloadMapping::GetNumPayloadElements_RTWrite(const GenIntrinsicInst* inst)
{
    const int numElements = 4; //4 colors, always form homogeneous 'part'.
    return numElements;
}

///
Value* PayloadMapping::GetPayloadElementToValueMapping_URBWrite(const GenIntrinsicInst* inst, uint index)
{
    //First 3 operands are immediates with specific meaning (not part of payload)
    return inst->getOperand(index + 2);
}

/// \brief Gets non-adjusted mapping from the payload element index to intrinsic value(argument) index.
///
/// Peeling is not applied here.
uint PayloadMapping::GetNonAdjustedPayloadElementIndexToValueIndexMapping_sample(
    const SampleIntrinsic* inst, uint index)
{
    const bool zeroLOD = IsZeroLOD(inst);
    uint startIndex = zeroLOD ? 1 : 0;

    GenISAIntrinsic::ID IID = inst->getIntrinsicID();
    //Here we want to get 'C', but to skip 'L'.
    // C L ...
    if (!(zeroLOD &&
        index == 0 &&
        IID == GenISAIntrinsic::GenISA_sampleLCptr))
    {
        index = index + startIndex;
    }

    return index;
}

/// \brief Gets payload element index to value mapping, adjusted with splitting decision(peeling).
Value* PayloadMapping::GetPayloadElementToValueMapping_sample(const SampleIntrinsic* inst, const uint index)
{
    uint valueIndex = GetNonAdjustedPayloadElementIndexToValueIndexMapping_sample(inst, index);
    return inst->getOperand(valueIndex);
}

/// \brief Gets payload element index to value mapping for RT writes.
Value* PayloadMapping::GetPayloadElementToValueMapping_RTWrite(const GenIntrinsicInst* inst, const uint index)
{
    //s0Alpha oM[R G B A] sZ oS
    IGC_ASSERT(index < GetNumPayloadElements(inst));
    IGC_ASSERT(llvm::isa<llvm::RTWritIntrinsic>(inst));
    const RTWritIntrinsic* rtwi = cast<RTWritIntrinsic>(inst);

    if (index < 4)
    {
        switch (index)
        {
        case 0: return rtwi->getRed();
        case 1: return rtwi->getGreen();
        case 2: return rtwi->getBlue();
        case 3: return rtwi->getAlpha();
        }
    }

    return nullptr;
}

Value* PayloadMapping::GetPayloadElementToValueMapping_LDMS(const SamplerLoadIntrinsic* inst, const uint index)
{
    uint valueIndex = index;

    return inst->getOperand(valueIndex);
}


Value* PayloadMapping::GetPayloadElementToValueMapping(const Instruction* inst, uint index)
{
    IGC_ASSERT(index < GetNumPayloadElements(inst));

    const llvm::GenIntrinsicInst* intrinsicInst = dyn_cast<GenIntrinsicInst>(inst);
    IGC_ASSERT(intrinsicInst);

    std::pair<const llvm::Instruction*, uint> instIndexPair(inst, index);

    PayloadMappingCache::iterator cachedValue = m_PayloadMappingCache.find(instIndexPair);
    if (cachedValue != m_PayloadMappingCache.end())
    {
        return cachedValue->second;
    }

    Value* payloadValue = nullptr;

    if (const SampleIntrinsic * sampleInst = dyn_cast<SampleIntrinsic>(inst))
    {
        payloadValue = GetPayloadElementToValueMapping_sample(sampleInst, index);
        IGC_ASSERT(payloadValue != nullptr);
        m_PayloadMappingCache.insert(std::pair<std::pair<const llvm::Instruction*, uint>, Value*>(instIndexPair, payloadValue));
        return payloadValue;
    }
    else if (const SamplerLoadIntrinsic * sampleInst = dyn_cast<SamplerLoadIntrinsic>(inst))
    {
        payloadValue = GetPayloadElementToValueMapping_LDMS(sampleInst, index);
        IGC_ASSERT(payloadValue != nullptr);
        m_PayloadMappingCache.insert(std::pair<std::pair<const llvm::Instruction*, uint>, Value*>(instIndexPair, payloadValue));
        return payloadValue;
    }


    switch (intrinsicInst->getIntrinsicID())
    {
    case GenISAIntrinsic::GenISA_URBWrite:
        payloadValue = GetPayloadElementToValueMapping_URBWrite(intrinsicInst, index);
        IGC_ASSERT(payloadValue != nullptr);
        m_PayloadMappingCache.insert(std::pair<std::pair<const llvm::Instruction*, uint>, Value*>(instIndexPair, payloadValue));
        return payloadValue;
    case GenISAIntrinsic::GenISA_RTWrite:
        payloadValue = GetPayloadElementToValueMapping_RTWrite(intrinsicInst, index);
        IGC_ASSERT(payloadValue != nullptr);
        m_PayloadMappingCache.insert(std::pair<std::pair<const llvm::Instruction*, uint>, Value*>(instIndexPair, payloadValue));
        return payloadValue;
    case GenISAIntrinsic::GenISA_RTDualBlendSource:
        payloadValue = GetPayloadElementToValueMapping_DSRTWrite(intrinsicInst, index);
        IGC_ASSERT(payloadValue != nullptr);
        m_PayloadMappingCache.insert(std::pair<std::pair<const llvm::Instruction*, uint>, Value*>(instIndexPair, payloadValue));
        return payloadValue;
    default:
        break;
    }

    IGC_ASSERT(0);
    return NULL;
}

/// \brief Determines whether the payload is being split by peeling the first element.
bool PayloadMapping::DoPeelFirstElement(const Instruction* inst)
{
    if (dyn_cast<SamplerLoadIntrinsic>(inst))
    {
        return true;
    }

    return false;
}

/// \brief Returns the number of homogeneous slots in dual-source payload.
///
/// dual-source RT write complete format: oM [R0 G0 B0 A0 R1 G1 B1 A1] sZ oS
/// Only [R0 G0 B0 A0 R1 G1 B1 A1] part forms the homogeneous sequence.
uint PayloadMapping::GetNumPayloadElements_DSRTWrite(const GenIntrinsicInst* inst)
{
    IGC_ASSERT(llvm::isa<llvm::RTDualBlendSourceIntrinsic>(inst));
    return 8; //8 colors, always form homogeneous 'part'.
}

/// \brief Gets payload element index to value mapping for dual-source RT writes.
Value* PayloadMapping::GetPayloadElementToValueMapping_DSRTWrite(const GenIntrinsicInst* inst, const uint index)
{
    //oM [R0 G0 B0 A0 R1 G1 B1 A1] sZ oS
    IGC_ASSERT(index < GetNumPayloadElements(inst));
    IGC_ASSERT(llvm::isa<llvm::RTDualBlendSourceIntrinsic>(inst));
    const RTDualBlendSourceIntrinsic* dsrtwi = cast<RTDualBlendSourceIntrinsic>(inst);

    if (index < 8)
    {
        switch (index)
        {
        case 0: return dsrtwi->getRed0();
        case 1: return dsrtwi->getGreen0();
        case 2: return dsrtwi->getBlue0();
        case 3: return dsrtwi->getAlpha0();
        case 4: return dsrtwi->getRed1();
        case 5: return dsrtwi->getGreen1();
        case 6: return dsrtwi->getBlue1();
        case 7: return dsrtwi->getAlpha1();
        }
    }

    return nullptr;
}