Skip to content

Commit 61d2aa0

Browse files
pkwasnie-inteligcbot
authored andcommitted
GEP LSR - don't optimize bool SCEVMulExpr expressions
Scalar Evolution can produce SCEVAddRecExpr based on boolean type, for example: {(true + (trunc i16 %localIdX to i1)),+,true} Ignore such expressions in GEP LSR pass.
1 parent 7d1f041 commit 61d2aa0

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/GEPLoopStrengthReduction/GEPLoopStrengthReduction.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,13 @@ bool Analyzer::deconstructSCEV(const SCEV *S, Analyzer::DeconstructedSCEV &Resul
11221122
if (Add->getNumOperands() != 2)
11231123
return false;
11241124

1125+
// Scalar Evolution can produce SCEVAddRecExpr based on boolean type, for example:
1126+
// {(true + (trunc i16 %localIdX to i1)),+,true}
1127+
// Ignore such expressions.
1128+
Type *Ty = Add->getStart()->getType();
1129+
if (Ty->isIntegerTy() && Ty->getScalarSizeInBits() == 1)
1130+
return false;
1131+
11251132
const SCEV *OpStep = Add->getOperand(1);
11261133

11271134
// Step must be constant in loop's body.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2025 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; RUN: igc_opt --typed-pointers --igc-gep-loop-strength-reduction -S < %s 2>&1 | FileCheck %s
10+
;
11+
; Don't optimize SCEVAddRecExpr based on boolean type.
12+
;
13+
; kernel void test(global float* p, int n) {
14+
; for (int i = 0; i < n; i++) {
15+
; *p += p[i & 1];
16+
; }
17+
; }
18+
19+
define spir_kernel void @test(i32 addrspace(1)* %p, i32 %n) {
20+
entry:
21+
%cmp1 = icmp slt i32 0, %n
22+
br i1 %cmp1, label %for.body.lr.ph, label %for.end
23+
24+
; CHECK-LABEL: for.body.lr.ph:
25+
; CHECK-NEXT: br label %for.body
26+
for.body.lr.ph: ; preds = %entry
27+
br label %for.body
28+
29+
; CHECK-LABEL: for.body:
30+
; CHECK-NEXT: %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
31+
; CHECK-NEXT: %and = and i32 %i.02, 1
32+
; CHECK-NEXT: %idxprom = zext i32 %and to i64
33+
; CHECK-NEXT: %arrayidx = getelementptr inbounds i32, i32 addrspace(1)* %p, i64 %idxprom
34+
; CHECK-NEXT: %0 = load i32, i32 addrspace(1)* %arrayidx, align 4
35+
; CHECK-NEXT: %add = add nsw i32 %0, 1
36+
; CHECK-NEXT: store i32 %add, i32 addrspace(1)* %arrayidx, align 4
37+
; CHECK-NEXT: %inc = add nuw nsw i32 %i.02, 1
38+
; CHECK-NEXT: %cmp = icmp slt i32 %inc, %n
39+
; CHECK-NEXT: br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
40+
for.body: ; preds = %for.body.lr.ph, %for.body
41+
%i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
42+
%and = and i32 %i.02, 1
43+
%idxprom = zext i32 %and to i64
44+
%arrayidx = getelementptr inbounds i32, i32 addrspace(1)* %p, i64 %idxprom
45+
%0 = load i32, i32 addrspace(1)* %arrayidx, align 4
46+
%add = add nsw i32 %0, 1
47+
store i32 %add, i32 addrspace(1)* %arrayidx, align 4
48+
%inc = add nuw nsw i32 %i.02, 1
49+
%cmp = icmp slt i32 %inc, %n
50+
br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
51+
52+
for.cond.for.end_crit_edge: ; preds = %for.body
53+
br label %for.end
54+
55+
for.end: ; preds = %for.cond.for.end_crit_edge, %entry
56+
ret void
57+
}
58+
59+
!igc.functions = !{!0}
60+
61+
!0 = !{void (i32 addrspace(1)*, i32)* @test, !1}
62+
!1 = !{!2}
63+
!2 = !{!"function_type", i32 0}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2025 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; REQUIRES: llvm-14-plus
10+
; RUN: igc_opt --opaque-pointers --igc-gep-loop-strength-reduction -S < %s 2>&1 | FileCheck %s
11+
;
12+
; Don't optimize SCEVAddRecExpr based on boolean type.
13+
;
14+
; kernel void test(global float* p, int n) {
15+
; for (int i = 0; i < n; i++) {
16+
; *p += p[i & 1];
17+
; }
18+
; }
19+
20+
define spir_kernel void @test(ptr addrspace(1) %p, i32 %n) {
21+
entry:
22+
%cmp1 = icmp slt i32 0, %n
23+
br i1 %cmp1, label %for.body.lr.ph, label %for.end
24+
25+
; CHECK-LABEL: for.body.lr.ph:
26+
; CHECK-NEXT: br label %for.body
27+
for.body.lr.ph: ; preds = %entry
28+
br label %for.body
29+
30+
; CHECK-LABEL: for.body:
31+
; CHECK-NEXT: %i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
32+
; CHECK-NEXT: %and = and i32 %i.02, 1
33+
; CHECK-NEXT: %idxprom = zext i32 %and to i64
34+
; CHECK-NEXT: %arrayidx = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %idxprom
35+
; CHECK-NEXT: %0 = load i32, ptr addrspace(1) %arrayidx, align 4
36+
; CHECK-NEXT: %add = add nsw i32 %0, 1
37+
; CHECK-NEXT: store i32 %add, ptr addrspace(1) %arrayidx, align 4
38+
; CHECK-NEXT: %inc = add nuw nsw i32 %i.02, 1
39+
; CHECK-NEXT: %cmp = icmp slt i32 %inc, %n
40+
; CHECK-NEXT: br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
41+
for.body: ; preds = %for.body.lr.ph, %for.body
42+
%i.02 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
43+
%and = and i32 %i.02, 1
44+
%idxprom = zext i32 %and to i64
45+
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %idxprom
46+
%0 = load i32, ptr addrspace(1) %arrayidx, align 4
47+
%add = add nsw i32 %0, 1
48+
store i32 %add, ptr addrspace(1) %arrayidx, align 4
49+
%inc = add nuw nsw i32 %i.02, 1
50+
%cmp = icmp slt i32 %inc, %n
51+
br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
52+
53+
for.cond.for.end_crit_edge: ; preds = %for.body
54+
br label %for.end
55+
56+
for.end: ; preds = %for.cond.for.end_crit_edge, %entry
57+
ret void
58+
}
59+
60+
!igc.functions = !{!0}
61+
62+
!0 = !{ptr @test, !1}
63+
!1 = !{!2}
64+
!2 = !{!"function_type", i32 0}

0 commit comments

Comments
 (0)