Skip to content

Commit 81b7f11

Browse files
[llvm][TypeSize] Fix addition/subtraction in TypeSize. (#72979)
It seems TypeSize is currently broken in the sense that: TypeSize::Fixed(4) + TypeSize::Scalable(4) => TypeSize::Fixed(8) without failing its assert that explicitly tests for this case: assert(LHS.Scalable == RHS.Scalable && ...); The reason this fails is that `Scalable` is a static method of class TypeSize, and LHS and RHS are both objects of class TypeSize. So this is evaluating if the pointer to the function Scalable == the pointer to the function Scalable, which is always true because LHS and RHS have the same class. This patch fixes the issue by renaming `TypeSize::Scalable` -> `TypeSize::getScalable`, as well as `TypeSize::Fixed` to `TypeSize::getFixed`, so that it no longer clashes with the variable in FixedOrScalableQuantity. The new methods now also better match the coding standard, which specifies that: * Variable names should be nouns (as they represent state) * Function names should be verb phrases (as they represent actions)
1 parent 03f05a4 commit 81b7f11

File tree

61 files changed

+295
-269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+295
-269
lines changed

clang/lib/CodeGen/CGGPUBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ packArgsIntoNVPTXFormatBuffer(CodeGenFunction *CGF, const CallArgList &Args) {
101101
// If there are no args, pass a null pointer and size 0
102102
llvm::Value *BufferPtr =
103103
llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Ctx));
104-
return {BufferPtr, llvm::TypeSize::Fixed(0)};
104+
return {BufferPtr, llvm::TypeSize::getFixed(0)};
105105
} else {
106106
llvm::SmallVector<llvm::Type *, 8> ArgTypes;
107107
for (unsigned I = 1, NumArgs = Args.size(); I < NumArgs; ++I)

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class TargetTransformInfoImplBase {
443443
}
444444

445445
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
446-
return TypeSize::Fixed(32);
446+
return TypeSize::getFixed(32);
447447
}
448448

449449
unsigned getMinVectorRegisterBitWidth() const { return 128; }

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
714714
/// @{
715715

716716
TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
717-
return TypeSize::Fixed(32);
717+
return TypeSize::getFixed(32);
718718
}
719719

720720
std::optional<unsigned> getMaxVScale() const { return std::nullopt; }

