[llvm] 546aef6 - [LLVM] Add `Type::getTruncatedType()` and use it in Intrinsics.cpp (#196239)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 8 05:49:53 PDT 2026
Author: Rahul Joshi
Date: 2026-05-08T05:49:48-07:00
New Revision: 546aef6871cefdab1de1b3ee90eb825892be969e
URL: https://github.com/llvm/llvm-project/commit/546aef6871cefdab1de1b3ee90eb825892be969e
DIFF: https://github.com/llvm/llvm-project/commit/546aef6871cefdab1de1b3ee90eb825892be969e.diff
LOG: [LLVM] Add `Type::getTruncatedType()` and use it in Intrinsics.cpp (#196239)
Added:
Modified:
llvm/include/llvm/IR/DerivedTypes.h
llvm/include/llvm/IR/Type.h
llvm/lib/IR/Intrinsics.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/DerivedTypes.h b/llvm/include/llvm/IR/DerivedTypes.h
index c272f091e31a4..0c686cc5c32d7 100644
--- a/llvm/include/llvm/IR/DerivedTypes.h
+++ b/llvm/include/llvm/IR/DerivedTypes.h
@@ -67,7 +67,15 @@ class IntegerType : public Type {
/// Returns type twice as wide the input type.
IntegerType *getExtendedType() const {
- return Type::getIntNTy(getContext(), 2 * getScalarSizeInBits());
+ return Type::getIntNTy(getContext(), 2 * getBitWidth());
+ }
+
+ /// Returns type half as wide the input type.
+ IntegerType *getTruncatedType() const {
+ unsigned BitWidth = getBitWidth();
+ assert((BitWidth & 1) == 0 &&
+ "Cannot truncate integer type with odd bit-width");
+ return Type::getIntNTy(getContext(), BitWidth / 2);
}
/// Get the number of bits in this IntegerType
@@ -542,9 +550,9 @@ class VectorType : public Type {
// the input type, and the element type is an integer or float type which
// is half as wide as the elements in the input type.
static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
- Type *EltTy;
- if (VTy->getElementType()->isFloatingPointTy()) {
- switch(VTy->getElementType()->getTypeID()) {
+ Type *EltTy = VTy->getElementType();
+ if (EltTy->isFloatingPointTy()) {
+ switch (EltTy->getTypeID()) {
case DoubleTyID:
EltTy = Type::getFloatTy(VTy->getContext());
break;
@@ -555,11 +563,7 @@ class VectorType : public Type {
llvm_unreachable("Cannot create narrower fp vector element type");
}
} else {
- unsigned EltBits =
- VTy->getElementType()->getPrimitiveSizeInBits().getFixedValue();
- assert((EltBits & 1) == 0 &&
- "Cannot truncate vector element with odd bit-width");
- EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
+ EltTy = cast<IntegerType>(EltTy)->getTruncatedType();
}
return VectorType::get(EltTy, VTy->getElementCount());
}
@@ -801,6 +805,16 @@ Type *Type::getExtendedType() const {
return cast<IntegerType>(this)->getExtendedType();
}
+Type *Type::getTruncatedType() const {
+ assert(
+ isIntOrIntVectorTy() &&
+ "Original type expected to be a vector of integers or a scalar integer.");
+ if (auto *VTy = dyn_cast<VectorType>(this))
+ return VectorType::getTruncatedElementVectorType(
+ const_cast<VectorType *>(VTy));
+ return cast<IntegerType>(this)->getTruncatedType();
+}
+
Type *Type::getWithNewType(Type *EltTy) const {
if (auto *VTy = dyn_cast<VectorType>(this))
return VectorType::get(EltTy, VTy->getElementCount());
diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h
index 4217d797cdf28..7d40e5dac9463 100644
--- a/llvm/include/llvm/IR/Type.h
+++ b/llvm/include/llvm/IR/Type.h
@@ -444,6 +444,10 @@ class Type {
/// wide as in the original type. For vectors, preserves element count.
LLVM_ABI inline Type *getExtendedType() const;
+ /// Given scalar/vector integer type, returns a type with elements half as
+ /// wide as in the original type. For vectors, preserves element count.
+ LLVM_ABI inline Type *getTruncatedType() const;
+
/// Get the address space of this pointer or pointer vector type.
LLVM_ABI inline unsigned getPointerAddressSpace() const;
diff --git a/llvm/lib/IR/Intrinsics.cpp b/llvm/lib/IR/Intrinsics.cpp
index 00cc17b3cabf5..ff57d335c9a13 100644
--- a/llvm/lib/IR/Intrinsics.cpp
+++ b/llvm/lib/IR/Intrinsics.cpp
@@ -554,22 +554,10 @@ static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
case IITDescriptor::Overloaded:
case IITDescriptor::VecOfAnyPtrsToElt:
return OverloadTys[D.getOverloadIndex()];
- case IITDescriptor::Extend: {
- Type *Ty = OverloadTys[D.getOverloadIndex()];
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return VectorType::getExtendedElementVectorType(VTy);
-
- return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
- }
- case IITDescriptor::Trunc: {
- Type *Ty = OverloadTys[D.getOverloadIndex()];
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
- return VectorType::getTruncatedElementVectorType(VTy);
-
- IntegerType *ITy = cast<IntegerType>(Ty);
- assert(ITy->getBitWidth() % 2 == 0);
- return IntegerType::get(Context, ITy->getBitWidth() / 2);
- }
+ case IITDescriptor::Extend:
+ return OverloadTys[D.getOverloadIndex()]->getExtendedType();
+ case IITDescriptor::Trunc:
+ return OverloadTys[D.getOverloadIndex()]->getTruncatedType();
case IITDescriptor::Subdivide2:
case IITDescriptor::Subdivide4: {
Type *Ty = OverloadTys[D.getOverloadIndex()];
@@ -960,14 +948,7 @@ matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
if (D.getOverloadIndex() >= OverloadTys.size())
return IsDeferredCheck || DeferCheck(Ty);
- Type *NewTy = OverloadTys[D.getOverloadIndex()];
- if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
- NewTy = VectorType::getExtendedElementVectorType(VTy);
- else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
- NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
- else
- return true;
-
+ Type *NewTy = OverloadTys[D.getOverloadIndex()]->getExtendedType();
return Ty != NewTy;
}
case IITDescriptor::Trunc: {
@@ -975,14 +956,7 @@ matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
if (D.getOverloadIndex() >= OverloadTys.size())
return IsDeferredCheck || DeferCheck(Ty);
- Type *NewTy = OverloadTys[D.getOverloadIndex()];
- if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
- NewTy = VectorType::getTruncatedElementVectorType(VTy);
- else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
- NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
- else
- return true;
-
+ Type *NewTy = OverloadTys[D.getOverloadIndex()]->getTruncatedType();
return Ty != NewTy;
}
case IITDescriptor::OneNthEltsVec: {
More information about the llvm-commits
mailing list