Skip to content

Commit b317f1b

Browse files
authored
Set the resource kind as u32 for boolean resources (#7116)
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. Includes a couple incidental NFC fixes as well: Moves lit filecheck tests into the correct directory. I got CodeGenHLSL and CodeGenDXIL mixed up. 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 the type came from more sources. The code shows as never executed in coverage. Related to #7079
1 parent d5951b7 commit b317f1b

File tree

8 files changed

+498
-96
lines changed

8 files changed

+498
-96
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

+43-80
Original file line numberDiff line numberDiff line change
@@ -8368,96 +8368,59 @@ void TranslateStructBufSubscriptUser(Instruction *user, Value *handle,
83688368
baseOffset, status, OP, DL);
83698369
}
83708370
} else if (isa<LoadInst>(user) || isa<StoreInst>(user)) {
8371-
LoadInst *ldInst = dyn_cast<LoadInst>(user);
8372-
StoreInst *stInst = dyn_cast<StoreInst>(user);
8371+
LoadInst *LdInst = dyn_cast<LoadInst>(user);
8372+
StoreInst *StInst = dyn_cast<StoreInst>(user);
83738373

8374-
Type *Ty = isa<LoadInst>(user) ? ldInst->getType()
8375-
: stInst->getValueOperand()->getType();
8374+
Type *Ty = isa<LoadInst>(user) ? LdInst->getType()
8375+
: StInst->getValueOperand()->getType();
83768376
Type *pOverloadTy = Ty->getScalarType();
8377-
Value *offset = baseOffset;
8378-
unsigned arraySize = 1;
8379-
Value *eltSize = nullptr;
8377+
Value *Offset = baseOffset;
83808378

8381-
if (pOverloadTy->isArrayTy()) {
8382-
arraySize = pOverloadTy->getArrayNumElements();
8383-
eltSize = OP->GetU32Const(
8384-
DL.getTypeAllocSize(pOverloadTy->getArrayElementType()));
8379+
if (LdInst) {
8380+
unsigned NumComponents = 0;
8381+
Value *NewLd = nullptr;
8382+
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
8383+
NumComponents = VTy->getNumElements();
8384+
else
8385+
NumComponents = 1;
83858386

8386-
pOverloadTy = pOverloadTy->getArrayElementType()->getScalarType();
8387-
}
8387+
if (ResKind == HLResource::Kind::TypedBuffer) {
8388+
// Typed buffer cannot have offsets, they must be loaded all at once
8389+
ResRetValueArray ResRet = GenerateTypedBufferLoad(
8390+
handle, pOverloadTy, bufIdx, status, OP, Builder);
83888391

8389-
if (ldInst) {
8390-
auto LdElement = [=](Value *offset, IRBuilder<> &Builder) -> Value * {
8391-
unsigned numComponents = 0;
8392-
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
8393-
numComponents = VTy->getNumElements();
8394-
} else {
8395-
numComponents = 1;
8396-
}
8397-
Constant *alignment =
8392+
NewLd = ExtractFromTypedBufferLoad(ResRet, Ty, Offset, Builder);
8393+
} else {
8394+
Value *ResultElts[4];
8395+
Constant *Alignment =
83988396
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8399-
if (ResKind == HLResource::Kind::TypedBuffer) {
8400-
// Typed buffer cannot have offsets, they must be loaded all at once
8401-
ResRetValueArray ResRet = GenerateTypedBufferLoad(
8402-
handle, pOverloadTy, bufIdx, status, OP, Builder);
8403-
8404-
return ExtractFromTypedBufferLoad(ResRet, Ty, offset, Builder);
8405-
} else {
8406-
Value *ResultElts[4];
8407-
GenerateRawBufLd(handle, bufIdx, offset, status, pOverloadTy,
8408-
ResultElts, OP, Builder, numComponents, alignment);
8409-
return ScalarizeElements(Ty, ResultElts, Builder);
8410-
}
8411-
};
8412-
8413-
Value *newLd = LdElement(offset, Builder);
8414-
if (arraySize > 1) {
8415-
newLd =
8416-
Builder.CreateInsertValue(UndefValue::get(Ty), newLd, (uint64_t)0);
8417-
8418-
for (unsigned i = 1; i < arraySize; i++) {
8419-
offset = Builder.CreateAdd(offset, eltSize);
8420-
Value *eltLd = LdElement(offset, Builder);
8421-
newLd = Builder.CreateInsertValue(newLd, eltLd, i);
8422-
}
8397+
GenerateRawBufLd(handle, bufIdx, Offset, status, pOverloadTy,
8398+
ResultElts, OP, Builder, NumComponents, Alignment);
8399+
NewLd = ScalarizeElements(Ty, ResultElts, Builder);
84238400
}
8424-
ldInst->replaceAllUsesWith(newLd);
8401+
8402+
LdInst->replaceAllUsesWith(NewLd);
84258403
} else {
8426-
Value *val = stInst->getValueOperand();
8427-
auto StElement = [&](Value *offset, Value *val, IRBuilder<> &Builder) {
8428-
Value *undefVal = llvm::UndefValue::get(pOverloadTy);
8429-
Value *vals[] = {undefVal, undefVal, undefVal, undefVal};
8430-
uint8_t mask = 0;
8431-
if (Ty->isVectorTy()) {
8432-
unsigned vectorNumElements = Ty->getVectorNumElements();
8433-
DXASSERT(vectorNumElements <= 4, "up to 4 elements in vector");
8434-
assert(vectorNumElements <= 4);
8435-
for (unsigned i = 0; i < vectorNumElements; i++) {
8436-
vals[i] = Builder.CreateExtractElement(val, i);
8437-
mask |= (1 << i);
8438-
}
8439-
} else {
8440-
vals[0] = val;
8441-
mask = DXIL::kCompMask_X;
8442-
}
8443-
Constant *alignment =
8444-
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8445-
GenerateStructBufSt(handle, bufIdx, offset, pOverloadTy, OP, Builder,
8446-
vals, mask, alignment);
8447-
};
8448-
if (arraySize > 1)
8449-
val = Builder.CreateExtractValue(val, 0);
8450-
8451-
StElement(offset, val, Builder);
8452-
if (arraySize > 1) {
8453-
val = stInst->getValueOperand();
8454-
8455-
for (unsigned i = 1; i < arraySize; i++) {
8456-
offset = Builder.CreateAdd(offset, eltSize);
8457-
Value *eltVal = Builder.CreateExtractValue(val, i);
8458-
StElement(offset, eltVal, Builder);
8404+
Value *val = StInst->getValueOperand();
8405+
Value *undefVal = llvm::UndefValue::get(pOverloadTy);
8406+
Value *vals[] = {undefVal, undefVal, undefVal, undefVal};
8407+
uint8_t mask = 0;
8408+
if (Ty->isVectorTy()) {
8409+
unsigned vectorNumElements = Ty->getVectorNumElements();
8410+
DXASSERT(vectorNumElements <= 4, "up to 4 elements in vector");
8411+
assert(vectorNumElements <= 4);
8412+
for (unsigned i = 0; i < vectorNumElements; i++) {
8413+
vals[i] = Builder.CreateExtractElement(val, i);
8414+
mask |= (1 << i);
84598415
}
8416+
} else {
8417+
vals[0] = val;
8418+
mask = DXIL::kCompMask_X;
84608419
}
8420+
Constant *alignment =
8421+
OP->GetI32Const(DL.getTypeAllocSize(Ty->getScalarType()));
8422+
GenerateStructBufSt(handle, bufIdx, Offset, pOverloadTy, OP, Builder,
8423+
vals, mask, alignment);
84618424
}
84628425
user->eraseFromParent();
84638426
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(user)) {

tools/clang/lib/CodeGen/CGHLSLMS.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -3510,11 +3510,20 @@ bool CGMSHLSLRuntime::SetUAVSRV(SourceLocation loc,
35103510
if (const BuiltinType *BTy = EltTy->getAs<BuiltinType>()) {
35113511
CompType::Kind kind = BuiltinTyToCompTy(BTy, bHasNormAttribute && bSNorm,
35123512
bHasNormAttribute && !bSNorm);
3513-
// 64bits types are implemented with u32.
3514-
if (kind == CompType::Kind::U64 || kind == CompType::Kind::I64 ||
3515-
kind == CompType::Kind::SNormF64 ||
3516-
kind == CompType::Kind::UNormF64 || kind == CompType::Kind::F64) {
3513+
// Boolean, 64-bit, and packed types are implemented with u32.
3514+
switch (kind) {
3515+
case CompType::Kind::I1:
3516+
case CompType::Kind::U64:
3517+
case CompType::Kind::I64:
3518+
case CompType::Kind::F64:
3519+
case CompType::Kind::SNormF64:
3520+
case CompType::Kind::UNormF64:
3521+
case CompType::Kind::PackedS8x32:
3522+
case CompType::Kind::PackedU8x32:
35173523
kind = CompType::Kind::U32;
3524+
break;
3525+
default:
3526+
break;
35183527
}
35193528
hlslRes->SetCompType(kind);
35203529
} else {

0 commit comments

Comments
 (0)