1//===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to NVPTX assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
15#define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
16
17#include "NVPTX.h"
18#include "NVPTXSubtarget.h"
19#include "NVPTXTargetMachine.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DebugLoc.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalAlias.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/Pass.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"
42#include <algorithm>
43#include <cassert>
44#include <map>
45#include <memory>
46#include <string>
47#include <vector>
48
49// The ptx syntax and format is very different from that usually seem in a .s
50// file,
51// therefore we are not able to use the MCAsmStreamer interface here.
52//
53// We are handcrafting the output method here.
54//
55// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
56// (subclass of MCStreamer).
57
58namespace llvm {
59
60class MCOperand;
61class NVPTXTargetStreamer;
62
63class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
64
65 class AggBuffer {
66 // Used to buffer the emitted string for initializing global aggregates.
67 //
68 // Normally an aggregate (array, vector, or structure) is emitted as a u8[].
69 // However, if either element/field of the aggregate is a non-NULL address,
70 // and all such addresses are properly aligned, then the aggregate is
71 // emitted as u32[] or u64[]. In the case of unaligned addresses, the
72 // aggregate is emitted as u8[], and the mask() operator is used for all
73 // pointers.
74 //
75 // We first layout the aggregate in 'buffer' in bytes, except for those
76 // symbol addresses. For the i-th symbol address in the aggregate, its
77 // corresponding 4-byte or 8-byte elements in 'buffer' are filled with 0s.
78 // symbolPosInBuffer[i-1] records its position in 'buffer', and Symbols[i-1]
79 // records the Value*.
80 //
81 // Once we have this AggBuffer setup, we can choose how to print it out.
82 public:
83 // number of symbol addresses
84 unsigned numSymbols() const { return Symbols.size(); }
85
86 bool allSymbolsAligned(unsigned ptrSize) const {
87 return llvm::all_of(Range: symbolPosInBuffer,
88 P: [=](unsigned pos) { return pos % ptrSize == 0; });
89 }
90
91 private:
92 const unsigned Size; // size of the buffer in bytes
93 std::vector<unsigned char> buffer; // the buffer
94 SmallVector<unsigned, 4> symbolPosInBuffer;
95 SmallVector<const Value *, 4> Symbols;
96 // SymbolsBeforeStripping[i] is the original form of Symbols[i] before
97 // stripping pointer casts, i.e.,
98 // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().
99 //
100 // We need to keep these values because AggBuffer::print decides whether to
101 // emit a "generic()" cast for Symbols[i] depending on the address space of
102 // SymbolsBeforeStripping[i].
103 SmallVector<const Value *, 4> SymbolsBeforeStripping;
104 unsigned curpos;
105 const NVPTXAsmPrinter &AP;
106 const bool EmitGeneric;
107
108 public:
109 AggBuffer(unsigned Size, const NVPTXAsmPrinter &AP)
110 : Size(Size), buffer(Size), curpos(0), AP(AP),
111 EmitGeneric(AP.EmitGeneric) {}
112
113 unsigned getBufferSize() const { return Size; }
114
115 // Number of bytes written so far.
116 unsigned getCurpos() const { return curpos; }
117
118 // Copy Num bytes from Ptr.
119 // if Bytes > Num, zero fill up to Bytes.
120 void addBytes(const unsigned char *Ptr, unsigned Num, unsigned Bytes) {
121 for (unsigned I : llvm::seq(Size: Num))
122 addByte(Byte: Ptr[I]);
123 if (Bytes > Num)
124 addZeros(Num: Bytes - Num);
125 }
126
127 void addByte(uint8_t Byte) {
128 assert(curpos < Size);
129 buffer[curpos] = Byte;
130 curpos++;
131 }
132
133 void addZeros(unsigned Num) {
134 for ([[maybe_unused]] unsigned _ : llvm::seq(Size: Num)) {
135 addByte(Byte: 0);
136 }
137 }
138
139 void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {
140 symbolPosInBuffer.push_back(Elt: curpos);
141 Symbols.push_back(Elt: GVar);
142 SymbolsBeforeStripping.push_back(Elt: GVarBeforeStripping);
143 }
144
145 void printBytes(raw_ostream &os);
146 void printWords(raw_ostream &os);
147
148 private:
149 void printSymbol(unsigned nSym, raw_ostream &os);
150 };
151
152 friend class AggBuffer;
153
154public:
155 static char ID;
156
157 StringRef getPassName() const override { return "NVPTX Assembly Printer"; }
158
159private:
160 const Function *F;
161
162 NVPTXTargetStreamer *getTargetStreamer() const;
163
164 void emitStartOfAsmFile(Module &M) override;
165 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
166 void emitFunctionEntryLabel() override;
167 void emitFunctionBodyStart() override;
168 void emitFunctionBodyEnd() override;
169 void emitImplicitDef(const MachineInstr *MI) const override;
170
171 void emitInstruction(const MachineInstr *) override;
172 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
173 MCOperand lowerOperand(const MachineOperand &MO);
174 MCOperand GetSymbolRef(const MCSymbol *Symbol);
175 unsigned encodeVirtualRegister(unsigned Reg);
176
177 void printMemOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O,
178 const char *Modifier = nullptr);
179 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
180 bool processDemoted, const NVPTXSubtarget &STI);
181 void emitGlobals(const Module &M);
182 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;
183 void emitHeader(Module &M, const NVPTXSubtarget &STI);
184 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
185 void emitVirtualRegister(unsigned int vr, raw_ostream &);
186 void emitFunctionParamList(const Function *, raw_ostream &O);
187 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
188 void encodeDebugInfoRegisterNumbers(const MachineFunction &MF);
189 void printReturnValStr(const Function *, raw_ostream &O);
190 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
191 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
192 const char *ExtraCode, raw_ostream &) override;
193 void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O);
194 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
195 const char *ExtraCode, raw_ostream &) override;
196
197 const MCExpr *lowerConstantForGV(const Constant *CV,
198 bool ProcessingGeneric) const;
199 void printMCExpr(const MCExpr &Expr, raw_ostream &OS) const;
200 /// Emit a blob of inline asm to the output streamer.
201 void emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
202 const MCTargetOptions &MCOptions, const MDNode *LocMDNode,
203 InlineAsm::AsmDialect Dialect,
204 const MachineInstr *MI) override;
205
206protected:
207 bool doInitialization(Module &M) override;
208 bool doFinalization(Module &M) override;
209
210 /// Create NVPTX-specific DwarfDebug handler.
211 DwarfDebug *createDwarfDebug() override;
212
213private:
214 bool GlobalsEmitted;
215
216 // This is specific per MachineFunction.
217 const MachineRegisterInfo *MRI;
218 // The contents are specific for each
219 // MachineFunction. But the size of the
220 // array is not.
221 typedef DenseMap<unsigned, unsigned> VRegMap;
222 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
223 VRegRCMap VRegMapping;
224
225 // List of variables demoted to a function scope.
226 std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;
227
228 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O,
229 const NVPTXSubtarget &STI);
230 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
231 std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;
232 void printScalarConstant(const Constant *CPV, raw_ostream &O);
233 void printFPConstant(const ConstantFP *Fp, raw_ostream &O) const;
234 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
235 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
236 void bufferAggregateConstVec(const ConstantVector *CV, AggBuffer *aggBuffer);
237
238 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
239 void emitDeclarations(const Module &, raw_ostream &O);
240 void emitDeclaration(const Function *, raw_ostream &O);
241 void emitAliasDeclaration(const GlobalAlias *, raw_ostream &O);
242 void emitDeclarationWithName(const Function *, MCSymbol *, raw_ostream &O);
243 void emitDemotedVars(const Function *, raw_ostream &);
244
245 bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;
246
247 // Used to control the need to emit .generic() in the initializer of
248 // module scope variables.
249 // Although ptx supports the hybrid mode like the following,
250 // .global .u32 a;
251 // .global .u32 b;
252 // .global .u32 addr[] = {a, generic(b)}
253 // we have difficulty representing the difference in the NVVM IR.
254 //
255 // Since the address value should always be generic in CUDA C and always
256 // be specific in OpenCL, we use this simple control here.
257 //
258 const bool EmitGeneric;
259
260public:
261 NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
262 : AsmPrinter(TM, std::move(Streamer), ID),
263 EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==
264 NVPTX::CUDA) {}
265
266 bool runOnMachineFunction(MachineFunction &F) override;
267
268 void getAnalysisUsage(AnalysisUsage &AU) const override {
269 AU.addRequired<MachineLoopInfoWrapperPass>();
270 AsmPrinter::getAnalysisUsage(AU);
271 }
272
273 std::string getVirtualRegisterName(unsigned) const;
274
275 const MCSymbol *getFunctionFrameSymbol() const override;
276
277 // Make emitGlobalVariable() no-op for NVPTX.
278 // Global variables have been already emitted by the time the base AsmPrinter
279 // attempts to do so in doFinalization() (see NVPTXAsmPrinter::emitGlobals()).
280 void emitGlobalVariable(const GlobalVariable *GV) override {}
281};
282
283} // end namespace llvm
284
285#endif // LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
286