File: OpenCLPrintfResolution.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (762 lines) | stat: -rw-r--r-- 33,104 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "AdaptorCommon/ImplicitArgs.hpp"
#include "Compiler/Optimizer/OpenCLPasses/OpenCLPrintf/OpenCLPrintfResolution.hpp"
#include "Compiler/Optimizer/OpenCLPasses/OpenCLPrintf/OpenCLPrintfAnalysis.hpp"
#include "Compiler/IGCPassSupport.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/IR/Attributes.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvmWrapper/Support/Alignment.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/Instructions.h>
#include "common/LLVMWarningsPop.hpp"
#include "ShaderTypesEnum.h"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;

// Register pass to igc-opt
#define PASS_FLAG "igc-opencl-printf-resolution"
#define PASS_DESCRIPTION "Resolves OpenCL printf calls"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(OpenCLPrintfResolution, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(OpenCLPrintfResolution, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char OpenCLPrintfResolution::ID = 0;

//
// FORMAT OF PRINTF OUTPUT BUFFER:
// ================================
/*
  ======================================================================
  | DWORD  bufferSize             Size of the buffer in bytes          |  <-- This value is incremented by atomic_add
  |====================================================================|
  | DWORD  stringIndex_ch_0       Index of format string for channel 0 |  \
  |--------------------------------------------------------------------|  |
  | DWORD  data0Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data0                  Data for channel 0                   |  |
  |--------------------------------------------------------------------|  | Channel 0 data
  | . . .  . . .                                                       |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data1Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data1                  Data for channel 0                   |  /
  |====================================================================|
  | DWORD  stringIndex_ch_1       Index of format string for channel 1 |  \
  |--------------------------------------------------------------------|  |
  | DWORD  data0Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data0                  Data for channel 1                   |  |
  |--------------------------------------------------------------------|  |  Channel 1 data
  | . . .  . . .                                                       |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data0Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data1                  Data for channel 1                   |  /
  |====================================================================|
  | . . .  . . .                                                       |
  | . . .  . . .                                                       |
  | . . .  . . .                                                       |
  |====================================================================|
  | DWORD  stringIndex_ch_N       Index of format string for channel N |  \
  |--------------------------------------------------------------------|  |
  | DWORD  data0Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data0                  Data for channel N                   |  |
  |--------------------------------------------------------------------|  |  Channel N data
  | . . .  . . .                                                       |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data0Type              Type identifier                      |  |
  |--------------------------------------------------------------------|  |
  | DWORD  data1                  Data for channel N                   |  /
  |--------------------------------------------------------------------|
*/

// For vector arguments, 2 type identifiers are used: 1st is IGC::SHADER_PRINTF_VECTOR_*  and 2nd is the vector length.
// These 2 type identifiers are followed by the elements of the vector.
// Example: float4
//
// |------------------------------|
// |  IGC::SHADER_PRINTF_VECTOR_FLOAT  |
// |------------------------------|
// |             0x4              |
// |------------------------------|
// |      < vec_element_0 >       |
// |------------------------------|
// |      < vec_element_1 >       |
// |------------------------------|
// |      < vec_element_2 >       |
// |------------------------------|
// |      < vec_element_3 >       |
// |------------------------------|

OpenCLPrintfResolution::OpenCLPrintfResolution() : FunctionPass(ID), m_atomicAddFunc(nullptr) {
  initializeOpenCLPrintfResolutionPass(*PassRegistry::getPassRegistry());
}

bool IGC::OpenCLPrintfResolution::doInitialization(Module &M) {
  m_module = (IGCLLVM::Module *)&M;
  m_context = &M.getContext();
  m_CGContext = nullptr;
  m_stringIndex = 0;
  m_ptrSizeIntType = M.getDataLayout().getIntPtrType(*m_context, ADDRESS_SPACE_GLOBAL);
  m_int32Type = Type::getInt32Ty(*m_context);

  return FunctionPass::doInitialization(M);
}

bool OpenCLPrintfResolution::runOnFunction(Function &F) {
  if (m_CGContext == nullptr) {
    m_CGContext = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
    m_fp64Supported = !m_CGContext->platform.hasNoFP64Inst();
  }

  // Gather all found printf calls into the m_printfCalls vector.
  visit(F);

  bool changed = !m_printfCalls.empty();

  // Put strings found in the printf calls into metadata.
  // Replace the printf calls with sequences of instructions that
  // writes data into printf output buffer.
  for (CallInst *printfCall : m_printfCalls) {
    m_DL = printfCall->getDebugLoc();
    expandPrintfCall(*printfCall, F);
  }

  m_printfCalls.clear();

  return changed;
}

