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 
14 #include "CodeGenFunction.h"
15 #include "clang/Basic/Builtins.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Instruction.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h"
20 
21 using namespace clang;
22 using namespace CodeGen;
23 
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);
29 
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
33     // signature.
34     assert(F->getFunctionType() == VprintfFuncType);
35     return F;
36   }
37 
38   // vprintf doesn't already exist; create a declaration and insert it into the
39   // module.
40   return llvm::Function::Create(
41       VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
42 }
43 
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
47 // the varargs.
48 //
49 // For example, the call
50 //
51 //   printf("format string", arg1, arg2, arg3);
52 //
53 // is converted into something resembling
54 //
55 //   struct Tmp {
56 //     Arg1 a1;
57 //     Arg2 a2;
58 //     Arg3 a3;
59 //   };
60 //   char* buf = alloca(sizeof(Tmp));
61 //   *(Tmp*)buf = {a1, a2, a3};
62 //   vprintf("format string", buf);
63 //
64 // buf is aligned to the max of {alignof(Arg1), ...}.  Furthermore, each of the
65 // args is itself aligned to its preferred alignment.
66 //
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.).
69 
70 namespace {
71 llvm::Value *packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF,
72                                            const CallArgList &Args) {
73   const llvm::DataLayout &DL = CGF->CGM.getDataLayout();
74   llvm::LLVMContext &Ctx = CGF->CGM.getLLVMContext();
75   CGBuilderTy &Builder = CGF->Builder;
76 
77   // Construct and fill the args buffer that we'll pass to vprintf.
78   if (Args.size() <= 1) {
79     // If there are no args, pass a null pointer to vprintf.
80     return llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
81   } else {
82     llvm::SmallVector<llvm::Type *, 8> ArgTypes;
83     for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
84       ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
85 
86     // Using llvm::StructType is correct only because printf doesn't accept
87     // aggregates.  If we had to handle aggregates here, we'd have to manually
88     // compute the offsets within the alloca -- we wouldn't be able to assume
89     // that the alignment of the llvm type was the same as the alignment of the
90     // clang type.
91     llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
92     llvm::Value *Alloca = CGF->CreateTempAlloca(AllocaTy);
93 
94     for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
95       llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
96       llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
97       Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlign(Arg->getType()));
98     }
99     return Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
100   }
101 }
102 } // namespace
103 
104 RValue
105 CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E,
106                                                ReturnValueSlot ReturnValue) {
107   assert(getTarget().getTriple().isNVPTX());
108   assert(E->getBuiltinCallee() == Builtin::BIprintf);
109   assert(E->getNumArgs() >= 1); // printf always has at least one arg.
110 
111   CallArgList Args;
112   EmitCallArgs(Args,
113                E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
114                E->arguments(), E->getDirectCallee(),
115                /* ParamsToSkip = */ 0);
116 
117   // We don't know how to emit non-scalar varargs.
118   if (llvm::any_of(llvm::drop_begin(Args), [&](const CallArg &A) {
119         return !A.getRValue(*this).isScalar();
120       })) {
121     CGM.ErrorUnsupported(E, "non-scalar arg to printf");
122     return RValue::get(llvm::ConstantInt::get(IntTy, 0));
123   }
124 
125   llvm::Value *BufferPtr = packArgsIntoNVPTXFormatBuffer(this, Args);
126 
127   // Invoke vprintf and return.
128   llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule());
129   return RValue::get(Builder.CreateCall(
130       VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr}));
131 }
132 
133 RValue
134 CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E,
135                                                 ReturnValueSlot ReturnValue) {
136   assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
137   assert(E->getBuiltinCallee() == Builtin::BIprintf ||
138          E->getBuiltinCallee() == Builtin::BI__builtin_printf);
139   assert(E->getNumArgs() >= 1); // printf always has at least one arg.
140 
141   CallArgList CallArgs;
142   EmitCallArgs(CallArgs,
143                E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
144                E->arguments(), E->getDirectCallee(),
145                /* ParamsToSkip = */ 0);
146 
147   SmallVector<llvm::Value *, 8> Args;
148   for (auto A : CallArgs) {
149     // We don't know how to emit non-scalar varargs.
150     if (!A.getRValue(*this).isScalar()) {
151       CGM.ErrorUnsupported(E, "non-scalar arg to printf");
152       return RValue::get(llvm::ConstantInt::get(IntTy, -1));
153     }
154 
155     llvm::Value *Arg = A.getRValue(*this).getScalarVal();
156     Args.push_back(Arg);
157   }
158 
159   llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
160   IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
161   auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args);
162   Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
163   return RValue::get(Printf);
164 }
165