Skip to content

Commit 7d337af

Browse files
committed
Set the resource kind as u32 for boolean resources
It was producing a validation error previously because they are represented as u32 and i1 is not valid for output resources. This preserves the cmp conversions for values read from the resource, but satisfies the validation requirement. Performs some incidental consolidation of types that are represented as u32. Related to microsoft#7079 remove dead array handing code The array type checks were for a type extracted from a vector. Arrays cannot be elements of vectors. This is leftover from when that was not the case. The code shows as never executed in coverage
1 parent c2f85a6 commit 7d337af

File tree

7 files changed

+51
-90
lines changed

7 files changed

+51
-90
lines changed

lib/DXIL/DxilResource.cpp

+1-12
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,7 @@ DxilResource::DxilResource()
3030

3131
CompType DxilResource::GetCompType() const { return m_CompType; }
3232

33-
void DxilResource::SetCompType(const CompType CT) {
34-
// Translate packed types to u32
35-
switch (CT.GetKind()) {
36-
case CompType::Kind::PackedS8x32:
37-
case CompType::Kind::PackedU8x32:
38-
m_CompType = CompType::getU32();
39-
break;
40-
default:
41-
m_CompType = CT;
42-
break;
43-
}
44-
}
33+
void DxilResource::SetCompType(const CompType CT) { m_CompType = CT; }
4534

