-
Notifications
You must be signed in to change notification settings - Fork 341
[Bugfix] Fix index handling to promote 64-bit integers #796
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,35 +123,43 @@ class IndexLegalizer : public IRMutatorWithAnalyzer { | |
| auto buffer_store = | ||
| Downcast<BufferStore>(IRMutatorWithAnalyzer::VisitStmt_(op)); | ||
| auto indices = buffer_store->indices; | ||
| Array<PrimExpr> new_indices; | ||
| for (auto index : indices) { | ||
| if (index->dtype.is_int() && index->dtype.bits() < 64) { | ||
| auto int_bound = analyzer_->const_int_bound(index); | ||
| if (int_bound->max_value >= (1LL << (index->dtype.bits() - 1)) - 1 || | ||
| int_bound->min_value < -(1LL << (index->dtype.bits() - 1))) { | ||
| Int64Promoter promoter; | ||
| index = promoter(index); | ||
| new_indices.push_back(index); | ||
| continue; | ||
| } | ||
| } | ||
| new_indices.push_back(index); | ||
| } | ||
|
Comment on lines
127
to
139
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic inside this for-loop can be simplified by removing the for (auto index : indices) {
if (index->dtype.is_int() && index->dtype.bits() < 64) {
auto int_bound = analyzer_->const_int_bound(index);
if (int_bound->max_value >= (1LL << (index->dtype.bits() - 1)) - 1 ||
int_bound->min_value < -(1LL << (index->dtype.bits() - 1))) {
Int64Promoter promoter;
index = promoter(index);
}
}
new_indices.push_back(index);
} |
||
| buffer_store.CopyOnWrite()->indices = indices; | ||
| buffer_store.CopyOnWrite()->indices = new_indices; | ||
| return std::move(buffer_store); | ||
| } | ||
|
|
||
| PrimExpr VisitExpr_(const BufferLoadNode *op) final { | ||
| auto buffer_load = | ||
| Downcast<BufferLoad>(IRMutatorWithAnalyzer::VisitExpr_(op)); | ||
| auto indices = buffer_load->indices; | ||
| Array<PrimExpr> new_indices; | ||
| for (auto index : indices) { | ||
| if (index->dtype.is_int() && index->dtype.bits() < 64) { | ||
| auto int_bound = analyzer_->const_int_bound(index); | ||
| if (int_bound->max_value >= (1LL << (index->dtype.bits() - 1)) - 1 || | ||
| int_bound->min_value < -(1LL << (index->dtype.bits() - 1))) { | ||
| Int64Promoter promoter; | ||
| index = promoter(index); | ||
| new_indices.push_back(index); | ||
| continue; | ||
| } | ||
| } | ||
| new_indices.push_back(index); | ||
| } | ||
|
Comment on lines
149
to
161
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop can be simplified in the same way as in for (auto index : indices) {
if (index->dtype.is_int() && index->dtype.bits() < 64) {
auto int_bound = analyzer_->const_int_bound(index);
if (int_bound->max_value >= (1LL << (index->dtype.bits() - 1)) - 1 ||
int_bound->min_value < -(1LL << (index->dtype.bits() - 1))) {
Int64Promoter promoter;
index = promoter(index);
}
}
new_indices.push_back(index);
} |
||
| buffer_load.CopyOnWrite()->indices = indices; | ||
| buffer_load.CopyOnWrite()->indices = new_indices; | ||
| return std::move(buffer_load); | ||
| } | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify overflow detection logic for signed integer bounds.
The overflow detection uses
(1LL << (index->dtype.bits() - 1)) - 1for max_value and-(1LL << (index->dtype.bits() - 1))for min_value. This appears to check if the value exceeds the representable range for the current bit width, but the logic seems to promote indices that are already at the boundary of their type's range.Let me verify the signed integer overflow detection logic:
Also applies to: 152-153
🏁 Script executed:
Length of output: 652
Fix signed overflow check to exclude boundary value
In src/transform/config_index_bitwidth.cc (lines 130–131), the current condition
will promote indices already equal to the max representable value (e.g. 127 for 8 bits). Change the max‐side comparison to
>so only values exceeding the range trigger promotion:🤖 Prompt for AI Agents