blob: d7e2676307623dd845fd14fa6cbe4a3f1e760307 [file] [log] [blame]
Arpith Chacko Jacobcdda3daa2017-01-29 20:49:31 +00001//===------ CGGPUBuiltin.cpp - Codegen for GPU builtins -------------------===//
Justin Lebar3039a592016-01-23 21:28:14 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Justin Lebar3039a592016-01-23 21:28:14 +00006//
7//===----------------------------------------------------------------------===//
8//
Arpith Chacko Jacobcdda3daa2017-01-29 20:49:31 +00009// Generates code for built-in GPU calls which are not runtime-specific.
10// (Runtime-specific codegen lives in programming model specific files.)
Justin Lebar3039a592016-01-23 21:28:14 +000011//
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
20using namespace clang;
21using namespace CodeGen;
22
23static llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
24 llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
25 llvm::Type::getInt8PtrTy(M.getContext())};
26 llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
27 llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
28
29 if (auto* F = M.getFunction("vprintf")) {
30 // Our CUDA system header declares vprintf with the right signature, so
31 // nobody else should have been able to declare vprintf with a bogus
32 // signature.
33 assert(F->getFunctionType() == VprintfFuncType);
34 return F;
35 }
36
37 // vprintf doesn't already exist; create a declaration and insert it into the
38 // module.
39 return llvm::Function::Create(
40 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
41}
42
43// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
44// isn't particularly special; it's invoked just like a regular function).
45// vprintf takes two args: A format string, and a pointer to a buffer containing
46// the varargs.
47//
48// For example, the call
49//
50// printf("format string", arg1, arg2, arg3);
51//
52// is converted into something resembling
53//
Justin Lebarc0e42752016-01-28 23:58:28 +000054// struct Tmp {
55// Arg1 a1;
56// Arg2 a2;
57// Arg3 a3;
58// };
59// char* buf = alloca(sizeof(Tmp));
60// *(Tmp*)buf = {a1, a2, a3};
Justin Lebar3039a592016-01-23 21:28:14 +000061// vprintf("format string", buf);
62//
63// buf is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of the
64// args is itself aligned to its preferred alignment.
65//
66// Note that by the time this function runs, E's args have already undergone the
67// standard C vararg promotion (short -> int, float -> double, etc.).
68RValue
Arpith Chacko Jacobcdda3daa2017-01-29 20:49:31 +000069CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E,
70 ReturnValueSlot ReturnValue) {
71 assert(getTarget().getTriple().isNVPTX());
Justin Lebar3039a592016-01-23 21:28:14 +000072 assert(E->getBuiltinCallee() == Builtin::BIprintf);
73 assert(E->getNumArgs() >= 1); // printf always has at least one arg.
74
75 const llvm::DataLayout &DL = CGM.getDataLayout();
76 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
77
78 CallArgList Args;
79 EmitCallArgs(Args,
80 E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
81 E->arguments(), E->getDirectCallee(),
82 /* ParamsToSkip = */ 0);
83
Justin Lebar9a2c0fb2016-02-11 02:00:52 +000084 // We don't know how to emit non-scalar varargs.
Yaxun Liu5b330e82018-03-15 15:25:19 +000085 if (std::any_of(Args.begin() + 1, Args.end(), [&](const CallArg &A) {
86 return !A.getRValue(*this).isScalar();
87 })) {
Justin Lebar9a2c0fb2016-02-11 02:00:52 +000088 CGM.ErrorUnsupported(E, "non-scalar arg to printf");
89 return RValue::get(llvm::ConstantInt::get(IntTy, 0));
90 }
91
Justin Lebarc0e42752016-01-28 23:58:28 +000092 // Construct and fill the args buffer that we'll pass to vprintf.
93 llvm::Value *BufferPtr;
94 if (Args.size() <= 1) {
Justin Lebar3039a592016-01-23 21:28:14 +000095 // If there are no args, pass a null pointer to vprintf.
96 BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
97 } else {
Justin Lebarc0e42752016-01-28 23:58:28 +000098 llvm::SmallVector<llvm::Type *, 8> ArgTypes;
99 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
Yaxun Liu5b330e82018-03-15 15:25:19 +0000100 ArgTypes.push_back(Args[I].getRValue(*this).getScalarVal()->getType());
Justin Lebare56360a2016-07-27 22:36:21 +0000101
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
106 // clang type.
Justin Lebarc0e42752016-01-28 23:58:28 +0000107 llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
108 llvm::Value *Alloca = CreateTempAlloca(AllocaTy);
Justin Lebar3039a592016-01-23 21:28:14 +0000109
Justin Lebar3039a592016-01-23 21:28:14 +0000110 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
Justin Lebarc0e42752016-01-28 23:58:28 +0000111 llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
Yaxun Liu5b330e82018-03-15 15:25:19 +0000112 llvm::Value *Arg = Args[I].getRValue(*this).getScalarVal();
Justin Lebarc0e42752016-01-28 23:58:28 +0000113 Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlignment(Arg->getType()));
Justin Lebar3039a592016-01-23 21:28:14 +0000114 }
Justin Lebarc0e42752016-01-28 23:58:28 +0000115 BufferPtr = Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
Justin Lebar3039a592016-01-23 21:28:14 +0000116 }
117
118 // Invoke vprintf and return.
119 llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule());
Yaxun Liu5b330e82018-03-15 15:25:19 +0000120 return RValue::get(Builder.CreateCall(
121 VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr}));
Justin Lebar3039a592016-01-23 21:28:14 +0000122}