Skip to content
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

[CIR] Add support for complex cast operations #758

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Jul 27, 2024

This PR adds support for complex cast operations. It adds the following new cast kind variants to the cir.cast operation:

  • float_to_complex,
  • int_to_complex,
  • float_complex_to_real,
  • int_complex_to_real,
  • float_complex_to_bool,
  • int_complex_to_bool,
  • float_complex,
  • float_complex_to_int_complex,
  • int_complex, and
  • int_complex_to_float_complex.

CIRGen and LLVM IR support for these new cast variants are also included.

Comment on lines +464 to +489
return builder
.create<mlir::cir::TernaryOp>(
op.getLoc(), srcRealToBool,
[&](mlir::OpBuilder &, mlir::Location) {
builder.createYield(op.getLoc(),
builder.getTrue(op.getLoc()).getResult());
},
[&](mlir::OpBuilder &, mlir::Location) {
auto inner =
builder
.create<mlir::cir::TernaryOp>(
op.getLoc(), srcImagToBool,
[&](mlir::OpBuilder &, mlir::Location) {
builder.createYield(
op.getLoc(),
builder.getTrue(op.getLoc()).getResult());
},
[&](mlir::OpBuilder &, mlir::Location) {
builder.createYield(
op.getLoc(),
builder.getFalse(op.getLoc()).getResult());
})
.getResult();
builder.createYield(op.getLoc(), inner);
})
.getResult();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the first time I write code like these to implement a simple boolean binary op like a && b or a || b. I think a dedicated operation for binary logical operations are really necessary and they allow generation of much better LLVM IR, although these operations won't be generated from CIRGen directly.

Copy link
Member

@bcardosolopes bcardosolopes Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have gone through the same feeling, we need a ternary that's basically a select operation. If you wanna add that it would be a great addition. cir.ternary_select or something, then a fold operation can even transform a cir.ternary into cir.ternary_select to handle all really simple cases like this one (some CIRGen will use generic code and might emit this pattern regardless of cases we actually know we want a select-like op).

Copy link
Member Author

@Lancern Lancern Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have gone through the same feeling, we need a ternary that's basically a select operation.

Do we additionally need a boolean binary op, something like cir.binop logical_and ? This is preferable to me since it lowers to different code with select operation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how usable this would be, given that usually those have short-circuiting semantics. We have folders and other transformations passes that can help out with this, have you explored any of those solutions already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's track this in #785 .

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nit on testcase

@Lancern
Copy link
Member Author

Lancern commented Aug 2, 2024

Rebased onto the latest main.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some remaining conflict but other than that good to go

This patch adds support for complex cast operations. It adds the following new
cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also included.
@Lancern
Copy link
Member Author

Lancern commented Aug 8, 2024

Rebased onto the latest main.

@bcardosolopes bcardosolopes merged commit 6d069d3 into llvm:main Aug 8, 2024
6 checks passed
@Lancern Lancern deleted the complex-cast branch August 9, 2024 01:06
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
This PR adds support for complex cast operations. It adds the following
new cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also
included.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
This PR adds support for complex cast operations. It adds the following
new cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also
included.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
This PR adds support for complex cast operations. It adds the following
new cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also
included.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
This PR adds support for complex cast operations. It adds the following
new cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also
included.
lanza pushed a commit that referenced this pull request Nov 5, 2024
This PR adds support for complex cast operations. It adds the following
new cast kind variants to the `cir.cast` operation:

  - `float_to_complex`,
  - `int_to_complex`,
  - `float_complex_to_real`,
  - `int_complex_to_real`,
  - `float_complex_to_bool`,
  - `int_complex_to_bool`,
  - `float_complex`,
  - `float_complex_to_int_complex`,
  - `int_complex`, and
  - `int_complex_to_float_complex`.

CIRGen and LLVM IR support for these new cast variants are also
included.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants