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/ADT/SmallString.h" 238bcb0991SDimitry Andric #include "llvm/ADT/StringExtras.h" 248bcb0991SDimitry Andric #include "llvm/ADT/Triple.h" 258bcb0991SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h" 268bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 278bcb0991SDimitry Andric #include "llvm/CodeGen/Passes.h" 288bcb0991SDimitry Andric #include "llvm/IR/Constants.h" 298bcb0991SDimitry Andric #include "llvm/IR/DataLayout.h" 308bcb0991SDimitry Andric #include "llvm/IR/Dominators.h" 318bcb0991SDimitry Andric #include "llvm/IR/GlobalVariable.h" 328bcb0991SDimitry Andric #include "llvm/IR/IRBuilder.h" 338bcb0991SDimitry Andric #include "llvm/IR/Instructions.h" 348bcb0991SDimitry Andric #include "llvm/IR/Module.h" 358bcb0991SDimitry Andric #include "llvm/IR/Type.h" 36480093f4SDimitry Andric #include "llvm/InitializePasses.h" 378bcb0991SDimitry Andric #include "llvm/Support/CommandLine.h" 388bcb0991SDimitry Andric #include "llvm/Support/Debug.h" 398bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h" 408bcb0991SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 418bcb0991SDimitry Andric using namespace llvm; 428bcb0991SDimitry Andric 438bcb0991SDimitry Andric #define DEBUG_TYPE "printfToRuntime" 448bcb0991SDimitry Andric #define DWORD_ALIGN 4 458bcb0991SDimitry Andric 468bcb0991SDimitry Andric namespace { 478bcb0991SDimitry Andric class LLVM_LIBRARY_VISIBILITY AMDGPUPrintfRuntimeBinding final 488bcb0991SDimitry Andric : public ModulePass { 498bcb0991SDimitry Andric 508bcb0991SDimitry Andric public: 518bcb0991SDimitry Andric static char ID; 528bcb0991SDimitry Andric 538bcb0991SDimitry Andric explicit AMDGPUPrintfRuntimeBinding(); 548bcb0991SDimitry Andric 558bcb0991SDimitry Andric private: 568bcb0991SDimitry Andric bool runOnModule(Module &M) override; 578bcb0991SDimitry Andric void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers, 588bcb0991SDimitry Andric StringRef fmt, size_t num_ops) const; 598bcb0991SDimitry Andric 608bcb0991SDimitry Andric bool shouldPrintAsStr(char Specifier, Type *OpType) const; 618bcb0991SDimitry Andric bool 628bcb0991SDimitry Andric lowerPrintfForGpu(Module &M, 638bcb0991SDimitry Andric function_ref<const TargetLibraryInfo &(Function &)> GetTLI); 648bcb0991SDimitry Andric 658bcb0991SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 668bcb0991SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 678bcb0991SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 688bcb0991SDimitry Andric } 698bcb0991SDimitry Andric 708bcb0991SDimitry Andric Value *simplify(Instruction *I, const TargetLibraryInfo *TLI) { 718bcb0991SDimitry Andric return SimplifyInstruction(I, {*TD, TLI, DT}); 728bcb0991SDimitry Andric } 738bcb0991SDimitry Andric 748bcb0991SDimitry Andric const DataLayout *TD; 758bcb0991SDimitry Andric const DominatorTree *DT; 768bcb0991SDimitry Andric SmallVector<CallInst *, 32> Printfs; 778bcb0991SDimitry Andric }; 788bcb0991SDimitry Andric } // namespace 798bcb0991SDimitry Andric 808bcb0991SDimitry Andric char AMDGPUPrintfRuntimeBinding::ID = 0; 818bcb0991SDimitry Andric 828bcb0991SDimitry Andric INITIALIZE_PASS_BEGIN(AMDGPUPrintfRuntimeBinding, 838bcb0991SDimitry Andric "amdgpu-printf-runtime-binding", "AMDGPU Printf lowering", 848bcb0991SDimitry Andric false, false) 858bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 868bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 878bcb0991SDimitry Andric INITIALIZE_PASS_END(AMDGPUPrintfRuntimeBinding, "amdgpu-printf-runtime-binding", 888bcb0991SDimitry Andric "AMDGPU Printf lowering", false, false) 898bcb0991SDimitry Andric 908bcb0991SDimitry Andric char &llvm::AMDGPUPrintfRuntimeBindingID = AMDGPUPrintfRuntimeBinding::ID; 918bcb0991SDimitry Andric 928bcb0991SDimitry Andric namespace llvm { 938bcb0991SDimitry Andric ModulePass *createAMDGPUPrintfRuntimeBinding() { 948bcb0991SDimitry Andric return new AMDGPUPrintfRuntimeBinding(); 958bcb0991SDimitry Andric } 968bcb0991SDimitry Andric } // namespace llvm 978bcb0991SDimitry Andric 988bcb0991SDimitry Andric AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() 998bcb0991SDimitry Andric : ModulePass(ID), TD(nullptr), DT(nullptr) { 1008bcb0991SDimitry Andric initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry()); 1018bcb0991SDimitry Andric } 1028bcb0991SDimitry Andric 1038bcb0991SDimitry Andric void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers( 1048bcb0991SDimitry Andric SmallVectorImpl<char> &OpConvSpecifiers, StringRef Fmt, 1058bcb0991SDimitry Andric size_t NumOps) const { 1068bcb0991SDimitry Andric // not all format characters are collected. 1078bcb0991SDimitry Andric // At this time the format characters of interest 1088bcb0991SDimitry Andric // are %p and %s, which use to know if we 1098bcb0991SDimitry Andric // are either storing a literal string or a 1108bcb0991SDimitry Andric // pointer to the printf buffer. 1118bcb0991SDimitry Andric static const char ConvSpecifiers[] = "cdieEfgGaosuxXp"; 1128bcb0991SDimitry Andric size_t CurFmtSpecifierIdx = 0; 1138bcb0991SDimitry Andric size_t PrevFmtSpecifierIdx = 0; 1148bcb0991SDimitry Andric 1158bcb0991SDimitry Andric while ((CurFmtSpecifierIdx = Fmt.find_first_of( 1168bcb0991SDimitry Andric ConvSpecifiers, CurFmtSpecifierIdx)) != StringRef::npos) { 1178bcb0991SDimitry Andric bool ArgDump = false; 1188bcb0991SDimitry Andric StringRef CurFmt = Fmt.substr(PrevFmtSpecifierIdx, 1198bcb0991SDimitry Andric CurFmtSpecifierIdx - PrevFmtSpecifierIdx); 1208bcb0991SDimitry Andric size_t pTag = CurFmt.find_last_of("%"); 1218bcb0991SDimitry Andric if (pTag != StringRef::npos) { 1228bcb0991SDimitry Andric ArgDump = true; 1238bcb0991SDimitry Andric while (pTag && CurFmt[--pTag] == '%') { 1248bcb0991SDimitry Andric ArgDump = !ArgDump; 1258bcb0991SDimitry Andric } 1268bcb0991SDimitry Andric } 1278bcb0991SDimitry Andric 1288bcb0991SDimitry Andric if (ArgDump) 1298bcb0991SDimitry Andric OpConvSpecifiers.push_back(Fmt[CurFmtSpecifierIdx]); 1308bcb0991SDimitry Andric 1318bcb0991SDimitry Andric PrevFmtSpecifierIdx = ++CurFmtSpecifierIdx; 1328bcb0991SDimitry Andric } 1338bcb0991SDimitry Andric } 1348bcb0991SDimitry Andric 1358bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::shouldPrintAsStr(char Specifier, 1368bcb0991SDimitry Andric Type *OpType) const { 1378bcb0991SDimitry Andric if (Specifier != 's') 1388bcb0991SDimitry Andric return false; 1398bcb0991SDimitry Andric const PointerType *PT = dyn_cast<PointerType>(OpType); 1408bcb0991SDimitry Andric if (!PT || PT->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) 1418bcb0991SDimitry Andric return false; 1428bcb0991SDimitry Andric Type *ElemType = PT->getContainedType(0); 1438bcb0991SDimitry Andric if (ElemType->getTypeID() != Type::IntegerTyID) 1448bcb0991SDimitry Andric return false; 1458bcb0991SDimitry Andric IntegerType *ElemIType = cast<IntegerType>(ElemType); 1468bcb0991SDimitry Andric return ElemIType->getBitWidth() == 8; 1478bcb0991SDimitry Andric } 1488bcb0991SDimitry Andric 1498bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu( 1508bcb0991SDimitry Andric Module &M, function_ref<const TargetLibraryInfo &(Function &)> GetTLI) { 1518bcb0991SDimitry Andric LLVMContext &Ctx = M.getContext(); 1528bcb0991SDimitry Andric IRBuilder<> Builder(Ctx); 1538bcb0991SDimitry Andric Type *I32Ty = Type::getInt32Ty(Ctx); 1548bcb0991SDimitry Andric unsigned UniqID = 0; 1558bcb0991SDimitry Andric // NB: This is important for this string size to be divizable by 4 1568bcb0991SDimitry Andric const char NonLiteralStr[4] = "???"; 1578bcb0991SDimitry Andric 1588bcb0991SDimitry Andric for (auto CI : Printfs) { 1598bcb0991SDimitry Andric unsigned NumOps = CI->getNumArgOperands(); 1608bcb0991SDimitry Andric 1618bcb0991SDimitry Andric SmallString<16> OpConvSpecifiers; 1628bcb0991SDimitry Andric Value *Op = CI->getArgOperand(0); 1638bcb0991SDimitry Andric 1648bcb0991SDimitry Andric if (auto LI = dyn_cast<LoadInst>(Op)) { 1658bcb0991SDimitry Andric Op = LI->getPointerOperand(); 1668bcb0991SDimitry Andric for (auto Use : Op->users()) { 1678bcb0991SDimitry Andric if (auto SI = dyn_cast<StoreInst>(Use)) { 1688bcb0991SDimitry Andric Op = SI->getValueOperand(); 1698bcb0991SDimitry Andric break; 1708bcb0991SDimitry Andric } 1718bcb0991SDimitry Andric } 1728bcb0991SDimitry Andric } 1738bcb0991SDimitry Andric 1748bcb0991SDimitry Andric if (auto I = dyn_cast<Instruction>(Op)) { 1758bcb0991SDimitry Andric Value *Op_simplified = simplify(I, &GetTLI(*I->getFunction())); 1768bcb0991SDimitry Andric if (Op_simplified) 1778bcb0991SDimitry Andric Op = Op_simplified; 1788bcb0991SDimitry Andric } 1798bcb0991SDimitry Andric 1808bcb0991SDimitry Andric ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Op); 1818bcb0991SDimitry Andric 1828bcb0991SDimitry Andric if (ConstExpr) { 1838bcb0991SDimitry Andric GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 1848bcb0991SDimitry Andric 1858bcb0991SDimitry Andric StringRef Str("unknown"); 1868bcb0991SDimitry Andric if (GVar && GVar->hasInitializer()) { 1878bcb0991SDimitry Andric auto Init = GVar->getInitializer(); 1888bcb0991SDimitry Andric if (auto CA = dyn_cast<ConstantDataArray>(Init)) { 1898bcb0991SDimitry Andric if (CA->isString()) 1908bcb0991SDimitry Andric Str = CA->getAsCString(); 1918bcb0991SDimitry Andric } else if (isa<ConstantAggregateZero>(Init)) { 1928bcb0991SDimitry Andric Str = ""; 1938bcb0991SDimitry Andric } 1948bcb0991SDimitry Andric // 1958bcb0991SDimitry Andric // we need this call to ascertain 1968bcb0991SDimitry Andric // that we are printing a string 1978bcb0991SDimitry Andric // or a pointer. It takes out the 1988bcb0991SDimitry Andric // specifiers and fills up the first 1998bcb0991SDimitry Andric // arg 2008bcb0991SDimitry Andric getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1); 2018bcb0991SDimitry Andric } 2028bcb0991SDimitry Andric // Add metadata for the string 2038bcb0991SDimitry Andric std::string AStreamHolder; 2048bcb0991SDimitry Andric raw_string_ostream Sizes(AStreamHolder); 2058bcb0991SDimitry Andric int Sum = DWORD_ALIGN; 2068bcb0991SDimitry Andric Sizes << CI->getNumArgOperands() - 1; 2078bcb0991SDimitry Andric Sizes << ':'; 2088bcb0991SDimitry Andric for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() && 2098bcb0991SDimitry Andric ArgCount <= OpConvSpecifiers.size(); 2108bcb0991SDimitry Andric ArgCount++) { 2118bcb0991SDimitry Andric Value *Arg = CI->getArgOperand(ArgCount); 2128bcb0991SDimitry Andric Type *ArgType = Arg->getType(); 2138bcb0991SDimitry Andric unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType); 2148bcb0991SDimitry Andric ArgSize = ArgSize / 8; 2158bcb0991SDimitry Andric // 2168bcb0991SDimitry Andric // ArgSize by design should be a multiple of DWORD_ALIGN, 2178bcb0991SDimitry Andric // expand the arguments that do not follow this rule. 2188bcb0991SDimitry Andric // 2198bcb0991SDimitry Andric if (ArgSize % DWORD_ALIGN != 0) { 2208bcb0991SDimitry Andric llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx); 221*5ffd83dbSDimitry Andric auto *LLVMVecType = llvm::dyn_cast<llvm::FixedVectorType>(ArgType); 2228bcb0991SDimitry Andric int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1; 2238bcb0991SDimitry Andric if (LLVMVecType && NumElem > 1) 224*5ffd83dbSDimitry Andric ResType = llvm::FixedVectorType::get(ResType, NumElem); 2258bcb0991SDimitry Andric Builder.SetInsertPoint(CI); 2268bcb0991SDimitry Andric Builder.SetCurrentDebugLocation(CI->getDebugLoc()); 2278bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'x' || 2288bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'X' || 2298bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'u' || 2308bcb0991SDimitry Andric OpConvSpecifiers[ArgCount - 1] == 'o') 2318bcb0991SDimitry Andric Arg = Builder.CreateZExt(Arg, ResType); 2328bcb0991SDimitry Andric else 2338bcb0991SDimitry Andric Arg = Builder.CreateSExt(Arg, ResType); 2348bcb0991SDimitry Andric ArgType = Arg->getType(); 2358bcb0991SDimitry Andric ArgSize = TD->getTypeAllocSizeInBits(ArgType); 2368bcb0991SDimitry Andric ArgSize = ArgSize / 8; 2378bcb0991SDimitry Andric CI->setOperand(ArgCount, Arg); 2388bcb0991SDimitry Andric } 2398bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'f') { 2408bcb0991SDimitry Andric ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg); 2418bcb0991SDimitry Andric if (FpCons) 2428bcb0991SDimitry Andric ArgSize = 4; 2438bcb0991SDimitry Andric else { 2448bcb0991SDimitry Andric FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg); 2458bcb0991SDimitry Andric if (FpExt && FpExt->getType()->isDoubleTy() && 2468bcb0991SDimitry Andric FpExt->getOperand(0)->getType()->isFloatTy()) 2478bcb0991SDimitry Andric ArgSize = 4; 2488bcb0991SDimitry Andric } 2498bcb0991SDimitry Andric } 2508bcb0991SDimitry Andric if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) { 2518bcb0991SDimitry Andric if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) { 2528bcb0991SDimitry Andric GlobalVariable *GV = 2538bcb0991SDimitry Andric dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 2548bcb0991SDimitry Andric if (GV && GV->hasInitializer()) { 2558bcb0991SDimitry Andric Constant *Init = GV->getInitializer(); 2568bcb0991SDimitry Andric ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init); 2578bcb0991SDimitry Andric if (Init->isZeroValue() || CA->isString()) { 2588bcb0991SDimitry Andric size_t SizeStr = Init->isZeroValue() 2598bcb0991SDimitry Andric ? 1 2608bcb0991SDimitry Andric : (strlen(CA->getAsCString().data()) + 1); 2618bcb0991SDimitry Andric size_t Rem = SizeStr % DWORD_ALIGN; 2628bcb0991SDimitry Andric size_t NSizeStr = 0; 2638bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr 2648bcb0991SDimitry Andric << '\n'); 2658bcb0991SDimitry Andric if (Rem) { 2668bcb0991SDimitry Andric NSizeStr = SizeStr + (DWORD_ALIGN - Rem); 2678bcb0991SDimitry Andric } else { 2688bcb0991SDimitry Andric NSizeStr = SizeStr; 2698bcb0991SDimitry Andric } 2708bcb0991SDimitry Andric ArgSize = NSizeStr; 2718bcb0991SDimitry Andric } 2728bcb0991SDimitry Andric } else { 2738bcb0991SDimitry Andric ArgSize = sizeof(NonLiteralStr); 2748bcb0991SDimitry Andric } 2758bcb0991SDimitry Andric } else { 2768bcb0991SDimitry Andric ArgSize = sizeof(NonLiteralStr); 2778bcb0991SDimitry Andric } 2788bcb0991SDimitry Andric } 2798bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize 2808bcb0991SDimitry Andric << " for type: " << *ArgType << '\n'); 2818bcb0991SDimitry Andric Sizes << ArgSize << ':'; 2828bcb0991SDimitry Andric Sum += ArgSize; 2838bcb0991SDimitry Andric } 2848bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str() 2858bcb0991SDimitry Andric << '\n'); 2868bcb0991SDimitry Andric for (size_t I = 0; I < Str.size(); ++I) { 2878bcb0991SDimitry Andric // Rest of the C escape sequences (e.g. \') are handled correctly 2888bcb0991SDimitry Andric // by the MDParser 2898bcb0991SDimitry Andric switch (Str[I]) { 2908bcb0991SDimitry Andric case '\a': 2918bcb0991SDimitry Andric Sizes << "\\a"; 2928bcb0991SDimitry Andric break; 2938bcb0991SDimitry Andric case '\b': 2948bcb0991SDimitry Andric Sizes << "\\b"; 2958bcb0991SDimitry Andric break; 2968bcb0991SDimitry Andric case '\f': 2978bcb0991SDimitry Andric Sizes << "\\f"; 2988bcb0991SDimitry Andric break; 2998bcb0991SDimitry Andric case '\n': 3008bcb0991SDimitry Andric Sizes << "\\n"; 3018bcb0991SDimitry Andric break; 3028bcb0991SDimitry Andric case '\r': 3038bcb0991SDimitry Andric Sizes << "\\r"; 3048bcb0991SDimitry Andric break; 3058bcb0991SDimitry Andric case '\v': 3068bcb0991SDimitry Andric Sizes << "\\v"; 3078bcb0991SDimitry Andric break; 3088bcb0991SDimitry Andric case ':': 3098bcb0991SDimitry Andric // ':' cannot be scanned by Flex, as it is defined as a delimiter 3108bcb0991SDimitry Andric // Replace it with it's octal representation \72 3118bcb0991SDimitry Andric Sizes << "\\72"; 3128bcb0991SDimitry Andric break; 3138bcb0991SDimitry Andric default: 3148bcb0991SDimitry Andric Sizes << Str[I]; 3158bcb0991SDimitry Andric break; 3168bcb0991SDimitry Andric } 3178bcb0991SDimitry Andric } 3188bcb0991SDimitry Andric 3198bcb0991SDimitry Andric // Insert the printf_alloc call 3208bcb0991SDimitry Andric Builder.SetInsertPoint(CI); 3218bcb0991SDimitry Andric Builder.SetCurrentDebugLocation(CI->getDebugLoc()); 3228bcb0991SDimitry Andric 3238bcb0991SDimitry Andric AttributeList Attr = AttributeList::get(Ctx, AttributeList::FunctionIndex, 3248bcb0991SDimitry Andric Attribute::NoUnwind); 3258bcb0991SDimitry Andric 3268bcb0991SDimitry Andric Type *SizetTy = Type::getInt32Ty(Ctx); 3278bcb0991SDimitry Andric 3288bcb0991SDimitry Andric Type *Tys_alloc[1] = {SizetTy}; 3298bcb0991SDimitry Andric Type *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 1); 3308bcb0991SDimitry Andric FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false); 3318bcb0991SDimitry Andric FunctionCallee PrintfAllocFn = 3328bcb0991SDimitry Andric M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr); 3338bcb0991SDimitry Andric 3348bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n'); 3358bcb0991SDimitry Andric std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str().c_str(); 3368bcb0991SDimitry Andric MDString *fmtStrArray = MDString::get(Ctx, fmtstr); 3378bcb0991SDimitry Andric 3388bcb0991SDimitry Andric // Instead of creating global variables, the 3398bcb0991SDimitry Andric // printf format strings are extracted 3408bcb0991SDimitry Andric // and passed as metadata. This avoids 3418bcb0991SDimitry Andric // polluting llvm's symbol tables in this module. 3428bcb0991SDimitry Andric // Metadata is going to be extracted 3438bcb0991SDimitry Andric // by the backend passes and inserted 3448bcb0991SDimitry Andric // into the OpenCL binary as appropriate. 3458bcb0991SDimitry Andric StringRef amd("llvm.printf.fmts"); 3468bcb0991SDimitry Andric NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd); 3478bcb0991SDimitry Andric MDNode *myMD = MDNode::get(Ctx, fmtStrArray); 3488bcb0991SDimitry Andric metaD->addOperand(myMD); 3498bcb0991SDimitry Andric Value *sumC = ConstantInt::get(SizetTy, Sum, false); 3508bcb0991SDimitry Andric SmallVector<Value *, 1> alloc_args; 3518bcb0991SDimitry Andric alloc_args.push_back(sumC); 3528bcb0991SDimitry Andric CallInst *pcall = 3538bcb0991SDimitry Andric CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI); 3548bcb0991SDimitry Andric 3558bcb0991SDimitry Andric // 3568bcb0991SDimitry Andric // Insert code to split basicblock with a 3578bcb0991SDimitry Andric // piece of hammock code. 3588bcb0991SDimitry Andric // basicblock splits after buffer overflow check 3598bcb0991SDimitry Andric // 3608bcb0991SDimitry Andric ConstantPointerNull *zeroIntPtr = 3618bcb0991SDimitry Andric ConstantPointerNull::get(PointerType::get(Type::getInt8Ty(Ctx), 1)); 3628bcb0991SDimitry Andric ICmpInst *cmp = 3638bcb0991SDimitry Andric dyn_cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, "")); 3648bcb0991SDimitry Andric if (!CI->use_empty()) { 3658bcb0991SDimitry Andric Value *result = 3668bcb0991SDimitry Andric Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res"); 3678bcb0991SDimitry Andric CI->replaceAllUsesWith(result); 3688bcb0991SDimitry Andric } 3698bcb0991SDimitry Andric SplitBlock(CI->getParent(), cmp); 3708bcb0991SDimitry Andric Instruction *Brnch = 3718bcb0991SDimitry Andric SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false); 3728bcb0991SDimitry Andric 3738bcb0991SDimitry Andric Builder.SetInsertPoint(Brnch); 3748bcb0991SDimitry Andric 3758bcb0991SDimitry Andric // store unique printf id in the buffer 3768bcb0991SDimitry Andric // 3778bcb0991SDimitry Andric SmallVector<Value *, 1> ZeroIdxList; 3788bcb0991SDimitry Andric ConstantInt *zeroInt = 3798bcb0991SDimitry Andric ConstantInt::get(Ctx, APInt(32, StringRef("0"), 10)); 3808bcb0991SDimitry Andric ZeroIdxList.push_back(zeroInt); 3818bcb0991SDimitry Andric 3828bcb0991SDimitry Andric GetElementPtrInst *BufferIdx = 3838bcb0991SDimitry Andric dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create( 3848bcb0991SDimitry Andric nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch)); 3858bcb0991SDimitry Andric 3868bcb0991SDimitry Andric Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS); 3878bcb0991SDimitry Andric Value *id_gep_cast = 3888bcb0991SDimitry Andric new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch); 3898bcb0991SDimitry Andric 390*5ffd83dbSDimitry Andric new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast, Brnch); 3918bcb0991SDimitry Andric 3928bcb0991SDimitry Andric SmallVector<Value *, 2> FourthIdxList; 3938bcb0991SDimitry Andric ConstantInt *fourInt = 3948bcb0991SDimitry Andric ConstantInt::get(Ctx, APInt(32, StringRef("4"), 10)); 3958bcb0991SDimitry Andric 3968bcb0991SDimitry Andric FourthIdxList.push_back(fourInt); // 1st 4 bytes hold the printf_id 3978bcb0991SDimitry Andric // the following GEP is the buffer pointer 3988bcb0991SDimitry Andric BufferIdx = cast<GetElementPtrInst>(GetElementPtrInst::Create( 3998bcb0991SDimitry Andric nullptr, pcall, FourthIdxList, "PrintBuffGep", Brnch)); 4008bcb0991SDimitry Andric 4018bcb0991SDimitry Andric Type *Int32Ty = Type::getInt32Ty(Ctx); 4028bcb0991SDimitry Andric Type *Int64Ty = Type::getInt64Ty(Ctx); 4038bcb0991SDimitry Andric for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() && 4048bcb0991SDimitry Andric ArgCount <= OpConvSpecifiers.size(); 4058bcb0991SDimitry Andric ArgCount++) { 4068bcb0991SDimitry Andric Value *Arg = CI->getArgOperand(ArgCount); 4078bcb0991SDimitry Andric Type *ArgType = Arg->getType(); 4088bcb0991SDimitry Andric SmallVector<Value *, 32> WhatToStore; 409*5ffd83dbSDimitry Andric if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) { 4108bcb0991SDimitry Andric Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty; 4118bcb0991SDimitry Andric if (OpConvSpecifiers[ArgCount - 1] == 'f') { 4128bcb0991SDimitry Andric ConstantFP *fpCons = dyn_cast<ConstantFP>(Arg); 4138bcb0991SDimitry Andric if (fpCons) { 4148bcb0991SDimitry Andric APFloat Val(fpCons->getValueAPF()); 4158bcb0991SDimitry Andric bool Lost = false; 4168bcb0991SDimitry Andric Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 4178bcb0991SDimitry Andric &Lost); 4188bcb0991SDimitry Andric Arg = ConstantFP::get(Ctx, Val); 4198bcb0991SDimitry Andric IType = Int32Ty; 4208bcb0991SDimitry Andric } else { 4218bcb0991SDimitry Andric FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg); 4228bcb0991SDimitry Andric if (FpExt && FpExt->getType()->isDoubleTy() && 4238bcb0991SDimitry Andric FpExt->getOperand(0)->getType()->isFloatTy()) { 4248bcb0991SDimitry Andric Arg = FpExt->getOperand(0); 4258bcb0991SDimitry Andric IType = Int32Ty; 4268bcb0991SDimitry Andric } 4278bcb0991SDimitry Andric } 4288bcb0991SDimitry Andric } 4298bcb0991SDimitry Andric Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch); 4308bcb0991SDimitry Andric WhatToStore.push_back(Arg); 4318bcb0991SDimitry Andric } else if (ArgType->getTypeID() == Type::PointerTyID) { 4328bcb0991SDimitry Andric if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) { 4338bcb0991SDimitry Andric const char *S = NonLiteralStr; 4348bcb0991SDimitry Andric if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) { 4358bcb0991SDimitry Andric GlobalVariable *GV = 4368bcb0991SDimitry Andric dyn_cast<GlobalVariable>(ConstExpr->getOperand(0)); 4378bcb0991SDimitry Andric if (GV && GV->hasInitializer()) { 4388bcb0991SDimitry Andric Constant *Init = GV->getInitializer(); 4398bcb0991SDimitry Andric ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init); 4408bcb0991SDimitry Andric if (Init->isZeroValue() || CA->isString()) { 4418bcb0991SDimitry Andric S = Init->isZeroValue() ? "" : CA->getAsCString().data(); 4428bcb0991SDimitry Andric } 4438bcb0991SDimitry Andric } 4448bcb0991SDimitry Andric } 4458bcb0991SDimitry Andric size_t SizeStr = strlen(S) + 1; 4468bcb0991SDimitry Andric size_t Rem = SizeStr % DWORD_ALIGN; 4478bcb0991SDimitry Andric size_t NSizeStr = 0; 4488bcb0991SDimitry Andric if (Rem) { 4498bcb0991SDimitry Andric NSizeStr = SizeStr + (DWORD_ALIGN - Rem); 4508bcb0991SDimitry Andric } else { 4518bcb0991SDimitry Andric NSizeStr = SizeStr; 4528bcb0991SDimitry Andric } 4538bcb0991SDimitry Andric if (S[0]) { 4548bcb0991SDimitry Andric char *MyNewStr = new char[NSizeStr](); 4558bcb0991SDimitry Andric strcpy(MyNewStr, S); 4568bcb0991SDimitry Andric int NumInts = NSizeStr / 4; 4578bcb0991SDimitry Andric int CharC = 0; 4588bcb0991SDimitry Andric while (NumInts) { 4598bcb0991SDimitry Andric int ANum = *(int *)(MyNewStr + CharC); 4608bcb0991SDimitry Andric CharC += 4; 4618bcb0991SDimitry Andric NumInts--; 4628bcb0991SDimitry Andric Value *ANumV = ConstantInt::get(Int32Ty, ANum, false); 4638bcb0991SDimitry Andric WhatToStore.push_back(ANumV); 4648bcb0991SDimitry Andric } 4658bcb0991SDimitry Andric delete[] MyNewStr; 4668bcb0991SDimitry Andric } else { 4678bcb0991SDimitry Andric // Empty string, give a hint to RT it is no NULL 4688bcb0991SDimitry Andric Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false); 4698bcb0991SDimitry Andric WhatToStore.push_back(ANumV); 4708bcb0991SDimitry Andric } 4718bcb0991SDimitry Andric } else { 4728bcb0991SDimitry Andric uint64_t Size = TD->getTypeAllocSizeInBits(ArgType); 4738bcb0991SDimitry Andric assert((Size == 32 || Size == 64) && "unsupported size"); 4748bcb0991SDimitry Andric Type *DstType = (Size == 32) ? Int32Ty : Int64Ty; 4758bcb0991SDimitry Andric Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch); 4768bcb0991SDimitry Andric WhatToStore.push_back(Arg); 4778bcb0991SDimitry Andric } 478*5ffd83dbSDimitry Andric } else if (isa<FixedVectorType>(ArgType)) { 4798bcb0991SDimitry Andric Type *IType = NULL; 480*5ffd83dbSDimitry Andric uint32_t EleCount = cast<FixedVectorType>(ArgType)->getNumElements(); 4818bcb0991SDimitry Andric uint32_t EleSize = ArgType->getScalarSizeInBits(); 4828bcb0991SDimitry Andric uint32_t TotalSize = EleCount * EleSize; 4838bcb0991SDimitry Andric if (EleCount == 3) { 484*5ffd83dbSDimitry Andric ShuffleVectorInst *Shuffle = 485*5ffd83dbSDimitry Andric new ShuffleVectorInst(Arg, Arg, ArrayRef<int>{0, 1, 2, 2}); 4868bcb0991SDimitry Andric Shuffle->insertBefore(Brnch); 4878bcb0991SDimitry Andric Arg = Shuffle; 4888bcb0991SDimitry Andric ArgType = Arg->getType(); 4898bcb0991SDimitry Andric TotalSize += EleSize; 4908bcb0991SDimitry Andric } 4918bcb0991SDimitry Andric switch (EleSize) { 4928bcb0991SDimitry Andric default: 4938bcb0991SDimitry Andric EleCount = TotalSize / 64; 4948bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext())); 4958bcb0991SDimitry Andric break; 4968bcb0991SDimitry Andric case 8: 4978bcb0991SDimitry Andric if (EleCount >= 8) { 4988bcb0991SDimitry Andric EleCount = TotalSize / 64; 4998bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext())); 5008bcb0991SDimitry Andric } else if (EleCount >= 3) { 5018bcb0991SDimitry Andric EleCount = 1; 5028bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext())); 5038bcb0991SDimitry Andric } else { 5048bcb0991SDimitry Andric EleCount = 1; 5058bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt16Ty(ArgType->getContext())); 5068bcb0991SDimitry Andric } 5078bcb0991SDimitry Andric break; 5088bcb0991SDimitry Andric case 16: 5098bcb0991SDimitry Andric if (EleCount >= 3) { 5108bcb0991SDimitry Andric EleCount = TotalSize / 64; 5118bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext())); 5128bcb0991SDimitry Andric } else { 5138bcb0991SDimitry Andric EleCount = 1; 5148bcb0991SDimitry Andric IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext())); 5158bcb0991SDimitry Andric } 5168bcb0991SDimitry Andric break; 5178bcb0991SDimitry Andric } 5188bcb0991SDimitry Andric if (EleCount > 1) { 519*5ffd83dbSDimitry Andric IType = FixedVectorType::get(IType, EleCount); 5208bcb0991SDimitry Andric } 5218bcb0991SDimitry Andric Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch); 5228bcb0991SDimitry Andric WhatToStore.push_back(Arg); 5238bcb0991SDimitry Andric } else { 5248bcb0991SDimitry Andric WhatToStore.push_back(Arg); 5258bcb0991SDimitry Andric } 5268bcb0991SDimitry Andric for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) { 5278bcb0991SDimitry Andric Value *TheBtCast = WhatToStore[I]; 5288bcb0991SDimitry Andric unsigned ArgSize = 5298bcb0991SDimitry Andric TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8; 5308bcb0991SDimitry Andric SmallVector<Value *, 1> BuffOffset; 5318bcb0991SDimitry Andric BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize)); 5328bcb0991SDimitry Andric 5338bcb0991SDimitry Andric Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1); 5348bcb0991SDimitry Andric Value *CastedGEP = 5358bcb0991SDimitry Andric new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch); 5368bcb0991SDimitry Andric StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch); 5378bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n" 5388bcb0991SDimitry Andric << *StBuff << '\n'); 5398bcb0991SDimitry Andric (void)StBuff; 5408bcb0991SDimitry Andric if (I + 1 == E && ArgCount + 1 == CI->getNumArgOperands()) 5418bcb0991SDimitry Andric break; 5428bcb0991SDimitry Andric BufferIdx = dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create( 5438bcb0991SDimitry Andric nullptr, BufferIdx, BuffOffset, "PrintBuffNextPtr", Brnch)); 5448bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n" 5458bcb0991SDimitry Andric << *BufferIdx << '\n'); 5468bcb0991SDimitry Andric } 5478bcb0991SDimitry Andric } 5488bcb0991SDimitry Andric } 5498bcb0991SDimitry Andric } 5508bcb0991SDimitry Andric 5518bcb0991SDimitry Andric // erase the printf calls 5528bcb0991SDimitry Andric for (auto CI : Printfs) 5538bcb0991SDimitry Andric CI->eraseFromParent(); 5548bcb0991SDimitry Andric 5558bcb0991SDimitry Andric Printfs.clear(); 5568bcb0991SDimitry Andric return true; 5578bcb0991SDimitry Andric } 5588bcb0991SDimitry Andric 5598bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) { 5608bcb0991SDimitry Andric Triple TT(M.getTargetTriple()); 5618bcb0991SDimitry Andric if (TT.getArch() == Triple::r600) 5628bcb0991SDimitry Andric return false; 5638bcb0991SDimitry Andric 5648bcb0991SDimitry Andric auto PrintfFunction = M.getFunction("printf"); 5658bcb0991SDimitry Andric if (!PrintfFunction) 5668bcb0991SDimitry Andric return false; 5678bcb0991SDimitry Andric 5688bcb0991SDimitry Andric for (auto &U : PrintfFunction->uses()) { 5698bcb0991SDimitry Andric if (auto *CI = dyn_cast<CallInst>(U.getUser())) { 5708bcb0991SDimitry Andric if (CI->isCallee(&U)) 5718bcb0991SDimitry Andric Printfs.push_back(CI); 5728bcb0991SDimitry Andric } 5738bcb0991SDimitry Andric } 5748bcb0991SDimitry Andric 5758bcb0991SDimitry Andric if (Printfs.empty()) 5768bcb0991SDimitry Andric return false; 5778bcb0991SDimitry Andric 578480093f4SDimitry Andric if (auto HostcallFunction = M.getFunction("__ockl_hostcall_internal")) { 579480093f4SDimitry Andric for (auto &U : HostcallFunction->uses()) { 580480093f4SDimitry Andric if (auto *CI = dyn_cast<CallInst>(U.getUser())) { 581480093f4SDimitry Andric M.getContext().emitError( 582480093f4SDimitry Andric CI, "Cannot use both printf and hostcall in the same module"); 583480093f4SDimitry Andric } 584480093f4SDimitry Andric } 585480093f4SDimitry Andric } 586480093f4SDimitry Andric 5878bcb0991SDimitry Andric TD = &M.getDataLayout(); 5888bcb0991SDimitry Andric auto DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 5898bcb0991SDimitry Andric DT = DTWP ? &DTWP->getDomTree() : nullptr; 5908bcb0991SDimitry Andric auto GetTLI = [this](Function &F) -> TargetLibraryInfo & { 5918bcb0991SDimitry Andric return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 5928bcb0991SDimitry Andric }; 5938bcb0991SDimitry Andric 5948bcb0991SDimitry Andric return lowerPrintfForGpu(M, GetTLI); 5958bcb0991SDimitry Andric } 596