clang  14.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"
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 namespace {
25 llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
26  llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
27  llvm::Type::getInt8PtrTy(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 llvm::Function *GetOpenMPVprintfDeclaration(CodeGenModule &CGM) {
46  const char *Name = "__llvm_omp_vprintf";
47  llvm::Module &M = CGM.getModule();
48  llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()),
49  llvm::Type::getInt8PtrTy(M.getContext()),
50  llvm::Type::getInt32Ty(M.getContext())};
51  llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
52  llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
53 
54  if (auto *F = M.getFunction(Name)) {
55  if (F->getFunctionType() != VprintfFuncType) {
56  CGM.Error(SourceLocation(),
57  "Invalid type declaration for __llvm_omp_vprintf");
58  return nullptr;
59  }
60  return F;
61  }
62 
63  return llvm::Function::Create(
64  VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, Name, &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 
93 std::pair<llvm::Value *, llvm::TypeSize>
94 packArgsIntoNVPTXFormatBuffer(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 = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
103  return {BufferPtr, llvm::TypeSize::Fixed(0)};
104  } else {
106  for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
107  ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
108 
109  // Using llvm::StructType is correct only because printf doesn't accept
110  // aggregates. If we had to handle aggregates here, we'd have to manually
111  // compute the offsets within the alloca -- we wouldn't be able to assume
112  // that the alignment of the llvm type was the same as the alignment of the
113  // clang type.
114  llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args");
115  llvm::Value *Alloca = CGF->CreateTempAlloca(AllocaTy);
116 
117  for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
118  llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
119  llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
120  Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlign(Arg->getType()));
121  }
122  llvm::Value *BufferPtr =
123  Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
124  return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
125  }
126 }
127 
128 bool containsNonScalarVarargs(CodeGenFunction *CGF, CallArgList Args) {
129  return llvm::any_of(llvm::drop_begin(Args), [&](const CallArg &A) {
130  return !A.getRValue(*CGF).isScalar();
131  });
132 }
133 
134 RValue EmitDevicePrintfCallExpr(const CallExpr *E, CodeGenFunction *CGF,
135  llvm::Function *Decl, bool WithSizeArg) {
136  CodeGenModule &CGM = CGF->CGM;
137  CGBuilderTy &Builder = CGF->Builder;
138  assert(E->getBuiltinCallee() == Builtin::BIprintf);
139  assert(E->getNumArgs() >= 1); // printf always has at least one arg.
140 
141  // Uses the same format as nvptx for the argument packing, but also passes
142  // an i32 for the total size of the passed pointer
143  CallArgList Args;
144  CGF->EmitCallArgs(Args,
146  E->arguments(), E->getDirectCallee(),
147  /* ParamsToSkip = */ 0);
148 
149  // We don't know how to emit non-scalar varargs.
150  if (containsNonScalarVarargs(CGF, Args)) {
151  CGM.ErrorUnsupported(E, "non-scalar arg to printf");
152  return RValue::get(llvm::ConstantInt::get(CGF->IntTy, 0));
153  }
154 
155  auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
156  llvm::Value *BufferPtr = r.first;
157 
159  Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
160  if (WithSizeArg) {
161  // Passing > 32bit of data as a local alloca doesn't work for nvptx or
162  // amdgpu
163  llvm::Constant *Size =
164  llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.getLLVMContext()),
165  static_cast<uint32_t>(r.second.getFixedSize()));
166 
167  Vec.push_back(Size);
168  }
169  return RValue::get(Builder.CreateCall(Decl, Vec));
170 }
171 } // namespace
172 
174  assert(getTarget().getTriple().isNVPTX());
175  return EmitDevicePrintfCallExpr(
176  E, this, GetVprintfDeclaration(CGM.getModule()), false);
177 }
178 
180  assert(getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
181  assert(E->getBuiltinCallee() == Builtin::BIprintf ||
182  E->getBuiltinCallee() == Builtin::BI__builtin_printf);
183  assert(E->getNumArgs() >= 1); // printf always has at least one arg.
184 
185  CallArgList CallArgs;
186  EmitCallArgs(CallArgs,
188  E->arguments(), E->getDirectCallee(),
189  /* ParamsToSkip = */ 0);
190 
192  for (auto A : CallArgs) {
193  // We don't know how to emit non-scalar varargs.
194  if (!A.getRValue(*this).isScalar()) {
195  CGM.ErrorUnsupported(E, "non-scalar arg to printf");
196  return RValue::get(llvm::ConstantInt::get(IntTy, -1));
197  }
198 
199  llvm::Value *Arg = A.getRValue(*this).getScalarVal();
200  Args.push_back(Arg);
201  }
202 
203  llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
204  IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
205  auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args);
206  Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
207  return RValue::get(Printf);
208 }
209 
211  assert(getTarget().getTriple().isNVPTX() ||
212  getTarget().getTriple().isAMDGCN());
213  return EmitDevicePrintfCallExpr(E, this, GetOpenMPVprintfDeclaration(CGM),
214  true);
215 }
Builtins.h
clang::CodeGen::CodeGenTypeCache::IntTy
llvm::IntegerType * IntTy
int
Definition: CodeGenTypeCache.h:42
clang::CodeGen::RValue
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
CodeGenFunction.h
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::CodeGen::CodeGenFunction::CreateTempAlloca
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...
Definition: CGExpr.cpp:110
clang::CodeGen::CGBuilderTy
Definition: CGBuilder.h:43
clang::CodeGen::CodeGenFunction::Builder
CGBuilderTy Builder
Definition: CodeGenFunction.h:274
clang::CodeGen::CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr
RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E)
Definition: CGGPUBuiltin.cpp:179
clang::CallExpr::getDirectCallee
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2965
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7165
clang::CodeGen::CodeGenFunction::getTarget
const TargetInfo & getTarget() const
Definition: CodeGenFunction.h:2017
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:709
clang::CodeGen::CodeGenFunction::EmitNVPTXDevicePrintfCallExpr
RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E)
Definition: CGGPUBuiltin.cpp:173
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:711
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::serialized_diags::create
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Definition: SerializedDiagnosticPrinter.cpp:302
clang::CodeGen::CallArg::getRValue
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4258
clang::syntax::NodeRole::Size
@ Size
clang::CodeGen::CodeGenModule::ErrorUnsupported
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
Definition: CodeGenModule.cpp:1005
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:235
clang::FunctionProtoType
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3885
clang::CallExpr::getNumArgs
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition: Expr.h:2973
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:284
Value
Value
Definition: UninitializedValues.cpp:102
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::CodeGen::CodeGenFunction::EmitOpenMPDevicePrintfCallExpr
RValue EmitOpenMPDevicePrintfCallExpr(const CallExpr *E)
Definition: CGGPUBuiltin.cpp:210
clang::CodeGen::CodeGenFunction::CGM
CodeGenModule & CGM
Definition: CodeGenFunction.h:266
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::RValue::get
static RValue get(llvm::Value *V)
Definition: CGValue.h:86
clang::CodeGen::CodeGenFunction::EmitCallArgs
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition: CGCall.cpp:4078
clang::ExternalLinkage
@ ExternalLinkage
External linkage, which indicates that the entity can be referred to from other translation units.
Definition: Linkage.h:59
clang::CodeGen::RValue::isScalar
bool isScalar() const
Definition: CGValue.h:52
clang::CallExpr::getBuiltinCallee
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition: Expr.cpp:1500
clang::CallExpr::arguments
arg_range arguments()
Definition: Expr.h:3034
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:687
clang::CodeGen::CallArg
Definition: CGCall.h:219
clang::CodeGen::CallArgList
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:264
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2795
clang::CodeGen::CodeGenModule::Error
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
Definition: CodeGenModule.cpp:998
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:720