Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataLayout::getTypeSizeInBits may get error result when input type is !cir.long_double<!cir.f80> #1057

Closed
PikachuHyA opened this issue Nov 5, 2024 · 2 comments · Fixed by #1058

Comments

@PikachuHyA
Copy link
Collaborator

I am refactoring the code in #1033 by replacing the manually implemented switch-case statements with DataLayout::getTypeSizeInBits.

During this process, I noticed that DataLayout::getTypeSizeInBits(!cir.long_double<!cir.f80>) returns 16 bits when I expected it to return 80 bits. This discrepancy is causing the test cases to fail.

For reference, you can find the relevant code in ClangIR

llvm::TypeSize
FP80Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(16);
}

and the LLVM IR code

// The implementation of this method is provided inline as it is particularly
// well suited to constant folding when called on a specific Type subclass.
inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
switch (Ty->getTypeID()) {
case Type::LabelTyID:
return TypeSize::getFixed(getPointerSizeInBits(0));
case Type::PointerTyID:
return TypeSize::getFixed(
getPointerSizeInBits(Ty->getPointerAddressSpace()));
case Type::ArrayTyID: {
ArrayType *ATy = cast<ArrayType>(Ty);
return ATy->getNumElements() *
getTypeAllocSizeInBits(ATy->getElementType());
}
case Type::StructTyID:
// Get the layout annotation... which is lazily created on demand.
return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
case Type::IntegerTyID:
return TypeSize::getFixed(Ty->getIntegerBitWidth());
case Type::HalfTyID:
case Type::BFloatTyID:
return TypeSize::getFixed(16);
case Type::FloatTyID:
return TypeSize::getFixed(32);
case Type::DoubleTyID:
return TypeSize::getFixed(64);
case Type::PPC_FP128TyID:
case Type::FP128TyID:
return TypeSize::getFixed(128);
case Type::X86_AMXTyID:
return TypeSize::getFixed(8192);
// In memory objects this is always aligned to a higher boundary, but
// only 80 bits contain information.
case Type::X86_FP80TyID:
return TypeSize::getFixed(80);
case Type::FixedVectorTyID:
case Type::ScalableVectorTyID: {
VectorType *VTy = cast<VectorType>(Ty);
auto EltCnt = VTy->getElementCount();
uint64_t MinBits = EltCnt.getKnownMinValue() *
getTypeSizeInBits(VTy->getElementType()).getFixedValue();
return TypeSize(MinBits, EltCnt.isScalable());
}
case Type::TargetExtTyID: {
Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
return getTypeSizeInBits(LayoutTy);
}
default:
llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
}
}

For additional context, please see #536 .

@PikachuHyA
Copy link
Collaborator Author

cc @Lancern

@PikachuHyA
Copy link
Collaborator Author

FP128

llvm::TypeSize
FP128Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
return llvm::TypeSize::getFixed(16);
}

see #966

bcardosolopes added a commit that referenced this issue Nov 6, 2024
fix #1057

---------

Co-authored-by: Bruno Cardoso Lopes <bcardosolopes@users.noreply.github.com>
Co-authored-by: Sirui Mu <msrlancern@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant