Skip to content

[LV] Fix '-1U' bits for smallest type in getSmallestAndWidestTypes #135783

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
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
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4798,17 +4798,16 @@ LoopVectorizationCostModel::getSmallestAndWidestTypes() {
// if there are no loads/stores in the loop. In this case, check through the
// reduction variables to determine the maximum width.
if (ElementTypesInLoop.empty() && !Legal->getReductionVars().empty()) {
// Reset MaxWidth so that we can find the smallest type used by recurrences
// in the loop.
MaxWidth = -1U;
for (const auto &PhiDescriptorPair : Legal->getReductionVars()) {
const RecurrenceDescriptor &RdxDesc = PhiDescriptorPair.second;
// When finding the min width used by the recurrence we need to account
// for casts on the input operands of the recurrence.
MaxWidth = std::min<unsigned>(
MaxWidth, std::min<unsigned>(
MinWidth = std::min<unsigned>(
MinWidth, std::min<unsigned>(
RdxDesc.getMinWidthCastToRecurrenceTypeInBits(),
RdxDesc.getRecurrenceType()->getScalarSizeInBits()));
MaxWidth = std::max<unsigned>(
Copy link
Contributor

Choose a reason for hiding this comment

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

This change does also change how MaxWidth is calculated, previously MaxWidth was what MinWidth is now?

Granted, it seems to make sense to change this as well, although I am not sure if this was done intentionally when support for in-loop reductions originally

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change does also change how MaxWidth is calculated, previously MaxWidth was what MinWidth is now?

Yes, that's right.

I am not sure if this was done intentionally when support for in-loop reductions originally

I wonder if MaxWidth got confused with the resulting maximum vector width (/factor) at some point. The original patch that added this was https://reviews.llvm.org/D113973.

The tests added in that patch contain two different types, particularly for testing this purpose, although the CHECK lines (e.g. CHECK: The Smallest and Widest types: 4294967295 / 16 bits) seemed wrong, but after this change it is what I would expect it to be.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it seems like the new behavior matches the expectations.

MaxWidth, RdxDesc.getRecurrenceType()->getScalarSizeInBits());
}
} else {
for (Type *T : ElementTypesInLoop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ for.end:
; chosen. The following 3 cases check different combinations of widths.

; CHECK-LABEL: Checking a loop in 'no_loads_stores_32'
; CHECK: The Smallest and Widest types: 4294967295 / 32 bits
; CHECK: The Smallest and Widest types: 32 / 64 bits
; CHECK: Selecting VF: 4

define double @no_loads_stores_32(i32 %n) {
Expand All @@ -60,7 +60,7 @@ for.end:
}

; CHECK-LABEL: Checking a loop in 'no_loads_stores_16'
; CHECK: The Smallest and Widest types: 4294967295 / 16 bits
; CHECK: The Smallest and Widest types: 16 / 64 bits
; CHECK: Selecting VF: 8

define double @no_loads_stores_16() {
Expand All @@ -82,7 +82,7 @@ for.end:
}

; CHECK-LABEL: Checking a loop in 'no_loads_stores_8'
; CHECK: The Smallest and Widest types: 4294967295 / 8 bits
; CHECK: The Smallest and Widest types: 8 / 32 bits
; CHECK: Selecting VF: 16

define float @no_loads_stores_8() {
Expand Down
Loading