Skip to content

[ValueTracking] Fix Overflow with i1 Constant GEPs #125470

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1477,8 +1477,11 @@ static void computeKnownBitsFromOperator(const Operator *I,
// that this is a multiple of the minimum size.
ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes));
} else if (IndexBits.isConstant()) {
APInt IndexConst = IndexBits.getConstant();
APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
// i1 is a valid GEP index, ensure we have enough space to do the
// computation in that case.
unsigned CalcBitWidth = std::max(IndexBitWidth, 8u);
APInt IndexConst = IndexBits.getConstant().zext(CalcBitWidth);
APInt ScalingFactor(CalcBitWidth, TypeSizeInBytes);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be setting IndexBitWidth above to the index type size from DataLayout, and then sextOrTrunc from the size of the index. What the code currently does is just incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and then sextOrTrunc from the size of the index

Which value should be sextOrTrunc'd ? Do you mean IndexConst.sextOrTrunc below should use the GEP Operand type width, and all the rest use the DL Index type size?

Copy link
Contributor

Choose a reason for hiding this comment

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

The correct logic here is quite tricky. Do you mind if I fix this myself?

Copy link
Contributor

Choose a reason for hiding this comment

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

Here it is: #125532

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nikic Thank you, your patch fixes the testcase so it's good on my end! 😄

IndexConst *= ScalingFactor;
AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 -passes=load-store-vectorizer -S -o - %s | FileCheck %s

define amdgpu_kernel void @simple_users_scores() {
; CHECK-LABEL: define amdgpu_kernel void @simple_users_scores(
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[SIMPLEUSER:%.*]] = alloca [4 x i64], i32 0, align 4, addrspace(5)
; CHECK-NEXT: [[G:%.*]] = getelementptr i32, ptr addrspace(5) [[SIMPLEUSER]], i1 true
; CHECK-NEXT: store <2 x i32> zeroinitializer, ptr addrspace(5) [[G]], align 4
; CHECK-NEXT: ret void
;
entry:
%simpleuser = alloca [4 x i64], i32 0, align 4, addrspace(5)
store i32 0, ptr addrspace(5) %simpleuser, align 4
%G = getelementptr i32, ptr addrspace(5) %simpleuser, i1 true
store i32 0, ptr addrspace(5) %G, align 4
ret void
}