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