[clang] [CIR][LLVMLowering] Upstream Bitcast lowering (PR #140774)

2025-05-21 Thread Amr Hesham via cfe-commits

https://github.com/AmrDeveloper closed 
https://github.com/llvm/llvm-project/pull/140774
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CIR][LLVMLowering] Upstream Bitcast lowering (PR #140774)

2025-05-20 Thread Andy Kaylor via cfe-commits

https://github.com/andykaylor approved this pull request.

lgtm

https://github.com/llvm/llvm-project/pull/140774
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CIR][LLVMLowering] Upstream Bitcast lowering (PR #140774)

2025-05-20 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clangir

Author: Amr Hesham (AmrDeveloper)


Changes

This change adds support for lowering BitCastOp




---
Full diff: https://github.com/llvm/llvm-project/pull/140774.diff


2 Files Affected:

- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+10-2) 
- (modified) clang/test/CIR/CodeGen/cast.cpp (+15-2) 


``diff
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp 
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 365569ce1f48a..3a94806c3af7c 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -530,10 +530,18 @@ mlir::LogicalResult 
CIRToLLVMCastOpLowering::matchAndRewrite(
 llvmSrcVal);
 return mlir::success();
   }
-  case cir::CastKind::bitcast:
+  case cir::CastKind::bitcast: {
+mlir::Type dstTy = castOp.getType();
+mlir::Type llvmDstTy = getTypeConverter()->convertType(dstTy);
+
 assert(!MissingFeatures::cxxABI());
 assert(!MissingFeatures::dataMemberType());
-break;
+
+mlir::Value llvmSrcVal = adaptor.getOperands().front();
+rewriter.replaceOpWithNewOp(castOp, llvmDstTy,
+   llvmSrcVal);
+return mlir::success();
+  }
   case cir::CastKind::ptr_to_bool: {
 mlir::Value llvmSrcVal = adaptor.getOperands().front();
 mlir::Value zeroPtr = rewriter.create(
diff --git a/clang/test/CIR/CodeGen/cast.cpp b/clang/test/CIR/CodeGen/cast.cpp
index dd3ea8f8f02b2..9da72cd35d963 100644
--- a/clang/test/CIR/CodeGen/cast.cpp
+++ b/clang/test/CIR/CodeGen/cast.cpp
@@ -25,7 +25,6 @@ unsigned char cxxstaticcast_0(unsigned int x) {
 // LLVM: %[[R:[0-9]+]] = load i8, ptr %[[RV]], align 1
 // LLVM: ret i8 %[[R]]
 
-
 int cStyleCasts_0(unsigned x1, int x2, float x3, short x4, double x5) {
 // CIR: cir.func @_Z13cStyleCasts_0jifsd
 // LLVM: define i32 @_Z13cStyleCasts_0jifsd
@@ -103,10 +102,24 @@ void should_not_cast() {
 
   bool x2;
   bool ib = (bool)x2; // identity
-  
+
   (void) ib; // void cast
 }
 
 // CIR: cir.func @_Z15should_not_castv
 // CIR-NOT:   cir.cast
 // CIR: cir.return
+
+typedef int vi4 __attribute__((vector_size(16)));
+typedef double vd2 __attribute__((vector_size(16)));
+
+void bitcast() {
+  vd2 a = {};
+  vi4 b = (vi4)a;
+}
+
+// CIR: %[[D_VEC:.*]] = cir.load {{.*}} : !cir.ptr>, !cir.vector<2 x !cir.double>
+// CIR: %[[I_VEC:.*]] = cir.cast(bitcast, %[[D_VEC]] : !cir.vector<2 x 
!cir.double>), !cir.vector<4 x !s32i>
+
+// LLVM: %[[D_VEC:.*]] = load <2 x double>, ptr {{.*}}, align 16
+// LLVM: %[[I_VEC:.*]] = bitcast <2 x double> %[[D_VEC]] to <4 x i32>

``




https://github.com/llvm/llvm-project/pull/140774
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CIR][LLVMLowering] Upstream Bitcast lowering (PR #140774)

2025-05-20 Thread Amr Hesham via cfe-commits

https://github.com/AmrDeveloper edited 
https://github.com/llvm/llvm-project/pull/140774
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits