2014-04-09 15:39:15 +00:00
|
|
|
//===-- NVPTXReplaceImageHandles.cpp - Replace image handles for Fermi ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// On Fermi, image handles are not supported. To work around this, we traverse
|
|
|
|
// the machine code and replace image handles with concrete symbols. For this
|
|
|
|
// to work reliably, inlining of all function call must be performed.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NVPTX.h"
|
|
|
|
#include "NVPTXMachineFunctionInfo.h"
|
2014-07-17 11:59:04 +00:00
|
|
|
#include "NVPTXSubtarget.h"
|
2014-04-09 15:39:15 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class NVPTXReplaceImageHandles : public MachineFunctionPass {
|
|
|
|
private:
|
|
|
|
static char ID;
|
|
|
|
DenseSet<MachineInstr *> InstrsToRemove;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NVPTXReplaceImageHandles();
|
|
|
|
|
2014-09-03 11:41:21 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2014-07-17 11:59:04 +00:00
|
|
|
|
2014-08-30 16:48:34 +00:00
|
|
|
const char *getPassName() const override {
|
2014-07-17 11:59:04 +00:00
|
|
|
return "NVPTX Replace Image Handles";
|
|
|
|
}
|
2014-04-09 15:39:15 +00:00
|
|
|
private:
|
|
|
|
bool processInstr(MachineInstr &MI);
|
|
|
|
void replaceImageHandle(MachineOperand &Op, MachineFunction &MF);
|
2014-07-17 11:59:04 +00:00
|
|
|
bool findIndexForHandle(MachineOperand &Op, MachineFunction &MF,
|
|
|
|
unsigned &Idx);
|
2014-04-09 15:39:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char NVPTXReplaceImageHandles::ID = 0;
|
|
|
|
|
|
|
|
NVPTXReplaceImageHandles::NVPTXReplaceImageHandles()
|
|
|
|
: MachineFunctionPass(ID) {}
|
|
|
|
|
|
|
|
bool NVPTXReplaceImageHandles::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
bool Changed = false;
|
|
|
|
InstrsToRemove.clear();
|
|
|
|
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
|
|
|
|
++BI) {
|
|
|
|
for (MachineBasicBlock::iterator I = (*BI).begin(), E = (*BI).end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
|
|
|
Changed |= processInstr(MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now clean up any handle-access instructions
|
|
|
|
// This is needed in debug mode when code cleanup passes are not executed,
|
|
|
|
// but we need the handle access to be eliminated because they are not
|
|
|
|
// valid instructions when image handles are disabled.
|
|
|
|
for (DenseSet<MachineInstr *>::iterator I = InstrsToRemove.begin(),
|
|
|
|
E = InstrsToRemove.end(); I != E; ++I) {
|
|
|
|
(*I)->eraseFromParent();
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NVPTXReplaceImageHandles::processInstr(MachineInstr &MI) {
|
|
|
|
MachineFunction &MF = *MI.getParent()->getParent();
|
2014-07-17 11:59:04 +00:00
|
|
|
const MCInstrDesc &MCID = MI.getDesc();
|
|
|
|
|
|
|
|
if (MCID.TSFlags & NVPTXII::IsTexFlag) {
|
2014-04-09 15:39:15 +00:00
|
|
|
// This is a texture fetch, so operand 4 is a texref and operand 5 is
|
|
|
|
// a samplerref
|
|
|
|
MachineOperand &TexHandle = MI.getOperand(4);
|
|
|
|
replaceImageHandle(TexHandle, MF);
|
|
|
|
|
2014-07-17 11:59:04 +00:00
|
|
|
if (!(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
|
|
|
|
MachineOperand &SampHandle = MI.getOperand(5);
|
|
|
|
replaceImageHandle(SampHandle, MF);
|
|
|
|
}
|
2014-04-09 15:39:15 +00:00
|
|
|
|
|
|
|
return true;
|
2014-07-17 11:59:04 +00:00
|
|
|
} else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
|
|
|
|
unsigned VecSize =
|
|
|
|
1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
|
2014-04-09 15:39:15 +00:00
|
|
|
|
2014-07-17 11:59:04 +00:00
|
|
|
// For a surface load of vector size N, the Nth operand will be the surfref
|
|
|
|
MachineOperand &SurfHandle = MI.getOperand(VecSize);
|
2014-04-09 15:39:15 +00:00
|
|
|
|
|
|
|
replaceImageHandle(SurfHandle, MF);
|
|
|
|
|
|
|
|
return true;
|
2014-07-17 11:59:04 +00:00
|
|
|
} else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
|
2014-04-09 15:39:15 +00:00
|
|
|
// This is a surface store, so operand 0 is a surfref
|
|
|
|
MachineOperand &SurfHandle = MI.getOperand(0);
|
|
|
|
|
|
|
|
replaceImageHandle(SurfHandle, MF);
|
|
|
|
|
|
|
|
return true;
|
2014-07-17 11:59:04 +00:00
|
|
|
} else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
|
2014-04-09 15:39:15 +00:00
|
|
|
// This is a query, so operand 1 is a surfref/texref
|
|
|
|
MachineOperand &Handle = MI.getOperand(1);
|
|
|
|
|
|
|
|
replaceImageHandle(Handle, MF);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-17 11:59:04 +00:00
|
|
|
|
|
|
|
return false;
|
2014-04-09 15:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVPTXReplaceImageHandles::
|
|
|
|
replaceImageHandle(MachineOperand &Op, MachineFunction &MF) {
|
2014-07-17 11:59:04 +00:00
|
|
|
unsigned Idx;
|
|
|
|
if (findIndexForHandle(Op, MF, Idx)) {
|
|
|
|
Op.ChangeToImmediate(Idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NVPTXReplaceImageHandles::
|
|
|
|
findIndexForHandle(MachineOperand &Op, MachineFunction &MF, unsigned &Idx) {
|
2014-04-09 15:39:15 +00:00
|
|
|
const MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
NVPTXMachineFunctionInfo *MFI = MF.getInfo<NVPTXMachineFunctionInfo>();
|
2014-07-17 11:59:04 +00:00
|
|
|
|
|
|
|
assert(Op.isReg() && "Handle is not in a reg?");
|
|
|
|
|
2014-04-09 15:39:15 +00:00
|
|
|
// Which instruction defines the handle?
|
2014-07-17 11:59:04 +00:00
|
|
|
MachineInstr &TexHandleDef = *MRI.getVRegDef(Op.getReg());
|
2014-04-09 15:39:15 +00:00
|
|
|
|
|
|
|
switch (TexHandleDef.getOpcode()) {
|
|
|
|
case NVPTX::LD_i64_avar: {
|
|
|
|
// The handle is a parameter value being loaded, replace with the
|
|
|
|
// parameter symbol
|
2014-07-17 11:59:04 +00:00
|
|
|
const NVPTXSubtarget &ST = MF.getTarget().getSubtarget<NVPTXSubtarget>();
|
|
|
|
if (ST.getDrvInterface() == NVPTX::CUDA) {
|
|
|
|
// For CUDA, we preserve the param loads coming from function arguments
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-09 15:39:15 +00:00
|
|
|
assert(TexHandleDef.getOperand(6).isSymbol() && "Load is not a symbol!");
|
|
|
|
StringRef Sym = TexHandleDef.getOperand(6).getSymbolName();
|
|
|
|
std::string ParamBaseName = MF.getName();
|
|
|
|
ParamBaseName += "_param_";
|
|
|
|
assert(Sym.startswith(ParamBaseName) && "Invalid symbol reference");
|
|
|
|
unsigned Param = atoi(Sym.data()+ParamBaseName.size());
|
|
|
|
std::string NewSym;
|
|
|
|
raw_string_ostream NewSymStr(NewSym);
|
|
|
|
NewSymStr << MF.getFunction()->getName() << "_param_" << Param;
|
2014-07-17 11:59:04 +00:00
|
|
|
|
2014-04-09 15:39:15 +00:00
|
|
|
InstrsToRemove.insert(&TexHandleDef);
|
2014-07-17 11:59:04 +00:00
|
|
|
Idx = MFI->getImageHandleSymbolIndex(NewSymStr.str().c_str());
|
|
|
|
return true;
|
2014-04-09 15:39:15 +00:00
|
|
|
}
|
|
|
|
case NVPTX::texsurf_handles: {
|
|
|
|
// The handle is a global variable, replace with the global variable name
|
|
|
|
assert(TexHandleDef.getOperand(1).isGlobal() && "Load is not a global!");
|
|
|
|
const GlobalValue *GV = TexHandleDef.getOperand(1).getGlobal();
|
|
|
|
assert(GV->hasName() && "Global sampler must be named!");
|
|
|
|
InstrsToRemove.insert(&TexHandleDef);
|
2014-07-17 11:59:04 +00:00
|
|
|
Idx = MFI->getImageHandleSymbolIndex(GV->getName().data());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case NVPTX::nvvm_move_i64:
|
|
|
|
case TargetOpcode::COPY: {
|
|
|
|
bool Res = findIndexForHandle(TexHandleDef.getOperand(1), MF, Idx);
|
|
|
|
if (Res) {
|
|
|
|
InstrsToRemove.insert(&TexHandleDef);
|
|
|
|
}
|
|
|
|
return Res;
|
2014-04-09 15:39:15 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown instruction operating on handle");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineFunctionPass *llvm::createNVPTXReplaceImageHandlesPass() {
|
|
|
|
return new NVPTXReplaceImageHandles();
|
|
|
|
}
|