Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 1 | //===-- PTXAsmPrinter.cpp - PTX 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 PTX assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "ptx-asm-printer" |
| 16 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 17 | #include "PTX.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 18 | #include "PTXMachineFunctionInfo.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 19 | #include "PTXTargetMachine.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Module.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/AsmPrinter.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstr.h" |
| 27 | #include "llvm/MC/MCStreamer.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbol.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 29 | #include "llvm/Target/Mangler.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetRegistry.h" |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/Support/ErrorHandling.h" |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 40 | static cl::opt<std::string> |
| 41 | OptPTXVersion("ptx-version", cl::desc("Set PTX version"), |
| 42 | cl::init("1.4")); |
| 43 | |
| 44 | static cl::opt<std::string> |
| 45 | OptPTXTarget("ptx-target", cl::desc("Set GPU target (comma-separated list)"), |
| 46 | cl::init("sm_10")); |
| 47 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 48 | namespace { |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 49 | class PTXAsmPrinter : public AsmPrinter { |
| 50 | public: |
| 51 | explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 52 | : AsmPrinter(TM, Streamer) {} |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 53 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 54 | const char *getPassName() const { return "PTX Assembly Printer"; } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 55 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 56 | bool doFinalization(Module &M); |
| 57 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 58 | virtual void EmitStartOfAsmFile(Module &M); |
| 59 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 60 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 61 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 62 | virtual void EmitFunctionBodyStart(); |
| 63 | virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); } |
| 64 | |
| 65 | virtual void EmitInstruction(const MachineInstr *MI); |
| 66 | |
| 67 | void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 68 | void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS, |
| 69 | const char *Modifier = 0); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 70 | |
| 71 | // autogen'd. |
| 72 | void printInstruction(const MachineInstr *MI, raw_ostream &OS); |
| 73 | static const char *getRegisterName(unsigned RegNo); |
| 74 | |
| 75 | private: |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 76 | void EmitVariableDeclaration(const GlobalVariable *gv); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 77 | void EmitFunctionDeclaration(); |
| 78 | }; // class PTXAsmPrinter |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 81 | static const char PARAM_PREFIX[] = "__param_"; |
| 82 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 83 | static const char *getRegisterTypeName(unsigned RegNo) { |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 84 | #define TEST_REGCLS(cls, clsstr) \ |
| 85 | if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr; |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 86 | TEST_REGCLS(RRegs32, s32); |
| 87 | TEST_REGCLS(Preds, pred); |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 88 | #undef TEST_REGCLS |
| 89 | |
| 90 | llvm_unreachable("Not in any register class!"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 94 | static const char *getInstructionTypeName(const MachineInstr *MI) { |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 95 | for (int i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 96 | const MachineOperand &MO = MI->getOperand(i); |
| 97 | if (MO.getType() == MachineOperand::MO_Register) |
| 98 | return getRegisterTypeName(MO.getReg()); |
| 99 | } |
| 100 | |
| 101 | llvm_unreachable("No reg operand found in instruction!"); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 105 | static const char *getStateSpaceName(unsigned addressSpace) { |
| 106 | if (addressSpace <= 255) |
| 107 | return "global"; |
| 108 | // TODO Add more state spaces |
| 109 | |
| 110 | llvm_unreachable("Unknown state space"); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | bool PTXAsmPrinter::doFinalization(Module &M) { |
| 115 | // XXX Temproarily remove global variables so that doFinalization() will not |
| 116 | // emit them again (global variables are emitted at beginning). |
| 117 | |
| 118 | Module::GlobalListType &global_list = M.getGlobalList(); |
| 119 | int i, n = global_list.size(); |
| 120 | GlobalVariable **gv_array = new GlobalVariable* [n]; |
| 121 | |
| 122 | // first, back-up GlobalVariable in gv_array |
| 123 | i = 0; |
| 124 | for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
| 125 | I != E; ++I) |
| 126 | gv_array[i++] = &*I; |
| 127 | |
| 128 | // second, empty global_list |
| 129 | while (!global_list.empty()) |
| 130 | global_list.remove(global_list.begin()); |
| 131 | |
| 132 | // call doFinalization |
| 133 | bool ret = AsmPrinter::doFinalization(M); |
| 134 | |
| 135 | // now we restore global variables |
| 136 | for (i = 0; i < n; i ++) |
| 137 | global_list.insert(global_list.end(), gv_array[i]); |
| 138 | |
| 139 | delete[] gv_array; |
| 140 | return ret; |
| 141 | } |
| 142 | |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 143 | void PTXAsmPrinter::EmitStartOfAsmFile(Module &M) |
| 144 | { |
| 145 | OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion)); |
| 146 | OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget)); |
| 147 | OutStreamer.AddBlankLine(); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 148 | |
| 149 | // declare global variables |
| 150 | for (Module::const_global_iterator i = M.global_begin(), e = M.global_end(); |
| 151 | i != e; ++i) |
| 152 | EmitVariableDeclaration(i); |
Che-Liang Chiou | 21d8b9b | 2010-11-30 10:14:14 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 155 | bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 156 | SetupMachineFunction(MF); |
| 157 | EmitFunctionDeclaration(); |
| 158 | EmitFunctionBody(); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | void PTXAsmPrinter::EmitFunctionBodyStart() { |
| 163 | OutStreamer.EmitRawText(Twine("{")); |
| 164 | |
| 165 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 166 | |
| 167 | // Print local variable definition |
| 168 | for (PTXMachineFunctionInfo::reg_iterator |
| 169 | i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) { |
| 170 | unsigned reg = *i; |
| 171 | |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 172 | std::string def = "\t.reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 173 | def += getRegisterTypeName(reg); |
| 174 | def += ' '; |
| 175 | def += getRegisterName(reg); |
| 176 | def += ';'; |
| 177 | OutStreamer.EmitRawText(Twine(def)); |
| 178 | } |
| 179 | } |
| 180 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 181 | void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 182 | std::string str; |
| 183 | str.reserve(64); |
| 184 | |
| 185 | // Write instruction to str |
| 186 | raw_string_ostream OS(str); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 187 | printInstruction(MI, OS); |
| 188 | OS << ';'; |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 189 | OS.flush(); |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 190 | |
| 191 | // Replace "%type" if found |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 192 | size_t pos; |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 193 | if ((pos = str.find("%type")) != std::string::npos) |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 194 | str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI)); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 195 | |
Che-Liang Chiou | 3608d2a | 2010-12-01 11:45:53 +0000 | [diff] [blame] | 196 | StringRef strref = StringRef(str); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 197 | OutStreamer.EmitRawText(strref); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum, |
| 201 | raw_ostream &OS) { |
| 202 | const MachineOperand &MO = MI->getOperand(opNum); |
| 203 | |
| 204 | switch (MO.getType()) { |
| 205 | default: |
| 206 | llvm_unreachable("<unknown operand type>"); |
| 207 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 208 | case MachineOperand::MO_GlobalAddress: |
| 209 | OS << *Mang->getSymbol(MO.getGlobal()); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 210 | break; |
| 211 | case MachineOperand::MO_Immediate: |
| 212 | OS << (int) MO.getImm(); |
| 213 | break; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 214 | case MachineOperand::MO_Register: |
| 215 | OS << getRegisterName(MO.getReg()); |
| 216 | break; |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 217 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 220 | void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, |
| 221 | raw_ostream &OS, const char *Modifier) { |
| 222 | printOperand(MI, opNum, OS); |
| 223 | |
| 224 | if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0) |
| 225 | return; // don't print "+0" |
| 226 | |
| 227 | OS << "+"; |
| 228 | printOperand(MI, opNum+1, OS); |
| 229 | } |
| 230 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 231 | void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) { |
| 232 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 233 | if (EmitSpecialLLVMGlobal(gv)) |
| 234 | return; |
| 235 | |
| 236 | MCSymbol *gvsym = Mang->getSymbol(gv); |
| 237 | |
| 238 | assert(gvsym->isUndefined() && "Cannot define a symbol twice!"); |
| 239 | |
| 240 | std::string decl; |
| 241 | |
| 242 | // check if it is defined in some other translation unit |
| 243 | if (gv->isDeclaration()) |
| 244 | decl += ".extern "; |
| 245 | |
| 246 | // state space: e.g., .global |
| 247 | decl += "."; |
| 248 | decl += getStateSpaceName(gv->getType()->getAddressSpace()); |
| 249 | decl += " "; |
| 250 | |
| 251 | // alignment (optional) |
| 252 | unsigned alignment = gv->getAlignment(); |
| 253 | if (alignment != 0) { |
| 254 | decl += ".align "; |
| 255 | decl += utostr(Log2_32(gv->getAlignment())); |
| 256 | decl += " "; |
| 257 | } |
| 258 | |
| 259 | // TODO: add types |
| 260 | decl += ".s32 "; |
| 261 | |
| 262 | decl += gvsym->getName(); |
| 263 | |
| 264 | if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType())) |
| 265 | decl += "[]"; |
| 266 | |
| 267 | decl += ";"; |
| 268 | |
| 269 | OutStreamer.EmitRawText(Twine(decl)); |
| 270 | |
| 271 | OutStreamer.AddBlankLine(); |
| 272 | } |
| 273 | |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 274 | void PTXAsmPrinter::EmitFunctionDeclaration() { |
| 275 | // The function label could have already been emitted if two symbols end up |
| 276 | // conflicting due to asm renaming. Detect this and emit an error. |
| 277 | if (!CurrentFnSym->isUndefined()) { |
| 278 | report_fatal_error("'" + Twine(CurrentFnSym->getName()) + |
| 279 | "' label emitted multiple times to assembly file"); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>(); |
| 284 | const bool isKernel = MFI->isKernel(); |
| 285 | unsigned reg; |
| 286 | |
| 287 | std::string decl = isKernel ? ".entry" : ".func"; |
| 288 | |
| 289 | // Print return register |
| 290 | reg = MFI->retReg(); |
| 291 | if (!isKernel && reg != PTX::NoRegister) { |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 292 | decl += " (.reg ."; // FIXME: could it return in .param space? |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 293 | decl += getRegisterTypeName(reg); |
| 294 | decl += " "; |
| 295 | decl += getRegisterName(reg); |
| 296 | decl += ")"; |
| 297 | } |
| 298 | |
| 299 | // Print function name |
| 300 | decl += " "; |
| 301 | decl += CurrentFnSym->getName().str(); |
| 302 | |
| 303 | // Print parameter list |
| 304 | if (!MFI->argRegEmpty()) { |
| 305 | decl += " ("; |
| 306 | if (isKernel) { |
| 307 | for (int i = 0, e = MFI->getNumArg(); i != e; ++i) { |
| 308 | if (i != 0) |
| 309 | decl += ", "; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 310 | decl += ".param .s32 "; // TODO: add types |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 311 | decl += PARAM_PREFIX; |
| 312 | decl += utostr(i + 1); |
| 313 | } |
| 314 | } else { |
| 315 | for (PTXMachineFunctionInfo::reg_iterator |
| 316 | i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) { |
| 317 | reg = *i; |
| 318 | assert(reg != PTX::NoRegister && "Not a valid register!"); |
| 319 | if (i != b) |
| 320 | decl += ", "; |
Che-Liang Chiou | 3f409f7 | 2010-11-17 08:08:49 +0000 | [diff] [blame] | 321 | decl += ".reg ."; |
Che-Liang Chiou | df65963 | 2010-11-08 03:06:08 +0000 | [diff] [blame] | 322 | decl += getRegisterTypeName(reg); |
| 323 | decl += " "; |
| 324 | decl += getRegisterName(reg); |
| 325 | } |
| 326 | } |
| 327 | decl += ")"; |
| 328 | } |
| 329 | |
| 330 | OutStreamer.EmitRawText(Twine(decl)); |
| 331 | } |
| 332 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 333 | #include "PTXGenAsmWriter.inc" |
| 334 | |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 335 | // Force static initialization. |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 336 | extern "C" void LLVMInitializePTXAsmPrinter() { |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 337 | RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget); |
| 338 | } |