void OpenCLPrintfResolution::visitCallInst(CallInst &callInst) {
  if (!callInst.getCalledFunction()) {
    return;
  }

  StringRef funcName = callInst.getCalledFunction()->getName();
  if (funcName == OpenCLPrintfAnalysis::OPENCL_PRINTF_FUNCTION_NAME ||
      funcName == OpenCLPrintfAnalysis::BUILTIN_PRINTF_FUNCTION_NAME) {
    m_printfCalls.push_back(&callInst);
  }
}

Value *OpenCLPrintfResolution::processPrintfString(Value *arg, Function &F) {
  GlobalVariable *formatString = nullptr;

  if (isa<GlobalVariable>(arg)) {
    formatString = dyn_cast_or_null<GlobalVariable>(arg);
    if ((nullptr == formatString) || !formatString->hasInitializer()) {
      IGC_ASSERT_MESSAGE(0, "Unexpected printf argument (expected string literal)");
      return ConstantInt::get(m_int32Type, -1);
    }

    return arg;
  } else if (CastInst *castInst = dyn_cast<CastInst>(arg)) {
    return processPrintfString(castInst->getOperand(0), F);
  } else if (GetElementPtrInst *getElemPtrInst = dyn_cast<GetElementPtrInst>(arg)) {
    IGC_ASSERT_MESSAGE(getElemPtrInst->hasAllZeroIndices(), "Only All Zero indices GEP supported");
    return processPrintfString(getElemPtrInst->getPointerOperand(), F);
  } else if (SelectInst *selectInst = dyn_cast<SelectInst>(arg)) {
    SelectInst *selectInst2 =
        SelectInst::Create(selectInst->getOperand(0), processPrintfString(selectInst->getOperand(1), F),
                           processPrintfString(selectInst->getOperand(2), F), "", selectInst);
    // TODO: Clean up the original select within the current pass
    return selectInst2;
  } else if (PHINode *phiNode = dyn_cast<PHINode>(arg)) {
    unsigned inNum = phiNode->getNumIncomingValues();
    PHINode *newPhi = nullptr;
    for (unsigned i = 0; i < inNum; ++i) {
      Value *newIV = processPrintfString(phiNode->getIncomingValue(i), F);
      if (!newPhi)
        newPhi = PHINode::Create(newIV->getType(), inNum, "", phiNode);
      newPhi->addIncoming(newIV, phiNode->getIncomingBlock(i));
    }
    // TODO: Clean up the original PHI node within the current pass
    return newPhi;
  } else {
    IGC_ASSERT_MESSAGE(0, "Unsupported Instruction!");
  }

  return ConstantInt::get(m_int32Type, -1);
}

// Checks pathes to global variables and returns true if all paths lead to constant strings.
// Only these instructions acepted in pathes:
// * a CastInst
// * a GEP with all-zero indices
// * a SelectInst
// * a PHINode
// It is expected that the paths are not looped.
bool OpenCLPrintfResolution::argIsString(Value *arg) {
  if (isa<GlobalVariable>(arg)) {
    GlobalVariable *formatString = dyn_cast_or_null<GlobalVariable>(arg);
    if (nullptr == formatString || !formatString->hasInitializer()) {
      return false;
    }
    Constant *initializer = formatString->getInitializer();
    if (initializer->isZeroValue() && initializer->getType()->isArrayTy()) {
      // Is zeroinitializer; can't be casted to ConstantDataArray.
      // This caused zeroinitializers to be treated as non-strings,
      // which caused assertion errors.
      return true;
    }
    ConstantDataArray *formatStringConst = dyn_cast<ConstantDataArray>(initializer);
    if (!formatStringConst || !formatStringConst->isCString()) {
      return false;
    }
    return true;
  } else if (CastInst *castInst = dyn_cast<CastInst>(arg)) {
    return argIsString(castInst->getOperand(0));
  }
  if (GetElementPtrInst *getElemPtrInst = dyn_cast<GetElementPtrInst>(arg)) {
    return getElemPtrInst->hasAllZeroIndices() && argIsString(getElemPtrInst->getPointerOperand());
  } else if (SelectInst *selectInst = dyn_cast<SelectInst>(arg)) {
    return argIsString(selectInst->getOperand(1)) && argIsString(selectInst->getOperand(2));
  } else if (PHINode *phiNode = dyn_cast<PHINode>(arg)) {
    for (unsigned i = 0; i < phiNode->getNumIncomingValues(); i++) {
      if (!argIsString(phiNode->getIncomingValue(i)))
        return false;
    }
    return true;
  }
  return false;
}

std::string OpenCLPrintfResolution::getPrintfStringsMDNodeName(Function &F) { return "printf.strings"; }

static StoreInst *genStoreInternal(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd, const DebugLoc &DL,
                                   bool isNontemporal) {
  bool isVolatile = false;
  unsigned Align = 4;
  auto SI = new llvm::StoreInst(Val, Ptr, isVolatile, IGCLLVM::getCorrectAlign(Align), InsertAtEnd);
  SI->setDebugLoc(DL);
  if (isNontemporal) {
    Constant *One = ConstantInt::get(Type::getInt32Ty(SI->getContext()), 1);
    MDNode *Node = MDNode::get(SI->getContext(), ConstantAsMetadata::get(One));
    SI->setMetadata(LLVMContext::MD_nontemporal, Node);
  }
  return SI;
}

void OpenCLPrintfResolution::removeExcessArgs() {
  SPrintfArgDescriptor *formatStringArgDesc = &m_argDescriptors[0];

  Value *formatString = formatStringArgDesc->value;
  IGC::SHADER_PRINTF_TYPE dataType = formatStringArgDesc->argType;
  IGC_ASSERT(dataType == SHADER_PRINTF_STRING_LITERAL);

  if (auto GV = dyn_cast<GlobalVariable>(formatString)) {
    IGC_ASSERT(GV->hasInitializer());

    unsigned int numFormatSpecifiers = 0;

    Constant *initializer = GV->getInitializer();
    if (initializer->isZeroValue() && initializer->getType()->isArrayTy())
      // The string literal is empty, can't be casted to ConstantDataArray
      // and there is no way it contains any format specifiers.
      numFormatSpecifiers = 0;
    else
      numFormatSpecifiers = getNumFormatSpecifiers(cast<ConstantDataArray>(GV->getInitializer()));

    if (m_argDescriptors.size() > numFormatSpecifiers + 1)
      m_argDescriptors.erase(m_argDescriptors.begin() + numFormatSpecifiers + 1, m_argDescriptors.end());
  }
}

