[clang] [CIR] Upstream Cast kinds for ComplexType (PR #149717)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 21 00:43:47 PDT 2025
================
@@ -24,11 +25,118 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
void runOnOperation() override;
void runOnOp(mlir::Operation *op);
+ void lowerCastOp(cir::CastOp op);
void lowerUnaryOp(cir::UnaryOp op);
};
} // namespace
+static mlir::Value lowerScalarToComplexCast(MLIRContext &ctx, CastOp op) {
+ CIRBaseBuilderTy builder(ctx);
+ builder.setInsertionPoint(op);
+
+ mlir::Value src = op.getSrc();
+ mlir::Value imag = builder.getNullValue(src.getType(), op.getLoc());
+ return builder.createComplexCreate(op.getLoc(), src, imag);
+}
+
+static mlir::Value lowerComplexToScalarCast(MLIRContext &ctx, CastOp op) {
+ CIRBaseBuilderTy builder(ctx);
+ builder.setInsertionPoint(op);
+
+ mlir::Value src = op.getSrc();
+ if (!mlir::isa<cir::BoolType>(op.getType()))
+ return builder.createComplexReal(op.getLoc(), src);
+
+ // Complex cast to bool: (bool)(a+bi) => (bool)a || (bool)b
+ mlir::Value srcReal = builder.createComplexReal(op.getLoc(), src);
+ mlir::Value srcImag = builder.createComplexImag(op.getLoc(), src);
+
+ cir::CastKind elemToBoolKind;
+ if (op.getKind() == cir::CastKind::float_complex_to_bool)
+ elemToBoolKind = cir::CastKind::float_to_bool;
+ else if (op.getKind() == cir::CastKind::int_complex_to_bool)
+ elemToBoolKind = cir::CastKind::int_to_bool;
+ else
+ llvm_unreachable("invalid complex to bool cast kind");
----------------
xlauko wrote:
Same here, this does not even cover everything as switch where `lowerComplexToScalarCast` is used, i.e.:
`float_complex_to_real` and `int_complex_to_real`.
https://github.com/llvm/llvm-project/pull/149717
More information about the cfe-commits
mailing list