clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name NVPTXAsmPrinter.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Target/NVPTX -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Target/NVPTX -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/Target/NVPTX -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Target/NVPTX -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "NVPTXAsmPrinter.h" |
15 | #include "MCTargetDesc/NVPTXBaseInfo.h" |
16 | #include "MCTargetDesc/NVPTXInstPrinter.h" |
17 | #include "MCTargetDesc/NVPTXMCAsmInfo.h" |
18 | #include "MCTargetDesc/NVPTXTargetStreamer.h" |
19 | #include "NVPTX.h" |
20 | #include "NVPTXMCExpr.h" |
21 | #include "NVPTXMachineFunctionInfo.h" |
22 | #include "NVPTXRegisterInfo.h" |
23 | #include "NVPTXSubtarget.h" |
24 | #include "NVPTXTargetMachine.h" |
25 | #include "NVPTXUtilities.h" |
26 | #include "TargetInfo/NVPTXTargetInfo.h" |
27 | #include "cl_common_defines.h" |
28 | #include "llvm/ADT/APFloat.h" |
29 | #include "llvm/ADT/APInt.h" |
30 | #include "llvm/ADT/DenseMap.h" |
31 | #include "llvm/ADT/DenseSet.h" |
32 | #include "llvm/ADT/SmallString.h" |
33 | #include "llvm/ADT/SmallVector.h" |
34 | #include "llvm/ADT/StringExtras.h" |
35 | #include "llvm/ADT/StringRef.h" |
36 | #include "llvm/ADT/Triple.h" |
37 | #include "llvm/ADT/Twine.h" |
38 | #include "llvm/Analysis/ConstantFolding.h" |
39 | #include "llvm/CodeGen/Analysis.h" |
40 | #include "llvm/CodeGen/MachineBasicBlock.h" |
41 | #include "llvm/CodeGen/MachineFrameInfo.h" |
42 | #include "llvm/CodeGen/MachineFunction.h" |
43 | #include "llvm/CodeGen/MachineInstr.h" |
44 | #include "llvm/CodeGen/MachineLoopInfo.h" |
45 | #include "llvm/CodeGen/MachineModuleInfo.h" |
46 | #include "llvm/CodeGen/MachineOperand.h" |
47 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
48 | #include "llvm/CodeGen/TargetLowering.h" |
49 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
50 | #include "llvm/CodeGen/ValueTypes.h" |
51 | #include "llvm/IR/Attributes.h" |
52 | #include "llvm/IR/BasicBlock.h" |
53 | #include "llvm/IR/Constant.h" |
54 | #include "llvm/IR/Constants.h" |
55 | #include "llvm/IR/DataLayout.h" |
56 | #include "llvm/IR/DebugInfo.h" |
57 | #include "llvm/IR/DebugInfoMetadata.h" |
58 | #include "llvm/IR/DebugLoc.h" |
59 | #include "llvm/IR/DerivedTypes.h" |
60 | #include "llvm/IR/Function.h" |
61 | #include "llvm/IR/GlobalValue.h" |
62 | #include "llvm/IR/GlobalVariable.h" |
63 | #include "llvm/IR/Instruction.h" |
64 | #include "llvm/IR/LLVMContext.h" |
65 | #include "llvm/IR/Module.h" |
66 | #include "llvm/IR/Operator.h" |
67 | #include "llvm/IR/Type.h" |
68 | #include "llvm/IR/User.h" |
69 | #include "llvm/MC/MCExpr.h" |
70 | #include "llvm/MC/MCInst.h" |
71 | #include "llvm/MC/MCInstrDesc.h" |
72 | #include "llvm/MC/MCStreamer.h" |
73 | #include "llvm/MC/MCSymbol.h" |
74 | #include "llvm/Support/Casting.h" |
75 | #include "llvm/Support/CommandLine.h" |
76 | #include "llvm/Support/ErrorHandling.h" |
77 | #include "llvm/Support/MachineValueType.h" |
78 | #include "llvm/Support/Path.h" |
79 | #include "llvm/Support/TargetRegistry.h" |
80 | #include "llvm/Support/raw_ostream.h" |
81 | #include "llvm/Target/TargetLoweringObjectFile.h" |
82 | #include "llvm/Target/TargetMachine.h" |
83 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
84 | #include <cassert> |
85 | #include <cstdint> |
86 | #include <cstring> |
87 | #include <new> |
88 | #include <string> |
89 | #include <utility> |
90 | #include <vector> |
91 | |
92 | using namespace llvm; |
93 | |
94 | #define DEPOTNAME "__local_depot" |
95 | |
96 | |
97 | |
98 | static void |
99 | DiscoverDependentGlobals(const Value *V, |
100 | DenseSet<const GlobalVariable *> &Globals) { |
101 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
102 | Globals.insert(GV); |
103 | else { |
104 | if (const User *U = dyn_cast<User>(V)) { |
105 | for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) { |
106 | DiscoverDependentGlobals(U->getOperand(i), Globals); |
107 | } |
108 | } |
109 | } |
110 | } |
111 | |
112 | |
113 | |
114 | |
115 | static void |
116 | VisitGlobalVariableForEmission(const GlobalVariable *GV, |
117 | SmallVectorImpl<const GlobalVariable *> &Order, |
118 | DenseSet<const GlobalVariable *> &Visited, |
119 | DenseSet<const GlobalVariable *> &Visiting) { |
120 | |
121 | if (Visited.count(GV)) |
122 | return; |
123 | |
124 | |
125 | if (!Visiting.insert(GV).second) |
126 | report_fatal_error("Circular dependency found in global variable set"); |
127 | |
128 | |
129 | DenseSet<const GlobalVariable *> Others; |
130 | for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i) |
131 | DiscoverDependentGlobals(GV->getOperand(i), Others); |
132 | |
133 | for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(), |
134 | E = Others.end(); |
135 | I != E; ++I) |
136 | VisitGlobalVariableForEmission(*I, Order, Visited, Visiting); |
137 | |
138 | |
139 | Order.push_back(GV); |
140 | Visited.insert(GV); |
141 | Visiting.erase(GV); |
142 | } |
143 | |
144 | void NVPTXAsmPrinter::emitInstruction(const MachineInstr *MI) { |
145 | MCInst Inst; |
146 | lowerToMCInst(MI, Inst); |
147 | EmitToStreamer(*OutStreamer, Inst); |
148 | } |
149 | |
150 | |
151 | bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI, |
152 | unsigned OpNo, MCOperand &MCOp) { |
153 | const MachineOperand &MO = MI->getOperand(OpNo); |
154 | const MCInstrDesc &MCID = MI->getDesc(); |
155 | |
156 | if (MCID.TSFlags & NVPTXII::IsTexFlag) { |
157 | |
158 | |
159 | if (OpNo == 4 && MO.isImm()) { |
160 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
161 | return true; |
162 | } |
163 | if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) { |
164 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
165 | return true; |
166 | } |
167 | |
168 | return false; |
169 | } else if (MCID.TSFlags & NVPTXII::IsSuldMask) { |
170 | unsigned VecSize = |
171 | 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1); |
172 | |
173 | |
174 | if (OpNo == VecSize && MO.isImm()) { |
175 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
176 | return true; |
177 | } |
178 | |
179 | return false; |
180 | } else if (MCID.TSFlags & NVPTXII::IsSustFlag) { |
181 | |
182 | if (OpNo == 0 && MO.isImm()) { |
183 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
184 | return true; |
185 | } |
186 | |
187 | return false; |
188 | } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) { |
189 | |
190 | if (OpNo == 1 && MO.isImm()) { |
191 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
192 | return true; |
193 | } |
194 | |
195 | return false; |
196 | } |
197 | |
198 | return false; |
199 | } |
200 | |
201 | void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) { |
202 | |
203 | LLVMTargetMachine &TM = const_cast<LLVMTargetMachine&>(MF->getTarget()); |
204 | NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM); |
205 | const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>(); |
206 | const char *Sym = MFI->getImageHandleSymbol(Index); |
207 | std::string *SymNamePtr = |
208 | nvTM.getManagedStrPool()->getManagedString(Sym); |
209 | MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(StringRef(*SymNamePtr))); |
210 | } |
211 | |
212 | void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) { |
213 | OutMI.setOpcode(MI->getOpcode()); |
214 | |
215 | if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) { |
216 | const MachineOperand &MO = MI->getOperand(0); |
217 | OutMI.addOperand(GetSymbolRef( |
218 | OutContext.getOrCreateSymbol(Twine(MO.getSymbolName())))); |
219 | return; |
220 | } |
221 | |
222 | const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>(); |
223 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
224 | const MachineOperand &MO = MI->getOperand(i); |
225 | |
226 | MCOperand MCOp; |
227 | if (!STI.hasImageHandles()) { |
228 | if (lowerImageHandleOperand(MI, i, MCOp)) { |
229 | OutMI.addOperand(MCOp); |
230 | continue; |
231 | } |
232 | } |
233 | |
234 | if (lowerOperand(MO, MCOp)) |
235 | OutMI.addOperand(MCOp); |
236 | } |
237 | } |
238 | |
239 | bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO, |
240 | MCOperand &MCOp) { |
241 | switch (MO.getType()) { |
242 | default: llvm_unreachable("unknown operand type"); |
243 | case MachineOperand::MO_Register: |
244 | MCOp = MCOperand::createReg(encodeVirtualRegister(MO.getReg())); |
245 | break; |
246 | case MachineOperand::MO_Immediate: |
247 | MCOp = MCOperand::createImm(MO.getImm()); |
248 | break; |
249 | case MachineOperand::MO_MachineBasicBlock: |
250 | MCOp = MCOperand::createExpr(MCSymbolRefExpr::create( |
251 | MO.getMBB()->getSymbol(), OutContext)); |
252 | break; |
253 | case MachineOperand::MO_ExternalSymbol: |
254 | MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName())); |
255 | break; |
256 | case MachineOperand::MO_GlobalAddress: |
257 | MCOp = GetSymbolRef(getSymbol(MO.getGlobal())); |
258 | break; |
259 | case MachineOperand::MO_FPImmediate: { |
260 | const ConstantFP *Cnt = MO.getFPImm(); |
261 | const APFloat &Val = Cnt->getValueAPF(); |
262 | |
263 | switch (Cnt->getType()->getTypeID()) { |
264 | default: report_fatal_error("Unsupported FP type"); break; |
265 | case Type::HalfTyID: |
266 | MCOp = MCOperand::createExpr( |
267 | NVPTXFloatMCExpr::createConstantFPHalf(Val, OutContext)); |
268 | break; |
269 | case Type::FloatTyID: |
270 | MCOp = MCOperand::createExpr( |
271 | NVPTXFloatMCExpr::createConstantFPSingle(Val, OutContext)); |
272 | break; |
273 | case Type::DoubleTyID: |
274 | MCOp = MCOperand::createExpr( |
275 | NVPTXFloatMCExpr::createConstantFPDouble(Val, OutContext)); |
276 | break; |
277 | } |
278 | break; |
279 | } |
280 | } |
281 | return true; |
282 | } |
283 | |
284 | unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) { |
285 | if (Register::isVirtualRegister(Reg)) { |
286 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
287 | |
288 | DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC]; |
289 | unsigned RegNum = RegMap[Reg]; |
290 | |
291 | |
292 | |
293 | unsigned Ret = 0; |
294 | if (RC == &NVPTX::Int1RegsRegClass) { |
295 | Ret = (1 << 28); |
296 | } else if (RC == &NVPTX::Int16RegsRegClass) { |
297 | Ret = (2 << 28); |
298 | } else if (RC == &NVPTX::Int32RegsRegClass) { |
299 | Ret = (3 << 28); |
300 | } else if (RC == &NVPTX::Int64RegsRegClass) { |
301 | Ret = (4 << 28); |
302 | } else if (RC == &NVPTX::Float32RegsRegClass) { |
303 | Ret = (5 << 28); |
304 | } else if (RC == &NVPTX::Float64RegsRegClass) { |
305 | Ret = (6 << 28); |
306 | } else if (RC == &NVPTX::Float16RegsRegClass) { |
307 | Ret = (7 << 28); |
308 | } else if (RC == &NVPTX::Float16x2RegsRegClass) { |
309 | Ret = (8 << 28); |
310 | } else { |
311 | report_fatal_error("Bad register class"); |
312 | } |
313 | |
314 | |
315 | Ret |= (RegNum & 0x0FFFFFFF); |
316 | return Ret; |
317 | } else { |
318 | |
319 | |
320 | return Reg & 0x0FFFFFFF; |
321 | } |
322 | } |
323 | |
324 | MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) { |
325 | const MCExpr *Expr; |
326 | Expr = MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, |
327 | OutContext); |
328 | return MCOperand::createExpr(Expr); |
329 | } |
330 | |
331 | void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) { |
332 | const DataLayout &DL = getDataLayout(); |
333 | const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F); |
334 | const TargetLowering *TLI = STI.getTargetLowering(); |
335 | |
336 | Type *Ty = F->getReturnType(); |
337 | |
338 | bool isABI = (STI.getSmVersion() >= 20); |
339 | |
340 | if (Ty->getTypeID() == Type::VoidTyID) |
341 | return; |
342 | |
343 | O << " ("; |
344 | |
345 | if (isABI) { |
346 | if (Ty->isFloatingPointTy() || (Ty->isIntegerTy() && !Ty->isIntegerTy(128))) { |
347 | unsigned size = 0; |
348 | if (auto *ITy = dyn_cast<IntegerType>(Ty)) { |
349 | size = ITy->getBitWidth(); |
350 | } else { |
351 | assert(Ty->isFloatingPointTy() && "Floating point type expected here"); |
352 | size = Ty->getPrimitiveSizeInBits(); |
353 | } |
354 | |
355 | |
356 | |
357 | if (size < 32) |
358 | size = 32; |
359 | |
360 | O << ".param .b" << size << " func_retval0"; |
361 | } else if (isa<PointerType>(Ty)) { |
362 | O << ".param .b" << TLI->getPointerTy(DL).getSizeInBits() |
363 | << " func_retval0"; |
364 | } else if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
365 | unsigned totalsz = DL.getTypeAllocSize(Ty); |
366 | unsigned retAlignment = 0; |
367 | if (!getAlign(*F, 0, retAlignment)) |
368 | retAlignment = DL.getABITypeAlignment(Ty); |
369 | O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz |
370 | << "]"; |
371 | } else |
372 | llvm_unreachable("Unknown return type"); |
373 | } else { |
374 | SmallVector<EVT, 16> vtparts; |
375 | ComputeValueVTs(*TLI, DL, Ty, vtparts); |
376 | unsigned idx = 0; |
377 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
378 | unsigned elems = 1; |
379 | EVT elemtype = vtparts[i]; |
380 | if (vtparts[i].isVector()) { |
381 | elems = vtparts[i].getVectorNumElements(); |
382 | elemtype = vtparts[i].getVectorElementType(); |
383 | } |
384 | |
385 | for (unsigned j = 0, je = elems; j != je; ++j) { |
386 | unsigned sz = elemtype.getSizeInBits(); |
387 | if (elemtype.isInteger() && (sz < 32)) |
388 | sz = 32; |
389 | O << ".reg .b" << sz << " func_retval" << idx; |
390 | if (j < je - 1) |
391 | O << ", "; |
392 | ++idx; |
393 | } |
394 | if (i < e - 1) |
395 | O << ", "; |
396 | } |
397 | } |
398 | O << ") "; |
399 | } |
400 | |
401 | void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF, |
402 | raw_ostream &O) { |
403 | const Function &F = MF.getFunction(); |
404 | printReturnValStr(&F, O); |
405 | } |
406 | |
407 | |
408 | |
409 | |
410 | bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll( |
411 | const MachineBasicBlock &MBB) const { |
412 | MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>(); |
413 | |
414 | if (!LI.isLoopHeader(&MBB)) |
415 | return false; |
416 | |
417 | |
418 | |
419 | |
420 | for (auto I = MBB.pred_begin(); I != MBB.pred_end(); ++I) { |
421 | const MachineBasicBlock *PMBB = *I; |
422 | if (LI.getLoopFor(PMBB) != LI.getLoopFor(&MBB)) { |
423 | |
424 | continue; |
425 | } |
426 | if (const BasicBlock *PBB = PMBB->getBasicBlock()) { |
427 | if (MDNode *LoopID = |
428 | PBB->getTerminator()->getMetadata(LLVMContext::MD_loop)) { |
429 | if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable")) |
430 | return true; |
431 | } |
432 | } |
433 | } |
434 | return false; |
435 | } |
436 | |
437 | void NVPTXAsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) { |
438 | AsmPrinter::emitBasicBlockStart(MBB); |
439 | if (isLoopHeaderOfNoUnroll(MBB)) |
440 | OutStreamer->emitRawText(StringRef("\t.pragma \"nounroll\";\n")); |
441 | } |
442 | |
443 | void NVPTXAsmPrinter::emitFunctionEntryLabel() { |
444 | SmallString<128> Str; |
445 | raw_svector_ostream O(Str); |
446 | |
447 | if (!GlobalsEmitted) { |
448 | emitGlobals(*MF->getFunction().getParent()); |
449 | GlobalsEmitted = true; |
450 | } |
451 | |
452 | |
453 | MRI = &MF->getRegInfo(); |
454 | F = &MF->getFunction(); |
455 | emitLinkageDirective(F, O); |
456 | if (isKernelFunction(*F)) |
457 | O << ".entry "; |
458 | else { |
459 | O << ".func "; |
460 | printReturnValStr(*MF, O); |
461 | } |
462 | |
463 | CurrentFnSym->print(O, MAI); |
464 | |
465 | emitFunctionParamList(*MF, O); |
466 | |
467 | if (isKernelFunction(*F)) |
468 | emitKernelFunctionDirectives(*F, O); |
469 | |
470 | OutStreamer->emitRawText(O.str()); |
471 | |
472 | VRegMapping.clear(); |
473 | |
474 | OutStreamer->emitRawText(StringRef("{\n")); |
475 | setAndEmitFunctionVirtualRegisters(*MF); |
476 | |
477 | if (MMI && MMI->hasDebugInfo()) |
478 | emitInitialRawDwarfLocDirective(*MF); |
479 | } |
480 | |
481 | bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) { |
482 | bool Result = AsmPrinter::runOnMachineFunction(F); |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | OutStreamer->emitRawText(StringRef("}\n")); |
489 | return Result; |
490 | } |
491 | |
492 | void NVPTXAsmPrinter::emitFunctionBodyStart() { |
493 | SmallString<128> Str; |
494 | raw_svector_ostream O(Str); |
495 | emitDemotedVars(&MF->getFunction(), O); |
496 | OutStreamer->emitRawText(O.str()); |
497 | } |
498 | |
499 | void NVPTXAsmPrinter::emitFunctionBodyEnd() { |
500 | VRegMapping.clear(); |
501 | } |
502 | |
503 | const MCSymbol *NVPTXAsmPrinter::getFunctionFrameSymbol() const { |
504 | SmallString<128> Str; |
505 | raw_svector_ostream(Str) << DEPOTNAME << getFunctionNumber(); |
506 | return OutContext.getOrCreateSymbol(Str); |
507 | } |
508 | |
509 | void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const { |
510 | Register RegNo = MI->getOperand(0).getReg(); |
511 | if (Register::isVirtualRegister(RegNo)) { |
512 | OutStreamer->AddComment(Twine("implicit-def: ") + |
513 | getVirtualRegisterName(RegNo)); |
514 | } else { |
515 | const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>(); |
516 | OutStreamer->AddComment(Twine("implicit-def: ") + |
517 | STI.getRegisterInfo()->getName(RegNo)); |
518 | } |
519 | OutStreamer->AddBlankLine(); |
520 | } |
521 | |
522 | void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F, |
523 | raw_ostream &O) const { |
524 | |
525 | |
526 | |
527 | unsigned reqntidx, reqntidy, reqntidz; |
528 | bool specified = false; |
529 | if (!getReqNTIDx(F, reqntidx)) |
530 | reqntidx = 1; |
531 | else |
532 | specified = true; |
533 | if (!getReqNTIDy(F, reqntidy)) |
534 | reqntidy = 1; |
535 | else |
536 | specified = true; |
537 | if (!getReqNTIDz(F, reqntidz)) |
538 | reqntidz = 1; |
539 | else |
540 | specified = true; |
541 | |
542 | if (specified) |
543 | O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz |
544 | << "\n"; |
545 | |
546 | |
547 | |
548 | |
549 | unsigned maxntidx, maxntidy, maxntidz; |
550 | specified = false; |
551 | if (!getMaxNTIDx(F, maxntidx)) |
552 | maxntidx = 1; |
553 | else |
554 | specified = true; |
555 | if (!getMaxNTIDy(F, maxntidy)) |
556 | maxntidy = 1; |
557 | else |
558 | specified = true; |
559 | if (!getMaxNTIDz(F, maxntidz)) |
560 | maxntidz = 1; |
561 | else |
562 | specified = true; |
563 | |
564 | if (specified) |
565 | O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz |
566 | << "\n"; |
567 | |
568 | unsigned mincta; |
569 | if (getMinCTASm(F, mincta)) |
570 | O << ".minnctapersm " << mincta << "\n"; |
571 | |
572 | unsigned maxnreg; |
573 | if (getMaxNReg(F, maxnreg)) |
574 | O << ".maxnreg " << maxnreg << "\n"; |
575 | } |
576 | |
577 | std::string |
578 | NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const { |
579 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
580 | |
581 | std::string Name; |
582 | raw_string_ostream NameStr(Name); |
583 | |
584 | VRegRCMap::const_iterator I = VRegMapping.find(RC); |
585 | assert(I != VRegMapping.end() && "Bad register class"); |
586 | const DenseMap<unsigned, unsigned> &RegMap = I->second; |
587 | |
588 | VRegMap::const_iterator VI = RegMap.find(Reg); |
589 | assert(VI != RegMap.end() && "Bad virtual register"); |
590 | unsigned MappedVR = VI->second; |
591 | |
592 | NameStr << getNVPTXRegClassStr(RC) << MappedVR; |
593 | |
594 | NameStr.flush(); |
595 | return Name; |
596 | } |
597 | |
598 | void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr, |
599 | raw_ostream &O) { |
600 | O << getVirtualRegisterName(vr); |
601 | } |
602 | |
603 | void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) { |
604 | emitLinkageDirective(F, O); |
605 | if (isKernelFunction(*F)) |
| 10 | | Assuming the condition is false | |
|
| |
606 | O << ".entry "; |
607 | else |
608 | O << ".func "; |
609 | printReturnValStr(F, O); |
610 | getSymbol(F)->print(O, MAI); |
611 | O << "\n"; |
612 | emitFunctionParamList(F, O); |
| 12 | | Calling 'NVPTXAsmPrinter::emitFunctionParamList' | |
|
613 | O << ";\n"; |
614 | } |
615 | |
616 | static bool usedInGlobalVarDef(const Constant *C) { |
617 | if (!C) |
618 | return false; |
619 | |
620 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
621 | return GV->getName() != "llvm.used"; |
622 | } |
623 | |
624 | for (const User *U : C->users()) |
625 | if (const Constant *C = dyn_cast<Constant>(U)) |
626 | if (usedInGlobalVarDef(C)) |
627 | return true; |
628 | |
629 | return false; |
630 | } |
631 | |
632 | static bool usedInOneFunc(const User *U, Function const *&oneFunc) { |
633 | if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) { |
634 | if (othergv->getName() == "llvm.used") |
635 | return true; |
636 | } |
637 | |
638 | if (const Instruction *instr = dyn_cast<Instruction>(U)) { |
639 | if (instr->getParent() && instr->getParent()->getParent()) { |
640 | const Function *curFunc = instr->getParent()->getParent(); |
641 | if (oneFunc && (curFunc != oneFunc)) |
642 | return false; |
643 | oneFunc = curFunc; |
644 | return true; |
645 | } else |
646 | return false; |
647 | } |
648 | |
649 | for (const User *UU : U->users()) |
650 | if (!usedInOneFunc(UU, oneFunc)) |
651 | return false; |
652 | |
653 | return true; |
654 | } |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) { |
664 | if (!gv->hasInternalLinkage()) |
665 | return false; |
666 | PointerType *Pty = gv->getType(); |
667 | if (Pty->getAddressSpace() != ADDRESS_SPACE_SHARED) |
668 | return false; |
669 | |
670 | const Function *oneFunc = nullptr; |
671 | |
672 | bool flag = usedInOneFunc(gv, oneFunc); |
673 | if (!flag) |
674 | return false; |
675 | if (!oneFunc) |
676 | return false; |
677 | f = oneFunc; |
678 | return true; |
679 | } |
680 | |
681 | static bool useFuncSeen(const Constant *C, |
682 | DenseMap<const Function *, bool> &seenMap) { |
683 | for (const User *U : C->users()) { |
684 | if (const Constant *cu = dyn_cast<Constant>(U)) { |
685 | if (useFuncSeen(cu, seenMap)) |
686 | return true; |
687 | } else if (const Instruction *I = dyn_cast<Instruction>(U)) { |
688 | const BasicBlock *bb = I->getParent(); |
689 | if (!bb) |
690 | continue; |
691 | const Function *caller = bb->getParent(); |
692 | if (!caller) |
693 | continue; |
694 | if (seenMap.find(caller) != seenMap.end()) |
695 | return true; |
696 | } |
697 | } |
698 | return false; |
699 | } |
700 | |
701 | void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) { |
702 | DenseMap<const Function *, bool> seenMap; |
703 | for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
| 6 | | Loop condition is true. Entering loop body | |
|
704 | const Function *F = &*FI; |
705 | |
706 | if (F->getAttributes().hasFnAttr("nvptx-libcall-callee")) { |
| 7 | | Assuming the condition is true | |
|
| |
707 | emitDeclaration(F, O); |
| 9 | | Calling 'NVPTXAsmPrinter::emitDeclaration' | |
|
708 | continue; |
709 | } |
710 | |
711 | if (F->isDeclaration()) { |
712 | if (F->use_empty()) |
713 | continue; |
714 | if (F->getIntrinsicID()) |
715 | continue; |
716 | emitDeclaration(F, O); |
717 | continue; |
718 | } |
719 | for (const User *U : F->users()) { |
720 | if (const Constant *C = dyn_cast<Constant>(U)) { |
721 | if (usedInGlobalVarDef(C)) { |
722 | |
723 | |
724 | |
725 | emitDeclaration(F, O); |
726 | break; |
727 | } |
728 | |
729 | |
730 | if (useFuncSeen(C, seenMap)) { |
731 | emitDeclaration(F, O); |
732 | break; |
733 | } |
734 | } |
735 | |
736 | if (!isa<Instruction>(U)) |
737 | continue; |
738 | const Instruction *instr = cast<Instruction>(U); |
739 | const BasicBlock *bb = instr->getParent(); |
740 | if (!bb) |
741 | continue; |
742 | const Function *caller = bb->getParent(); |
743 | if (!caller) |
744 | continue; |
745 | |
746 | |
747 | |
748 | |
749 | if (seenMap.find(caller) != seenMap.end()) { |
750 | emitDeclaration(F, O); |
751 | break; |
752 | } |
753 | } |
754 | seenMap[F] = true; |
755 | } |
756 | } |
757 | |
758 | static bool isEmptyXXStructor(GlobalVariable *GV) { |
759 | if (!GV) return true; |
760 | const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
761 | if (!InitList) return true; |
762 | return InitList->getNumOperands() == 0; |
763 | } |
764 | |
765 | void NVPTXAsmPrinter::emitStartOfAsmFile(Module &M) { |
766 | |
767 | |
768 | |
769 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
770 | const auto* STI = static_cast<const NVPTXSubtarget*>(NTM.getSubtargetImpl()); |
771 | SmallString<128> Str1; |
772 | raw_svector_ostream OS1(Str1); |
773 | |
774 | |
775 | emitHeader(M, OS1, *STI); |
776 | OutStreamer->emitRawText(OS1.str()); |
777 | } |
778 | |
779 | bool NVPTXAsmPrinter::doInitialization(Module &M) { |
780 | if (M.alias_size()) { |
781 | report_fatal_error("Module has aliases, which NVPTX does not support."); |
782 | return true; |
783 | } |
784 | if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_ctors"))) { |
785 | report_fatal_error( |
786 | "Module has a nontrivial global ctor, which NVPTX does not support."); |
787 | return true; |
788 | } |
789 | if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_dtors"))) { |
790 | report_fatal_error( |
791 | "Module has a nontrivial global dtor, which NVPTX does not support."); |
792 | return true; |
793 | } |
794 | |
795 | |
796 | bool Result = AsmPrinter::doInitialization(M); |
797 | |
798 | GlobalsEmitted = false; |
799 | |
800 | return Result; |
801 | } |
802 | |
803 | void NVPTXAsmPrinter::emitGlobals(const Module &M) { |
804 | SmallString<128> Str2; |
805 | raw_svector_ostream OS2(Str2); |
806 | |
807 | emitDeclarations(M, OS2); |
| 5 | | Calling 'NVPTXAsmPrinter::emitDeclarations' | |
|
808 | |
809 | |
810 | |
811 | |
812 | |
813 | |
814 | SmallVector<const GlobalVariable *, 8> Globals; |
815 | DenseSet<const GlobalVariable *> GVVisited; |
816 | DenseSet<const GlobalVariable *> GVVisiting; |
817 | |
818 | |
819 | for (const GlobalVariable &I : M.globals()) |
820 | VisitGlobalVariableForEmission(&I, Globals, GVVisited, GVVisiting); |
821 | |
822 | assert(GVVisited.size() == M.getGlobalList().size() && |
823 | "Missed a global variable"); |
824 | assert(GVVisiting.size() == 0 && "Did not fully process a global variable"); |
825 | |
826 | |
827 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) |
828 | printModuleLevelGV(Globals[i], OS2); |
829 | |
830 | OS2 << '\n'; |
831 | |
832 | OutStreamer->emitRawText(OS2.str()); |
833 | } |
834 | |
835 | void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O, |
836 | const NVPTXSubtarget &STI) { |
837 | O << "//\n"; |
838 | O << "// Generated by LLVM NVPTX Back-End\n"; |
839 | O << "//\n"; |
840 | O << "\n"; |
841 | |
842 | unsigned PTXVersion = STI.getPTXVersion(); |
843 | O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n"; |
844 | |
845 | O << ".target "; |
846 | O << STI.getTargetName(); |
847 | |
848 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
849 | if (NTM.getDrvInterface() == NVPTX::NVCL) |
850 | O << ", texmode_independent"; |
851 | |
852 | bool HasFullDebugInfo = false; |
853 | for (DICompileUnit *CU : M.debug_compile_units()) { |
854 | switch(CU->getEmissionKind()) { |
855 | case DICompileUnit::NoDebug: |
856 | case DICompileUnit::DebugDirectivesOnly: |
857 | break; |
858 | case DICompileUnit::LineTablesOnly: |
859 | case DICompileUnit::FullDebug: |
860 | HasFullDebugInfo = true; |
861 | break; |
862 | } |
863 | if (HasFullDebugInfo) |
864 | break; |
865 | } |
866 | if (MMI && MMI->hasDebugInfo() && HasFullDebugInfo) |
867 | O << ", debug"; |
868 | |
869 | O << "\n"; |
870 | |
871 | O << ".address_size "; |
872 | if (NTM.is64Bit()) |
873 | O << "64"; |
874 | else |
875 | O << "32"; |
876 | O << "\n"; |
877 | |
878 | O << "\n"; |
879 | } |
880 | |
881 | bool NVPTXAsmPrinter::doFinalization(Module &M) { |
882 | bool HasDebugInfo = MMI && MMI->hasDebugInfo(); |
| 1 | Assuming field 'MMI' is null | |
|
883 | |
884 | |
885 | |
886 | if (!GlobalsEmitted) { |
| 2 | | Assuming field 'GlobalsEmitted' is false | |
|
| |
887 | emitGlobals(M); |
| 4 | | Calling 'NVPTXAsmPrinter::emitGlobals' | |
|
888 | GlobalsEmitted = true; |
889 | } |
890 | |
891 | |
892 | |
893 | |
894 | Module::GlobalListType &global_list = M.getGlobalList(); |
895 | int i, n = global_list.size(); |
896 | GlobalVariable **gv_array = new GlobalVariable *[n]; |
897 | |
898 | |
899 | i = 0; |
900 | for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
901 | I != E; ++I) |
902 | gv_array[i++] = &*I; |
903 | |
904 | |
905 | while (!global_list.empty()) |
906 | global_list.remove(global_list.begin()); |
907 | |
908 | |
909 | bool ret = AsmPrinter::doFinalization(M); |
910 | |
911 | |
912 | for (i = 0; i < n; i++) |
913 | global_list.insert(global_list.end(), gv_array[i]); |
914 | |
915 | clearAnnotationCache(&M); |
916 | |
917 | delete[] gv_array; |
918 | |
919 | if (HasDebugInfo) { |
920 | static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer()) |
921 | ->closeLastSection(); |
922 | |
923 | OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}"); |
924 | } |
925 | |
926 | |
927 | static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer()) |
928 | ->outputDwarfFileDirectives(); |
929 | |
930 | return ret; |
931 | |
932 | |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | |
940 | } |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | |
955 | void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V, |
956 | raw_ostream &O) { |
957 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) { |
958 | if (V->hasExternalLinkage()) { |
959 | if (isa<GlobalVariable>(V)) { |
960 | const GlobalVariable *GVar = cast<GlobalVariable>(V); |
961 | if (GVar) { |
962 | if (GVar->hasInitializer()) |
963 | O << ".visible "; |
964 | else |
965 | O << ".extern "; |
966 | } |
967 | } else if (V->isDeclaration()) |
968 | O << ".extern "; |
969 | else |
970 | O << ".visible "; |
971 | } else if (V->hasAppendingLinkage()) { |
972 | std::string msg; |
973 | msg.append("Error: "); |
974 | msg.append("Symbol "); |
975 | if (V->hasName()) |
976 | msg.append(std::string(V->getName())); |
977 | msg.append("has unsupported appending linkage type"); |
978 | llvm_unreachable(msg.c_str()); |
979 | } else if (!V->hasInternalLinkage() && |
980 | !V->hasPrivateLinkage()) { |
981 | O << ".weak "; |
982 | } |
983 | } |
984 | } |
985 | |
986 | void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, |
987 | raw_ostream &O, |
988 | bool processDemoted) { |
989 | |
990 | if (GVar->hasSection()) { |
991 | if (GVar->getSection() == "llvm.metadata") |
992 | return; |
993 | } |
994 | |
995 | |
996 | if (GVar->getName().startswith("llvm.") || |
997 | GVar->getName().startswith("nvvm.")) |
998 | return; |
999 | |
1000 | const DataLayout &DL = getDataLayout(); |
1001 | |
1002 | |
1003 | PointerType *PTy = GVar->getType(); |
1004 | Type *ETy = GVar->getValueType(); |
1005 | |
1006 | if (GVar->hasExternalLinkage()) { |
1007 | if (GVar->hasInitializer()) |
1008 | O << ".visible "; |
1009 | else |
1010 | O << ".extern "; |
1011 | } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() || |
1012 | GVar->hasAvailableExternallyLinkage() || |
1013 | GVar->hasCommonLinkage()) { |
1014 | O << ".weak "; |
1015 | } |
1016 | |
1017 | if (isTexture(*GVar)) { |
1018 | O << ".global .texref " << getTextureName(*GVar) << ";\n"; |
1019 | return; |
1020 | } |
1021 | |
1022 | if (isSurface(*GVar)) { |
1023 | O << ".global .surfref " << getSurfaceName(*GVar) << ";\n"; |
1024 | return; |
1025 | } |
1026 | |
1027 | if (GVar->isDeclaration()) { |
1028 | |
1029 | |
1030 | |
1031 | emitPTXGlobalVariable(GVar, O); |
1032 | O << ";\n"; |
1033 | return; |
1034 | } |
1035 | |
1036 | if (isSampler(*GVar)) { |
1037 | O << ".global .samplerref " << getSamplerName(*GVar); |
1038 | |
1039 | const Constant *Initializer = nullptr; |
1040 | if (GVar->hasInitializer()) |
1041 | Initializer = GVar->getInitializer(); |
1042 | const ConstantInt *CI = nullptr; |
1043 | if (Initializer) |
1044 | CI = dyn_cast<ConstantInt>(Initializer); |
1045 | if (CI) { |
1046 | unsigned sample = CI->getZExtValue(); |
1047 | |
1048 | O << " = { "; |
1049 | |
1050 | for (int i = 0, |
1051 | addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE); |
1052 | i < 3; i++) { |
1053 | O << "addr_mode_" << i << " = "; |
1054 | switch (addr) { |
1055 | case 0: |
1056 | O << "wrap"; |
1057 | break; |
1058 | case 1: |
1059 | O << "clamp_to_border"; |
1060 | break; |
1061 | case 2: |
1062 | O << "clamp_to_edge"; |
1063 | break; |
1064 | case 3: |
1065 | O << "wrap"; |
1066 | break; |
1067 | case 4: |
1068 | O << "mirror"; |
1069 | break; |
1070 | } |
1071 | O << ", "; |
1072 | } |
1073 | O << "filter_mode = "; |
1074 | switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) { |
1075 | case 0: |
1076 | O << "nearest"; |
1077 | break; |
1078 | case 1: |
1079 | O << "linear"; |
1080 | break; |
1081 | case 2: |
1082 | llvm_unreachable("Anisotropic filtering is not supported"); |
1083 | default: |
1084 | O << "nearest"; |
1085 | break; |
1086 | } |
1087 | if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) { |
1088 | O << ", force_unnormalized_coords = 1"; |
1089 | } |
1090 | O << " }"; |
1091 | } |
1092 | |
1093 | O << ";\n"; |
1094 | return; |
1095 | } |
1096 | |
1097 | if (GVar->hasPrivateLinkage()) { |
1098 | if (strncmp(GVar->getName().data(), "unrollpragma", 12) == 0) |
1099 | return; |
1100 | |
1101 | |
1102 | if (strncmp(GVar->getName().data(), "filename", 8) == 0) |
1103 | return; |
1104 | if (GVar->use_empty()) |
1105 | return; |
1106 | } |
1107 | |
1108 | const Function *demotedFunc = nullptr; |
1109 | if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) { |
1110 | O << "// " << GVar->getName() << " has been demoted\n"; |
1111 | if (localDecls.find(demotedFunc) != localDecls.end()) |
1112 | localDecls[demotedFunc].push_back(GVar); |
1113 | else { |
1114 | std::vector<const GlobalVariable *> temp; |
1115 | temp.push_back(GVar); |
1116 | localDecls[demotedFunc] = temp; |
1117 | } |
1118 | return; |
1119 | } |
1120 | |
1121 | O << "."; |
1122 | emitPTXAddressSpace(PTy->getAddressSpace(), O); |
1123 | |
1124 | if (isManaged(*GVar)) { |
1125 | O << " .attribute(.managed)"; |
1126 | } |
1127 | |
1128 | if (GVar->getAlignment() == 0) |
1129 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
1130 | else |
1131 | O << " .align " << GVar->getAlignment(); |
1132 | |
1133 | if (ETy->isFloatingPointTy() || ETy->isPointerTy() || |
1134 | (ETy->isIntegerTy() && ETy->getScalarSizeInBits() <= 64)) { |
1135 | O << " ."; |
1136 | |
1137 | if (ETy->isIntegerTy(1)) |
1138 | O << "u8"; |
1139 | else |
1140 | O << getPTXFundamentalTypeStr(ETy, false); |
1141 | O << " "; |
1142 | getSymbol(GVar)->print(O, MAI); |
1143 | |
1144 | |
1145 | |
1146 | if (GVar->hasInitializer()) { |
1147 | if ((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
1148 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) { |
1149 | const Constant *Initializer = GVar->getInitializer(); |
1150 | |
1151 | if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) { |
1152 | O << " = "; |
1153 | printScalarConstant(Initializer, O); |
1154 | } |
1155 | } else { |
1156 | |
1157 | |
1158 | |
1159 | if (!GVar->getInitializer()->isNullValue() && |
1160 | !isa<UndefValue>(GVar->getInitializer())) { |
1161 | report_fatal_error("initial value of '" + GVar->getName() + |
1162 | "' is not allowed in addrspace(" + |
1163 | Twine(PTy->getAddressSpace()) + ")"); |
1164 | } |
1165 | } |
1166 | } |
1167 | } else { |
1168 | unsigned int ElementSize = 0; |
1169 | |
1170 | |
1171 | |
1172 | |
1173 | |
1174 | switch (ETy->getTypeID()) { |
1175 | case Type::IntegerTyID: |
1176 | case Type::StructTyID: |
1177 | case Type::ArrayTyID: |
1178 | case Type::FixedVectorTyID: |
1179 | ElementSize = DL.getTypeStoreSize(ETy); |
1180 | |
1181 | |
1182 | if (((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
1183 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) && |
1184 | GVar->hasInitializer()) { |
1185 | const Constant *Initializer = GVar->getInitializer(); |
1186 | if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) { |
1187 | AggBuffer aggBuffer(ElementSize, O, *this); |
1188 | bufferAggregateConstant(Initializer, &aggBuffer); |
1189 | if (aggBuffer.numSymbols) { |
1190 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) { |
1191 | O << " .u64 "; |
1192 | getSymbol(GVar)->print(O, MAI); |
1193 | O << "["; |
1194 | O << ElementSize / 8; |
1195 | } else { |
1196 | O << " .u32 "; |
1197 | getSymbol(GVar)->print(O, MAI); |
1198 | O << "["; |
1199 | O << ElementSize / 4; |
1200 | } |
1201 | O << "]"; |
1202 | } else { |
1203 | O << " .b8 "; |
1204 | getSymbol(GVar)->print(O, MAI); |
1205 | O << "["; |
1206 | O << ElementSize; |
1207 | O << "]"; |
1208 | } |
1209 | O << " = {"; |
1210 | aggBuffer.print(); |
1211 | O << "}"; |
1212 | } else { |
1213 | O << " .b8 "; |
1214 | getSymbol(GVar)->print(O, MAI); |
1215 | if (ElementSize) { |
1216 | O << "["; |
1217 | O << ElementSize; |
1218 | O << "]"; |
1219 | } |
1220 | } |
1221 | } else { |
1222 | O << " .b8 "; |
1223 | getSymbol(GVar)->print(O, MAI); |
1224 | if (ElementSize) { |
1225 | O << "["; |
1226 | O << ElementSize; |
1227 | O << "]"; |
1228 | } |
1229 | } |
1230 | break; |
1231 | default: |
1232 | llvm_unreachable("type not supported yet"); |
1233 | } |
1234 | } |
1235 | O << ";\n"; |
1236 | } |
1237 | |
1238 | void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) { |
1239 | if (localDecls.find(f) == localDecls.end()) |
1240 | return; |
1241 | |
1242 | std::vector<const GlobalVariable *> &gvars = localDecls[f]; |
1243 | |
1244 | for (unsigned i = 0, e = gvars.size(); i != e; ++i) { |
1245 | O << "\t// demoted variable\n\t"; |
1246 | printModuleLevelGV(gvars[i], O, true); |
1247 | } |
1248 | } |
1249 | |
1250 | void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace, |
1251 | raw_ostream &O) const { |
1252 | switch (AddressSpace) { |
1253 | case ADDRESS_SPACE_LOCAL: |
1254 | O << "local"; |
1255 | break; |
1256 | case ADDRESS_SPACE_GLOBAL: |
1257 | O << "global"; |
1258 | break; |
1259 | case ADDRESS_SPACE_CONST: |
1260 | O << "const"; |
1261 | break; |
1262 | case ADDRESS_SPACE_SHARED: |
1263 | O << "shared"; |
1264 | break; |
1265 | default: |
1266 | report_fatal_error("Bad address space found while emitting PTX: " + |
1267 | llvm::Twine(AddressSpace)); |
1268 | break; |
1269 | } |
1270 | } |
1271 | |
1272 | std::string |
1273 | NVPTXAsmPrinter::getPTXFundamentalTypeStr(Type *Ty, bool useB4PTR) const { |
1274 | switch (Ty->getTypeID()) { |
1275 | case Type::IntegerTyID: { |
1276 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); |
1277 | if (NumBits == 1) |
1278 | return "pred"; |
1279 | else if (NumBits <= 64) { |
1280 | std::string name = "u"; |
1281 | return name + utostr(NumBits); |
1282 | } else { |
1283 | llvm_unreachable("Integer too large"); |
1284 | break; |
1285 | } |
1286 | break; |
1287 | } |
1288 | case Type::HalfTyID: |
1289 | |
1290 | return "b16"; |
1291 | case Type::FloatTyID: |
1292 | return "f32"; |
1293 | case Type::DoubleTyID: |
1294 | return "f64"; |
1295 | case Type::PointerTyID: |
1296 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) |
1297 | if (useB4PTR) |
1298 | return "b64"; |
1299 | else |
1300 | return "u64"; |
1301 | else if (useB4PTR) |
1302 | return "b32"; |
1303 | else |
1304 | return "u32"; |
1305 | default: |
1306 | break; |
1307 | } |
1308 | llvm_unreachable("unexpected type"); |
1309 | } |
1310 | |
1311 | void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar, |
1312 | raw_ostream &O) { |
1313 | const DataLayout &DL = getDataLayout(); |
1314 | |
1315 | |
1316 | Type *ETy = GVar->getValueType(); |
1317 | |
1318 | O << "."; |
1319 | emitPTXAddressSpace(GVar->getType()->getAddressSpace(), O); |
1320 | if (GVar->getAlignment() == 0) |
1321 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
1322 | else |
1323 | O << " .align " << GVar->getAlignment(); |
1324 | |
1325 | |
1326 | if (ETy->isIntegerTy(128)) { |
1327 | O << " .b8 "; |
1328 | getSymbol(GVar)->print(O, MAI); |
1329 | O << "[16]"; |
1330 | return; |
1331 | } |
1332 | |
1333 | if (ETy->isFloatingPointTy() || ETy->isIntOrPtrTy()) { |
1334 | O << " ."; |
1335 | O << getPTXFundamentalTypeStr(ETy); |
1336 | O << " "; |
1337 | getSymbol(GVar)->print(O, MAI); |
1338 | return; |
1339 | } |
1340 | |
1341 | int64_t ElementSize = 0; |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | switch (ETy->getTypeID()) { |
1348 | case Type::StructTyID: |
1349 | case Type::ArrayTyID: |
1350 | case Type::FixedVectorTyID: |
1351 | ElementSize = DL.getTypeStoreSize(ETy); |
1352 | O << " .b8 "; |
1353 | getSymbol(GVar)->print(O, MAI); |
1354 | O << "["; |
1355 | if (ElementSize) { |
1356 | O << ElementSize; |
1357 | } |
1358 | O << "]"; |
1359 | break; |
1360 | default: |
1361 | llvm_unreachable("type not supported yet"); |
1362 | } |
1363 | } |
1364 | |
1365 | static unsigned int getOpenCLAlignment(const DataLayout &DL, Type *Ty) { |
1366 | if (Ty->isSingleValueType()) |
1367 | return DL.getPrefTypeAlignment(Ty); |
1368 | |
1369 | auto *ATy = dyn_cast<ArrayType>(Ty); |
1370 | if (ATy) |
1371 | return getOpenCLAlignment(DL, ATy->getElementType()); |
1372 | |
1373 | auto *STy = dyn_cast<StructType>(Ty); |
1374 | if (STy) { |
1375 | unsigned int alignStruct = 1; |
1376 | |
1377 | |
1378 | for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { |
1379 | Type *ETy = STy->getElementType(i); |
1380 | unsigned int align = getOpenCLAlignment(DL, ETy); |
1381 | if (align > alignStruct) |
1382 | alignStruct = align; |
1383 | } |
1384 | return alignStruct; |
1385 | } |
1386 | |
1387 | auto *FTy = dyn_cast<FunctionType>(Ty); |
1388 | if (FTy) |
1389 | return DL.getPointerPrefAlignment().value(); |
1390 | return DL.getPrefTypeAlignment(Ty); |
1391 | } |
1392 | |
1393 | void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, |
1394 | int paramIndex, raw_ostream &O) { |
1395 | getSymbol(I->getParent())->print(O, MAI); |
1396 | O << "_param_" << paramIndex; |
1397 | } |
1398 | |
1399 | void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) { |
1400 | const DataLayout &DL = getDataLayout(); |
1401 | const AttributeList &PAL = F->getAttributes(); |
1402 | const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F); |
1403 | const TargetLowering *TLI = STI.getTargetLowering(); |
1404 | Function::const_arg_iterator I, E; |
1405 | unsigned paramIndex = 0; |
1406 | bool first = true; |
1407 | bool isKernelFunc = isKernelFunction(*F); |
1408 | bool isABI = (STI.getSmVersion() >= 20); |
| 13 | | Assuming the condition is false | |
|
1409 | bool hasImageHandles = STI.hasImageHandles(); |
1410 | MVT thePointerTy = TLI->getPointerTy(DL); |
1411 | |
1412 | if (F->arg_empty()) { |
| 14 | | Assuming the condition is false | |
|
| |
1413 | O << "()\n"; |
1414 | return; |
1415 | } |
1416 | |
1417 | O << "(\n"; |
1418 | |
1419 | for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) { |
| 16 | | Assuming 'I' is not equal to 'E' | |
|
| 17 | | Loop condition is true. Entering loop body | |
|
1420 | Type *Ty = I->getType(); |
1421 | |
1422 | if (!first) |
| |
1423 | O << ",\n"; |
1424 | |
1425 | first = false; |
1426 | |
1427 | |
1428 | if (isKernelFunction(*F)) { |
| 19 | | Assuming the condition is false | |
|
| |
1429 | if (isSampler(*I) || isImage(*I)) { |
1430 | if (isImage(*I)) { |
1431 | std::string sname = std::string(I->getName()); |
1432 | if (isImageWriteOnly(*I) || isImageReadWrite(*I)) { |
1433 | if (hasImageHandles) |
1434 | O << "\t.param .u64 .ptr .surfref "; |
1435 | else |
1436 | O << "\t.param .surfref "; |
1437 | CurrentFnSym->print(O, MAI); |
1438 | O << "_param_" << paramIndex; |
1439 | } |
1440 | else { |
1441 | if (hasImageHandles) |
1442 | O << "\t.param .u64 .ptr .texref "; |
1443 | else |
1444 | O << "\t.param .texref "; |
1445 | CurrentFnSym->print(O, MAI); |
1446 | O << "_param_" << paramIndex; |
1447 | } |
1448 | } else { |
1449 | if (hasImageHandles) |
1450 | O << "\t.param .u64 .ptr .samplerref "; |
1451 | else |
1452 | O << "\t.param .samplerref "; |
1453 | CurrentFnSym->print(O, MAI); |
1454 | O << "_param_" << paramIndex; |
1455 | } |
1456 | continue; |
1457 | } |
1458 | } |
1459 | |
1460 | if (!PAL.hasParamAttr(paramIndex, Attribute::ByVal)) { |
| 21 | | Assuming the condition is false | |
|
| |
1461 | if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
1462 | |
1463 | |
1464 | |
1465 | const Align align = DL.getValueOrABITypeAlignment( |
1466 | PAL.getParamAlignment(paramIndex), Ty); |
1467 | |
1468 | unsigned sz = DL.getTypeAllocSize(Ty); |
1469 | O << "\t.param .align " << align.value() << " .b8 "; |
1470 | printParamName(I, paramIndex, O); |
1471 | O << "[" << sz << "]"; |
1472 | |
1473 | continue; |
1474 | } |
1475 | |
1476 | auto *PTy = dyn_cast<PointerType>(Ty); |
1477 | if (isKernelFunc) { |
1478 | if (PTy) { |
1479 | |
1480 | O << "\t.param .u" << thePointerTy.getSizeInBits() << " "; |
1481 | |
1482 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() != |
1483 | NVPTX::CUDA) { |
1484 | Type *ETy = PTy->getElementType(); |
1485 | int addrSpace = PTy->getAddressSpace(); |
1486 | switch (addrSpace) { |
1487 | default: |
1488 | O << ".ptr "; |
1489 | break; |
1490 | case ADDRESS_SPACE_CONST: |
1491 | O << ".ptr .const "; |
1492 | break; |
1493 | case ADDRESS_SPACE_SHARED: |
1494 | O << ".ptr .shared "; |
1495 | break; |
1496 | case ADDRESS_SPACE_GLOBAL: |
1497 | O << ".ptr .global "; |
1498 | break; |
1499 | } |
1500 | O << ".align " << (int)getOpenCLAlignment(DL, ETy) << " "; |
1501 | } |
1502 | printParamName(I, paramIndex, O); |
1503 | continue; |
1504 | } |
1505 | |
1506 | |
1507 | O << "\t.param ."; |
1508 | |
1509 | if (Ty->isIntegerTy(1)) |
1510 | O << "u8"; |
1511 | else |
1512 | O << getPTXFundamentalTypeStr(Ty); |
1513 | O << " "; |
1514 | printParamName(I, paramIndex, O); |
1515 | continue; |
1516 | } |
1517 | |
1518 | |
1519 | unsigned sz = 0; |
1520 | if (isa<IntegerType>(Ty)) { |
1521 | sz = cast<IntegerType>(Ty)->getBitWidth(); |
1522 | if (sz < 32) |
1523 | sz = 32; |
1524 | } else if (isa<PointerType>(Ty)) |
1525 | sz = thePointerTy.getSizeInBits(); |
1526 | else if (Ty->isHalfTy()) |
1527 | |
1528 | |
1529 | |
1530 | sz = 32; |
1531 | else |
1532 | sz = Ty->getPrimitiveSizeInBits(); |
1533 | if (isABI) |
1534 | O << "\t.param .b" << sz << " "; |
1535 | else |
1536 | O << "\t.reg .b" << sz << " "; |
1537 | printParamName(I, paramIndex, O); |
1538 | continue; |
1539 | } |
1540 | |
1541 | |
1542 | auto *PTy = dyn_cast<PointerType>(Ty); |
| 23 | | Assuming 'Ty' is not a 'PointerType' | |
|
| 24 | | 'PTy' initialized to a null pointer value | |
|
1543 | assert(PTy && "Param with byval attribute should be a pointer type"); |
1544 | Type *ETy = PTy->getElementType(); |
| 25 | | Called C++ object pointer is null |
|
1545 | |
1546 | if (isABI || isKernelFunc) { |
1547 | |
1548 | |
1549 | |
1550 | Align align = |
1551 | DL.getValueOrABITypeAlignment(PAL.getParamAlignment(paramIndex), ETy); |
1552 | |
1553 | |
1554 | |
1555 | |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | |
1561 | |
1562 | |
1563 | if (!isKernelFunc && align < Align(4)) |
1564 | align = Align(4); |
1565 | unsigned sz = DL.getTypeAllocSize(ETy); |
1566 | O << "\t.param .align " << align.value() << " .b8 "; |
1567 | printParamName(I, paramIndex, O); |
1568 | O << "[" << sz << "]"; |
1569 | continue; |
1570 | } else { |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | SmallVector<EVT, 16> vtparts; |
1576 | ComputeValueVTs(*TLI, DL, ETy, vtparts); |
1577 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
1578 | unsigned elems = 1; |
1579 | EVT elemtype = vtparts[i]; |
1580 | if (vtparts[i].isVector()) { |
1581 | elems = vtparts[i].getVectorNumElements(); |
1582 | elemtype = vtparts[i].getVectorElementType(); |
1583 | } |
1584 | |
1585 | for (unsigned j = 0, je = elems; j != je; ++j) { |
1586 | unsigned sz = elemtype.getSizeInBits(); |
1587 | if (elemtype.isInteger() && (sz < 32)) |
1588 | sz = 32; |
1589 | O << "\t.reg .b" << sz << " "; |
1590 | printParamName(I, paramIndex, O); |
1591 | if (j < je - 1) |
1592 | O << ",\n"; |
1593 | ++paramIndex; |
1594 | } |
1595 | if (i < e - 1) |
1596 | O << ",\n"; |
1597 | } |
1598 | --paramIndex; |
1599 | continue; |
1600 | } |
1601 | } |
1602 | |
1603 | O << "\n)\n"; |
1604 | } |
1605 | |
1606 | void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF, |
1607 | raw_ostream &O) { |
1608 | const Function &F = MF.getFunction(); |
1609 | emitFunctionParamList(&F, O); |
1610 | } |
1611 | |
1612 | void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters( |
1613 | const MachineFunction &MF) { |
1614 | SmallString<128> Str; |
1615 | raw_svector_ostream O(Str); |
1616 | |
1617 | |
1618 | |
1619 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
1620 | |
1621 | |
1622 | |
1623 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
1624 | int NumBytes = (int) MFI.getStackSize(); |
1625 | if (NumBytes) { |
1626 | O << "\t.local .align " << MFI.getMaxAlign().value() << " .b8 \t" |
1627 | << DEPOTNAME << getFunctionNumber() << "[" << NumBytes << "];\n"; |
1628 | if (static_cast<const NVPTXTargetMachine &>(MF.getTarget()).is64Bit()) { |
1629 | O << "\t.reg .b64 \t%SP;\n"; |
1630 | O << "\t.reg .b64 \t%SPL;\n"; |
1631 | } else { |
1632 | O << "\t.reg .b32 \t%SP;\n"; |
1633 | O << "\t.reg .b32 \t%SPL;\n"; |
1634 | } |
1635 | } |
1636 | |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | unsigned int numVRs = MRI->getNumVirtRegs(); |
1642 | for (unsigned i = 0; i < numVRs; i++) { |
1643 | unsigned int vr = Register::index2VirtReg(i); |
1644 | const TargetRegisterClass *RC = MRI->getRegClass(vr); |
1645 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
1646 | int n = regmap.size(); |
1647 | regmap.insert(std::make_pair(vr, n + 1)); |
1648 | } |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | |
1662 | for (unsigned i=0; i< TRI->getNumRegClasses(); i++) { |
1663 | const TargetRegisterClass *RC = TRI->getRegClass(i); |
1664 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
1665 | std::string rcname = getNVPTXRegClassName(RC); |
1666 | std::string rcStr = getNVPTXRegClassStr(RC); |
1667 | int n = regmap.size(); |
1668 | |
1669 | |
1670 | if (n) { |
1671 | O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1) |
1672 | << ">;\n"; |
1673 | } |
1674 | } |
1675 | |
1676 | OutStreamer->emitRawText(O.str()); |
1677 | } |
1678 | |
1679 | void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) { |
1680 | APFloat APF = APFloat(Fp->getValueAPF()); |
1681 | bool ignored; |
1682 | unsigned int numHex; |
1683 | const char *lead; |
1684 | |
1685 | if (Fp->getType()->getTypeID() == Type::FloatTyID) { |
1686 | numHex = 8; |
1687 | lead = "0f"; |
1688 | APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &ignored); |
1689 | } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) { |
1690 | numHex = 16; |
1691 | lead = "0d"; |
1692 | APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &ignored); |
1693 | } else |
1694 | llvm_unreachable("unsupported fp type"); |
1695 | |
1696 | APInt API = APF.bitcastToAPInt(); |
1697 | O << lead << format_hex_no_prefix(API.getZExtValue(), numHex, true); |
1698 | } |
1699 | |
1700 | void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) { |
1701 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
1702 | O << CI->getValue(); |
1703 | return; |
1704 | } |
1705 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
1706 | printFPConstant(CFP, O); |
1707 | return; |
1708 | } |
1709 | if (isa<ConstantPointerNull>(CPV)) { |
1710 | O << "0"; |
1711 | return; |
1712 | } |
1713 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
1714 | bool IsNonGenericPointer = false; |
1715 | if (GVar->getType()->getAddressSpace() != 0) { |
1716 | IsNonGenericPointer = true; |
1717 | } |
1718 | if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) { |
1719 | O << "generic("; |
1720 | getSymbol(GVar)->print(O, MAI); |
1721 | O << ")"; |
1722 | } else { |
1723 | getSymbol(GVar)->print(O, MAI); |
1724 | } |
1725 | return; |
1726 | } |
1727 | if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1728 | const Value *v = Cexpr->stripPointerCasts(); |
1729 | PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType()); |
1730 | bool IsNonGenericPointer = false; |
1731 | if (PTy && PTy->getAddressSpace() != 0) { |
1732 | IsNonGenericPointer = true; |
1733 | } |
1734 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
1735 | if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) { |
1736 | O << "generic("; |
1737 | getSymbol(GVar)->print(O, MAI); |
1738 | O << ")"; |
1739 | } else { |
1740 | getSymbol(GVar)->print(O, MAI); |
1741 | } |
1742 | return; |
1743 | } else { |
1744 | lowerConstant(CPV)->print(O, MAI); |
1745 | return; |
1746 | } |
1747 | } |
1748 | llvm_unreachable("Not scalar type found in printScalarConstant()"); |
1749 | } |
1750 | |
1751 | |
1752 | |
1753 | template <typename T> static void ConvertIntToBytes(unsigned char *p, T val) { |
1754 | int64_t vp = (int64_t)val; |
1755 | for (unsigned i = 0; i < sizeof(T); ++i) { |
1756 | p[i] = (unsigned char)vp; |
1757 | vp >>= 8; |
1758 | } |
1759 | } |
1760 | static void ConvertFloatToBytes(unsigned char *p, float val) { |
1761 | int32_t *vp = (int32_t *)&val; |
1762 | for (unsigned i = 0; i < sizeof(int32_t); ++i) { |
1763 | p[i] = (unsigned char)*vp; |
1764 | *vp >>= 8; |
1765 | } |
1766 | } |
1767 | static void ConvertDoubleToBytes(unsigned char *p, double val) { |
1768 | int64_t *vp = (int64_t *)&val; |
1769 | for (unsigned i = 0; i < sizeof(int64_t); ++i) { |
1770 | p[i] = (unsigned char)*vp; |
1771 | *vp >>= 8; |
1772 | } |
1773 | } |
1774 | |
1775 | void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, |
1776 | AggBuffer *aggBuffer) { |
1777 | const DataLayout &DL = getDataLayout(); |
1778 | |
1779 | if (isa<UndefValue>(CPV) || CPV->isNullValue()) { |
1780 | int s = DL.getTypeAllocSize(CPV->getType()); |
1781 | if (s < Bytes) |
1782 | s = Bytes; |
1783 | aggBuffer->addZeros(s); |
1784 | return; |
1785 | } |
1786 | |
1787 | unsigned char ptr[8]; |
1788 | switch (CPV->getType()->getTypeID()) { |
1789 | |
1790 | case Type::IntegerTyID: { |
1791 | Type *ETy = CPV->getType(); |
1792 | if (ETy == Type::getInt8Ty(CPV->getContext())) { |
1793 | unsigned char c = (unsigned char)cast<ConstantInt>(CPV)->getZExtValue(); |
1794 | ConvertIntToBytes<>(ptr, c); |
1795 | aggBuffer->addBytes(ptr, 1, Bytes); |
1796 | } else if (ETy == Type::getInt16Ty(CPV->getContext())) { |
1797 | short int16 = (short)cast<ConstantInt>(CPV)->getZExtValue(); |
1798 | ConvertIntToBytes<>(ptr, int16); |
1799 | aggBuffer->addBytes(ptr, 2, Bytes); |
1800 | } else if (ETy == Type::getInt32Ty(CPV->getContext())) { |
1801 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
1802 | int int32 = (int)(constInt->getZExtValue()); |
1803 | ConvertIntToBytes<>(ptr, int32); |
1804 | aggBuffer->addBytes(ptr, 4, Bytes); |
1805 | break; |
1806 | } else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1807 | if (const auto *constInt = dyn_cast<ConstantInt>( |
1808 | ConstantFoldConstant(Cexpr, DL))) { |
1809 | int int32 = (int)(constInt->getZExtValue()); |
1810 | ConvertIntToBytes<>(ptr, int32); |
1811 | aggBuffer->addBytes(ptr, 4, Bytes); |
1812 | break; |
1813 | } |
1814 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
1815 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
1816 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
1817 | aggBuffer->addZeros(4); |
1818 | break; |
1819 | } |
1820 | } |
1821 | llvm_unreachable("unsupported integer const type"); |
1822 | } else if (ETy == Type::getInt64Ty(CPV->getContext())) { |
1823 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
1824 | long long int64 = (long long)(constInt->getZExtValue()); |
1825 | ConvertIntToBytes<>(ptr, int64); |
1826 | aggBuffer->addBytes(ptr, 8, Bytes); |
1827 | break; |
1828 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1829 | if (const auto *constInt = dyn_cast<ConstantInt>( |
1830 | ConstantFoldConstant(Cexpr, DL))) { |
1831 | long long int64 = (long long)(constInt->getZExtValue()); |
1832 | ConvertIntToBytes<>(ptr, int64); |
1833 | aggBuffer->addBytes(ptr, 8, Bytes); |
1834 | break; |
1835 | } |
1836 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
1837 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
1838 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
1839 | aggBuffer->addZeros(8); |
1840 | break; |
1841 | } |
1842 | } |
1843 | llvm_unreachable("unsupported integer const type"); |
1844 | } else |
1845 | llvm_unreachable("unsupported integer const type"); |
1846 | break; |
1847 | } |
1848 | case Type::HalfTyID: |
1849 | case Type::FloatTyID: |
1850 | case Type::DoubleTyID: { |
1851 | const auto *CFP = cast<ConstantFP>(CPV); |
1852 | Type *Ty = CFP->getType(); |
1853 | if (Ty == Type::getHalfTy(CPV->getContext())) { |
1854 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
1855 | uint16_t float16 = API.getLoBits(16).getZExtValue(); |
1856 | ConvertIntToBytes<>(ptr, float16); |
1857 | aggBuffer->addBytes(ptr, 2, Bytes); |
1858 | } else if (Ty == Type::getFloatTy(CPV->getContext())) { |
1859 | float float32 = (float) CFP->getValueAPF().convertToFloat(); |
1860 | ConvertFloatToBytes(ptr, float32); |
1861 | aggBuffer->addBytes(ptr, 4, Bytes); |
1862 | } else if (Ty == Type::getDoubleTy(CPV->getContext())) { |
1863 | double float64 = CFP->getValueAPF().convertToDouble(); |
1864 | ConvertDoubleToBytes(ptr, float64); |
1865 | aggBuffer->addBytes(ptr, 8, Bytes); |
1866 | } else { |
1867 | llvm_unreachable("unsupported fp const type"); |
1868 | } |
1869 | break; |
1870 | } |
1871 | case Type::PointerTyID: { |
1872 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
1873 | aggBuffer->addSymbol(GVar, GVar); |
1874 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1875 | const Value *v = Cexpr->stripPointerCasts(); |
1876 | aggBuffer->addSymbol(v, Cexpr); |
1877 | } |
1878 | unsigned int s = DL.getTypeAllocSize(CPV->getType()); |
1879 | aggBuffer->addZeros(s); |
1880 | break; |
1881 | } |
1882 | |
1883 | case Type::ArrayTyID: |
1884 | case Type::FixedVectorTyID: |
1885 | case Type::StructTyID: { |
1886 | if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) { |
1887 | int ElementSize = DL.getTypeAllocSize(CPV->getType()); |
1888 | bufferAggregateConstant(CPV, aggBuffer); |
1889 | if (Bytes > ElementSize) |
1890 | aggBuffer->addZeros(Bytes - ElementSize); |
1891 | } else if (isa<ConstantAggregateZero>(CPV)) |
1892 | aggBuffer->addZeros(Bytes); |
1893 | else |
1894 | llvm_unreachable("Unexpected Constant type"); |
1895 | break; |
1896 | } |
1897 | |
1898 | default: |
1899 | llvm_unreachable("unsupported type"); |
1900 | } |
1901 | } |
1902 | |
1903 | void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV, |
1904 | AggBuffer *aggBuffer) { |
1905 | const DataLayout &DL = getDataLayout(); |
1906 | int Bytes; |
1907 | |
1908 | |
1909 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
1910 | APInt Val = CI->getValue(); |
1911 | for (unsigned I = 0, E = DL.getTypeAllocSize(CPV->getType()); I < E; ++I) { |
1912 | uint8_t Byte = Val.getLoBits(8).getZExtValue(); |
1913 | aggBuffer->addBytes(&Byte, 1, 1); |
1914 | Val.lshrInPlace(8); |
1915 | } |
1916 | return; |
1917 | } |
1918 | |
1919 | |
1920 | if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) { |
1921 | if (CPV->getNumOperands()) |
1922 | for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) |
1923 | bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer); |
1924 | return; |
1925 | } |
1926 | |
1927 | if (const ConstantDataSequential *CDS = |
1928 | dyn_cast<ConstantDataSequential>(CPV)) { |
1929 | if (CDS->getNumElements()) |
1930 | for (unsigned i = 0; i < CDS->getNumElements(); ++i) |
1931 | bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0, |
1932 | aggBuffer); |
1933 | return; |
1934 | } |
1935 | |
1936 | if (isa<ConstantStruct>(CPV)) { |
1937 | if (CPV->getNumOperands()) { |
1938 | StructType *ST = cast<StructType>(CPV->getType()); |
1939 | for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) { |
1940 | if (i == (e - 1)) |
1941 | Bytes = DL.getStructLayout(ST)->getElementOffset(0) + |
1942 | DL.getTypeAllocSize(ST) - |
1943 | DL.getStructLayout(ST)->getElementOffset(i); |
1944 | else |
1945 | Bytes = DL.getStructLayout(ST)->getElementOffset(i + 1) - |
1946 | DL.getStructLayout(ST)->getElementOffset(i); |
1947 | bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer); |
1948 | } |
1949 | } |
1950 | return; |
1951 | } |
1952 | llvm_unreachable("unsupported constant type in printAggregateConstant()"); |
1953 | } |
1954 | |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | const MCExpr * |
1960 | NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric) { |
1961 | MCContext &Ctx = OutContext; |
1962 | |
1963 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
1964 | return MCConstantExpr::create(0, Ctx); |
1965 | |
1966 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) |
1967 | return MCConstantExpr::create(CI->getZExtValue(), Ctx); |
1968 | |
1969 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
1970 | const MCSymbolRefExpr *Expr = |
1971 | MCSymbolRefExpr::create(getSymbol(GV), Ctx); |
1972 | if (ProcessingGeneric) { |
1973 | return NVPTXGenericMCSymbolRefExpr::create(Expr, Ctx); |
1974 | } else { |
1975 | return Expr; |
1976 | } |
1977 | } |
1978 | |
1979 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); |
1980 | if (!CE) { |
1981 | llvm_unreachable("Unknown constant value to lower!"); |
1982 | } |
1983 | |
1984 | switch (CE->getOpcode()) { |
1985 | default: { |
1986 | |
1987 | |
1988 | |
1989 | Constant *C = ConstantFoldConstant(CE, getDataLayout()); |
1990 | if (C != CE) |
1991 | return lowerConstantForGV(C, ProcessingGeneric); |
1992 | |
1993 | |
1994 | std::string S; |
1995 | raw_string_ostream OS(S); |
1996 | OS << "Unsupported expression in static initializer: "; |
1997 | CE->printAsOperand(OS, false, |
1998 | !MF ? nullptr : MF->getFunction().getParent()); |
1999 | report_fatal_error(OS.str()); |
2000 | } |
2001 | |
2002 | case Instruction::AddrSpaceCast: { |
2003 | |
2004 | PointerType *DstTy = cast<PointerType>(CE->getType()); |
2005 | if (DstTy->getAddressSpace() == 0) { |
2006 | return lowerConstantForGV(cast<const Constant>(CE->getOperand(0)), true); |
2007 | } |
2008 | std::string S; |
2009 | raw_string_ostream OS(S); |
2010 | OS << "Unsupported expression in static initializer: "; |
2011 | CE->printAsOperand(OS, false, |
2012 | !MF ? nullptr : MF->getFunction().getParent()); |
2013 | report_fatal_error(OS.str()); |
2014 | } |
2015 | |
2016 | case Instruction::GetElementPtr: { |
2017 | const DataLayout &DL = getDataLayout(); |
2018 | |
2019 | |
2020 | APInt OffsetAI(DL.getPointerTypeSizeInBits(CE->getType()), 0); |
2021 | cast<GEPOperator>(CE)->accumulateConstantOffset(DL, OffsetAI); |
2022 | |
2023 | const MCExpr *Base = lowerConstantForGV(CE->getOperand(0), |
2024 | ProcessingGeneric); |
2025 | if (!OffsetAI) |
2026 | return Base; |
2027 | |
2028 | int64_t Offset = OffsetAI.getSExtValue(); |
2029 | return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx), |
2030 | Ctx); |
2031 | } |
2032 | |
2033 | case Instruction::Trunc: |
2034 | |
2035 | |
2036 | |
2037 | |
2038 | LLVM_FALLTHROUGH; |
2039 | case Instruction::BitCast: |
2040 | return lowerConstantForGV(CE->getOperand(0), ProcessingGeneric); |
2041 | |
2042 | case Instruction::IntToPtr: { |
2043 | const DataLayout &DL = getDataLayout(); |
2044 | |
2045 | |
2046 | |
2047 | Constant *Op = CE->getOperand(0); |
2048 | Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()), |
2049 | false); |
2050 | return lowerConstantForGV(Op, ProcessingGeneric); |
2051 | } |
2052 | |
2053 | case Instruction::PtrToInt: { |
2054 | const DataLayout &DL = getDataLayout(); |
2055 | |
2056 | |
2057 | |
2058 | Constant *Op = CE->getOperand(0); |
2059 | Type *Ty = CE->getType(); |
2060 | |
2061 | const MCExpr *OpExpr = lowerConstantForGV(Op, ProcessingGeneric); |
2062 | |
2063 | |
2064 | |
2065 | if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType())) |
2066 | return OpExpr; |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType()); |
2072 | const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx); |
2073 | return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx); |
2074 | } |
2075 | |
2076 | |
2077 | |
2078 | case Instruction::Add: { |
2079 | const MCExpr *LHS = lowerConstantForGV(CE->getOperand(0), ProcessingGeneric); |
2080 | const MCExpr *RHS = lowerConstantForGV(CE->getOperand(1), ProcessingGeneric); |
2081 | switch (CE->getOpcode()) { |
2082 | default: llvm_unreachable("Unknown binary operator constant cast expr"); |
2083 | case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx); |
2084 | } |
2085 | } |
2086 | } |
2087 | } |
2088 | |
2089 | |
2090 | void NVPTXAsmPrinter::printMCExpr(const MCExpr &Expr, raw_ostream &OS) { |
2091 | switch (Expr.getKind()) { |
2092 | case MCExpr::Target: |
2093 | return cast<MCTargetExpr>(&Expr)->printImpl(OS, MAI); |
2094 | case MCExpr::Constant: |
2095 | OS << cast<MCConstantExpr>(Expr).getValue(); |
2096 | return; |
2097 | |
2098 | case MCExpr::SymbolRef: { |
2099 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Expr); |
2100 | const MCSymbol &Sym = SRE.getSymbol(); |
2101 | Sym.print(OS, MAI); |
2102 | return; |
2103 | } |
2104 | |
2105 | case MCExpr::Unary: { |
2106 | const MCUnaryExpr &UE = cast<MCUnaryExpr>(Expr); |
2107 | switch (UE.getOpcode()) { |
2108 | case MCUnaryExpr::LNot: OS << '!'; break; |
2109 | case MCUnaryExpr::Minus: OS << '-'; break; |
2110 | case MCUnaryExpr::Not: OS << '~'; break; |
2111 | case MCUnaryExpr::Plus: OS << '+'; break; |
2112 | } |
2113 | printMCExpr(*UE.getSubExpr(), OS); |
2114 | return; |
2115 | } |
2116 | |
2117 | case MCExpr::Binary: { |
2118 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr); |
2119 | |
2120 | |
2121 | if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS()) || |
2122 | isa<NVPTXGenericMCSymbolRefExpr>(BE.getLHS())) { |
2123 | printMCExpr(*BE.getLHS(), OS); |
2124 | } else { |
2125 | OS << '('; |
2126 | printMCExpr(*BE.getLHS(), OS); |
2127 | OS<< ')'; |
2128 | } |
2129 | |
2130 | switch (BE.getOpcode()) { |
2131 | case MCBinaryExpr::Add: |
2132 | |
2133 | if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { |
2134 | if (RHSC->getValue() < 0) { |
2135 | OS << RHSC->getValue(); |
2136 | return; |
2137 | } |
2138 | } |
2139 | |
2140 | OS << '+'; |
2141 | break; |
2142 | default: llvm_unreachable("Unhandled binary operator"); |
2143 | } |
2144 | |
2145 | |
2146 | if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { |
2147 | printMCExpr(*BE.getRHS(), OS); |
2148 | } else { |
2149 | OS << '('; |
2150 | printMCExpr(*BE.getRHS(), OS); |
2151 | OS << ')'; |
2152 | } |
2153 | return; |
2154 | } |
2155 | } |
2156 | |
2157 | llvm_unreachable("Invalid expression kind!"); |
2158 | } |
2159 | |
2160 | |
2161 | |
2162 | bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
2163 | const char *ExtraCode, raw_ostream &O) { |
2164 | if (ExtraCode && ExtraCode[0]) { |
2165 | if (ExtraCode[1] != 0) |
2166 | return true; |
2167 | |
2168 | switch (ExtraCode[0]) { |
2169 | default: |
2170 | |
2171 | return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O); |
2172 | case 'r': |
2173 | break; |
2174 | } |
2175 | } |
2176 | |
2177 | printOperand(MI, OpNo, O); |
2178 | |
2179 | return false; |
2180 | } |
2181 | |
2182 | bool NVPTXAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
2183 | unsigned OpNo, |
2184 | const char *ExtraCode, |
2185 | raw_ostream &O) { |
2186 | if (ExtraCode && ExtraCode[0]) |
2187 | return true; |
2188 | |
2189 | O << '['; |
2190 | printMemOperand(MI, OpNo, O); |
2191 | O << ']'; |
2192 | |
2193 | return false; |
2194 | } |
2195 | |
2196 | void NVPTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
2197 | raw_ostream &O) { |
2198 | const MachineOperand &MO = MI->getOperand(opNum); |
2199 | switch (MO.getType()) { |
2200 | case MachineOperand::MO_Register: |
2201 | if (Register::isPhysicalRegister(MO.getReg())) { |
2202 | if (MO.getReg() == NVPTX::VRDepot) |
2203 | O << DEPOTNAME << getFunctionNumber(); |
2204 | else |
2205 | O << NVPTXInstPrinter::getRegisterName(MO.getReg()); |
2206 | } else { |
2207 | emitVirtualRegister(MO.getReg(), O); |
2208 | } |
2209 | break; |
2210 | |
2211 | case MachineOperand::MO_Immediate: |
2212 | O << MO.getImm(); |
2213 | break; |
2214 | |
2215 | case MachineOperand::MO_FPImmediate: |
2216 | printFPConstant(MO.getFPImm(), O); |
2217 | break; |
2218 | |
2219 | case MachineOperand::MO_GlobalAddress: |
2220 | PrintSymbolOperand(MO, O); |
2221 | break; |
2222 | |
2223 | case MachineOperand::MO_MachineBasicBlock: |
2224 | MO.getMBB()->getSymbol()->print(O, MAI); |
2225 | break; |
2226 | |
2227 | default: |
2228 | llvm_unreachable("Operand type not supported."); |
2229 | } |
2230 | } |
2231 | |
2232 | void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
2233 | raw_ostream &O, const char *Modifier) { |
2234 | printOperand(MI, opNum, O); |
2235 | |
2236 | if (Modifier && strcmp(Modifier, "add") == 0) { |
2237 | O << ", "; |
2238 | printOperand(MI, opNum + 1, O); |
2239 | } else { |
2240 | if (MI->getOperand(opNum + 1).isImm() && |
2241 | MI->getOperand(opNum + 1).getImm() == 0) |
2242 | return; |
2243 | O << "+"; |
2244 | printOperand(MI, opNum + 1, O); |
2245 | } |
2246 | } |
2247 | |
2248 | |
2249 | extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeNVPTXAsmPrinter() { |
2250 | RegisterAsmPrinter<NVPTXAsmPrinter> X(getTheNVPTXTarget32()); |
2251 | RegisterAsmPrinter<NVPTXAsmPrinter> Y(getTheNVPTXTarget64()); |
2252 | } |