void OpenCLPrintfResolution::expandPrintfCall(CallInst &printfCall, Function &F) {
  /* Replace a printf call with IR instructions that fill the printf
     output buffer created by the Runtime:
     --------------------------------------------------------------------------
           bufferPtr      - pointer to the printf output buffer. This pointer
                            is an implicit kernel argument. It is loaded into
                            GRF as part of thread payload.
           bufferSize     - size of the printf output buffer. By agreement with
                            Runtime, it is 4 Mb.
           dataSize       - size of printf data for current thread.

     Note: we use STATELESS mode for printf buffer access.
    ---------------------------------------------------------------------------
        writeOffset = atomic_add(bufferPtr, dataSize);
        writePtr = bufferPtr + writeOffset;
        endOffset = writeOffset + dataSize;
        if (endOffset < bufferSize) {                \
            // Write the format string index         |
            *writePtr = stringIndex;                 |
            writePtr += 4;                           |
                                                     |
            // Write the argument type               |
            *writePtr = argument[1].dataType;        |
            writePtr += 4;                           |
            // Write the argument value              |
            *writePtr = argument[1].value;           |
            writePtr += 4;                           | bblockTrue
            . . .                                    |
            . . .                                    |
            // Write the argument type               |
            *writePtr = argument[N].dataType;        |
            writePtr += 4;                           |
            // Write the argument value              |
            *writePtr = argument[N].value;           |
            writePtr += 4;                           |
                                                     |
            // printf returns 0 if successful        |
            return_val = 0;                          /
        }
        else {                                                           \
            // Check if the remaining output                             |
            // buffer space is enough for writing                        |
            //invalid string index.                                      |
            endOffset = writeOffset + 4;                                 |
            if (endOffset < bufferSize) {           \                    | bblockFalse
                // Write the invalid string index.  | bblockErrorString  |
                *writePtr = -1;                     |                    |
            }                                       /                    |
            // printf returns -1 if failed                               |
            return_val = -1;                                             /
       }

       We also support printf to any provided buffer.
       This is done with special builtin with following signature:
       int __builtin_IB_printf_to_buffer(global char* buf, global char* currentOffset, int bufSize, ...);
         buf - pointer to the begging of the buffer.
         currentOffset - pointer to the location with the current offset that will be atomically incremented.
           In the case of regular printf this offset is on the first DWORD of printfBuffer.
           E.g. in assert buffer it is on the third DWORD.
          bufSize - total size of the buffer.
       Note: in the case of builtin printf, all the stores will be nontemporal.


     ----------------------------------------------------------------------
  */
  MetaDataUtils *MdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
  ImplicitArgs implicitArgs(F, MdUtils);
  bool isPrintfBuiltin = OpenCLPrintfAnalysis::isBuiltinPrintf(printfCall.getCalledFunction());

  BasicBlock *currentBBlock = printfCall.getParent();

  // Put all printf argument into m_argDescriptors vector.
  // Scalarize vector arguments and substitute string arguments by their indices.
  preprocessPrintfArgs(printfCall);

  removeExcessArgs();

  // writeOffset = atomic_add(bufferPtr, dataSize)
  Value *basebufferPtr = isPrintfBuiltin ? printfCall.getArgOperand(0)
                                         : implicitArgs.getImplicitArgValue(F, ImplicitArg::PRINTF_BUFFER, MdUtils);

  Value *dataSizeVal = ConstantInt::get(m_int32Type, getTotalDataSize());
  Value *currentOffsetPtr = isPrintfBuiltin ? printfCall.getArgOperand(1) : basebufferPtr;
  Instruction *writeOffsetStart = genAtomicAdd(currentOffsetPtr, dataSizeVal, printfCall, "write_offset");
  writeOffsetStart->setDebugLoc(m_DL);

  Instruction *writeOffset = writeOffsetStart;
  Instruction *writeOffsetPtr = nullptr;

  // end_offset = write_offset + data_size
  Instruction *endOffset = BinaryOperator::CreateAdd(writeOffset, dataSizeVal, "end_offset", &printfCall);
  endOffset->setDebugLoc(m_DL);

  Value *bufferMaxSize = isPrintfBuiltin
                             ? printfCall.getArgOperand(2)
                             : ConstantInt::get(m_int32Type, m_CGContext->m_DriverInfo.getPrintfBufferSize());

  // write_ptr = buffer_ptr + write_offset;
  if (m_ptrSizeIntType != writeOffset->getType()) {
    writeOffset =
        CastInst::Create(Instruction::CastOps::ZExt, writeOffset, m_ptrSizeIntType, "write_offset", &printfCall);
    writeOffset->setDebugLoc(m_DL);
  }
  Instruction *bufferPtr =
      CastInst::Create(Instruction::CastOps::PtrToInt, basebufferPtr, m_ptrSizeIntType, "buffer_ptr", &printfCall);
  bufferPtr->setDebugLoc(m_DL);
  Instruction *writeOffsetAdd = BinaryOperator::CreateAdd(bufferPtr, writeOffset, "write_offset", &printfCall);
  writeOffsetAdd->setDebugLoc(m_DL);
  writeOffset = writeOffsetAdd;

  // if (end_offset < output_buffer_size))
  Instruction *cmp1 = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULE, endOffset, bufferMaxSize, "", &printfCall);
  cmp1->setDebugLoc(m_DL);

  // Since we need to insert a branch here, the current basic block should be
  // splitted into two parts.
  BasicBlock *bblockJoin = currentBBlock->splitBasicBlock(BasicBlock::iterator(printfCall), "bblockJoin");

  // Create "true" and "false" branches.
  BasicBlock *bblockTrue = BasicBlock::Create(*m_context, "write_offset_true", &F, bblockJoin);
  BasicBlock *bblockFalse = BasicBlock::Create(*m_context, "write_offset_false", &F, bblockJoin);

  currentBBlock->getTerminator()->eraseFromParent();
  BranchInst *brInst = BranchInst::Create(bblockTrue, bblockFalse, cmp1, currentBBlock);
  brInst->setDebugLoc(m_DL);

  //  ----------- Fill "true" block ----------------

  // write_offset += 4;
  Value *constVal4 = ConstantInt::get(m_ptrSizeIntType, 4);

  for (size_t i = 0, size = m_argDescriptors.size(); i < size; ++i) {
    SPrintfArgDescriptor *argDesc = &m_argDescriptors[i];
    Value *printfArg = argDesc->value;
    IGC::SHADER_PRINTF_TYPE dataType = argDesc->argType;

    // We don't store the dataType for format string (which is the first entry in m_argDescriptors).
    if (i != 0) {
      // *write_offset = argument[i].dataType
      Value *argTypeVal = ConstantInt::get(m_int32Type, (unsigned int)dataType);
      writeOffsetPtr =
          CastInst::Create(Instruction::CastOps::IntToPtr, writeOffset, m_int32Type->getPointerTo(ADDRESS_SPACE_GLOBAL),
                           "write_offset_ptr", bblockTrue);
      writeOffsetPtr->setDebugLoc(m_DL);
      genStoreInternal(argTypeVal, writeOffsetPtr, bblockTrue, m_DL, isPrintfBuiltin);

      // write_offset += 4
      writeOffset = BinaryOperator::CreateAdd(writeOffset, constVal4, "write_offset", bblockTrue);
      writeOffset->setDebugLoc(m_DL);

      // For vector arguments, add vector size after type ID.
      if (argDesc->vecSize > 0) {
        Value *vecSizeVal = ConstantInt::get(m_int32Type, argDesc->vecSize);
        writeOffsetPtr =
            CastInst::Create(Instruction::CastOps::IntToPtr, writeOffset,
                             m_int32Type->getPointerTo(ADDRESS_SPACE_GLOBAL), "write_offset_ptr", bblockTrue);
        writeOffsetPtr->setDebugLoc(m_DL);
        genStoreInternal(vecSizeVal, writeOffsetPtr, bblockTrue, m_DL, isPrintfBuiltin);

        // write_offset += 4
        writeOffset = BinaryOperator::CreateAdd(writeOffset, constVal4, "write_offset", bblockTrue);
        writeOffset->setDebugLoc(m_DL);
      }
    }

    writeOffsetPtr = generateCastToPtr(argDesc, writeOffset, bblockTrue);
    writeOffsetPtr->setDebugLoc(m_DL);

    if (dataType == SHADER_PRINTF_STRING_LITERAL) {
      printfArg = CastInst::Create(Instruction::CastOps::PtrToInt, argDesc->value, Type::getInt64Ty(*m_context), "",
                                   bblockTrue);
    }

    // *write_offset = argument[i].value
    genStoreInternal(printfArg, writeOffsetPtr, bblockTrue, m_DL, isPrintfBuiltin);

    // write_offset += argument[i].size
    Value *offsetInc = ConstantInt::get(m_ptrSizeIntType, getArgTypeSize(dataType, argDesc->vecSize));
    writeOffset = BinaryOperator::CreateAdd(writeOffset, offsetInc, "write_offset", bblockTrue);
    writeOffset->setDebugLoc(m_DL);
  } // for (SPrintfArgDescriptor *argDesc : m_argDescriptors)

  brInst = BranchInst::Create(bblockJoin, bblockTrue);
  brInst->setDebugLoc(m_DL);

  //  ----------- Fill "false" block ----------------
  // end_offset = write_offset + 4
  Value *constVal4_32 = ConstantInt::get(m_int32Type, 4);
  endOffset = BinaryOperator::CreateAdd(writeOffsetStart, constVal4_32, "end_offset", bblockFalse);
  endOffset->setDebugLoc(m_DL);
  // if (end_offset < output_buffer_size)
  Instruction *cmp2 = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULE, endOffset, bufferMaxSize, "", bblockFalse);
  cmp2->setDebugLoc(m_DL);
  // Here, we generate code that checks if the error string index can be
  // written into the output buffer.
  BasicBlock *bblockErrorString = BasicBlock::Create(*m_context, "write_error_string", &F, bblockJoin);
  BasicBlock *bblockFalseJoin = BasicBlock::Create(*m_context, "bblockFalseJoin", &F, bblockJoin);

  brInst = BranchInst::Create(bblockErrorString, bblockFalseJoin, cmp2, bblockFalse);
  brInst->setDebugLoc(m_DL);

  // *writeOffset = -1;
  Value *constValErrStringIdx = ConstantInt::get(m_int32Type, -1);
  writeOffsetPtr =
      CastInst::Create(Instruction::CastOps::IntToPtr, writeOffsetAdd, m_int32Type->getPointerTo(ADDRESS_SPACE_GLOBAL),
                       "write_offset_ptr", bblockErrorString);
  writeOffsetPtr->setDebugLoc(m_DL);
  genStoreInternal(constValErrStringIdx, writeOffsetPtr, bblockErrorString, m_DL, isPrintfBuiltin);
  brInst = BranchInst::Create(bblockFalseJoin, bblockErrorString);
  brInst->setDebugLoc(m_DL);

  // bblockFalseJoin is an empty basic block,
  // it is needed to assure bblockJoin have only 2 predecessors.
  brInst = BranchInst::Create(bblockJoin, bblockFalseJoin);
  brInst->setDebugLoc(m_DL);

  // return_val = select cmp1, 0, -1
  Value *constVal0 = ConstantInt::get(m_int32Type, 0);
  Value *constValm1 = ConstantInt::get(m_int32Type, -1);
  Instruction *returnVal = SelectInst::Create(cmp1, constVal0, constValm1, "printf_ret_val", &printfCall);
  returnVal->setDebugLoc(m_DL);

  printfCall.replaceAllUsesWith(returnVal);
  printfCall.eraseFromParent();
  m_argDescriptors.clear();
}

Value *OpenCLPrintfResolution::fixupPrintfArg(CallInst &printfCall, Value *arg, IGC::SHADER_PRINTF_TYPE &argDataType) {
  // For string argument, add the string to the metadata and put the string index
  // into the vector of arguments.
  switch (argDataType) {
  case IGC::SHADER_PRINTF_STRING_LITERAL: {
    Function *F = printfCall.getParent()->getParent();
    return processPrintfString(arg, *F);
  } break;
  case IGC::SHADER_PRINTF_POINTER: {
    Instruction *tmp = CastInst::Create(Instruction::CastOps::PtrToInt, arg, m_ptrSizeIntType, "", &printfCall);
    tmp->setDebugLoc(m_DL);
    return tmp;
  } break;
  case IGC::SHADER_PRINTF_FLOAT:
  case IGC::SHADER_PRINTF_VECTOR_FLOAT:
  case IGC::SHADER_PRINTF_DOUBLE:
  case IGC::SHADER_PRINTF_VECTOR_DOUBLE:
    // Cast halfs back to float. Cast doubles to floats if the platform does not support double fp type.
    if (arg->getType()->getScalarType()->isHalfTy() ||
        (!m_fp64Supported && arg->getType()->getScalarType()->isDoubleTy())) {
      if (argDataType == IGC::SHADER_PRINTF_DOUBLE)
        argDataType = IGC::SHADER_PRINTF_FLOAT;
      if (argDataType == IGC::SHADER_PRINTF_VECTOR_DOUBLE)
        argDataType = IGC::SHADER_PRINTF_VECTOR_FLOAT;

      if (ConstantFP *constVal = dyn_cast<ConstantFP>(arg)) {
        // If this is a constant, just replace it.
        bool ignored;
        APFloat FV = constVal->getValueAPF();
        FV.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &ignored);
        return ConstantFP::get(arg->getContext(), FV);
      } else if (CastInst *fpCastVal = dyn_cast<CastInst>(arg)) {
        // If this is a fpcast, use the origin value.
        Type *srcType = fpCastVal->getSrcTy();
        if (srcType->isFloatTy() ||
            (srcType->isVectorTy() && cast<VectorType>(srcType)->getElementType()->isFloatTy())) {
          return fpCastVal->getOperand(0);
        }
      }

      Type *newType = Type::getFloatTy(arg->getContext());
      if (auto argVT = dyn_cast<IGCLLVM::FixedVectorType>(arg->getType())) {
        newType = IGCLLVM::FixedVectorType::get(newType, (unsigned)argVT->getNumElements());
      }

      Instruction *tmp = CastInst::CreateFPCast(arg, newType, "to_float", &printfCall);
      tmp->setDebugLoc(m_DL);
      return tmp;
    }
    break;

  default:
    break;
  }

  return arg;
}