4635
Type *DxilResource::GetRetType() const {
4736
Type *Ty = GetHLSLType()->getPointerElementType();

lib/HLSL/HLOperationLower.cpp

+35-72
Original file line numberDiff line numberDiff line change
@@ -8436,89 +8436,52 @@ void TranslateStructBufSubscriptUser(Instruction *user, Value *handle,
84368436
: stInst->getValueOperand()->getType();
84378437
Type *pOverloadTy = Ty->getScalarType();
84388438
Value *offset = baseOffset;
8439-
unsigned arraySize = 1;
8440-
Value *eltSize = nullptr;
84418439

8442-
if (pOverloadTy->isArrayTy()) {
8443-
arraySize = pOverloadTy->getArrayNumElements();
8444-
eltSize = OP->GetU32Const(
8445-
DL.getTypeAllocSize(pOverloadTy->getArrayElementType()));
8440+
if (ldInst) {
8441+
unsigned numComponents = 0;
8442+
Value *newLd = nullptr;
8443+
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
8444+
numComponents = VTy->getNumElements();
8445+
else
8446+
numComponents = 1;
84468447

8447-
pOverloadTy = pOverloadTy->getArrayElementType()->getScalarType();
8448-
}
8448+
if (ResKind == HLResource::Kind::TypedBuffer) {
8449+
// Typed buffer cannot have offsets, they must be loaded all at once
8450+
ResRetValueArray ResRet = GenerateTypedBufferLoad(
8451+
handle, pOverloadTy, bufIdx, status, OP, Builder);
84498452

8450-
if (ldInst) {
8451-
auto LdElement = [=](Value *offset, IRBuilder<> &Builder) -> Value * {
8452-
unsigned numComponents = 0;
8453-
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
8454-
numComponents = VTy->getNumElements();
8455-
} else {
8456-
numComponents = 1;
8457-
}
8453+
newLd = ExtractFromTypedBufferLoad(ResRet, Ty, offset, Builder);
8454+
} else {
8455+
Value *ResultElts[4];
84588456
Constant *alignment =
84598457
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8460-
if (ResKind == HLResource::Kind::TypedBuffer) {
8461-
// Typed buffer cannot have offsets, they must be loaded all at once
8462-
ResRetValueArray ResRet = GenerateTypedBufferLoad(
8463-
handle, pOverloadTy, bufIdx, status, OP, Builder);
8464-
8465-
return ExtractFromTypedBufferLoad(ResRet, Ty, offset, Builder);
8466-
} else {
8467-
Value *ResultElts[4];
8468-
GenerateRawBufLd(handle, bufIdx, offset, status, pOverloadTy,
8469-
ResultElts, OP, Builder, numComponents, alignment);
8470-
return ScalarizeElements(Ty, ResultElts, Builder);
8471-
}
8472-
};
8473-
8474-
Value *newLd = LdElement(offset, Builder);
8475-
if (arraySize > 1) {
8476-
newLd =
8477-
Builder.CreateInsertValue(UndefValue::get(Ty), newLd, (uint64_t)0);
8478-
8479-
for (unsigned i = 1; i < arraySize; i++) {
8480-
offset = Builder.CreateAdd(offset, eltSize);
8481-
Value *eltLd = LdElement(offset, Builder);
8482-
newLd = Builder.CreateInsertValue(newLd, eltLd, i);
8483-
}
8458+
GenerateRawBufLd(handle, bufIdx, offset, status, pOverloadTy,
8459+
ResultElts, OP, Builder, numComponents, alignment);
8460+
newLd = ScalarizeElements(Ty, ResultElts, Builder);
84848461
}
8462+
84858463
ldInst->replaceAllUsesWith(newLd);
84868464
} else {
84878465
Value *val = stInst->getValueOperand();
8488-
auto StElement = [&](Value *offset, Value *val, IRBuilder<> &Builder) {
8489-
Value *undefVal = llvm::UndefValue::get(pOverloadTy);
8490-
Value *vals[] = {undefVal, undefVal, undefVal, undefVal};
8491-
uint8_t mask = 0;
8492-
if (Ty->isVectorTy()) {
8493-
unsigned vectorNumElements = Ty->getVectorNumElements();
8494-
DXASSERT(vectorNumElements <= 4, "up to 4 elements in vector");
8495-
assert(vectorNumElements <= 4);
8496-
for (unsigned i = 0; i < vectorNumElements; i++) {
8497-
vals[i] = Builder.CreateExtractElement(val, i);
8498-
mask |= (1 << i);
8499-
}
8500-
} else {
8501-
vals[0] = val;
8502-
mask = DXIL::kCompMask_X;
8503-
}
8504-
Constant *alignment =
8505-
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8506-
GenerateStructBufSt(handle, bufIdx, offset, pOverloadTy, OP, Builder,
8507-
vals, mask, alignment);
8508-
};
8509-
if (arraySize > 1)
8510-
val = Builder.CreateExtractValue(val, 0);
8511-
8512-
StElement(offset, val, Builder);
8513-
if (arraySize > 1) {
8514-
val = stInst->getValueOperand();
8515-
8516-
for (unsigned i = 1; i < arraySize; i++) {
8517-
offset = Builder.CreateAdd(offset, eltSize);
8518-
Value *eltVal = Builder.CreateExtractValue(val, i);
8519-
StElement(offset, eltVal, Builder);
8466+
Value *undefVal = llvm::UndefValue::get(pOverloadTy);
8467+
Value *vals[] = {undefVal, undefVal, undefVal, undefVal};
8468+
uint8_t mask = 0;
8469+
if (Ty->isVectorTy()) {
8470+
unsigned vectorNumElements = Ty->getVectorNumElements();
8471+
DXASSERT(vectorNumElements <= 4, "up to 4 elements in vector");
8472+
assert(vectorNumElements <= 4);
8473+
for (unsigned i = 0; i < vectorNumElements; i++) {
8474+
vals[i] = Builder.CreateExtractElement(val, i);
8475+
mask |= (1 << i);
85208476
}
8477+
} else {
8478+
vals[0] = val;
8479+
mask = DXIL::kCompMask_X;
85218480
}
8481+
Constant *alignment =
8482+
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8483+
GenerateStructBufSt(handle, bufIdx, offset, pOverloadTy, OP, Builder,
8484+
vals, mask, alignment);
85228485
}
85238486
user->eraseFromParent();
85248487
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(user)) {

tools/clang/lib/CodeGen/CGHLSLMS.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -3539,11 +3539,20 @@ bool CGMSHLSLRuntime::SetUAVSRV(SourceLocation loc,
35393539
if (const BuiltinType *BTy = EltTy->getAs<BuiltinType>()) {
35403540
CompType::Kind kind = BuiltinTyToCompTy(BTy, bHasNormAttribute && bSNorm,
35413541
bHasNormAttribute && !bSNorm);
3542-
// 64bits types are implemented with u32.
3543-
if (kind == CompType::Kind::U64 || kind == CompType::Kind::I64 ||
3544-
kind == CompType::Kind::SNormF64 ||
3545-
kind == CompType::Kind::UNormF64 || kind == CompType::Kind::F64) {
3542+
// Boolean, 64-bit, and packed types are implemented with u32.
3543+
switch (kind) {
3544+
case CompType::Kind::I1:
3545+
case CompType::Kind::U64:
3546+
case CompType::Kind::I64:
3547+
case CompType::Kind::F64:
3548+
case CompType::Kind::SNormF64:
3549+
case CompType::Kind::UNormF64:
3550+
case CompType::Kind::PackedS8x32:
3551+
case CompType::Kind::PackedU8x32:
35463552
kind = CompType::Kind::U32;
3553+
break;
3554+
default:
3555+
break;
35473556
}
35483557
hlslRes->SetCompType(kind);
35493558
} else {

tools/clang/test/CodeGenDXIL/hlsl/intrinsics/buffer-load-stores.hlsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: %dxc -DTYPE=float4 -DIX=SIx -T vs_6_6 %s | FileCheck %s
2-
// RUiN: %dxc -DTYPE=bool4 -DIX=SIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,I1
2+
// RUN: %dxc -DTYPE=bool4 -DIX=SIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,I1
33
// RUN: %dxc -DTYPE=uint64_t2 -DIX=SIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,I64
44
// RUN: %dxc -DTYPE=double2 -DIX=SIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,F64
55

66
// RUN: %dxc -DTYPE=float4 -DIX=VIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,VIX
7-
// RUiN: %dxc -DTYPE=bool4 -DIX=VIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,VIX,I1
7+
// RUN: %dxc -DTYPE=bool4 -DIX=VIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,VIX,I1
88
// RUN: %dxc -DTYPE=uint64_t2 -DIX=VIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,VIX,I64
99
// RUN: %dxc -DTYPE=double2 -DIX=VIx -T vs_6_6 %s | FileCheck %s --check-prefixes=CHECK,VIX,F64
1010

0 commit comments

Comments
 (0)