Skip to content

[NaryReassociate] Fix crash from pointer width / index width confusion #125923

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 3 commits into from
Feb 6, 2025
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
11 changes: 6 additions & 5 deletions llvm/lib/Transforms/Scalar/NaryReassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,17 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
IndexExprs.push_back(SE->getSCEV(Index));
// Replace the I-th index with LHS.
IndexExprs[I] = SE->getSCEV(LHS);
Type *GEPArgType = SE->getEffectiveSCEVType(GEP->getOperand(I)->getType());
Type *LHSType = SE->getEffectiveSCEVType(LHS->getType());
size_t LHSSize = DL->getTypeSizeInBits(LHSType).getFixedValue();
size_t GEPArgSize = DL->getTypeSizeInBits(GEPArgType).getFixedValue();
if (isKnownNonNegative(LHS, SimplifyQuery(*DL, DT, AC, GEP)) &&
DL->getTypeSizeInBits(LHS->getType()).getFixedValue() <
DL->getTypeSizeInBits(GEP->getOperand(I)->getType())
.getFixedValue()) {
LHSSize < GEPArgSize) {
// Zero-extend LHS if it is non-negative. InstCombine canonicalizes sext to
// zext if the source operand is proved non-negative. We should do that
// consistently so that CandidateExpr more likely appears before. See
// @reassociate_gep_assume for an example of this canonicalization.
IndexExprs[I] =
SE->getZeroExtendExpr(IndexExprs[I], GEP->getOperand(I)->getType());
IndexExprs[I] = SE->getZeroExtendExpr(IndexExprs[I], GEPArgType);
}
const SCEV *CandidateExpr = SE->getGEPExpr(cast<GEPOperator>(GEP),
IndexExprs);
Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Transforms/NaryReassociate/nary-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ define void @no_sext_fat_pointer(ptr addrspace(2) %a, i32 %i, i32 %j) {
ret void
}

define ptr addrspace(2) @zext_fat_pointer_crash(ptr addrspace(2) %p, i32 %a) {
; CHECK-LABEL: @zext_fat_pointer_crash(
; CHECK-NEXT: [[C:%.*]] = add i32 [[A:%.*]], 1
; CHECK-NEXT: [[Q:%.*]] = getelementptr double, ptr addrspace(2) [[P:%.*]], i32 [[C]]
; CHECK-NEXT: ret ptr addrspace(2) [[Q]]
;
%c = add i32 %a, 1
%q = getelementptr double, ptr addrspace(2) %p, i32 %c
ret ptr addrspace(2) %q
}

define void @or_disjoint(ptr addrspace(2) %a, i32 %i, i32 %j, i32 %k) {
; CHECK-LABEL: @or_disjoint(
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[I:%.*]], [[J:%.*]]
Expand Down