Skip to content

Commit c700d6b

Browse files
againulligcbot
authored andcommitted
[IGC Core] Fix ICmp instruction promotion during type legalization
Fix mask creation when promoting icmp instruction during type legalization
1 parent 69c79ca commit c700d6b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

IGC/Compiler/Legalizer/InstPromoter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ bool InstPromoter::visitICmpInst(ICmpInst &I) {
120120
unsigned finalWidth = LHS->getType()->getIntegerBitWidth();
121121

122122
if (!isSigned) {
123-
LHS = IRB->CreateAnd(LHS, (1 << initialWidth) - 1);
124-
RHS = IRB->CreateAnd(RHS, (1 << initialWidth) - 1);
123+
APInt M = APInt::getLowBitsSet(finalWidth, initialWidth);
124+
auto *MaskC = ConstantInt::get(LHS->getType(), M);
125+
LHS = IRB->CreateAnd(LHS, MaskC);
126+
RHS = IRB->CreateAnd(RHS, MaskC);
125127
} else {
126128
IGC_ASSERT(finalWidth >= initialWidth);
127129

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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-type-legalizer -S < %s | FileCheck %s
10+
; ------------------------------------------------
11+
; Legalization: icmp (i40 -> i64 promotion mask correctness)
12+
; ------------------------------------------------
13+
;
14+
; Ensure that unsigned icmp on an illegal integer width (i40) is promoted
15+
; with a correct low-40-bit mask (0xFFFFFFFFFF = 1099511627775).
16+
;
17+
; CHECK-LABEL: define i1 @test_icmp_i40
18+
; CHECK: %[[TRUNC:.*]] = and i64 %[[X:.*]], 1099511627775
19+
; CHECK: %[[MASKED:.*]] = and i64 %[[TRUNC]], 1099511627775
20+
; CHECK: icmp ult i64 %[[MASKED]], 4294967296
21+
; CHECK: ret i1
22+
23+
define i1 @test_icmp_i40(i64 %x) {
24+
; Truncate to illegal width i40, then compare with 1<<32
25+
%t = trunc i64 %x to i40
26+
%cmp = icmp ult i40 %t, 4294967296
27+
ret i1 %cmp
28+
}

0 commit comments

Comments
 (0)