Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:FoldBitCast. #117163

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
}

// Some of what follows may extend to cover scalable vectors but the current
// implementation is fixed length specific.
if (!isa<FixedVectorType>(C->getType()))
return ConstantExpr::getBitCast(C, DestTy);

// If this is a bitcast from constant vector -> vector, fold it.
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C) &&
!isa<ConstantInt>(C) && !isa<ConstantFP>(C))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refers to line 206: Do we need to add ConstantInt and ConstantFP checks here too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say no because it should be safe to not perform anymore transformations. However, if I'm reading it correctly I think that block of code is just broken upstream?

For the case where constant folding does not happen it is effectively returning ConstantExpr::getBitCast(C, SrcAsIntTy) and by this point we already know SrcAsIntTy != DestVTy?

return ConstantExpr::getBitCast(C, DestTy);

// If the element types match, IR can fold it.
Expand Down Expand Up @@ -194,10 +200,9 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
// Ask IR to do the conversion now that #elts line up.
C = ConstantExpr::getBitCast(C, SrcIVTy);
// If IR wasn't able to fold it, bail out.
if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
!isa<ConstantDataVector>(C))
return C;
assert((isa<ConstantVector>(C) || // FIXME: Remove ConstantVector.
isa<ConstantDataVector>(C) || isa<ConstantInt>(C)) &&
"Constant folding cannot fail for plain fp->int bitcast!");
}

// Now we know that the input and output vectors are both integer vectors
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Transforms/InstCombine/cast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
; Tests to make sure elimination of casts is working correctly
; RUN: opt < %s -passes=instcombine -S -data-layout="E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" | FileCheck %s --check-prefixes=ALL,BE
; RUN: opt < %s -passes=instcombine -S -data-layout="e-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" | FileCheck %s --check-prefixes=ALL,LE
; RUN: opt < %s -passes=instcombine -S -data-layout="E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ALL,BE
; RUN: opt < %s -passes=instcombine -S -data-layout="e-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ALL,LE

declare void @use_i32(i32)
declare void @use_v2i32(<2 x i32>)
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,11 @@ define <1 x i32> @bitcast_constexpr_scalar_fp_to_vector_int() {
%res = bitcast float 1.0 to <1 x i32>
ret <1 x i32> %res
}

define <2 x i64> @bitcast_constexpr_4f32_2i64_1111() {
; CHECK-LABEL: @bitcast_constexpr_4f32_2i64_1111(
; CHECK-NEXT: ret <2 x i64> splat (i64 4575657222473777152)
;
%res = bitcast <4 x float> splat (float 1.0) to <2 x i64>
ret <2 x i64> %res
}
Loading