Skip to content
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
4 changes: 3 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
llvm_unreachable("negation for the given type is NYI");
}

mlir::Value createFPExt(mlir::Value v, mlir::Type destType) {
// TODO: split this to createFPExt/createFPTrunc when we have dedicated cast
// operations.
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType) {
if (getIsFPConstrained())
llvm_unreachable("constrainedfp NYI");

Expand Down
5 changes: 2 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1670,11 +1670,10 @@ mlir::Value ScalarExprEmitter::buildScalarCast(
return Builder.create<mlir::cir::CastOp>(
Src.getLoc(), DstTy, mlir::cir::CastKind::float_to_int, Src);
} else if (DstElementTy.isa<mlir::FloatType>()) {
// TODO: split this to createFPExt/createFPTrunc
auto FloatDstTy = DstTy.cast<mlir::FloatType>();
auto FloatSrcTy = SrcTy.cast<mlir::FloatType>();
if (FloatDstTy.getWidth() < FloatSrcTy.getWidth())
llvm_unreachable("NYI: narrowing floating-point cast");
return Builder.createFPExt(Src, DstTy);
return Builder.createFloatingCast(Src, DstTy);
} else {
llvm_unreachable("Unexpected destination type for scalar cast");
}
Expand Down
9 changes: 6 additions & 3 deletions clang/test/CIR/CodeGen/cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ unsigned char cxxstaticcast_0(unsigned int x) {
// CHECK: }


int cStyleCasts_0(unsigned x1, int x2, float x3, short x4) {
int cStyleCasts_0(unsigned x1, int x2, float x3, short x4, double x5) {
// CHECK: cir.func @_{{.*}}cStyleCasts_0{{.*}}

char a = (char)x1; // truncate
Expand Down Expand Up @@ -68,11 +68,14 @@ int cStyleCasts_0(unsigned x1, int x2, float x3, short x4) {
unsigned fptoui = (unsigned)x3; // Floating point to unsigned integer
// CHECK: %{{.+}} = cir.cast(float_to_int, %{{[0-9]+}} : f32), !u32i

bool x5 = (bool)x1; // No checking, because this isn't a cast.
bool ib = (bool)x1; // No checking, because this isn't a cast.

int bi = (int)x5; // bool to int
int bi = (int)ib; // bool to int
// CHECK: %{{[0-9]+}} = cir.cast(bool_to_int, %{{[0-9]+}} : !cir.bool), !s32i

float dptofp = (float)x5;
// CHECK: %{{.+}} = cir.cast(floating, %{{[0-9]+}} : f64), f32

return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion clang/test/CIR/Lowering/cast.cir
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
!u64i = !cir.int<u, 64>

module {
cir.func @cStyleCasts(%arg0: !u32i, %arg1: !s32i, %arg2: f32) -> !s32i {
cir.func @cStyleCasts(%arg0: !u32i, %arg1: !s32i, %arg2: f32, %arg3: f64) -> !s32i {
// CHECK: llvm.func @cStyleCasts
%0 = cir.alloca !u32i, cir.ptr <!u32i>, ["x1", init] {alignment = 4 : i64}
%1 = cir.alloca !s32i, cir.ptr <!s32i>, ["x2", init] {alignment = 4 : i64}
Expand Down Expand Up @@ -74,6 +74,8 @@ module {
%28 = cir.cast(float_to_int, %arg2 : f32), !u32i
// CHECK: %{{.+}} = llvm.fptoui %{{.+}} : f32 to i32
%18 = cir.const(#cir.int<0> : !s32i) : !s32i
// CHECK: %{{.+}} = llvm.fptrunc %{{.+}} : f64 to f32
%34 = cir.cast(floating, %arg3 : f64), f32

cir.store %18, %2 : !s32i, cir.ptr <!s32i>
%19 = cir.load %2 : cir.ptr <!s32i>, !s32i
Expand Down