Skip to content

Commit e2f6290

Browse files
committed
[VectorCombine] Discard ScalarizationResult state in early exit.
ScalarizationResult's destructor makes sure ToFreeze is not ignored if set. Currently, scalarizeLoadExtract has an early exit if the index is not safe directly. But when it is SafeWithFreeze, we need to discard the state first, otherwise we hit the assert in the destructor. Fixes PR51992.
1 parent 1aa7b83 commit e2f6290

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,12 @@ class ScalarizationResult {
822822
/// freeze.
823823
bool isSafeWithFreeze() const { return Status == StatusTy::SafeWithFreeze; }
824824

825+
/// Reset the state of Unsafe and clear ToFreze if set.
826+
void discard() {
827+
ToFreeze = nullptr;
828+
Status = StatusTy::Unsafe;
829+
}
830+
825831
/// Freeze the ToFreeze and update the use in \p User to use it.
826832
void freeze(IRBuilder<> &Builder, Instruction &UserI) {
827833
assert(isSafeWithFreeze() &&
@@ -1006,6 +1012,7 @@ bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
10061012
auto ScalarIdx = canScalarizeAccess(FixedVT, UI->getOperand(1), &I, AC, DT);
10071013
if (!ScalarIdx.isSafe()) {
10081014
// TODO: Freeze index if it is safe to do so.
1015+
ScalarIdx.discard();
10091016
return false;
10101017
}
10111018

llvm/test/Transforms/VectorCombine/AArch64/load-extractelement-scalarization.ll

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,37 @@ define i32 @load_multiple_extracts_with_variable_indices_large_vector_all_valid_
678678
ret i32 %res
679679
}
680680

681+
; Test case from PR51992.
682+
; TODO: could handle by inserting freeze.
683+
define i8 @load_extract_safe_with_freeze(<8 x i8> %in, <16 x i8>* %src) {
684+
; CHECK-LABEL: @load_extract_safe_with_freeze(
685+
; CHECK-NEXT: entry:
686+
; CHECK-NEXT: [[EXT_IDX:%.*]] = extractelement <8 x i8> [[IN:%.*]], i32 0
687+
; CHECK-NEXT: [[EXT_IDX_I32:%.*]] = zext i8 [[EXT_IDX]] to i32
688+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[EXT_IDX]], 99
689+
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[EXIT:%.*]]
690+
; CHECK: then:
691+
; CHECK-NEXT: [[LOAD:%.*]] = load <16 x i8>, <16 x i8>* [[SRC:%.*]], align 16
692+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[EXT_IDX_I32]], 15
693+
; CHECK-NEXT: [[EXT:%.*]] = extractelement <16 x i8> [[LOAD]], i32 [[AND]]
694+
; CHECK-NEXT: br label [[EXIT]]
695+
; CHECK: exit:
696+
; CHECK-NEXT: [[P:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[EXT]], [[THEN]] ]
697+
; CHECK-NEXT: ret i8 0
698+
;
699+
entry:
700+
%ext.idx = extractelement <8 x i8> %in, i32 0
701+
%ext.idx.i32 = zext i8 %ext.idx to i32
702+
%cmp = icmp ult i8 %ext.idx, 99
703+
br i1 %cmp, label %then, label %exit
704+
705+
then:
706+
%load = load <16 x i8>, <16 x i8>* %src, align 16
707+
%and = and i32 %ext.idx.i32, 15
708+
%ext = extractelement <16 x i8> %load, i32 %and
709+
br label %exit
710+
711+
exit:
712+
%p = phi i8 [ 0, %entry ], [ %ext, %then ]
713+
ret i8 0
714+
}

0 commit comments

Comments
 (0)