diff options
author | Fraser Cormack <fraser@codeplay.com> | 2021-05-07 17:41:27 +0100 |
---|---|---|
committer | Fraser Cormack <fraser@codeplay.com> | 2021-05-10 13:51:53 +0100 |
commit | 3212a08a8c811441ca68009118758998750ce905 (patch) | |
tree | 663fa0d87994535edcd1d363444767b9d236f067 /llvm/lib/IR/Constants.cpp | |
parent | f088af37e6b570dd070ae4e6fc14e22d21cda3be (diff) | |
download | llvm-3212a08a8c811441ca68009118758998750ce905.zip llvm-3212a08a8c811441ca68009118758998750ce905.tar.gz llvm-3212a08a8c811441ca68009118758998750ce905.tar.bz2 |
[Constant] Allow ConstantAggregateZero a scalable element count
A ConstantAggregateZero may be created from a scalable vector type.
However, it still assumed fixed number of elements when queried for
them. This patch changes ConstantAggregateZero to correctly report its
element count.
This change fixes a couple of issues. Firstly, it fixes a crash in
Constant::getUniqueValue when called on a scalable-vector
zeroinitializer constant.
Secondly, it fixes a latent bug in GlobalISel's IRTranslator in which
translating a scalable-vector zeroinitializer would hit the assertion in
ConstantAggregateZero::getNumElements when casting to a FixedVectorType,
rather than reporting an error more gracefully. This is currently
hypothetical as the IRTranslator has deeper issues preventing the use of
scalable vector types.
Reviewed By: RKSimon
Differential Revision: https://reviews.llvm.org/D102082
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index c0b8af8..7d93d41 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -425,13 +425,15 @@ Constant *Constant::getAggregateElement(unsigned Elt) const { if (const auto *CC = dyn_cast<ConstantAggregate>(this)) return Elt < CC->getNumOperands() ? CC->getOperand(Elt) : nullptr; + if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this)) + return Elt < CAZ->getElementCount().getKnownMinValue() + ? CAZ->getElementValue(Elt) + : nullptr; + // FIXME: getNumElements() will fail for non-fixed vector types. if (isa<ScalableVectorType>(getType())) return nullptr; - if (const auto *CAZ = dyn_cast<ConstantAggregateZero>(this)) - return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr; - if (const auto *PV = dyn_cast<PoisonValue>(this)) return Elt < PV->getNumElements() ? PV->getElementValue(Elt) : nullptr; @@ -1088,13 +1090,13 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { return getStructElement(Idx); } -unsigned ConstantAggregateZero::getNumElements() const { +ElementCount ConstantAggregateZero::getElementCount() const { Type *Ty = getType(); if (auto *AT = dyn_cast<ArrayType>(Ty)) - return AT->getNumElements(); + return ElementCount::getFixed(AT->getNumElements()); if (auto *VT = dyn_cast<VectorType>(Ty)) - return cast<FixedVectorType>(VT)->getNumElements(); - return Ty->getStructNumElements(); + return VT->getElementCount(); + return ElementCount::getFixed(Ty->getStructNumElements()); } //===----------------------------------------------------------------------===// |