unsigned OpenCLPrintfResolution::getNumFormatSpecifiers(const ConstantDataArray *dataArray) {
  unsigned int count = 0;
  StringRef formatString = dataArray->getRawDataValues();

  const size_t length = formatString.size();
  for (size_t i = 0; i < length; i++) {
    if (formatString[i] == '%') {
      if (i + 1 < length && formatString[i + 1] == '%') {
        i++;
        continue;
      }

      count++;
    }
  }

  return count;
}

void OpenCLPrintfResolution::preprocessPrintfArgs(CallInst &printfCall) {
  int i = 0;
  if (OpenCLPrintfAnalysis::isBuiltinPrintf(printfCall.getCalledFunction())) {
    // printf builtin function has buffer pointer, current offset pointer and buffer size as first three arguments.
    // Skip them here, as we want to collect the arguments starting from format string.
    i = 3;
  }
  for (int numArgs = IGCLLVM::getNumArgOperands(&printfCall); i < numArgs; ++i) {
    Value *arg = printfCall.getOperand(i);
    Type *argType = arg->getType();
    IGC::SHADER_PRINTF_TYPE argDataType = getPrintfArgDataType(arg);
    arg = fixupPrintfArg(printfCall, arg, argDataType);
    uint vecSize = 0;
    if (auto argVType = dyn_cast<IGCLLVM::FixedVectorType>(argType)) {
      vecSize = (uint)argVType->getNumElements();
    }
    m_argDescriptors.push_back(SPrintfArgDescriptor(argDataType, arg, vecSize));
  }
}

