LLVM  10.0.0
AMDGPUPrintfRuntimeBinding.cpp
Go to the documentation of this file.
1 //=== AMDGPUPrintfRuntimeBinding.cpp - OpenCL printf implementation -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // \file
9 //
10 // The pass bind printfs to a kernel arg pointer that will be bound to a buffer
11 // later by the runtime.
12 //
13 // This pass traverses the functions in the module and converts
14 // each call to printf to a sequence of operations that
15 // store the following into the printf buffer:
16 // - format string (passed as a module's metadata unique ID)
17 // - bitwise copies of printf arguments
18 // The backend passes will need to store metadata in the kernel
19 //===----------------------------------------------------------------------===//
20 
21 #include "AMDGPU.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/InitializePasses.h"
38 #include "llvm/Support/Debug.h"
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "printfToRuntime"
44 #define DWORD_ALIGN 4
45 
46 namespace {
47 class LLVM_LIBRARY_VISIBILITY AMDGPUPrintfRuntimeBinding final
48  : public ModulePass {
49 
50 public:
51  static char ID;
52 
53  explicit AMDGPUPrintfRuntimeBinding();
54 
55 private:
56  bool runOnModule(Module &M) override;
57  void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers,
58  StringRef fmt, size_t num_ops) const;
59 
60  bool shouldPrintAsStr(char Specifier, Type *OpType) const;
61  bool
62  lowerPrintfForGpu(Module &M,
63  function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
64 
65  void getAnalysisUsage(AnalysisUsage &AU) const override {
68  }
69 
71  return SimplifyInstruction(I, {*TD, TLI, DT});
72  }
73 
74  const DataLayout *TD;
75  const DominatorTree *DT;
77 };
78 } // namespace
79 
81 
82 INITIALIZE_PASS_BEGIN(AMDGPUPrintfRuntimeBinding,
83  "amdgpu-printf-runtime-binding", "AMDGPU Printf lowering",
84  false, false)
87 INITIALIZE_PASS_END(AMDGPUPrintfRuntimeBinding, "amdgpu-printf-runtime-binding",
89 
90 char &llvm::AMDGPUPrintfRuntimeBindingID = AMDGPUPrintfRuntimeBinding::ID;
91 
92 namespace llvm {
94  return new AMDGPUPrintfRuntimeBinding();
95 }
96 } // namespace llvm
97 
98 AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding()
99  : ModulePass(ID), TD(nullptr), DT(nullptr) {
101 }
102 
103 void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers(
104  SmallVectorImpl<char> &OpConvSpecifiers, StringRef Fmt,
105  size_t NumOps) const {
106  // not all format characters are collected.
107  // At this time the format characters of interest
108  // are %p and %s, which use to know if we
109  // are either storing a literal string or a
110  // pointer to the printf buffer.
111  static const char ConvSpecifiers[] = "cdieEfgGaosuxXp";
112  size_t CurFmtSpecifierIdx = 0;
113  size_t PrevFmtSpecifierIdx = 0;
114 
115  while ((CurFmtSpecifierIdx = Fmt.find_first_of(
116  ConvSpecifiers, CurFmtSpecifierIdx)) != StringRef::npos) {
117  bool ArgDump = false;
118  StringRef CurFmt = Fmt.substr(PrevFmtSpecifierIdx,
119  CurFmtSpecifierIdx - PrevFmtSpecifierIdx);
120  size_t pTag = CurFmt.find_last_of("%");
121  if (pTag != StringRef::npos) {
122  ArgDump = true;
123  while (pTag && CurFmt[--pTag] == '%') {
124  ArgDump = !ArgDump;
125  }
126  }
127 
128  if (ArgDump)
129  OpConvSpecifiers.push_back(Fmt[CurFmtSpecifierIdx]);
130 
131  PrevFmtSpecifierIdx = ++CurFmtSpecifierIdx;
132  }
133 }
134 
135 bool AMDGPUPrintfRuntimeBinding::shouldPrintAsStr(char Specifier,
136  Type *OpType) const {
137  if (Specifier != 's')
138  return false;
139  const PointerType *PT = dyn_cast<PointerType>(OpType);
140  if (!PT || PT->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
141  return false;
142  Type *ElemType = PT->getContainedType(0);
143  if (ElemType->getTypeID() != Type::IntegerTyID)
144  return false;
145  IntegerType *ElemIType = cast<IntegerType>(ElemType);
146  return ElemIType->getBitWidth() == 8;
147 }
148 
149 bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
150  Module &M, function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
151  LLVMContext &Ctx = M.getContext();
152  IRBuilder<> Builder(Ctx);
153  Type *I32Ty = Type::getInt32Ty(Ctx);
154  unsigned UniqID = 0;
155  // NB: This is important for this string size to be divizable by 4
156  const char NonLiteralStr[4] = "???";
157 
158  for (auto CI : Printfs) {
159  unsigned NumOps = CI->getNumArgOperands();
160 
161  SmallString<16> OpConvSpecifiers;
162  Value *Op = CI->getArgOperand(0);
163 
164  if (auto LI = dyn_cast<LoadInst>(Op)) {
165  Op = LI->getPointerOperand();
166  for (auto Use : Op->users()) {
167  if (auto SI = dyn_cast<StoreInst>(Use)) {
168  Op = SI->getValueOperand();
169  break;
170  }
171  }
172  }
173 
174  if (auto I = dyn_cast<Instruction>(Op)) {
175  Value *Op_simplified = simplify(I, &GetTLI(*I->getFunction()));
176  if (Op_simplified)
177  Op = Op_simplified;
178  }
179 
180  ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Op);
181 
182  if (ConstExpr) {
183  GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
184 
185  StringRef Str("unknown");
186  if (GVar && GVar->hasInitializer()) {
187  auto Init = GVar->getInitializer();
188  if (auto CA = dyn_cast<ConstantDataArray>(Init)) {
189  if (CA->isString())
190  Str = CA->getAsCString();
191  } else if (isa<ConstantAggregateZero>(Init)) {
192  Str = "";
193  }
194  //
195  // we need this call to ascertain
196  // that we are printing a string
197  // or a pointer. It takes out the
198  // specifiers and fills up the first
199  // arg
200  getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1);
201  }
202  // Add metadata for the string
203  std::string AStreamHolder;
204  raw_string_ostream Sizes(AStreamHolder);
205  int Sum = DWORD_ALIGN;
206  Sizes << CI->getNumArgOperands() - 1;
207  Sizes << ':';
208  for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
209  ArgCount <= OpConvSpecifiers.size();
210  ArgCount++) {
211  Value *Arg = CI->getArgOperand(ArgCount);
212  Type *ArgType = Arg->getType();
213  unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType);
214  ArgSize = ArgSize / 8;
215  //
216  // ArgSize by design should be a multiple of DWORD_ALIGN,
217  // expand the arguments that do not follow this rule.
218  //
219  if (ArgSize % DWORD_ALIGN != 0) {
220  llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx);
221  VectorType *LLVMVecType = llvm::dyn_cast<llvm::VectorType>(ArgType);
222  int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1;
223  if (LLVMVecType && NumElem > 1)
224  ResType = llvm::VectorType::get(ResType, NumElem);
225  Builder.SetInsertPoint(CI);
226  Builder.SetCurrentDebugLocation(CI->getDebugLoc());
227  if (OpConvSpecifiers[ArgCount - 1] == 'x' ||
228  OpConvSpecifiers[ArgCount - 1] == 'X' ||
229  OpConvSpecifiers[ArgCount - 1] == 'u' ||
230  OpConvSpecifiers[ArgCount - 1] == 'o')
231  Arg = Builder.CreateZExt(Arg, ResType);
232  else
233  Arg = Builder.CreateSExt(Arg, ResType);
234  ArgType = Arg->getType();
235  ArgSize = TD->getTypeAllocSizeInBits(ArgType);
236  ArgSize = ArgSize / 8;
237  CI->setOperand(ArgCount, Arg);
238  }
239  if (OpConvSpecifiers[ArgCount - 1] == 'f') {
240  ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg);
241  if (FpCons)
242  ArgSize = 4;
243  else {
244  FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
245  if (FpExt && FpExt->getType()->isDoubleTy() &&
246  FpExt->getOperand(0)->getType()->isFloatTy())
247  ArgSize = 4;
248  }
249  }
250  if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
251  if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
252  GlobalVariable *GV =
253  dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
254  if (GV && GV->hasInitializer()) {
255  Constant *Init = GV->getInitializer();
257  if (Init->isZeroValue() || CA->isString()) {
258  size_t SizeStr = Init->isZeroValue()
259  ? 1
260  : (strlen(CA->getAsCString().data()) + 1);
261  size_t Rem = SizeStr % DWORD_ALIGN;
262  size_t NSizeStr = 0;
263  LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr
264  << '\n');
265  if (Rem) {
266  NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
267  } else {
268  NSizeStr = SizeStr;
269  }
270  ArgSize = NSizeStr;
271  }
272  } else {
273  ArgSize = sizeof(NonLiteralStr);
274  }
275  } else {
276  ArgSize = sizeof(NonLiteralStr);
277  }
278  }
279  LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize
280  << " for type: " << *ArgType << '\n');
281  Sizes << ArgSize << ':';
282  Sum += ArgSize;
283  }
284  LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str()
285  << '\n');
286  for (size_t I = 0; I < Str.size(); ++I) {
287  // Rest of the C escape sequences (e.g. \') are handled correctly
288  // by the MDParser
289  switch (Str[I]) {
290  case '\a':
291  Sizes << "\\a";
292  break;
293  case '\b':
294  Sizes << "\\b";
295  break;
296  case '\f':
297  Sizes << "\\f";
298  break;
299  case '\n':
300  Sizes << "\\n";
301  break;
302  case '\r':
303  Sizes << "\\r";
304  break;
305  case '\v':
306  Sizes << "\\v";
307  break;
308  case ':':
309  // ':' cannot be scanned by Flex, as it is defined as a delimiter
310  // Replace it with it's octal representation \72
311  Sizes << "\\72";
312  break;
313  default:
314  Sizes << Str[I];
315  break;
316  }
317  }
318 
319  // Insert the printf_alloc call
320  Builder.SetInsertPoint(CI);
321  Builder.SetCurrentDebugLocation(CI->getDebugLoc());
322 
324  Attribute::NoUnwind);
325 
326  Type *SizetTy = Type::getInt32Ty(Ctx);
327 
328  Type *Tys_alloc[1] = {SizetTy};
329  Type *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 1);
330  FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false);
331  FunctionCallee PrintfAllocFn =
332  M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr);
333 
334  LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n');
335  std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str().c_str();
336  MDString *fmtStrArray = MDString::get(Ctx, fmtstr);
337 
338  // Instead of creating global variables, the
339  // printf format strings are extracted
340  // and passed as metadata. This avoids
341  // polluting llvm's symbol tables in this module.
342  // Metadata is going to be extracted
343  // by the backend passes and inserted
344  // into the OpenCL binary as appropriate.
345  StringRef amd("llvm.printf.fmts");
346  NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd);
347  MDNode *myMD = MDNode::get(Ctx, fmtStrArray);
348  metaD->addOperand(myMD);
349  Value *sumC = ConstantInt::get(SizetTy, Sum, false);
350  SmallVector<Value *, 1> alloc_args;
351  alloc_args.push_back(sumC);
352  CallInst *pcall =
353  CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI);
354 
355  //
356  // Insert code to split basicblock with a
357  // piece of hammock code.
358  // basicblock splits after buffer overflow check
359  //
360  ConstantPointerNull *zeroIntPtr =
362  ICmpInst *cmp =
363  dyn_cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, ""));
364  if (!CI->use_empty()) {
365  Value *result =
366  Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res");
367  CI->replaceAllUsesWith(result);
368  }
369  SplitBlock(CI->getParent(), cmp);
370  Instruction *Brnch =
371  SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false);
372 
373  Builder.SetInsertPoint(Brnch);
374 
375  // store unique printf id in the buffer
376  //
377  SmallVector<Value *, 1> ZeroIdxList;
378  ConstantInt *zeroInt =
379  ConstantInt::get(Ctx, APInt(32, StringRef("0"), 10));
380  ZeroIdxList.push_back(zeroInt);
381 
382  GetElementPtrInst *BufferIdx =
384  nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch));
385 
386  Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
387  Value *id_gep_cast =
388  new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch);
389 
390  StoreInst *stbuff =
391  new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast);
392  stbuff->insertBefore(Brnch); // to Remove unused variable warning
393 
394  SmallVector<Value *, 2> FourthIdxList;
395  ConstantInt *fourInt =
396  ConstantInt::get(Ctx, APInt(32, StringRef("4"), 10));
397 
398  FourthIdxList.push_back(fourInt); // 1st 4 bytes hold the printf_id
399  // the following GEP is the buffer pointer
400  BufferIdx = cast<GetElementPtrInst>(GetElementPtrInst::Create(
401  nullptr, pcall, FourthIdxList, "PrintBuffGep", Brnch));
402 
403  Type *Int32Ty = Type::getInt32Ty(Ctx);
404  Type *Int64Ty = Type::getInt64Ty(Ctx);
405  for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
406  ArgCount <= OpConvSpecifiers.size();
407  ArgCount++) {
408  Value *Arg = CI->getArgOperand(ArgCount);
409  Type *ArgType = Arg->getType();
410  SmallVector<Value *, 32> WhatToStore;
411  if (ArgType->isFPOrFPVectorTy() &&
412  (ArgType->getTypeID() != Type::VectorTyID)) {
413  Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
414  if (OpConvSpecifiers[ArgCount - 1] == 'f') {
415  ConstantFP *fpCons = dyn_cast<ConstantFP>(Arg);
416  if (fpCons) {
417  APFloat Val(fpCons->getValueAPF());
418  bool Lost = false;
420  &Lost);
421  Arg = ConstantFP::get(Ctx, Val);
422  IType = Int32Ty;
423  } else {
424  FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
425  if (FpExt && FpExt->getType()->isDoubleTy() &&
426  FpExt->getOperand(0)->getType()->isFloatTy()) {
427  Arg = FpExt->getOperand(0);
428  IType = Int32Ty;
429  }
430  }
431  }
432  Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch);
433  WhatToStore.push_back(Arg);
434  } else if (ArgType->getTypeID() == Type::PointerTyID) {
435  if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
436  const char *S = NonLiteralStr;
437  if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
438  GlobalVariable *GV =
439  dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
440  if (GV && GV->hasInitializer()) {
441  Constant *Init = GV->getInitializer();
443  if (Init->isZeroValue() || CA->isString()) {
444  S = Init->isZeroValue() ? "" : CA->getAsCString().data();
445  }
446  }
447  }
448  size_t SizeStr = strlen(S) + 1;
449  size_t Rem = SizeStr % DWORD_ALIGN;
450  size_t NSizeStr = 0;
451  if (Rem) {
452  NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
453  } else {
454  NSizeStr = SizeStr;
455  }
456  if (S[0]) {
457  char *MyNewStr = new char[NSizeStr]();
458  strcpy(MyNewStr, S);
459  int NumInts = NSizeStr / 4;
460  int CharC = 0;
461  while (NumInts) {
462  int ANum = *(int *)(MyNewStr + CharC);
463  CharC += 4;
464  NumInts--;
465  Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
466  WhatToStore.push_back(ANumV);
467  }
468  delete[] MyNewStr;
469  } else {
470  // Empty string, give a hint to RT it is no NULL
471  Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);
472  WhatToStore.push_back(ANumV);
473  }
474  } else {
475  uint64_t Size = TD->getTypeAllocSizeInBits(ArgType);
476  assert((Size == 32 || Size == 64) && "unsupported size");
477  Type *DstType = (Size == 32) ? Int32Ty : Int64Ty;
478  Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch);
479  WhatToStore.push_back(Arg);
480  }
481  } else if (ArgType->getTypeID() == Type::VectorTyID) {
482  Type *IType = NULL;
483  uint32_t EleCount = cast<VectorType>(ArgType)->getNumElements();
484  uint32_t EleSize = ArgType->getScalarSizeInBits();
485  uint32_t TotalSize = EleCount * EleSize;
486  if (EleCount == 3) {
487  IntegerType *Int32Ty = Type::getInt32Ty(ArgType->getContext());
488  Constant *Indices[4] = {
489  ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1),
490  ConstantInt::get(Int32Ty, 2), ConstantInt::get(Int32Ty, 2)};
491  Constant *Mask = ConstantVector::get(Indices);
492  ShuffleVectorInst *Shuffle = new ShuffleVectorInst(Arg, Arg, Mask);
493  Shuffle->insertBefore(Brnch);
494  Arg = Shuffle;
495  ArgType = Arg->getType();
496  TotalSize += EleSize;
497  }
498  switch (EleSize) {
499  default:
500  EleCount = TotalSize / 64;
501  IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
502  break;
503  case 8:
504  if (EleCount >= 8) {
505  EleCount = TotalSize / 64;
506  IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
507  } else if (EleCount >= 3) {
508  EleCount = 1;
509  IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
510  } else {
511  EleCount = 1;
512  IType = dyn_cast<Type>(Type::getInt16Ty(ArgType->getContext()));
513  }
514  break;
515  case 16:
516  if (EleCount >= 3) {
517  EleCount = TotalSize / 64;
518  IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
519  } else {
520  EleCount = 1;
521  IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
522  }
523  break;
524  }
525  if (EleCount > 1) {
526  IType = dyn_cast<Type>(VectorType::get(IType, EleCount));
527  }
528  Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch);
529  WhatToStore.push_back(Arg);
530  } else {
531  WhatToStore.push_back(Arg);
532  }
533  for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) {
534  Value *TheBtCast = WhatToStore[I];
535  unsigned ArgSize =
536  TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8;
537  SmallVector<Value *, 1> BuffOffset;
538  BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize));
539 
540  Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1);
541  Value *CastedGEP =
542  new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch);
543  StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch);
544  LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n"
545  << *StBuff << '\n');
546  (void)StBuff;
547  if (I + 1 == E && ArgCount + 1 == CI->getNumArgOperands())
548  break;
550  nullptr, BufferIdx, BuffOffset, "PrintBuffNextPtr", Brnch));
551  LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
552  << *BufferIdx << '\n');
553  }
554  }
555  }
556  }
557 
558  // erase the printf calls
559  for (auto CI : Printfs)
560  CI->eraseFromParent();
561 
562  Printfs.clear();
563  return true;
564 }
565 
566 bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
568  if (TT.getArch() == Triple::r600)
569  return false;
570 
571  auto PrintfFunction = M.getFunction("printf");
572  if (!PrintfFunction)
573  return false;
574 
575  for (auto &U : PrintfFunction->uses()) {
576  if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
577  if (CI->isCallee(&U))
578  Printfs.push_back(CI);
579  }
580  }
581 
582  if (Printfs.empty())
583  return false;
584 
585  if (auto HostcallFunction = M.getFunction("__ockl_hostcall_internal")) {
586  for (auto &U : HostcallFunction->uses()) {
587  if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
588  M.getContext().emitError(
589  CI, "Cannot use both printf and hostcall in the same module");
590  }
591  }
592  }
593 
594  TD = &M.getDataLayout();
595  auto DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
596  DT = DTWP ? &DTWP->getDomTree() : nullptr;
597  auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
598  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
599  };
600 
601  return lowerPrintfForGpu(M, GetTLI);
602 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:241
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2250
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:453
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
void push_back(const T &Elt)
Definition: SmallVector.h:211
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:918
void addOperand(MDNode *M)
Definition: Metadata.cpp:1088
This class represents a function call, abstracting a target machine&#39;s calling convention.
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:272
StringRef getAsCString() const
If this array is isCString(), then this method returns the array (without the trailing null byte) as ...
Definition: Constants.h:663
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:637
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
This instruction constructs a fixed permutation of two input vectors.
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition: Module.cpp:259
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:427
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2013
Metadata node.
Definition: Metadata.h:872
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:181
15: Pointers
Definition: Type.h:75
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:179
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:130
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1617
amdgpu printf runtime binding
ModulePass * createAMDGPUPrintfRuntimeBinding()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
A tuple of MDNodes.
Definition: Metadata.h:1334
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
constexpr char Printf[]
Key for HSA::Metadata::mPrintf.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:600
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
Definition: DerivedTypes.h:398
This class represents a cast from a pointer to an integer.
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
Class to represent function types.
Definition: DerivedTypes.h:108
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:889
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4525
std::string itostr(int64_t X)
Definition: StringExtras.h:238
This class represents a no-op cast from one type to another.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
An instruction for storing to memory.
Definition: Instructions.h:331
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:156
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:430
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2009
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:71
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:132
Value * getOperand(unsigned i) const
Definition: User.h:169
#define DWORD_ALIGN
Class to represent pointers.
Definition: DerivedTypes.h:579
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:66
11: Arbitrary bit width integers
Definition: Type.h:71
bool isFloatTy() const
Return true if this is &#39;float&#39;, a 32-bit IEEE fp type.
Definition: Type.h:147
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:892
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
char & AMDGPUPrintfRuntimeBindingID
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1500
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:690
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:73
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
INITIALIZE_PASS_BEGIN(AMDGPUPrintfRuntimeBinding, "amdgpu-printf-runtime-binding", "AMDGPU Printf lowering", false, false) INITIALIZE_PASS_END(AMDGPUPrintfRuntimeBinding
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
void initializeAMDGPUPrintfRuntimeBindingPass(PassRegistry &)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:607
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:129
Class to represent integer types.
Definition: DerivedTypes.h:40
size_t size() const
Definition: SmallVector.h:52
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:532
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
const APFloat & getValueAPF() const
Definition: Constants.h:302
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:155
hexagon bit simplify
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
16: SIMD &#39;packed&#39; format, or other vector type
Definition: Type.h:76
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:134
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
A constant pointer value that points to null.
Definition: Constants.h:539
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:767
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
Class to represent vector types.
Definition: DerivedTypes.h:432
Class for arbitrary precision integers.
Definition: APInt.h:69
iterator_range< user_iterator > users()
Definition: Value.h:420
static const size_t npos
Definition: StringRef.h:56
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:614
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:401
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:222
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:185
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:143
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:516
LLVM Value Representation.
Definition: Value.h:74
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A single uniqued string.
Definition: Metadata.h:604
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
#define LLVM_DEBUG(X)
Definition: Debug.h:122
This class represents an extension of floating point types.
amdgpu printf runtime AMDGPU Printf lowering
bool isDoubleTy() const
Return true if this is &#39;double&#39;, a 64-bit IEEE fp type.
Definition: Type.h:150
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:178
static Constant * get(ArrayRef< Constant *> V)
Definition: Constants.cpp:1161
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
Address space for constant memory (VTX2).
Definition: AMDGPU.h:275
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:972
IntegerType * Int32Ty
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition: Type.h:336
bool isString(unsigned CharSize=8) const
This method returns true if this is an array of CharSize integers.
Definition: Constants.cpp:2849