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::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);
31 if (
auto *F = M.getFunction(
"vprintf")) {
35 assert(F->getFunctionType() == VprintfFuncType);
41 return llvm::Function::Create(
42 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage,
"vprintf", &M);
71std::pair<llvm::Value *, llvm::TypeSize>
78 if (Args.size() <= 1) {
80 llvm::Value *BufferPtr =
81 llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Ctx));
82 return {BufferPtr, llvm::TypeSize::getFixed(0)};
85 for (
unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
86 ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
93 llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes,
"printf_args");
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()));
101 llvm::Value *BufferPtr =
102 Builder.CreatePointerCast(Alloca, llvm::PointerType::getUnqual(Ctx));
103 return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
108 return llvm::any_of(llvm::drop_begin(Args), [&](
const CallArg &A) {
114 llvm::Function *
Decl,
bool WithSizeArg) {
117 assert(
E->getBuiltinCallee() == Builtin::BIprintf ||
118 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
119 assert(
E->getNumArgs() >= 1);
126 E->arguments(),
E->getDirectCallee(),
130 if (containsNonScalarVarargs(CGF, Args)) {
135 auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
136 llvm::Value *BufferPtr = r.first;
139 Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
143 llvm::Constant *
Size =
144 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.
getLLVMContext()),
145 static_cast<uint32_t
>(r.second.getFixedValue()));
153RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(
const CallExpr *
E) {
154 assert(
getTarget().getTriple().isNVPTX());
155 return EmitDevicePrintfCallExpr(
160 assert(
getTarget().getTriple().isAMDGCN() ||
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);
170 E->arguments(),
E->getDirectCallee(),
174 for (
const auto &A : CallArgs) {
176 if (!A.getRValue(*this).isScalar()) {
181 llvm::Value *Arg = A.getRValue(*this).getScalarVal();
185 llvm::IRBuilder<> IRB(
Builder.GetInsertBlock(),
Builder.GetInsertPoint());
186 IRB.SetCurrentDebugLocation(
Builder.getCurrentDebugLocation());
190 auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
191 Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
Defines enum values for all the target-independent builtin functions.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.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)
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.
static RValue get(llvm::Value *V)
Decl - This represents one declaration (or definition), e.g.
Represents a prototype with parameter type info, e.g.
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>'.
The JSON file list parser is used to communicate input to InstallAPI.
RValue getRValue(CodeGenFunction &CGF) const
llvm::IntegerType * IntTy
int