From 294fb60e5b25cf73aea677e2a360188337be26a6 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 12 Dec 2025 08:55:07 +0100 Subject: [PATCH] [SandboxIR] Fix ConstantInt::get() for vector types (#171852) As the comment on the method indicates, this method is supposed to produce a splat for vector types. However, currently it has a ConstantInt return type that is incompatible with that. There is a separate overload on IntegerType -- only that one should return ConstantInt. This also requires adjusting Type::getIntNTy() to return IntegerType (matching the normal Type API), so it uses the right overload. --- llvm/include/llvm/SandboxIR/Constant.h | 2 +- llvm/include/llvm/SandboxIR/Type.h | 10 +++++----- llvm/lib/SandboxIR/Constant.cpp | 4 ++-- llvm/lib/SandboxIR/Type.cpp | 20 ++++++++++---------- llvm/unittests/SandboxIR/SandboxIRTest.cpp | 8 ++++++++ 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/SandboxIR/Constant.h b/llvm/include/llvm/SandboxIR/Constant.h index 1925195b69e6..669cc79aed95 100644 --- a/llvm/include/llvm/SandboxIR/Constant.h +++ b/llvm/include/llvm/SandboxIR/Constant.h @@ -85,7 +85,7 @@ public: /// If Ty is a vector type, return a Constant with a splat of the given /// value. Otherwise return a ConstantInt for the given value. - LLVM_ABI static ConstantInt *get(Type *Ty, uint64_t V, bool IsSigned = false); + LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false); /// Return a ConstantInt with the specified integer value for the specified /// type. If the type is wider than 64 bits, the value will be zero-extended diff --git a/llvm/include/llvm/SandboxIR/Type.h b/llvm/include/llvm/SandboxIR/Type.h index d9c5e6c098da..bd14e6bdcefb 100644 --- a/llvm/include/llvm/SandboxIR/Type.h +++ b/llvm/include/llvm/SandboxIR/Type.h @@ -269,11 +269,11 @@ public: // TODO: ADD MISSING - LLVM_ABI static Type *getInt64Ty(Context &Ctx); - LLVM_ABI static Type *getInt32Ty(Context &Ctx); - LLVM_ABI static Type *getInt16Ty(Context &Ctx); - LLVM_ABI static Type *getInt8Ty(Context &Ctx); - LLVM_ABI static Type *getInt1Ty(Context &Ctx); + LLVM_ABI static IntegerType *getInt64Ty(Context &Ctx); + LLVM_ABI static IntegerType *getInt32Ty(Context &Ctx); + LLVM_ABI static IntegerType *getInt16Ty(Context &Ctx); + LLVM_ABI static IntegerType *getInt8Ty(Context &Ctx); + LLVM_ABI static IntegerType *getInt1Ty(Context &Ctx); LLVM_ABI static Type *getDoubleTy(Context &Ctx); LLVM_ABI static Type *getFloatTy(Context &Ctx); LLVM_ABI static Type *getHalfTy(Context &Ctx); diff --git a/llvm/lib/SandboxIR/Constant.cpp b/llvm/lib/SandboxIR/Constant.cpp index eb14797af081..5c50d79d50c8 100644 --- a/llvm/lib/SandboxIR/Constant.cpp +++ b/llvm/lib/SandboxIR/Constant.cpp @@ -45,9 +45,9 @@ Constant *ConstantInt::getBool(Type *Ty, bool V) { auto *LLVMC = llvm::ConstantInt::getBool(Ty->LLVMTy, V); return Ty->getContext().getOrCreateConstant(LLVMC); } -ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) { +Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) { auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned); - return cast(Ty->getContext().getOrCreateConstant(LLVMC)); + return Ty->getContext().getOrCreateConstant(LLVMC); } ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned) { auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned); diff --git a/llvm/lib/SandboxIR/Type.cpp b/llvm/lib/SandboxIR/Type.cpp index dfb54df0c9ce..4ae678f6673e 100644 --- a/llvm/lib/SandboxIR/Type.cpp +++ b/llvm/lib/SandboxIR/Type.cpp @@ -15,20 +15,20 @@ Type *Type::getScalarType() const { return Ctx.getType(LLVMTy->getScalarType()); } -Type *Type::getInt64Ty(Context &Ctx) { - return Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx)); +IntegerType *Type::getInt64Ty(Context &Ctx) { + return cast(Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx))); } -Type *Type::getInt32Ty(Context &Ctx) { - return Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx)); +IntegerType *Type::getInt32Ty(Context &Ctx) { + return cast(Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx))); } -Type *Type::getInt16Ty(Context &Ctx) { - return Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx)); +IntegerType *Type::getInt16Ty(Context &Ctx) { + return cast(Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx))); } -Type *Type::getInt8Ty(Context &Ctx) { - return Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx)); +IntegerType *Type::getInt8Ty(Context &Ctx) { + return cast(Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx))); } -Type *Type::getInt1Ty(Context &Ctx) { - return Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx)); +IntegerType *Type::getInt1Ty(Context &Ctx) { + return cast(Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx))); } Type *Type::getDoubleTy(Context &Ctx) { return Ctx.getType(llvm::Type::getDoubleTy(Ctx.LLVMCtx)); diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index 168502f89cbf..26fe9bc35ac1 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -160,12 +160,20 @@ define void @foo(i32 %v0) { auto *Int32Ty = sandboxir::Type::getInt32Ty(Ctx); auto *LLVMInt32Ty = llvm::Type::getInt32Ty(C); + auto *Int32VecTy = + sandboxir::VectorType::get(Int32Ty, ElementCount::getFixed(2u)); + auto *LLVMInt32VecTy = llvm::FixedVectorType::get(LLVMInt32Ty, 2); { // Check get(Type, V). auto *FortyThree = sandboxir::ConstantInt::get(Int32Ty, 43); auto *LLVMFortyThree = llvm::ConstantInt::get(LLVMInt32Ty, 43); EXPECT_NE(FortyThree, FortyTwo); EXPECT_EQ(FortyThree, Ctx.getValue(LLVMFortyThree)); + + // Check vector splat. + auto *FortyThreeVec = sandboxir::ConstantInt::get(Int32VecTy, 43); + auto *LLVMFortyThreeVec = llvm::ConstantInt::get(LLVMInt32VecTy, 43); + EXPECT_EQ(FortyThreeVec, Ctx.getValue(LLVMFortyThreeVec)); } { // Check get(Type, V, IsSigned).