Skip to content

Commit 0aff106

Browse files
committed
first attempt
1 parent 9f7f4ac commit 0aff106

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

llvm/lib/Target/DirectX/DXILPrepare.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,42 @@ class DXILPrepareModule : public ModulePass {
148148
Type *Ty) {
149149
// Omit bitcasts if the incoming value matches the instruction type.
150150
auto It = PointerTypes.find(Operand);
151-
if (It != PointerTypes.end())
152-
if (cast<TypedPointerType>(It->second)->getElementType() == Ty)
151+
if (It != PointerTypes.end()) {
152+
auto OpTy = cast<TypedPointerType>(It->second)->getElementType();
153+
if (OpTy == Ty)
153154
return nullptr;
155+
}
156+
157+
// Also omit the bitcast for matching global array types
158+
if (auto *GlobalVar = llvm::dyn_cast<llvm::GlobalVariable>(Operand)) {
159+
llvm::Type *ValTy = GlobalVar->getValueType();
160+
161+
if (auto *ArrTy = llvm::dyn_cast<llvm::ArrayType>(ValTy)) {
162+
llvm::Type *ElTy = ArrTy->getElementType();
163+
if (ElTy == Ty)
164+
return nullptr;
165+
}
166+
}
167+
168+
// finally, drill down GEP instructions until we get the array
169+
// that is being accessed, and compare element types
170+
if (auto *GEPInstr = llvm::dyn_cast<llvm::ConstantExpr>(Operand)) {
171+
while (GEPInstr->getOpcode() == llvm::Instruction::GetElementPtr) {
172+
llvm::Value *OpArg = GEPInstr->getOperand(0);
173+
174+
if (auto *GlobalVar = llvm::dyn_cast<llvm::GlobalVariable>(OpArg)) {
175+
llvm::Constant *initializer = GlobalVar->getInitializer();
176+
177+
llvm::Type *ValTy = GlobalVar->getValueType();
178+
if (auto *ArrTy = llvm::dyn_cast<llvm::ArrayType>(ValTy)) {
179+
llvm::Type *ElTy = ArrTy->getElementType();
180+
if (ElTy == Ty)
181+
return nullptr;
182+
}
183+
}
184+
}
185+
}
186+
154187
// Insert bitcasts where we are removing the instruction.
155188
Builder.SetInsertPoint(&Inst);
156189
// This code only gets hit in opaque-pointer mode, so the type of the
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt -S --dxil-prepare %s | FileCheck %s
2+
3+
; Test that global arrays do not get a bitcast instruction
4+
; after the dxil-prepare pass.
5+
6+
target triple = "dxilv1.2-unknown-shadermodel6.2-compute"
7+
8+
@inputTile.1dim = local_unnamed_addr addrspace(3) global [3 x float] zeroinitializer, align 2
9+
10+
define float @testload() local_unnamed_addr {
11+
; NOTE: this would be "bitcast ptr addrspace(3)..." before the change that introduced this test,
12+
; after the dxil-prepare pass is run
13+
; CHECK: load float, ptr addrspace(3) @inputTile.1dim, align 2
14+
%v = load float, ptr addrspace(3) @inputTile.1dim, align 2
15+
16+
ret float %v
17+
}
18+
19+
define void @teststore() local_unnamed_addr {
20+
; CHECK: store float 2.000000e+00, ptr addrspace(3) @inputTile.1dim, align 2
21+
store float 2.000000e+00, ptr addrspace(3) @inputTile.1dim, align 2
22+
23+
ret void
24+
}
25+
26+
define float @testGEPConst() local_unnamed_addr {
27+
; CHECK: load float, ptr addrspace(3) getelementptr (float, ptr addrspace(3) @inputTile.1dim, i32 1), align 4
28+
%v = load float, ptr addrspace(3) getelementptr (float, ptr addrspace(3) @inputTile.1dim, i32 1), align 4
29+
30+
ret float %v
31+
}
32+
33+
define float @testGEPNonConst(i32 %i) local_unnamed_addr {
34+
; CHECK: getelementptr float, ptr addrspace(3) @inputTile.1dim, i32 %i
35+
%gep = getelementptr float, ptr addrspace(3) @inputTile.1dim, i32 %i
36+
%v = load float, ptr addrspace(3) %gep
37+
38+
ret float %v
39+
}

0 commit comments

Comments
 (0)