18bcb0991SDimitry Andric //=== AMDGPUPrintfRuntimeBinding.cpp - OpenCL printf implementation -------===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric // \file 98bcb0991SDimitry Andric // 108bcb0991SDimitry Andric // The pass bind printfs to a kernel arg pointer that will be bound to a buffer 118bcb0991SDimitry Andric // later by the runtime. 128bcb0991SDimitry Andric // 138bcb0991SDimitry Andric // This pass traverses the functions in the module and converts 148bcb0991SDimitry Andric // each call to printf to a sequence of operations that 158bcb0991SDimitry Andric // store the following into the printf buffer: 168bcb0991SDimitry Andric // - format string (passed as a module's metadata unique ID) 178bcb0991SDimitry Andric // - bitwise copies of printf arguments 188bcb0991SDimitry Andric // The backend passes will need to store metadata in the kernel 198bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 208bcb0991SDimitry Andric 218bcb0991SDimitry Andric #include "AMDGPU.h" 228bcb0991SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h" 238bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 248bcb0991SDimitry Andric #include "llvm/IR/Dominators.h" 258bcb0991SDimitry Andric #include "llvm/IR/IRBuilder.h" 268bcb0991SDimitry Andric #include "llvm/IR/Instructions.h" 27480093f4SDimitry Andric #include "llvm/InitializePasses.h" 288bcb0991SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 29e8d8bef9SDimitry Andric 308bcb0991SDimitry Andric using namespace llvm; 318bcb0991SDimitry Andric 328bcb0991SDimitry Andric #define DEBUG_TYPE "printfToRuntime" 338bcb0991SDimitry Andric #define DWORD_ALIGN 4 348bcb0991SDimitry Andric 358bcb0991SDimitry Andric namespace { 36e8d8bef9SDimitry Andric class AMDGPUPrintfRuntimeBinding final : public ModulePass { 378bcb0991SDimitry Andric 388bcb0991SDimitry Andric public: 398bcb0991SDimitry Andric static char ID; 408bcb0991SDimitry Andric 418bcb0991SDimitry Andric explicit AMDGPUPrintfRuntimeBinding(); 428bcb0991SDimitry Andric 438bcb0991SDimitry Andric private: 448bcb0991SDimitry Andric bool runOnModule(Module &M) override; 458bcb0991SDimitry Andric 468bcb0991SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 478bcb0991SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 488bcb0991SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 498bcb0991SDimitry Andric } 50e8d8bef9SDimitry Andric }; 518bcb0991SDimitry Andric 52e8d8bef9SDimitry Andric class AMDGPUPrintfRuntimeBindingImpl { 53e8d8bef9SDimitry Andric public: 54e8d8bef9SDimitry Andric AMDGPUPrintfRuntimeBindingImpl( 55e8d8bef9SDimitry Andric function_ref<const DominatorTree &(Function &)> GetDT, 56e8d8bef9SDimitry Andric function_ref<const TargetLibraryInfo &(Function &)> GetTLI) 57e8d8bef9SDimitry Andric : GetDT(GetDT), GetTLI(GetTLI) {} 58e8d8bef9SDimitry Andric bool run(Module &M); 59e8d8bef9SDimitry Andric 60e8d8bef9SDimitry Andric private: 61e8d8bef9SDimitry Andric void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers, 62e8d8bef9SDimitry Andric StringRef fmt, size_t num_ops) const; 63e8d8bef9SDimitry Andric 64e8d8bef9SDimitry Andric bool shouldPrintAsStr(char Specifier, Type *OpType) const; 65e8d8bef9SDimitry Andric bool lowerPrintfForGpu(Module &M); 66e8d8bef9SDimitry Andric 67e8d8bef9SDimitry Andric Value *simplify(Instruction *I, const TargetLibraryInfo *TLI, 68e8d8bef9SDimitry Andric const DominatorTree *DT) { 698bcb0991SDimitry Andric return SimplifyInstruction(I, {*TD, TLI, DT}); 708bcb0991SDimitry Andric } 718bcb0991SDimitry Andric 728bcb0991SDimitry Andric const DataLayout *TD; 73e8d8bef9SDimitry Andric function_ref<const DominatorTree &(Function &)> GetDT; 74e8d8bef9SDimitry Andric function_ref<const TargetLibraryInfo &(Function &)> GetTLI; 758bcb0991SDimitry Andric SmallVector<CallInst *, 32> Printfs; 768bcb0991SDimitry Andric }; 778bcb0991SDimitry Andric } // namespace 788bcb0991SDimitry Andric 798bcb0991SDimitry Andric char AMDGPUPrintfRuntimeBinding::ID = 0; 808bcb0991SDimitry Andric 818bcb0991SDimitry Andric INITIALIZE_PASS_BEGIN(AMDGPUPrintfRuntimeBinding, 828bcb0991SDimitry Andric "amdgpu-printf-runtime-binding", "AMDGPU Printf lowering", 838bcb0991SDimitry Andric false, false) 848bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 858bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 868bcb0991SDimitry Andric INITIALIZE_PASS_END(AMDGPUPrintfRuntimeBinding, "amdgpu-printf-runtime-binding", 878bcb0991SDimitry Andric "AMDGPU Printf lowering", false, false) 888bcb0991SDimitry Andric 898bcb0991SDimitry Andric char &llvm::AMDGPUPrintfRuntimeBindingID = AMDGPUPrintfRuntimeBinding::ID; 908bcb0991SDimitry Andric 918bcb0991SDimitry Andric namespace llvm { 928bcb0991SDimitry Andric ModulePass *createAMDGPUPrintfRuntimeBinding() { 938bcb0991SDimitry Andric return new AMDGPUPrintfRuntimeBinding(); 948bcb0991SDimitry Andric } 958bcb0991SDimitry Andric } // namespace llvm 968bcb0991SDimitry Andric 97e8d8bef9SDimitry Andric AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() : ModulePass(ID) { 988bcb0991SDimitry Andric initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry()); 998bcb0991SDimitry Andric } 1008bcb0991SDimitry Andric 101e8d8bef9SDimitry Andric void AMDGPUPrintfRuntimeBindingImpl::getConversionSpecifiers( 1028bcb0991SDimitry Andric SmallVectorImpl<char> &OpConvSpecifiers, StringRef Fmt, 1038bcb0991SDimitry Andric size_t NumOps) const { 1048bcb0991SDimitry Andric // not all format characters are collected. 1058bcb0991SDimitry Andric // At this time the format characters of interest 1068bcb0991SDimitry Andric // are %p and %s, which use to know if we 1078bcb0991SDimitry Andric // are either storing a literal string or a 1088bcb0991SDimitry Andric // pointer to the printf buffer. 1098bcb0991SDimitry Andric static const char ConvSpecifiers[] = "cdieEfgGaosuxXp"; 1108bcb0991SDimitry Andric size_t CurFmtSpecifierIdx = 0; 1118bcb0991SDimitry Andric size_t PrevFmtSpecifierIdx = 0; 1128bcb0991SDimitry Andric 1138bcb0991SDimitry Andric while ((CurFmtSpecifierIdx = Fmt.find_first_of( 1148bcb0991SDimitry Andric ConvSpecifiers, CurFmtSpecifierIdx)) != StringRef::npos) { 1158bcb0991SDimitry Andric bool ArgDump = false; 1168bcb0991SDimitry Andric StringRef CurFmt = Fmt.substr(PrevFmtSpecifierIdx, 1178bcb0991SDimitry Andric CurFmtSpecifierIdx - PrevFmtSpecifierIdx); 1188bcb0991SDimitry Andric size_t pTag = CurFmt.find_last_of("%"); 1198bcb0991SDimitry Andric if (pTag != StringRef::npos) { 1208bcb0991SDimitry Andric ArgDump = true; 1218bcb0991SDimitry Andric while (pTag && CurFmt[--pTag] == '%') { 1228bcb0991SDimitry Andric ArgDump = !ArgDump; 1238bcb0991SDimitry Andric } 1248bcb0991SDimitry Andric } 1258bcb0991SDimitry Andric 1268bcb0991SDimitry Andric if (ArgDump) 1278bcb0991SDimitry Andric OpConvSpecifiers.push_back(Fmt[CurFmtSpecifierIdx]); 1288bcb0991SDimitry Andric 1298bcb0991SDimitry Andric PrevFmtSpecifierIdx = ++CurFmtSpecifierIdx; 1308bcb0991SDimitry Andric } 1318bcb0991SDimitry Andric } 1328bcb0991SDimitry Andric 133e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBindingImpl::shouldPrintAsStr(char Specifier, 1348bcb0991SDimitry Andric Type *OpType) const { 1358bcb0991SDimitry Andric if (Specifier != 's') 1368bcb0991SDimitry Andric return false; 1378bcb0991SDimitry Andric const PointerType *PT = dyn_cast<PointerType>(OpType); 1388bcb0991SDimitry Andric if (!PT || PT->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) 1398bcb0991SDimitry Andric return false; 1408bcb0991SDimitry Andric Type *ElemType = PT->getContainedType(0); 1418bcb0991SDimitry Andric if (ElemType->getTypeID() != Type::IntegerTyID) 1428bcb0991SDimitry Andric return false; 1438bcb0991SDimitry Andric IntegerType *ElemIType = cast<IntegerType>(ElemType); 1448bcb0991SDimitry Andric return ElemIType->getBitWidth() == 8; 1458bcb0991SDimitry Andric } 1468bcb0991SDimitry Andric 147e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) { 1488bcb0991SDimitry Andric LLVMContext &Ctx = M.getContext(); 1498bcb0991SDimitry Andric IRBuilder<> Builder(Ctx); 1508bcb0991SDimitry Andric Type *I32Ty = Type::getInt32Ty(Ctx); 1518bcb0991SDimitry Andric unsigned UniqID = 0; 152*349cc55cSDimitry Andric // NB: This is important for this string size to be divisible by 4 1538bcb0991SDimitry Andric const char NonLiteralStr[4] = "???"; 1548bcb0991SDimitry Andric 1558bcb0991SDimitry Andric for (auto CI : Printfs) { 156*349cc55cSDimitry Andric unsigned NumOps = CI->arg_size(); 1578bcb0991SDimitry Andric 1588bcb0991SDimitry Andric SmallString<16> OpConvSpecifiers; 1598bcb0991SDimitry Andric Value *Op = CI->getArgOperand(0); 1608bcb0991SDimitry Andric 1618bcb0991SDimitry Andric if (auto LI = dyn_cast<LoadInst>(Op)) { 1628bcb0991SDimitry Andric Op = LI->getPointerOperand(); 1638bcb0991SDimitry Andric for (auto Use : Op->users()) { 1648bcb0991SDimitry Andric if (auto SI = dyn_cast<StoreInst>(Use)) { 1658bcb0991SDimitry Andric Op = SI->getValueOperand(); 1668bcb0991SDimitry Andric break; 1678bcb0991SDimitry Andric } 1688bcb0991SDimitry Andric } 1698bcb0991SDimitry Andric } 1708bcb0991SDimitry Andric 1718bcb0991SDimitry Andric if (auto I = dyn_cast<Instruction>(Op)) { 172e8d8bef9SDimitry Andric Value *Op_simplified = 173e8d8bef9SDimitry Andric simplify(I, &GetTLI(*I->getFunction()), &GetDT(*I->getFunction())); 1748bcb0991SDimitry Andric if (Op_simplified) 1758bcb0991SDimitry Andric Op = Op_simplified; 1768bcb0991SDimitry Andric } 1778bcb0991SDimitry Andric 1788bcb0991SDimitry Andric ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Op); 1798bcb0991SDimitry Andric 1808bcb0991SDimitry Andric if (ConstExpr) { 1818bcb0991SDimitry Andric GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 1828bcb0991SDimitry Andric 1838bcb0991SDimitry Andric StringRef Str("unknown"); 1848bcb0991SDimitry Andric if (GVar && GVar->hasInitializer()) { 185e8d8bef9SDimitry Andric auto *Init = GVar->getInitializer(); 186e8d8bef9SDimitry Andric if (auto *CA = dyn_cast<ConstantDataArray>(Init)) { 1878bcb0991SDimitry Andric if (CA->isString()) 1888bcb0991SDimitry Andric Str = CA->getAsCString(); 1898bcb0991SDimitry Andric } else if (isa<ConstantAggregateZero>(Init)) { 1908bcb0991SDimitry Andric Str = ""; 1918bcb0991SDimitry Andric } 1928bcb0991SDimitry Andric // 1938bcb0991SDimitry Andric // we need this call to ascertain 1948bcb0991SDimitry Andric // that we are printing a string 1958bcb0991SDimitry Andric // or a pointer. It takes out the 1968bcb0991SDimitry Andric // specifiers and fills up the first 1978bcb0991SDimitry Andric // arg 1988bcb0991SDimitry Andric getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1); 1998bcb0991SDimitry Andric } 2008bcb0991SDimitry Andric // Add metadata for the string 2018bcb0991SDimitry Andric std::string AStreamHolder; 2028bcb0991SDimitry Andric raw_string_ostream Sizes(AStreamHolder); 2038bcb0991SDimitry Andric int Sum = DWORD_ALIGN; 204*349cc55cSDimitry Andric Sizes << CI->arg_size() - 1; 2058bcb0991SDimitry Andric Sizes << ':'; 206*349cc55cSDimitry Andric for (unsigned ArgCount = 1; 207*349cc55cSDimitry Andric ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size(); 2088bcb0991SDimitry Andric ArgCount++) { 2098bcb0991SDimitry Andric Value *Arg = CI->getArgOperand(ArgCount); 2108bcb0991SDimitry Andric Type *ArgType = Arg->getType(); 2118bcb0991SDimitry Andric unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType); 2128bcb0991SDimitry Andric ArgSize = ArgSize / 8; 2138bcb0991SDimitry Andric // 2148bcb0991SDimitry Andric // ArgSize by design should be a multiple of DWORD_ALIGN, 2158bcb0991SDimitry Andric // expand the arguments that do not follow this rule. 2168bcb0991SDimitry Andric // 2178bcb0991SDimitry Andric if (ArgSize % DWORD_ALIGN != 0) { 2188bcb0991SDimitry Andric llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx); 2195ffd83dbSDimitry Andric auto *LLVMVecType = llvm::dyn_cast<llvm::FixedVectorType>(ArgType); 2208bcb0991SDimitry Andric int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1; 2218bcb0991SDimitry Andric if (LLVMVecType && NumElem > 1) 2225ffd83dbSDimitry Andric ResType = llvm::FixedVectorType::get(ResType, NumElem); 2238bcb0991SDimitry Andric Builder.SetInsertPoint(CI); 2248bcb0991SDimitry Andric Builder.SetCurrentDebugLocation(CI->getDebugLoc()); 2258bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'x' || 2268bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'X' || 2278bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'u' || 2288bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'o') 2298bcb0991SDimitry Andric Arg = Builder.CreateZExt(Arg, ResType); 2308bcb0991SDimitry Andric else 2318bcb0991SDimitry Andric Arg = Builder.CreateSExt(Arg, ResType); 2328bcb0991SDimitry Andric ArgType = Arg->getType(); 2338bcb0991SDimitry Andric ArgSize = TD->getTypeAllocSizeInBits(ArgType); 2348bcb0991SDimitry Andric ArgSize = ArgSize / 8; 2358bcb0991SDimitry Andric CI->setOperand(ArgCount, Arg); 2368bcb0991SDimitry Andric } 2378bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'f') { 2388bcb0991SDimitry Andric ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg); 2398bcb0991SDimitry Andric if (FpCons) 2408bcb0991SDimitry Andric ArgSize = 4; 2418bcb0991SDimitry Andric else { 2428bcb0991SDimitry Andric FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg); 2438bcb0991SDimitry Andric if (FpExt && FpExt->getType()->isDoubleTy() && 2448bcb0991SDimitry Andric FpExt->getOperand(0)->getType()->isFloatTy()) 2458bcb0991SDimitry Andric ArgSize = 4; 2468bcb0991SDimitry Andric } 2478bcb0991SDimitry Andric } 2488bcb0991SDimitry Andric if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) { 249e8d8bef9SDimitry Andric if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) { 250e8d8bef9SDimitry Andric auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 2518bcb0991SDimitry Andric if (GV && GV->hasInitializer()) { 2528bcb0991SDimitry Andric Constant *Init = GV->getInitializer(); 253e8d8bef9SDimitry Andric bool IsZeroValue = Init->isZeroValue(); 254e8d8bef9SDimitry Andric auto *CA = dyn_cast<ConstantDataArray>(Init); 255e8d8bef9SDimitry Andric if (IsZeroValue || (CA && CA->isString())) { 256e8d8bef9SDimitry Andric size_t SizeStr = 257e8d8bef9SDimitry Andric IsZeroValue ? 1 : (strlen(CA->getAsCString().data()) + 1); 2588bcb0991SDimitry Andric size_t Rem = SizeStr % DWORD_ALIGN; 2598bcb0991SDimitry Andric size_t NSizeStr = 0; 2608bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr 2618bcb0991SDimitry Andric << '\n'); 2628bcb0991SDimitry Andric if (Rem) { 2638bcb0991SDimitry Andric NSizeStr = SizeStr + (DWORD_ALIGN - Rem); 2648bcb0991SDimitry Andric } else { 2658bcb0991SDimitry Andric NSizeStr = SizeStr; 2668bcb0991SDimitry Andric } 2678bcb0991SDimitry Andric ArgSize = NSizeStr; 2688bcb0991SDimitry Andric } 2698bcb0991SDimitry Andric } else { 2708bcb0991SDimitry Andric ArgSize = sizeof(NonLiteralStr); 2718bcb0991SDimitry Andric } 2728bcb0991SDimitry Andric } else { 2738bcb0991SDimitry Andric ArgSize = sizeof(NonLiteralStr); 2748bcb0991SDimitry Andric } 2758bcb0991SDimitry Andric } 2768bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize 2778bcb0991SDimitry Andric << " for type: " << *ArgType << '\n'); 2788bcb0991SDimitry Andric Sizes << ArgSize << ':'; 2798bcb0991SDimitry Andric Sum += ArgSize; 2808bcb0991SDimitry Andric } 2818bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str() 2828bcb0991SDimitry Andric << '\n'); 2838bcb0991SDimitry Andric for (size_t I = 0; I < Str.size(); ++I) { 2848bcb0991SDimitry Andric // Rest of the C escape sequences (e.g. \') are handled correctly 2858bcb0991SDimitry Andric // by the MDParser 2868bcb0991SDimitry Andric switch (Str[I]) { 2878bcb0991SDimitry Andric case '\a': 2888bcb0991SDimitry Andric Sizes << "\\a"; 2898bcb0991SDimitry Andric break; 2908bcb0991SDimitry Andric case '\b': 2918bcb0991SDimitry Andric Sizes << "\\b"; 2928bcb0991SDimitry Andric break; 2938bcb0991SDimitry Andric case '\f': 2948bcb0991SDimitry Andric Sizes << "\\f"; 2958bcb0991SDimitry Andric break; 2968bcb0991SDimitry Andric case '\n': 2978bcb0991SDimitry Andric Sizes << "\\n"; 2988bcb0991SDimitry Andric break; 2998bcb0991SDimitry Andric case '\r': 3008bcb0991SDimitry Andric Sizes << "\\r"; 3018bcb0991SDimitry Andric break; 3028bcb0991SDimitry Andric case '\v': 3038bcb0991SDimitry Andric Sizes << "\\v"; 3048bcb0991SDimitry Andric break; 3058bcb0991SDimitry Andric case ':': 3068bcb0991SDimitry Andric // ':' cannot be scanned by Flex, as it is defined as a delimiter 3078bcb0991SDimitry Andric // Replace it with it's octal representation \72 3088bcb0991SDimitry Andric Sizes << "\\72"; 3098bcb0991SDimitry Andric break; 3108bcb0991SDimitry Andric default: 3118bcb0991SDimitry Andric Sizes << Str[I]; 3128bcb0991SDimitry Andric break; 3138bcb0991SDimitry Andric } 3148bcb0991SDimitry Andric } 3158bcb0991SDimitry Andric 3168bcb0991SDimitry Andric // Insert the printf_alloc call 3178bcb0991SDimitry Andric Builder.SetInsertPoint(CI); 3188bcb0991SDimitry Andric Builder.SetCurrentDebugLocation(CI->getDebugLoc()); 3198bcb0991SDimitry Andric 3208bcb0991SDimitry Andric AttributeList Attr = AttributeList::get(Ctx, AttributeList::FunctionIndex, 3218bcb0991SDimitry Andric Attribute::NoUnwind); 3228bcb0991SDimitry Andric 3238bcb0991SDimitry Andric Type *SizetTy = Type::getInt32Ty(Ctx); 3248bcb0991SDimitry Andric 3258bcb0991SDimitry Andric Type *Tys_alloc[1] = {SizetTy}; 326fe6060f1SDimitry Andric Type *I8Ty = Type::getInt8Ty(Ctx); 327fe6060f1SDimitry Andric Type *I8Ptr = PointerType::get(I8Ty, 1); 3288bcb0991SDimitry Andric FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false); 3298bcb0991SDimitry Andric FunctionCallee PrintfAllocFn = 3308bcb0991SDimitry Andric M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr); 3318bcb0991SDimitry Andric 3328bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n'); 333*349cc55cSDimitry Andric std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str(); 3348bcb0991SDimitry Andric MDString *fmtStrArray = MDString::get(Ctx, fmtstr); 3358bcb0991SDimitry Andric 3368bcb0991SDimitry Andric // Instead of creating global variables, the 3378bcb0991SDimitry Andric // printf format strings are extracted 3388bcb0991SDimitry Andric // and passed as metadata. This avoids 3398bcb0991SDimitry Andric // polluting llvm's symbol tables in this module. 3408bcb0991SDimitry Andric // Metadata is going to be extracted 3418bcb0991SDimitry Andric // by the backend passes and inserted 3428bcb0991SDimitry Andric // into the OpenCL binary as appropriate. 3438bcb0991SDimitry Andric StringRef amd("llvm.printf.fmts"); 3448bcb0991SDimitry Andric NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd); 3458bcb0991SDimitry Andric MDNode *myMD = MDNode::get(Ctx, fmtStrArray); 3468bcb0991SDimitry Andric metaD->addOperand(myMD); 3478bcb0991SDimitry Andric Value *sumC = ConstantInt::get(SizetTy, Sum, false); 3488bcb0991SDimitry Andric SmallVector<Value *, 1> alloc_args; 3498bcb0991SDimitry Andric alloc_args.push_back(sumC); 3508bcb0991SDimitry Andric CallInst *pcall = 3518bcb0991SDimitry Andric CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI); 3528bcb0991SDimitry Andric 3538bcb0991SDimitry Andric // 3548bcb0991SDimitry Andric // Insert code to split basicblock with a 3558bcb0991SDimitry Andric // piece of hammock code. 3568bcb0991SDimitry Andric // basicblock splits after buffer overflow check 3578bcb0991SDimitry Andric // 3588bcb0991SDimitry Andric ConstantPointerNull *zeroIntPtr = 359fe6060f1SDimitry Andric ConstantPointerNull::get(PointerType::get(I8Ty, 1)); 360fe6060f1SDimitry Andric auto *cmp = cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, "")); 3618bcb0991SDimitry Andric if (!CI->use_empty()) { 3628bcb0991SDimitry Andric Value *result = 3638bcb0991SDimitry Andric Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res"); 3648bcb0991SDimitry Andric CI->replaceAllUsesWith(result); 3658bcb0991SDimitry Andric } 3668bcb0991SDimitry Andric SplitBlock(CI->getParent(), cmp); 3678bcb0991SDimitry Andric Instruction *Brnch = 3688bcb0991SDimitry Andric SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false); 3698bcb0991SDimitry Andric 3708bcb0991SDimitry Andric Builder.SetInsertPoint(Brnch); 3718bcb0991SDimitry Andric 3728bcb0991SDimitry Andric // store unique printf id in the buffer 3738bcb0991SDimitry Andric // 374e8d8bef9SDimitry Andric GetElementPtrInst *BufferIdx = GetElementPtrInst::Create( 375fe6060f1SDimitry Andric I8Ty, pcall, ConstantInt::get(Ctx, APInt(32, 0)), "PrintBuffID", 376fe6060f1SDimitry Andric Brnch); 3778bcb0991SDimitry Andric 3788bcb0991SDimitry Andric Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS); 3798bcb0991SDimitry Andric Value *id_gep_cast = 3808bcb0991SDimitry Andric new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch); 3818bcb0991SDimitry Andric 3825ffd83dbSDimitry Andric new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast, Brnch); 3838bcb0991SDimitry Andric 384fe6060f1SDimitry Andric // 1st 4 bytes hold the printf_id 3858bcb0991SDimitry Andric // the following GEP is the buffer pointer 386fe6060f1SDimitry Andric BufferIdx = GetElementPtrInst::Create( 387fe6060f1SDimitry Andric I8Ty, pcall, ConstantInt::get(Ctx, APInt(32, 4)), "PrintBuffGep", 388fe6060f1SDimitry Andric Brnch); 3898bcb0991SDimitry Andric 3908bcb0991SDimitry Andric Type *Int32Ty = Type::getInt32Ty(Ctx); 3918bcb0991SDimitry Andric Type *Int64Ty = Type::getInt64Ty(Ctx); 392*349cc55cSDimitry Andric for (unsigned ArgCount = 1; 393*349cc55cSDimitry Andric ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size(); 3948bcb0991SDimitry Andric ArgCount++) { 3958bcb0991SDimitry Andric Value *Arg = CI->getArgOperand(ArgCount); 3968bcb0991SDimitry Andric Type *ArgType = Arg->getType(); 3978bcb0991SDimitry Andric SmallVector<Value *, 32> WhatToStore; 3985ffd83dbSDimitry Andric if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) { 3998bcb0991SDimitry Andric Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty; 4008bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'f') { 401e8d8bef9SDimitry Andric if (auto *FpCons = dyn_cast<ConstantFP>(Arg)) { 402e8d8bef9SDimitry Andric APFloat Val(FpCons->getValueAPF()); 4038bcb0991SDimitry Andric bool Lost = false; 4048bcb0991SDimitry Andric Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 4058bcb0991SDimitry Andric &Lost); 4068bcb0991SDimitry Andric Arg = ConstantFP::get(Ctx, Val); 4078bcb0991SDimitry Andric IType = Int32Ty; 408e8d8bef9SDimitry Andric } else if (auto *FpExt = dyn_cast<FPExtInst>(Arg)) { 409e8d8bef9SDimitry Andric if (FpExt->getType()->isDoubleTy() && 4108bcb0991SDimitry Andric FpExt->getOperand(0)->getType()->isFloatTy()) { 4118bcb0991SDimitry Andric Arg = FpExt->getOperand(0); 4128bcb0991SDimitry Andric IType = Int32Ty; 4138bcb0991SDimitry Andric } 4148bcb0991SDimitry Andric } 4158bcb0991SDimitry Andric } 4168bcb0991SDimitry Andric Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch); 4178bcb0991SDimitry Andric WhatToStore.push_back(Arg); 4188bcb0991SDimitry Andric } else if (ArgType->getTypeID() == Type::PointerTyID) { 4198bcb0991SDimitry Andric if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) { 4208bcb0991SDimitry Andric const char *S = NonLiteralStr; 421e8d8bef9SDimitry Andric if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) { 422e8d8bef9SDimitry Andric auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 4238bcb0991SDimitry Andric if (GV && GV->hasInitializer()) { 4248bcb0991SDimitry Andric Constant *Init = GV->getInitializer(); 425e8d8bef9SDimitry Andric bool IsZeroValue = Init->isZeroValue(); 426e8d8bef9SDimitry Andric auto *CA = dyn_cast<ConstantDataArray>(Init); 427e8d8bef9SDimitry Andric if (IsZeroValue || (CA && CA->isString())) { 428e8d8bef9SDimitry Andric S = IsZeroValue ? "" : CA->getAsCString().data(); 4298bcb0991SDimitry Andric } 4308bcb0991SDimitry Andric } 4318bcb0991SDimitry Andric } 4328bcb0991SDimitry Andric size_t SizeStr = strlen(S) + 1; 4338bcb0991SDimitry Andric size_t Rem = SizeStr % DWORD_ALIGN; 4348bcb0991SDimitry Andric size_t NSizeStr = 0; 4358bcb0991SDimitry Andric if (Rem) { 4368bcb0991SDimitry Andric NSizeStr = SizeStr + (DWORD_ALIGN - Rem); 4378bcb0991SDimitry Andric } else { 4388bcb0991SDimitry Andric NSizeStr = SizeStr; 4398bcb0991SDimitry Andric } 4408bcb0991SDimitry Andric if (S[0]) { 4418bcb0991SDimitry Andric char *MyNewStr = new char[NSizeStr](); 4428bcb0991SDimitry Andric strcpy(MyNewStr, S); 4438bcb0991SDimitry Andric int NumInts = NSizeStr / 4; 4448bcb0991SDimitry Andric int CharC = 0; 4458bcb0991SDimitry Andric while (NumInts) { 4468bcb0991SDimitry Andric int ANum = *(int *)(MyNewStr + CharC); 4478bcb0991SDimitry Andric CharC += 4; 4488bcb0991SDimitry Andric NumInts--; 4498bcb0991SDimitry Andric Value *ANumV = ConstantInt::get(Int32Ty, ANum, false); 4508bcb0991SDimitry Andric WhatToStore.push_back(ANumV); 4518bcb0991SDimitry Andric } 4528bcb0991SDimitry Andric delete[] MyNewStr; 4538bcb0991SDimitry Andric } else { 4548bcb0991SDimitry Andric // Empty string, give a hint to RT it is no NULL 4558bcb0991SDimitry Andric Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false); 4568bcb0991SDimitry Andric WhatToStore.push_back(ANumV); 4578bcb0991SDimitry Andric } 4588bcb0991SDimitry Andric } else { 4598bcb0991SDimitry Andric uint64_t Size = TD->getTypeAllocSizeInBits(ArgType); 4608bcb0991SDimitry Andric assert((Size == 32 || Size == 64) && "unsupported size"); 4618bcb0991SDimitry Andric Type *DstType = (Size == 32) ? Int32Ty : Int64Ty; 4628bcb0991SDimitry Andric Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch); 4638bcb0991SDimitry Andric WhatToStore.push_back(Arg); 4648bcb0991SDimitry Andric } 4655ffd83dbSDimitry Andric } else if (isa<FixedVectorType>(ArgType)) { 4668bcb0991SDimitry Andric Type *IType = NULL; 4675ffd83dbSDimitry Andric uint32_t EleCount = cast<FixedVectorType>(ArgType)->getNumElements(); 4688bcb0991SDimitry Andric uint32_t EleSize = ArgType->getScalarSizeInBits(); 4698bcb0991SDimitry Andric uint32_t TotalSize = EleCount * EleSize; 4708bcb0991SDimitry Andric if (EleCount == 3) { 4715ffd83dbSDimitry Andric ShuffleVectorInst *Shuffle = 4725ffd83dbSDimitry Andric new ShuffleVectorInst(Arg, Arg, ArrayRef<int>{0, 1, 2, 2}); 4738bcb0991SDimitry Andric Shuffle->insertBefore(Brnch); 4748bcb0991SDimitry Andric Arg = Shuffle; 4758bcb0991SDimitry Andric ArgType = Arg->getType(); 4768bcb0991SDimitry Andric TotalSize += EleSize; 4778bcb0991SDimitry Andric } 4788bcb0991SDimitry Andric switch (EleSize) { 4798bcb0991SDimitry Andric default: 4808bcb0991SDimitry Andric EleCount = TotalSize / 64; 481e8d8bef9SDimitry Andric IType = Type::getInt64Ty(ArgType->getContext()); 4828bcb0991SDimitry Andric break; 4838bcb0991SDimitry Andric case 8: 4848bcb0991SDimitry Andric if (EleCount >= 8) { 4858bcb0991SDimitry Andric EleCount = TotalSize / 64; 486e8d8bef9SDimitry Andric IType = Type::getInt64Ty(ArgType->getContext()); 4878bcb0991SDimitry Andric } else if (EleCount >= 3) { 4888bcb0991SDimitry Andric EleCount = 1; 489e8d8bef9SDimitry Andric IType = Type::getInt32Ty(ArgType->getContext()); 4908bcb0991SDimitry Andric } else { 4918bcb0991SDimitry Andric EleCount = 1; 492e8d8bef9SDimitry Andric IType = Type::getInt16Ty(ArgType->getContext()); 4938bcb0991SDimitry Andric } 4948bcb0991SDimitry Andric break; 4958bcb0991SDimitry Andric case 16: 4968bcb0991SDimitry Andric if (EleCount >= 3) { 4978bcb0991SDimitry Andric EleCount = TotalSize / 64; 498e8d8bef9SDimitry Andric IType = Type::getInt64Ty(ArgType->getContext()); 4998bcb0991SDimitry Andric } else { 5008bcb0991SDimitry Andric EleCount = 1; 501e8d8bef9SDimitry Andric IType = Type::getInt32Ty(ArgType->getContext()); 5028bcb0991SDimitry Andric } 5038bcb0991SDimitry Andric break; 5048bcb0991SDimitry Andric } 5058bcb0991SDimitry Andric if (EleCount > 1) { 5065ffd83dbSDimitry Andric IType = FixedVectorType::get(IType, EleCount); 5078bcb0991SDimitry Andric } 5088bcb0991SDimitry Andric Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch); 5098bcb0991SDimitry Andric WhatToStore.push_back(Arg); 5108bcb0991SDimitry Andric } else { 5118bcb0991SDimitry Andric WhatToStore.push_back(Arg); 5128bcb0991SDimitry Andric } 5138bcb0991SDimitry Andric for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) { 5148bcb0991SDimitry Andric Value *TheBtCast = WhatToStore[I]; 5158bcb0991SDimitry Andric unsigned ArgSize = 5168bcb0991SDimitry Andric TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8; 5178bcb0991SDimitry Andric SmallVector<Value *, 1> BuffOffset; 5188bcb0991SDimitry Andric BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize)); 5198bcb0991SDimitry Andric 5208bcb0991SDimitry Andric Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1); 5218bcb0991SDimitry Andric Value *CastedGEP = 5228bcb0991SDimitry Andric new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch); 5238bcb0991SDimitry Andric StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch); 5248bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n" 5258bcb0991SDimitry Andric << *StBuff << '\n'); 5268bcb0991SDimitry Andric (void)StBuff; 527*349cc55cSDimitry Andric if (I + 1 == E && ArgCount + 1 == CI->arg_size()) 5288bcb0991SDimitry Andric break; 529fe6060f1SDimitry Andric BufferIdx = GetElementPtrInst::Create(I8Ty, BufferIdx, BuffOffset, 530e8d8bef9SDimitry Andric "PrintBuffNextPtr", Brnch); 5318bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n" 5328bcb0991SDimitry Andric << *BufferIdx << '\n'); 5338bcb0991SDimitry Andric } 5348bcb0991SDimitry Andric } 5358bcb0991SDimitry Andric } 5368bcb0991SDimitry Andric } 5378bcb0991SDimitry Andric 5388bcb0991SDimitry Andric // erase the printf calls 5398bcb0991SDimitry Andric for (auto CI : Printfs) 5408bcb0991SDimitry Andric CI->eraseFromParent(); 5418bcb0991SDimitry Andric 5428bcb0991SDimitry Andric Printfs.clear(); 5438bcb0991SDimitry Andric return true; 5448bcb0991SDimitry Andric } 5458bcb0991SDimitry Andric 546e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBindingImpl::run(Module &M) { 5478bcb0991SDimitry Andric Triple TT(M.getTargetTriple()); 5488bcb0991SDimitry Andric if (TT.getArch() == Triple::r600) 5498bcb0991SDimitry Andric return false; 5508bcb0991SDimitry Andric 5518bcb0991SDimitry Andric auto PrintfFunction = M.getFunction("printf"); 5528bcb0991SDimitry Andric if (!PrintfFunction) 5538bcb0991SDimitry Andric return false; 5548bcb0991SDimitry Andric 5558bcb0991SDimitry Andric for (auto &U : PrintfFunction->uses()) { 5568bcb0991SDimitry Andric if (auto *CI = dyn_cast<CallInst>(U.getUser())) { 5578bcb0991SDimitry Andric if (CI->isCallee(&U)) 5588bcb0991SDimitry Andric Printfs.push_back(CI); 5598bcb0991SDimitry Andric } 5608bcb0991SDimitry Andric } 5618bcb0991SDimitry Andric 5628bcb0991SDimitry Andric if (Printfs.empty()) 5638bcb0991SDimitry Andric return false; 5648bcb0991SDimitry Andric 565480093f4SDimitry Andric if (auto HostcallFunction = M.getFunction("__ockl_hostcall_internal")) { 566480093f4SDimitry Andric for (auto &U : HostcallFunction->uses()) { 567480093f4SDimitry Andric if (auto *CI = dyn_cast<CallInst>(U.getUser())) { 568480093f4SDimitry Andric M.getContext().emitError( 569480093f4SDimitry Andric CI, "Cannot use both printf and hostcall in the same module"); 570480093f4SDimitry Andric } 571480093f4SDimitry Andric } 572480093f4SDimitry Andric } 573480093f4SDimitry Andric 5748bcb0991SDimitry Andric TD = &M.getDataLayout(); 575e8d8bef9SDimitry Andric 576e8d8bef9SDimitry Andric return lowerPrintfForGpu(M); 577e8d8bef9SDimitry Andric } 578e8d8bef9SDimitry Andric 579e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) { 580e8d8bef9SDimitry Andric auto GetDT = [this](Function &F) -> DominatorTree & { 581e8d8bef9SDimitry Andric return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree(); 582e8d8bef9SDimitry Andric }; 5838bcb0991SDimitry Andric auto GetTLI = [this](Function &F) -> TargetLibraryInfo & { 5848bcb0991SDimitry Andric return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 5858bcb0991SDimitry Andric }; 5868bcb0991SDimitry Andric 587e8d8bef9SDimitry Andric return AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M); 588e8d8bef9SDimitry Andric } 589e8d8bef9SDimitry Andric 590e8d8bef9SDimitry Andric PreservedAnalyses 591e8d8bef9SDimitry Andric AMDGPUPrintfRuntimeBindingPass::run(Module &M, ModuleAnalysisManager &AM) { 592e8d8bef9SDimitry Andric FunctionAnalysisManager &FAM = 593e8d8bef9SDimitry Andric AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 594e8d8bef9SDimitry Andric auto GetDT = [&FAM](Function &F) -> DominatorTree & { 595e8d8bef9SDimitry Andric return FAM.getResult<DominatorTreeAnalysis>(F); 596e8d8bef9SDimitry Andric }; 597e8d8bef9SDimitry Andric auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & { 598e8d8bef9SDimitry Andric return FAM.getResult<TargetLibraryAnalysis>(F); 599e8d8bef9SDimitry Andric }; 600e8d8bef9SDimitry Andric bool Changed = AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M); 601e8d8bef9SDimitry Andric return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 6028bcb0991SDimitry Andric } 603