CallInst *OpenCLPrintfResolution::genAtomicAdd(Value *outputBufferPtr, Value *dataSize, CallInst &printfCall,
                                               StringRef name) {
  // outputBufferPtr->getType() could be "i8 addrspace(1)*", and the atomic prototype
  // requires "i32 addrspace(1)*":
  //
  //   %writeOffset = call i32 @__builtin_IB_atomic_add_global_i32(i32 addrspace(1)* <outputBufferPtr>,
  //                                                               i32 <dataSize>)
  //
  Type *bufPtrType = Type::getInt32PtrTy(*m_context, ADDRESS_SPACE_GLOBAL);
  if (outputBufferPtr->getType() != bufPtrType) {
    outputBufferPtr =
        CastInst::Create(Instruction::CastOps::BitCast, outputBufferPtr, bufPtrType, "ptrBC", &printfCall);
  }

  if (m_atomicAddFunc == nullptr) {
    Type *argTypes[] = {outputBufferPtr->getType(), dataSize->getType()};
    FunctionType *atomicFuncType = FunctionType::get(dataSize->getType(), argTypes, false);
    m_atomicAddFunc = cast<Function>(
        m_module->getOrInsertFunction("__builtin_IB_atomic_add_global_i32", atomicFuncType, AttributeList()));
  }
  std::vector<Value *> args;
  args.push_back(outputBufferPtr);
  args.push_back(dataSize);

  return CallInst::Create(m_atomicAddFunc, args, name, &printfCall);
}

unsigned int OpenCLPrintfResolution::getArgTypeSize(IGC::SHADER_PRINTF_TYPE argType, uint vecSize) {
  switch (argType) {
  case IGC::SHADER_PRINTF_LONG:
  case IGC::SHADER_PRINTF_DOUBLE:
  case IGC::SHADER_PRINTF_POINTER: // Runtime expects 64 bit value for pointer regardless of its actual size.
    return 8;
  case IGC::SHADER_PRINTF_VECTOR_LONG:
  case IGC::SHADER_PRINTF_VECTOR_DOUBLE:
    return vecSize * 8;

  case IGC::SHADER_PRINTF_STRING_LITERAL: {
    // The size of the format string address
    return 8;
  }

  default:
    if (vecSize > 0) {
      return vecSize * 4;
    } else {
      return 4;
    }
  }
}

