Skip to content

Commit bb047b9

Browse files
authored
[SandboxIR] Implement missing ConstantVector member functions (#131390)
This patch implements get(), getSplat(), getSplatValue() and getType(), mirroring LLVM IR.
1 parent 4324f23 commit bb047b9

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,19 @@ class ConstantVector final : public ConstantAggregate {
424424
friend class Context; // For constructor.
425425

426426
public:
427-
// TODO: Missing functions: getSplat(), getType(), getSplatValue(), get().
427+
static Constant *get(ArrayRef<Constant *> V);
428+
/// Return a ConstantVector with the specified constant in each element.
429+
/// Note that this might not return an instance of ConstantVector
430+
static Constant *getSplat(ElementCount EC, Constant *Elt);
431+
/// Specialize the getType() method to always return a FixedVectorType,
432+
/// which reduces the amount of casting needed in parts of the compiler.
433+
inline FixedVectorType *getType() const {
434+
return cast<FixedVectorType>(Value::getType());
435+
}
436+
/// If all elements of the vector constant have the same value, return that
437+
/// value. Otherwise, return nullptr. Ignore poison elements by setting
438+
/// AllowPoison to true.
439+
Constant *getSplatValue(bool AllowPoison = false) const;
428440

429441
/// For isa/dyn_cast.
430442
static bool classof(const Value *From) {

llvm/include/llvm/SandboxIR/Value.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class Value {
145145
friend class CmpInst; // For getting `Val`.
146146
friend class ConstantArray; // For `Val`.
147147
friend class ConstantStruct; // For `Val`.
148+
friend class ConstantVector; // For `Val`.
148149
friend class ConstantAggregateZero; // For `Val`.
149150
friend class ConstantPointerNull; // For `Val`.
150151
friend class UndefValue; // For `Val`.

llvm/lib/SandboxIR/Constant.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ StructType *ConstantStruct::getTypeForElements(Context &Ctx,
174174
return StructType::get(Ctx, EltTypes, Packed);
175175
}
176176

177+
Constant *ConstantVector::get(ArrayRef<Constant *> V) {
178+
assert(!V.empty() && "Expected non-empty V!");
179+
auto &Ctx = V[0]->getContext();
180+
SmallVector<llvm::Constant *, 8> LLVMV;
181+
LLVMV.reserve(V.size());
182+
for (auto *Elm : V)
183+
LLVMV.push_back(cast<llvm::Constant>(Elm->Val));
184+
return Ctx.getOrCreateConstant(llvm::ConstantVector::get(LLVMV));
185+
}
186+
187+
Constant *ConstantVector::getSplat(ElementCount EC, Constant *Elt) {
188+
auto *LLVMElt = cast<llvm::Constant>(Elt->Val);
189+
auto &Ctx = Elt->getContext();
190+
return Ctx.getOrCreateConstant(llvm::ConstantVector::getSplat(EC, LLVMElt));
191+
}
192+
193+
Constant *ConstantVector::getSplatValue(bool AllowPoison) const {
194+
auto *LLVMSplatValue = cast_or_null<llvm::Constant>(
195+
cast<llvm::ConstantVector>(Val)->getSplatValue(AllowPoison));
196+
return LLVMSplatValue ? Ctx.getOrCreateConstant(LLVMSplatValue) : nullptr;
197+
}
198+
177199
ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
178200
auto *LLVMC = llvm::ConstantAggregateZero::get(Ty->LLVMTy);
179201
return cast<ConstantAggregateZero>(

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,18 @@ define void @foo() {
524524
auto *StructTy2Packed = sandboxir::ConstantStruct::getTypeForElements(
525525
Ctx, {ZeroI42, OneI42}, /*Packed=*/true);
526526
EXPECT_EQ(StructTy2Packed, StructTyPacked);
527+
528+
// Check ConstantVector::get().
529+
auto *NewCV = sandboxir::ConstantVector::get({ZeroI42, OneI42});
530+
EXPECT_EQ(NewCV, Vector);
531+
// Check ConstantVector::getSplat(), getType().
532+
auto *SplatRaw =
533+
sandboxir::ConstantVector::getSplat(ElementCount::getFixed(2), OneI42);
534+
auto *Splat = cast<sandboxir::ConstantVector>(SplatRaw);
535+
EXPECT_EQ(Splat->getType()->getNumElements(), 2u);
536+
EXPECT_THAT(Splat->operands(), testing::ElementsAre(OneI42, OneI42));
537+
// Check ConstantVector::getSplatValue().
538+
EXPECT_EQ(Splat->getSplatValue(), OneI42);
527539
}
528540

529541
TEST_F(SandboxIRTest, ConstantAggregateZero) {

0 commit comments

Comments
 (0)