Skip to content

Commit 332d61e

Browse files
mmereckiigcbot
authored andcommitted
Support more scenarios in ShrinkArrayAlloca
This PR adds support for scenarios that use `InsertElement` instructions.
1 parent 77c938c commit 332d61e

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

IGC/Compiler/ShrinkArrayAlloca.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ inline Value* RepackToOldType(
176176
// - only bitcast, PHI, load, store and extract element instructions are
177177
// supported
178178
inline bool GetUsedVectorElements(
179-
Value* parentVal,
179+
Value* parentPtr,
180180
Value* val,
181181
SmallVector<bool, 4>& used)
182182
{
@@ -188,6 +188,7 @@ inline bool GetUsedVectorElements(
188188
// Unsupported alloca use type.
189189
return false;
190190
}
191+
parentPtr = gepInst;
191192
}
192193
else if (ExtractElementInst* ee = dyn_cast<ExtractElementInst>(val))
193194
{
@@ -230,13 +231,17 @@ inline bool GetUsedVectorElements(
230231
{
231232
return false;
232233
}
234+
if (srcTy->isPointerTy())
235+
{
236+
parentPtr = bc;
237+
}
233238
// Supported bit cast type, check users for extract element
234239
// instructions.
235240
}
236241
else if (StoreInst* store = dyn_cast<StoreInst>(val))
237242
{
238243
// This function only needs to check read access.
239-
if (store->getPointerOperand() == parentVal)
244+
if (store->getPointerOperand() == parentPtr)
240245
{
241246
return true;
242247
}
@@ -247,6 +252,7 @@ inline bool GetUsedVectorElements(
247252
}
248253
else if (!(isa<AllocaInst>(val) ||
249254
isa<PHINode>(val) ||
255+
isa<InsertElementInst>(val) ||
250256
isa<LoadInst>(val)))
251257
{
252258
return false;
@@ -255,7 +261,7 @@ inline bool GetUsedVectorElements(
255261
// Follow the def-use chain
256262
for (User* user : val->users())
257263
{
258-
if (!GetUsedVectorElements(val, user, used))
264+
if (!GetUsedVectorElements(parentPtr, user, used))
259265
{
260266
return false;
261267
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 %s -S --shrink-array-alloca | FileCheck %s
10+
;
11+
12+
define float @f0(i32 %index, float %data0) #1 {
13+
Label-0:
14+
%alloca = alloca [64 x <4 x float>], align 16
15+
%addr = getelementptr [64 x <4 x float>], [64 x <4 x float>]* %alloca, i32 0, i32 %index
16+
%vec0 = load <4 x float>, <4 x float>* %addr
17+
%data1 = insertelement <4 x float> %vec0, float %data0, i64 2
18+
store <4 x float> %data1, <4 x float>* %addr, align 16
19+
%addr1 = getelementptr [64 x <4 x float>], [64 x <4 x float>]* %alloca, i32 0, i32 %index
20+
%vec1 = load <4 x float>, <4 x float>* %addr1
21+
%a = extractelement <4 x float> %vec1, i64 2
22+
ret float %a
23+
}
24+
25+
; CHECK: [[ALLOCA:%[A-z0-9]*]] = alloca [64 x float], align 4
26+
; CHECK: [[ADDR:%[A-z0-9]*]] = getelementptr [64 x float], [64 x float]* [[ALLOCA]], i32 0, i32 %index
27+
; CHECK: [[Z:%[A-z0-9]*]] = load float, float* [[ADDR]], align 4
28+
; CHECK: [[VEC0:%[A-z0-9]*]] = insertelement <4 x float> undef, float [[Z]], i64 2
29+
; CHECK: [[VEC1:%[A-z0-9]*]] = insertelement <4 x float> [[VEC0]], float %data0, i64 2
30+
; CHECK: [[Z:%[A-z0-9]*]] = extractelement <4 x float> [[VEC1]], i64 2
31+
; CHECK-NEXT: store float [[Z]], float* [[ADDR]], align 4
32+
; CHECK: [[ADDR:%[A-z0-9]*]] = getelementptr [64 x float], [64 x float]* [[ALLOCA]], i32 0, i32 %index
33+
; CHECK: [[A:%[A-z0-9]*]] = load float, float* [[ADDR]], align 4
34+
; CHECK: ret float [[A]]
35+
36+
attributes #1 = { "null-pointer-is-valid"="true" }
37+

0 commit comments

Comments
 (0)