From e620eac75e470f94928a17d215a3be947712028b Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 21 Aug 2023 10:36:17 -0700 Subject: [PATCH] [SelectionDAG][RISCV][SVE] Harden fixed offset version of ComputeValueVTs against scalable offsets. Use getFixedValue instead of getKnownMinValue to convert TypeSize to uint64_t. I believe this would have caught the bug fixed by D157872. To prevent false failures, I had to treat a scalable 0 as if it is fixed value. Reviewed By: paulwalker-arm Differential Revision: https://reviews.llvm.org/D158115 --- llvm/include/llvm/Support/TypeSize.h | 2 +- llvm/lib/CodeGen/Analysis.cpp | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/llvm/include/llvm/Support/TypeSize.h b/llvm/include/llvm/Support/TypeSize.h index 9683c82b2278f..5c15ed9e4d9bd 100644 --- a/llvm/include/llvm/Support/TypeSize.h +++ b/llvm/include/llvm/Support/TypeSize.h @@ -180,7 +180,7 @@ template class FixedOrScalableQuantity { // Use in places where a scalable count doesn't make sense (e.g. non-vector // types, or vectors in backends which don't support scalable vectors). constexpr ScalarTy getFixedValue() const { - assert(!isScalable() && + assert((!isScalable() || isZero()) && "Request for a fixed element count on a scalable object"); return getKnownMinValue(); } diff --git a/llvm/lib/CodeGen/Analysis.cpp b/llvm/lib/CodeGen/Analysis.cpp index 2065bfbd1c449..1994e6aec84b2 100644 --- a/llvm/lib/CodeGen/Analysis.cpp +++ b/llvm/lib/CodeGen/Analysis.cpp @@ -140,15 +140,14 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, SmallVectorImpl *FixedOffsets, uint64_t StartingOffset) { TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); - SmallVector Offsets; - if (FixedOffsets) + if (FixedOffsets) { + SmallVector Offsets; ComputeValueVTs(TLI, DL, Ty, ValueVTs, &Offsets, Offset); - else - ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offset); - - if (FixedOffsets) for (TypeSize Offset : Offsets) - FixedOffsets->push_back(Offset.getKnownMinValue()); + FixedOffsets->push_back(Offset.getFixedValue()); + } else { + ComputeValueVTs(TLI, DL, Ty, ValueVTs, nullptr, Offset); + } } void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, @@ -166,15 +165,14 @@ void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, SmallVectorImpl *FixedOffsets, uint64_t StartingOffset) { TypeSize Offset = TypeSize::get(StartingOffset, Ty->isScalableTy()); - SmallVector Offsets; - if (FixedOffsets) + if (FixedOffsets) { + SmallVector Offsets; ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, &Offsets, Offset); - else - ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, nullptr, Offset); - - if (FixedOffsets) for (TypeSize Offset : Offsets) - FixedOffsets->push_back(Offset.getKnownMinValue()); + FixedOffsets->push_back(Offset.getFixedValue()); + } else { + ComputeValueVTs(TLI, DL, Ty, ValueVTs, MemVTs, nullptr, Offset); + } } void llvm::computeValueLLTs(const DataLayout &DL, Type &Ty,