2017-01-09 23:16:51 +01:00
|
|
|
//===-- NVPTXAsmPrinter.h - NVPTX LLVM assembly writer ----------*- C++ -*-===//
|
2012-05-04 22:18:50 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-05-04 22:18:50 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to NVPTX assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
|
|
|
|
#define LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
#include "NVPTX.h"
|
|
|
|
#include "NVPTXSubtarget.h"
|
2012-12-04 08:12:27 +01:00
|
|
|
#include "NVPTXTargetMachine.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-05-04 22:18:50 +02:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2016-01-27 20:29:56 +01:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DebugLoc.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2012-05-04 22:18:50 +02:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2016-01-27 20:29:56 +01:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2012-05-04 22:18:50 +02:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include "llvm/PassAnalysisSupport.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 08:12:27 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-01-09 23:16:51 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
// The ptx syntax and format is very different from that usually seem in a .s
|
|
|
|
// file,
|
|
|
|
// therefore we are not able to use the MCAsmStreamer interface here.
|
|
|
|
//
|
|
|
|
// We are handcrafting the output method here.
|
|
|
|
//
|
|
|
|
// A better approach is to clone the MCAsmStreamer to a MCPTXAsmStreamer
|
|
|
|
// (subclass of MCStreamer).
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-01-09 23:16:51 +01:00
|
|
|
|
|
|
|
class MCOperand;
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
|
|
|
|
|
|
|
|
class AggBuffer {
|
|
|
|
// Used to buffer the emitted string for initializing global
|
|
|
|
// aggregates.
|
|
|
|
//
|
|
|
|
// Normally an aggregate (array, vector or structure) is emitted
|
|
|
|
// as a u8[]. However, if one element/field of the aggregate
|
|
|
|
// is a non-NULL address, then the aggregate is emitted as u32[]
|
|
|
|
// or u64[].
|
|
|
|
//
|
|
|
|
// We first layout the aggregate in 'buffer' in bytes, except for
|
|
|
|
// those symbol addresses. For the i-th symbol address in the
|
|
|
|
//aggregate, its corresponding 4-byte or 8-byte elements in 'buffer'
|
|
|
|
// are filled with 0s. symbolPosInBuffer[i-1] records its position
|
|
|
|
// in 'buffer', and Symbols[i-1] records the Value*.
|
|
|
|
//
|
|
|
|
// Once we have this AggBuffer setup, we can choose how to print
|
|
|
|
// it out.
|
|
|
|
public:
|
|
|
|
unsigned numSymbols; // number of symbol addresses
|
|
|
|
|
|
|
|
private:
|
2014-08-25 03:59:29 +02:00
|
|
|
const unsigned size; // size of the buffer in bytes
|
|
|
|
std::vector<unsigned char> buffer; // the buffer
|
|
|
|
SmallVector<unsigned, 4> symbolPosInBuffer;
|
|
|
|
SmallVector<const Value *, 4> Symbols;
|
2015-04-24 04:57:30 +02:00
|
|
|
// SymbolsBeforeStripping[i] is the original form of Symbols[i] before
|
|
|
|
// stripping pointer casts, i.e.,
|
|
|
|
// Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts().
|
|
|
|
//
|
|
|
|
// We need to keep these values because AggBuffer::print decides whether to
|
|
|
|
// emit a "generic()" cast for Symbols[i] depending on the address space of
|
|
|
|
// SymbolsBeforeStripping[i].
|
|
|
|
SmallVector<const Value *, 4> SymbolsBeforeStripping;
|
2012-05-04 22:18:50 +02:00
|
|
|
unsigned curpos;
|
|
|
|
raw_ostream &O;
|
|
|
|
NVPTXAsmPrinter &AP;
|
2014-04-09 17:39:11 +02:00
|
|
|
bool EmitGeneric;
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
public:
|
2015-03-16 19:06:57 +01:00
|
|
|
AggBuffer(unsigned size, raw_ostream &O, NVPTXAsmPrinter &AP)
|
|
|
|
: size(size), buffer(size), O(O), AP(AP) {
|
2012-05-04 22:18:50 +02:00
|
|
|
curpos = 0;
|
|
|
|
numSymbols = 0;
|
2014-04-09 17:39:11 +02:00
|
|
|
EmitGeneric = AP.EmitGeneric;
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
2017-01-09 23:16:51 +01:00
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
|
2013-03-30 15:29:21 +01:00
|
|
|
assert((curpos + Num) <= size);
|
|
|
|
assert((curpos + Bytes) <= size);
|
|
|
|
for (int i = 0; i < Num; ++i) {
|
2012-05-04 22:18:50 +02:00
|
|
|
buffer[curpos] = Ptr[i];
|
2013-03-30 15:29:21 +01:00
|
|
|
curpos++;
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
2013-03-30 15:29:21 +01:00
|
|
|
for (int i = Num; i < Bytes; ++i) {
|
2012-05-04 22:18:50 +02:00
|
|
|
buffer[curpos] = 0;
|
2013-03-30 15:29:21 +01:00
|
|
|
curpos++;
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
|
|
|
return curpos;
|
|
|
|
}
|
2017-01-09 23:16:51 +01:00
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
unsigned addZeros(int Num) {
|
2013-03-30 15:29:21 +01:00
|
|
|
assert((curpos + Num) <= size);
|
|
|
|
for (int i = 0; i < Num; ++i) {
|
2012-05-04 22:18:50 +02:00
|
|
|
buffer[curpos] = 0;
|
2013-03-30 15:29:21 +01:00
|
|
|
curpos++;
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
|
|
|
return curpos;
|
|
|
|
}
|
2017-01-09 23:16:51 +01:00
|
|
|
|
2015-04-24 04:57:30 +02:00
|
|
|
void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) {
|
2012-05-04 22:18:50 +02:00
|
|
|
symbolPosInBuffer.push_back(curpos);
|
|
|
|
Symbols.push_back(GVar);
|
2015-04-24 04:57:30 +02:00
|
|
|
SymbolsBeforeStripping.push_back(GVarBeforeStripping);
|
2012-05-04 22:18:50 +02:00
|
|
|
numSymbols++;
|
|
|
|
}
|
2017-01-09 23:16:51 +01:00
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
void print() {
|
|
|
|
if (numSymbols == 0) {
|
|
|
|
// print out in bytes
|
2013-03-30 15:29:21 +01:00
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2012-05-04 22:18:50 +02:00
|
|
|
if (i)
|
|
|
|
O << ", ";
|
2013-03-30 15:29:21 +01:00
|
|
|
O << (unsigned int) buffer[i];
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
2012-05-24 09:02:50 +02:00
|
|
|
} else {
|
2012-05-04 22:18:50 +02:00
|
|
|
// print out in 4-bytes or 8-bytes
|
|
|
|
unsigned int pos = 0;
|
|
|
|
unsigned int nSym = 0;
|
|
|
|
unsigned int nextSymbolPos = symbolPosInBuffer[nSym];
|
|
|
|
unsigned int nBytes = 4;
|
2015-02-19 01:08:14 +01:00
|
|
|
if (static_cast<const NVPTXTargetMachine &>(AP.TM).is64Bit())
|
2012-05-04 22:18:50 +02:00
|
|
|
nBytes = 8;
|
2013-03-30 15:29:21 +01:00
|
|
|
for (pos = 0; pos < size; pos += nBytes) {
|
2012-05-04 22:18:50 +02:00
|
|
|
if (pos)
|
|
|
|
O << ", ";
|
|
|
|
if (pos == nextSymbolPos) {
|
2013-05-20 14:13:32 +02:00
|
|
|
const Value *v = Symbols[nSym];
|
2015-04-24 04:57:30 +02:00
|
|
|
const Value *v0 = SymbolsBeforeStripping[nSym];
|
2013-05-20 14:13:32 +02:00
|
|
|
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
2013-10-29 18:07:16 +01:00
|
|
|
MCSymbol *Name = AP.getSymbol(GVar);
|
2015-04-24 04:57:30 +02:00
|
|
|
PointerType *PTy = dyn_cast<PointerType>(v0->getType());
|
|
|
|
bool IsNonGenericPointer = false; // Is v0 a non-generic pointer?
|
2014-04-09 17:39:11 +02:00
|
|
|
if (PTy && PTy->getAddressSpace() != 0) {
|
|
|
|
IsNonGenericPointer = true;
|
|
|
|
}
|
|
|
|
if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
|
|
|
|
O << "generic(";
|
2015-06-09 02:31:39 +02:00
|
|
|
Name->print(O, AP.MAI);
|
2014-04-09 17:39:11 +02:00
|
|
|
O << ")";
|
|
|
|
} else {
|
2015-06-09 02:31:39 +02:00
|
|
|
Name->print(O, AP.MAI);
|
2014-04-09 17:39:11 +02:00
|
|
|
}
|
2015-04-28 19:18:30 +02:00
|
|
|
} else if (const ConstantExpr *CExpr = dyn_cast<ConstantExpr>(v0)) {
|
|
|
|
const MCExpr *Expr =
|
|
|
|
AP.lowerConstantForGV(cast<Constant>(CExpr), false);
|
|
|
|
AP.printMCExpr(*Expr, O);
|
2012-05-24 09:02:50 +02:00
|
|
|
} else
|
|
|
|
llvm_unreachable("symbol type unknown");
|
2012-05-04 22:18:50 +02:00
|
|
|
nSym++;
|
|
|
|
if (nSym >= numSymbols)
|
2013-03-30 15:29:21 +01:00
|
|
|
nextSymbolPos = size + 1;
|
2012-05-04 22:18:50 +02:00
|
|
|
else
|
|
|
|
nextSymbolPos = symbolPosInBuffer[nSym];
|
2013-03-30 15:29:21 +01:00
|
|
|
} else if (nBytes == 4)
|
2014-08-25 03:59:29 +02:00
|
|
|
O << *(unsigned int *)(&buffer[pos]);
|
2013-03-30 15:29:21 +01:00
|
|
|
else
|
2014-08-25 03:59:29 +02:00
|
|
|
O << *(unsigned long long *)(&buffer[pos]);
|
2012-05-04 22:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class AggBuffer;
|
|
|
|
|
2013-03-30 15:29:21 +01:00
|
|
|
private:
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override { return "NVPTX Assembly Printer"; }
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
const Function *F;
|
|
|
|
std::string CurrentFnName;
|
|
|
|
|
2015-02-01 03:27:45 +01:00
|
|
|
void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
|
2014-04-29 09:57:44 +02:00
|
|
|
void EmitFunctionEntryLabel() override;
|
|
|
|
void EmitFunctionBodyStart() override;
|
|
|
|
void EmitFunctionBodyEnd() override;
|
|
|
|
void emitImplicitDef(const MachineInstr *MI) const override;
|
2012-05-04 22:18:50 +02:00
|
|
|
|
2014-04-29 09:57:44 +02:00
|
|
|
void EmitInstruction(const MachineInstr *) override;
|
2013-08-06 16:13:27 +02:00
|
|
|
void lowerToMCInst(const MachineInstr *MI, MCInst &OutMI);
|
|
|
|
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp);
|
2014-04-09 17:39:15 +02:00
|
|
|
MCOperand GetSymbolRef(const MCSymbol *Symbol);
|
2013-08-06 16:13:27 +02:00
|
|
|
unsigned encodeVirtualRegister(unsigned Reg);
|
2012-05-04 22:18:50 +02:00
|
|
|
|
2013-03-30 15:29:21 +01:00
|
|
|
void printVecModifiedImmediate(const MachineOperand &MO, const char *Modifier,
|
|
|
|
raw_ostream &O);
|
2012-05-04 22:18:50 +02:00
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
|
2014-04-28 06:05:08 +02:00
|
|
|
const char *Modifier = nullptr);
|
2013-05-20 14:13:32 +02:00
|
|
|
void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
|
|
|
|
bool = false);
|
2012-05-04 22:18:50 +02:00
|
|
|
void printParamName(Function::const_arg_iterator I, int paramIndex,
|
|
|
|
raw_ostream &O);
|
2013-05-20 14:13:32 +02:00
|
|
|
void emitGlobals(const Module &M);
|
2015-02-19 01:08:14 +01:00
|
|
|
void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI);
|
2013-03-30 15:29:21 +01:00
|
|
|
void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
|
2013-10-11 14:39:36 +02:00
|
|
|
void emitVirtualRegister(unsigned int vr, raw_ostream &);
|
2012-05-04 22:18:50 +02:00
|
|
|
void emitFunctionParamList(const Function *, raw_ostream &O);
|
|
|
|
void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O);
|
|
|
|
void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF);
|
|
|
|
void printReturnValStr(const Function *, raw_ostream &O);
|
|
|
|
void printReturnValStr(const MachineFunction &MF, raw_ostream &O);
|
2013-08-24 03:17:23 +02:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
2014-04-29 09:57:44 +02:00
|
|
|
raw_ostream &) override;
|
2013-08-24 03:17:23 +02:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
|
2014-04-28 06:05:08 +02:00
|
|
|
const char *Modifier = nullptr);
|
2013-08-24 03:17:23 +02:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
2014-04-29 09:57:44 +02:00
|
|
|
raw_ostream &) override;
|
2015-04-28 19:18:30 +02:00
|
|
|
|
|
|
|
const MCExpr *lowerConstantForGV(const Constant *CV, bool ProcessingGeneric);
|
|
|
|
void printMCExpr(const MCExpr &Expr, raw_ostream &OS);
|
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
protected:
|
2014-04-29 09:57:44 +02:00
|
|
|
bool doInitialization(Module &M) override;
|
|
|
|
bool doFinalization(Module &M) override;
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
private:
|
2013-05-20 14:13:32 +02:00
|
|
|
bool GlobalsEmitted;
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
// This is specific per MachineFunction.
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
// The contents are specific for each
|
|
|
|
// MachineFunction. But the size of the
|
|
|
|
// array is not.
|
2013-05-31 14:14:49 +02:00
|
|
|
typedef DenseMap<unsigned, unsigned> VRegMap;
|
|
|
|
typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
|
|
|
|
VRegRCMap VRegMapping;
|
2015-02-19 01:08:14 +01:00
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
// List of variables demoted to a function scope.
|
2017-01-09 23:16:51 +01:00
|
|
|
std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;
|
2012-05-04 22:18:50 +02:00
|
|
|
|
|
|
|
void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
|
2013-03-30 15:29:21 +01:00
|
|
|
void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
|
2015-08-02 00:20:21 +02:00
|
|
|
std::string getPTXFundamentalTypeStr(Type *Ty, bool = true) const;
|
2013-05-20 14:13:32 +02:00
|
|
|
void printScalarConstant(const Constant *CPV, raw_ostream &O);
|
2013-03-30 15:29:21 +01:00
|
|
|
void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
|
2013-05-20 14:13:32 +02:00
|
|
|
void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
|
|
|
|
void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);
|
2012-05-04 22:18:50 +02:00
|
|
|
|
2013-03-30 15:29:21 +01:00
|
|
|
void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
|
2013-05-20 14:13:32 +02:00
|
|
|
void emitDeclarations(const Module &, raw_ostream &O);
|
2012-05-04 22:18:50 +02:00
|
|
|
void emitDeclaration(const Function *, raw_ostream &O);
|
|
|
|
void emitDemotedVars(const Function *, raw_ostream &);
|
|
|
|
|
2014-04-09 17:39:15 +02:00
|
|
|
bool lowerImageHandleOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
MCOperand &MCOp);
|
|
|
|
void lowerImageHandleSymbol(unsigned Index, MCOperand &MCOp);
|
|
|
|
|
2015-02-01 03:27:45 +01:00
|
|
|
bool isLoopHeaderOfNoUnroll(const MachineBasicBlock &MBB) const;
|
|
|
|
|
2014-04-09 17:39:11 +02:00
|
|
|
// Used to control the need to emit .generic() in the initializer of
|
|
|
|
// module scope variables.
|
|
|
|
// Although ptx supports the hybrid mode like the following,
|
|
|
|
// .global .u32 a;
|
|
|
|
// .global .u32 b;
|
|
|
|
// .global .u32 addr[] = {a, generic(b)}
|
|
|
|
// we have difficulty representing the difference in the NVVM IR.
|
|
|
|
//
|
|
|
|
// Since the address value should always be generic in CUDA C and always
|
|
|
|
// be specific in OpenCL, we use this simple control here.
|
|
|
|
//
|
|
|
|
bool EmitGeneric;
|
|
|
|
|
2012-05-04 22:18:50 +02:00
|
|
|
public:
|
2015-01-18 21:29:04 +01:00
|
|
|
NVPTXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
|
|
|
|
: AsmPrinter(TM, std::move(Streamer)),
|
2015-02-19 01:08:14 +01:00
|
|
|
EmitGeneric(static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() ==
|
2017-01-09 23:16:51 +01:00
|
|
|
NVPTX::CUDA) {}
|
2012-05-04 22:18:50 +02:00
|
|
|
|
2018-05-18 05:13:08 +02:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2017-01-09 23:16:51 +01:00
|
|
|
|
2015-02-01 03:27:45 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AsmPrinter::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2013-10-11 14:39:36 +02:00
|
|
|
std::string getVirtualRegisterName(unsigned) const;
|
2018-07-26 18:29:52 +02:00
|
|
|
|
|
|
|
const MCSymbol *getFunctionFrameSymbol() const override;
|
2012-05-04 22:18:50 +02:00
|
|
|
};
|
|
|
|
|
2017-01-09 23:16:51 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_TARGET_NVPTX_NVPTXASMPRINTER_H
|