xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1*8bcb0991SDimitry Andric //=== AMDGPUPrintfRuntimeBinding.cpp - OpenCL printf implementation -------===//
2*8bcb0991SDimitry Andric //
3*8bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*8bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8bcb0991SDimitry Andric //
7*8bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
8*8bcb0991SDimitry Andric // \file
9*8bcb0991SDimitry Andric //
10*8bcb0991SDimitry Andric // The pass bind printfs to a kernel arg pointer that will be bound to a buffer
11*8bcb0991SDimitry Andric // later by the runtime.
12*8bcb0991SDimitry Andric //
13*8bcb0991SDimitry Andric // This pass traverses the functions in the module and converts
14*8bcb0991SDimitry Andric // each call to printf to a sequence of operations that
15*8bcb0991SDimitry Andric // store the following into the printf buffer:
16*8bcb0991SDimitry Andric // - format string (passed as a module's metadata unique ID)
17*8bcb0991SDimitry Andric // - bitwise copies of printf arguments
18*8bcb0991SDimitry Andric // The backend passes will need to store metadata in the kernel
19*8bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
20*8bcb0991SDimitry Andric 
21*8bcb0991SDimitry Andric #include "AMDGPU.h"
22*8bcb0991SDimitry Andric #include "llvm/ADT/SmallString.h"
23*8bcb0991SDimitry Andric #include "llvm/ADT/StringExtras.h"
24*8bcb0991SDimitry Andric #include "llvm/ADT/Triple.h"
25*8bcb0991SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
26*8bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
27*8bcb0991SDimitry Andric #include "llvm/CodeGen/Passes.h"
28*8bcb0991SDimitry Andric #include "llvm/IR/Constants.h"
29*8bcb0991SDimitry Andric #include "llvm/IR/DataLayout.h"
30*8bcb0991SDimitry Andric #include "llvm/IR/Dominators.h"
31*8bcb0991SDimitry Andric #include "llvm/IR/GlobalVariable.h"
32*8bcb0991SDimitry Andric #include "llvm/IR/IRBuilder.h"
33*8bcb0991SDimitry Andric #include "llvm/IR/Instructions.h"
34*8bcb0991SDimitry Andric #include "llvm/IR/Module.h"
35*8bcb0991SDimitry Andric #include "llvm/IR/Type.h"
36*8bcb0991SDimitry Andric #include "llvm/Support/CommandLine.h"
37*8bcb0991SDimitry Andric #include "llvm/Support/Debug.h"
38*8bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h"
39*8bcb0991SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40*8bcb0991SDimitry Andric using namespace llvm;
41*8bcb0991SDimitry Andric 
42*8bcb0991SDimitry Andric #define DEBUG_TYPE "printfToRuntime"
43*8bcb0991SDimitry Andric #define DWORD_ALIGN 4
44*8bcb0991SDimitry Andric 
45*8bcb0991SDimitry Andric namespace {
46*8bcb0991SDimitry Andric class LLVM_LIBRARY_VISIBILITY AMDGPUPrintfRuntimeBinding final
47*8bcb0991SDimitry Andric     : public ModulePass {
48*8bcb0991SDimitry Andric 
49*8bcb0991SDimitry Andric public:
50*8bcb0991SDimitry Andric   static char ID;
51*8bcb0991SDimitry Andric 
52*8bcb0991SDimitry Andric   explicit AMDGPUPrintfRuntimeBinding();
53*8bcb0991SDimitry Andric 
54*8bcb0991SDimitry Andric private:
55*8bcb0991SDimitry Andric   bool runOnModule(Module &M) override;
56*8bcb0991SDimitry Andric   void getConversionSpecifiers(SmallVectorImpl<char> &OpConvSpecifiers,
57*8bcb0991SDimitry Andric                                StringRef fmt, size_t num_ops) const;
58*8bcb0991SDimitry Andric 
59*8bcb0991SDimitry Andric   bool shouldPrintAsStr(char Specifier, Type *OpType) const;
60*8bcb0991SDimitry Andric   bool
61*8bcb0991SDimitry Andric   lowerPrintfForGpu(Module &M,
62*8bcb0991SDimitry Andric                     function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
63*8bcb0991SDimitry Andric 
64*8bcb0991SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
65*8bcb0991SDimitry Andric     AU.addRequired<TargetLibraryInfoWrapperPass>();
66*8bcb0991SDimitry Andric     AU.addRequired<DominatorTreeWrapperPass>();
67*8bcb0991SDimitry Andric   }
68*8bcb0991SDimitry Andric 
69*8bcb0991SDimitry Andric   Value *simplify(Instruction *I, const TargetLibraryInfo *TLI) {
70*8bcb0991SDimitry Andric     return SimplifyInstruction(I, {*TD, TLI, DT});
71*8bcb0991SDimitry Andric   }
72*8bcb0991SDimitry Andric 
73*8bcb0991SDimitry Andric   const DataLayout *TD;
74*8bcb0991SDimitry Andric   const DominatorTree *DT;
75*8bcb0991SDimitry Andric   SmallVector<CallInst *, 32> Printfs;
76*8bcb0991SDimitry Andric };
77*8bcb0991SDimitry Andric } // namespace
78*8bcb0991SDimitry Andric 
79*8bcb0991SDimitry Andric char AMDGPUPrintfRuntimeBinding::ID = 0;
80*8bcb0991SDimitry Andric 
81*8bcb0991SDimitry Andric INITIALIZE_PASS_BEGIN(AMDGPUPrintfRuntimeBinding,
82*8bcb0991SDimitry Andric                       "amdgpu-printf-runtime-binding", "AMDGPU Printf lowering",
83*8bcb0991SDimitry Andric                       false, false)
84*8bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
85*8bcb0991SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
86*8bcb0991SDimitry Andric INITIALIZE_PASS_END(AMDGPUPrintfRuntimeBinding, "amdgpu-printf-runtime-binding",
87*8bcb0991SDimitry Andric                     "AMDGPU Printf lowering", false, false)
88*8bcb0991SDimitry Andric 
89*8bcb0991SDimitry Andric char &llvm::AMDGPUPrintfRuntimeBindingID = AMDGPUPrintfRuntimeBinding::ID;
90*8bcb0991SDimitry Andric 
91*8bcb0991SDimitry Andric namespace llvm {
92*8bcb0991SDimitry Andric ModulePass *createAMDGPUPrintfRuntimeBinding() {
93*8bcb0991SDimitry Andric   return new AMDGPUPrintfRuntimeBinding();
94*8bcb0991SDimitry Andric }
95*8bcb0991SDimitry Andric } // namespace llvm
96*8bcb0991SDimitry Andric 
97*8bcb0991SDimitry Andric AMDGPUPrintfRuntimeBinding::AMDGPUPrintfRuntimeBinding()
98*8bcb0991SDimitry Andric     : ModulePass(ID), TD(nullptr), DT(nullptr) {
99*8bcb0991SDimitry Andric   initializeAMDGPUPrintfRuntimeBindingPass(*PassRegistry::getPassRegistry());
100*8bcb0991SDimitry Andric }
101*8bcb0991SDimitry Andric 
102*8bcb0991SDimitry Andric void AMDGPUPrintfRuntimeBinding::getConversionSpecifiers(
103*8bcb0991SDimitry Andric     SmallVectorImpl<char> &OpConvSpecifiers, StringRef Fmt,
104*8bcb0991SDimitry Andric     size_t NumOps) const {
105*8bcb0991SDimitry Andric   // not all format characters are collected.
106*8bcb0991SDimitry Andric   // At this time the format characters of interest
107*8bcb0991SDimitry Andric   // are %p and %s, which use to know if we
108*8bcb0991SDimitry Andric   // are either storing a literal string or a
109*8bcb0991SDimitry Andric   // pointer to the printf buffer.
110*8bcb0991SDimitry Andric   static const char ConvSpecifiers[] = "cdieEfgGaosuxXp";
111*8bcb0991SDimitry Andric   size_t CurFmtSpecifierIdx = 0;
112*8bcb0991SDimitry Andric   size_t PrevFmtSpecifierIdx = 0;
113*8bcb0991SDimitry Andric 
114*8bcb0991SDimitry Andric   while ((CurFmtSpecifierIdx = Fmt.find_first_of(
115*8bcb0991SDimitry Andric               ConvSpecifiers, CurFmtSpecifierIdx)) != StringRef::npos) {
116*8bcb0991SDimitry Andric     bool ArgDump = false;
117*8bcb0991SDimitry Andric     StringRef CurFmt = Fmt.substr(PrevFmtSpecifierIdx,
118*8bcb0991SDimitry Andric                                   CurFmtSpecifierIdx - PrevFmtSpecifierIdx);
119*8bcb0991SDimitry Andric     size_t pTag = CurFmt.find_last_of("%");
120*8bcb0991SDimitry Andric     if (pTag != StringRef::npos) {
121*8bcb0991SDimitry Andric       ArgDump = true;
122*8bcb0991SDimitry Andric       while (pTag && CurFmt[--pTag] == '%') {
123*8bcb0991SDimitry Andric         ArgDump = !ArgDump;
124*8bcb0991SDimitry Andric       }
125*8bcb0991SDimitry Andric     }
126*8bcb0991SDimitry Andric 
127*8bcb0991SDimitry Andric     if (ArgDump)
128*8bcb0991SDimitry Andric       OpConvSpecifiers.push_back(Fmt[CurFmtSpecifierIdx]);
129*8bcb0991SDimitry Andric 
130*8bcb0991SDimitry Andric     PrevFmtSpecifierIdx = ++CurFmtSpecifierIdx;
131*8bcb0991SDimitry Andric   }
132*8bcb0991SDimitry Andric }
133*8bcb0991SDimitry Andric 
134*8bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::shouldPrintAsStr(char Specifier,
135*8bcb0991SDimitry Andric                                                   Type *OpType) const {
136*8bcb0991SDimitry Andric   if (Specifier != 's')
137*8bcb0991SDimitry Andric     return false;
138*8bcb0991SDimitry Andric   const PointerType *PT = dyn_cast<PointerType>(OpType);
139*8bcb0991SDimitry Andric   if (!PT || PT->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
140*8bcb0991SDimitry Andric     return false;
141*8bcb0991SDimitry Andric   Type *ElemType = PT->getContainedType(0);
142*8bcb0991SDimitry Andric   if (ElemType->getTypeID() != Type::IntegerTyID)
143*8bcb0991SDimitry Andric     return false;
144*8bcb0991SDimitry Andric   IntegerType *ElemIType = cast<IntegerType>(ElemType);
145*8bcb0991SDimitry Andric   return ElemIType->getBitWidth() == 8;
146*8bcb0991SDimitry Andric }
147*8bcb0991SDimitry Andric 
148*8bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::lowerPrintfForGpu(
149*8bcb0991SDimitry Andric     Module &M, function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
150*8bcb0991SDimitry Andric   LLVMContext &Ctx = M.getContext();
151*8bcb0991SDimitry Andric   IRBuilder<> Builder(Ctx);
152*8bcb0991SDimitry Andric   Type *I32Ty = Type::getInt32Ty(Ctx);
153*8bcb0991SDimitry Andric   unsigned UniqID = 0;
154*8bcb0991SDimitry Andric   // NB: This is important for this string size to be divizable by 4
155*8bcb0991SDimitry Andric   const char NonLiteralStr[4] = "???";
156*8bcb0991SDimitry Andric 
157*8bcb0991SDimitry Andric   for (auto CI : Printfs) {
158*8bcb0991SDimitry Andric     unsigned NumOps = CI->getNumArgOperands();
159*8bcb0991SDimitry Andric 
160*8bcb0991SDimitry Andric     SmallString<16> OpConvSpecifiers;
161*8bcb0991SDimitry Andric     Value *Op = CI->getArgOperand(0);
162*8bcb0991SDimitry Andric 
163*8bcb0991SDimitry Andric     if (auto LI = dyn_cast<LoadInst>(Op)) {
164*8bcb0991SDimitry Andric       Op = LI->getPointerOperand();
165*8bcb0991SDimitry Andric       for (auto Use : Op->users()) {
166*8bcb0991SDimitry Andric         if (auto SI = dyn_cast<StoreInst>(Use)) {
167*8bcb0991SDimitry Andric           Op = SI->getValueOperand();
168*8bcb0991SDimitry Andric           break;
169*8bcb0991SDimitry Andric         }
170*8bcb0991SDimitry Andric       }
171*8bcb0991SDimitry Andric     }
172*8bcb0991SDimitry Andric 
173*8bcb0991SDimitry Andric     if (auto I = dyn_cast<Instruction>(Op)) {
174*8bcb0991SDimitry Andric       Value *Op_simplified = simplify(I, &GetTLI(*I->getFunction()));
175*8bcb0991SDimitry Andric       if (Op_simplified)
176*8bcb0991SDimitry Andric         Op = Op_simplified;
177*8bcb0991SDimitry Andric     }
178*8bcb0991SDimitry Andric 
179*8bcb0991SDimitry Andric     ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Op);
180*8bcb0991SDimitry Andric 
181*8bcb0991SDimitry Andric     if (ConstExpr) {
182*8bcb0991SDimitry Andric       GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
183*8bcb0991SDimitry Andric 
184*8bcb0991SDimitry Andric       StringRef Str("unknown");
185*8bcb0991SDimitry Andric       if (GVar && GVar->hasInitializer()) {
186*8bcb0991SDimitry Andric         auto Init = GVar->getInitializer();
187*8bcb0991SDimitry Andric         if (auto CA = dyn_cast<ConstantDataArray>(Init)) {
188*8bcb0991SDimitry Andric           if (CA->isString())
189*8bcb0991SDimitry Andric             Str = CA->getAsCString();
190*8bcb0991SDimitry Andric         } else if (isa<ConstantAggregateZero>(Init)) {
191*8bcb0991SDimitry Andric           Str = "";
192*8bcb0991SDimitry Andric         }
193*8bcb0991SDimitry Andric         //
194*8bcb0991SDimitry Andric         // we need this call to ascertain
195*8bcb0991SDimitry Andric         // that we are printing a string
196*8bcb0991SDimitry Andric         // or a pointer. It takes out the
197*8bcb0991SDimitry Andric         // specifiers and fills up the first
198*8bcb0991SDimitry Andric         // arg
199*8bcb0991SDimitry Andric         getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1);
200*8bcb0991SDimitry Andric       }
201*8bcb0991SDimitry Andric       // Add metadata for the string
202*8bcb0991SDimitry Andric       std::string AStreamHolder;
203*8bcb0991SDimitry Andric       raw_string_ostream Sizes(AStreamHolder);
204*8bcb0991SDimitry Andric       int Sum = DWORD_ALIGN;
205*8bcb0991SDimitry Andric       Sizes << CI->getNumArgOperands() - 1;
206*8bcb0991SDimitry Andric       Sizes << ':';
207*8bcb0991SDimitry Andric       for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
208*8bcb0991SDimitry Andric                                   ArgCount <= OpConvSpecifiers.size();
209*8bcb0991SDimitry Andric            ArgCount++) {
210*8bcb0991SDimitry Andric         Value *Arg = CI->getArgOperand(ArgCount);
211*8bcb0991SDimitry Andric         Type *ArgType = Arg->getType();
212*8bcb0991SDimitry Andric         unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType);
213*8bcb0991SDimitry Andric         ArgSize = ArgSize / 8;
214*8bcb0991SDimitry Andric         //
215*8bcb0991SDimitry Andric         // ArgSize by design should be a multiple of DWORD_ALIGN,
216*8bcb0991SDimitry Andric         // expand the arguments that do not follow this rule.
217*8bcb0991SDimitry Andric         //
218*8bcb0991SDimitry Andric         if (ArgSize % DWORD_ALIGN != 0) {
219*8bcb0991SDimitry Andric           llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx);
220*8bcb0991SDimitry Andric           VectorType *LLVMVecType = llvm::dyn_cast<llvm::VectorType>(ArgType);
221*8bcb0991SDimitry Andric           int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1;
222*8bcb0991SDimitry Andric           if (LLVMVecType && NumElem > 1)
223*8bcb0991SDimitry Andric             ResType = llvm::VectorType::get(ResType, NumElem);
224*8bcb0991SDimitry Andric           Builder.SetInsertPoint(CI);
225*8bcb0991SDimitry Andric           Builder.SetCurrentDebugLocation(CI->getDebugLoc());
226*8bcb0991SDimitry Andric           if (OpConvSpecifiers[ArgCount - 1] == 'x' ||
227*8bcb0991SDimitry Andric               OpConvSpecifiers[ArgCount - 1] == 'X' ||
228*8bcb0991SDimitry Andric               OpConvSpecifiers[ArgCount - 1] == 'u' ||
229*8bcb0991SDimitry Andric               OpConvSpecifiers[ArgCount - 1] == 'o')
230*8bcb0991SDimitry Andric             Arg = Builder.CreateZExt(Arg, ResType);
231*8bcb0991SDimitry Andric           else
232*8bcb0991SDimitry Andric             Arg = Builder.CreateSExt(Arg, ResType);
233*8bcb0991SDimitry Andric           ArgType = Arg->getType();
234*8bcb0991SDimitry Andric           ArgSize = TD->getTypeAllocSizeInBits(ArgType);
235*8bcb0991SDimitry Andric           ArgSize = ArgSize / 8;
236*8bcb0991SDimitry Andric           CI->setOperand(ArgCount, Arg);
237*8bcb0991SDimitry Andric         }
238*8bcb0991SDimitry Andric         if (OpConvSpecifiers[ArgCount - 1] == 'f') {
239*8bcb0991SDimitry Andric           ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg);
240*8bcb0991SDimitry Andric           if (FpCons)
241*8bcb0991SDimitry Andric             ArgSize = 4;
242*8bcb0991SDimitry Andric           else {
243*8bcb0991SDimitry Andric             FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
244*8bcb0991SDimitry Andric             if (FpExt && FpExt->getType()->isDoubleTy() &&
245*8bcb0991SDimitry Andric                 FpExt->getOperand(0)->getType()->isFloatTy())
246*8bcb0991SDimitry Andric               ArgSize = 4;
247*8bcb0991SDimitry Andric           }
248*8bcb0991SDimitry Andric         }
249*8bcb0991SDimitry Andric         if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
250*8bcb0991SDimitry Andric           if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
251*8bcb0991SDimitry Andric             GlobalVariable *GV =
252*8bcb0991SDimitry Andric                 dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
253*8bcb0991SDimitry Andric             if (GV && GV->hasInitializer()) {
254*8bcb0991SDimitry Andric               Constant *Init = GV->getInitializer();
255*8bcb0991SDimitry Andric               ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init);
256*8bcb0991SDimitry Andric               if (Init->isZeroValue() || CA->isString()) {
257*8bcb0991SDimitry Andric                 size_t SizeStr = Init->isZeroValue()
258*8bcb0991SDimitry Andric                                      ? 1
259*8bcb0991SDimitry Andric                                      : (strlen(CA->getAsCString().data()) + 1);
260*8bcb0991SDimitry Andric                 size_t Rem = SizeStr % DWORD_ALIGN;
261*8bcb0991SDimitry Andric                 size_t NSizeStr = 0;
262*8bcb0991SDimitry Andric                 LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr
263*8bcb0991SDimitry Andric                                   << '\n');
264*8bcb0991SDimitry Andric                 if (Rem) {
265*8bcb0991SDimitry Andric                   NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
266*8bcb0991SDimitry Andric                 } else {
267*8bcb0991SDimitry Andric                   NSizeStr = SizeStr;
268*8bcb0991SDimitry Andric                 }
269*8bcb0991SDimitry Andric                 ArgSize = NSizeStr;
270*8bcb0991SDimitry Andric               }
271*8bcb0991SDimitry Andric             } else {
272*8bcb0991SDimitry Andric               ArgSize = sizeof(NonLiteralStr);
273*8bcb0991SDimitry Andric             }
274*8bcb0991SDimitry Andric           } else {
275*8bcb0991SDimitry Andric             ArgSize = sizeof(NonLiteralStr);
276*8bcb0991SDimitry Andric           }
277*8bcb0991SDimitry Andric         }
278*8bcb0991SDimitry Andric         LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize
279*8bcb0991SDimitry Andric                           << " for type: " << *ArgType << '\n');
280*8bcb0991SDimitry Andric         Sizes << ArgSize << ':';
281*8bcb0991SDimitry Andric         Sum += ArgSize;
282*8bcb0991SDimitry Andric       }
283*8bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str()
284*8bcb0991SDimitry Andric                         << '\n');
285*8bcb0991SDimitry Andric       for (size_t I = 0; I < Str.size(); ++I) {
286*8bcb0991SDimitry Andric         // Rest of the C escape sequences (e.g. \') are handled correctly
287*8bcb0991SDimitry Andric         // by the MDParser
288*8bcb0991SDimitry Andric         switch (Str[I]) {
289*8bcb0991SDimitry Andric         case '\a':
290*8bcb0991SDimitry Andric           Sizes << "\\a";
291*8bcb0991SDimitry Andric           break;
292*8bcb0991SDimitry Andric         case '\b':
293*8bcb0991SDimitry Andric           Sizes << "\\b";
294*8bcb0991SDimitry Andric           break;
295*8bcb0991SDimitry Andric         case '\f':
296*8bcb0991SDimitry Andric           Sizes << "\\f";
297*8bcb0991SDimitry Andric           break;
298*8bcb0991SDimitry Andric         case '\n':
299*8bcb0991SDimitry Andric           Sizes << "\\n";
300*8bcb0991SDimitry Andric           break;
301*8bcb0991SDimitry Andric         case '\r':
302*8bcb0991SDimitry Andric           Sizes << "\\r";
303*8bcb0991SDimitry Andric           break;
304*8bcb0991SDimitry Andric         case '\v':
305*8bcb0991SDimitry Andric           Sizes << "\\v";
306*8bcb0991SDimitry Andric           break;
307*8bcb0991SDimitry Andric         case ':':
308*8bcb0991SDimitry Andric           // ':' cannot be scanned by Flex, as it is defined as a delimiter
309*8bcb0991SDimitry Andric           // Replace it with it's octal representation \72
310*8bcb0991SDimitry Andric           Sizes << "\\72";
311*8bcb0991SDimitry Andric           break;
312*8bcb0991SDimitry Andric         default:
313*8bcb0991SDimitry Andric           Sizes << Str[I];
314*8bcb0991SDimitry Andric           break;
315*8bcb0991SDimitry Andric         }
316*8bcb0991SDimitry Andric       }
317*8bcb0991SDimitry Andric 
318*8bcb0991SDimitry Andric       // Insert the printf_alloc call
319*8bcb0991SDimitry Andric       Builder.SetInsertPoint(CI);
320*8bcb0991SDimitry Andric       Builder.SetCurrentDebugLocation(CI->getDebugLoc());
321*8bcb0991SDimitry Andric 
322*8bcb0991SDimitry Andric       AttributeList Attr = AttributeList::get(Ctx, AttributeList::FunctionIndex,
323*8bcb0991SDimitry Andric                                               Attribute::NoUnwind);
324*8bcb0991SDimitry Andric 
325*8bcb0991SDimitry Andric       Type *SizetTy = Type::getInt32Ty(Ctx);
326*8bcb0991SDimitry Andric 
327*8bcb0991SDimitry Andric       Type *Tys_alloc[1] = {SizetTy};
328*8bcb0991SDimitry Andric       Type *I8Ptr = PointerType::get(Type::getInt8Ty(Ctx), 1);
329*8bcb0991SDimitry Andric       FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false);
330*8bcb0991SDimitry Andric       FunctionCallee PrintfAllocFn =
331*8bcb0991SDimitry Andric           M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr);
332*8bcb0991SDimitry Andric 
333*8bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n');
334*8bcb0991SDimitry Andric       std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str().c_str();
335*8bcb0991SDimitry Andric       MDString *fmtStrArray = MDString::get(Ctx, fmtstr);
336*8bcb0991SDimitry Andric 
337*8bcb0991SDimitry Andric       // Instead of creating global variables, the
338*8bcb0991SDimitry Andric       // printf format strings are extracted
339*8bcb0991SDimitry Andric       // and passed as metadata. This avoids
340*8bcb0991SDimitry Andric       // polluting llvm's symbol tables in this module.
341*8bcb0991SDimitry Andric       // Metadata is going to be extracted
342*8bcb0991SDimitry Andric       // by the backend passes and inserted
343*8bcb0991SDimitry Andric       // into the OpenCL binary as appropriate.
344*8bcb0991SDimitry Andric       StringRef amd("llvm.printf.fmts");
345*8bcb0991SDimitry Andric       NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd);
346*8bcb0991SDimitry Andric       MDNode *myMD = MDNode::get(Ctx, fmtStrArray);
347*8bcb0991SDimitry Andric       metaD->addOperand(myMD);
348*8bcb0991SDimitry Andric       Value *sumC = ConstantInt::get(SizetTy, Sum, false);
349*8bcb0991SDimitry Andric       SmallVector<Value *, 1> alloc_args;
350*8bcb0991SDimitry Andric       alloc_args.push_back(sumC);
351*8bcb0991SDimitry Andric       CallInst *pcall =
352*8bcb0991SDimitry Andric           CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI);
353*8bcb0991SDimitry Andric 
354*8bcb0991SDimitry Andric       //
355*8bcb0991SDimitry Andric       // Insert code to split basicblock with a
356*8bcb0991SDimitry Andric       // piece of hammock code.
357*8bcb0991SDimitry Andric       // basicblock splits after buffer overflow check
358*8bcb0991SDimitry Andric       //
359*8bcb0991SDimitry Andric       ConstantPointerNull *zeroIntPtr =
360*8bcb0991SDimitry Andric           ConstantPointerNull::get(PointerType::get(Type::getInt8Ty(Ctx), 1));
361*8bcb0991SDimitry Andric       ICmpInst *cmp =
362*8bcb0991SDimitry Andric           dyn_cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, ""));
363*8bcb0991SDimitry Andric       if (!CI->use_empty()) {
364*8bcb0991SDimitry Andric         Value *result =
365*8bcb0991SDimitry Andric             Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res");
366*8bcb0991SDimitry Andric         CI->replaceAllUsesWith(result);
367*8bcb0991SDimitry Andric       }
368*8bcb0991SDimitry Andric       SplitBlock(CI->getParent(), cmp);
369*8bcb0991SDimitry Andric       Instruction *Brnch =
370*8bcb0991SDimitry Andric           SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false);
371*8bcb0991SDimitry Andric 
372*8bcb0991SDimitry Andric       Builder.SetInsertPoint(Brnch);
373*8bcb0991SDimitry Andric 
374*8bcb0991SDimitry Andric       // store unique printf id in the buffer
375*8bcb0991SDimitry Andric       //
376*8bcb0991SDimitry Andric       SmallVector<Value *, 1> ZeroIdxList;
377*8bcb0991SDimitry Andric       ConstantInt *zeroInt =
378*8bcb0991SDimitry Andric           ConstantInt::get(Ctx, APInt(32, StringRef("0"), 10));
379*8bcb0991SDimitry Andric       ZeroIdxList.push_back(zeroInt);
380*8bcb0991SDimitry Andric 
381*8bcb0991SDimitry Andric       GetElementPtrInst *BufferIdx =
382*8bcb0991SDimitry Andric           dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create(
383*8bcb0991SDimitry Andric               nullptr, pcall, ZeroIdxList, "PrintBuffID", Brnch));
384*8bcb0991SDimitry Andric 
385*8bcb0991SDimitry Andric       Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
386*8bcb0991SDimitry Andric       Value *id_gep_cast =
387*8bcb0991SDimitry Andric           new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch);
388*8bcb0991SDimitry Andric 
389*8bcb0991SDimitry Andric       StoreInst *stbuff =
390*8bcb0991SDimitry Andric           new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast);
391*8bcb0991SDimitry Andric       stbuff->insertBefore(Brnch); // to Remove unused variable warning
392*8bcb0991SDimitry Andric 
393*8bcb0991SDimitry Andric       SmallVector<Value *, 2> FourthIdxList;
394*8bcb0991SDimitry Andric       ConstantInt *fourInt =
395*8bcb0991SDimitry Andric           ConstantInt::get(Ctx, APInt(32, StringRef("4"), 10));
396*8bcb0991SDimitry Andric 
397*8bcb0991SDimitry Andric       FourthIdxList.push_back(fourInt); // 1st 4 bytes hold the printf_id
398*8bcb0991SDimitry Andric       // the following GEP is the buffer pointer
399*8bcb0991SDimitry Andric       BufferIdx = cast<GetElementPtrInst>(GetElementPtrInst::Create(
400*8bcb0991SDimitry Andric           nullptr, pcall, FourthIdxList, "PrintBuffGep", Brnch));
401*8bcb0991SDimitry Andric 
402*8bcb0991SDimitry Andric       Type *Int32Ty = Type::getInt32Ty(Ctx);
403*8bcb0991SDimitry Andric       Type *Int64Ty = Type::getInt64Ty(Ctx);
404*8bcb0991SDimitry Andric       for (unsigned ArgCount = 1; ArgCount < CI->getNumArgOperands() &&
405*8bcb0991SDimitry Andric                                   ArgCount <= OpConvSpecifiers.size();
406*8bcb0991SDimitry Andric            ArgCount++) {
407*8bcb0991SDimitry Andric         Value *Arg = CI->getArgOperand(ArgCount);
408*8bcb0991SDimitry Andric         Type *ArgType = Arg->getType();
409*8bcb0991SDimitry Andric         SmallVector<Value *, 32> WhatToStore;
410*8bcb0991SDimitry Andric         if (ArgType->isFPOrFPVectorTy() &&
411*8bcb0991SDimitry Andric             (ArgType->getTypeID() != Type::VectorTyID)) {
412*8bcb0991SDimitry Andric           Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
413*8bcb0991SDimitry Andric           if (OpConvSpecifiers[ArgCount - 1] == 'f') {
414*8bcb0991SDimitry Andric             ConstantFP *fpCons = dyn_cast<ConstantFP>(Arg);
415*8bcb0991SDimitry Andric             if (fpCons) {
416*8bcb0991SDimitry Andric               APFloat Val(fpCons->getValueAPF());
417*8bcb0991SDimitry Andric               bool Lost = false;
418*8bcb0991SDimitry Andric               Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
419*8bcb0991SDimitry Andric                           &Lost);
420*8bcb0991SDimitry Andric               Arg = ConstantFP::get(Ctx, Val);
421*8bcb0991SDimitry Andric               IType = Int32Ty;
422*8bcb0991SDimitry Andric             } else {
423*8bcb0991SDimitry Andric               FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
424*8bcb0991SDimitry Andric               if (FpExt && FpExt->getType()->isDoubleTy() &&
425*8bcb0991SDimitry Andric                   FpExt->getOperand(0)->getType()->isFloatTy()) {
426*8bcb0991SDimitry Andric                 Arg = FpExt->getOperand(0);
427*8bcb0991SDimitry Andric                 IType = Int32Ty;
428*8bcb0991SDimitry Andric               }
429*8bcb0991SDimitry Andric             }
430*8bcb0991SDimitry Andric           }
431*8bcb0991SDimitry Andric           Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch);
432*8bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
433*8bcb0991SDimitry Andric         } else if (ArgType->getTypeID() == Type::PointerTyID) {
434*8bcb0991SDimitry Andric           if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
435*8bcb0991SDimitry Andric             const char *S = NonLiteralStr;
436*8bcb0991SDimitry Andric             if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
437*8bcb0991SDimitry Andric               GlobalVariable *GV =
438*8bcb0991SDimitry Andric                   dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
439*8bcb0991SDimitry Andric               if (GV && GV->hasInitializer()) {
440*8bcb0991SDimitry Andric                 Constant *Init = GV->getInitializer();
441*8bcb0991SDimitry Andric                 ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init);
442*8bcb0991SDimitry Andric                 if (Init->isZeroValue() || CA->isString()) {
443*8bcb0991SDimitry Andric                   S = Init->isZeroValue() ? "" : CA->getAsCString().data();
444*8bcb0991SDimitry Andric                 }
445*8bcb0991SDimitry Andric               }
446*8bcb0991SDimitry Andric             }
447*8bcb0991SDimitry Andric             size_t SizeStr = strlen(S) + 1;
448*8bcb0991SDimitry Andric             size_t Rem = SizeStr % DWORD_ALIGN;
449*8bcb0991SDimitry Andric             size_t NSizeStr = 0;
450*8bcb0991SDimitry Andric             if (Rem) {
451*8bcb0991SDimitry Andric               NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
452*8bcb0991SDimitry Andric             } else {
453*8bcb0991SDimitry Andric               NSizeStr = SizeStr;
454*8bcb0991SDimitry Andric             }
455*8bcb0991SDimitry Andric             if (S[0]) {
456*8bcb0991SDimitry Andric               char *MyNewStr = new char[NSizeStr]();
457*8bcb0991SDimitry Andric               strcpy(MyNewStr, S);
458*8bcb0991SDimitry Andric               int NumInts = NSizeStr / 4;
459*8bcb0991SDimitry Andric               int CharC = 0;
460*8bcb0991SDimitry Andric               while (NumInts) {
461*8bcb0991SDimitry Andric                 int ANum = *(int *)(MyNewStr + CharC);
462*8bcb0991SDimitry Andric                 CharC += 4;
463*8bcb0991SDimitry Andric                 NumInts--;
464*8bcb0991SDimitry Andric                 Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
465*8bcb0991SDimitry Andric                 WhatToStore.push_back(ANumV);
466*8bcb0991SDimitry Andric               }
467*8bcb0991SDimitry Andric               delete[] MyNewStr;
468*8bcb0991SDimitry Andric             } else {
469*8bcb0991SDimitry Andric               // Empty string, give a hint to RT it is no NULL
470*8bcb0991SDimitry Andric               Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);
471*8bcb0991SDimitry Andric               WhatToStore.push_back(ANumV);
472*8bcb0991SDimitry Andric             }
473*8bcb0991SDimitry Andric           } else {
474*8bcb0991SDimitry Andric             uint64_t Size = TD->getTypeAllocSizeInBits(ArgType);
475*8bcb0991SDimitry Andric             assert((Size == 32 || Size == 64) && "unsupported size");
476*8bcb0991SDimitry Andric             Type *DstType = (Size == 32) ? Int32Ty : Int64Ty;
477*8bcb0991SDimitry Andric             Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch);
478*8bcb0991SDimitry Andric             WhatToStore.push_back(Arg);
479*8bcb0991SDimitry Andric           }
480*8bcb0991SDimitry Andric         } else if (ArgType->getTypeID() == Type::VectorTyID) {
481*8bcb0991SDimitry Andric           Type *IType = NULL;
482*8bcb0991SDimitry Andric           uint32_t EleCount = cast<VectorType>(ArgType)->getNumElements();
483*8bcb0991SDimitry Andric           uint32_t EleSize = ArgType->getScalarSizeInBits();
484*8bcb0991SDimitry Andric           uint32_t TotalSize = EleCount * EleSize;
485*8bcb0991SDimitry Andric           if (EleCount == 3) {
486*8bcb0991SDimitry Andric             IntegerType *Int32Ty = Type::getInt32Ty(ArgType->getContext());
487*8bcb0991SDimitry Andric             Constant *Indices[4] = {
488*8bcb0991SDimitry Andric                 ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1),
489*8bcb0991SDimitry Andric                 ConstantInt::get(Int32Ty, 2), ConstantInt::get(Int32Ty, 2)};
490*8bcb0991SDimitry Andric             Constant *Mask = ConstantVector::get(Indices);
491*8bcb0991SDimitry Andric             ShuffleVectorInst *Shuffle = new ShuffleVectorInst(Arg, Arg, Mask);
492*8bcb0991SDimitry Andric             Shuffle->insertBefore(Brnch);
493*8bcb0991SDimitry Andric             Arg = Shuffle;
494*8bcb0991SDimitry Andric             ArgType = Arg->getType();
495*8bcb0991SDimitry Andric             TotalSize += EleSize;
496*8bcb0991SDimitry Andric           }
497*8bcb0991SDimitry Andric           switch (EleSize) {
498*8bcb0991SDimitry Andric           default:
499*8bcb0991SDimitry Andric             EleCount = TotalSize / 64;
500*8bcb0991SDimitry Andric             IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
501*8bcb0991SDimitry Andric             break;
502*8bcb0991SDimitry Andric           case 8:
503*8bcb0991SDimitry Andric             if (EleCount >= 8) {
504*8bcb0991SDimitry Andric               EleCount = TotalSize / 64;
505*8bcb0991SDimitry Andric               IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
506*8bcb0991SDimitry Andric             } else if (EleCount >= 3) {
507*8bcb0991SDimitry Andric               EleCount = 1;
508*8bcb0991SDimitry Andric               IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
509*8bcb0991SDimitry Andric             } else {
510*8bcb0991SDimitry Andric               EleCount = 1;
511*8bcb0991SDimitry Andric               IType = dyn_cast<Type>(Type::getInt16Ty(ArgType->getContext()));
512*8bcb0991SDimitry Andric             }
513*8bcb0991SDimitry Andric             break;
514*8bcb0991SDimitry Andric           case 16:
515*8bcb0991SDimitry Andric             if (EleCount >= 3) {
516*8bcb0991SDimitry Andric               EleCount = TotalSize / 64;
517*8bcb0991SDimitry Andric               IType = dyn_cast<Type>(Type::getInt64Ty(ArgType->getContext()));
518*8bcb0991SDimitry Andric             } else {
519*8bcb0991SDimitry Andric               EleCount = 1;
520*8bcb0991SDimitry Andric               IType = dyn_cast<Type>(Type::getInt32Ty(ArgType->getContext()));
521*8bcb0991SDimitry Andric             }
522*8bcb0991SDimitry Andric             break;
523*8bcb0991SDimitry Andric           }
524*8bcb0991SDimitry Andric           if (EleCount > 1) {
525*8bcb0991SDimitry Andric             IType = dyn_cast<Type>(VectorType::get(IType, EleCount));
526*8bcb0991SDimitry Andric           }
527*8bcb0991SDimitry Andric           Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch);
528*8bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
529*8bcb0991SDimitry Andric         } else {
530*8bcb0991SDimitry Andric           WhatToStore.push_back(Arg);
531*8bcb0991SDimitry Andric         }
532*8bcb0991SDimitry Andric         for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) {
533*8bcb0991SDimitry Andric           Value *TheBtCast = WhatToStore[I];
534*8bcb0991SDimitry Andric           unsigned ArgSize =
535*8bcb0991SDimitry Andric               TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8;
536*8bcb0991SDimitry Andric           SmallVector<Value *, 1> BuffOffset;
537*8bcb0991SDimitry Andric           BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize));
538*8bcb0991SDimitry Andric 
539*8bcb0991SDimitry Andric           Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1);
540*8bcb0991SDimitry Andric           Value *CastedGEP =
541*8bcb0991SDimitry Andric               new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch);
542*8bcb0991SDimitry Andric           StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch);
543*8bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n"
544*8bcb0991SDimitry Andric                             << *StBuff << '\n');
545*8bcb0991SDimitry Andric           (void)StBuff;
546*8bcb0991SDimitry Andric           if (I + 1 == E && ArgCount + 1 == CI->getNumArgOperands())
547*8bcb0991SDimitry Andric             break;
548*8bcb0991SDimitry Andric           BufferIdx = dyn_cast<GetElementPtrInst>(GetElementPtrInst::Create(
549*8bcb0991SDimitry Andric               nullptr, BufferIdx, BuffOffset, "PrintBuffNextPtr", Brnch));
550*8bcb0991SDimitry Andric           LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
551*8bcb0991SDimitry Andric                             << *BufferIdx << '\n');
552*8bcb0991SDimitry Andric         }
553*8bcb0991SDimitry Andric       }
554*8bcb0991SDimitry Andric     }
555*8bcb0991SDimitry Andric   }
556*8bcb0991SDimitry Andric 
557*8bcb0991SDimitry Andric   // erase the printf calls
558*8bcb0991SDimitry Andric   for (auto CI : Printfs)
559*8bcb0991SDimitry Andric     CI->eraseFromParent();
560*8bcb0991SDimitry Andric 
561*8bcb0991SDimitry Andric   Printfs.clear();
562*8bcb0991SDimitry Andric   return true;
563*8bcb0991SDimitry Andric }
564*8bcb0991SDimitry Andric 
565*8bcb0991SDimitry Andric bool AMDGPUPrintfRuntimeBinding::runOnModule(Module &M) {
566*8bcb0991SDimitry Andric   Triple TT(M.getTargetTriple());
567*8bcb0991SDimitry Andric   if (TT.getArch() == Triple::r600)
568*8bcb0991SDimitry Andric     return false;
569*8bcb0991SDimitry Andric 
570*8bcb0991SDimitry Andric   auto PrintfFunction = M.getFunction("printf");
571*8bcb0991SDimitry Andric   if (!PrintfFunction)
572*8bcb0991SDimitry Andric     return false;
573*8bcb0991SDimitry Andric 
574*8bcb0991SDimitry Andric   for (auto &U : PrintfFunction->uses()) {
575*8bcb0991SDimitry Andric     if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
576*8bcb0991SDimitry Andric       if (CI->isCallee(&U))
577*8bcb0991SDimitry Andric         Printfs.push_back(CI);
578*8bcb0991SDimitry Andric     }
579*8bcb0991SDimitry Andric   }
580*8bcb0991SDimitry Andric 
581*8bcb0991SDimitry Andric   if (Printfs.empty())
582*8bcb0991SDimitry Andric     return false;
583*8bcb0991SDimitry Andric 
584*8bcb0991SDimitry Andric   TD = &M.getDataLayout();
585*8bcb0991SDimitry Andric   auto DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
586*8bcb0991SDimitry Andric   DT = DTWP ? &DTWP->getDomTree() : nullptr;
587*8bcb0991SDimitry Andric   auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
588*8bcb0991SDimitry Andric     return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
589*8bcb0991SDimitry Andric   };
590*8bcb0991SDimitry Andric 
591*8bcb0991SDimitry Andric   return lowerPrintfForGpu(M, GetTLI);
592*8bcb0991SDimitry Andric }
593