-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[CIR] Upstream Cast kinds for ComplexType #149717
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
Changes from 1 commit
8a3e3e8
13cc762
7c43c07
1c6ce40
7611b81
eb5d912
7bb89b6
bf5393d
ab1687c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -31,7 +31,8 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> { | |||||
|
|
||||||
| } // namespace | ||||||
|
|
||||||
| static mlir::Value lowerScalarToComplexCast(MLIRContext &ctx, CastOp op) { | ||||||
| static mlir::Value lowerScalarToComplexCast(mlir::MLIRContext &ctx, | ||||||
| cir::CastOp op) { | ||||||
| CIRBaseBuilderTy builder(ctx); | ||||||
| builder.setInsertionPoint(op); | ||||||
|
|
||||||
|
|
@@ -40,7 +41,9 @@ static mlir::Value lowerScalarToComplexCast(MLIRContext &ctx, CastOp op) { | |||||
| return builder.createComplexCreate(op.getLoc(), src, imag); | ||||||
| } | ||||||
|
|
||||||
| static mlir::Value lowerComplexToScalarCast(MLIRContext &ctx, CastOp op) { | ||||||
| static mlir::Value lowerComplexToScalarCast(mlir::MLIRContext &ctx, | ||||||
| cir::CastOp op, | ||||||
| cir::CastKind elemToBoolKind) { | ||||||
| CIRBaseBuilderTy builder(ctx); | ||||||
| builder.setInsertionPoint(op); | ||||||
|
|
||||||
|
|
@@ -52,24 +55,17 @@ static mlir::Value lowerComplexToScalarCast(MLIRContext &ctx, CastOp op) { | |||||
| 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"); | ||||||
|
|
||||||
| cir::BoolType boolTy = builder.getBoolTy(); | ||||||
| mlir::Value srcRealToBool = | ||||||
| builder.createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy); | ||||||
| mlir::Value srcImagToBool = | ||||||
| builder.createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy); | ||||||
|
|
||||||
| return builder.createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool); | ||||||
| } | ||||||
|
|
||||||
| static mlir::Value lowerComplexToComplexCast(MLIRContext &ctx, CastOp op) { | ||||||
| static mlir::Value lowerComplexToComplexCast(mlir::MLIRContext &ctx, | ||||||
| cir::CastOp op, | ||||||
| cir::CastKind scalarCastKind) { | ||||||
| CIRBaseBuilderTy builder(ctx); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| builder.setInsertionPoint(op); | ||||||
|
|
||||||
|
|
@@ -80,24 +76,6 @@ static mlir::Value lowerComplexToComplexCast(MLIRContext &ctx, CastOp op) { | |||||
| mlir::Value srcReal = builder.createComplexReal(op.getLoc(), src); | ||||||
| mlir::Value srcImag = builder.createComplexReal(op.getLoc(), src); | ||||||
|
||||||
|
|
||||||
| cir::CastKind scalarCastKind; | ||||||
| switch (op.getKind()) { | ||||||
| case cir::CastKind::float_complex: | ||||||
| scalarCastKind = cir::CastKind::floating; | ||||||
| break; | ||||||
| case cir::CastKind::float_complex_to_int_complex: | ||||||
| scalarCastKind = cir::CastKind::float_to_int; | ||||||
| break; | ||||||
| case cir::CastKind::int_complex: | ||||||
| scalarCastKind = cir::CastKind::integral; | ||||||
| break; | ||||||
| case cir::CastKind::int_complex_to_float_complex: | ||||||
| scalarCastKind = cir::CastKind::int_to_float; | ||||||
| break; | ||||||
| default: | ||||||
| llvm_unreachable("invalid complex to complex cast kind"); | ||||||
| } | ||||||
|
|
||||||
| mlir::Value dstReal = builder.createCast(op.getLoc(), scalarCastKind, srcReal, | ||||||
| dstComplexElemTy); | ||||||
| mlir::Value dstImag = builder.createCast(op.getLoc(), scalarCastKind, srcImag, | ||||||
|
|
@@ -114,19 +92,42 @@ void LoweringPreparePass::lowerCastOp(cir::CastOp op) { | |||||
| break; | ||||||
|
|
||||||
| case cir::CastKind::float_complex_to_real: | ||||||
| case cir::CastKind::int_complex_to_real: | ||||||
| case cir::CastKind::float_complex_to_bool: | ||||||
| case cir::CastKind::int_complex_to_real: { | ||||||
| loweredValue = lowerComplexToScalarCast(getContext(), op, op.getKind()); | ||||||
|
||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| case cir::CastKind::float_complex_to_bool: { | ||||||
| loweredValue = lowerComplexToScalarCast(getContext(), op, | ||||||
| cir::CastKind::float_to_bool); | ||||||
| break; | ||||||
| } | ||||||
| case cir::CastKind::int_complex_to_bool: { | ||||||
| loweredValue = lowerComplexToScalarCast(getContext(), op); | ||||||
| loweredValue = | ||||||
| lowerComplexToScalarCast(getContext(), op, cir::CastKind::int_to_bool); | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| case cir::CastKind::float_complex: | ||||||
| case cir::CastKind::float_complex_to_int_complex: | ||||||
| case cir::CastKind::int_complex: | ||||||
| case cir::CastKind::int_complex_to_float_complex: | ||||||
| loweredValue = lowerComplexToComplexCast(getContext(), op); | ||||||
| case cir::CastKind::float_complex: { | ||||||
| loweredValue = | ||||||
| lowerComplexToComplexCast(getContext(), op, cir::CastKind::floating); | ||||||
| break; | ||||||
| } | ||||||
| case cir::CastKind::float_complex_to_int_complex: { | ||||||
| loweredValue = lowerComplexToComplexCast(getContext(), op, | ||||||
| cir::CastKind::float_to_int); | ||||||
| break; | ||||||
| } | ||||||
| case cir::CastKind::int_complex: { | ||||||
| loweredValue = | ||||||
| lowerComplexToComplexCast(getContext(), op, cir::CastKind::integral); | ||||||
| break; | ||||||
| } | ||||||
| case cir::CastKind::int_complex_to_float_complex: { | ||||||
| loweredValue = lowerComplexToComplexCast(getContext(), op, | ||||||
| cir::CastKind::int_to_float); | ||||||
| break; | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use immediately invoked lambda here and getContext beforehand, it will be way nicer :) Something like this: void LoweringPreparePass::lowerCastOp(cir::CastOp op) {
mlir::MLIRContext *ctx = getContext();
Value loweredValue = [&]() -> Value {
switch (op.getKind()) {
case cir::CastKind::float_to_complex:
case cir::CastKind::int_to_complex:
return lowerScalarToComplexCast(ctx, op);
case cir::CastKind::float_complex_to_real:
case cir::CastKind::int_complex_to_real:
return lowerComplexToScalarCast(ctx, op, op.getKind());
case cir::CastKind::float_complex_to_bool:
return lowerComplexToScalarCast(ctx, op, cir::CastKind::float_to_bool);
case cir::CastKind::int_complex_to_bool:
return lowerComplexToScalarCast(ctx, op, cir::CastKind::int_to_bool);
case cir::CastKind::float_complex:
return lowerComplexToComplexCast(ctx, op, cir::CastKind::floating);
case cir::CastKind::float_complex_to_int_complex:
return lowerComplexToComplexCast(ctx, op, cir::CastKind::float_to_int);
case cir::CastKind::int_complex:
return lowerComplexToComplexCast(ctx, op, cir::CastKind::integral);
case cir::CastKind::int_complex_to_float_complex:
return lowerComplexToComplexCast(ctx, op, cir::CastKind::int_to_float);
default:
return nullptr;
}
}();
if (loweredValue) {
op.replaceAllUsesWith(loweredValue);
op.erase();
}
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice, i will do that |
||||||
|
|
||||||
| default: | ||||||
| return; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.