Skip to content

Commit fbb8613

Browse files
yugrlanza
authored andcommitted
[CIR][Codegen] Support codegen for FP truncations. (#291)
1 parent dbdf4bb commit fbb8613

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
557557
llvm_unreachable("negation for the given type is NYI");
558558
}
559559

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

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,11 +1670,8 @@ mlir::Value ScalarExprEmitter::buildScalarCast(
16701670
return Builder.create<mlir::cir::CastOp>(
16711671
Src.getLoc(), DstTy, mlir::cir::CastKind::float_to_int, Src);
16721672
} else if (DstElementTy.isa<mlir::FloatType>()) {
1673-
auto FloatDstTy = DstTy.cast<mlir::FloatType>();
1674-
auto FloatSrcTy = SrcTy.cast<mlir::FloatType>();
1675-
if (FloatDstTy.getWidth() < FloatSrcTy.getWidth())
1676-
llvm_unreachable("NYI: narrowing floating-point cast");
1677-
return Builder.createFPExt(Src, DstTy);
1673+
// TODO: split this to createFPExt/createFPTrunc
1674+
return Builder.createFloatingCast(Src, DstTy);
16781675
} else {
16791676
llvm_unreachable("Unexpected destination type for scalar cast");
16801677
}

clang/test/CIR/CodeGen/cast.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ unsigned char cxxstaticcast_0(unsigned int x) {
1717
// CHECK: }
1818

1919

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

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

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

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

76+
float dptofp = (float)x5;
77+
// CHECK: %{{.+}} = cir.cast(floating, %{{[0-9]+}} : f64), f32
78+
7679
return 0;
7780
}
7881

clang/test/CIR/Lowering/cast.cir

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
!u64i = !cir.int<u, 64>
1111

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

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

0 commit comments

Comments
 (0)