clang 20.0.0git
CGGPUBuiltin.cpp
Go to the documentation of this file.
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"
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(M.getContext()),
27 llvm::PointerType::getUnqual(M.getContext())};
28 llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
29 llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
30
31 if (auto *F = M.getFunction("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 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
43}
44
45// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
46// isn't particularly special; it's invoked just like a regular function).
47// vprintf takes two args: A format string, and a pointer to a buffer containing
48// the varargs.
49//
50// For example, the call
51//
52// printf("format string", arg1, arg2, arg3);
53//
54// is converted into something resembling
55//
56// struct Tmp {
57// Arg1 a1;
58// Arg2 a2;
59// Arg3 a3;
60// };
61// char* buf = alloca(sizeof(Tmp));
62// *(Tmp*)buf = {a1, a2, a3};
63// vprintf("format string", buf);
64//
65// buf is aligned to the max of {alignof(Arg1), ...}. Furthermore, each of the
66// args is itself aligned to its preferred alignment.
67//
68// Note that by the time this function runs, E's args have already undergone the
69// standard C vararg promotion (short -> int, float -> double, etc.).
70
71std::pair<llvm::Value *, llvm::TypeSize>
72packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF, 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 and size 0
80 llvm::Value *BufferPtr =
81 llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Ctx));
82 return {BufferPtr, llvm::TypeSize::getFixed(0)};
83 } else {
85 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
86 ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
87
88 // Using llvm::StructType is correct only because printf doesn't accept
89 // aggregates. If we had to handle aggregates here, we'd have to manually
90 // compute the offsets within the alloca -- we wouldn't be able to assume
91 // that the alignment of the llvm type was the same as the alignment of the
92 // clang type.
93 llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
94 llvm::Value *Alloca = CGF->CreateTempAlloca(AllocaTy);
95
96 for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
97 llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
98 llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
99 Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlign(Arg->getType()));
100 }
101 llvm::Value *BufferPtr =
102 Builder.CreatePointerCast(Alloca, llvm::PointerType::getUnqual(Ctx));
103 return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
104 }
105}
106
107bool containsNonScalarVarargs(CodeGenFunction *CGF, const CallArgList &Args) {
108 return llvm::any_of(llvm::drop_begin(Args), [&](const CallArg &A) {
109 return !A.getRValue(*CGF).isScalar();
110 });
111}
112
113RValue EmitDevicePrintfCallExpr(const CallExpr *E, CodeGenFunction *CGF,
114 llvm::Function *Decl, bool WithSizeArg) {
115 CodeGenModule &CGM = CGF->CGM;
116 CGBuilderTy &Builder = CGF->Builder;
117 assert(E->getBuiltinCallee() == Builtin::BIprintf ||
118 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
119 assert(E->getNumArgs() >= 1); // printf always has at least one arg.
120
121 // Uses the same format as nvptx for the argument packing, but also passes
122 // an i32 for the total size of the passed pointer
123 CallArgList Args;
124 CGF->EmitCallArgs(Args,
125 E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
126 E->arguments(), E->getDirectCallee(),
127 /* ParamsToSkip = */ 0);
128
129 // We don't know how to emit non-scalar varargs.
130 if (containsNonScalarVarargs(CGF, Args)) {
131 CGM.ErrorUnsupported(E, "non-scalar arg to printf");
132 return RValue::get(llvm::ConstantInt::get(CGF->IntTy, 0));
133 }
134
135 auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
136 llvm::Value *BufferPtr = r.first;
137
139 Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
140 if (WithSizeArg) {
141 // Passing > 32bit of data as a local alloca doesn't work for nvptx or
142 // amdgpu
143 llvm::Constant *Size =
144 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.getLLVMContext()),
145 static_cast<uint32_t>(r.second.getFixedValue()));
146
147 Vec.push_back(Size);
148 }
149 return RValue::get(Builder.CreateCall(Decl, Vec));
150}
151} // namespace
152
153RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E) {
154 assert(getTarget().getTriple().isNVPTX());
155 return EmitDevicePrintfCallExpr(
156 E, this, GetVprintfDeclaration(CGM.getModule()), false);
157}
158
160 assert(getTarget().getTriple().isAMDGCN() ||
161 (getTarget().getTriple().isSPIRV() &&
162 getTarget().getTriple().getVendor() == llvm::Triple::AMD));
163 assert(E->getBuiltinCallee() == Builtin::BIprintf ||
164 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
165 assert(E->getNumArgs() >= 1); // printf always has at least one arg.
166
167 CallArgList CallArgs;
168 EmitCallArgs(CallArgs,
169 E->getDirectCallee()->getType()->getAs<FunctionProtoType>(),
170 E->arguments(), E->getDirectCallee(),
171 /* ParamsToSkip = */ 0);
172
174 for (const auto &A : CallArgs) {
175 // We don't know how to emit non-scalar varargs.
176 if (!A.getRValue(*this).isScalar()) {
177 CGM.ErrorUnsupported(E, "non-scalar arg to printf");
178 return RValue::get(llvm::ConstantInt::get(IntTy, -1));
179 }
180
181 llvm::Value *Arg = A.getRValue(*this).getScalarVal();
182 Args.push_back(Arg);
183 }
184
185 llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
186 IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
187
188 bool isBuffered = (CGM.getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
190 auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
191 Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
192 return RValue::get(Printf);
193}
StringRef P
Defines enum values for all the target-independent builtin functions.
Expr * E
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:274
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
const TargetInfo & getTarget() const
RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E)
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
const TargetInfo & getTarget() const
const llvm::DataLayout & getDataLayout() const
llvm::LLVMContext & getLLVMContext()
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:42
bool isScalar() const
Definition: CGValue.h:64
static RValue get(llvm::Value *V)
Definition: CGValue.h:98
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
QualType getType() const
Definition: Expr.h:142
Represents a prototype with parameter type info, e.g.
Definition: Type.h:5002
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition: TargetInfo.h:312
@ Buffered
printf lowering scheme involving implicit printf buffers,
AMDGPUPrintfKind AMDGPUPrintfKindVal
AMDGPU Printf lowering scheme.
Definition: TargetOptions.h:96
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
The JSON file list parser is used to communicate input to InstallAPI.
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4636
llvm::IntegerType * IntTy
int