Skip to content

[CIR][LLVMLowering] Upstream Bitcast lowering #140774

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

Merged
merged 1 commit into from
May 21, 2025
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
12 changes: 10 additions & 2 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::LLVM::BitcastOp>(castOp, llvmDstTy,
llvmSrcVal);
return mlir::success();
}
case cir::CastKind::ptr_to_bool: {
mlir::Value llvmSrcVal = adaptor.getOperands().front();
mlir::Value zeroPtr = rewriter.create<mlir::LLVM::ZeroOp>(
Expand Down
17 changes: 15 additions & 2 deletions clang/test/CIR/CodeGen/cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.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>