2013-05-17 16:50:20 +00:00
|
|
|
//===-- R600TextureIntrinsicsReplacer.cpp ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// This pass translates tgsi-like texture intrinsics into R600 texture
|
|
|
|
/// closer to hardware intrinsics.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-05-23 17:10:37 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2013-05-17 16:50:20 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2013-05-23 17:10:37 +00:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-06 03:23:41 +00:00
|
|
|
#include "llvm/IR/InstVisitor.h"
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class R600TextureIntrinsicsReplacer :
|
|
|
|
public FunctionPass, public InstVisitor<R600TextureIntrinsicsReplacer> {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
Module *Mod;
|
|
|
|
Type *FloatType;
|
|
|
|
Type *Int32Type;
|
|
|
|
Type *V4f32Type;
|
|
|
|
Type *V4i32Type;
|
|
|
|
FunctionType *TexSign;
|
|
|
|
FunctionType *TexQSign;
|
|
|
|
|
2013-08-15 23:11:03 +00:00
|
|
|
void getAdjustmentFromTextureTarget(unsigned TextureType, bool hasLOD,
|
|
|
|
unsigned SrcSelect[4], unsigned CT[4],
|
|
|
|
bool &useShadowVariant) {
|
2013-05-17 16:50:20 +00:00
|
|
|
enum TextureTypes {
|
|
|
|
TEXTURE_1D = 1,
|
|
|
|
TEXTURE_2D,
|
|
|
|
TEXTURE_3D,
|
|
|
|
TEXTURE_CUBE,
|
|
|
|
TEXTURE_RECT,
|
|
|
|
TEXTURE_SHADOW1D,
|
|
|
|
TEXTURE_SHADOW2D,
|
|
|
|
TEXTURE_SHADOWRECT,
|
|
|
|
TEXTURE_1D_ARRAY,
|
|
|
|
TEXTURE_2D_ARRAY,
|
|
|
|
TEXTURE_SHADOW1D_ARRAY,
|
|
|
|
TEXTURE_SHADOW2D_ARRAY,
|
|
|
|
TEXTURE_SHADOWCUBE,
|
|
|
|
TEXTURE_2D_MSAA,
|
|
|
|
TEXTURE_2D_ARRAY_MSAA,
|
|
|
|
TEXTURE_CUBE_ARRAY,
|
|
|
|
TEXTURE_SHADOWCUBE_ARRAY
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (TextureType) {
|
|
|
|
case 0:
|
2013-08-17 00:06:51 +00:00
|
|
|
useShadowVariant = false;
|
2013-05-17 16:50:20 +00:00
|
|
|
return;
|
|
|
|
case TEXTURE_RECT:
|
|
|
|
case TEXTURE_1D:
|
|
|
|
case TEXTURE_2D:
|
|
|
|
case TEXTURE_3D:
|
|
|
|
case TEXTURE_CUBE:
|
|
|
|
case TEXTURE_1D_ARRAY:
|
|
|
|
case TEXTURE_2D_ARRAY:
|
|
|
|
case TEXTURE_CUBE_ARRAY:
|
|
|
|
case TEXTURE_2D_MSAA:
|
|
|
|
case TEXTURE_2D_ARRAY_MSAA:
|
|
|
|
useShadowVariant = false;
|
|
|
|
break;
|
|
|
|
case TEXTURE_SHADOW1D:
|
|
|
|
case TEXTURE_SHADOW2D:
|
|
|
|
case TEXTURE_SHADOWRECT:
|
|
|
|
case TEXTURE_SHADOW1D_ARRAY:
|
|
|
|
case TEXTURE_SHADOW2D_ARRAY:
|
|
|
|
case TEXTURE_SHADOWCUBE:
|
|
|
|
case TEXTURE_SHADOWCUBE_ARRAY:
|
|
|
|
useShadowVariant = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknow Texture Type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TextureType == TEXTURE_RECT ||
|
|
|
|
TextureType == TEXTURE_SHADOWRECT) {
|
|
|
|
CT[0] = 0;
|
|
|
|
CT[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TextureType == TEXTURE_CUBE_ARRAY ||
|
2013-09-05 23:55:13 +00:00
|
|
|
TextureType == TEXTURE_SHADOWCUBE_ARRAY)
|
2013-05-17 16:50:20 +00:00
|
|
|
CT[2] = 0;
|
|
|
|
|
|
|
|
if (TextureType == TEXTURE_1D_ARRAY ||
|
|
|
|
TextureType == TEXTURE_SHADOW1D_ARRAY) {
|
|
|
|
if (hasLOD && useShadowVariant) {
|
|
|
|
CT[1] = 0;
|
|
|
|
} else {
|
|
|
|
CT[2] = 0;
|
|
|
|
SrcSelect[2] = 1;
|
|
|
|
}
|
|
|
|
} else if (TextureType == TEXTURE_2D_ARRAY ||
|
|
|
|
TextureType == TEXTURE_SHADOW2D_ARRAY) {
|
|
|
|
CT[2] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((TextureType == TEXTURE_SHADOW1D ||
|
|
|
|
TextureType == TEXTURE_SHADOW2D ||
|
|
|
|
TextureType == TEXTURE_SHADOWRECT ||
|
|
|
|
TextureType == TEXTURE_SHADOW1D_ARRAY) &&
|
2013-09-05 23:55:13 +00:00
|
|
|
!(hasLOD && useShadowVariant))
|
2013-05-17 16:50:20 +00:00
|
|
|
SrcSelect[3] = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceCallInst(CallInst &I, FunctionType *FT, const char *Name,
|
|
|
|
unsigned SrcSelect[4], Value *Offset[3], Value *Resource,
|
|
|
|
Value *Sampler, unsigned CT[4], Value *Coord) {
|
|
|
|
IRBuilder<> Builder(&I);
|
|
|
|
Constant *Mask[] = {
|
|
|
|
ConstantInt::get(Int32Type, SrcSelect[0]),
|
|
|
|
ConstantInt::get(Int32Type, SrcSelect[1]),
|
|
|
|
ConstantInt::get(Int32Type, SrcSelect[2]),
|
|
|
|
ConstantInt::get(Int32Type, SrcSelect[3])
|
|
|
|
};
|
|
|
|
Value *SwizzleMask = ConstantVector::get(Mask);
|
|
|
|
Value *SwizzledCoord =
|
|
|
|
Builder.CreateShuffleVector(Coord, Coord, SwizzleMask);
|
|
|
|
|
|
|
|
Value *Args[] = {
|
|
|
|
SwizzledCoord,
|
|
|
|
Offset[0],
|
|
|
|
Offset[1],
|
|
|
|
Offset[2],
|
|
|
|
Resource,
|
|
|
|
Sampler,
|
|
|
|
ConstantInt::get(Int32Type, CT[0]),
|
|
|
|
ConstantInt::get(Int32Type, CT[1]),
|
|
|
|
ConstantInt::get(Int32Type, CT[2]),
|
|
|
|
ConstantInt::get(Int32Type, CT[3])
|
|
|
|
};
|
|
|
|
|
|
|
|
Function *F = Mod->getFunction(Name);
|
|
|
|
if (!F) {
|
|
|
|
F = Function::Create(FT, GlobalValue::ExternalLinkage, Name, Mod);
|
|
|
|
F->addFnAttr(Attribute::ReadNone);
|
|
|
|
}
|
|
|
|
I.replaceAllUsesWith(Builder.CreateCall(F, Args));
|
|
|
|
I.eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceTexIntrinsic(CallInst &I, bool hasLOD, FunctionType *FT,
|
|
|
|
const char *VanillaInt,
|
|
|
|
const char *ShadowInt) {
|
|
|
|
Value *Coord = I.getArgOperand(0);
|
|
|
|
Value *ResourceId = I.getArgOperand(1);
|
|
|
|
Value *SamplerId = I.getArgOperand(2);
|
|
|
|
|
|
|
|
unsigned TextureType =
|
2015-04-10 11:24:51 +00:00
|
|
|
cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
unsigned SrcSelect[4] = { 0, 1, 2, 3 };
|
|
|
|
unsigned CT[4] = {1, 1, 1, 1};
|
|
|
|
Value *Offset[3] = {
|
|
|
|
ConstantInt::get(Int32Type, 0),
|
|
|
|
ConstantInt::get(Int32Type, 0),
|
|
|
|
ConstantInt::get(Int32Type, 0)
|
|
|
|
};
|
|
|
|
bool useShadowVariant;
|
|
|
|
|
2013-08-15 23:11:03 +00:00
|
|
|
getAdjustmentFromTextureTarget(TextureType, hasLOD, SrcSelect, CT,
|
|
|
|
useShadowVariant);
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
ReplaceCallInst(I, FT, useShadowVariant?ShadowInt:VanillaInt, SrcSelect,
|
|
|
|
Offset, ResourceId, SamplerId, CT, Coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceTXF(CallInst &I) {
|
|
|
|
Value *Coord = I.getArgOperand(0);
|
|
|
|
Value *ResourceId = I.getArgOperand(4);
|
|
|
|
Value *SamplerId = I.getArgOperand(5);
|
|
|
|
|
|
|
|
unsigned TextureType =
|
2015-04-10 11:24:51 +00:00
|
|
|
cast<ConstantInt>(I.getArgOperand(6))->getZExtValue();
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
unsigned SrcSelect[4] = { 0, 1, 2, 3 };
|
|
|
|
unsigned CT[4] = {1, 1, 1, 1};
|
|
|
|
Value *Offset[3] = {
|
|
|
|
I.getArgOperand(1),
|
|
|
|
I.getArgOperand(2),
|
|
|
|
I.getArgOperand(3),
|
|
|
|
};
|
|
|
|
bool useShadowVariant;
|
|
|
|
|
2013-08-15 23:11:03 +00:00
|
|
|
getAdjustmentFromTextureTarget(TextureType, false, SrcSelect, CT,
|
|
|
|
useShadowVariant);
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
ReplaceCallInst(I, TexQSign, "llvm.R600.txf", SrcSelect,
|
|
|
|
Offset, ResourceId, SamplerId, CT, Coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
R600TextureIntrinsicsReplacer():
|
|
|
|
FunctionPass(ID) {
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:57:24 +00:00
|
|
|
bool doInitialization(Module &M) override {
|
2013-05-17 16:50:20 +00:00
|
|
|
LLVMContext &Ctx = M.getContext();
|
|
|
|
Mod = &M;
|
|
|
|
FloatType = Type::getFloatTy(Ctx);
|
|
|
|
Int32Type = Type::getInt32Ty(Ctx);
|
|
|
|
V4f32Type = VectorType::get(FloatType, 4);
|
|
|
|
V4i32Type = VectorType::get(Int32Type, 4);
|
|
|
|
Type *ArgsType[] = {
|
|
|
|
V4f32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
};
|
2013-05-20 15:58:43 +00:00
|
|
|
TexSign = FunctionType::get(V4f32Type, ArgsType, /*isVarArg=*/false);
|
2013-05-17 16:50:20 +00:00
|
|
|
Type *ArgsQType[] = {
|
|
|
|
V4i32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
Int32Type,
|
|
|
|
};
|
2013-05-20 15:58:43 +00:00
|
|
|
TexQSign = FunctionType::get(V4f32Type, ArgsQType, /*isVarArg=*/false);
|
2013-05-17 16:50:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:57:24 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2013-05-17 16:50:20 +00:00
|
|
|
visit(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:57:24 +00:00
|
|
|
const char *getPassName() const override {
|
2013-05-17 16:50:20 +00:00
|
|
|
return "R600 Texture Intrinsics Replacer";
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:57:24 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-05-17 16:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void visitCallInst(CallInst &I) {
|
2013-09-05 23:55:13 +00:00
|
|
|
if (!I.getCalledFunction())
|
2013-09-05 18:37:45 +00:00
|
|
|
return;
|
2013-09-05 23:55:13 +00:00
|
|
|
|
2013-05-22 15:31:11 +00:00
|
|
|
StringRef Name = I.getCalledFunction()->getName();
|
|
|
|
if (Name == "llvm.AMDGPU.tex") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.tex", "llvm.R600.texc");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.txl") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, true, TexSign, "llvm.R600.txl", "llvm.R600.txlc");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.txb") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, true, TexSign, "llvm.R600.txb", "llvm.R600.txbc");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.txf") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTXF(I);
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.txq") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, false, TexQSign, "llvm.R600.txq", "llvm.R600.txq");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.ddx") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.ddx", "llvm.R600.ddx");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Name == "llvm.AMDGPU.ddy") {
|
2013-05-17 16:50:20 +00:00
|
|
|
ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.ddy", "llvm.R600.ddy");
|
2013-05-22 15:31:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-17 16:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
char R600TextureIntrinsicsReplacer::ID = 0;
|
|
|
|
|
2015-06-19 15:57:42 +00:00
|
|
|
} // namespace
|
2013-05-17 16:50:20 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createR600TextureIntrinsicsReplacer() {
|
|
|
|
return new R600TextureIntrinsicsReplacer();
|
|
|
|
}
|