Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion llvm/include/llvm/SandboxIR/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,19 @@ class ConstantVector final : public ConstantAggregate {
friend class Context; // For constructor.

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

/// For isa/dyn_cast.
static bool classof(const Value *From) {
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Value {
friend class CmpInst; // For getting `Val`.
friend class ConstantArray; // For `Val`.
friend class ConstantStruct; // For `Val`.
friend class ConstantVector; // For `Val`.
friend class ConstantAggregateZero; // For `Val`.
friend class ConstantPointerNull; // For `Val`.
friend class UndefValue; // For `Val`.
Expand Down
22 changes: 22 additions & 0 deletions llvm/lib/SandboxIR/Constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ StructType *ConstantStruct::getTypeForElements(Context &Ctx,
return StructType::get(Ctx, EltTypes, Packed);
}

Constant *ConstantVector::get(ArrayRef<Constant *> V) {
assert(!V.empty() && "Expected non-empty V!");
auto &Ctx = V[0]->getContext();
SmallVector<llvm::Constant *, 8> LLVMV;
LLVMV.reserve(V.size());
for (auto *Elm : V)
LLVMV.push_back(cast<llvm::Constant>(Elm->Val));
return Ctx.getOrCreateConstant(llvm::ConstantVector::get(LLVMV));
}

Constant *ConstantVector::getSplat(ElementCount EC, Constant *Elt) {
auto *LLVMElt = cast<llvm::Constant>(Elt->Val);
auto &Ctx = Elt->getContext();
return Ctx.getOrCreateConstant(llvm::ConstantVector::getSplat(EC, LLVMElt));
}

Constant *ConstantVector::getSplatValue(bool AllowPoison) const {
auto *LLVMSplatValue = cast_or_null<llvm::Constant>(
cast<llvm::ConstantVector>(Val)->getSplatValue(AllowPoison));
return LLVMSplatValue ? Ctx.getOrCreateConstant(LLVMSplatValue) : nullptr;
}

ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
auto *LLVMC = llvm::ConstantAggregateZero::get(Ty->LLVMTy);
return cast<ConstantAggregateZero>(
Expand Down
12 changes: 12 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ define void @foo() {
auto *StructTy2Packed = sandboxir::ConstantStruct::getTypeForElements(
Ctx, {ZeroI42, OneI42}, /*Packed=*/true);
EXPECT_EQ(StructTy2Packed, StructTyPacked);

// Check ConstantVector::get().
auto *NewCV = sandboxir::ConstantVector::get({ZeroI42, OneI42});
EXPECT_EQ(NewCV, Vector);
// Check ConstantVector::getSplat(), getType().
auto *SplatRaw =
sandboxir::ConstantVector::getSplat(ElementCount::getFixed(2), OneI42);
auto *Splat = cast<sandboxir::ConstantVector>(SplatRaw);
EXPECT_EQ(Splat->getType()->getNumElements(), 2u);
EXPECT_THAT(Splat->operands(), testing::ElementsAre(OneI42, OneI42));
// Check ConstantVector::getSplatValue().
EXPECT_EQ(Splat->getSplatValue(), OneI42);
}

TEST_F(SandboxIRTest, ConstantAggregateZero) {
Expand Down