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"
22using namespace CodeGen;
25llvm::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);
31 if (
auto *F = M.getFunction(
"vprintf")) {
35 assert(F->getFunctionType() == VprintfFuncType);
41 return llvm::Function::Create(
42 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage,
"vprintf", &M);
45llvm::Function *GetOpenMPVprintfDeclaration(
CodeGenModule &CGM) {
46 const char *Name =
"__llvm_omp_vprintf";
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);
54 if (
auto *F = M.getFunction(Name)) {
55 if (F->getFunctionType() != VprintfFuncType) {
57 "Invalid type declaration for __llvm_omp_vprintf");
63 return llvm::Function::Create(
64 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, Name, &M);
93std::pair<llvm::Value *, llvm::TypeSize>
100 if (Args.size() <= 1) {
102 llvm::Value * BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx));
103 return {BufferPtr, llvm::TypeSize::Fixed(0)};
106 for (
unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
107 ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
114 llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes,
"printf_args");
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()));
122 llvm::Value *BufferPtr =
123 Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx));
124 return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
129 return llvm::any_of(llvm::drop_begin(Args), [&](
const CallArg &A) {
135 llvm::Function *
Decl,
bool WithSizeArg) {
150 if (containsNonScalarVarargs(CGF, Args)) {
155 auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
156 llvm::Value *BufferPtr = r.first;
159 Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
163 llvm::Constant *
Size =
164 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.
getLLVMContext()),
165 static_cast<uint32_t
>(r.second.getFixedValue()));
173RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(
const CallExpr *E) {
174 assert(
getTarget().getTriple().isNVPTX());
175 return EmitDevicePrintfCallExpr(
176 E,
this, GetVprintfDeclaration(
CGM.
getModule()),
false);
180 assert(
getTarget().getTriple().getArch() == llvm::Triple::amdgcn);
192 for (
const auto &A : CallArgs) {
194 if (!A.getRValue(*this).isScalar()) {
199 llvm::Value *Arg = A.getRValue(*this).getScalarVal();
203 llvm::IRBuilder<> IRB(
Builder.GetInsertBlock(),
Builder.GetInsertPoint());
204 IRB.SetCurrentDebugLocation(
Builder.getCurrentDebugLocation());
208 auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
209 Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
214 assert(
getTarget().getTriple().isNVPTX() ||
216 return EmitDevicePrintfCallExpr(E,
this, GetOpenMPVprintfDeclaration(
CGM),
Defines enum values for all the target-independent builtin functions.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
CallArgList - Type for representing both the value and type of arguments in a call.
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)
RValue EmitOpenMPDevicePrintfCallExpr(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
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
llvm::LLVMContext & getLLVMContext()
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
static RValue get(llvm::Value *V)
Decl - This represents one declaration (or definition), e.g.
Represents a prototype with parameter type info, e.g.
Encodes a location in the source.
TargetOptions & getTargetOpts() const
Retrieve the target options.
@ Buffered
printf lowering scheme involving implicit printf buffers,
AMDGPUPrintfKind AMDGPUPrintfKindVal
AMDGPU Printf lowering scheme.
const T * getAs() const
Member-template getAs<specific type>'.
RValue getRValue(CodeGenFunction &CGF) const
llvm::IntegerType * IntTy
int