1 //===--- RuntimeDebugBuilder.cpp - Helper to insert prints into LLVM-IR ---===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 //===----------------------------------------------------------------------===// 11 12 #include "polly/CodeGen/RuntimeDebugBuilder.h" 13 #include "llvm/IR/Intrinsics.h" 14 #include "llvm/IR/Module.h" 15 #include "llvm/Support/Debug.h" 16 #include <string> 17 #include <vector> 18 19 using namespace llvm; 20 using namespace polly; 21 22 Function *RuntimeDebugBuilder::getVPrintF(PollyIRBuilder &Builder) { 23 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 24 const char *Name = "vprintf"; 25 Function *F = M->getFunction(Name); 26 27 if (!F) { 28 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 29 FunctionType *Ty = FunctionType::get( 30 Builder.getInt32Ty(), {Builder.getInt8PtrTy(), Builder.getInt8PtrTy()}, 31 false); 32 F = Function::Create(Ty, Linkage, Name, M); 33 } 34 35 return F; 36 } 37 38 Function *RuntimeDebugBuilder::getAddressSpaceCast(PollyIRBuilder &Builder, 39 unsigned Src, unsigned Dst, 40 unsigned SrcBits, 41 unsigned DstBits) { 42 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 43 auto Name = std::string("llvm.nvvm.ptr.constant.to.gen.p") + 44 std::to_string(Dst) + "i" + std::to_string(DstBits) + ".p" + 45 std::to_string(Src) + "i" + std::to_string(SrcBits); 46 Function *F = M->getFunction(Name); 47 48 if (!F) { 49 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 50 FunctionType *Ty = FunctionType::get( 51 PointerType::get(Builder.getIntNTy(DstBits), Dst), 52 PointerType::get(Builder.getIntNTy(SrcBits), Src), false); 53 F = Function::Create(Ty, Linkage, Name, M); 54 } 55 56 return F; 57 } 58 59 std::vector<Value *> 60 RuntimeDebugBuilder::getGPUThreadIdentifiers(PollyIRBuilder &Builder) { 61 std::vector<Value *> Identifiers; 62 63 auto M = Builder.GetInsertBlock()->getParent()->getParent(); 64 65 std::vector<Function *> BlockIDs = { 66 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_x), 67 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_y), 68 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_ctaid_z), 69 }; 70 71 Identifiers.push_back(Builder.CreateGlobalStringPtr("> block-id: ", "", 4)); 72 for (auto GetID : BlockIDs) { 73 Value *Id = Builder.CreateCall(GetID, {}); 74 Id = Builder.CreateIntCast(Id, Builder.getInt64Ty(), false); 75 Identifiers.push_back(Id); 76 Identifiers.push_back(Builder.CreateGlobalStringPtr(" ", "", 4)); 77 } 78 79 Identifiers.push_back(Builder.CreateGlobalStringPtr("| ", "", 4)); 80 81 std::vector<Function *> ThreadIDs = { 82 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_x), 83 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_y), 84 Intrinsic::getDeclaration(M, Intrinsic::nvvm_read_ptx_sreg_tid_z), 85 }; 86 87 Identifiers.push_back(Builder.CreateGlobalStringPtr("thread-id: ", "", 4)); 88 for (auto GetId : ThreadIDs) { 89 Value *Id = Builder.CreateCall(GetId, {}); 90 Id = Builder.CreateIntCast(Id, Builder.getInt64Ty(), false); 91 Identifiers.push_back(Id); 92 Identifiers.push_back(Builder.CreateGlobalStringPtr(" ", "", 4)); 93 } 94 95 return Identifiers; 96 } 97 98 void RuntimeDebugBuilder::createPrinter(PollyIRBuilder &Builder, bool IsGPU, 99 ArrayRef<Value *> Values) { 100 if (IsGPU) 101 createGPUPrinterT(Builder, Values); 102 else 103 createCPUPrinterT(Builder, Values); 104 } 105 106 static std::tuple<std::string, std::vector<Value *>> 107 prepareValuesForPrinting(PollyIRBuilder &Builder, ArrayRef<Value *> Values) { 108 std::string FormatString; 109 std::vector<Value *> ValuesToPrint; 110 111 for (auto Val : Values) { 112 Type *Ty = Val->getType(); 113 114 if (Ty->isFloatingPointTy()) { 115 if (!Ty->isDoubleTy()) 116 Val = Builder.CreateFPExt(Val, Builder.getDoubleTy()); 117 } else if (Ty->isIntegerTy()) { 118 if (Ty->getIntegerBitWidth() < 64) 119 Val = Builder.CreateSExt(Val, Builder.getInt64Ty()); 120 else 121 assert(Ty->getIntegerBitWidth() && 122 "Integer types larger 64 bit not supported"); 123 } else if (isa<PointerType>(Ty)) { 124 if (Ty->getPointerElementType() == Builder.getInt8Ty() && 125 Ty->getPointerAddressSpace() == 4) { 126 Val = Builder.CreateGEP(Val, Builder.getInt64(0)); 127 } else { 128 Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty()); 129 } 130 } else { 131 llvm_unreachable("Unknown type"); 132 } 133 134 Ty = Val->getType(); 135 136 if (Ty->isFloatingPointTy()) 137 FormatString += "%f"; 138 else if (Ty->isIntegerTy()) 139 FormatString += "%ld"; 140 else 141 FormatString += "%s"; 142 143 ValuesToPrint.push_back(Val); 144 } 145 146 return std::make_tuple(FormatString, ValuesToPrint); 147 } 148 149 void RuntimeDebugBuilder::createCPUPrinterT(PollyIRBuilder &Builder, 150 ArrayRef<Value *> Values) { 151 152 std::string FormatString; 153 std::vector<Value *> ValuesToPrint; 154 155 std::tie(FormatString, ValuesToPrint) = 156 prepareValuesForPrinting(Builder, Values); 157 158 createPrintF(Builder, FormatString, ValuesToPrint); 159 createFlush(Builder); 160 } 161 162 void RuntimeDebugBuilder::createGPUPrinterT(PollyIRBuilder &Builder, 163 ArrayRef<Value *> Values) { 164 std::string str; 165 166 auto *Zero = Builder.getInt64(0); 167 168 auto ToPrint = getGPUThreadIdentifiers(Builder); 169 170 ToPrint.push_back(Builder.CreateGlobalStringPtr("\n ", "", 4)); 171 ToPrint.insert(ToPrint.end(), Values.begin(), Values.end()); 172 173 const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout(); 174 175 // Allocate print buffer (assuming 2*32 bit per element) 176 auto T = ArrayType::get(Builder.getInt32Ty(), ToPrint.size() * 2); 177 Value *Data = new AllocaInst( 178 T, DL.getAllocaAddrSpace(), "polly.vprint.buffer", 179 &Builder.GetInsertBlock()->getParent()->getEntryBlock().front()); 180 auto *DataPtr = Builder.CreateGEP(Data, {Zero, Zero}); 181 182 int Offset = 0; 183 for (auto Val : ToPrint) { 184 auto Ptr = Builder.CreateGEP(DataPtr, Builder.getInt64(Offset)); 185 Type *Ty = Val->getType(); 186 187 if (Ty->isFloatingPointTy()) { 188 if (!Ty->isDoubleTy()) 189 Val = Builder.CreateFPExt(Val, Builder.getDoubleTy()); 190 } else if (Ty->isIntegerTy()) { 191 if (Ty->getIntegerBitWidth() < 64) 192 Val = Builder.CreateSExt(Val, Builder.getInt64Ty()); 193 else 194 llvm_unreachable("Integer types larger 64 bit not supported"); 195 } else if (auto PtTy = dyn_cast<PointerType>(Ty)) { 196 if (PtTy->getAddressSpace() == 4) { 197 // Pointers in constant address space are printed as strings 198 Val = Builder.CreateGEP(Val, Builder.getInt64(0)); 199 auto F = RuntimeDebugBuilder::getAddressSpaceCast(Builder, 4, 0); 200 Val = Builder.CreateCall(F, Val); 201 } else { 202 Val = Builder.CreatePtrToInt(Val, Builder.getInt64Ty()); 203 } 204 } else { 205 llvm_unreachable("Unknown type"); 206 } 207 208 Ty = Val->getType(); 209 Ptr = Builder.CreatePointerBitCastOrAddrSpaceCast(Ptr, Ty->getPointerTo(5)); 210 Builder.CreateAlignedStore(Val, Ptr, 4); 211 212 if (Ty->isFloatingPointTy()) 213 str += "%f"; 214 else if (Ty->isIntegerTy()) 215 str += "%ld"; 216 else 217 str += "%s"; 218 219 Offset += 2; 220 } 221 222 Value *Format = Builder.CreateGlobalStringPtr(str, "polly.vprintf.buffer", 4); 223 Format = Builder.CreateCall(getAddressSpaceCast(Builder, 4, 0), Format); 224 225 Data = Builder.CreateBitCast(Data, Builder.getInt8PtrTy()); 226 227 Builder.CreateCall(getVPrintF(Builder), {Format, Data}); 228 } 229 230 Function *RuntimeDebugBuilder::getPrintF(PollyIRBuilder &Builder) { 231 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 232 const char *Name = "printf"; 233 Function *F = M->getFunction(Name); 234 235 if (!F) { 236 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 237 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), true); 238 F = Function::Create(Ty, Linkage, Name, M); 239 } 240 241 return F; 242 } 243 244 void RuntimeDebugBuilder::createPrintF(PollyIRBuilder &Builder, 245 std::string Format, 246 ArrayRef<Value *> Values) { 247 Value *FormatString = Builder.CreateGlobalStringPtr(Format); 248 std::vector<Value *> Arguments; 249 250 Arguments.push_back(FormatString); 251 Arguments.insert(Arguments.end(), Values.begin(), Values.end()); 252 Builder.CreateCall(getPrintF(Builder), Arguments); 253 } 254 255 void RuntimeDebugBuilder::createFlush(PollyIRBuilder &Builder) { 256 Module *M = Builder.GetInsertBlock()->getParent()->getParent(); 257 const char *Name = "fflush"; 258 Function *F = M->getFunction(Name); 259 260 if (!F) { 261 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 262 FunctionType *Ty = 263 FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false); 264 F = Function::Create(Ty, Linkage, Name, M); 265 } 266 267 // fflush(NULL) flushes _all_ open output streams. 268 // 269 // fflush is declared as 'int fflush(FILE *stream)'. As we only pass on a NULL 270 // pointer, the type we point to does conceptually not matter. However, if 271 // fflush is already declared in this translation unit, we use the very same 272 // type to ensure that LLVM does not complain about mismatching types. 273 Builder.CreateCall(F, Constant::getNullValue(F->arg_begin()->getType())); 274 } 275