| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===-- NVPTXAsmPrinter.cpp - NVPTX LLVM assembly writer ------------------===// |
| 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 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to NVPTX assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "NVPTXAsmPrinter.h" |
| Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "InstPrinter/NVPTXInstPrinter.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/NVPTXBaseInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/NVPTXMCAsmInfo.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 19 | #include "NVPTX.h" |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 20 | #include "NVPTXMCExpr.h" |
| Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "NVPTXMachineFunctionInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "NVPTXRegisterInfo.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 23 | #include "NVPTXSubtarget.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "NVPTXTargetMachine.h" |
| 25 | #include "NVPTXUtilities.h" |
| 26 | #include "cl_common_defines.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/APFloat.h" |
| 28 | #include "llvm/ADT/APInt.h" |
| 29 | #include "llvm/ADT/DenseMap.h" |
| 30 | #include "llvm/ADT/DenseSet.h" |
| 31 | #include "llvm/ADT/SmallString.h" |
| 32 | #include "llvm/ADT/SmallVector.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringRef.h" |
| 35 | #include "llvm/ADT/Triple.h" |
| 36 | #include "llvm/ADT/Twine.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/ConstantFolding.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/Analysis.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineFunction.h" |
| 42 | #include "llvm/CodeGen/MachineInstr.h" |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineOperand.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 46 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/TargetLowering.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/ValueTypes.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 50 | #include "llvm/IR/Attributes.h" |
| 51 | #include "llvm/IR/BasicBlock.h" |
| 52 | #include "llvm/IR/Constant.h" |
| 53 | #include "llvm/IR/Constants.h" |
| 54 | #include "llvm/IR/DataLayout.h" |
| Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 55 | #include "llvm/IR/DebugInfo.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 56 | #include "llvm/IR/DebugInfoMetadata.h" |
| 57 | #include "llvm/IR/DebugLoc.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 58 | #include "llvm/IR/DerivedTypes.h" |
| 59 | #include "llvm/IR/Function.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 60 | #include "llvm/IR/GlobalValue.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 61 | #include "llvm/IR/GlobalVariable.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 62 | #include "llvm/IR/Instruction.h" |
| 63 | #include "llvm/IR/LLVMContext.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 64 | #include "llvm/IR/Module.h" |
| 65 | #include "llvm/IR/Operator.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 66 | #include "llvm/IR/Type.h" |
| 67 | #include "llvm/IR/User.h" |
| 68 | #include "llvm/MC/MCExpr.h" |
| Pete Cooper | 81902a3 | 2015-05-15 22:19:42 +0000 | [diff] [blame] | 69 | #include "llvm/MC/MCInst.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 70 | #include "llvm/MC/MCInstrDesc.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 71 | #include "llvm/MC/MCStreamer.h" |
| 72 | #include "llvm/MC/MCSymbol.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 73 | #include "llvm/Support/Casting.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 74 | #include "llvm/Support/CommandLine.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 75 | #include "llvm/Support/ErrorHandling.h" |
| David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 76 | #include "llvm/Support/MachineValueType.h" |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Path.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 78 | #include "llvm/Support/TargetRegistry.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 79 | #include "llvm/Support/raw_ostream.h" |
| David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 80 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 81 | #include "llvm/Target/TargetMachine.h" |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/Utils/UnrollLoop.h" |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 83 | #include <cassert> |
| 84 | #include <cstdint> |
| 85 | #include <cstring> |
| 86 | #include <new> |
| Bill Wendling | e38859d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 87 | #include <sstream> |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 88 | #include <string> |
| 89 | #include <utility> |
| 90 | #include <vector> |
| 91 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 92 | using namespace llvm; |
| 93 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 94 | #define DEPOTNAME "__local_depot" |
| 95 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 96 | static cl::opt<bool> |
| 97 | EmitLineNumbers("nvptx-emit-line-numbers", cl::Hidden, |
| 98 | cl::desc("NVPTX Specific: Emit Line numbers even without -G"), |
| 99 | cl::init(true)); |
| 100 | |
| 101 | static cl::opt<bool> |
| 102 | InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, cl::Hidden, |
| 103 | cl::desc("NVPTX Specific: Emit source line in ptx file"), |
| 104 | cl::init(false)); |
| 105 | |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 106 | /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V |
| 107 | /// depends. |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 108 | static void |
| 109 | DiscoverDependentGlobals(const Value *V, |
| 110 | DenseSet<const GlobalVariable *> &Globals) { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 111 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 112 | Globals.insert(GV); |
| 113 | else { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 114 | if (const User *U = dyn_cast<User>(V)) { |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 115 | for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) { |
| 116 | DiscoverDependentGlobals(U->getOperand(i), Globals); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 121 | |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 122 | /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable |
| 123 | /// instances to be emitted, but only after any dependents have been added |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 124 | /// first.s |
| 125 | static void |
| 126 | VisitGlobalVariableForEmission(const GlobalVariable *GV, |
| 127 | SmallVectorImpl<const GlobalVariable *> &Order, |
| 128 | DenseSet<const GlobalVariable *> &Visited, |
| 129 | DenseSet<const GlobalVariable *> &Visiting) { |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 130 | // Have we already visited this one? |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 131 | if (Visited.count(GV)) |
| 132 | return; |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 133 | |
| 134 | // Do we have a circular dependency? |
| Benjamin Kramer | 2c99e41 | 2014-10-10 15:32:50 +0000 | [diff] [blame] | 135 | if (!Visiting.insert(GV).second) |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 136 | report_fatal_error("Circular dependency found in global variable set"); |
| 137 | |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 138 | // Make sure we visit all dependents first |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 139 | DenseSet<const GlobalVariable *> Others; |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 140 | for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i) |
| 141 | DiscoverDependentGlobals(GV->getOperand(i), Others); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 142 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 143 | for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(), |
| 144 | E = Others.end(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 145 | I != E; ++I) |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 146 | VisitGlobalVariableForEmission(*I, Order, Visited, Visiting); |
| 147 | |
| 148 | // Now we can visit ourself |
| 149 | Order.push_back(GV); |
| 150 | Visited.insert(GV); |
| 151 | Visiting.erase(GV); |
| 152 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 153 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 154 | void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) { |
| 155 | if (!EmitLineNumbers) |
| 156 | return; |
| 157 | if (ignoreLoc(MI)) |
| 158 | return; |
| 159 | |
| 160 | const DebugLoc &curLoc = MI.getDebugLoc(); |
| 161 | |
| 162 | if (!prevDebugLoc && !curLoc) |
| 163 | return; |
| 164 | |
| 165 | if (prevDebugLoc == curLoc) |
| 166 | return; |
| 167 | |
| 168 | prevDebugLoc = curLoc; |
| 169 | |
| 170 | if (!curLoc) |
| 171 | return; |
| 172 | |
| 173 | auto *Scope = cast_or_null<DIScope>(curLoc.getScope()); |
| 174 | if (!Scope) |
| 175 | return; |
| 176 | |
| 177 | StringRef fileName(Scope->getFilename()); |
| 178 | StringRef dirName(Scope->getDirectory()); |
| 179 | SmallString<128> FullPathName = dirName; |
| 180 | if (!dirName.empty() && !sys::path::is_absolute(fileName)) { |
| 181 | sys::path::append(FullPathName, fileName); |
| 182 | fileName = FullPathName; |
| 183 | } |
| 184 | |
| 185 | if (filenameMap.find(fileName) == filenameMap.end()) |
| 186 | return; |
| 187 | |
| 188 | // Emit the line from the source file. |
| 189 | if (InterleaveSrc) |
| 190 | this->emitSrcInText(fileName, curLoc.getLine()); |
| 191 | |
| 192 | std::stringstream temp; |
| 193 | temp << "\t.loc " << filenameMap[fileName] << " " << curLoc.getLine() |
| 194 | << " " << curLoc.getCol(); |
| 195 | OutStreamer->EmitRawText(temp.str()); |
| 196 | } |
| 197 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 198 | void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 199 | SmallString<128> Str; |
| 200 | raw_svector_ostream OS(Str); |
| 201 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) |
| 202 | emitLineNumberAsDotLoc(*MI); |
| 203 | |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 204 | MCInst Inst; |
| 205 | lowerToMCInst(MI, Inst); |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 206 | EmitToStreamer(*OutStreamer, Inst); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 209 | // Handle symbol backtracking for targets that do not support image handles |
| 210 | bool NVPTXAsmPrinter::lowerImageHandleOperand(const MachineInstr *MI, |
| 211 | unsigned OpNo, MCOperand &MCOp) { |
| 212 | const MachineOperand &MO = MI->getOperand(OpNo); |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 213 | const MCInstrDesc &MCID = MI->getDesc(); |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 214 | |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 215 | if (MCID.TSFlags & NVPTXII::IsTexFlag) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 216 | // This is a texture fetch, so operand 4 is a texref and operand 5 is |
| 217 | // a samplerref |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 218 | if (OpNo == 4 && MO.isImm()) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 219 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 220 | return true; |
| 221 | } |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 222 | if (OpNo == 5 && MO.isImm() && !(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 223 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | return false; |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 228 | } else if (MCID.TSFlags & NVPTXII::IsSuldMask) { |
| 229 | unsigned VecSize = |
| 230 | 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1); |
| 231 | |
| 232 | // For a surface load of vector size N, the Nth operand will be the surfref |
| 233 | if (OpNo == VecSize && MO.isImm()) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 234 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | return false; |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 239 | } else if (MCID.TSFlags & NVPTXII::IsSustFlag) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 240 | // This is a surface store, so operand 0 is a surfref |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 241 | if (OpNo == 0 && MO.isImm()) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 242 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | return false; |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 247 | } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 248 | // This is a query, so operand 1 is a surfref/texref |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 249 | if (OpNo == 1 && MO.isImm()) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 250 | lowerImageHandleSymbol(MO.getImm(), MCOp); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| Justin Holewinski | 9a2350e | 2014-07-17 11:59:04 +0000 | [diff] [blame] | 256 | |
| 257 | return false; |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void NVPTXAsmPrinter::lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp) { |
| 261 | // Ewwww |
| 262 | TargetMachine &TM = const_cast<TargetMachine&>(MF->getTarget()); |
| 263 | NVPTXTargetMachine &nvTM = static_cast<NVPTXTargetMachine&>(TM); |
| 264 | const NVPTXMachineFunctionInfo *MFI = MF->getInfo<NVPTXMachineFunctionInfo>(); |
| 265 | const char *Sym = MFI->getImageHandleSymbol(Index); |
| 266 | std::string *SymNamePtr = |
| 267 | nvTM.getManagedStrPool()->getManagedString(Sym); |
| Malcolm Parsons | 06ac79c | 2016-11-02 16:43:50 +0000 | [diff] [blame] | 268 | MCOp = GetSymbolRef(OutContext.getOrCreateSymbol(StringRef(*SymNamePtr))); |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 271 | void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) { |
| 272 | OutMI.setOpcode(MI->getOpcode()); |
| Justin Holewinski | 3d49e5c | 2013-11-15 12:30:04 +0000 | [diff] [blame] | 273 | // Special: Do not mangle symbol operand of CALL_PROTOTYPE |
| 274 | if (MI->getOpcode() == NVPTX::CALL_PROTOTYPE) { |
| 275 | const MachineOperand &MO = MI->getOperand(0); |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 276 | OutMI.addOperand(GetSymbolRef( |
| Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 277 | OutContext.getOrCreateSymbol(Twine(MO.getSymbolName())))); |
| Justin Holewinski | 3d49e5c | 2013-11-15 12:30:04 +0000 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 281 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 282 | const MachineOperand &MO = MI->getOperand(i); |
| 283 | |
| 284 | MCOperand MCOp; |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 285 | if (!nvptxSubtarget->hasImageHandles()) { |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 286 | if (lowerImageHandleOperand(MI, i, MCOp)) { |
| 287 | OutMI.addOperand(MCOp); |
| 288 | continue; |
| 289 | } |
| 290 | } |
| 291 | |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 292 | if (lowerOperand(MO, MCOp)) |
| 293 | OutMI.addOperand(MCOp); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | bool NVPTXAsmPrinter::lowerOperand(const MachineOperand &MO, |
| 298 | MCOperand &MCOp) { |
| 299 | switch (MO.getType()) { |
| 300 | default: llvm_unreachable("unknown operand type"); |
| 301 | case MachineOperand::MO_Register: |
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 302 | MCOp = MCOperand::createReg(encodeVirtualRegister(MO.getReg())); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 303 | break; |
| 304 | case MachineOperand::MO_Immediate: |
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 305 | MCOp = MCOperand::createImm(MO.getImm()); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 306 | break; |
| 307 | case MachineOperand::MO_MachineBasicBlock: |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 308 | MCOp = MCOperand::createExpr(MCSymbolRefExpr::create( |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 309 | MO.getMBB()->getSymbol(), OutContext)); |
| 310 | break; |
| 311 | case MachineOperand::MO_ExternalSymbol: |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 312 | MCOp = GetSymbolRef(GetExternalSymbolSymbol(MO.getSymbolName())); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 313 | break; |
| 314 | case MachineOperand::MO_GlobalAddress: |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 315 | MCOp = GetSymbolRef(getSymbol(MO.getGlobal())); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 316 | break; |
| 317 | case MachineOperand::MO_FPImmediate: { |
| 318 | const ConstantFP *Cnt = MO.getFPImm(); |
| Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 319 | const APFloat &Val = Cnt->getValueAPF(); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 320 | |
| 321 | switch (Cnt->getType()->getTypeID()) { |
| 322 | default: report_fatal_error("Unsupported FP type"); break; |
| Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 323 | case Type::HalfTyID: |
| 324 | MCOp = MCOperand::createExpr( |
| 325 | NVPTXFloatMCExpr::createConstantFPHalf(Val, OutContext)); |
| 326 | break; |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 327 | case Type::FloatTyID: |
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 328 | MCOp = MCOperand::createExpr( |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 329 | NVPTXFloatMCExpr::createConstantFPSingle(Val, OutContext)); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 330 | break; |
| 331 | case Type::DoubleTyID: |
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 332 | MCOp = MCOperand::createExpr( |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 333 | NVPTXFloatMCExpr::createConstantFPDouble(Val, OutContext)); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | unsigned NVPTXAsmPrinter::encodeVirtualRegister(unsigned Reg) { |
| Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 343 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 344 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 345 | |
| Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 346 | DenseMap<unsigned, unsigned> &RegMap = VRegMapping[RC]; |
| 347 | unsigned RegNum = RegMap[Reg]; |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 348 | |
| Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 349 | // Encode the register class in the upper 4 bits |
| 350 | // Must be kept in sync with NVPTXInstPrinter::printRegName |
| 351 | unsigned Ret = 0; |
| 352 | if (RC == &NVPTX::Int1RegsRegClass) { |
| 353 | Ret = (1 << 28); |
| 354 | } else if (RC == &NVPTX::Int16RegsRegClass) { |
| 355 | Ret = (2 << 28); |
| 356 | } else if (RC == &NVPTX::Int32RegsRegClass) { |
| 357 | Ret = (3 << 28); |
| 358 | } else if (RC == &NVPTX::Int64RegsRegClass) { |
| 359 | Ret = (4 << 28); |
| 360 | } else if (RC == &NVPTX::Float32RegsRegClass) { |
| 361 | Ret = (5 << 28); |
| 362 | } else if (RC == &NVPTX::Float64RegsRegClass) { |
| 363 | Ret = (6 << 28); |
| Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 364 | } else if (RC == &NVPTX::Float16RegsRegClass) { |
| 365 | Ret = (7 << 28); |
| Artem Belevich | 620db1f | 2017-02-23 22:38:24 +0000 | [diff] [blame] | 366 | } else if (RC == &NVPTX::Float16x2RegsRegClass) { |
| 367 | Ret = (8 << 28); |
| Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 368 | } else { |
| 369 | report_fatal_error("Bad register class"); |
| 370 | } |
| 371 | |
| 372 | // Insert the vreg number |
| 373 | Ret |= (RegNum & 0x0FFFFFFF); |
| 374 | return Ret; |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 375 | } else { |
| Justin Holewinski | 871ec93 | 2013-08-06 14:13:31 +0000 | [diff] [blame] | 376 | // Some special-use registers are actually physical registers. |
| 377 | // Encode this as the register class ID of 0 and the real register ID. |
| 378 | return Reg & 0x0FFFFFFF; |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 379 | } |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 382 | MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) { |
| Justin Holewinski | a2a63d2 | 2013-08-06 14:13:27 +0000 | [diff] [blame] | 383 | const MCExpr *Expr; |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 384 | Expr = MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, |
| Justin Holewinski | 8b24e1e | 2013-08-06 23:06:42 +0000 | [diff] [blame] | 385 | OutContext); |
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 386 | return MCOperand::createExpr(Expr); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 389 | void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) { |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 390 | const DataLayout &DL = getDataLayout(); |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 391 | const TargetLowering *TLI = nvptxSubtarget->getTargetLowering(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 392 | |
| 393 | Type *Ty = F->getReturnType(); |
| 394 | |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 395 | bool isABI = (nvptxSubtarget->getSmVersion() >= 20); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 396 | |
| 397 | if (Ty->getTypeID() == Type::VoidTyID) |
| 398 | return; |
| 399 | |
| 400 | O << " ("; |
| 401 | |
| 402 | if (isABI) { |
| Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 403 | if (Ty->isFloatingPointTy() || (Ty->isIntegerTy() && !Ty->isIntegerTy(128))) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 404 | unsigned size = 0; |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 405 | if (auto *ITy = dyn_cast<IntegerType>(Ty)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 406 | size = ITy->getBitWidth(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 407 | } else { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 408 | assert(Ty->isFloatingPointTy() && "Floating point type expected here"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 409 | size = Ty->getPrimitiveSizeInBits(); |
| 410 | } |
| Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 411 | // PTX ABI requires all scalar return values to be at least 32 |
| 412 | // bits in size. fp16 normally uses .b16 as its storage type in |
| 413 | // PTX, so its size must be adjusted here, too. |
| 414 | if (size < 32) |
| 415 | size = 32; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 416 | |
| 417 | O << ".param .b" << size << " func_retval0"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 418 | } else if (isa<PointerType>(Ty)) { |
| Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 419 | O << ".param .b" << TLI->getPointerTy(DL).getSizeInBits() |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 420 | << " func_retval0"; |
| Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 421 | } else if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 422 | unsigned totalsz = DL.getTypeAllocSize(Ty); |
| Artem Belevich | 74158b5 | 2016-07-20 18:39:52 +0000 | [diff] [blame] | 423 | unsigned retAlignment = 0; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 424 | if (!getAlign(*F, 0, retAlignment)) |
| Artem Belevich | 74158b5 | 2016-07-20 18:39:52 +0000 | [diff] [blame] | 425 | retAlignment = DL.getABITypeAlignment(Ty); |
| 426 | O << ".param .align " << retAlignment << " .b8 func_retval0[" << totalsz |
| 427 | << "]"; |
| Craig Topper | d3c02f1 | 2015-01-05 10:15:49 +0000 | [diff] [blame] | 428 | } else |
| 429 | llvm_unreachable("Unknown return type"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 430 | } else { |
| 431 | SmallVector<EVT, 16> vtparts; |
| Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 432 | ComputeValueVTs(*TLI, DL, Ty, vtparts); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 433 | unsigned idx = 0; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 434 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 435 | unsigned elems = 1; |
| 436 | EVT elemtype = vtparts[i]; |
| 437 | if (vtparts[i].isVector()) { |
| 438 | elems = vtparts[i].getVectorNumElements(); |
| 439 | elemtype = vtparts[i].getVectorElementType(); |
| 440 | } |
| 441 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 442 | for (unsigned j = 0, je = elems; j != je; ++j) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 443 | unsigned sz = elemtype.getSizeInBits(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 444 | if (elemtype.isInteger() && (sz < 32)) |
| 445 | sz = 32; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 446 | O << ".reg .b" << sz << " func_retval" << idx; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 447 | if (j < je - 1) |
| 448 | O << ", "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 449 | ++idx; |
| 450 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 451 | if (i < e - 1) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 452 | O << ", "; |
| 453 | } |
| 454 | } |
| 455 | O << ") "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF, |
| 459 | raw_ostream &O) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 460 | const Function &F = MF.getFunction(); |
| 461 | printReturnValStr(&F, O); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 464 | // Return true if MBB is the header of a loop marked with |
| 465 | // llvm.loop.unroll.disable. |
| Jingyue Wu | 5bbcdaa | 2015-02-03 17:57:38 +0000 | [diff] [blame] | 466 | // TODO: consider "#pragma unroll 1" which is equivalent to "#pragma nounroll". |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 467 | bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll( |
| 468 | const MachineBasicBlock &MBB) const { |
| 469 | MachineLoopInfo &LI = getAnalysis<MachineLoopInfo>(); |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 470 | // We insert .pragma "nounroll" only to the loop header. |
| Benjamin Kramer | db220db | 2015-06-02 15:28:27 +0000 | [diff] [blame] | 471 | if (!LI.isLoopHeader(&MBB)) |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 472 | return false; |
| 473 | |
| 474 | // llvm.loop.unroll.disable is marked on the back edges of a loop. Therefore, |
| 475 | // we iterate through each back edge of the loop with header MBB, and check |
| 476 | // whether its metadata contains llvm.loop.unroll.disable. |
| 477 | for (auto I = MBB.pred_begin(); I != MBB.pred_end(); ++I) { |
| 478 | const MachineBasicBlock *PMBB = *I; |
| 479 | if (LI.getLoopFor(PMBB) != LI.getLoopFor(&MBB)) { |
| 480 | // Edges from other loops to MBB are not back edges. |
| 481 | continue; |
| 482 | } |
| 483 | if (const BasicBlock *PBB = PMBB->getBasicBlock()) { |
| Duncan P. N. Exon Smith | 1d15a9f | 2016-03-25 00:35:38 +0000 | [diff] [blame] | 484 | if (MDNode *LoopID = |
| 485 | PBB->getTerminator()->getMetadata(LLVMContext::MD_loop)) { |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 486 | if (GetUnrollMetadata(LoopID, "llvm.loop.unroll.disable")) |
| 487 | return true; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | void NVPTXAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const { |
| 495 | AsmPrinter::EmitBasicBlockStart(MBB); |
| 496 | if (isLoopHeaderOfNoUnroll(MBB)) |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 497 | OutStreamer->EmitRawText(StringRef("\t.pragma \"nounroll\";\n")); |
| Jingyue Wu | 0220df0 | 2015-02-01 02:27:45 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 500 | void NVPTXAsmPrinter::EmitFunctionEntryLabel() { |
| 501 | SmallString<128> Str; |
| 502 | raw_svector_ostream O(Str); |
| 503 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 504 | if (!GlobalsEmitted) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 505 | emitGlobals(*MF->getFunction().getParent()); |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 506 | GlobalsEmitted = true; |
| 507 | } |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 508 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 509 | // Set up |
| 510 | MRI = &MF->getRegInfo(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 511 | F = &MF->getFunction(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 512 | emitLinkageDirective(F, O); |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 513 | if (isKernelFunction(*F)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 514 | O << ".entry "; |
| 515 | else { |
| 516 | O << ".func "; |
| 517 | printReturnValStr(*MF, O); |
| 518 | } |
| 519 | |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 520 | CurrentFnSym->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 521 | |
| 522 | emitFunctionParamList(*MF, O); |
| 523 | |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 524 | if (isKernelFunction(*F)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 525 | emitKernelFunctionDirectives(*F, O); |
| 526 | |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 527 | OutStreamer->EmitRawText(O.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 528 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 529 | prevDebugLoc = DebugLoc(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void NVPTXAsmPrinter::EmitFunctionBodyStart() { |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 533 | VRegMapping.clear(); |
| 534 | OutStreamer->EmitRawText(StringRef("{\n")); |
| 535 | setAndEmitFunctionVirtualRegisters(*MF); |
| 536 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 537 | SmallString<128> Str; |
| 538 | raw_svector_ostream O(Str); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 539 | emitDemotedVars(&MF->getFunction(), O); |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 540 | OutStreamer->EmitRawText(O.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void NVPTXAsmPrinter::EmitFunctionBodyEnd() { |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 544 | OutStreamer->EmitRawText(StringRef("}\n")); |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 545 | VRegMapping.clear(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 548 | void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const { |
| 549 | unsigned RegNo = MI->getOperand(0).getReg(); |
| Andrew Kaylor | 5c73e1f | 2015-03-24 23:37:10 +0000 | [diff] [blame] | 550 | if (TargetRegisterInfo::isVirtualRegister(RegNo)) { |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 551 | OutStreamer->AddComment(Twine("implicit-def: ") + |
| 552 | getVirtualRegisterName(RegNo)); |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 553 | } else { |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 554 | OutStreamer->AddComment(Twine("implicit-def: ") + |
| 555 | nvptxSubtarget->getRegisterInfo()->getName(RegNo)); |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 556 | } |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 557 | OutStreamer->AddBlankLine(); |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 560 | void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F, |
| 561 | raw_ostream &O) const { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 562 | // If the NVVM IR has some of reqntid* specified, then output |
| 563 | // the reqntid directive, and set the unspecified ones to 1. |
| 564 | // If none of reqntid* is specified, don't output reqntid directive. |
| 565 | unsigned reqntidx, reqntidy, reqntidz; |
| 566 | bool specified = false; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 567 | if (!getReqNTIDx(F, reqntidx)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 568 | reqntidx = 1; |
| 569 | else |
| 570 | specified = true; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 571 | if (!getReqNTIDy(F, reqntidy)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 572 | reqntidy = 1; |
| 573 | else |
| 574 | specified = true; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 575 | if (!getReqNTIDz(F, reqntidz)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 576 | reqntidz = 1; |
| 577 | else |
| 578 | specified = true; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 579 | |
| 580 | if (specified) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 581 | O << ".reqntid " << reqntidx << ", " << reqntidy << ", " << reqntidz |
| 582 | << "\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 583 | |
| 584 | // If the NVVM IR has some of maxntid* specified, then output |
| 585 | // the maxntid directive, and set the unspecified ones to 1. |
| 586 | // If none of maxntid* is specified, don't output maxntid directive. |
| 587 | unsigned maxntidx, maxntidy, maxntidz; |
| 588 | specified = false; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 589 | if (!getMaxNTIDx(F, maxntidx)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 590 | maxntidx = 1; |
| 591 | else |
| 592 | specified = true; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 593 | if (!getMaxNTIDy(F, maxntidy)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 594 | maxntidy = 1; |
| 595 | else |
| 596 | specified = true; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 597 | if (!getMaxNTIDz(F, maxntidz)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 598 | maxntidz = 1; |
| 599 | else |
| 600 | specified = true; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 601 | |
| 602 | if (specified) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 603 | O << ".maxntid " << maxntidx << ", " << maxntidy << ", " << maxntidz |
| 604 | << "\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 605 | |
| 606 | unsigned mincta; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 607 | if (getMinCTASm(F, mincta)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 608 | O << ".minnctapersm " << mincta << "\n"; |
| Justin Lebar | 19bf9d2 | 2016-12-14 22:32:50 +0000 | [diff] [blame] | 609 | |
| 610 | unsigned maxnreg; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 611 | if (getMaxNReg(F, maxnreg)) |
| Justin Lebar | 19bf9d2 | 2016-12-14 22:32:50 +0000 | [diff] [blame] | 612 | O << ".maxnreg " << maxnreg << "\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 615 | std::string |
| 616 | NVPTXAsmPrinter::getVirtualRegisterName(unsigned Reg) const { |
| 617 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 618 | |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 619 | std::string Name; |
| 620 | raw_string_ostream NameStr(Name); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 621 | |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 622 | VRegRCMap::const_iterator I = VRegMapping.find(RC); |
| 623 | assert(I != VRegMapping.end() && "Bad register class"); |
| 624 | const DenseMap<unsigned, unsigned> &RegMap = I->second; |
| 625 | |
| 626 | VRegMap::const_iterator VI = RegMap.find(Reg); |
| 627 | assert(VI != RegMap.end() && "Bad virtual register"); |
| 628 | unsigned MappedVR = VI->second; |
| 629 | |
| 630 | NameStr << getNVPTXRegClassStr(RC) << MappedVR; |
| 631 | |
| 632 | NameStr.flush(); |
| 633 | return Name; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 636 | void NVPTXAsmPrinter::emitVirtualRegister(unsigned int vr, |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 637 | raw_ostream &O) { |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 638 | O << getVirtualRegisterName(vr); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 641 | void NVPTXAsmPrinter::printVecModifiedImmediate( |
| 642 | const MachineOperand &MO, const char *Modifier, raw_ostream &O) { |
| 643 | static const char vecelem[] = { '0', '1', '2', '3', '0', '1', '2', '3' }; |
| 644 | int Imm = (int) MO.getImm(); |
| 645 | if (0 == strcmp(Modifier, "vecelem")) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 646 | O << "_" << vecelem[Imm]; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 647 | else if (0 == strcmp(Modifier, "vecv4comm1")) { |
| 648 | if ((Imm < 0) || (Imm > 3)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 649 | O << "//"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 650 | } else if (0 == strcmp(Modifier, "vecv4comm2")) { |
| 651 | if ((Imm < 4) || (Imm > 7)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 652 | O << "//"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 653 | } else if (0 == strcmp(Modifier, "vecv4pos")) { |
| 654 | if (Imm < 0) |
| 655 | Imm = 0; |
| 656 | O << "_" << vecelem[Imm % 4]; |
| 657 | } else if (0 == strcmp(Modifier, "vecv2comm1")) { |
| 658 | if ((Imm < 0) || (Imm > 1)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 659 | O << "//"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 660 | } else if (0 == strcmp(Modifier, "vecv2comm2")) { |
| 661 | if ((Imm < 2) || (Imm > 3)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 662 | O << "//"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 663 | } else if (0 == strcmp(Modifier, "vecv2pos")) { |
| 664 | if (Imm < 0) |
| 665 | Imm = 0; |
| 666 | O << "_" << vecelem[Imm % 2]; |
| 667 | } else |
| Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 668 | llvm_unreachable("Unknown Modifier on immediate operand"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 671 | void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 672 | emitLinkageDirective(F, O); |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 673 | if (isKernelFunction(*F)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 674 | O << ".entry "; |
| 675 | else |
| 676 | O << ".func "; |
| 677 | printReturnValStr(F, O); |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 678 | getSymbol(F)->print(O, MAI); |
| 679 | O << "\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 680 | emitFunctionParamList(F, O); |
| 681 | O << ";\n"; |
| 682 | } |
| 683 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 684 | static bool usedInGlobalVarDef(const Constant *C) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 685 | if (!C) |
| 686 | return false; |
| 687 | |
| 688 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| Jingyue Wu | 4be014a | 2015-07-31 05:09:47 +0000 | [diff] [blame] | 689 | return GV->getName() != "llvm.used"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 692 | for (const User *U : C->users()) |
| 693 | if (const Constant *C = dyn_cast<Constant>(U)) |
| 694 | if (usedInGlobalVarDef(C)) |
| 695 | return true; |
| 696 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 697 | return false; |
| 698 | } |
| 699 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 700 | static bool usedInOneFunc(const User *U, Function const *&oneFunc) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 701 | if (const GlobalVariable *othergv = dyn_cast<GlobalVariable>(U)) { |
| Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 702 | if (othergv->getName() == "llvm.used") |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
| 706 | if (const Instruction *instr = dyn_cast<Instruction>(U)) { |
| 707 | if (instr->getParent() && instr->getParent()->getParent()) { |
| 708 | const Function *curFunc = instr->getParent()->getParent(); |
| 709 | if (oneFunc && (curFunc != oneFunc)) |
| 710 | return false; |
| 711 | oneFunc = curFunc; |
| 712 | return true; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 713 | } else |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 717 | for (const User *UU : U->users()) |
| Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 718 | if (!usedInOneFunc(UU, oneFunc)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 719 | return false; |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 720 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 721 | return true; |
| 722 | } |
| 723 | |
| 724 | /* Find out if a global variable can be demoted to local scope. |
| 725 | * Currently, this is valid for CUDA shared variables, which have local |
| 726 | * scope and global lifetime. So the conditions to check are : |
| 727 | * 1. Is the global variable in shared address space? |
| 728 | * 2. Does it have internal linkage? |
| 729 | * 3. Is the global variable referenced only in one function? |
| 730 | */ |
| 731 | static bool canDemoteGlobalVar(const GlobalVariable *gv, Function const *&f) { |
| Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 732 | if (!gv->hasInternalLinkage()) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 733 | return false; |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 734 | PointerType *Pty = gv->getType(); |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 735 | if (Pty->getAddressSpace() != ADDRESS_SPACE_SHARED) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 736 | return false; |
| 737 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 738 | const Function *oneFunc = nullptr; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 739 | |
| 740 | bool flag = usedInOneFunc(gv, oneFunc); |
| Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 741 | if (!flag) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 742 | return false; |
| 743 | if (!oneFunc) |
| 744 | return false; |
| 745 | f = oneFunc; |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | static bool useFuncSeen(const Constant *C, |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 750 | DenseMap<const Function *, bool> &seenMap) { |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 751 | for (const User *U : C->users()) { |
| 752 | if (const Constant *cu = dyn_cast<Constant>(U)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 753 | if (useFuncSeen(cu, seenMap)) |
| 754 | return true; |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 755 | } else if (const Instruction *I = dyn_cast<Instruction>(U)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 756 | const BasicBlock *bb = I->getParent(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 757 | if (!bb) |
| 758 | continue; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 759 | const Function *caller = bb->getParent(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 760 | if (!caller) |
| 761 | continue; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 762 | if (seenMap.find(caller) != seenMap.end()) |
| 763 | return true; |
| 764 | } |
| 765 | } |
| 766 | return false; |
| 767 | } |
| 768 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 769 | void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) { |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 770 | DenseMap<const Function *, bool> seenMap; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 771 | for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
| Duncan P. N. Exon Smith | 61149b8 | 2015-10-20 00:54:09 +0000 | [diff] [blame] | 772 | const Function *F = &*FI; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 773 | |
| 774 | if (F->isDeclaration()) { |
| 775 | if (F->use_empty()) |
| 776 | continue; |
| 777 | if (F->getIntrinsicID()) |
| 778 | continue; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 779 | emitDeclaration(F, O); |
| 780 | continue; |
| 781 | } |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 782 | for (const User *U : F->users()) { |
| 783 | if (const Constant *C = dyn_cast<Constant>(U)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 784 | if (usedInGlobalVarDef(C)) { |
| 785 | // The use is in the initialization of a global variable |
| 786 | // that is a function pointer, so print a declaration |
| 787 | // for the original function |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 788 | emitDeclaration(F, O); |
| 789 | break; |
| 790 | } |
| 791 | // Emit a declaration of this function if the function that |
| 792 | // uses this constant expr has already been seen. |
| 793 | if (useFuncSeen(C, seenMap)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 794 | emitDeclaration(F, O); |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 799 | if (!isa<Instruction>(U)) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 800 | continue; |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 801 | const Instruction *instr = cast<Instruction>(U); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 802 | const BasicBlock *bb = instr->getParent(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 803 | if (!bb) |
| 804 | continue; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 805 | const Function *caller = bb->getParent(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 806 | if (!caller) |
| 807 | continue; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 808 | |
| 809 | // If a caller has already been seen, then the caller is |
| 810 | // appearing in the module before the callee. so print out |
| 811 | // a declaration for the callee. |
| 812 | if (seenMap.find(caller) != seenMap.end()) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 813 | emitDeclaration(F, O); |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | seenMap[F] = true; |
| 818 | } |
| 819 | } |
| 820 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 821 | void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) { |
| 822 | DebugInfoFinder DbgFinder; |
| 823 | DbgFinder.processModule(M); |
| 824 | |
| 825 | unsigned i = 1; |
| 826 | for (const DICompileUnit *DIUnit : DbgFinder.compile_units()) { |
| 827 | StringRef Filename = DIUnit->getFilename(); |
| 828 | StringRef Dirname = DIUnit->getDirectory(); |
| 829 | SmallString<128> FullPathName = Dirname; |
| 830 | if (!Dirname.empty() && !sys::path::is_absolute(Filename)) { |
| 831 | sys::path::append(FullPathName, Filename); |
| 832 | Filename = FullPathName; |
| 833 | } |
| 834 | if (filenameMap.find(Filename) != filenameMap.end()) |
| 835 | continue; |
| 836 | filenameMap[Filename] = i; |
| 837 | OutStreamer->EmitDwarfFileDirective(i, "", Filename); |
| 838 | ++i; |
| 839 | } |
| 840 | |
| 841 | for (DISubprogram *SP : DbgFinder.subprograms()) { |
| 842 | StringRef Filename = SP->getFilename(); |
| 843 | StringRef Dirname = SP->getDirectory(); |
| 844 | SmallString<128> FullPathName = Dirname; |
| 845 | if (!Dirname.empty() && !sys::path::is_absolute(Filename)) { |
| 846 | sys::path::append(FullPathName, Filename); |
| 847 | Filename = FullPathName; |
| 848 | } |
| 849 | if (filenameMap.find(Filename) != filenameMap.end()) |
| 850 | continue; |
| 851 | filenameMap[Filename] = i; |
| 852 | OutStreamer->EmitDwarfFileDirective(i, "", Filename); |
| 853 | ++i; |
| 854 | } |
| 855 | } |
| 856 | |
| Justin Lebar | ead59f4 | 2016-01-30 01:07:38 +0000 | [diff] [blame] | 857 | static bool isEmptyXXStructor(GlobalVariable *GV) { |
| 858 | if (!GV) return true; |
| 859 | const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 860 | if (!InitList) return true; // Not an array; we don't know how to parse. |
| 861 | return InitList->getNumOperands() == 0; |
| 862 | } |
| 863 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 864 | bool NVPTXAsmPrinter::doInitialization(Module &M) { |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 865 | // Construct a default subtarget off of the TargetMachine defaults. The |
| 866 | // rest of NVPTX isn't friendly to change subtargets per function and |
| 867 | // so the default TargetMachine will have all of the options. |
| Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 868 | const Triple &TT = TM.getTargetTriple(); |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 869 | StringRef CPU = TM.getTargetCPU(); |
| 870 | StringRef FS = TM.getTargetFeatureString(); |
| 871 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
| Eric Christopher | 02389e3 | 2015-02-19 00:08:27 +0000 | [diff] [blame] | 872 | const NVPTXSubtarget STI(TT, CPU, FS, NTM); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 873 | |
| Justin Lebar | 3a5f579 | 2016-01-23 21:12:20 +0000 | [diff] [blame] | 874 | if (M.alias_size()) { |
| 875 | report_fatal_error("Module has aliases, which NVPTX does not support."); |
| 876 | return true; // error |
| 877 | } |
| Justin Lebar | ead59f4 | 2016-01-30 01:07:38 +0000 | [diff] [blame] | 878 | if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_ctors"))) { |
| 879 | report_fatal_error( |
| 880 | "Module has a nontrivial global ctor, which NVPTX does not support."); |
| 881 | return true; // error |
| 882 | } |
| 883 | if (!isEmptyXXStructor(M.getNamedGlobal("llvm.global_dtors"))) { |
| 884 | report_fatal_error( |
| 885 | "Module has a nontrivial global dtor, which NVPTX does not support."); |
| 886 | return true; // error |
| 887 | } |
| Justin Lebar | 3a5f579 | 2016-01-23 21:12:20 +0000 | [diff] [blame] | 888 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 889 | SmallString<128> Str1; |
| 890 | raw_svector_ostream OS1(Str1); |
| 891 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 892 | MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 893 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 894 | // We need to call the parent's one explicitly. |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 895 | //bool Result = AsmPrinter::doInitialization(M); |
| 896 | |
| 897 | // Initialize TargetLoweringObjectFile since we didn't do in |
| 898 | // AsmPrinter::doInitialization either right above or where it's commented out |
| 899 | // below. |
| 900 | const_cast<TargetLoweringObjectFile &>(getObjFileLowering()) |
| 901 | .Initialize(OutContext, TM); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 902 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 903 | // Emit header before any dwarf directives are emitted below. |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 904 | emitHeader(M, OS1, STI); |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 905 | OutStreamer->EmitRawText(OS1.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 906 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 907 | // Already commented out |
| 908 | //bool Result = AsmPrinter::doInitialization(M); |
| 909 | |
| Justin Holewinski | d2bbdf0 | 2013-07-01 13:00:14 +0000 | [diff] [blame] | 910 | // Emit module-level inline asm if it exists. |
| 911 | if (!M.getModuleInlineAsm().empty()) { |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 912 | OutStreamer->AddComment("Start of file scope inline assembly"); |
| 913 | OutStreamer->AddBlankLine(); |
| 914 | OutStreamer->EmitRawText(StringRef(M.getModuleInlineAsm())); |
| 915 | OutStreamer->AddBlankLine(); |
| 916 | OutStreamer->AddComment("End of file scope inline assembly"); |
| 917 | OutStreamer->AddBlankLine(); |
| Justin Holewinski | d2bbdf0 | 2013-07-01 13:00:14 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 920 | // If we're not NVCL we're CUDA, go ahead and emit filenames. |
| 921 | if (TM.getTargetTriple().getOS() != Triple::NVCL) |
| 922 | recordAndEmitFilenames(M); |
| Alexey Bataev | 242706b | 2018-04-18 16:13:41 +0000 | [diff] [blame] | 923 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 924 | GlobalsEmitted = false; |
| 925 | |
| 926 | return false; // success |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | void NVPTXAsmPrinter::emitGlobals(const Module &M) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 930 | SmallString<128> Str2; |
| 931 | raw_svector_ostream OS2(Str2); |
| 932 | |
| 933 | emitDeclarations(M, OS2); |
| 934 | |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 935 | // As ptxas does not support forward references of globals, we need to first |
| 936 | // sort the list of module-level globals in def-use order. We visit each |
| 937 | // global variable in order, and ensure that we emit it *after* its dependent |
| 938 | // globals. We use a little extra memory maintaining both a set and a list to |
| 939 | // have fast searches while maintaining a strict ordering. |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 940 | SmallVector<const GlobalVariable *, 8> Globals; |
| 941 | DenseSet<const GlobalVariable *> GVVisited; |
| 942 | DenseSet<const GlobalVariable *> GVVisiting; |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 943 | |
| 944 | // Visit each global variable, in order |
| Duncan P. N. Exon Smith | 61149b8 | 2015-10-20 00:54:09 +0000 | [diff] [blame] | 945 | for (const GlobalVariable &I : M.globals()) |
| 946 | VisitGlobalVariableForEmission(&I, Globals, GVVisited, GVVisiting); |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 947 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 948 | assert(GVVisited.size() == M.getGlobalList().size() && |
| Justin Holewinski | 2c5ac70 | 2012-11-16 21:03:51 +0000 | [diff] [blame] | 949 | "Missed a global variable"); |
| 950 | assert(GVVisiting.size() == 0 && "Did not fully process a global variable"); |
| 951 | |
| 952 | // Print out module-level global variables in proper order |
| 953 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) |
| 954 | printModuleLevelGV(Globals[i], OS2); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 955 | |
| 956 | OS2 << '\n'; |
| 957 | |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 958 | OutStreamer->EmitRawText(OS2.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 961 | void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O, |
| 962 | const NVPTXSubtarget &STI) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 963 | O << "//\n"; |
| 964 | O << "// Generated by LLVM NVPTX Back-End\n"; |
| 965 | O << "//\n"; |
| 966 | O << "\n"; |
| 967 | |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 968 | unsigned PTXVersion = STI.getPTXVersion(); |
| Justin Holewinski | 1812ee9 | 2012-11-12 03:16:43 +0000 | [diff] [blame] | 969 | O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 970 | |
| 971 | O << ".target "; |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 972 | O << STI.getTargetName(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 973 | |
| Eric Christopher | ca929f2 | 2015-02-19 00:22:47 +0000 | [diff] [blame] | 974 | const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM); |
| 975 | if (NTM.getDrvInterface() == NVPTX::NVCL) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 976 | O << ", texmode_independent"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 977 | |
| Eric Christopher | 8024425 | 2018-05-01 00:10:13 +0000 | [diff] [blame^] | 978 | if (MAI->doesSupportDebugInformation()) |
| 979 | O << ", debug"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 980 | |
| 981 | O << "\n"; |
| 982 | |
| 983 | O << ".address_size "; |
| Eric Christopher | ca929f2 | 2015-02-19 00:22:47 +0000 | [diff] [blame] | 984 | if (NTM.is64Bit()) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 985 | O << "64"; |
| 986 | else |
| 987 | O << "32"; |
| 988 | O << "\n"; |
| 989 | |
| 990 | O << "\n"; |
| 991 | } |
| 992 | |
| 993 | bool NVPTXAsmPrinter::doFinalization(Module &M) { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 994 | // If we did not emit any functions, then the global declarations have not |
| 995 | // yet been emitted. |
| 996 | if (!GlobalsEmitted) { |
| 997 | emitGlobals(M); |
| 998 | GlobalsEmitted = true; |
| 999 | } |
| 1000 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1001 | // XXX Temproarily remove global variables so that doFinalization() will not |
| 1002 | // emit them again (global variables are emitted at beginning). |
| 1003 | |
| 1004 | Module::GlobalListType &global_list = M.getGlobalList(); |
| 1005 | int i, n = global_list.size(); |
| Dylan Noblesmith | c9e2a27 | 2014-08-26 02:03:35 +0000 | [diff] [blame] | 1006 | GlobalVariable **gv_array = new GlobalVariable *[n]; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1007 | |
| 1008 | // first, back-up GlobalVariable in gv_array |
| 1009 | i = 0; |
| 1010 | for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1011 | I != E; ++I) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1012 | gv_array[i++] = &*I; |
| 1013 | |
| 1014 | // second, empty global_list |
| 1015 | while (!global_list.empty()) |
| 1016 | global_list.remove(global_list.begin()); |
| 1017 | |
| 1018 | // call doFinalization |
| 1019 | bool ret = AsmPrinter::doFinalization(M); |
| 1020 | |
| 1021 | // now we restore global variables |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1022 | for (i = 0; i < n; i++) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1023 | global_list.insert(global_list.end(), gv_array[i]); |
| 1024 | |
| Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 1025 | clearAnnotationCache(&M); |
| Dylan Noblesmith | c9e2a27 | 2014-08-26 02:03:35 +0000 | [diff] [blame] | 1026 | |
| 1027 | delete[] gv_array; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1028 | return ret; |
| 1029 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1030 | //bool Result = AsmPrinter::doFinalization(M); |
| 1031 | // Instead of calling the parents doFinalization, we may |
| 1032 | // clone parents doFinalization and customize here. |
| 1033 | // Currently, we if NVISA out the EmitGlobals() in |
| 1034 | // parent's doFinalization, which is too intrusive. |
| 1035 | // |
| 1036 | // Same for the doInitialization. |
| 1037 | //return Result; |
| 1038 | } |
| 1039 | |
| 1040 | // This function emits appropriate linkage directives for |
| 1041 | // functions and global variables. |
| 1042 | // |
| 1043 | // extern function declaration -> .extern |
| 1044 | // extern function definition -> .visible |
| 1045 | // external global variable with init -> .visible |
| 1046 | // external without init -> .extern |
| 1047 | // appending -> not allowed, assert. |
| Justin Holewinski | 7d5bf66 | 2014-06-27 18:35:10 +0000 | [diff] [blame] | 1048 | // for any linkage other than |
| 1049 | // internal, private, linker_private, |
| 1050 | // linker_private_weak, linker_private_weak_def_auto, |
| 1051 | // we emit -> .weak. |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1052 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1053 | void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V, |
| 1054 | raw_ostream &O) { |
| Eric Christopher | beffc4e | 2015-02-19 00:08:23 +0000 | [diff] [blame] | 1055 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1056 | if (V->hasExternalLinkage()) { |
| 1057 | if (isa<GlobalVariable>(V)) { |
| 1058 | const GlobalVariable *GVar = cast<GlobalVariable>(V); |
| 1059 | if (GVar) { |
| 1060 | if (GVar->hasInitializer()) |
| 1061 | O << ".visible "; |
| 1062 | else |
| 1063 | O << ".extern "; |
| 1064 | } |
| 1065 | } else if (V->isDeclaration()) |
| 1066 | O << ".extern "; |
| 1067 | else |
| 1068 | O << ".visible "; |
| 1069 | } else if (V->hasAppendingLinkage()) { |
| 1070 | std::string msg; |
| 1071 | msg.append("Error: "); |
| 1072 | msg.append("Symbol "); |
| 1073 | if (V->hasName()) |
| Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1074 | msg.append(V->getName()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1075 | msg.append("has unsupported appending linkage type"); |
| 1076 | llvm_unreachable(msg.c_str()); |
| Justin Holewinski | 7d5bf66 | 2014-06-27 18:35:10 +0000 | [diff] [blame] | 1077 | } else if (!V->hasInternalLinkage() && |
| 1078 | !V->hasPrivateLinkage()) { |
| 1079 | O << ".weak "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1084 | void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, |
| 1085 | raw_ostream &O, |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1086 | bool processDemoted) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1087 | // Skip meta data |
| 1088 | if (GVar->hasSection()) { |
| Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 1089 | if (GVar->getSection() == "llvm.metadata") |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | } |
| 1092 | |
| Justin Holewinski | 73cb5de | 2014-06-27 18:35:53 +0000 | [diff] [blame] | 1093 | // Skip LLVM intrinsic global variables |
| 1094 | if (GVar->getName().startswith("llvm.") || |
| 1095 | GVar->getName().startswith("nvvm.")) |
| 1096 | return; |
| 1097 | |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1098 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1099 | |
| 1100 | // GlobalVariables are always constant pointers themselves. |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1101 | PointerType *PTy = GVar->getType(); |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1102 | Type *ETy = GVar->getValueType(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (GVar->hasExternalLinkage()) { |
| 1105 | if (GVar->hasInitializer()) |
| 1106 | O << ".visible "; |
| 1107 | else |
| 1108 | O << ".extern "; |
| Justin Holewinski | d73767a | 2014-06-27 18:35:56 +0000 | [diff] [blame] | 1109 | } else if (GVar->hasLinkOnceLinkage() || GVar->hasWeakLinkage() || |
| 1110 | GVar->hasAvailableExternallyLinkage() || |
| 1111 | GVar->hasCommonLinkage()) { |
| 1112 | O << ".weak "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1115 | if (isTexture(*GVar)) { |
| 1116 | O << ".global .texref " << getTextureName(*GVar) << ";\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1120 | if (isSurface(*GVar)) { |
| 1121 | O << ".global .surfref " << getSurfaceName(*GVar) << ";\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | if (GVar->isDeclaration()) { |
| 1126 | // (extern) declarations, no definition or initializer |
| 1127 | // Currently the only known declaration is for an automatic __local |
| 1128 | // (.shared) promoted to global. |
| 1129 | emitPTXGlobalVariable(GVar, O); |
| 1130 | O << ";\n"; |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1134 | if (isSampler(*GVar)) { |
| 1135 | O << ".global .samplerref " << getSamplerName(*GVar); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1136 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1137 | const Constant *Initializer = nullptr; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1138 | if (GVar->hasInitializer()) |
| 1139 | Initializer = GVar->getInitializer(); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1140 | const ConstantInt *CI = nullptr; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1141 | if (Initializer) |
| 1142 | CI = dyn_cast<ConstantInt>(Initializer); |
| 1143 | if (CI) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1144 | unsigned sample = CI->getZExtValue(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1145 | |
| 1146 | O << " = { "; |
| 1147 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1148 | for (int i = 0, |
| 1149 | addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE); |
| 1150 | i < 3; i++) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1151 | O << "addr_mode_" << i << " = "; |
| 1152 | switch (addr) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1153 | case 0: |
| 1154 | O << "wrap"; |
| 1155 | break; |
| 1156 | case 1: |
| 1157 | O << "clamp_to_border"; |
| 1158 | break; |
| 1159 | case 2: |
| 1160 | O << "clamp_to_edge"; |
| 1161 | break; |
| 1162 | case 3: |
| 1163 | O << "wrap"; |
| 1164 | break; |
| 1165 | case 4: |
| 1166 | O << "mirror"; |
| 1167 | break; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1168 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1169 | O << ", "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1170 | } |
| 1171 | O << "filter_mode = "; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1172 | switch ((sample & __CLK_FILTER_MASK) >> __CLK_FILTER_BASE) { |
| 1173 | case 0: |
| 1174 | O << "nearest"; |
| 1175 | break; |
| 1176 | case 1: |
| 1177 | O << "linear"; |
| 1178 | break; |
| 1179 | case 2: |
| Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1180 | llvm_unreachable("Anisotropic filtering is not supported"); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1181 | default: |
| 1182 | O << "nearest"; |
| 1183 | break; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1184 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1185 | if (!((sample & __CLK_NORMALIZED_MASK) >> __CLK_NORMALIZED_BASE)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1186 | O << ", force_unnormalized_coords = 1"; |
| 1187 | } |
| 1188 | O << " }"; |
| 1189 | } |
| 1190 | |
| 1191 | O << ";\n"; |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | if (GVar->hasPrivateLinkage()) { |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1196 | if (strncmp(GVar->getName().data(), "unrollpragma", 12) == 0) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1197 | return; |
| 1198 | |
| 1199 | // FIXME - need better way (e.g. Metadata) to avoid generating this global |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1200 | if (strncmp(GVar->getName().data(), "filename", 8) == 0) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1201 | return; |
| 1202 | if (GVar->use_empty()) |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1206 | const Function *demotedFunc = nullptr; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1207 | if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) { |
| Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 1208 | O << "// " << GVar->getName() << " has been demoted\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1209 | if (localDecls.find(demotedFunc) != localDecls.end()) |
| 1210 | localDecls[demotedFunc].push_back(GVar); |
| 1211 | else { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1212 | std::vector<const GlobalVariable *> temp; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1213 | temp.push_back(GVar); |
| 1214 | localDecls[demotedFunc] = temp; |
| 1215 | } |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | O << "."; |
| 1220 | emitPTXAddressSpace(PTy->getAddressSpace(), O); |
| Justin Holewinski | 773ca40 | 2014-06-27 18:35:58 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (isManaged(*GVar)) { |
| 1223 | O << " .attribute(.managed)"; |
| 1224 | } |
| 1225 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1226 | if (GVar->getAlignment() == 0) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1227 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1228 | else |
| 1229 | O << " .align " << GVar->getAlignment(); |
| 1230 | |
| Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1231 | if (ETy->isFloatingPointTy() || ETy->isPointerTy() || |
| 1232 | (ETy->isIntegerTy() && ETy->getScalarSizeInBits() <= 64)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1233 | O << " ."; |
| Justin Holewinski | 700b6fa | 2013-05-20 12:13:28 +0000 | [diff] [blame] | 1234 | // Special case: ABI requires that we use .u8 for predicates |
| 1235 | if (ETy->isIntegerTy(1)) |
| 1236 | O << "u8"; |
| 1237 | else |
| 1238 | O << getPTXFundamentalTypeStr(ETy, false); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1239 | O << " "; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1240 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1241 | |
| 1242 | // Ptx allows variable initilization only for constant and global state |
| 1243 | // spaces. |
| Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1244 | if (GVar->hasInitializer()) { |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1245 | if ((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
| 1246 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) { |
| Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1247 | const Constant *Initializer = GVar->getInitializer(); |
| Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1248 | // 'undef' is treated as there is no value specified. |
| Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1249 | if (!Initializer->isNullValue() && !isa<UndefValue>(Initializer)) { |
| 1250 | O << " = "; |
| 1251 | printScalarConstant(Initializer, O); |
| 1252 | } |
| 1253 | } else { |
| Jingyue Wu | fcec098 | 2015-08-22 05:40:26 +0000 | [diff] [blame] | 1254 | // The frontend adds zero-initializer to device and constant variables |
| 1255 | // that don't have an initial value, and UndefValue to shared |
| 1256 | // variables, so skip warning for this case. |
| 1257 | if (!GVar->getInitializer()->isNullValue() && |
| 1258 | !isa<UndefValue>(GVar->getInitializer())) { |
| Benjamin Kramer | dba7ee9 | 2015-05-28 11:24:24 +0000 | [diff] [blame] | 1259 | report_fatal_error("initial value of '" + GVar->getName() + |
| 1260 | "' is not allowed in addrspace(" + |
| 1261 | Twine(PTy->getAddressSpace()) + ")"); |
| Justin Holewinski | 549c773 | 2014-06-27 18:36:01 +0000 | [diff] [blame] | 1262 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | } else { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1266 | unsigned int ElementSize = 0; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Although PTX has direct support for struct type and array type and |
| 1269 | // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for |
| 1270 | // targets that support these high level field accesses. Structs, arrays |
| 1271 | // and vectors are lowered into arrays of bytes. |
| 1272 | switch (ETy->getTypeID()) { |
| Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 1273 | case Type::IntegerTyID: // Integers larger than 64 bits |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1274 | case Type::StructTyID: |
| 1275 | case Type::ArrayTyID: |
| 1276 | case Type::VectorTyID: |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1277 | ElementSize = DL.getTypeStoreSize(ETy); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1278 | // Ptx allows variable initilization only for constant and |
| 1279 | // global state spaces. |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1280 | if (((PTy->getAddressSpace() == ADDRESS_SPACE_GLOBAL) || |
| 1281 | (PTy->getAddressSpace() == ADDRESS_SPACE_CONST)) && |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1282 | GVar->hasInitializer()) { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1283 | const Constant *Initializer = GVar->getInitializer(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1284 | if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1285 | AggBuffer aggBuffer(ElementSize, O, *this); |
| 1286 | bufferAggregateConstant(Initializer, &aggBuffer); |
| 1287 | if (aggBuffer.numSymbols) { |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1288 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1289 | O << " .u64 "; |
| 1290 | getSymbol(GVar)->print(O, MAI); |
| 1291 | O << "["; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1292 | O << ElementSize / 8; |
| 1293 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1294 | O << " .u32 "; |
| 1295 | getSymbol(GVar)->print(O, MAI); |
| 1296 | O << "["; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1297 | O << ElementSize / 4; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1298 | } |
| 1299 | O << "]"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1300 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1301 | O << " .b8 "; |
| 1302 | getSymbol(GVar)->print(O, MAI); |
| 1303 | O << "["; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1304 | O << ElementSize; |
| 1305 | O << "]"; |
| 1306 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1307 | O << " = {"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1308 | aggBuffer.print(); |
| 1309 | O << "}"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1310 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1311 | O << " .b8 "; |
| 1312 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1313 | if (ElementSize) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1314 | O << "["; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1315 | O << ElementSize; |
| 1316 | O << "]"; |
| 1317 | } |
| 1318 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1319 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1320 | O << " .b8 "; |
| 1321 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1322 | if (ElementSize) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1323 | O << "["; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1324 | O << ElementSize; |
| 1325 | O << "]"; |
| 1326 | } |
| 1327 | } |
| 1328 | break; |
| 1329 | default: |
| Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1330 | llvm_unreachable("type not supported yet"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1331 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1332 | } |
| 1333 | O << ";\n"; |
| 1334 | } |
| 1335 | |
| 1336 | void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) { |
| 1337 | if (localDecls.find(f) == localDecls.end()) |
| 1338 | return; |
| 1339 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1340 | std::vector<const GlobalVariable *> &gvars = localDecls[f]; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1341 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1342 | for (unsigned i = 0, e = gvars.size(); i != e; ++i) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1343 | O << "\t// demoted variable\n\t"; |
| 1344 | printModuleLevelGV(gvars[i], O, true); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace, |
| 1349 | raw_ostream &O) const { |
| 1350 | switch (AddressSpace) { |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1351 | case ADDRESS_SPACE_LOCAL: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1352 | O << "local"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1353 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1354 | case ADDRESS_SPACE_GLOBAL: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1355 | O << "global"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1356 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1357 | case ADDRESS_SPACE_CONST: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1358 | O << "const"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1359 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1360 | case ADDRESS_SPACE_SHARED: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1361 | O << "shared"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | default: |
| Justin Lebar | 321b443 | 2018-02-08 00:50:04 +0000 | [diff] [blame] | 1364 | report_fatal_error("Bad address space found while emitting PTX: " + |
| 1365 | llvm::Twine(AddressSpace)); |
| Justin Holewinski | 36a5099 | 2013-02-09 13:34:15 +0000 | [diff] [blame] | 1366 | break; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1370 | std::string |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1371 | NVPTXAsmPrinter::getPTXFundamentalTypeStr(Type *Ty, bool useB4PTR) const { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1372 | switch (Ty->getTypeID()) { |
| 1373 | default: |
| 1374 | llvm_unreachable("unexpected type"); |
| 1375 | break; |
| 1376 | case Type::IntegerTyID: { |
| 1377 | unsigned NumBits = cast<IntegerType>(Ty)->getBitWidth(); |
| 1378 | if (NumBits == 1) |
| 1379 | return "pred"; |
| 1380 | else if (NumBits <= 64) { |
| 1381 | std::string name = "u"; |
| 1382 | return name + utostr(NumBits); |
| 1383 | } else { |
| 1384 | llvm_unreachable("Integer too large"); |
| 1385 | break; |
| 1386 | } |
| 1387 | break; |
| 1388 | } |
| Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 1389 | case Type::HalfTyID: |
| 1390 | // fp16 is stored as .b16 for compatibility with pre-sm_53 PTX assembly. |
| 1391 | return "b16"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1392 | case Type::FloatTyID: |
| 1393 | return "f32"; |
| 1394 | case Type::DoubleTyID: |
| 1395 | return "f64"; |
| 1396 | case Type::PointerTyID: |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1397 | if (static_cast<const NVPTXTargetMachine &>(TM).is64Bit()) |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1398 | if (useB4PTR) |
| 1399 | return "b64"; |
| 1400 | else |
| 1401 | return "u64"; |
| 1402 | else if (useB4PTR) |
| 1403 | return "b32"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1404 | else |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1405 | return "u32"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1406 | } |
| 1407 | llvm_unreachable("unexpected type"); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1408 | return nullptr; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1411 | void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar, |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1412 | raw_ostream &O) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1413 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1414 | |
| 1415 | // GlobalVariables are always constant pointers themselves. |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1416 | Type *ETy = GVar->getValueType(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1417 | |
| 1418 | O << "."; |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1419 | emitPTXAddressSpace(GVar->getType()->getAddressSpace(), O); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1420 | if (GVar->getAlignment() == 0) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1421 | O << " .align " << (int)DL.getPrefTypeAlignment(ETy); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1422 | else |
| 1423 | O << " .align " << GVar->getAlignment(); |
| 1424 | |
| Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 1425 | // Special case for i128 |
| 1426 | if (ETy->isIntegerTy(128)) { |
| 1427 | O << " .b8 "; |
| 1428 | getSymbol(GVar)->print(O, MAI); |
| 1429 | O << "[16]"; |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| Jingyue Wu | e4c9cf0 | 2014-12-17 17:59:04 +0000 | [diff] [blame] | 1433 | if (ETy->isFloatingPointTy() || ETy->isIntegerTy() || ETy->isPointerTy()) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1434 | O << " ."; |
| 1435 | O << getPTXFundamentalTypeStr(ETy); |
| 1436 | O << " "; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1437 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1438 | return; |
| 1439 | } |
| 1440 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1441 | int64_t ElementSize = 0; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Although PTX has direct support for struct type and array type and LLVM IR |
| 1444 | // is very similar to PTX, the LLVM CodeGen does not support for targets that |
| 1445 | // support these high level field accesses. Structs and arrays are lowered |
| 1446 | // into arrays of bytes. |
| 1447 | switch (ETy->getTypeID()) { |
| 1448 | case Type::StructTyID: |
| 1449 | case Type::ArrayTyID: |
| 1450 | case Type::VectorTyID: |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1451 | ElementSize = DL.getTypeStoreSize(ETy); |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1452 | O << " .b8 "; |
| 1453 | getSymbol(GVar)->print(O, MAI); |
| 1454 | O << "["; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1455 | if (ElementSize) { |
| Benjamin Kramer | dba7ee9 | 2015-05-28 11:24:24 +0000 | [diff] [blame] | 1456 | O << ElementSize; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1457 | } |
| 1458 | O << "]"; |
| 1459 | break; |
| 1460 | default: |
| Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1461 | llvm_unreachable("type not supported yet"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1462 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1465 | static unsigned int getOpenCLAlignment(const DataLayout &DL, Type *Ty) { |
| Rafael Espindola | 0801334 | 2013-12-07 19:34:20 +0000 | [diff] [blame] | 1466 | if (Ty->isSingleValueType()) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1467 | return DL.getPrefTypeAlignment(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1468 | |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1469 | auto *ATy = dyn_cast<ArrayType>(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1470 | if (ATy) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1471 | return getOpenCLAlignment(DL, ATy->getElementType()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1472 | |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1473 | auto *STy = dyn_cast<StructType>(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1474 | if (STy) { |
| 1475 | unsigned int alignStruct = 1; |
| 1476 | // Go through each element of the struct and find the |
| 1477 | // largest alignment. |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1478 | for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1479 | Type *ETy = STy->getElementType(i); |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1480 | unsigned int align = getOpenCLAlignment(DL, ETy); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1481 | if (align > alignStruct) |
| 1482 | alignStruct = align; |
| 1483 | } |
| 1484 | return alignStruct; |
| 1485 | } |
| 1486 | |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1487 | auto *FTy = dyn_cast<FunctionType>(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1488 | if (FTy) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1489 | return DL.getPointerPrefAlignment(); |
| 1490 | return DL.getPrefTypeAlignment(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, |
| 1494 | int paramIndex, raw_ostream &O) { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1495 | getSymbol(I->getParent())->print(O, MAI); |
| 1496 | O << "_param_" << paramIndex; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1499 | void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1500 | const DataLayout &DL = getDataLayout(); |
| Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1501 | const AttributeList &PAL = F->getAttributes(); |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1502 | const TargetLowering *TLI = nvptxSubtarget->getTargetLowering(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1503 | Function::const_arg_iterator I, E; |
| 1504 | unsigned paramIndex = 0; |
| 1505 | bool first = true; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1506 | bool isKernelFunc = isKernelFunction(*F); |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1507 | bool isABI = (nvptxSubtarget->getSmVersion() >= 20); |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1508 | MVT thePointerTy = TLI->getPointerTy(DL); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1509 | |
| Justin Lebar | 2a161f9 | 2016-01-23 21:12:17 +0000 | [diff] [blame] | 1510 | if (F->arg_empty()) { |
| 1511 | O << "()\n"; |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1515 | O << "(\n"; |
| 1516 | |
| 1517 | for (I = F->arg_begin(), E = F->arg_end(); I != E; ++I, paramIndex++) { |
| Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1518 | Type *Ty = I->getType(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (!first) |
| 1521 | O << ",\n"; |
| 1522 | |
| 1523 | first = false; |
| 1524 | |
| 1525 | // Handle image/sampler parameters |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1526 | if (isKernelFunction(*F)) { |
| 1527 | if (isSampler(*I) || isImage(*I)) { |
| 1528 | if (isImage(*I)) { |
| 1529 | std::string sname = I->getName(); |
| 1530 | if (isImageWriteOnly(*I) || isImageReadWrite(*I)) { |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1531 | if (nvptxSubtarget->hasImageHandles()) |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1532 | O << "\t.param .u64 .ptr .surfref "; |
| 1533 | else |
| 1534 | O << "\t.param .surfref "; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1535 | CurrentFnSym->print(O, MAI); |
| 1536 | O << "_param_" << paramIndex; |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1537 | } |
| 1538 | else { // Default image is read_only |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1539 | if (nvptxSubtarget->hasImageHandles()) |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1540 | O << "\t.param .u64 .ptr .texref "; |
| 1541 | else |
| 1542 | O << "\t.param .texref "; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1543 | CurrentFnSym->print(O, MAI); |
| 1544 | O << "_param_" << paramIndex; |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1545 | } |
| 1546 | } else { |
| Eric Christopher | 6aad8b1 | 2015-02-19 00:08:14 +0000 | [diff] [blame] | 1547 | if (nvptxSubtarget->hasImageHandles()) |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1548 | O << "\t.param .u64 .ptr .samplerref "; |
| 1549 | else |
| 1550 | O << "\t.param .samplerref "; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1551 | CurrentFnSym->print(O, MAI); |
| 1552 | O << "_param_" << paramIndex; |
| Justin Holewinski | 30d56a7 | 2014-04-09 15:39:15 +0000 | [diff] [blame] | 1553 | } |
| 1554 | continue; |
| 1555 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 1558 | if (!PAL.hasParamAttribute(paramIndex, Attribute::ByVal)) { |
| Artem Belevich | d7a7382 | 2017-07-20 21:16:03 +0000 | [diff] [blame] | 1559 | if (Ty->isAggregateType() || Ty->isVectorTy() || Ty->isIntegerTy(128)) { |
| Gautam Chakrabarti | 2c28340 | 2014-01-28 18:35:29 +0000 | [diff] [blame] | 1560 | // Just print .param .align <a> .b8 .param[size]; |
| Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1561 | // <a> = PAL.getparamalignment |
| 1562 | // size = typeallocsize of element type |
| Reid Kleckner | 859f8b5 | 2017-04-28 20:34:27 +0000 | [diff] [blame] | 1563 | unsigned align = PAL.getParamAlignment(paramIndex); |
| Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1564 | if (align == 0) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1565 | align = DL.getABITypeAlignment(Ty); |
| Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1566 | |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1567 | unsigned sz = DL.getTypeAllocSize(Ty); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1568 | O << "\t.param .align " << align << " .b8 "; |
| Justin Holewinski | e988409 | 2013-03-24 21:17:47 +0000 | [diff] [blame] | 1569 | printParamName(I, paramIndex, O); |
| 1570 | O << "[" << sz << "]"; |
| 1571 | |
| 1572 | continue; |
| 1573 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1574 | // Just a scalar |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1575 | auto *PTy = dyn_cast<PointerType>(Ty); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1576 | if (isKernelFunc) { |
| 1577 | if (PTy) { |
| 1578 | // Special handling for pointer arguments to kernel |
| 1579 | O << "\t.param .u" << thePointerTy.getSizeInBits() << " "; |
| 1580 | |
| Eric Christopher | beffc4e | 2015-02-19 00:08:23 +0000 | [diff] [blame] | 1581 | if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() != |
| 1582 | NVPTX::CUDA) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1583 | Type *ETy = PTy->getElementType(); |
| 1584 | int addrSpace = PTy->getAddressSpace(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1585 | switch (addrSpace) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1586 | default: |
| 1587 | O << ".ptr "; |
| 1588 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1589 | case ADDRESS_SPACE_CONST: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1590 | O << ".ptr .const "; |
| 1591 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1592 | case ADDRESS_SPACE_SHARED: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1593 | O << ".ptr .shared "; |
| 1594 | break; |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 1595 | case ADDRESS_SPACE_GLOBAL: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1596 | O << ".ptr .global "; |
| 1597 | break; |
| 1598 | } |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1599 | O << ".align " << (int)getOpenCLAlignment(DL, ETy) << " "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1600 | } |
| 1601 | printParamName(I, paramIndex, O); |
| 1602 | continue; |
| 1603 | } |
| 1604 | |
| 1605 | // non-pointer scalar to kernel func |
| Justin Holewinski | 700b6fa | 2013-05-20 12:13:28 +0000 | [diff] [blame] | 1606 | O << "\t.param ."; |
| 1607 | // Special case: predicate operands become .u8 types |
| 1608 | if (Ty->isIntegerTy(1)) |
| 1609 | O << "u8"; |
| 1610 | else |
| 1611 | O << getPTXFundamentalTypeStr(Ty); |
| 1612 | O << " "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1613 | printParamName(I, paramIndex, O); |
| 1614 | continue; |
| 1615 | } |
| 1616 | // Non-kernel function, just print .param .b<size> for ABI |
| Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 1617 | // and .reg .b<size> for non-ABI |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1618 | unsigned sz = 0; |
| 1619 | if (isa<IntegerType>(Ty)) { |
| 1620 | sz = cast<IntegerType>(Ty)->getBitWidth(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1621 | if (sz < 32) |
| 1622 | sz = 32; |
| 1623 | } else if (isa<PointerType>(Ty)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1624 | sz = thePointerTy.getSizeInBits(); |
| Artem Belevich | 64dc9be | 2017-01-13 20:56:17 +0000 | [diff] [blame] | 1625 | else if (Ty->isHalfTy()) |
| 1626 | // PTX ABI requires all scalar parameters to be at least 32 |
| 1627 | // bits in size. fp16 normally uses .b16 as its storage type |
| 1628 | // in PTX, so its size must be adjusted here, too. |
| 1629 | sz = 32; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1630 | else |
| 1631 | sz = Ty->getPrimitiveSizeInBits(); |
| 1632 | if (isABI) |
| 1633 | O << "\t.param .b" << sz << " "; |
| 1634 | else |
| 1635 | O << "\t.reg .b" << sz << " "; |
| 1636 | printParamName(I, paramIndex, O); |
| 1637 | continue; |
| 1638 | } |
| 1639 | |
| 1640 | // param has byVal attribute. So should be a pointer |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1641 | auto *PTy = dyn_cast<PointerType>(Ty); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1642 | assert(PTy && "Param with byval attribute should be a pointer type"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1643 | Type *ETy = PTy->getElementType(); |
| 1644 | |
| 1645 | if (isABI || isKernelFunc) { |
| Gautam Chakrabarti | 2c28340 | 2014-01-28 18:35:29 +0000 | [diff] [blame] | 1646 | // Just print .param .align <a> .b8 .param[size]; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1647 | // <a> = PAL.getparamalignment |
| 1648 | // size = typeallocsize of element type |
| Reid Kleckner | 859f8b5 | 2017-04-28 20:34:27 +0000 | [diff] [blame] | 1649 | unsigned align = PAL.getParamAlignment(paramIndex); |
| Justin Holewinski | 2dc9d07 | 2012-11-09 23:50:24 +0000 | [diff] [blame] | 1650 | if (align == 0) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1651 | align = DL.getABITypeAlignment(ETy); |
| Artem Belevich | 052b1ed | 2016-07-18 19:54:56 +0000 | [diff] [blame] | 1652 | // Work around a bug in ptxas. When PTX code takes address of |
| 1653 | // byval parameter with alignment < 4, ptxas generates code to |
| 1654 | // spill argument into memory. Alas on sm_50+ ptxas generates |
| 1655 | // SASS code that fails with misaligned access. To work around |
| 1656 | // the problem, make sure that we align byval parameters by at |
| 1657 | // least 4. Matching change must be made in LowerCall() where we |
| 1658 | // prepare parameters for the call. |
| 1659 | // |
| 1660 | // TODO: this will need to be undone when we get to support multi-TU |
| 1661 | // device-side compilation as it breaks ABI compatibility with nvcc. |
| 1662 | // Hopefully ptxas bug is fixed by then. |
| 1663 | if (!isKernelFunc && align < 4) |
| 1664 | align = 4; |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1665 | unsigned sz = DL.getTypeAllocSize(ETy); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1666 | O << "\t.param .align " << align << " .b8 "; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1667 | printParamName(I, paramIndex, O); |
| 1668 | O << "[" << sz << "]"; |
| 1669 | continue; |
| 1670 | } else { |
| 1671 | // Split the ETy into constituent parts and |
| 1672 | // print .param .b<size> <name> for each part. |
| 1673 | // Further, if a part is vector, print the above for |
| 1674 | // each vector element. |
| 1675 | SmallVector<EVT, 16> vtparts; |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1676 | ComputeValueVTs(*TLI, DL, ETy, vtparts); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1677 | for (unsigned i = 0, e = vtparts.size(); i != e; ++i) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1678 | unsigned elems = 1; |
| 1679 | EVT elemtype = vtparts[i]; |
| 1680 | if (vtparts[i].isVector()) { |
| 1681 | elems = vtparts[i].getVectorNumElements(); |
| 1682 | elemtype = vtparts[i].getVectorElementType(); |
| 1683 | } |
| 1684 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1685 | for (unsigned j = 0, je = elems; j != je; ++j) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1686 | unsigned sz = elemtype.getSizeInBits(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1687 | if (elemtype.isInteger() && (sz < 32)) |
| 1688 | sz = 32; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1689 | O << "\t.reg .b" << sz << " "; |
| 1690 | printParamName(I, paramIndex, O); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1691 | if (j < je - 1) |
| 1692 | O << ",\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1693 | ++paramIndex; |
| 1694 | } |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1695 | if (i < e - 1) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1696 | O << ",\n"; |
| 1697 | } |
| 1698 | --paramIndex; |
| 1699 | continue; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | O << "\n)\n"; |
| 1704 | } |
| 1705 | |
| 1706 | void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF, |
| 1707 | raw_ostream &O) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1708 | const Function &F = MF.getFunction(); |
| 1709 | emitFunctionParamList(&F, O); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1712 | void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters( |
| 1713 | const MachineFunction &MF) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1714 | SmallString<128> Str; |
| 1715 | raw_svector_ostream O(Str); |
| 1716 | |
| 1717 | // Map the global virtual register number to a register class specific |
| 1718 | // virtual register number starting from 1 with that class. |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1719 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1720 | //unsigned numRegClasses = TRI->getNumRegClasses(); |
| 1721 | |
| 1722 | // Emit the Fake Stack Object |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1723 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1724 | int NumBytes = (int) MFI.getStackSize(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1725 | if (NumBytes) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1726 | O << "\t.local .align " << MFI.getMaxAlignment() << " .b8 \t" << DEPOTNAME |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1727 | << getFunctionNumber() << "[" << NumBytes << "];\n"; |
| Eric Christopher | 02389e3 | 2015-02-19 00:08:27 +0000 | [diff] [blame] | 1728 | if (static_cast<const NVPTXTargetMachine &>(MF.getTarget()).is64Bit()) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1729 | O << "\t.reg .b64 \t%SP;\n"; |
| 1730 | O << "\t.reg .b64 \t%SPL;\n"; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1731 | } else { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1732 | O << "\t.reg .b32 \t%SP;\n"; |
| 1733 | O << "\t.reg .b32 \t%SPL;\n"; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | // Go through all virtual registers to establish the mapping between the |
| 1738 | // global virtual |
| 1739 | // register number and the per class virtual register number. |
| 1740 | // We use the per class virtual register number in the ptx output. |
| 1741 | unsigned int numVRs = MRI->getNumVirtRegs(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1742 | for (unsigned i = 0; i < numVRs; i++) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1743 | unsigned int vr = TRI->index2VirtReg(i); |
| 1744 | const TargetRegisterClass *RC = MRI->getRegClass(vr); |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1745 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1746 | int n = regmap.size(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1747 | regmap.insert(std::make_pair(vr, n + 1)); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | // Emit register declarations |
| 1751 | // @TODO: Extract out the real register usage |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1752 | // O << "\t.reg .pred %p<" << NVPTXNumRegisters << ">;\n"; |
| 1753 | // O << "\t.reg .s16 %rc<" << NVPTXNumRegisters << ">;\n"; |
| 1754 | // O << "\t.reg .s16 %rs<" << NVPTXNumRegisters << ">;\n"; |
| 1755 | // O << "\t.reg .s32 %r<" << NVPTXNumRegisters << ">;\n"; |
| Justin Holewinski | 3e037d9 | 2014-07-16 16:26:58 +0000 | [diff] [blame] | 1756 | // O << "\t.reg .s64 %rd<" << NVPTXNumRegisters << ">;\n"; |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1757 | // O << "\t.reg .f32 %f<" << NVPTXNumRegisters << ">;\n"; |
| Justin Holewinski | 3e037d9 | 2014-07-16 16:26:58 +0000 | [diff] [blame] | 1758 | // O << "\t.reg .f64 %fd<" << NVPTXNumRegisters << ">;\n"; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Emit declaration of the virtual registers or 'physical' registers for |
| 1761 | // each register class |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1762 | for (unsigned i=0; i< TRI->getNumRegClasses(); i++) { |
| 1763 | const TargetRegisterClass *RC = TRI->getRegClass(i); |
| 1764 | DenseMap<unsigned, unsigned> ®map = VRegMapping[RC]; |
| 1765 | std::string rcname = getNVPTXRegClassName(RC); |
| 1766 | std::string rcStr = getNVPTXRegClassStr(RC); |
| 1767 | int n = regmap.size(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1768 | |
| Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 1769 | // Only declare those registers that may be used. |
| 1770 | if (n) { |
| 1771 | O << "\t.reg " << rcname << " \t" << rcStr << "<" << (n+1) |
| 1772 | << ">;\n"; |
| 1773 | } |
| 1774 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1775 | |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1776 | OutStreamer->EmitRawText(O.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1779 | void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1780 | APFloat APF = APFloat(Fp->getValueAPF()); // make a copy |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1781 | bool ignored; |
| 1782 | unsigned int numHex; |
| 1783 | const char *lead; |
| 1784 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1785 | if (Fp->getType()->getTypeID() == Type::FloatTyID) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1786 | numHex = 8; |
| 1787 | lead = "0f"; |
| Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1788 | APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &ignored); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1789 | } else if (Fp->getType()->getTypeID() == Type::DoubleTyID) { |
| 1790 | numHex = 16; |
| 1791 | lead = "0d"; |
| Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1792 | APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &ignored); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1793 | } else |
| 1794 | llvm_unreachable("unsupported fp type"); |
| 1795 | |
| 1796 | APInt API = APF.bitcastToAPInt(); |
| Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 1797 | O << lead << format_hex_no_prefix(API.getZExtValue(), numHex, /*Upper=*/true); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1800 | void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) { |
| 1801 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1802 | O << CI->getValue(); |
| 1803 | return; |
| 1804 | } |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1805 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1806 | printFPConstant(CFP, O); |
| 1807 | return; |
| 1808 | } |
| 1809 | if (isa<ConstantPointerNull>(CPV)) { |
| 1810 | O << "0"; |
| 1811 | return; |
| 1812 | } |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1813 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1814 | bool IsNonGenericPointer = false; |
| Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1815 | if (GVar->getType()->getAddressSpace() != 0) { |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1816 | IsNonGenericPointer = true; |
| 1817 | } |
| 1818 | if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) { |
| 1819 | O << "generic("; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1820 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1821 | O << ")"; |
| 1822 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1823 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1824 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1825 | return; |
| 1826 | } |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1827 | if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1828 | const Value *v = Cexpr->stripPointerCasts(); |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1829 | PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType()); |
| 1830 | bool IsNonGenericPointer = false; |
| 1831 | if (PTy && PTy->getAddressSpace() != 0) { |
| 1832 | IsNonGenericPointer = true; |
| 1833 | } |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1834 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1835 | if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) { |
| 1836 | O << "generic("; |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1837 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1838 | O << ")"; |
| 1839 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1840 | getSymbol(GVar)->print(O, MAI); |
| Justin Holewinski | 9d852a8 | 2014-04-09 15:39:11 +0000 | [diff] [blame] | 1841 | } |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1842 | return; |
| 1843 | } else { |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 1844 | lowerConstant(CPV)->print(O, MAI); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | } |
| 1848 | llvm_unreachable("Not scalar type found in printScalarConstant()"); |
| 1849 | } |
| 1850 | |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1851 | // These utility functions assure we get the right sequence of bytes for a given |
| 1852 | // type even for big-endian machines |
| 1853 | template <typename T> static void ConvertIntToBytes(unsigned char *p, T val) { |
| 1854 | int64_t vp = (int64_t)val; |
| 1855 | for (unsigned i = 0; i < sizeof(T); ++i) { |
| 1856 | p[i] = (unsigned char)vp; |
| 1857 | vp >>= 8; |
| 1858 | } |
| 1859 | } |
| 1860 | static void ConvertFloatToBytes(unsigned char *p, float val) { |
| 1861 | int32_t *vp = (int32_t *)&val; |
| 1862 | for (unsigned i = 0; i < sizeof(int32_t); ++i) { |
| 1863 | p[i] = (unsigned char)*vp; |
| 1864 | *vp >>= 8; |
| 1865 | } |
| 1866 | } |
| 1867 | static void ConvertDoubleToBytes(unsigned char *p, double val) { |
| 1868 | int64_t *vp = (int64_t *)&val; |
| 1869 | for (unsigned i = 0; i < sizeof(int64_t); ++i) { |
| 1870 | p[i] = (unsigned char)*vp; |
| 1871 | *vp >>= 8; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1875 | void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1876 | AggBuffer *aggBuffer) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1877 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1878 | |
| 1879 | if (isa<UndefValue>(CPV) || CPV->isNullValue()) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1880 | int s = DL.getTypeAllocSize(CPV->getType()); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1881 | if (s < Bytes) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1882 | s = Bytes; |
| 1883 | aggBuffer->addZeros(s); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1887 | unsigned char ptr[8]; |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1888 | switch (CPV->getType()->getTypeID()) { |
| 1889 | |
| 1890 | case Type::IntegerTyID: { |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1891 | Type *ETy = CPV->getType(); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1892 | if (ETy == Type::getInt8Ty(CPV->getContext())) { |
| Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 1893 | unsigned char c = (unsigned char)cast<ConstantInt>(CPV)->getZExtValue(); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1894 | ConvertIntToBytes<>(ptr, c); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1895 | aggBuffer->addBytes(ptr, 1, Bytes); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1896 | } else if (ETy == Type::getInt16Ty(CPV->getContext())) { |
| Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 1897 | short int16 = (short)cast<ConstantInt>(CPV)->getZExtValue(); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1898 | ConvertIntToBytes<>(ptr, int16); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1899 | aggBuffer->addBytes(ptr, 2, Bytes); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1900 | } else if (ETy == Type::getInt32Ty(CPV->getContext())) { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1901 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1902 | int int32 = (int)(constInt->getZExtValue()); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1903 | ConvertIntToBytes<>(ptr, int32); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1904 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1905 | break; |
| David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 1906 | } else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1907 | if (const auto *constInt = dyn_cast_or_null<ConstantInt>( |
| 1908 | ConstantFoldConstant(Cexpr, DL))) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1909 | int int32 = (int)(constInt->getZExtValue()); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1910 | ConvertIntToBytes<>(ptr, int32); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1911 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1912 | break; |
| 1913 | } |
| 1914 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
| 1915 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
| Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1916 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1917 | aggBuffer->addZeros(4); |
| 1918 | break; |
| 1919 | } |
| 1920 | } |
| Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 1921 | llvm_unreachable("unsupported integer const type"); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1922 | } else if (ETy == Type::getInt64Ty(CPV->getContext())) { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1923 | if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1924 | long long int64 = (long long)(constInt->getZExtValue()); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1925 | ConvertIntToBytes<>(ptr, int64); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1926 | aggBuffer->addBytes(ptr, 8, Bytes); |
| 1927 | break; |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1928 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 1929 | if (const auto *constInt = dyn_cast_or_null<ConstantInt>( |
| 1930 | ConstantFoldConstant(Cexpr, DL))) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1931 | long long int64 = (long long)(constInt->getZExtValue()); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1932 | ConvertIntToBytes<>(ptr, int64); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1933 | aggBuffer->addBytes(ptr, 8, Bytes); |
| 1934 | break; |
| 1935 | } |
| 1936 | if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
| 1937 | Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
| Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1938 | aggBuffer->addSymbol(v, Cexpr->getOperand(0)); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1939 | aggBuffer->addZeros(8); |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
| 1943 | llvm_unreachable("unsupported integer const type"); |
| Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 1944 | } else |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1945 | llvm_unreachable("unsupported integer const type"); |
| 1946 | break; |
| 1947 | } |
| Artem Belevich | f256dec | 2018-04-06 22:25:08 +0000 | [diff] [blame] | 1948 | case Type::HalfTyID: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1949 | case Type::FloatTyID: |
| 1950 | case Type::DoubleTyID: { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1951 | const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV); |
| Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 1952 | Type *Ty = CFP->getType(); |
| Artem Belevich | f256dec | 2018-04-06 22:25:08 +0000 | [diff] [blame] | 1953 | if (Ty == Type::getHalfTy(CPV->getContext())) { |
| 1954 | APInt API = CFP->getValueAPF().bitcastToAPInt(); |
| 1955 | uint16_t float16 = API.getLoBits(16).getZExtValue(); |
| 1956 | ConvertIntToBytes<>(ptr, float16); |
| 1957 | aggBuffer->addBytes(ptr, 2, Bytes); |
| 1958 | } else if (Ty == Type::getFloatTy(CPV->getContext())) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1959 | float float32 = (float) CFP->getValueAPF().convertToFloat(); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1960 | ConvertFloatToBytes(ptr, float32); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1961 | aggBuffer->addBytes(ptr, 4, Bytes); |
| 1962 | } else if (Ty == Type::getDoubleTy(CPV->getContext())) { |
| 1963 | double float64 = CFP->getValueAPF().convertToDouble(); |
| Samuel Antao | cd50135 | 2015-06-09 16:29:34 +0000 | [diff] [blame] | 1964 | ConvertDoubleToBytes(ptr, float64); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1965 | aggBuffer->addBytes(ptr, 8, Bytes); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1966 | } else { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1967 | llvm_unreachable("unsupported fp const type"); |
| 1968 | } |
| 1969 | break; |
| 1970 | } |
| 1971 | case Type::PointerTyID: { |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1972 | if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
| Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1973 | aggBuffer->addSymbol(GVar, GVar); |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 1974 | } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
| 1975 | const Value *v = Cexpr->stripPointerCasts(); |
| Jingyue Wu | 312fd02 | 2015-04-24 02:57:30 +0000 | [diff] [blame] | 1976 | aggBuffer->addSymbol(v, Cexpr); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1977 | } |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1978 | unsigned int s = DL.getTypeAllocSize(CPV->getType()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1979 | aggBuffer->addZeros(s); |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | case Type::ArrayTyID: |
| 1984 | case Type::VectorTyID: |
| 1985 | case Type::StructTyID: { |
| Duncan P. N. Exon Smith | 1de3c7e | 2016-04-05 21:10:45 +0000 | [diff] [blame] | 1986 | if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 1987 | int ElementSize = DL.getTypeAllocSize(CPV->getType()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1988 | bufferAggregateConstant(CPV, aggBuffer); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 1989 | if (Bytes > ElementSize) |
| 1990 | aggBuffer->addZeros(Bytes - ElementSize); |
| 1991 | } else if (isa<ConstantAggregateZero>(CPV)) |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1992 | aggBuffer->addZeros(Bytes); |
| 1993 | else |
| 1994 | llvm_unreachable("Unexpected Constant type"); |
| 1995 | break; |
| 1996 | } |
| 1997 | |
| 1998 | default: |
| 1999 | llvm_unreachable("unsupported type"); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| Justin Holewinski | 01f89f0 | 2013-05-20 12:13:32 +0000 | [diff] [blame] | 2003 | void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV, |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2004 | AggBuffer *aggBuffer) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2005 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2006 | int Bytes; |
| 2007 | |
| Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 2008 | // Integers of arbitrary width |
| 2009 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
| 2010 | APInt Val = CI->getValue(); |
| 2011 | for (unsigned I = 0, E = DL.getTypeAllocSize(CPV->getType()); I < E; ++I) { |
| 2012 | uint8_t Byte = Val.getLoBits(8).getZExtValue(); |
| 2013 | aggBuffer->addBytes(&Byte, 1, 1); |
| Craig Topper | fc947bc | 2017-04-18 17:14:21 +0000 | [diff] [blame] | 2014 | Val.lshrInPlace(8); |
| Justin Lebar | 1cf6bf4 | 2017-01-18 00:29:53 +0000 | [diff] [blame] | 2015 | } |
| 2016 | return; |
| 2017 | } |
| 2018 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2019 | // Old constants |
| 2020 | if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) { |
| 2021 | if (CPV->getNumOperands()) |
| 2022 | for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) |
| 2023 | bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer); |
| 2024 | return; |
| 2025 | } |
| 2026 | |
| 2027 | if (const ConstantDataSequential *CDS = |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2028 | dyn_cast<ConstantDataSequential>(CPV)) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2029 | if (CDS->getNumElements()) |
| 2030 | for (unsigned i = 0; i < CDS->getNumElements(); ++i) |
| 2031 | bufferLEByte(cast<Constant>(CDS->getElementAsConstant(i)), 0, |
| 2032 | aggBuffer); |
| 2033 | return; |
| 2034 | } |
| 2035 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2036 | if (isa<ConstantStruct>(CPV)) { |
| 2037 | if (CPV->getNumOperands()) { |
| 2038 | StructType *ST = cast<StructType>(CPV->getType()); |
| 2039 | for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2040 | if (i == (e - 1)) |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2041 | Bytes = DL.getStructLayout(ST)->getElementOffset(0) + |
| 2042 | DL.getTypeAllocSize(ST) - |
| 2043 | DL.getStructLayout(ST)->getElementOffset(i); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2044 | else |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2045 | Bytes = DL.getStructLayout(ST)->getElementOffset(i + 1) - |
| 2046 | DL.getStructLayout(ST)->getElementOffset(i); |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2047 | bufferLEByte(cast<Constant>(CPV->getOperand(i)), Bytes, aggBuffer); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | return; |
| 2051 | } |
| Craig Topper | bdf39a4 | 2012-05-24 07:02:50 +0000 | [diff] [blame] | 2052 | llvm_unreachable("unsupported constant type in printAggregateConstant()"); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | // buildTypeNameMap - Run through symbol table looking for type names. |
| 2056 | // |
| 2057 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2058 | bool NVPTXAsmPrinter::ignoreLoc(const MachineInstr &MI) { |
| 2059 | switch (MI.getOpcode()) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2060 | default: |
| 2061 | return false; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2062 | case NVPTX::CallArgBeginInst: |
| 2063 | case NVPTX::CallArgEndInst0: |
| 2064 | case NVPTX::CallArgEndInst1: |
| 2065 | case NVPTX::CallArgF32: |
| 2066 | case NVPTX::CallArgF64: |
| 2067 | case NVPTX::CallArgI16: |
| 2068 | case NVPTX::CallArgI32: |
| 2069 | case NVPTX::CallArgI32imm: |
| 2070 | case NVPTX::CallArgI64: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2071 | case NVPTX::CallArgParam: |
| 2072 | case NVPTX::CallVoidInst: |
| 2073 | case NVPTX::CallVoidInstReg: |
| 2074 | case NVPTX::Callseq_End: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2075 | case NVPTX::CallVoidInstReg64: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2076 | case NVPTX::DeclareParamInst: |
| 2077 | case NVPTX::DeclareRetMemInst: |
| 2078 | case NVPTX::DeclareRetRegInst: |
| 2079 | case NVPTX::DeclareRetScalarInst: |
| 2080 | case NVPTX::DeclareScalarParamInst: |
| 2081 | case NVPTX::DeclareScalarRegInst: |
| 2082 | case NVPTX::StoreParamF32: |
| 2083 | case NVPTX::StoreParamF64: |
| 2084 | case NVPTX::StoreParamI16: |
| 2085 | case NVPTX::StoreParamI32: |
| 2086 | case NVPTX::StoreParamI64: |
| 2087 | case NVPTX::StoreParamI8: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2088 | case NVPTX::StoreRetvalF32: |
| 2089 | case NVPTX::StoreRetvalF64: |
| 2090 | case NVPTX::StoreRetvalI16: |
| 2091 | case NVPTX::StoreRetvalI32: |
| 2092 | case NVPTX::StoreRetvalI64: |
| 2093 | case NVPTX::StoreRetvalI8: |
| 2094 | case NVPTX::LastCallArgF32: |
| 2095 | case NVPTX::LastCallArgF64: |
| 2096 | case NVPTX::LastCallArgI16: |
| 2097 | case NVPTX::LastCallArgI32: |
| 2098 | case NVPTX::LastCallArgI32imm: |
| 2099 | case NVPTX::LastCallArgI64: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2100 | case NVPTX::LastCallArgParam: |
| 2101 | case NVPTX::LoadParamMemF32: |
| 2102 | case NVPTX::LoadParamMemF64: |
| 2103 | case NVPTX::LoadParamMemI16: |
| 2104 | case NVPTX::LoadParamMemI32: |
| 2105 | case NVPTX::LoadParamMemI64: |
| 2106 | case NVPTX::LoadParamMemI8: |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2107 | case NVPTX::PrototypeInst: |
| 2108 | case NVPTX::DBG_VALUE: |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2109 | return true; |
| 2110 | } |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2114 | /// lowerConstantForGV - Return an MCExpr for the given Constant. This is mostly |
| 2115 | /// a copy from AsmPrinter::lowerConstant, except customized to only handle |
| 2116 | /// expressions that are representable in PTX and create |
| 2117 | /// NVPTXGenericMCSymbolRefExpr nodes for addrspacecast instructions. |
| 2118 | const MCExpr * |
| 2119 | NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric) { |
| 2120 | MCContext &Ctx = OutContext; |
| 2121 | |
| 2122 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2123 | return MCConstantExpr::create(0, Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2124 | |
| 2125 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2126 | return MCConstantExpr::create(CI->getZExtValue(), Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2127 | |
| 2128 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
| 2129 | const MCSymbolRefExpr *Expr = |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2130 | MCSymbolRefExpr::create(getSymbol(GV), Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2131 | if (ProcessingGeneric) { |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2132 | return NVPTXGenericMCSymbolRefExpr::create(Expr, Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2133 | } else { |
| 2134 | return Expr; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV); |
| 2139 | if (!CE) { |
| 2140 | llvm_unreachable("Unknown constant value to lower!"); |
| 2141 | } |
| 2142 | |
| 2143 | switch (CE->getOpcode()) { |
| 2144 | default: |
| 2145 | // If the code isn't optimized, there may be outstanding folding |
| 2146 | // opportunities. Attempt to fold the expression using DataLayout as a |
| 2147 | // last resort before giving up. |
| David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 2148 | if (Constant *C = ConstantFoldConstant(CE, getDataLayout())) |
| 2149 | if (C && C != CE) |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2150 | return lowerConstantForGV(C, ProcessingGeneric); |
| 2151 | |
| 2152 | // Otherwise report the problem to the user. |
| 2153 | { |
| 2154 | std::string S; |
| 2155 | raw_string_ostream OS(S); |
| 2156 | OS << "Unsupported expression in static initializer: "; |
| 2157 | CE->printAsOperand(OS, /*PrintType=*/false, |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2158 | !MF ? nullptr : MF->getFunction().getParent()); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2159 | report_fatal_error(OS.str()); |
| 2160 | } |
| 2161 | |
| 2162 | case Instruction::AddrSpaceCast: { |
| 2163 | // Strip the addrspacecast and pass along the operand |
| 2164 | PointerType *DstTy = cast<PointerType>(CE->getType()); |
| 2165 | if (DstTy->getAddressSpace() == 0) { |
| 2166 | return lowerConstantForGV(cast<const Constant>(CE->getOperand(0)), true); |
| 2167 | } |
| 2168 | std::string S; |
| 2169 | raw_string_ostream OS(S); |
| 2170 | OS << "Unsupported expression in static initializer: "; |
| 2171 | CE->printAsOperand(OS, /*PrintType=*/ false, |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2172 | !MF ? nullptr : MF->getFunction().getParent()); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2173 | report_fatal_error(OS.str()); |
| 2174 | } |
| 2175 | |
| 2176 | case Instruction::GetElementPtr: { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2177 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2178 | |
| 2179 | // Generate a symbolic expression for the byte address |
| 2180 | APInt OffsetAI(DL.getPointerTypeSizeInBits(CE->getType()), 0); |
| 2181 | cast<GEPOperator>(CE)->accumulateConstantOffset(DL, OffsetAI); |
| 2182 | |
| 2183 | const MCExpr *Base = lowerConstantForGV(CE->getOperand(0), |
| 2184 | ProcessingGeneric); |
| 2185 | if (!OffsetAI) |
| 2186 | return Base; |
| 2187 | |
| 2188 | int64_t Offset = OffsetAI.getSExtValue(); |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2189 | return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx), |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2190 | Ctx); |
| 2191 | } |
| 2192 | |
| 2193 | case Instruction::Trunc: |
| 2194 | // We emit the value and depend on the assembler to truncate the generated |
| 2195 | // expression properly. This is important for differences between |
| 2196 | // blockaddress labels. Since the two labels are in the same function, it |
| 2197 | // is reasonable to treat their delta as a 32-bit value. |
| Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 2198 | LLVM_FALLTHROUGH; |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2199 | case Instruction::BitCast: |
| 2200 | return lowerConstantForGV(CE->getOperand(0), ProcessingGeneric); |
| 2201 | |
| 2202 | case Instruction::IntToPtr: { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2203 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2204 | |
| 2205 | // Handle casts to pointers by changing them into casts to the appropriate |
| 2206 | // integer type. This promotes constant folding and simplifies this code. |
| 2207 | Constant *Op = CE->getOperand(0); |
| 2208 | Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()), |
| 2209 | false/*ZExt*/); |
| 2210 | return lowerConstantForGV(Op, ProcessingGeneric); |
| 2211 | } |
| 2212 | |
| 2213 | case Instruction::PtrToInt: { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 2214 | const DataLayout &DL = getDataLayout(); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Support only foldable casts to/from pointers that can be eliminated by |
| 2217 | // changing the pointer to the appropriately sized integer type. |
| 2218 | Constant *Op = CE->getOperand(0); |
| 2219 | Type *Ty = CE->getType(); |
| 2220 | |
| 2221 | const MCExpr *OpExpr = lowerConstantForGV(Op, ProcessingGeneric); |
| 2222 | |
| 2223 | // We can emit the pointer value into this slot if the slot is an |
| 2224 | // integer slot equal to the size of the pointer. |
| 2225 | if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType())) |
| 2226 | return OpExpr; |
| 2227 | |
| 2228 | // Otherwise the pointer is smaller than the resultant integer, mask off |
| 2229 | // the high bits so we are sure to get a proper truncation if the input is |
| 2230 | // a constant expr. |
| 2231 | unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType()); |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2232 | const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx); |
| 2233 | return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | // The MC library also has a right-shift operator, but it isn't consistently |
| 2237 | // signed or unsigned between different targets. |
| 2238 | case Instruction::Add: { |
| 2239 | const MCExpr *LHS = lowerConstantForGV(CE->getOperand(0), ProcessingGeneric); |
| 2240 | const MCExpr *RHS = lowerConstantForGV(CE->getOperand(1), ProcessingGeneric); |
| 2241 | switch (CE->getOpcode()) { |
| 2242 | default: llvm_unreachable("Unknown binary operator constant cast expr"); |
| Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 2243 | case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | // Copy of MCExpr::print customized for NVPTX |
| 2250 | void NVPTXAsmPrinter::printMCExpr(const MCExpr &Expr, raw_ostream &OS) { |
| 2251 | switch (Expr.getKind()) { |
| 2252 | case MCExpr::Target: |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2253 | return cast<MCTargetExpr>(&Expr)->printImpl(OS, MAI); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2254 | case MCExpr::Constant: |
| 2255 | OS << cast<MCConstantExpr>(Expr).getValue(); |
| 2256 | return; |
| 2257 | |
| 2258 | case MCExpr::SymbolRef: { |
| 2259 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(Expr); |
| 2260 | const MCSymbol &Sym = SRE.getSymbol(); |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2261 | Sym.print(OS, MAI); |
| Justin Holewinski | 3d2a976 | 2015-04-28 17:18:30 +0000 | [diff] [blame] | 2262 | return; |
| 2263 | } |
| 2264 | |
| 2265 | case MCExpr::Unary: { |
| 2266 | const MCUnaryExpr &UE = cast<MCUnaryExpr>(Expr); |
| 2267 | switch (UE.getOpcode()) { |
| 2268 | case MCUnaryExpr::LNot: OS << '!'; break; |
| 2269 | case MCUnaryExpr::Minus: OS << '-'; break; |
| 2270 | case MCUnaryExpr::Not: OS << '~'; break; |
| 2271 | case MCUnaryExpr::Plus: OS << '+'; break; |
| 2272 | } |
| 2273 | printMCExpr(*UE.getSubExpr(), OS); |
| 2274 | return; |
| 2275 | } |
| 2276 | |
| 2277 | case MCExpr::Binary: { |
| 2278 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr); |
| 2279 | |
| 2280 | // Only print parens around the LHS if it is non-trivial. |
| 2281 | if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS()) || |
| 2282 | isa<NVPTXGenericMCSymbolRefExpr>(BE.getLHS())) { |
| 2283 | printMCExpr(*BE.getLHS(), OS); |
| 2284 | } else { |
| 2285 | OS << '('; |
| 2286 | printMCExpr(*BE.getLHS(), OS); |
| 2287 | OS<< ')'; |
| 2288 | } |
| 2289 | |
| 2290 | switch (BE.getOpcode()) { |
| 2291 | case MCBinaryExpr::Add: |
| 2292 | // Print "X-42" instead of "X+-42". |
| 2293 | if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { |
| 2294 | if (RHSC->getValue() < 0) { |
| 2295 | OS << RHSC->getValue(); |
| 2296 | return; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | OS << '+'; |
| 2301 | break; |
| 2302 | default: llvm_unreachable("Unhandled binary operator"); |
| 2303 | } |
| 2304 | |
| 2305 | // Only print parens around the LHS if it is non-trivial. |
| 2306 | if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { |
| 2307 | printMCExpr(*BE.getRHS(), OS); |
| 2308 | } else { |
| 2309 | OS << '('; |
| 2310 | printMCExpr(*BE.getRHS(), OS); |
| 2311 | OS << ')'; |
| 2312 | } |
| 2313 | return; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | llvm_unreachable("Invalid expression kind!"); |
| 2318 | } |
| 2319 | |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2320 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 2321 | /// |
| 2322 | bool NVPTXAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 2323 | unsigned AsmVariant, |
| 2324 | const char *ExtraCode, raw_ostream &O) { |
| 2325 | if (ExtraCode && ExtraCode[0]) { |
| 2326 | if (ExtraCode[1] != 0) |
| 2327 | return true; // Unknown modifier. |
| 2328 | |
| 2329 | switch (ExtraCode[0]) { |
| 2330 | default: |
| 2331 | // See if this is a generic print operand |
| 2332 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O); |
| 2333 | case 'r': |
| 2334 | break; |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | printOperand(MI, OpNo, O); |
| 2339 | |
| 2340 | return false; |
| 2341 | } |
| 2342 | |
| 2343 | bool NVPTXAsmPrinter::PrintAsmMemoryOperand( |
| 2344 | const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, |
| 2345 | const char *ExtraCode, raw_ostream &O) { |
| 2346 | if (ExtraCode && ExtraCode[0]) |
| 2347 | return true; // Unknown modifier |
| 2348 | |
| 2349 | O << '['; |
| 2350 | printMemOperand(MI, OpNo, O); |
| 2351 | O << ']'; |
| 2352 | |
| 2353 | return false; |
| 2354 | } |
| 2355 | |
| 2356 | void NVPTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 2357 | raw_ostream &O, const char *Modifier) { |
| 2358 | const MachineOperand &MO = MI->getOperand(opNum); |
| 2359 | switch (MO.getType()) { |
| 2360 | case MachineOperand::MO_Register: |
| 2361 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { |
| 2362 | if (MO.getReg() == NVPTX::VRDepot) |
| 2363 | O << DEPOTNAME << getFunctionNumber(); |
| 2364 | else |
| 2365 | O << NVPTXInstPrinter::getRegisterName(MO.getReg()); |
| 2366 | } else { |
| Justin Holewinski | 660597d | 2013-10-11 12:39:36 +0000 | [diff] [blame] | 2367 | emitVirtualRegister(MO.getReg(), O); |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2368 | } |
| 2369 | return; |
| 2370 | |
| 2371 | case MachineOperand::MO_Immediate: |
| 2372 | if (!Modifier) |
| 2373 | O << MO.getImm(); |
| 2374 | else if (strstr(Modifier, "vec") == Modifier) |
| 2375 | printVecModifiedImmediate(MO, Modifier, O); |
| 2376 | else |
| 2377 | llvm_unreachable( |
| 2378 | "Don't know how to handle modifier on immediate operand"); |
| 2379 | return; |
| 2380 | |
| 2381 | case MachineOperand::MO_FPImmediate: |
| 2382 | printFPConstant(MO.getFPImm(), O); |
| 2383 | break; |
| 2384 | |
| 2385 | case MachineOperand::MO_GlobalAddress: |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2386 | getSymbol(MO.getGlobal())->print(O, MAI); |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2389 | case MachineOperand::MO_MachineBasicBlock: |
| Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 2390 | MO.getMBB()->getSymbol()->print(O, MAI); |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | |
| 2393 | default: |
| 2394 | llvm_unreachable("Operand type not supported."); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 2399 | raw_ostream &O, const char *Modifier) { |
| 2400 | printOperand(MI, opNum, O); |
| 2401 | |
| Eugene Zelenko | c9f1f6b | 2017-01-09 22:16:51 +0000 | [diff] [blame] | 2402 | if (Modifier && strcmp(Modifier, "add") == 0) { |
| Justin Holewinski | aaa8b6e | 2013-08-24 01:17:23 +0000 | [diff] [blame] | 2403 | O << ", "; |
| 2404 | printOperand(MI, opNum + 1, O); |
| 2405 | } else { |
| 2406 | if (MI->getOperand(opNum + 1).isImm() && |
| 2407 | MI->getOperand(opNum + 1).getImm() == 0) |
| 2408 | return; // don't print ',0' or '+0' |
| 2409 | O << "+"; |
| 2410 | printOperand(MI, opNum + 1, O); |
| 2411 | } |
| 2412 | } |
| 2413 | |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2414 | void NVPTXAsmPrinter::emitSrcInText(StringRef filename, unsigned line) { |
| 2415 | std::stringstream temp; |
| Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 2416 | LineReader *reader = this->getReader(filename); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2417 | temp << "\n//"; |
| 2418 | temp << filename.str(); |
| 2419 | temp << ":"; |
| 2420 | temp << line; |
| 2421 | temp << " "; |
| 2422 | temp << reader->readLine(line); |
| 2423 | temp << "\n"; |
| Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 2424 | this->OutStreamer->EmitRawText(temp.str()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 2427 | LineReader *NVPTXAsmPrinter::getReader(const std::string &filename) { |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2428 | if (!reader) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2429 | reader = new LineReader(filename); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | if (reader->fileName() != filename) { |
| 2433 | delete reader; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2434 | reader = new LineReader(filename); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | return reader; |
| 2438 | } |
| 2439 | |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2440 | std::string LineReader::readLine(unsigned lineNum) { |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2441 | if (lineNum < theCurLine) { |
| 2442 | theCurLine = 0; |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2443 | fstr.seekg(0, std::ios::beg); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2444 | } |
| 2445 | while (theCurLine < lineNum) { |
| Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 2446 | fstr.getline(buff, 500); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2447 | theCurLine++; |
| 2448 | } |
| 2449 | return buff; |
| 2450 | } |
| 2451 | |
| 2452 | // Force static initialization. |
| 2453 | extern "C" void LLVMInitializeNVPTXAsmPrinter() { |
| Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 2454 | RegisterAsmPrinter<NVPTXAsmPrinter> X(getTheNVPTXTarget32()); |
| 2455 | RegisterAsmPrinter<NVPTXAsmPrinter> Y(getTheNVPTXTarget64()); |
| Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 2456 | } |