Skip to content

Commit ed8790f

Browse files
michalpaszkowskiigcbot
authored andcommitted
Emit a bitcast after select in SimplifyConstant
In typed pointer mode, LLVM inserts pointer bitcasts between GEPs and loads of different types. This is not the case in opaque pointer mode and causes an issue in SimplifyConstant when the GEP + load are replaced with select of values. This change makes the pass emit a bitcast for bitwise compatible types.
1 parent 32de786 commit ed8790f

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

IGC/Compiler/CISACodeGen/SimplifyConstant.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*========================== begin_copyright_notice ============================
22
3-
Copyright (C) 2017-2021 Intel Corporation
3+
Copyright (C) 2017-2025 Intel Corporation
44
55
SPDX-License-Identifier: MIT
66
@@ -190,6 +190,12 @@ void ConstantLoader::simplify() {
190190
Value* Cmp = Builder.CreateTrunc(Index, Builder.getInt1Ty());
191191
Val = Builder.CreateSelect(Cmp, V1, V0);
192192
}
193+
194+
Type* SrcTy = Val->getType();
195+
Type* DstTy = LI->getType();
196+
if (SrcTy != DstTy && SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits())
197+
Val = Builder.CreateBitCast(Val, DstTy);
198+
193199
LI->replaceAllUsesWith(Val);
194200
LI->eraseFromParent();
195201
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2022 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; RUN: igc_opt --typed-pointers -enable-debugify -SimplifyConstant -S < %s 2>&1 | FileCheck %s
10+
; ------------------------------------------------
11+
; SimplifyConstant
12+
; ------------------------------------------------
13+
14+
; Debug-info related check
15+
;
16+
; CHECK-COUNT-1: WARNING
17+
; CHECK-SAME: Missing line 1
18+
; CHECK-NOT: WARNING
19+
; CHECK: CheckModuleDebugify: PASS
20+
21+
@a = private addrspace(2) constant [4 x i32] [i32 13, i32 42, i32 13, i32 42], align 1
22+
23+
define spir_kernel void @test_simpleconst(i32 %a, i32* %b) {
24+
; CHECK-LABEL: @test_simpleconst(
25+
; CHECK: entry:
26+
; CHECK: [[TMP0:%.*]] = trunc i32 [[A:%.*]] to i1
27+
; CHECK: [[TMP1:%.*]] = select i1 [[TMP0]], i32 42, i32 13
28+
; CHECK: store i32 [[TMP1]], i32* [[B:%.*]], align 4
29+
; CHECK: ret void
30+
;
31+
entry:
32+
%0 = getelementptr inbounds [4 x i32], [4 x i32] addrspace(2)* @a, i32 0, i32 %a
33+
%1 = load i32, i32 addrspace(2)* %0, align 4
34+
store i32 %1, i32* %b, align 4
35+
ret void
36+
}

IGC/Compiler/tests/SimplifyConstant/basic.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
;
77
;============================ end_copyright_notice =============================
88
;
9-
; RUN: igc_opt --typed-pointers -enable-debugify -SimplifyConstant -S < %s 2>&1 | FileCheck %s
9+
; REQUIRES: llvm-14-plus
10+
; RUN: igc_opt --opaque-pointers -enable-debugify -SimplifyConstant -S < %s 2>&1 | FileCheck %s
1011
; ------------------------------------------------
1112
; SimplifyConstant
1213
; ------------------------------------------------
@@ -25,7 +26,7 @@ define spir_kernel void @test_simpleconst(i32 %a, i32* %b) {
2526
; CHECK: entry:
2627
; CHECK: [[TMP0:%.*]] = trunc i32 [[A:%.*]] to i1
2728
; CHECK: [[TMP1:%.*]] = select i1 [[TMP0]], i32 42, i32 13
28-
; CHECK: store i32 [[TMP1]], i32* [[B:%.*]], align 4
29+
; CHECK: store i32 [[TMP1]], ptr [[B:%.*]], align 4
2930
; CHECK: ret void
3031
;
3132
entry:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 -SimplifyConstant -S < %s 2>&1 | FileCheck %s
11+
12+
; This test checks that SimplifyConstant pass emits a bitcast following
13+
; simplification.
14+
;
15+
; Before the transition to opaque pointers, the pass accepted the following pattern:
16+
; @g = dso_local unnamed_addr addrspace(2) constant [2 x i32] [i32 1065353216, i32 -1082130432], align 4
17+
; %gep = getelementptr inbounds [2 x i32], [2 x i32] addrspace(2)* @g, i64 0, i64 %a
18+
; %bc = bitcast i32 addrspace(2)* %30 to float addrspace(2)*
19+
; %load = load float, float addrspace(2)* %bc, align 4
20+
;
21+
; In opaque pointer mode, the pattern changes to:
22+
; @g = dso_local unnamed_addr addrspace(2) constant [2 x i32] [i32 1065353216, i32 -1082130432], align 4
23+
; %gep = getelementptr inbounds [2 x i32], ptr addrspace(2) @g, i64 0, i64 %a
24+
; %load = load float, ptr addrspace(2) %gep, align 4
25+
;
26+
; After simplification, the load expects a different primitive type and given
27+
; that the types are bitwise compatible, the pass can emit a bitcast.
28+
29+
; CHECK: [[SELECT:%.*]] = select i1 {{%.*}}, i32 -1082130432, i32 1065353216
30+
; CHECK: [[BC:%.*]] = bitcast i32 [[SELECT]] to float
31+
; CHECK: store float [[BC]], ptr {{%.*}}, align 4
32+
33+
@g = dso_local unnamed_addr addrspace(2) constant [2 x i32] [i32 1065353216, i32 -1082130432], align 4
34+
35+
define spir_kernel void @test_bitcast_after_simplification(i64 %a, ptr %b) {
36+
%gep = getelementptr inbounds [2 x i32], ptr addrspace(2) @g, i64 0, i64 %a
37+
%load = load float, ptr addrspace(2) %gep, align 4
38+
store float %load, ptr %b, align 4
39+
ret void
40+
}

0 commit comments

Comments
 (0)