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
21using namespace clang;
22using namespace CodeGen;
23
24namespace {
25llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
26 llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(C&: M.getContext()),
27 llvm::PointerType::getUnqual(C&: M.getContext())};
28 llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
29 Result: llvm::Type::getInt32Ty(C&: M.getContext()), Params: ArgTypes, isVarArg: false);
30
31 if (auto *F = M.getFunction(Name: "vprintf")) {
32 // Our CUDA system header declares vprintf with the right signature, so
33 // nobody else should have been able to declare vprintf with a bogus
34 // signature.
35 assert(F->getFunctionType() == VprintfFuncType);
36 return F;
37 }
38
39 // vprintf doesn't already exist; create a declaration and insert it into the
40 // module.
41 return llvm::Function::Create(
42 Ty: VprintfFuncType, Linkage: llvm::GlobalVariable::ExternalLinkage, N: "vprintf", M: &M);
43}
44
45llvm::Function *GetOpenMPVprintfDeclaration(CodeGenModule &CGM) {
46 const char *Name = "__llvm_omp_vprintf";
47 llvm::Module &M = CGM.getModule();
48 llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(C&: M.getContext()),
49 llvm::PointerType::getUnqual(C&: M.getContext()),
50 llvm::Type::getInt32Ty(C&: M.getContext())};
51 llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
52 Result: llvm::Type::getInt32Ty(C&: M.getContext()), Params: ArgTypes, isVarArg: false);
53
54 if (auto *F = M.getFunction(Name)) {
55 if (F->getFunctionType() != VprintfFuncType) {
56 CGM.Error(loc: SourceLocation(),
57 error: "Invalid type declaration for __llvm_omp_vprintf");
58 return nullptr;
59 }
60 return F;
61 }
62
63 return llvm::Function::Create(
64 Ty: VprintfFuncType, Linkage: llvm::GlobalVariable::ExternalLinkage, N: Name, M: &M);
65}
66
67// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
68// isn't particularly special; it's invoked just like a regular function).
69// vprintf takes two args: A format string, and a pointer to a buffer containing
70// the varargs.
71//
72// For example, the call
73//
74// printf("format string", arg1, arg2, arg3);
75//
76// is converted into something resembling
77//
78// struct Tmp {
79// Arg1 a1;
80// Arg2 a2;
81// Arg3 a3;
82// };
83// char* buf = alloca(sizeof(Tmp));
84// *(Tmp*)buf = {a1, a2, a3};
85// vprintf("format string", buf);
86//
87// buf is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of the
88// args is itself aligned to its preferred alignment.
89//
90// Note that by the time this function runs, E's args have already undergone the
91// standard C vararg promotion (short -> int, float -> double, etc.).
92
93std::pair<llvm::Value *, llvm::TypeSize>
94packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF, const CallArgList &Args) {
95 const llvm::DataLayout &DL = CGF->CGM.getDataLayout();
96 llvm::LLVMContext &Ctx = CGF->CGM.getLLVMContext();
97 CGBuilderTy &Builder = CGF->Builder;
98
99 // Construct and fill the args buffer that we'll pass to vprintf.
100 if (Args.size() <= 1) {
101 // If there are no args, pass a null pointer and size 0
102 llvm::Value *BufferPtr =
103 llvm::ConstantPointerNull::get(T: llvm::PointerType::getUnqual(C&: Ctx));
104 return {BufferPtr, llvm::TypeSize::getFixed(ExactSize: 0)};
105 } else {
106 llvm::SmallVector<llvm::Type *, 8> ArgTypes;
107 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
108 ArgTypes.push_back(Elt: Args[I].getRValue(CGF&: *CGF).getScalarVal()->getType());
109
110 // Using llvm::StructType is correct only because printf doesn't accept
111 // aggregates. If we had to handle aggregates here, we'd have to manually
112 // compute the offsets within the alloca -- we wouldn't be able to assume
113 // that the alignment of the llvm type was the same as the alignment of the
114 // clang type.
115 llvm::Type *AllocaTy = llvm::StructType::create(Elements: ArgTypes, Name: "printf_args");
116 llvm::Value *Alloca = CGF->CreateTempAlloca(Ty: AllocaTy);
117
118 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
119 llvm::Value *P = Builder.CreateStructGEP(Ty: AllocaTy, Ptr: Alloca, Idx: I - 1);
120 llvm::Value *Arg = Args[I].getRValue(CGF&: *CGF).getScalarVal();
121 Builder.CreateAlignedStore(Val: Arg, Ptr: P, Align: DL.getPrefTypeAlign(Ty: Arg->getType()));
122 }
123 llvm::Value *BufferPtr =
124 Builder.CreatePointerCast(V: Alloca, DestTy: llvm::PointerType::getUnqual(C&: Ctx));
125 return {BufferPtr, DL.getTypeAllocSize(Ty: AllocaTy)};
126 }
127}
128
129bool containsNonScalarVarargs(CodeGenFunction *CGF, const CallArgList &Args) {
130 return llvm::any_of(Range: llvm::drop_begin(RangeOrContainer: Args), P: [&](const CallArg &A) {
131 return !A.getRValue(CGF&: *CGF).isScalar();
132 });
133}
134
135RValue EmitDevicePrintfCallExpr(const CallExpr *E, CodeGenFunction *CGF,
136 llvm::Function *Decl, bool WithSizeArg) {
137 CodeGenModule &CGM = CGF->CGM;
138 CGBuilderTy &Builder = CGF->Builder;
139 assert(E->getBuiltinCallee() == Builtin::BIprintf ||
140 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
141 assert(E->getNumArgs() >= 1); // printf always has at least one arg.
142
143 // Uses the same format as nvptx for the argument packing, but also passes
144 // an i32 for the total size of the passed pointer
145 CallArgList Args;
146 CGF->EmitCallArgs(Args,
147 Prototype: E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
148 ArgRange: E->arguments(), AC: E->getDirectCallee(),
149 /* ParamsToSkip = */ 0);
150
151 // We don't know how to emit non-scalar varargs.
152 if (containsNonScalarVarargs(CGF, Args)) {
153 CGM.ErrorUnsupported(E, "non-scalar arg to printf");
154 return RValue::get(V: llvm::ConstantInt::get(Ty: CGF->IntTy, V: 0));
155 }
156
157 auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
158 llvm::Value *BufferPtr = r.first;
159
160 llvm::SmallVector<llvm::Value *, 3> Vec = {
161 Args[0].getRValue(CGF&: *CGF).getScalarVal(), BufferPtr};
162 if (WithSizeArg) {
163 // Passing > 32bit of data as a local alloca doesn't work for nvptx or
164 // amdgpu
165 llvm::Constant *Size =
166 llvm::ConstantInt::get(Ty: llvm::Type::getInt32Ty(C&: CGM.getLLVMContext()),
167 V: static_cast<uint32_t>(r.second.getFixedValue()));
168
169 Vec.push_back(Elt: Size);
170 }
171 return RValue::get(V: Builder.CreateCall(Callee: Decl, Args: Vec));
172}
173} // namespace
174
175RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E) {
176 assert(getTarget().getTriple().isNVPTX());
177 return EmitDevicePrintfCallExpr(
178 E, CGF: this, Decl: GetVprintfDeclaration(M&: CGM.getModule()), WithSizeArg: false);
179}
180
181RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
182 assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
183 assert(E->getBuiltinCallee() == Builtin::BIprintf ||
184 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
185 assert(E->getNumArgs() >= 1); // printf always has at least one arg.
186
187 CallArgList CallArgs;
188 EmitCallArgs(Args&: CallArgs,
189 Prototype: E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
190 ArgRange: E->arguments(), AC: E->getDirectCallee(),
191 /* ParamsToSkip = */ 0);
192
193 SmallVector<llvm::Value *, 8> Args;
194 for (const auto &A : CallArgs) {
195 // We don't know how to emit non-scalar varargs.
196 if (!A.getRValue(CGF&: *this).isScalar()) {
197 CGM.ErrorUnsupported(E, "non-scalar arg to printf");
198 return RValue::get(V: llvm::ConstantInt::get(Ty: IntTy, V: -1));
199 }
200
201 llvm::Value *Arg = A.getRValue(CGF&: *this).getScalarVal();
202 Args.push_back(Elt: Arg);
203 }
204
205 llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
206 IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
207
208 bool isBuffered = (CGM.getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
209 clang::TargetOptions::AMDGPUPrintfKind::Buffered);
210 auto Printf = llvm::emitAMDGPUPrintfCall(Builder&: IRB, Args, isBuffered);
211 Builder.SetInsertPoint(TheBB: IRB.GetInsertBlock(), IP: IRB.GetInsertPoint());
212 return RValue::get(V: Printf);
213}
214
215RValue CodeGenFunction::EmitOpenMPDevicePrintfCallExpr(const CallExpr *E) {
216 assert(getTarget().getTriple().isNVPTX() ||
217 getTarget().getTriple().isAMDGCN());
218 return EmitDevicePrintfCallExpr(E, CGF: this, Decl: GetOpenMPVprintfDeclaration(CGM),
219 WithSizeArg: true);
220}
221

source code of clang/lib/CodeGen/CGGPUBuiltin.cpp