LLVM 18.0.0git
NVPTXAsmPrinter.h
Go to the documentation of this file.
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"
22#include "llvm/ADT/StringRef.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DebugLoc.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalValue.h"
31#include "llvm/IR/Value.h"
32#include "llvm/MC/MCExpr.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
35#include "llvm/Pass.h"
41#include <algorithm>
42#include <cassert>
43#include <map>
44#include <memory>
45#include <string>
46#include <vector>
47
48// The ptx syntax and format is very different from that usually seem in a .s
49// file,
50// therefore we are not able to use the MCAsmStreamer interface here.
51//
52// We are handcrafting the output method here.
53//
54// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
55// (subclass of MCStreamer).
56
57namespace llvm {
58
59class MCOperand;
60
62
63 class AggBuffer {
64 // Used to buffer the emitted string for initializing global aggregates.
65 //
66 // Normally an aggregate (array, vector, or structure) is emitted as a u8[].
67 // However, if either element/field of the aggregate is a non-NULL address,
68 // and all such addresses are properly aligned, then the aggregate is
69 // emitted as u32[] or u64[]. In the case of unaligned addresses, the
70 // aggregate is emitted as u8[], and the mask() operator is used for all
71 // pointers.
72 //
73 // We first layout the aggregate in 'buffer' in bytes, except for those
74 // symbol addresses. For the i-th symbol address in the aggregate, its
75 // corresponding 4-byte or 8-byte elements in 'buffer' are filled with 0s.
76 // symbolPosInBuffer[i-1] records its position in 'buffer', and Symbols[i-1]
77 // records the Value*.
78 //
79 // Once we have this AggBuffer setup, we can choose how to print it out.
80 public:
81 // number of symbol addresses
82 unsigned numSymbols() const { return Symbols.size(); }
83
84 bool allSymbolsAligned(unsigned ptrSize) const {
85 return llvm::all_of(symbolPosInBuffer,
86 [=](unsigned pos) { return pos % ptrSize == 0; });
87 }
88
89 private:
90 const unsigned size; // size of the buffer in bytes
91 std::vector<unsigned char> buffer; // the buffer
92 SmallVector<unsigned, 4> symbolPosInBuffer;
94 // SymbolsBeforeStripping[i] is the original form of Symbols[i] before
95 // stripping pointer casts, i.e.,
96 // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().
97 //
98 // We need to keep these values because AggBuffer::print decides whether to
99 // emit a "generic()" cast for Symbols[i] depending on the address space of
100 // SymbolsBeforeStripping[i].
101 SmallVector<const Value *, 4> SymbolsBeforeStripping;
102 unsigned curpos;
103 NVPTXAsmPrinter &AP;
104 bool EmitGeneric;
105
106 public:
107 AggBuffer(unsigned size, NVPTXAsmPrinter &AP)
108 : size(size), buffer(size), AP(AP) {
109 curpos = 0;
110 EmitGeneric = AP.EmitGeneric;
111 }
112
113 // Copy Num bytes from Ptr.
114 // if Bytes > Num, zero fill up to Bytes.
115 unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
116 assert((curpos + Num) <= size);
117 assert((curpos + Bytes) <= size);
118 for (int i = 0; i < Num; ++i) {
119 buffer[curpos] = Ptr[i];
120 curpos++;
121 }
122 for (int i = Num; i < Bytes; ++i) {
123 buffer[curpos] = 0;
124 curpos++;
125 }
126 return curpos;
127 }
128
129 unsigned addZeros(int Num) {
130 assert((curpos + Num) <= size);
131 for (int i = 0; i < Num; ++i) {
132 buffer[curpos] = 0;
133 curpos++;
134 }
135 return curpos;
136 }
137
138 void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {
139 symbolPosInBuffer.push_back(curpos);
140 Symbols.push_back(GVar);
141 SymbolsBeforeStripping.push_back(GVarBeforeStripping);
142 }
143
144 void printBytes(raw_ostream &os);
145 void printWords(raw_ostream &os);
146
147 private:
148 void printSymbol(unsigned nSym, raw_ostream &os);
149 };
150
151 friend class AggBuffer;
152
153private:
154 StringRef getPassName() const override { return "NVPTX Assembly Printer"; }
155
156 const Function *F;
157 std::string CurrentFnName;
158
159 void emitStartOfAsmFile(Module &M) override;
160 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
161 void emitFunctionEntryLabel() override;
162 void emitFunctionBodyStart() override;
163 void emitFunctionBodyEnd() override;
164 void emitImplicitDef(const MachineInstr *MI) const override;
165
166 void emitInstruction(const MachineInstr *) override;
167 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
168 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
169 MCOperand GetSymbolRef(const MCSymbol *Symbol);
170 unsigned encodeVirtualRegister(unsigned Reg);
171
172 void printMemOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O,
173 const char *Modifier = nullptr);
174 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
175 bool processDemoted, const NVPTXSubtarget &STI);
176 void emitGlobals(const Module &M);
177 void emitGlobalAlias(const Module &M, const GlobalAlias &GA);
178 void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI);
179 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
180 void emitVirtualRegister(unsigned int vr, raw_ostream &);
181 void emitFunctionParamList(const Function *, raw_ostream &O);
182 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
183 void printReturnValStr(const Function *, raw_ostream &O);
184 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
185 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
186 const char *ExtraCode, raw_ostream &) override;
187 void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O);
188 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
189 const char *ExtraCode, raw_ostream &) override;
190
191 const MCExpr *lowerConstantForGV(const Constant *CV, bool ProcessingGeneric);
192 void printMCExpr(const MCExpr &Expr, raw_ostream &OS);
193
194protected:
195 bool doInitialization(Module &M) override;
196 bool doFinalization(Module &M) override;
197
198private:
199 bool GlobalsEmitted;
200
201 // This is specific per MachineFunction.
203 // The contents are specific for each
204 // MachineFunction. But the size of the
205 // array is not.
206 typedef DenseMap<unsigned, unsigned> VRegMap;
208 VRegRCMap VRegMapping;
209
210 // List of variables demoted to a function scope.
211 std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;
212
213 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O,
214 const NVPTXSubtarget &STI);
215 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
216 std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;
217 void printScalarConstant(const Constant *CPV, raw_ostream &O);
218 void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
219 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
220 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
221
222 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
223 void emitDeclarations(const Module &, raw_ostream &O);
224 void emitDeclaration(const Function *, raw_ostream &O);
225 void emitDemotedVars(const Function *, raw_ostream &);
226
227 bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo,
228 MCOperand &MCOp);
229 void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp);
230
231 bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;
232
233 // Used to control the need to emit .generic() in the initializer of
234 // module scope variables.
235 // Although ptx supports the hybrid mode like the following,
236 // .global .u32 a;
237 // .global .u32 b;
238 // .global .u32 addr[] = {a, generic(b)}
239 // we have difficulty representing the difference in the NVVM IR.
240 //
241 // Since the address value should always be generic in CUDA C and always
242 // be specific in OpenCL, we use this simple control here.
243 //
244 bool EmitGeneric;
245
246public:
247 NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
248 : AsmPrinter(TM, std::move(Streamer)),
249 EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==
250 NVPTX::CUDA) {}
251
252 bool runOnMachineFunction(MachineFunction &F) override;
253
254 void getAnalysisUsage(AnalysisUsage &AU) const override {
256 AsmPrinter::getAnalysisUsage(AU);
257 }
258
259 std::string getVirtualRegisterName(unsigned) const;
260
261 const MCSymbol *getFunctionFrameSymbol() const override;
262
263 // Make emitGlobalVariable() no-op for NVPTX.
264 // Global variables have been already emitted by the time the base AsmPrinter
265 // attempts to do so in doFinalization() (see NVPTXAsmPrinter::emitGlobals()).
266 void emitGlobalVariable(const GlobalVariable *GV) override {}
267};
268
269} // end namespace llvm
270
271#endif // LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
#define LLVM_LIBRARY_VISIBILITY
Definition: Compiler.h:131
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
Definition: ELFObjcopy.cpp:547
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol, HexagonAsmPrinter &Printer, bool MustExtend)
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
unsigned Reg
const char LLVMTargetMachineRef TM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO, const MachineFunction *MF, const Module *M, const MachineFrameInfo *MFI, const TargetInstrInfo *TII, LLVMContext &Ctx)
This file defines the SmallVector class.
static void printMCExpr(const MCExpr *E, raw_ostream &OS)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:261
This is an important base class in LLVM.
Definition: Constant.h:41
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Representation of each machine instruction.
Definition: MachineInstr.h:68
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void emitGlobalVariable(const GlobalVariable *GV) override
Emit the specified global variable to the .s file.
NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
void getAnalysisUsage(AnalysisUsage &AU) const override
Record analysis usage.
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1726
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1684
AddressSpace
Definition: NVPTXBaseInfo.h:21
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1853
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858