Skip to content

[ValueLattice] Support constant vectors in mergeIn() #99466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2024
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
34 changes: 22 additions & 12 deletions llvm/include/llvm/Analysis/ValueLattice.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,21 @@ class ValueLatticeElement {
return std::nullopt;
}

ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) const {
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
ConstantRange asConstantRange(unsigned BW, bool UndefAllowed = false) const {
if (isConstantRange(UndefAllowed))
return getConstantRange();
if (isConstant())
return getConstant()->toConstantRange();
unsigned BW = Ty->getScalarSizeInBits();
if (isUnknown())
return ConstantRange::getEmpty(BW);
return ConstantRange::getFull(BW);
}

ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) const {
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
return asConstantRange(Ty->getScalarSizeInBits(), UndefAllowed);
}

bool markOverdefined() {
if (isOverdefined())
return false;
Expand Down Expand Up @@ -384,7 +387,9 @@ class ValueLatticeElement {
return true;
}

assert(isUnknown() || isUndef());
assert(isUnknown() || isUndef() || isConstant());
assert((!isConstant() || NewR.contains(getConstant()->toConstantRange())) &&
"Constant must be subset of new range");

NumRangeExtensions = 0;
Tag = NewTag;
Expand Down Expand Up @@ -426,6 +431,16 @@ class ValueLatticeElement {
return false;
if (RHS.isUndef())
return false;
// If the constant is a vector of integers, try to treat it as a range.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know the reason that we weren't allowed to convert a constant into a constant range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For plain integers, constants get converted into constant range on construction. For vectors this doesn't happen as it's a lossy operation. So I think there was just never a need for it before.

if (getConstant()->getType()->isVectorTy() &&
getConstant()->getType()->getScalarType()->isIntegerTy()) {
ConstantRange L = getConstant()->toConstantRange();
ConstantRange NewR = L.unionWith(
RHS.asConstantRange(L.getBitWidth(), /*UndefAllowed=*/true));
return markConstantRange(
std::move(NewR),
Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));
}
markOverdefined();
return true;
}
Expand All @@ -444,14 +459,9 @@ class ValueLatticeElement {
return OldTag != Tag;
}

if (!RHS.isConstantRange()) {
// We can get here if we've encountered a constantexpr of integer type
// and merge it with a constantrange.
markOverdefined();
return true;
}

ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
const ConstantRange &L = getConstantRange();
ConstantRange NewR = L.unionWith(
RHS.asConstantRange(L.getBitWidth(), /*UndefAllowed=*/true));
return markConstantRange(
std::move(NewR),
Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ define <2 x i16> @phi_merge1(i1 %c, <2 x i8> %a) {
; CHECK-NEXT: br label %[[JOIN]]
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ [[ZEXT]], %[[ENTRY]] ], [ <i16 1, i16 2>, %[[IF]] ]
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: ret <2 x i16> [[ADD]]
;
entry:
Expand All @@ -312,7 +312,7 @@ define <2 x i16> @phi_merge2(i1 %c, <2 x i8> %a) {
; CHECK-NEXT: br label %[[JOIN]]
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ <i16 1, i16 2>, %[[ENTRY]] ], [ [[ZEXT]], %[[IF]] ]
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: ret <2 x i16> [[ADD]]
;
entry:
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/SCCP/phis.ll
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ define <2 x i16> @phi_vector_merge1(i1 %c, <2 x i8> %a) {
; CHECK-NEXT: br label %[[JOIN]]
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ [[ZEXT]], %[[ENTRY]] ], [ <i16 1, i16 2>, %[[IF]] ]
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: ret <2 x i16> [[ADD]]
;
entry:
Expand All @@ -135,7 +135,7 @@ define <2 x i16> @phi_vector_merge2(i1 %c, <2 x i8> %a) {
; CHECK-NEXT: br label %[[JOIN]]
; CHECK: [[JOIN]]:
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ <i16 1, i16 2>, %[[ENTRY]] ], [ [[ZEXT]], %[[IF]] ]
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
; CHECK-NEXT: ret <2 x i16> [[ADD]]
;
entry:
Expand Down
Loading