Skip to content

Commit 006dbab

Browse files
vmustyaigcbot
authored andcommitted
Backport LLVM-15 fix for instcombine trunc(ext) case
llvm/llvm-project@5724231
1 parent 0b10cfb commit 006dbab

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2023 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
From 6d6c78dd486c0ed1995bab48ac8cd7ecf1d926b8 Mon Sep 17 00:00:00 2001
10+
From: Benjamin Kramer <[email protected]>
11+
Date: Mon, 13 Jun 2022 14:31:43 +0200
12+
Subject: [InstCombine] Only fold trunc(ext) pairs to bitcast if the source and
13+
destination types are the same
14+
15+
This used to be always the case, but the addition of bfloat to the type
16+
matrix makes this invalid.
17+
---
18+
llvm/lib/IR/Instructions.cpp | 10 ++++++----
19+
llvm/test/Transforms/InstCombine/fpextend.ll | 11 +++++++++++
20+
2 files changed, 17 insertions(+), 4 deletions(-)
21+
22+
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
23+
index 7798af3b19b9..2f68807bb8bc 100644
24+
--- a/llvm/lib/IR/Instructions.cpp
25+
+++ b/llvm/lib/IR/Instructions.cpp
26+
@@ -3060,16 +3060,18 @@ unsigned CastInst::isEliminableCastPair(
27+
return 0;
28+
}
29+
case 8: {
30+
- // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
31+
+ // ext, trunc -> bitcast, if the SrcTy and DstTy are the same
32+
// ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
33+
// ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
34+
unsigned SrcSize = SrcTy->getScalarSizeInBits();
35+
unsigned DstSize = DstTy->getScalarSizeInBits();
36+
- if (SrcSize == DstSize)
37+
+ if (SrcTy == DstTy)
38+
return Instruction::BitCast;
39+
- else if (SrcSize < DstSize)
40+
+ if (SrcSize < DstSize)
41+
return firstOp;
42+
- return secondOp;
43+
+ if (SrcSize > DstSize)
44+
+ return secondOp;
45+
+ return 0;
46+
}
47+
case 9:
48+
// zext, sext -> zext, because sext can't sign extend after zext
49+
diff --git a/llvm/test/Transforms/InstCombine/fpextend.ll b/llvm/test/Transforms/InstCombine/fpextend.ll
50+
index 9fe85e983fb7..6b8fe928ff8a 100644
51+
--- a/llvm/test/Transforms/InstCombine/fpextend.ll
52+
+++ b/llvm/test/Transforms/InstCombine/fpextend.ll
53+
@@ -429,3 +429,14 @@ define double @FtoItoFtoF_f32_su32_f32_f64(float %f) {
54+
%r = fpext float %x to double
55+
ret double %r
56+
}
57+
+
58+
+define half @bf16_to_f32_to_f16(bfloat %a) nounwind {
59+
+; CHECK-LABEL: @bf16_to_f32_to_f16(
60+
+; CHECK-NEXT: [[Y:%.*]] = fpext bfloat [[A:%.*]] to float
61+
+; CHECK-NEXT: [[Z:%.*]] = fptrunc float [[Y]] to half
62+
+; CHECK-NEXT: ret half [[Z]]
63+
+;
64+
+ %y = fpext bfloat %a to float
65+
+ %z = fptrunc float %y to half
66+
+ ret half %z
67+
+}
68+
--
69+
2.34.1
70+

0 commit comments

Comments
 (0)