269 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains a printer that converts from our internal representation10// of machine-dependent LLVM code to NVPTX assembly language.11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H15#define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H16 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 .s50// 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 MCPTXAsmStreamer56// (subclass of MCStreamer).57 58namespace llvm {59 60class MCOperand;61 62class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {63 64 class AggBuffer {65 // Used to buffer the emitted string for initializing global aggregates.66 //67 // Normally an aggregate (array, vector, or structure) is emitted as a u8[].68 // However, if either element/field of the aggregate is a non-NULL address,69 // and all such addresses are properly aligned, then the aggregate is70 // emitted as u32[] or u64[]. In the case of unaligned addresses, the71 // aggregate is emitted as u8[], and the mask() operator is used for all72 // pointers.73 //74 // We first layout the aggregate in 'buffer' in bytes, except for those75 // symbol addresses. For the i-th symbol address in the aggregate, its76 // corresponding 4-byte or 8-byte elements in 'buffer' are filled with 0s.77 // symbolPosInBuffer[i-1] records its position in 'buffer', and Symbols[i-1]78 // records the Value*.79 //80 // Once we have this AggBuffer setup, we can choose how to print it out.81 public:82 // number of symbol addresses83 unsigned numSymbols() const { return Symbols.size(); }84 85 bool allSymbolsAligned(unsigned ptrSize) const {86 return llvm::all_of(symbolPosInBuffer,87 [=](unsigned pos) { return pos % ptrSize == 0; });88 }89 90 private:91 const unsigned size; // size of the buffer in bytes92 std::vector<unsigned char> buffer; // the buffer93 SmallVector<unsigned, 4> symbolPosInBuffer;94 SmallVector<const Value *, 4> Symbols;95 // SymbolsBeforeStripping[i] is the original form of Symbols[i] before96 // stripping pointer casts, i.e.,97 // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().98 //99 // We need to keep these values because AggBuffer::print decides whether to100 // emit a "generic()" cast for Symbols[i] depending on the address space of101 // SymbolsBeforeStripping[i].102 SmallVector<const Value *, 4> SymbolsBeforeStripping;103 unsigned curpos;104 const NVPTXAsmPrinter &AP;105 const bool EmitGeneric;106 107 public:108 AggBuffer(unsigned size, const NVPTXAsmPrinter &AP)109 : size(size), buffer(size), curpos(0), AP(AP),110 EmitGeneric(AP.EmitGeneric) {}111 112 // Copy Num bytes from Ptr.113 // if Bytes > Num, zero fill up to Bytes.114 void addBytes(const unsigned char *Ptr, unsigned Num, unsigned Bytes) {115 for (unsigned I : llvm::seq(Num))116 addByte(Ptr[I]);117 if (Bytes > Num)118 addZeros(Bytes - Num);119 }120 121 void addByte(uint8_t Byte) {122 assert(curpos < size);123 buffer[curpos] = Byte;124 curpos++;125 }126 127 void addZeros(unsigned Num) {128 for ([[maybe_unused]] unsigned _ : llvm::seq(Num)) {129 addByte(0);130 }131 }132 133 void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {134 symbolPosInBuffer.push_back(curpos);135 Symbols.push_back(GVar);136 SymbolsBeforeStripping.push_back(GVarBeforeStripping);137 }138 139 void printBytes(raw_ostream &os);140 void printWords(raw_ostream &os);141 142 private:143 void printSymbol(unsigned nSym, raw_ostream &os);144 };145 146 friend class AggBuffer;147 148public:149 static char ID;150 151 StringRef getPassName() const override { return "NVPTX Assembly Printer"; }152 153private:154 const Function *F;155 156 void emitStartOfAsmFile(Module &M) override;157 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;158 void emitFunctionEntryLabel() override;159 void emitFunctionBodyStart() override;160 void emitFunctionBodyEnd() override;161 void emitImplicitDef(const MachineInstr *MI) const override;162 163 void emitInstruction(const MachineInstr *) override;164 void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);165 MCOperand lowerOperand(const MachineOperand &MO);166 MCOperand GetSymbolRef(const MCSymbol *Symbol);167 unsigned encodeVirtualRegister(unsigned Reg);168 169 void printMemOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O,170 const char *Modifier = nullptr);171 void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,172 bool processDemoted, const NVPTXSubtarget &STI);173 void emitGlobals(const Module &M);174 void emitGlobalAlias(const Module &M, const GlobalAlias &GA) override;175 void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI);176 void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;177 void emitVirtualRegister(unsigned int vr, raw_ostream &);178 void emitFunctionParamList(const Function *, raw_ostream &O);179 void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);180 void encodeDebugInfoRegisterNumbers(const MachineFunction &MF);181 void printReturnValStr(const Function *, raw_ostream &O);182 void printReturnValStr(const MachineFunction &MF, raw_ostream &O);183 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,184 const char *ExtraCode, raw_ostream &) override;185 void printOperand(const MachineInstr *MI, unsigned OpNum, raw_ostream &O);186 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,187 const char *ExtraCode, raw_ostream &) override;188 189 const MCExpr *lowerConstantForGV(const Constant *CV,190 bool ProcessingGeneric) const;191 void printMCExpr(const MCExpr &Expr, raw_ostream &OS) const;192 193protected:194 bool doInitialization(Module &M) override;195 bool doFinalization(Module &M) override;196 197private:198 bool GlobalsEmitted;199 200 // This is specific per MachineFunction.201 const MachineRegisterInfo *MRI;202 // The contents are specific for each203 // MachineFunction. But the size of the204 // array is not.205 typedef DenseMap<unsigned, unsigned> VRegMap;206 typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;207 VRegRCMap VRegMapping;208 209 // List of variables demoted to a function scope.210 std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;211 212 void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O,213 const NVPTXSubtarget &STI);214 void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;215 std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;216 void printScalarConstant(const Constant *CPV, raw_ostream &O);217 void printFPConstant(const ConstantFP *Fp, raw_ostream &O) const;218 void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);219 void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);220 221 void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);222 void emitDeclarations(const Module &, raw_ostream &O);223 void emitDeclaration(const Function *, raw_ostream &O);224 void emitAliasDeclaration(const GlobalAlias *, raw_ostream &O);225 void emitDeclarationWithName(const Function *, MCSymbol *, raw_ostream &O);226 void emitDemotedVars(const Function *, raw_ostream &);227 228 bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;229 230 // Used to control the need to emit .generic() in the initializer of231 // module scope variables.232 // Although ptx supports the hybrid mode like the following,233 // .global .u32 a;234 // .global .u32 b;235 // .global .u32 addr[] = {a, generic(b)}236 // we have difficulty representing the difference in the NVVM IR.237 //238 // Since the address value should always be generic in CUDA C and always239 // be specific in OpenCL, we use this simple control here.240 //241 const bool EmitGeneric;242 243public:244 NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)245 : AsmPrinter(TM, std::move(Streamer), ID),246 EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==247 NVPTX::CUDA) {}248 249 bool runOnMachineFunction(MachineFunction &F) override;250 251 void getAnalysisUsage(AnalysisUsage &AU) const override {252 AU.addRequired<MachineLoopInfoWrapperPass>();253 AsmPrinter::getAnalysisUsage(AU);254 }255 256 std::string getVirtualRegisterName(unsigned) const;257 258 const MCSymbol *getFunctionFrameSymbol() const override;259 260 // Make emitGlobalVariable() no-op for NVPTX.261 // Global variables have been already emitted by the time the base AsmPrinter262 // attempts to do so in doFinalization() (see NVPTXAsmPrinter::emitGlobals()).263 void emitGlobalVariable(const GlobalVariable *GV) override {}264};265 266} // end namespace llvm267 268#endif // LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H269