xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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"
29*e8d8bef9SDimitry 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 {
36*e8d8bef9SDimitry 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   }
50*e8d8bef9SDimitry Andric };
518bcb0991SDimitry Andric 
52*e8d8bef9SDimitry Andric class AMDGPUPrintfRuntimeBindingImpl {
53*e8d8bef9SDimitry Andric public:
54*e8d8bef9SDimitry Andric   AMDGPUPrintfRuntimeBindingImpl(
55*e8d8bef9SDimitry Andric       function_ref<const DominatorTree &(Function &)> GetDT,
56*e8d8bef9SDimitry Andric       function_ref<const TargetLibraryInfo &(Function &)> GetTLI)
57*e8d8bef9SDimitry Andric       : GetDT(GetDT), GetTLI(GetTLI) {}
58*e8d8bef9SDimitry Andric   bool run(Module &M);
59*e8d8bef9SDimitry Andric 
60*e8d8bef9SDimitry Andric private:
61*e8d8bef9SDimitry Andric   void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers,
62*e8d8bef9SDimitry Andric                                StringRef fmt, size_t num_ops) const;
63*e8d8bef9SDimitry Andric 
64*e8d8bef9SDimitry Andric   bool shouldPrintAsStr(char Specifier, Type *OpType) const;
65*e8d8bef9SDimitry Andric   bool lowerPrintfForGpu(Module &M);
66*e8d8bef9SDimitry Andric 
67*e8d8bef9SDimitry Andric   Value *simplify(Instruction *I, const TargetLibraryInfo *TLI,
68*e8d8bef9SDimitry Andric                   const DominatorTree *DT) {
698bcb0991SDimitry Andric     return SimplifyInstruction(I, {*TD, TLI, DT});
708bcb0991SDimitry Andric   }
718bcb0991SDimitry Andric 
728bcb0991SDimitry Andric   const DataLayout *TD;
73*e8d8bef9SDimitry Andric   function_ref<const DominatorTree &(Function &)> GetDT;
74*e8d8bef9SDimitry 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 
97*e8d8bef9SDimitry Andric AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding() : ModulePass(ID) {
988bcb0991SDimitry Andric   initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry());
998bcb0991SDimitry Andric }
1008bcb0991SDimitry Andric 
101*e8d8bef9SDimitry 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 
133*e8d8bef9SDimitry 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 
147*e8d8bef9SDimitry 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;
1528bcb0991SDimitry Andric   // NB: This is important for this string size to be divizable by 4
1538bcb0991SDimitry Andric   const char NonLiteralStr[4] = "???";
1548bcb0991SDimitry Andric 
1558bcb0991SDimitry Andric   for (auto CI : Printfs) {
1568bcb0991SDimitry Andric     unsigned NumOps = CI->getNumArgOperands();
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)) {
172*e8d8bef9SDimitry Andric       Value *Op_simplified =
173*e8d8bef9SDimitry 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()) {
185*e8d8bef9SDimitry Andric         auto *Init = GVar->getInitializer();
186*e8d8bef9SDimitry 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;
2048bcb0991SDimitry Andric       Sizes << CI->getNumArgOperands() - 1;
2058bcb0991SDimitry Andric       Sizes << ':';
2068bcb0991SDimitry Andric       for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
2078bcb0991SDimitry Andric                                   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)) {
249*e8d8bef9SDimitry Andric           if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
250*e8d8bef9SDimitry Andric             auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
2518bcb0991SDimitry Andric             if (GV && GV->hasInitializer()) {
2528bcb0991SDimitry Andric               Constant *Init = GV->getInitializer();
253*e8d8bef9SDimitry Andric               bool IsZeroValue = Init->isZeroValue();
254*e8d8bef9SDimitry Andric               auto *CA = dyn_cast<ConstantDataArray>(Init);
255*e8d8bef9SDimitry Andric               if (IsZeroValue || (CA && CA->isString())) {
256*e8d8bef9SDimitry Andric                 size_t SizeStr =
257*e8d8bef9SDimitry 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};
3268bcb0991SDimitry Andric       Type *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 1);
3278bcb0991SDimitry Andric       FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false);
3288bcb0991SDimitry Andric       FunctionCallee PrintfAllocFn =
3298bcb0991SDimitry Andric           M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr);
3308bcb0991SDimitry Andric 
3318bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n');
3328bcb0991SDimitry Andric       std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str().c_str();
3338bcb0991SDimitry Andric       MDString *fmtStrArray = MDString::get(Ctx, fmtstr);
3348bcb0991SDimitry Andric 
3358bcb0991SDimitry Andric       // Instead of creating global variables, the
3368bcb0991SDimitry Andric       // printf format strings are extracted
3378bcb0991SDimitry Andric       // and passed as metadata. This avoids
3388bcb0991SDimitry Andric       // polluting llvm's symbol tables in this module.
3398bcb0991SDimitry Andric       // Metadata is going to be extracted
3408bcb0991SDimitry Andric       // by the backend passes and inserted
3418bcb0991SDimitry Andric       // into the OpenCL binary as appropriate.
3428bcb0991SDimitry Andric       StringRef amd("llvm.printf.fmts");
3438bcb0991SDimitry Andric       NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd);
3448bcb0991SDimitry Andric       MDNode *myMD = MDNode::get(Ctx, fmtStrArray);
3458bcb0991SDimitry Andric       metaD->addOperand(myMD);
3468bcb0991SDimitry Andric       Value *sumC = ConstantInt::get(SizetTy, Sum, false);
3478bcb0991SDimitry Andric       SmallVector<Value *, 1> alloc_args;
3488bcb0991SDimitry Andric       alloc_args.push_back(sumC);
3498bcb0991SDimitry Andric       CallInst *pcall =
3508bcb0991SDimitry Andric           CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI);
3518bcb0991SDimitry Andric 
3528bcb0991SDimitry Andric       //
3538bcb0991SDimitry Andric       // Insert code to split basicblock with a
3548bcb0991SDimitry Andric       // piece of hammock code.
3558bcb0991SDimitry Andric       // basicblock splits after buffer overflow check
3568bcb0991SDimitry Andric       //
3578bcb0991SDimitry Andric       ConstantPointerNull *zeroIntPtr =
3588bcb0991SDimitry Andric           ConstantPointerNull::get(PointerType::get(Type::getInt8Ty(Ctx), 1));
3598bcb0991SDimitry Andric       ICmpInst *cmp =
3608bcb0991SDimitry Andric           dyn_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       //
3748bcb0991SDimitry Andric       SmallVector<Value *, 1> ZeroIdxList;
3758bcb0991SDimitry Andric       ConstantInt *zeroInt =
3768bcb0991SDimitry Andric           ConstantInt::get(Ctx, APInt(32, StringRef("0"), 10));
3778bcb0991SDimitry Andric       ZeroIdxList.push_back(zeroInt);
3788bcb0991SDimitry Andric 
379*e8d8bef9SDimitry Andric       GetElementPtrInst *BufferIdx = GetElementPtrInst::Create(
380*e8d8bef9SDimitry Andric           nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch);
3818bcb0991SDimitry Andric 
3828bcb0991SDimitry Andric       Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
3838bcb0991SDimitry Andric       Value *id_gep_cast =
3848bcb0991SDimitry Andric           new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch);
3858bcb0991SDimitry Andric 
3865ffd83dbSDimitry Andric       new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast, Brnch);
3878bcb0991SDimitry Andric 
3888bcb0991SDimitry Andric       SmallVector<Value *, 2> FourthIdxList;
3898bcb0991SDimitry Andric       ConstantInt *fourInt =
3908bcb0991SDimitry Andric           ConstantInt::get(Ctx, APInt(32, StringRef("4"), 10));
3918bcb0991SDimitry Andric 
3928bcb0991SDimitry Andric       FourthIdxList.push_back(fourInt); // 1st 4 bytes hold the printf_id
3938bcb0991SDimitry Andric       // the following GEP is the buffer pointer
394*e8d8bef9SDimitry Andric       BufferIdx = GetElementPtrInst::Create(nullptr, pcall, FourthIdxList,
395*e8d8bef9SDimitry Andric                                             "PrintBuffGep", Brnch);
3968bcb0991SDimitry Andric 
3978bcb0991SDimitry Andric       Type *Int32Ty = Type::getInt32Ty(Ctx);
3988bcb0991SDimitry Andric       Type *Int64Ty = Type::getInt64Ty(Ctx);
3998bcb0991SDimitry Andric       for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
4008bcb0991SDimitry Andric                                   ArgCount <= OpConvSpecifiers.size();
4018bcb0991SDimitry Andric            ArgCount++) {
4028bcb0991SDimitry Andric         Value *Arg = CI->getArgOperand(ArgCount);
4038bcb0991SDimitry Andric         Type *ArgType = Arg->getType();
4048bcb0991SDimitry Andric         SmallVector<Value *, 32> WhatToStore;
4055ffd83dbSDimitry Andric         if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) {
4068bcb0991SDimitry Andric           Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
4078bcb0991SDimitry Andric           if (OpConvSpecifiers[ArgCount - 1] == 'f') {
408*e8d8bef9SDimitry Andric             if (auto *FpCons = dyn_cast<ConstantFP>(Arg)) {
409*e8d8bef9SDimitry Andric               APFloat Val(FpCons->getValueAPF());
4108bcb0991SDimitry Andric               bool Lost = false;
4118bcb0991SDimitry Andric               Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
4128bcb0991SDimitry Andric                           &Lost);
4138bcb0991SDimitry Andric               Arg = ConstantFP::get(Ctx, Val);
4148bcb0991SDimitry Andric               IType = Int32Ty;
415*e8d8bef9SDimitry Andric             } else if (auto *FpExt = dyn_cast<FPExtInst>(Arg)) {
416*e8d8bef9SDimitry Andric               if (FpExt->getType()->isDoubleTy() &&
4178bcb0991SDimitry Andric                   FpExt->getOperand(0)->getType()->isFloatTy()) {
4188bcb0991SDimitry Andric                 Arg = FpExt->getOperand(0);
4198bcb0991SDimitry Andric                 IType = Int32Ty;
4208bcb0991SDimitry Andric               }
4218bcb0991SDimitry Andric             }
4228bcb0991SDimitry Andric           }
4238bcb0991SDimitry Andric           Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch);
4248bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
4258bcb0991SDimitry Andric         } else if (ArgType->getTypeID() == Type::PointerTyID) {
4268bcb0991SDimitry Andric           if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
4278bcb0991SDimitry Andric             const char *S = NonLiteralStr;
428*e8d8bef9SDimitry Andric             if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
429*e8d8bef9SDimitry Andric               auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
4308bcb0991SDimitry Andric               if (GV && GV->hasInitializer()) {
4318bcb0991SDimitry Andric                 Constant *Init = GV->getInitializer();
432*e8d8bef9SDimitry Andric                 bool IsZeroValue = Init->isZeroValue();
433*e8d8bef9SDimitry Andric                 auto *CA = dyn_cast<ConstantDataArray>(Init);
434*e8d8bef9SDimitry Andric                 if (IsZeroValue || (CA && CA->isString())) {
435*e8d8bef9SDimitry Andric                   S = IsZeroValue ? "" : CA->getAsCString().data();
4368bcb0991SDimitry Andric                 }
4378bcb0991SDimitry Andric               }
4388bcb0991SDimitry Andric             }
4398bcb0991SDimitry Andric             size_t SizeStr = strlen(S) + 1;
4408bcb0991SDimitry Andric             size_t Rem = SizeStr % DWORD_ALIGN;
4418bcb0991SDimitry Andric             size_t NSizeStr = 0;
4428bcb0991SDimitry Andric             if (Rem) {
4438bcb0991SDimitry Andric               NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
4448bcb0991SDimitry Andric             } else {
4458bcb0991SDimitry Andric               NSizeStr = SizeStr;
4468bcb0991SDimitry Andric             }
4478bcb0991SDimitry Andric             if (S[0]) {
4488bcb0991SDimitry Andric               char *MyNewStr = new char[NSizeStr]();
4498bcb0991SDimitry Andric               strcpy(MyNewStr, S);
4508bcb0991SDimitry Andric               int NumInts = NSizeStr / 4;
4518bcb0991SDimitry Andric               int CharC = 0;
4528bcb0991SDimitry Andric               while (NumInts) {
4538bcb0991SDimitry Andric                 int ANum = *(int *)(MyNewStr + CharC);
4548bcb0991SDimitry Andric                 CharC += 4;
4558bcb0991SDimitry Andric                 NumInts--;
4568bcb0991SDimitry Andric                 Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
4578bcb0991SDimitry Andric                 WhatToStore.push_back(ANumV);
4588bcb0991SDimitry Andric               }
4598bcb0991SDimitry Andric               delete[] MyNewStr;
4608bcb0991SDimitry Andric             } else {
4618bcb0991SDimitry Andric               // Empty string, give a hint to RT it is no NULL
4628bcb0991SDimitry Andric               Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);
4638bcb0991SDimitry Andric               WhatToStore.push_back(ANumV);
4648bcb0991SDimitry Andric             }
4658bcb0991SDimitry Andric           } else {
4668bcb0991SDimitry Andric             uint64_t Size = TD->getTypeAllocSizeInBits(ArgType);
4678bcb0991SDimitry Andric             assert((Size == 32 || Size == 64) && "unsupported size");
4688bcb0991SDimitry Andric             Type *DstType = (Size == 32) ? Int32Ty : Int64Ty;
4698bcb0991SDimitry Andric             Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch);
4708bcb0991SDimitry Andric             WhatToStore.push_back(Arg);
4718bcb0991SDimitry Andric           }
4725ffd83dbSDimitry Andric         } else if (isa<FixedVectorType>(ArgType)) {
4738bcb0991SDimitry Andric           Type *IType = NULL;
4745ffd83dbSDimitry Andric           uint32_t EleCount = cast<FixedVectorType>(ArgType)->getNumElements();
4758bcb0991SDimitry Andric           uint32_t EleSize = ArgType->getScalarSizeInBits();
4768bcb0991SDimitry Andric           uint32_t TotalSize = EleCount * EleSize;
4778bcb0991SDimitry Andric           if (EleCount == 3) {
4785ffd83dbSDimitry Andric             ShuffleVectorInst *Shuffle =
4795ffd83dbSDimitry Andric                 new ShuffleVectorInst(Arg, Arg, ArrayRef<int>{0, 1, 2, 2});
4808bcb0991SDimitry Andric             Shuffle->insertBefore(Brnch);
4818bcb0991SDimitry Andric             Arg = Shuffle;
4828bcb0991SDimitry Andric             ArgType = Arg->getType();
4838bcb0991SDimitry Andric             TotalSize += EleSize;
4848bcb0991SDimitry Andric           }
4858bcb0991SDimitry Andric           switch (EleSize) {
4868bcb0991SDimitry Andric           default:
4878bcb0991SDimitry Andric             EleCount = TotalSize / 64;
488*e8d8bef9SDimitry Andric             IType = Type::getInt64Ty(ArgType->getContext());
4898bcb0991SDimitry Andric             break;
4908bcb0991SDimitry Andric           case 8:
4918bcb0991SDimitry Andric             if (EleCount >= 8) {
4928bcb0991SDimitry Andric               EleCount = TotalSize / 64;
493*e8d8bef9SDimitry Andric               IType = Type::getInt64Ty(ArgType->getContext());
4948bcb0991SDimitry Andric             } else if (EleCount >= 3) {
4958bcb0991SDimitry Andric               EleCount = 1;
496*e8d8bef9SDimitry Andric               IType = Type::getInt32Ty(ArgType->getContext());
4978bcb0991SDimitry Andric             } else {
4988bcb0991SDimitry Andric               EleCount = 1;
499*e8d8bef9SDimitry Andric               IType = Type::getInt16Ty(ArgType->getContext());
5008bcb0991SDimitry Andric             }
5018bcb0991SDimitry Andric             break;
5028bcb0991SDimitry Andric           case 16:
5038bcb0991SDimitry Andric             if (EleCount >= 3) {
5048bcb0991SDimitry Andric               EleCount = TotalSize / 64;
505*e8d8bef9SDimitry Andric               IType = Type::getInt64Ty(ArgType->getContext());
5068bcb0991SDimitry Andric             } else {
5078bcb0991SDimitry Andric               EleCount = 1;
508*e8d8bef9SDimitry Andric               IType = Type::getInt32Ty(ArgType->getContext());
5098bcb0991SDimitry Andric             }
5108bcb0991SDimitry Andric             break;
5118bcb0991SDimitry Andric           }
5128bcb0991SDimitry Andric           if (EleCount > 1) {
5135ffd83dbSDimitry Andric             IType = FixedVectorType::get(IType, EleCount);
5148bcb0991SDimitry Andric           }
5158bcb0991SDimitry Andric           Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch);
5168bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
5178bcb0991SDimitry Andric         } else {
5188bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
5198bcb0991SDimitry Andric         }
5208bcb0991SDimitry Andric         for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) {
5218bcb0991SDimitry Andric           Value *TheBtCast = WhatToStore[I];
5228bcb0991SDimitry Andric           unsigned ArgSize =
5238bcb0991SDimitry Andric               TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8;
5248bcb0991SDimitry Andric           SmallVector<Value *, 1> BuffOffset;
5258bcb0991SDimitry Andric           BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize));
5268bcb0991SDimitry Andric 
5278bcb0991SDimitry Andric           Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1);
5288bcb0991SDimitry Andric           Value *CastedGEP =
5298bcb0991SDimitry Andric               new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch);
5308bcb0991SDimitry Andric           StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch);
5318bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n"
5328bcb0991SDimitry Andric                             << *StBuff << '\n');
5338bcb0991SDimitry Andric           (void)StBuff;
5348bcb0991SDimitry Andric           if (I + 1 == E && ArgCount + 1 == CI->getNumArgOperands())
5358bcb0991SDimitry Andric             break;
536*e8d8bef9SDimitry Andric           BufferIdx = GetElementPtrInst::Create(nullptr, BufferIdx, BuffOffset,
537*e8d8bef9SDimitry Andric                                                 "PrintBuffNextPtr", Brnch);
5388bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
5398bcb0991SDimitry Andric                             << *BufferIdx << '\n');
5408bcb0991SDimitry Andric         }
5418bcb0991SDimitry Andric       }
5428bcb0991SDimitry Andric     }
5438bcb0991SDimitry Andric   }
5448bcb0991SDimitry Andric 
5458bcb0991SDimitry Andric   // erase the printf calls
5468bcb0991SDimitry Andric   for (auto CI : Printfs)
5478bcb0991SDimitry Andric     CI->eraseFromParent();
5488bcb0991SDimitry Andric 
5498bcb0991SDimitry Andric   Printfs.clear();
5508bcb0991SDimitry Andric   return true;
5518bcb0991SDimitry Andric }
5528bcb0991SDimitry Andric 
553*e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBindingImpl::run(Module &M) {
5548bcb0991SDimitry Andric   Triple TT(M.getTargetTriple());
5558bcb0991SDimitry Andric   if (TT.getArch() == Triple::r600)
5568bcb0991SDimitry Andric     return false;
5578bcb0991SDimitry Andric 
5588bcb0991SDimitry Andric   auto PrintfFunction = M.getFunction("printf");
5598bcb0991SDimitry Andric   if (!PrintfFunction)
5608bcb0991SDimitry Andric     return false;
5618bcb0991SDimitry Andric 
5628bcb0991SDimitry Andric   for (auto &U : PrintfFunction->uses()) {
5638bcb0991SDimitry Andric     if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
5648bcb0991SDimitry Andric       if (CI->isCallee(&U))
5658bcb0991SDimitry Andric         Printfs.push_back(CI);
5668bcb0991SDimitry Andric     }
5678bcb0991SDimitry Andric   }
5688bcb0991SDimitry Andric 
5698bcb0991SDimitry Andric   if (Printfs.empty())
5708bcb0991SDimitry Andric     return false;
5718bcb0991SDimitry Andric 
572480093f4SDimitry Andric   if (auto HostcallFunction = M.getFunction("__ockl_hostcall_internal")) {
573480093f4SDimitry Andric     for (auto &U : HostcallFunction->uses()) {
574480093f4SDimitry Andric       if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
575480093f4SDimitry Andric         M.getContext().emitError(
576480093f4SDimitry Andric             CI, "Cannot use both printf and hostcall in the same module");
577480093f4SDimitry Andric       }
578480093f4SDimitry Andric     }
579480093f4SDimitry Andric   }
580480093f4SDimitry Andric 
5818bcb0991SDimitry Andric   TD = &M.getDataLayout();
582*e8d8bef9SDimitry Andric 
583*e8d8bef9SDimitry Andric   return lowerPrintfForGpu(M);
584*e8d8bef9SDimitry Andric }
585*e8d8bef9SDimitry Andric 
586*e8d8bef9SDimitry Andric bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
587*e8d8bef9SDimitry Andric   auto GetDT = [this](Function &F) -> DominatorTree & {
588*e8d8bef9SDimitry Andric     return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
589*e8d8bef9SDimitry Andric   };
5908bcb0991SDimitry Andric   auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
5918bcb0991SDimitry Andric     return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
5928bcb0991SDimitry Andric   };
5938bcb0991SDimitry Andric 
594*e8d8bef9SDimitry Andric   return AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M);
595*e8d8bef9SDimitry Andric }
596*e8d8bef9SDimitry Andric 
597*e8d8bef9SDimitry Andric PreservedAnalyses
598*e8d8bef9SDimitry Andric AMDGPUPrintfRuntimeBindingPass::run(Module &M, ModuleAnalysisManager &AM) {
599*e8d8bef9SDimitry Andric   FunctionAnalysisManager &FAM =
600*e8d8bef9SDimitry Andric       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
601*e8d8bef9SDimitry Andric   auto GetDT = [&FAM](Function &F) -> DominatorTree & {
602*e8d8bef9SDimitry Andric     return FAM.getResult<DominatorTreeAnalysis>(F);
603*e8d8bef9SDimitry Andric   };
604*e8d8bef9SDimitry Andric   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
605*e8d8bef9SDimitry Andric     return FAM.getResult<TargetLibraryAnalysis>(F);
606*e8d8bef9SDimitry Andric   };
607*e8d8bef9SDimitry Andric   bool Changed = AMDGPUPrintfRuntimeBindingImpl(GetDT, GetTLI).run(M);
608*e8d8bef9SDimitry Andric   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
6098bcb0991SDimitry Andric }
610