Skip to content

Commit 0bf9d04

Browse files
agrabezhpszymich
authored andcommitted
Fix legalizePhiInstruction for vector types
Build extra bitcast before zext instruction for correct vector types legalization (cherry picked from commit 0567062)
1 parent 86a4534 commit 0bf9d04

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

IGC/Compiler/Legalizer/PeepholeTypeLegalizer.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,32 +192,34 @@ void PeepholeTypeLegalizer::legalizePhiInstruction(Instruction &I) {
192192
if (!I.getType()->isIntOrIntVectorTy() || isLegalInteger(srcWidth) || srcWidth == 1) // nothing to legalize
193193
return;
194194

195+
unsigned numElements =
196+
I.getType()->isVectorTy() ? (unsigned)cast<IGCLLVM::FixedVectorType>(I.getType())->getNumElements() : 1;
195197
unsigned quotient = 0, promoteToInt = 0;
196-
promoteInt(srcWidth, quotient, promoteToInt, DL->getLargestLegalIntTypeSizeInBits());
198+
promoteInt(srcWidth * numElements, quotient, promoteToInt, DL->getLargestLegalIntTypeSizeInBits());
197199

198200
PHINode *oldPhi = cast<PHINode>(&I);
199201
Value *result = nullptr;
200202

201203
if (quotient > 1) {
202-
unsigned numElements =
203-
I.getType()->isVectorTy() ? (unsigned)cast<IGCLLVM::FixedVectorType>(I.getType())->getNumElements() : 1;
204-
Type *newVecType =
205-
IGCLLVM::FixedVectorType::get(Type::getIntNTy(I.getContext(), promoteToInt), quotient * numElements);
204+
Type *newVecType = IGCLLVM::FixedVectorType::get(Type::getIntNTy(I.getContext(), promoteToInt), quotient);
206205
Type *newLargeIntType = Type::getIntNTy(I.getContext(), promoteToInt * quotient);
206+
Type *newOrigIntType = Type::getIntNTy(I.getContext(), srcWidth * numElements);
207207

208208
PHINode *newPhi = m_builder->CreatePHI(newVecType, oldPhi->getNumIncomingValues());
209209
for (unsigned i = 0; i < oldPhi->getNumIncomingValues(); i++) {
210210
Value *incomingValue = oldPhi->getIncomingValue(i);
211211

212212
m_builder->SetInsertPoint(oldPhi->getIncomingBlock(i)->getTerminator());
213-
Value *newLargeIntValue = m_builder->CreateZExt(incomingValue, newLargeIntType);
213+
Value *newOrigValue = m_builder->CreateBitCast(incomingValue, newOrigIntType);
214+
Value *newLargeIntValue = m_builder->CreateZExt(newOrigValue, newLargeIntType);
214215
Value *newVecValue = m_builder->CreateBitCast(newLargeIntValue, newVecType);
215216
newPhi->addIncoming(newVecValue, oldPhi->getIncomingBlock(i));
216217
}
217218
// Cast back to original type
218219
m_builder->SetInsertPoint(newPhi->getParent()->getFirstNonPHI());
219220
Value *NewLargeIntPhi = m_builder->CreateBitCast(newPhi, newLargeIntType);
220-
result = m_builder->CreateTrunc(NewLargeIntPhi, oldPhi->getType());
221+
Value *NewOrigIntPhi = m_builder->CreateTrunc(NewLargeIntPhi, newOrigIntType);
222+
result = m_builder->CreateBitCast(NewOrigIntPhi, oldPhi->getType());
221223
} else {
222224
// quotient == 1 (integer promotion)
223225
Type *newType = Type::getIntNTy(I.getContext(), promoteToInt);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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-int-type-legalizer -S < %s | FileCheck %s
11+
12+
; Test checks phi types legalization
13+
14+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32-p2490368:32:32:32"
15+
16+
define void @test_2xi256(i1 %flag) {
17+
; CHECK-LABEL: define void @test_2xi256
18+
; CHECK: = phi <16 x i32> [ zeroinitializer, %bb1 ], [ zeroinitializer, %bb2 ]
19+
bb:
20+
br i1 %flag, label %bb1, label %bb2
21+
22+
bb1: ; preds = %bb
23+
br label %bb3
24+
25+
bb2: ; preds = %bb
26+
br label %bb3
27+
28+
bb3: ; preds = %bb2, %bb1
29+
%tmp = phi <2 x i256> [ zeroinitializer, %bb1 ], [ zeroinitializer, %bb2 ]
30+
br label %bb4
31+
32+
bb4: ; preds = %bb3
33+
ret void
34+
}
35+
36+
define void @test_2xi80(i1 %flag) {
37+
; CHECK-LABEL: define void @test_2xi80
38+
; CHECK: = phi <5 x i32> [ zeroinitializer, %bb1 ], [ zeroinitializer, %bb2 ]
39+
bb:
40+
br i1 %flag, label %bb1, label %bb2
41+
42+
bb1: ; preds = %bb
43+
br label %bb3
44+
45+
bb2: ; preds = %bb
46+
br label %bb3
47+
48+
bb3: ; preds = %bb2, %bb1
49+
%tmp = phi <2 x i80> [ zeroinitializer, %bb1 ], [ zeroinitializer, %bb2 ]
50+
br label %bb4
51+
52+
bb4: ; preds = %bb3
53+
ret void
54+
}
55+
56+
!igc.functions = !{}

0 commit comments

Comments
 (0)