1 //===-- R600TextureIntrinsicsReplacer.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This pass translates tgsi-like texture intrinsics into R600 texture
12 /// closer to hardware intrinsics.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InstVisitor.h"
26 class R600TextureIntrinsicsReplacer :
27 public FunctionPass, public InstVisitor<R600TextureIntrinsicsReplacer> {
35 FunctionType *TexSign;
36 FunctionType *TexQSign;
38 void getAdjustmentFromTextureTarget(unsigned TextureType, bool hasLOD,
39 unsigned SrcSelect[4], unsigned CT[4],
40 bool &useShadowVariant) {
52 TEXTURE_SHADOW1D_ARRAY,
53 TEXTURE_SHADOW2D_ARRAY,
56 TEXTURE_2D_ARRAY_MSAA,
58 TEXTURE_SHADOWCUBE_ARRAY
61 switch (TextureType) {
63 useShadowVariant = false;
70 case TEXTURE_1D_ARRAY:
71 case TEXTURE_2D_ARRAY:
72 case TEXTURE_CUBE_ARRAY:
74 case TEXTURE_2D_ARRAY_MSAA:
75 useShadowVariant = false;
77 case TEXTURE_SHADOW1D:
78 case TEXTURE_SHADOW2D:
79 case TEXTURE_SHADOWRECT:
80 case TEXTURE_SHADOW1D_ARRAY:
81 case TEXTURE_SHADOW2D_ARRAY:
82 case TEXTURE_SHADOWCUBE:
83 case TEXTURE_SHADOWCUBE_ARRAY:
84 useShadowVariant = true;
87 llvm_unreachable("Unknow Texture Type");
90 if (TextureType == TEXTURE_RECT ||
91 TextureType == TEXTURE_SHADOWRECT) {
96 if (TextureType == TEXTURE_CUBE_ARRAY ||
97 TextureType == TEXTURE_SHADOWCUBE_ARRAY)
100 if (TextureType == TEXTURE_1D_ARRAY ||
101 TextureType == TEXTURE_SHADOW1D_ARRAY) {
102 if (hasLOD && useShadowVariant) {
108 } else if (TextureType == TEXTURE_2D_ARRAY ||
109 TextureType == TEXTURE_SHADOW2D_ARRAY) {
113 if ((TextureType == TEXTURE_SHADOW1D ||
114 TextureType == TEXTURE_SHADOW2D ||
115 TextureType == TEXTURE_SHADOWRECT ||
116 TextureType == TEXTURE_SHADOW1D_ARRAY) &&
117 !(hasLOD && useShadowVariant))
121 void ReplaceCallInst(CallInst &I, FunctionType *FT, const char *Name,
122 unsigned SrcSelect[4], Value *Offset[3], Value *Resource,
123 Value *Sampler, unsigned CT[4], Value *Coord) {
124 IRBuilder<> Builder(&I);
126 ConstantInt::get(Int32Type, SrcSelect[0]),
127 ConstantInt::get(Int32Type, SrcSelect[1]),
128 ConstantInt::get(Int32Type, SrcSelect[2]),
129 ConstantInt::get(Int32Type, SrcSelect[3])
131 Value *SwizzleMask = ConstantVector::get(Mask);
132 Value *SwizzledCoord =
133 Builder.CreateShuffleVector(Coord, Coord, SwizzleMask);
142 ConstantInt::get(Int32Type, CT[0]),
143 ConstantInt::get(Int32Type, CT[1]),
144 ConstantInt::get(Int32Type, CT[2]),
145 ConstantInt::get(Int32Type, CT[3])
148 Function *F = Mod->getFunction(Name);
150 F = Function::Create(FT, GlobalValue::ExternalLinkage, Name, Mod);
151 F->addFnAttr(Attribute::ReadNone);
153 I.replaceAllUsesWith(Builder.CreateCall(F, Args));
157 void ReplaceTexIntrinsic(CallInst &I, bool hasLOD, FunctionType *FT,
158 const char *VanillaInt,
159 const char *ShadowInt) {
160 Value *Coord = I.getArgOperand(0);
161 Value *ResourceId = I.getArgOperand(1);
162 Value *SamplerId = I.getArgOperand(2);
164 unsigned TextureType =
165 cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
167 unsigned SrcSelect[4] = { 0, 1, 2, 3 };
168 unsigned CT[4] = {1, 1, 1, 1};
170 ConstantInt::get(Int32Type, 0),
171 ConstantInt::get(Int32Type, 0),
172 ConstantInt::get(Int32Type, 0)
174 bool useShadowVariant;
176 getAdjustmentFromTextureTarget(TextureType, hasLOD, SrcSelect, CT,
179 ReplaceCallInst(I, FT, useShadowVariant?ShadowInt:VanillaInt, SrcSelect,
180 Offset, ResourceId, SamplerId, CT, Coord);
183 void ReplaceTXF(CallInst &I) {
184 Value *Coord = I.getArgOperand(0);
185 Value *ResourceId = I.getArgOperand(4);
186 Value *SamplerId = I.getArgOperand(5);
188 unsigned TextureType =
189 cast<ConstantInt>(I.getArgOperand(6))->getZExtValue();
191 unsigned SrcSelect[4] = { 0, 1, 2, 3 };
192 unsigned CT[4] = {1, 1, 1, 1};
198 bool useShadowVariant;
200 getAdjustmentFromTextureTarget(TextureType, false, SrcSelect, CT,
203 ReplaceCallInst(I, TexQSign, "llvm.R600.txf", SrcSelect,
204 Offset, ResourceId, SamplerId, CT, Coord);
208 R600TextureIntrinsicsReplacer():
212 bool doInitialization(Module &M) override {
213 LLVMContext &Ctx = M.getContext();
215 FloatType = Type::getFloatTy(Ctx);
216 Int32Type = Type::getInt32Ty(Ctx);
217 V4f32Type = VectorType::get(FloatType, 4);
218 V4i32Type = VectorType::get(Int32Type, 4);
231 TexSign = FunctionType::get(V4f32Type, ArgsType, /*isVarArg=*/false);
232 Type *ArgsQType[] = {
244 TexQSign = FunctionType::get(V4f32Type, ArgsQType, /*isVarArg=*/false);
248 bool runOnFunction(Function &F) override {
253 const char *getPassName() const override {
254 return "R600 Texture Intrinsics Replacer";
257 void getAnalysisUsage(AnalysisUsage &AU) const override {
260 void visitCallInst(CallInst &I) {
261 if (!I.getCalledFunction())
264 StringRef Name = I.getCalledFunction()->getName();
265 if (Name == "llvm.AMDGPU.tex") {
266 ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.tex", "llvm.R600.texc");
269 if (Name == "llvm.AMDGPU.txl") {
270 ReplaceTexIntrinsic(I, true, TexSign, "llvm.R600.txl", "llvm.R600.txlc");
273 if (Name == "llvm.AMDGPU.txb") {
274 ReplaceTexIntrinsic(I, true, TexSign, "llvm.R600.txb", "llvm.R600.txbc");
277 if (Name == "llvm.AMDGPU.txf") {
281 if (Name == "llvm.AMDGPU.txq") {
282 ReplaceTexIntrinsic(I, false, TexQSign, "llvm.R600.txq", "llvm.R600.txq");
285 if (Name == "llvm.AMDGPU.ddx") {
286 ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.ddx", "llvm.R600.ddx");
289 if (Name == "llvm.AMDGPU.ddy") {
290 ReplaceTexIntrinsic(I, false, TexSign, "llvm.R600.ddy", "llvm.R600.ddy");
297 char R600TextureIntrinsicsReplacer::ID = 0;
301 FunctionPass *llvm::createR600TextureIntrinsicsReplacer() {
302 return new R600TextureIntrinsicsReplacer();