| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 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 | |
| 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
| 23 | static llvm::Function *GetVprintfDeclaration(llvm::Module &M) { |
| 24 | llvm::Type *ArgTypes[] = {llvm::Type::getInt8PtrTy(M.getContext()), |
| 25 | llvm::Type::getInt8PtrTy(M.getContext())}; |
| 26 | llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get( |
| 27 | llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false); |
| 28 | |
| 29 | if (auto* F = M.getFunction("vprintf")) { |
| 30 | |
| 31 | |
| 32 | |
| 33 | getFunctionType() == VprintfFuncType", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGGPUBuiltin.cpp", 33, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(F->getFunctionType() == VprintfFuncType); |
| 34 | return F; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | return llvm::Function::Create( |
| 40 | VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | RValue |
| 69 | CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E, |
| 70 | ReturnValueSlot ReturnValue) { |
| 71 | assert(getTarget().getTriple().isNVPTX()); |
| 72 | getBuiltinCallee() == Builtin..BIprintf", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGGPUBuiltin.cpp", 72, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getBuiltinCallee() == Builtin::BIprintf); |
| 73 | getNumArgs() >= 1", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGGPUBuiltin.cpp", 73, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getNumArgs() >= 1); |
| 74 | |
| 75 | const llvm::DataLayout &DL = CGM.getDataLayout(); |
| 76 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
| 77 | |
| 78 | CallArgList Args; |
| 79 | EmitCallArgs(Args, |
| 80 | E->getDirectCallee()->getType()->getAs<FunctionProtoType>(), |
| 81 | E->arguments(), E->getDirectCallee(), |
| 82 | 0); |
| 83 | |
| 84 | |
| 85 | if (std::any_of(Args.begin() + 1, Args.end(), [&](const CallArg &A) { |
| 86 | return !A.getRValue(*this).isScalar(); |
| 87 | })) { |
| 88 | CGM.ErrorUnsupported(E, "non-scalar arg to printf"); |
| 89 | return RValue::get(llvm::ConstantInt::get(IntTy, 0)); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | llvm::Value *BufferPtr; |
| 94 | if (Args.size() <= 1) { |
| 95 | |
| 96 | BufferPtr = llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(Ctx)); |
| 97 | } else { |
| 98 | llvm::SmallVector<llvm::Type *, 8> ArgTypes; |
| 99 | for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) |
| 100 | ArgTypes.push_back(Args[I].getRValue(*this).getScalarVal()->getType()); |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | llvm::Type *AllocaTy = llvm::StructType::create(ArgTypes, "printf_args"); |
| 108 | llvm::Value *Alloca = CreateTempAlloca(AllocaTy); |
| 109 | |
| 110 | for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I) { |
| 111 | llvm::Value *P = Builder.CreateStructGEP(AllocaTy, Alloca, I - 1); |
| 112 | llvm::Value *Arg = Args[I].getRValue(*this).getScalarVal(); |
| 113 | Builder.CreateAlignedStore(Arg, P, DL.getPrefTypeAlignment(Arg->getType())); |
| 114 | } |
| 115 | BufferPtr = Builder.CreatePointerCast(Alloca, llvm::Type::getInt8PtrTy(Ctx)); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule()); |
| 120 | return RValue::get(Builder.CreateCall( |
| 121 | VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr})); |
| 122 | } |
| 123 | |