16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/Instruction.h"
18#include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h"
21using namespace CodeGen;
24llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
25 llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(M.getContext()),
26 llvm::PointerType::getUnqual(M.getContext())};
27 llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
28 llvm::Type::getInt32Ty(M.getContext()), ArgTypes,
false);
30 if (
auto *F = M.getFunction(
"vprintf")) {
34 assert(F->getFunctionType() == VprintfFuncType);
40 return llvm::Function::Create(
41 VprintfFuncType, llvm::GlobalVariable::ExternalLinkage,
"vprintf", &M);
70std::pair<llvm::Value *, llvm::TypeSize>
77 if (Args.size() <= 1) {
79 llvm::Value *BufferPtr =
80 llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Ctx));
81 return {BufferPtr, llvm::TypeSize::getFixed(0)};
84 for (
unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)
85 ArgTypes.push_back(Args[I].getRValue(*CGF).getScalarVal()->getType());
92 llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes,
"printf_args");
95 for (
unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) {
96 llvm::Value *
P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1);
97 llvm::Value *Arg = Args[I].getRValue(*CGF).getScalarVal();
98 Builder.CreateAlignedStore(Arg,
P, DL.getPrefTypeAlign(Arg->getType()));
100 llvm::Value *BufferPtr =
101 Builder.CreatePointerCast(Alloca, llvm::PointerType::getUnqual(Ctx));
102 return {BufferPtr, DL.getTypeAllocSize(AllocaTy)};
107 return llvm::any_of(llvm::drop_begin(Args), [&](
const CallArg &A) {
113 llvm::Function *
Decl,
bool WithSizeArg) {
116 assert(
E->getBuiltinCallee() == Builtin::BIprintf ||
117 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
118 assert(
E->getNumArgs() >= 1);
125 E->arguments(),
E->getDirectCallee(),
129 if (containsNonScalarVarargs(CGF, Args)) {
134 auto r = packArgsIntoNVPTXFormatBuffer(CGF, Args);
135 llvm::Value *BufferPtr = r.first;
138 Args[0].getRValue(*CGF).getScalarVal(), BufferPtr};
142 llvm::Constant *
Size =
143 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGM.
getLLVMContext()),
144 static_cast<uint32_t>(r.second.getFixedValue()));
152RValue CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(
const CallExpr *
E) {
153 assert(
getTarget().getTriple().isNVPTX());
154 return EmitDevicePrintfCallExpr(
159 assert(
getTarget().getTriple().isAMDGCN() ||
161 getTarget().getTriple().getVendor() == llvm::Triple::AMD));
162 assert(
E->getBuiltinCallee() == Builtin::BIprintf ||
163 E->getBuiltinCallee() == Builtin::BI__builtin_printf);
164 assert(
E->getNumArgs() >= 1);
169 E->arguments(),
E->getDirectCallee(),
173 for (
const auto &A : CallArgs) {
175 if (!A.getRValue(*this).isScalar()) {
180 llvm::Value *Arg = A.getRValue(*this).getScalarVal();
184 llvm::IRBuilder<> IRB(
Builder.GetInsertBlock(),
Builder.GetInsertPoint());
185 IRB.SetCurrentDebugLocation(
Builder.getCurrentDebugLocation());
189 auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
190 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