Skip to content

Commit 3bae383

Browse files
ustachowigcbot
authored andcommitted
Enhance type safety checks in LegalizationPass
Adds an element type (EltTy) comparison to ensure ExtractElementInst (EEI) instances have the same element size as the original type.
1 parent 8ca9843 commit 3bae383

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

IGC/Compiler/LegalizationPass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ LegalizeGVNBitCastPattern(IRBuilder<>* Builder, const DataLayout* DL,
563563
// Assign null to BI if this is not ending with a bitcast.
564564
BI = dyn_cast<BitCastInst>(TI->user_back());
565565

566-
// This gurantees all uses of BI could be replaced by the source.
566+
// This guarantees all uses of BI could be replaced by the source.
567567
if (BI && BI->getType() != EltTy)
568568
return false;
569569
else if (TI->getType()->getPrimitiveSizeInBits() !=
@@ -594,11 +594,11 @@ LegalizeGVNBitCastPattern(IRBuilder<>* Builder, const DataLayout* DL,
594594
if (!BI || !BI->getType()->isVectorTy())
595595
return false;
596596

597-
// All uses must be EEI.
597+
// All uses must be EEI and must have the same element size as the original element type.
598598
for (auto U : BI->users())
599599
{
600600
auto EEI = dyn_cast<ExtractElementInst>(U);
601-
if (!EEI)
601+
if (!EEI || EEI->getType()->getPrimitiveSizeInBits() != EltTy->getPrimitiveSizeInBits())
602602
return false;
603603
EEIs.push_back(EEI);
604604
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 -igc-legalization -verify -S %s -o - | FileCheck %s
10+
11+
; ------------------------------------------------
12+
; Legalization: bitcast illegal types after GVN
13+
; ------------------------------------------------
14+
15+
define i16 @test_bitcast_pattern2(<4 x float> %src1, i32 %src2) {
16+
; CHECK-LABEL: define i16 @test_bitcast_pattern2(
17+
; CHECK-SAME: <4 x float> [[SRC1:%.*]], i32 [[SRC2:%.*]]) {
18+
; CHECK: [[TMP2:%.*]] = bitcast <4 x float> [[SRC1]] to <2 x i64>
19+
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i64> [[TMP2]], i32 0
20+
; CHECK-NEXT: [[TMP4:%.*]] = bitcast i64 [[TMP3]] to <4 x i16>
21+
; CHECK-NEXT: [[TMP5:%.*]] = extractelement <4 x i16> [[TMP4]], i32 1
22+
; CHECK-NEXT: ret i16 [[TMP5]]
23+
;
24+
%1 = bitcast <4 x float> %src1 to i128
25+
%2 = trunc i128 %1 to i64
26+
%3 = bitcast i64 %2 to <4 x i16>
27+
%4 = extractelement <4 x i16> %3, i32 1
28+
ret i16 %4
29+
}
30+
31+
!igc.functions = !{!0}
32+
33+
!0 = !{i16 (<4 x float>, i32)* @test_bitcast_pattern2, !1}
34+
!1 = !{}

0 commit comments

Comments
 (0)