[llvm-branch-commits] [llvm] [SandboxVec][LoadStoreVec] Add Reinterpret helper for same-size type casts (PR #236642)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Oct 28 21:04:16 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers
Author: Anshil Gandhi (gandhi56)
<details>
<summary>Changes</summary>
Replace `getEquivalentConstantWithType()` with a `Reinterpret` utility that reinterprets the bits of a value as another type of the same size. It builds a chain of `ptrtoint` / `bitcast` / `inttoptr` casts, and applies it either to a constant (folding) or to a runtime value (inserting casts). The runtime form will be used to unpack mixed-type vector loads and to pack mixed-type store operands in follow-up PRs.
Also add `VecUtils::isNonIntegralPtr()`. A non-integral pointer has no stable integer representation, so it is no longer packed into an integer lane. Previously only a non-integral destination type was rejected (via an assert), while a non-integral source such as `ptr addrspace(5) null` was silently converted with `ptrtoint`.
Stacked on #<!-- -->236636.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---
Full diff: https://github.com/llvm/llvm-project/pull/236642.diff
4 Files Affected:
- (modified) llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h (+38-7)
- (modified) llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h (+9)
- (modified) llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp (+67-19)
- (modified) llvm/test/Transforms/SandboxVectorizer/Passes/LoadStoreVec/load_store_vec_mixed_types.ll (+5-1)
``````````diff
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
index 7ce3acc837dba..9fde243b25311 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.h
@@ -36,6 +36,42 @@ class Instruction;
class Type;
class Constant;
+/// Reinterprets the bits of a value as another type of the same size, e.g.,
+/// float to i32, ptr to i64, or double to <2 x i32>.
+class Reinterpret {
+ /// A single cast in the chain built by getCastChain().
+ using CastStep = std::pair<Instruction::Opcode, Type *>;
+
+ /// \returns an integer type with the same layout as \p Ty: iN for scalars,
+ /// <N x iM> for vectors.
+ static Type *getIntTypeFor(Type *Ty, const DataLayout &DL);
+
+ /// \returns the casts that reinterpret \p SrcTy as \p DestTy, empty if the
+ /// types match, or nullopt if a non-integral pointer is involved.
+ static std::optional<SmallVector<CastStep, 3>>
+ getCastChain(Type *SrcTy, Type *DestTy, const DataLayout &DL);
+
+public:
+ /// \returns true if \p SrcTy can be reinterpreted as \p DestTy.
+ static bool canReinterpret(Type *SrcTy, Type *DestTy, const DataLayout &DL) {
+ return getCastChain(SrcTy, DestTy, DL).has_value();
+ }
+
+ /// Reinterprets \p V as \p DestTy, which must have the same size in \p DL,
+ /// inserting casts before \p WhereIt. Casts of constants fold, so nothing is
+ /// inserted for those. \returns nullptr if a non-integral pointer is
+ /// involved.
+ static Value *reinterpretValue(Value *V, Type *DestTy,
+ BasicBlock::iterator WhereIt, Context &Ctx,
+ const DataLayout &DL);
+
+ /// Reinterprets \p C as \p DestTy, e.g., float 1.0 to i32 0x3f800000, or a
+ /// null pointer to i64 0. \returns nullptr if a non-integral pointer is
+ /// involved.
+ static Constant *reinterpretConstant(Constant *C, Type *DestTy,
+ const DataLayout &DL);
+};
+
class LLVM_ABI LoadStoreVec final : public RegionPass {
const DataLayout *DL = nullptr;
/// The region saved by saveIR(), used by acceptOrRevert().
@@ -63,17 +99,12 @@ class LLVM_ABI LoadStoreVec final : public RegionPass {
/// or nullptr if \p Loads are not a vectorizable.
LoadInst *createVectorLoad(BndlRef<Instruction *> Loads);
- /// Reinterprets \p C as type \p DestTy without changing its bits, e.g.,
- /// float 1.0 to i32 0x3f800000, or a null pointer to i64 0.
- ///
- /// \p DestTy must not be a pointer whose size is unknown.
- Constant *getEquivalentConstantWithType(Constant *C, Type *DestTy);
-
/// Packs \p Constants into one constant vector. The element type is chosen
/// by VecUtils::getCombinedVectorTypeFor().
///
/// \returns the packed constant vector, or nullptr if some constant cannot
- /// be converted, e.g., a vector constant that is not yet flattened, or a
+ /// be converted, e.g., a vector constant that is not yet flattened, a
+ /// non-integral pointer, or a
/// wide constant without a known bit pattern such as a ConstantExpr or a
/// global's address.
Constant *createConstantVector(BndlRef<Constant *> Constants);
diff --git a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h
index e7971875c6cba..987eabc520ad0 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h
@@ -162,6 +162,15 @@ class VecUtils {
}
return FixedVectorType::get(ElemTy, NumElts);
}
+
+ /// \returns true if \p Ty is a pointer, or a vector of pointers, in a
+ /// non-integral address space, which has no stable integer representation.
+ static bool isNonIntegralPtr(Type *Ty, const DataLayout &DL) {
+ Type *ScalarTy = Ty->getScalarType();
+ return ScalarTy->isPointerTy() &&
+ DL.isNonIntegralAddressSpace(ScalarTy->getPointerAddressSpace());
+ }
+
/// Classifies whether all bundle elements are integers, halves, floats,
/// doubles, or pointers.
class BndlTypeClassifier {
diff --git a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
index c66acab5b1ada..39273860bb4d8 100644
--- a/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
+++ b/llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/LoadStoreVec.cpp
@@ -85,26 +85,73 @@ LoadInst *LoadStoreVec::createVectorLoad(BndlRef<Instruction *> Loads) {
return VecL;
}
-Constant *LoadStoreVec::getEquivalentConstantWithType(Constant *C,
- Type *DestTy) {
- // A non-integral pointer has no fixed ptrtoint/inttoptr width.
- if (DestTy->isPointerTy())
- assert(!DL->isNonIntegralAddressSpace(DestTy->getPointerAddressSpace()) &&
- "DestTy has no fixed size");
- Type *SrcTy = C->getType();
+Type *Reinterpret::getIntTypeFor(Type *Ty, const DataLayout &DL) {
+ Context &Ctx = Ty->getContext();
+ if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
+ auto *ElmIntTy =
+ IntegerType::get(Ctx, Utils::getNumBits(VecTy->getElementType(), DL));
+ return FixedVectorType::get(ElmIntTy, VecTy->getNumElements());
+ }
+ return IntegerType::get(Ctx, Utils::getNumBits(Ty, DL));
+}
+
+std::optional<SmallVector<Reinterpret::CastStep, 3>>
+Reinterpret::getCastChain(Type *SrcTy, Type *DestTy, const DataLayout &DL) {
+ assert(Utils::getNumBits(SrcTy, DL) == Utils::getNumBits(DestTy, DL) &&
+ "Expected types of the same size!");
+ // A non-integral pointer has no fixed ptrtoint/inttoptr representation.
+ if (VecUtils::isNonIntegralPtr(SrcTy, DL) ||
+ VecUtils::isNonIntegralPtr(DestTy, DL))
+ return std::nullopt;
+ SmallVector<CastStep, 3> Chain;
if (SrcTy == DestTy)
- return C;
+ return Chain;
+ Type *Ty = SrcTy;
+ if (Ty->getScalarType()->isPointerTy()) {
+ Ty = getIntTypeFor(Ty, DL);
+ Chain.emplace_back(Instruction::Opcode::PtrToInt, Ty);
+ }
+ bool DestIsPtr = DestTy->getScalarType()->isPointerTy();
+ Type *BitCastTy = DestIsPtr ? getIntTypeFor(DestTy, DL) : DestTy;
+ if (Ty != BitCastTy)
+ Chain.emplace_back(Instruction::Opcode::BitCast, BitCastTy);
+ if (DestIsPtr)
+ Chain.emplace_back(Instruction::Opcode::IntToPtr, DestTy);
+ return Chain;
+}
- Constant *AsInt = C;
- if (!SrcTy->isIntegerTy()) {
- Type *IntTy = IntegerType::get(*Ctx, Utils::getNumBits(SrcTy, *DL));
- AsInt = SrcTy->isPointerTy() ? ConstantExpr::getPtrToInt(C, IntTy)
- : ConstantExpr::getBitCast(C, IntTy);
+Value *Reinterpret::reinterpretValue(Value *V, Type *DestTy,
+ BasicBlock::iterator WhereIt, Context &Ctx,
+ const DataLayout &DL) {
+ auto Chain = getCastChain(V->getType(), DestTy, DL);
+ if (!Chain)
+ return nullptr;
+ for (auto [Opc, Ty] : *Chain)
+ V = CastInst::create(Ty, Opc, V, WhereIt, Ctx, "VCast");
+ return V;
+}
+
+Constant *Reinterpret::reinterpretConstant(Constant *C, Type *DestTy,
+ const DataLayout &DL) {
+ auto Chain = getCastChain(C->getType(), DestTy, DL);
+ if (!Chain)
+ return nullptr;
+ for (auto [Opc, Ty] : *Chain) {
+ switch (Opc) {
+ case Instruction::Opcode::PtrToInt:
+ C = ConstantExpr::getPtrToInt(C, Ty);
+ break;
+ case Instruction::Opcode::BitCast:
+ C = ConstantExpr::getBitCast(C, Ty);
+ break;
+ case Instruction::Opcode::IntToPtr:
+ C = ConstantExpr::getIntToPtr(C, Ty);
+ break;
+ default:
+ llvm_unreachable("Unexpected cast in reinterpret chain!");
+ }
}
- if (DestTy->isIntegerTy())
- return AsInt;
- return DestTy->isPointerTy() ? ConstantExpr::getIntToPtr(AsInt, DestTy)
- : ConstantExpr::getBitCast(AsInt, DestTy);
+ return C;
}
Constant *LoadStoreVec::createConstantVector(BndlRef<Constant *> Constants) {
@@ -177,7 +224,7 @@ Constant *LoadStoreVec::createConstantVector(BndlRef<Constant *> Constants) {
for (Constant *C : ConstantElements) {
unsigned Bits = Utils::getNumBits(C, *DL);
if (Bits == LaneBits) {
- Constant *Lane = getEquivalentConstantWithType(C, LaneTy);
+ Constant *Lane = Reinterpret::reinterpretConstant(C, LaneTy, *DL);
if (Lane == nullptr)
return nullptr;
LaneTyConstants.push_back(Lane);
@@ -203,7 +250,8 @@ Constant *LoadStoreVec::createConstantVector(BndlRef<Constant *> Constants) {
// without changing its bits, so a float lane stays a float.
Constant *SliceCInt = ConstantInt::get(
*Ctx, Val.extractBits(LaneBits, EndianAwareSlice * LaneBits));
- Constant *SliceC = getEquivalentConstantWithType(SliceCInt, LaneTy);
+ Constant *SliceC =
+ Reinterpret::reinterpretConstant(SliceCInt, LaneTy, *DL);
if (SliceC == nullptr)
return nullptr;
LaneTyConstants.push_back(SliceC);
diff --git a/llvm/test/Transforms/SandboxVectorizer/Passes/LoadStoreVec/load_store_vec_mixed_types.ll b/llvm/test/Transforms/SandboxVectorizer/Passes/LoadStoreVec/load_store_vec_mixed_types.ll
index c572485a9d479..06a4adc28c17b 100644
--- a/llvm/test/Transforms/SandboxVectorizer/Passes/LoadStoreVec/load_store_vec_mixed_types.ll
+++ b/llvm/test/Transforms/SandboxVectorizer/Passes/LoadStoreVec/load_store_vec_mixed_types.ll
@@ -753,10 +753,14 @@ define void @constants_i32_i32_ptr-gep(ptr %ptr0) {
ret void
}
+; A non-integral pointer has no stable integer representation, so it cannot be
+; packed into an integer lane.
define void @constants_i64_ptr-nonintegral(ptr addrspace(5) %ptr0) {
; CHECK-LABEL: define void @constants_i64_ptr-nonintegral(
; CHECK-SAME: ptr addrspace(5) [[PTR0:%.*]]) {
-; CHECK-NEXT: store <2 x i64> <i64 5, i64 0>, ptr addrspace(5) [[PTR0]], align 4, !sandboxvec [[META45:![0-9]+]]
+; CHECK-NEXT: [[PTR1:%.*]] = getelementptr i8, ptr addrspace(5) [[PTR0]], i64 8
+; CHECK-NEXT: store i64 5, ptr addrspace(5) [[PTR0]], align 4, !sandboxvec [[META45:![0-9]+]]
+; CHECK-NEXT: store ptr addrspace(5) null, ptr addrspace(5) [[PTR1]], align 8, !sandboxvec [[META45]]
; CHECK-NEXT: ret void
;
%ptr1 = getelementptr i8, ptr addrspace(5) %ptr0, i64 8
``````````
</details>
https://github.com/llvm/llvm-project/pull/236642
More information about the llvm-branch-commits
mailing list