Prevent FoldCmpLoadFromIndexedGlobal from introducing i64 use (#2787)

In future, we need to be more intentional for whether i64 use is desired, and produce i64 pattern if so.
This commit is contained in:
Tex Riddell 2020-03-24 11:45:36 -07:00 коммит произвёл GitHub
Родитель 78b58851fd
Коммит bd88e666ad
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 79 добавлений и 2 удалений

Просмотреть файл

@ -482,8 +482,14 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
// - Default to i32
if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
Ty = Idx->getType();
else
Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
// HLSL Change Begins: Don't introduce use of i64 here.
// TODO: Find a way to do this safely.
//else
// Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
// Use i32 if index type was i16 and too small, for instance
else if (ArrayElementCount <= 32)
Ty = Builder->getInt32Ty();
// HLSL Change Ends
if (Ty) {
Value *V = Builder->CreateIntCast(Idx, Ty, false);
@ -491,6 +497,20 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
}
// HLSL Change Begins: Generate 32-bit pattern for 64-bit case for now.
else if (ArrayElementCount <= 64) {
Ty = Builder->getInt32Ty();
Value *V = Builder->CreateIntCast(Idx, Ty, false);
Value *Cmp = Builder->CreateICmpULT(V, ConstantInt::get(Ty, 32));
Value *Sel = Builder->CreateSelect(Cmp,
ConstantInt::get(Ty, MagicBitvector & 0xFFFFFFFF),
ConstantInt::get(Ty, (MagicBitvector >> 32) & 0xFFFFFFFF));
Value *Shift = Builder->CreateAnd(V, ConstantInt::get(Ty, 0x1F));
V = Builder->CreateShl(ConstantInt::get(Ty, 0x1), Shift);
V = Builder->CreateAnd(Sel, V);
return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
}
// HLSL Change Ends
}
return nullptr;

Просмотреть файл

@ -0,0 +1,57 @@
// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
// Make sure instcombine doesn't introduce i64 from FoldCmpLoadFromIndexedGlobal.
// CHECK: define void @main()
// CHECK-NOT: i64
static const float A[33] = {
-1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
-1.0,
};
[RootSignature("DescriptorTable(SRV(t0),SRV(t1, numDescriptors=9))")]
uint main(uint i : A) : SV_Target
{
float f = A[i];
if (f < 0.0)
{
return 17;
}
else
{
return 23;
}
}