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