Debian Codegraph @

external/llvm/releases/17.0.0/patches_external/InstCombine-Only-fold-bitcast-fptrunc-if-destination.patch

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2024 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 From 58b5b7d4ed6204f61feeda68c7c1abe24bc143b1 Mon Sep 17 00:00:00 2001
10 From: Victor Mustya <[email protected]>
11 Date: Tue, 16 Jan 2024 14:13:05 -0800
12 Subject: [InstCombine] Only fold bitcast(fptrunc) if destination type matches
13 fptrunc result type. (#77046)
14
15 It's not enough to just make sure destination type is floating point,
16 because the following chain may be incorrectly optimized:
17 ```LLVM
18 %trunc = fptrunc float %src to bfloat
19 %cast = bitcast bfloat %trunc to half
20 ```
21 Before the fix, the instruction sequence mentioned above used to be
22 translated into single fptrunc instruction as follows:
23 ```LLVM
24 %trunc = fptrunc float %src to half
25 ```
26
27 Such transformation was semantically incorrect.
28 ---
29 llvm/lib/IR/Instructions.cpp | 4 ++--
30 llvm/test/Transforms/InstCombine/fptrunc.ll | 13 +++++++++++++
31 2 files changed, 15 insertions(+), 2 deletions(-)
32
33 diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
34 index 7c343a0ff..932fc66a8 100644
35 --- a/llvm/lib/IR/Instructions.cpp
36 +++ b/llvm/lib/IR/Instructions.cpp
37 @@ -3218,8 +3218,8 @@ unsigned CastInst::isEliminableCastPair(
38 return 0;
39 case 4:
40 // No-op cast in second op implies firstOp as long as the DestTy
41 - // is floating point.
42 - if (DstTy->isFloatingPointTy())
43 + // matches MidTy.
44 + if (DstTy == MidTy)
45 return firstOp;
46 return 0;
47 case 5:
48 diff --git a/llvm/test/Transforms/InstCombine/fptrunc.ll b/llvm/test/Transforms/InstCombine/fptrunc.ll
49 index d3e153f12..c78df0b83 100644
50 --- a/llvm/test/Transforms/InstCombine/fptrunc.ll
51 +++ b/llvm/test/Transforms/InstCombine/fptrunc.ll
52 @@ -190,3 +190,16 @@ define half @ItoFtoF_u25_f32_f16(i25 %i) {
53 %r = fptrunc float %x to half
54 ret half %r
55 }
56 +
57 +; Negative test - bitcast bfloat to half is not optimized
58 +
59 +define half @fptrunc_to_bfloat_bitcast_to_half(float %src) {
60 +; CHECK-LABEL: @fptrunc_to_bfloat_bitcast_to_half(
61 +; CHECK-NEXT: [[TRUNC:%.*]] = fptrunc float [[SRC:%.*]] to bfloat
62 +; CHECK-NEXT: [[CAST:%.*]] = bitcast bfloat [[TRUNC]] to half
63 +; CHECK-NEXT: ret half [[CAST]]
64 +;
65 + %trunc = fptrunc float %src to bfloat
66 + %cast = bitcast bfloat %trunc to half
67 + ret half %cast
68 +}
69 --
70 2.34.1
71