CGGPUBuiltin.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //===------ CGGPUBuiltin.cpp - Codegen for GPU builtins -------------------===//
  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. //
  9. // Generates code for built-in GPU calls which are not runtime-specific.
  10. // (Runtime-specific codegen lives in programming model specific files.)
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenFunction.h"
  14. #include "clang/Basic/Builtins.h"
  15. #include "llvm/IR/DataLayout.h"
  16. #include "llvm/IR/Instruction.h"
  17. #include "llvm/Support/MathExtras.h"
  18. using namespace clang;
  19. using namespace CodeGen;
  20. static llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
  21. llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
  22. llvm::Type::getInt8PtrTy(M.getContext())};
  23. llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
  24. llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
  25. if (auto* F = M.getFunction("vprintf")) {
  26. // Our CUDA system header declares vprintf with the right signature, so
  27. // nobody else should have been able to declare vprintf with a bogus
  28. // signature.
  29. assert(F->getFunctionType() == VprintfFuncType);
  30. return F;
  31. }
  32. // vprintf doesn't already exist; create a declaration and insert it into the
  33. // module.
  34. return llvm::Function::Create(
  35. VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
  36. }
  37. // Transforms a call to printf into a call to the NVPTX vprintf syscall (which
  38. // isn't particularly special; it's invoked just like a regular function).
  39. // vprintf takes two args: A format string, and a pointer to a buffer containing
  40. // the varargs.
  41. //
  42. // For example, the call
  43. //
  44. // printf("format string", arg1, arg2, arg3);
  45. //
  46. // is converted into something resembling
  47. //
  48. // struct Tmp {
  49. // Arg1 a1;
  50. // Arg2 a2;
  51. // Arg3 a3;
  52. // };
  53. // char* buf = alloca(sizeof(Tmp));
  54. // *(Tmp*)buf = {a1, a2, a3};
  55. // vprintf("format string", buf);
  56. //
  57. // buf is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of the
  58. // args is itself aligned to its preferred alignment.
  59. //
  60. // Note that by the time this function runs, E's args have already undergone the
  61. // standard C vararg promotion (short -> int, float -> double, etc.).
  62. RValue
  63. CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E,
  64. ReturnValueSlot ReturnValue) {
  65. assert(getTarget().getTriple().isNVPTX());
  66. assert(E->getBuiltinCallee() == Builtin::BIprintf);
  67. assert(E->getNumArgs() >= 1); // printf always has at least one arg.
  68. const llvm::DataLayout &DL = CGM.getDataLayout();
  69. llvm::LLVMContext &Ctx = CGM.getLLVMContext();
  70. CallArgList Args;
  71. EmitCallArgs(Args,
  72. E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
  73. E->arguments(), E->getDirectCallee(),
  74. /* ParamsToSkip = */ 0);
  75. // We don't know how to emit non-scalar varargs.
  76. if (std::any_of(Args.begin() + 1, Args.end(), [&](const CallArg &A) {
  77. return !A.getRValue(*this).isScalar();
  78. })) {
  79. CGM.ErrorUnsupported(E, "non-scalar arg to printf");
  80. return RValue::get(llvm::ConstantInt::get(IntTy, 0));
  81. }
  82. // Construct and fill the args buffer that we'll pass to vprintf.
  83. llvm::Value *BufferPtr;
  84. if (Args.size() <= 1) {
  85. // If there are no args, pass a null pointer to vprintf.
  86. BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
  87. } else {
  88. llvm::SmallVector<llvm::Type *, 8> ArgTypes;
  89. for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
  90. ArgTypes.push_back(Args[I].getRValue(*this).getScalarVal()->getType());
  91. // Using llvm::StructType is correct only because printf doesn't accept
  92. // aggregates. If we had to handle aggregates here, we'd have to manually
  93. // compute the offsets within the alloca -- we wouldn't be able to assume
  94. // that the alignment of the llvm type was the same as the alignment of the
  95. // clang type.
  96. llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
  97. llvm::Value *Alloca = CreateTempAlloca(AllocaTy);
  98. for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
  99. llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
  100. llvm::Value *Arg = Args[I].getRValue(*this).getScalarVal();
  101. Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlignment(Arg->getType()));
  102. }
  103. BufferPtr = Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
  104. }
  105. // Invoke vprintf and return.
  106. llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule());
  107. return RValue::get(Builder.CreateCall(
  108. VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr}));
  109. }