Skip to content

Commit 294fb60

Browse files
authored
[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.
1 parent 43a4442 commit 294fb60

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ConstantInt : public Constant {
8585

8686
/// If Ty is a vector type, return a Constant with a splat of the given
8787
/// value. Otherwise return a ConstantInt for the given value.
88-
LLVM_ABI static ConstantInt *get(Type *Ty, uint64_t V, bool IsSigned = false);
88+
LLVM_ABI static Constant *get(Type *Ty, uint64_t V, bool IsSigned = false);
8989

9090
/// Return a ConstantInt with the specified integer value for the specified
9191
/// type. If the type is wider than 64 bits, the value will be zero-extended

llvm/include/llvm/SandboxIR/Type.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ class Type {
269269

270270
// TODO: ADD MISSING
271271

272-
LLVM_ABI static Type *getInt64Ty(Context &Ctx);
273-
LLVM_ABI static Type *getInt32Ty(Context &Ctx);
274-
LLVM_ABI static Type *getInt16Ty(Context &Ctx);
275-
LLVM_ABI static Type *getInt8Ty(Context &Ctx);
276-
LLVM_ABI static Type *getInt1Ty(Context &Ctx);
272+
LLVM_ABI static IntegerType *getInt64Ty(Context &Ctx);
273+
LLVM_ABI static IntegerType *getInt32Ty(Context &Ctx);
274+
LLVM_ABI static IntegerType *getInt16Ty(Context &Ctx);
275+
LLVM_ABI static IntegerType *getInt8Ty(Context &Ctx);
276+
LLVM_ABI static IntegerType *getInt1Ty(Context &Ctx);
277277
LLVM_ABI static Type *getDoubleTy(Context &Ctx);
278278
LLVM_ABI static Type *getFloatTy(Context &Ctx);
279279
LLVM_ABI static Type *getHalfTy(Context &Ctx);

llvm/lib/SandboxIR/Constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ Constant *ConstantInt::getBool(Type *Ty, bool V) {
4545
auto *LLVMC = llvm::ConstantInt::getBool(Ty->LLVMTy, V);
4646
return Ty->getContext().getOrCreateConstant(LLVMC);
4747
}
48-
ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) {
48+
Constant *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) {
4949
auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned);
50-
return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC));
50+
return Ty->getContext().getOrCreateConstant(LLVMC);
5151
}
5252
ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned) {
5353
auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned);

llvm/lib/SandboxIR/Type.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ Type *Type::getScalarType() const {
1515
return Ctx.getType(LLVMTy->getScalarType());
1616
}
1717

18-
Type *Type::getInt64Ty(Context &Ctx) {
19-
return Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx));
18+
IntegerType *Type::getInt64Ty(Context &Ctx) {
19+
return cast<IntegerType>(Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx)));
2020
}
21-
Type *Type::getInt32Ty(Context &Ctx) {
22-
return Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx));
21+
IntegerType *Type::getInt32Ty(Context &Ctx) {
22+
return cast<IntegerType>(Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx)));
2323
}
24-
Type *Type::getInt16Ty(Context &Ctx) {
25-
return Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx));
24+
IntegerType *Type::getInt16Ty(Context &Ctx) {
25+
return cast<IntegerType>(Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx)));
2626
}
27-
Type *Type::getInt8Ty(Context &Ctx) {
28-
return Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx));
27+
IntegerType *Type::getInt8Ty(Context &Ctx) {
28+
return cast<IntegerType>(Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx)));
2929
}
30-
Type *Type::getInt1Ty(Context &Ctx) {
31-
return Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx));
30+
IntegerType *Type::getInt1Ty(Context &Ctx) {
31+
return cast<IntegerType>(Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx)));
3232
}
3333
Type *Type::getDoubleTy(Context &Ctx) {
3434
return Ctx.getType(llvm::Type::getDoubleTy(Ctx.LLVMCtx));

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,20 @@ define void @foo(i32 %v0) {
160160

161161
auto *Int32Ty = sandboxir::Type::getInt32Ty(Ctx);
162162
auto *LLVMInt32Ty = llvm::Type::getInt32Ty(C);
163+
auto *Int32VecTy =
164+
sandboxir::VectorType::get(Int32Ty, ElementCount::getFixed(2u));
165+
auto *LLVMInt32VecTy = llvm::FixedVectorType::get(LLVMInt32Ty, 2);
163166
{
164167
// Check get(Type, V).
165168
auto *FortyThree = sandboxir::ConstantInt::get(Int32Ty, 43);
166169
auto *LLVMFortyThree = llvm::ConstantInt::get(LLVMInt32Ty, 43);
167170
EXPECT_NE(FortyThree, FortyTwo);
168171
EXPECT_EQ(FortyThree, Ctx.getValue(LLVMFortyThree));
172+
173+
// Check vector splat.
174+
auto *FortyThreeVec = sandboxir::ConstantInt::get(Int32VecTy, 43);
175+
auto *LLVMFortyThreeVec = llvm::ConstantInt::get(LLVMInt32VecTy, 43);
176+
EXPECT_EQ(FortyThreeVec, Ctx.getValue(LLVMFortyThreeVec));
169177
}
170178
{
171179
// Check get(Type, V, IsSigned).

0 commit comments

Comments
 (0)