Skip to content

Commit 5964a8e

Browse files
committed
[LangRef] Require that vscale be a power of two
This change proposes that we require vscale to be a power of two. This is already true for both in tree backends which support scalable types. We had two mechanism for exposing this to the optimizer. First, we specify that any function with vscale_range must have a vscale power-of-two type. In practice, clang will always emit this attribute for RISCV and AArch64. Note there's a semantic oddity here as vscale is required to be a global constant, and yet attributes are per function. Second, we have a TTI hook. Again, both targets which support scalable vectors returned true for this hook. This change removes both the TTI hook and the vscale_range check in ValueTracking (since they're no longer needed), and deletes the very small amount of code which becomes dead once the results of the TTI hook are constant. As can be seen from the test diffs, we had at least some places which hadn't used the existing hooks. This might be an artifact of the way the tests were written, I haven't checked closely. The biggest argument against this change is that we might someday want to support a non-power-of-two vscale. I argue that this is false generality, and we should simplify for near term reality. If a piece of hardware comes along with a register size which is not a multiple of two (as the original SVE specification allowed before it was revised), we can reverse this change at that time.
1 parent c103bbc commit 5964a8e

26 files changed

+310
-462
lines changed

llvm/docs/LangRef.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4442,10 +4442,11 @@ elementtype may be any integer, floating-point, pointer type, or a sized
44424442
target extension type that has the ``CanBeVectorElement`` property. Vectors
44434443
of size zero are not allowed. For scalable vectors, the total number of
44444444
elements is a constant multiple (called vscale) of the specified number
4445-
of elements; vscale is a positive integer that is unknown at compile time
4446-
and the same hardware-dependent constant for all scalable vectors at run
4447-
time. The size of a specific scalable vector type is thus constant within
4448-
IR, even if the exact size in bytes cannot be determined until run time.
4445+
of elements; vscale is a positive power-of-two integer that is unknown
4446+
at compile time and the same hardware-dependent constant for all scalable
4447+
vectors at run time. The size of a specific scalable vector type is thus
4448+
constant within IR, even if the exact size in bytes cannot be determined
4449+
until run time.
44494450

44504451
:Examples:
44514452

@@ -30398,8 +30399,8 @@ vectors such as ``<vscale x 16 x i8>``.
3039830399
Semantics:
3039930400
""""""""""
3040030401

30401-
``vscale`` is a positive value that is constant throughout program
30402-
execution, but is unknown at compile time.
30402+
``vscale`` is a positive power-of-two value that is constant throughout
30403+
program execution, but is unknown at compile time.
3040330404
If the result value does not fit in the result type, then the result is
3040430405
a :ref:`poison value <poisonvalues>`.
3040530406

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,9 +1220,6 @@ class TargetTransformInfo {
12201220
/// \return the value of vscale to tune the cost model for.
12211221
LLVM_ABI std::optional<unsigned> getVScaleForTuning() const;
12221222

1223-
/// \return true if vscale is known to be a power of 2
1224-
LLVM_ABI bool isVScaleKnownToBeAPowerOfTwo() const;
1225-
12261223
/// \return True if the vectorization factor should be chosen to
12271224
/// make the vector of the smallest element type match the size of a
12281225
/// vector register. For wider element types, this could result in

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ class TargetTransformInfoImplBase {
591591
virtual std::optional<unsigned> getVScaleForTuning() const {
592592
return std::nullopt;
593593
}
594-
virtual bool isVScaleKnownToBeAPowerOfTwo() const { return false; }
595594

596595
virtual bool
597596
shouldMaximizeVectorBandwidth(TargetTransformInfo::RegisterKind K) const {

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
864864
std::optional<unsigned> getVScaleForTuning() const override {
865865
return std::nullopt;
866866
}
867-
bool isVScaleKnownToBeAPowerOfTwo() const override { return false; }
868867

869868
/// Estimate the overhead of scalarizing an instruction. Insert and Extract
870869
/// are set if the demanded result elements need to be inserted and/or

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,6 @@ class LLVM_ABI TargetLoweringBase {
623623
return BypassSlowDivWidths;
624624
}
625625

626-
/// Return true only if vscale must be a power of two.
627-
virtual bool isVScaleKnownToBeAPowerOfTwo() const { return false; }
628-
629626
/// Return true if Flow Control is an expensive operation that should be
630627
/// avoided.
631628
bool isJumpExpensive() const { return JumpIsExpensive; }

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,6 @@ std::optional<unsigned> TargetTransformInfo::getVScaleForTuning() const {
795795
return TTIImpl->getVScaleForTuning();
796796
}
797797

798-
bool TargetTransformInfo::isVScaleKnownToBeAPowerOfTwo() const {
799-
return TTIImpl->isVScaleKnownToBeAPowerOfTwo();
800-
}
801-
802798
bool TargetTransformInfo::shouldMaximizeVectorBandwidth(
803799
TargetTransformInfo::RegisterKind K) const {
804800
return TTIImpl->shouldMaximizeVectorBandwidth(K);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,11 +2474,9 @@ bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
24742474
if (!I)
24752475
return false;
24762476

2477-
if (Q.CxtI && match(V, m_VScale())) {
2478-
const Function *F = Q.CxtI->getFunction();
2479-
// The vscale_range indicates vscale is a power-of-two.
2480-
return F->hasFnAttribute(Attribute::VScaleRange);
2481-
}
2477+
// vscale is a power-of-two by definition
2478+
if (match(V, m_VScale()))
2479+
return true;
24822480

24832481
// 1 << X is clearly a power of two if the one is not shifted off the end. If
24842482
// it is shifted off the end then the result is undefined.

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4660,7 +4660,6 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth) const {
46604660

46614661
// vscale(power-of-two) is a power-of-two for some targets
46624662
if (Val.getOpcode() == ISD::VSCALE &&
4663-
getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
46644663
isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1))
46654664
return true;
46664665

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ class AArch64TargetLowering : public TargetLowering {
517517
SDValue Chain, SDValue InGlue, unsigned Condition,
518518
SDValue PStateSM = SDValue()) const;
519519

520-
bool isVScaleKnownToBeAPowerOfTwo() const override { return true; }
521-
522520
// Normally SVE is only used for byte size vectors that do not fit within a
523521
// NEON vector. This changes when OverrideNEON is true, allowing SVE to be
524522
// used for 64bit and 128bit vectors as well.

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ class AArch64TTIImpl final : public BasicTTIImplBase<AArch64TTIImpl> {
156156
return ST->getVScaleForTuning();
157157
}
158158

159-
bool isVScaleKnownToBeAPowerOfTwo() const override { return true; }
160-
161159
bool shouldMaximizeVectorBandwidth(
162160
TargetTransformInfo::RegisterKind K) const override;
163161

0 commit comments

Comments
 (0)