llvm/include/llvm/CodeGen/LowLevelType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class LLT {
182182
/// Returns the total size of the type. Must only be called on sized types.
183183
constexpr TypeSize getSizeInBits() const {
184184
if (isPointer() || isScalar())
185-
return TypeSize::Fixed(getScalarSizeInBits());
185+
return TypeSize::getFixed(getScalarSizeInBits());
186186
auto EC = getElementCount();
187187
return TypeSize(getScalarSizeInBits() * EC.getKnownMinValue(),
188188
EC.isScalable());

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class TargetRegisterInfo : public MCRegisterInfo {
284284

285285
/// Return the size in bits of a register from class RC.
286286
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const {
287-
return TypeSize::Fixed(getRegClassInfo(RC).RegSize);
287+
return TypeSize::getFixed(getRegClassInfo(RC).RegSize);
288288
}
289289

290290
/// Return the size in bytes of the stack slot allocated to hold a spilled

llvm/include/llvm/IR/DataLayout.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,10 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
673673
assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
674674
switch (Ty->getTypeID()) {
675675
case Type::LabelTyID:
676-
return TypeSize::Fixed(getPointerSizeInBits(0));
676+
return TypeSize::getFixed(getPointerSizeInBits(0));
677677
case Type::PointerTyID:
678-
return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace()));
678+
return TypeSize::getFixed(
679+
getPointerSizeInBits(Ty->getPointerAddressSpace()));
679680
case Type::ArrayTyID: {
680681
ArrayType *ATy = cast<ArrayType>(Ty);
681682
return ATy->getNumElements() *
@@ -685,24 +686,24 @@ inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
685686
// Get the layout annotation... which is lazily created on demand.
686687
return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
687688
case Type::IntegerTyID:
688-
return TypeSize::Fixed(Ty->getIntegerBitWidth());
689+
return TypeSize::getFixed(Ty->getIntegerBitWidth());
689690
case Type::HalfTyID:
690691
case Type::BFloatTyID:
691-
return TypeSize::Fixed(16);
692+
return TypeSize::getFixed(16);
692693
case Type::FloatTyID:
693-
return TypeSize::Fixed(32);
694+
return TypeSize::getFixed(32);
694695
case Type::DoubleTyID:
695696
case Type::X86_MMXTyID:
696-
return TypeSize::Fixed(64);
697+
return TypeSize::getFixed(64);
697698
case Type::PPC_FP128TyID:
698699
case Type::FP128TyID:
699-
return TypeSize::Fixed(128);
700+
return TypeSize::getFixed(128);
700701
case Type::X86_AMXTyID:
701-
return TypeSize::Fixed(8192);
702+
return TypeSize::getFixed(8192);
702703
// In memory objects this is always aligned to a higher boundary, but
703704
// only 80 bits contain information.
704705
case Type::X86_FP80TyID:
705-
return TypeSize::Fixed(80);
706+
return TypeSize::getFixed(80);
706707
case Type::FixedVectorTyID:
707708
case Type::ScalableVectorTyID: {
708709
VectorType *VTy = cast<VectorType>(Ty);

llvm/include/llvm/Support/TypeSize.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,10 @@ class TypeSize : public details::FixedOrScalableQuantity<TypeSize, uint64_t> {
321321
static constexpr TypeSize get(ScalarTy Quantity, bool Scalable) {
322322
return TypeSize(Quantity, Scalable);
323323
}
324-
static constexpr TypeSize Fixed(ScalarTy ExactSize) {
324+
static constexpr TypeSize getFixed(ScalarTy ExactSize) {
325325
return TypeSize(ExactSize, false);
326326
}
327-
static constexpr TypeSize Scalable(ScalarTy MinimumSize) {
327+
static constexpr TypeSize getScalable(ScalarTy MinimumSize) {
328328
return TypeSize(MinimumSize, true);
329329
}
330330

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class InterestingMemoryOperand {
2727
Use *PtrUse;
2828
bool IsWrite;
2929
Type *OpType;
30-
TypeSize TypeStoreSize = TypeSize::Fixed(0);
30+
TypeSize TypeStoreSize = TypeSize::getFixed(0);
3131
MaybeAlign Alignment;
3232
// The mask Value, if we're looking at a masked load/store.
3333
Value *MaybeMask;

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static std::optional<TypeSize> getObjectSize(const Value *V,
111111
Opts.RoundToAlign = RoundToAlign;
112112
Opts.NullIsUnknownSize = NullIsValidLoc;
113113
if (getObjectSize(V, Size, DL, &TLI, Opts))
114-
return TypeSize::Fixed(Size);
114+
return TypeSize::getFixed(Size);
115115
return std::nullopt;
116116
}
117117

@@ -177,7 +177,7 @@ static TypeSize getMinimalExtentFrom(const Value &V,
177177
// accessed, thus valid.
178178
if (LocSize.isPrecise())
179179
DerefBytes = std::max(DerefBytes, LocSize.getValue().getKnownMinValue());
180-
return TypeSize::Fixed(DerefBytes);
180+
return TypeSize::getFixed(DerefBytes);
181181
}
182182

183183
/// Returns true if we can prove that the object specified by V has size Size.

llvm/lib/Analysis/Local.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
6565
if (Op->getType() != IntIdxTy)
6666
Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
6767
TypeSize TSize = DL.getTypeAllocSize(GTI.getIndexedType());
68-
if (TSize != TypeSize::Fixed(1)) {
68+
if (TSize != TypeSize::getFixed(1)) {
6969
Value *Scale = Builder->CreateTypeSize(IntIdxTy->getScalarType(), TSize);
7070
if (IntIdxTy->isVectorTy())
7171
Scale = Builder->CreateVectorSplat(

0 commit comments

Comments
 (0)