unsigned int OpenCLPrintfResolution::getTotalDataSize() {
  IGC_ASSERT_MESSAGE(m_argDescriptors.size() > 0, "Empty printf arguments list.");
  unsigned int dataSize = 0;
  SPrintfArgDescriptor *argDesc = &m_argDescriptors[0];
  // Add the size that represents a format string.
  dataSize += getArgTypeSize(argDesc->argType, argDesc->vecSize);

  // Skip 0-th operand (format string) and count total size of
  // the remaining arguments.
  for (size_t i = 1, size = m_argDescriptors.size(); i < size; ++i) {
    argDesc = &m_argDescriptors[i];
    // Add size of the data type identifier.
    dataSize += 4;
    // Vector arguments require additional type identifier - number of elements.
    if (argDesc->vecSize > 0) {
      dataSize += 4;
    }
    // Add size of the data itself.
    dataSize += getArgTypeSize(argDesc->argType, argDesc->vecSize);
  }
  return dataSize;
}

IGC::SHADER_PRINTF_TYPE OpenCLPrintfResolution::getPrintfArgDataType(Value *printfArg) {
  Type *argType = printfArg->getType();

  if (auto argVType = dyn_cast<VectorType>(argType)) {
    Type *elemType = argVType->getElementType();
    if (elemType->isFloatingPointTy()) {
      if (elemType->isDoubleTy())
        return IGC::SHADER_PRINTF_VECTOR_DOUBLE;
      else
        return IGC::SHADER_PRINTF_VECTOR_FLOAT;
    } else if (elemType->isIntegerTy()) {
      unsigned int typeSize = elemType->getScalarSizeInBits();
      switch (typeSize) {
      case 8:
        return IGC::SHADER_PRINTF_VECTOR_BYTE;
      case 16:
        return IGC::SHADER_PRINTF_VECTOR_SHORT;
      case 32:
        return IGC::SHADER_PRINTF_VECTOR_INT;
      case 64:
        return IGC::SHADER_PRINTF_VECTOR_LONG;
      }
    }
  } else if (argType->isFloatingPointTy()) {
    if (argType->isDoubleTy())
      return IGC::SHADER_PRINTF_DOUBLE;
    else
      return IGC::SHADER_PRINTF_FLOAT;
  } else if (argType->isIntegerTy()) {
    unsigned int typeSize = argType->getScalarSizeInBits();
    switch (typeSize) {
    case 8:
      return IGC::SHADER_PRINTF_BYTE;
    case 16:
      return IGC::SHADER_PRINTF_SHORT;
    case 32:
      return IGC::SHADER_PRINTF_INT;
    case 64:
      return IGC::SHADER_PRINTF_LONG;
    }
  } else if (argIsString(printfArg)) {
    return IGC::SHADER_PRINTF_STRING_LITERAL;
  } else if (argType->isPointerTy()) {
    return IGC::SHADER_PRINTF_POINTER;
  }
  return IGC::SHADER_PRINTF_INVALID;
}

Instruction *OpenCLPrintfResolution::generateCastToPtr(SPrintfArgDescriptor *argDesc, Value *writeOffset,
                                                       BasicBlock *bblock) {
  Type *castedType = nullptr;

  switch (argDesc->argType) {
  case IGC::SHADER_PRINTF_BYTE:
  case IGC::SHADER_PRINTF_SHORT:
  case IGC::SHADER_PRINTF_INT:
  case IGC::SHADER_PRINTF_LONG:
  case IGC::SHADER_PRINTF_FLOAT:
  case IGC::SHADER_PRINTF_DOUBLE:
  case IGC::SHADER_PRINTF_VECTOR_BYTE:
  case IGC::SHADER_PRINTF_VECTOR_SHORT:
  case IGC::SHADER_PRINTF_VECTOR_INT:
  case IGC::SHADER_PRINTF_VECTOR_LONG:
  case IGC::SHADER_PRINTF_VECTOR_FLOAT:
  case IGC::SHADER_PRINTF_VECTOR_DOUBLE: {
    Type *origType = argDesc->value->getType();
    castedType = origType->getPointerTo(ADDRESS_SPACE_GLOBAL);
    break;
  }

  case IGC::SHADER_PRINTF_STRING_LITERAL: {
    castedType = Type::getInt64PtrTy(*m_context, ADDRESS_SPACE_GLOBAL);
    break;
  }
  case IGC::SHADER_PRINTF_POINTER:
    castedType = m_ptrSizeIntType->getPointerTo(ADDRESS_SPACE_GLOBAL);
    break;

  default:
    IGC_ASSERT_MESSAGE(0, "Unexpected printf argument type");
    break;
  }

  return CastInst::Create(Instruction::CastOps::IntToPtr, writeOffset, castedType, "write_offset_ptr", bblock);
}