Skip to content

Commit 7c95fb0

Browse files
alsepkowCopilot
andcommitted
Address review: extract helpers, simplify code, shorten comments
- Extract isMinPrecisionType() and widenMinPrecisionType() helpers - Remove unused TargetTy variable - Shorten verbose comments - Simplify store widening using helper function Co-authored-by: Copilot <[email protected]>
1 parent b9f249a commit 7c95fb0

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

lib/HLSL/HLOperationLower.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,6 +4322,26 @@ static SmallVector<Value *, 10> GetBufLoadArgs(ResLoadHelper helper,
43224322
return Args;
43234323
}
43244324

4325+
// Returns true if EltTy is a min precision type whose padded alloc size
4326+
// exceeds its primitive size (e.g., i16:32, f16:32 in the data layout).
4327+
static bool isMinPrecisionType(Type *EltTy, const DataLayout &DL) {
4328+
return !EltTy->isIntegerTy(1) &&
4329+
DL.getTypeAllocSizeInBits(EltTy) > EltTy->getPrimitiveSizeInBits();
4330+
}
4331+
4332+
// Widens a min precision element type to its 32-bit equivalent (i32 or f32).
4333+
// Returns the original type if not min precision.
4334+
static Type *widenMinPrecisionType(Type *EltTy, Type *VecOrScalarTy,
4335+
IRBuilder<> &Builder, const DataLayout &DL) {
4336+
if (!isMinPrecisionType(EltTy, DL))
4337+
return VecOrScalarTy;
4338+
Type *WideTy = EltTy->isFloatingPointTy() ? (Type *)Builder.getFloatTy()
4339+
: (Type *)Builder.getInt32Ty();
4340+
if (VecOrScalarTy->isVectorTy())
4341+
return VectorType::get(WideTy, VecOrScalarTy->getVectorNumElements());
4342+
return WideTy;
4343+
}
4344+
43254345
// Emits as many calls as needed to load the full vector
43264346
// Performs any needed extractions and conversions of the results.
43274347
Value *TranslateBufLoad(ResLoadHelper &helper, HLResource::Kind RK,
@@ -4338,12 +4358,8 @@ Value *TranslateBufLoad(ResLoadHelper &helper, HLResource::Kind RK,
43384358
Type *EltTy = Ty->getScalarType();
43394359
const bool is64 = (EltTy->isIntegerTy(64) || EltTy->isDoubleTy());
43404360
const bool isBool = EltTy->isIntegerTy(1);
4341-
// Check for min precision types: their alloc size (from data layout padding
4342-
// like i16:32, f16:32) exceeds their primitive size. RawBufferVectorLoad
4343-
// should use the widened type (i32/f32) to match how pre-SM6.9
4344-
// RawBufferLoad handles min precision (load i32, then trunc to i16).
4345-
const bool isMinPrec = !isBool && DL.getTypeAllocSizeInBits(EltTy) >
4346-
EltTy->getPrimitiveSizeInBits();
4361+
// Min precision alloc size exceeds prim size. Use the widened type.
4362+
const bool isMinPrec = isMinPrecisionType(EltTy, DL);
43474363
Type *OrigEltTy = EltTy;
43484364
// Values will be loaded in memory representations.
43494365
if (isBool || (is64 && isTyped) || isMinPrec) {
@@ -4466,13 +4482,11 @@ Value *TranslateBufLoad(ResLoadHelper &helper, HLResource::Kind RK,
44664482
retValNew, Constant::getNullValue(retValNew->getType()));
44674483

44684484
// Truncate widened min precision loads back to original type.
4469-
// e.g., <3 x i32> from rawBufferVectorLoad.v3i32 -> <3 x i16>
44704485
if (isMinPrec) {
4471-
Type *TargetTy = Ty;
44724486
if (OrigEltTy->isIntegerTy())
4473-
retValNew = Builder.CreateTrunc(retValNew, TargetTy);
4487+
retValNew = Builder.CreateTrunc(retValNew, Ty);
44744488
else
4475-
retValNew = Builder.CreateFPTrunc(retValNew, TargetTy);
4489+
retValNew = Builder.CreateFPTrunc(retValNew, Ty);
44764490
}
44774491

44784492
helper.retVal->replaceAllUsesWith(retValNew);
@@ -4595,24 +4609,18 @@ void TranslateStore(DxilResource::Kind RK, Value *handle, Value *val,
45954609
val = Builder.CreateZExt(val, Ty);
45964610
}
45974611

4598-
// Widen min precision types to i32/f32 for RawBufferVectorStore, matching
4599-
// how pre-SM6.9 RawBufferStore handles min precision (store as i32).
4612+
// Widen min precision types to i32/f32 for RawBufferVectorStore.
46004613
if (opcode == OP::OpCode::RawBufferVectorStore) {
46014614
const DataLayout &DL =
46024615
OP->GetModule()->GetHLModule().GetModule()->getDataLayout();
4603-
if (DL.getTypeAllocSizeInBits(EltTy) > EltTy->getPrimitiveSizeInBits()) {
4604-
Type *WideTy = EltTy->isFloatingPointTy() ? (Type *)Builder.getFloatTy()
4605-
: (Type *)i32Ty;
4606-
Type *WideVecTy =
4607-
Ty->isVectorTy()
4608-
? (Type *)VectorType::get(WideTy, Ty->getVectorNumElements())
4609-
: WideTy;
4616+
Type *WideTy = widenMinPrecisionType(EltTy, Ty, Builder, DL);
4617+
if (WideTy != Ty) {
46104618
if (EltTy->isFloatingPointTy())
4611-
val = Builder.CreateFPExt(val, WideVecTy);
4619+
val = Builder.CreateFPExt(val, WideTy);
46124620
else
4613-
val = Builder.CreateSExt(val, WideVecTy);
4614-
EltTy = WideTy;
4615-
Ty = WideVecTy;
4621+
val = Builder.CreateSExt(val, WideTy);
4622+
EltTy = WideTy->getScalarType();
4623+
Ty = WideTy;
46164624
}
46174625
}
46184626

0 commit comments

Comments
 (0)