[llvm] 5724231 - [InstCombine] Only fold trunc(ext) pairs to bitcast if the source and destination types are the same

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 13 05:34:32 PDT 2022


Author: Benjamin Kramer
Date: 2022-06-13T14:34:18+02:00
New Revision: 5724231af279d2acf7a6f7b44fe9089456c37777

URL: https://github.com/llvm/llvm-project/commit/5724231af279d2acf7a6f7b44fe9089456c37777
DIFF: https://github.com/llvm/llvm-project/commit/5724231af279d2acf7a6f7b44fe9089456c37777.diff

LOG: [InstCombine] Only fold trunc(ext) pairs to bitcast if the source and destination types are the same

This used to be always the case, but the addition of bfloat to the type
matrix makes this invalid.

Added: 
    

Modified: 
    llvm/lib/IR/Instructions.cpp
    llvm/test/Transforms/InstCombine/fpextend.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 7615a4dcc498..1b546ce3a3a0 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -3088,16 +3088,18 @@ unsigned CastInst::isEliminableCastPair(
       return 0;
     }
     case 8: {
-      // ext, trunc -> bitcast,    if the SrcTy and DstTy are same size
+      // ext, trunc -> bitcast,    if the SrcTy and DstTy are the same
       // ext, trunc -> ext,        if sizeof(SrcTy) < sizeof(DstTy)
       // ext, trunc -> trunc,      if sizeof(SrcTy) > sizeof(DstTy)
       unsigned SrcSize = SrcTy->getScalarSizeInBits();
       unsigned DstSize = DstTy->getScalarSizeInBits();
-      if (SrcSize == DstSize)
+      if (SrcTy == DstTy)
         return Instruction::BitCast;
-      else if (SrcSize < DstSize)
+      if (SrcSize < DstSize)
         return firstOp;
-      return secondOp;
+      if (SrcSize > DstSize)
+        return secondOp;
+      return 0;
     }
     case 9:
       // zext, sext -> zext, because sext can't sign extend after zext

diff  --git a/llvm/test/Transforms/InstCombine/fpextend.ll b/llvm/test/Transforms/InstCombine/fpextend.ll
index 13784c69c36b..c991f5d8c957 100644
--- a/llvm/test/Transforms/InstCombine/fpextend.ll
+++ b/llvm/test/Transforms/InstCombine/fpextend.ll
@@ -429,3 +429,14 @@ define double @FtoItoFtoF_f32_su32_f32_f64(float %f) {
   %r = fpext float %x to double
   ret double %r
 }
+
+define half @bf16_to_f32_to_f16(bfloat %a) nounwind {
+; CHECK-LABEL: @bf16_to_f32_to_f16(
+; CHECK-NEXT:    [[Y:%.*]] = fpext bfloat [[A:%.*]] to float
+; CHECK-NEXT:    [[Z:%.*]] = fptrunc float [[Y]] to half
+; CHECK-NEXT:    ret half [[Z]]
+;
+  %y = fpext bfloat %a to float
+  %z = fptrunc float %y to half
+  ret half %z
+}


        


More information about the llvm-commits mailing list