1 //===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to NVPTX assembly language.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
16 #define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
19 #include "NVPTXSubtarget.h"
20 #include "NVPTXTargetMachine.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineLoopInfo.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DebugLoc.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/MC/MCExpr.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/PassAnalysisSupport.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/TargetMachine.h"
49 // The ptx syntax and format is very different from that usually seem in a .s
51 // therefore we are not able to use the MCAsmStreamer interface here.
53 // We are handcrafting the output method here.
55 // A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
56 // (subclass of MCStreamer).
62 class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
65 // Used to buffer the emitted string for initializing global
68 // Normally an aggregate (array, vector or structure) is emitted
69 // as a u8[]. However, if one element/field of the aggregate
70 // is a non-NULL address, then the aggregate is emitted as u32[]
73 // We first layout the aggregate in 'buffer' in bytes, except for
74 // those symbol addresses. For the i-th symbol address in the
75 //aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
76 // are filled with 0s. symbolPosInBuffer[i-1] records its position
77 // in 'buffer', and Symbols[i-1] records the Value*.
79 // Once we have this AggBuffer setup, we can choose how to print
82 unsigned numSymbols; // number of symbol addresses
85 const unsigned size; // size of the buffer in bytes
86 std::vector<unsigned char> buffer; // the buffer
87 SmallVector<unsigned, 4> symbolPosInBuffer;
88 SmallVector<const Value *, 4> Symbols;
89 // SymbolsBeforeStripping[i] is the original form of Symbols[i] before
90 // stripping pointer casts, i.e.,
91 // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().
93 // We need to keep these values because AggBuffer::print decides whether to
94 // emit a "generic()" cast for Symbols[i] depending on the address space of
95 // SymbolsBeforeStripping[i].
96 SmallVector<const Value *, 4> SymbolsBeforeStripping;
103 AggBuffer(unsigned size, raw_ostream &O, NVPTXAsmPrinter &AP)
104 : size(size), buffer(size), O(O), AP(AP) {
107 EmitGeneric = AP.EmitGeneric;
110 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
111 assert((curpos + Num) <= size);
112 assert((curpos + Bytes) <= size);
113 for (int i = 0; i < Num; ++i) {
114 buffer[curpos] = Ptr[i];
117 for (int i = Num; i < Bytes; ++i) {
124 unsigned addZeros(int Num) {
125 assert((curpos + Num) <= size);
126 for (int i = 0; i < Num; ++i) {
133 void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {
134 symbolPosInBuffer.push_back(curpos);
135 Symbols.push_back(GVar);
136 SymbolsBeforeStripping.push_back(GVarBeforeStripping);
141 if (numSymbols == 0) {
142 // print out in bytes
143 for (unsigned i = 0; i < size; i++) {
146 O << (unsigned int) buffer[i];
149 // print out in 4-bytes or 8-bytes
150 unsigned int pos = 0;
151 unsigned int nSym = 0;
152 unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
153 unsigned int nBytes = 4;
154 if (static_cast<const NVPTXTargetMachine &>(AP.TM).is64Bit())
156 for (pos = 0; pos < size; pos += nBytes) {
159 if (pos == nextSymbolPos) {
160 const Value *v = Symbols[nSym];
161 const Value *v0 = SymbolsBeforeStripping[nSym];
162 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
163 MCSymbol *Name = AP.getSymbol(GVar);
164 PointerType *PTy = dyn_cast<PointerType>(v0->getType());
165 bool IsNonGenericPointer = false; // Is v0 a non-generic pointer?
166 if (PTy && PTy->getAddressSpace() != 0) {
167 IsNonGenericPointer = true;
169 if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
171 Name->print(O, AP.MAI);
174 Name->print(O, AP.MAI);
176 } else if (const ConstantExpr *CExpr = dyn_cast<ConstantExpr>(v0)) {
178 AP.lowerConstantForGV(cast<Constant>(CExpr), false);
179 AP.printMCExpr(*Expr, O);
181 llvm_unreachable("symbol type unknown");
183 if (nSym >= numSymbols)
184 nextSymbolPos = size + 1;
186 nextSymbolPos = symbolPosInBuffer[nSym];
187 } else if (nBytes == 4)
188 O << *(unsigned int *)(&buffer[pos]);
190 O << *(unsigned long long *)(&buffer[pos]);
196 friend class AggBuffer;
199 StringRef getPassName() const override { return "NVPTX Assembly Printer"; }
202 std::string CurrentFnName;
204 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
205 void EmitFunctionEntryLabel() override;
206 void EmitFunctionBodyStart() override;
207 void EmitFunctionBodyEnd() override;
208 void emitImplicitDef(const MachineInstr *MI) const override;
210 void EmitInstruction(const MachineInstr *) override;
211 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
212 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
213 MCOperand GetSymbolRef(const MCSymbol *Symbol);
214 unsigned encodeVirtualRegister(unsigned Reg);
216 void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
218 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
219 const char *Modifier = nullptr);
220 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
222 void printParamName(Function::const_arg_iterator I, int paramIndex,
224 void emitGlobals(const Module &M);
225 void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI);
226 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
227 void emitVirtualRegister(unsigned int vr, raw_ostream &);
228 void emitFunctionParamList(const Function *, raw_ostream &O);
229 void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
230 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
231 void printReturnValStr(const Function *, raw_ostream &O);
232 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
233 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
234 unsigned AsmVariant, const char *ExtraCode,
235 raw_ostream &) override;
236 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
237 const char *Modifier = nullptr);
238 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
239 unsigned AsmVariant, const char *ExtraCode,
240 raw_ostream &) override;
242 const MCExpr *lowerConstantForGV(const Constant *CV, bool ProcessingGeneric);
243 void printMCExpr(const MCExpr &Expr, raw_ostream &OS);
246 bool doInitialization(Module &M) override;
247 bool doFinalization(Module &M) override;
252 // This is specific per MachineFunction.
253 const MachineRegisterInfo *MRI;
254 // The contents are specific for each
255 // MachineFunction. But the size of the
257 typedef DenseMap<unsigned, unsigned> VRegMap;
258 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
259 VRegRCMap VRegMapping;
261 // List of variables demoted to a function scope.
262 std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;
264 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
265 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
266 std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;
267 void printScalarConstant(const Constant *CPV, raw_ostream &O);
268 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
269 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
270 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
272 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
273 void emitDeclarations(const Module &, raw_ostream &O);
274 void emitDeclaration(const Function *, raw_ostream &O);
275 void emitDemotedVars(const Function *, raw_ostream &);
277 bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo,
279 void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp);
281 bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;
283 // Used to control the need to emit .generic() in the initializer of
284 // module scope variables.
285 // Although ptx supports the hybrid mode like the following,
288 // .global .u32 addr[] = {a, generic(b)}
289 // we have difficulty representing the difference in the NVVM IR.
291 // Since the address value should always be generic in CUDA C and always
292 // be specific in OpenCL, we use this simple control here.
297 NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
298 : AsmPrinter(TM, std::move(Streamer)),
299 EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==
302 bool runOnMachineFunction(MachineFunction &F) override;
304 void getAnalysisUsage(AnalysisUsage &AU) const override {
305 AU.addRequired<MachineLoopInfo>();
306 AsmPrinter::getAnalysisUsage(AU);
309 std::string getVirtualRegisterName(unsigned) const;
311 const MCSymbol *getFunctionFrameSymbol() const override;
314 } // end namespace llvm
316 #endif // LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H