Skip to content

[CIR][Transform] Add ternary simplification #809

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

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Aug 28, 2024

This PR adds a new transformation that transform suitable ternary operations into select operations.

Currently the "suitable" ternary operations are those ternary operations whose both branches satisfy either one of the following criteria:

  • The branch only contain a single cir.yield operation;
  • The branch contains a cir.const followed by a cir.yield that yields the constant value produced by the cir.const.
  • The branch contains a cir.load followed by a cir.yield that yields the value loaded by the cir.load. The load operation cannot be volatile and must load from an alloca.

These criteria are hardcoded now so that simple C/C++ ternary expressions could be eventually lowered to a cir.select operation instead.

@Lancern Lancern force-pushed the ternary-fold-to-select branch from 92339bf to 9ae5932 Compare August 28, 2024 16:14
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.

Thanks for adding this, few comments!

@Lancern Lancern force-pushed the ternary-fold-to-select branch from 9ae5932 to 2f023a4 Compare August 29, 2024 13:47
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.

Sorry about the delay, just got back from vacation. This looks mostly good, one minor comment + there are some failing tests.

bcardosolopes pushed a commit that referenced this pull request Sep 9, 2024
As mentioned at
#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
@Lancern Lancern force-pushed the ternary-fold-to-select branch from d9c5e68 to 7f33374 Compare September 10, 2024 13:30
@Lancern
Copy link
Member Author

Lancern commented Sep 10, 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.

LGTM

@bcardosolopes
Copy link
Member

There are still bot failures, but should be good to go once they are solved

This patch adds a new transformation that transform suitable ternary operations
into select operations.
@Lancern Lancern force-pushed the ternary-fold-to-select branch from 7f33374 to e2380e3 Compare September 11, 2024 16:00
Copy link
Member Author

@Lancern Lancern left a comment

Choose a reason for hiding this comment

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

Test failures fixed.

@bcardosolopes bcardosolopes merged commit fcc2590 into llvm:main Sep 11, 2024
6 checks passed
@Lancern Lancern deleted the ternary-fold-to-select branch September 12, 2024 00:44
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
As mentioned at
llvm#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
As mentioned at
llvm#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
As mentioned at
llvm#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
As mentioned at
llvm#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
lanza pushed a commit that referenced this pull request Nov 5, 2024
As mentioned at
#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
lanza pushed a commit that referenced this pull request Nov 5, 2024
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
lanza pushed a commit that referenced this pull request Mar 18, 2025
As mentioned at
#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
lanza pushed a commit that referenced this pull request Mar 18, 2025
This PR adds a new transformation that transform suitable ternary
operations into select operations.

Currently the "suitable" ternary operations are those ternary operations
whose both branches satisfy either one of the following criteria:

- The branch only contain a single `cir.yield` operation;
- The branch contains a `cir.const` followed by a `cir.yield` that
yields the constant value produced by the `cir.const`.
- ~~The branch contains a `cir.load` followed by a `cir.yield` that
yields the value loaded by the `cir.load`. The load operation cannot be
volatile and must load from an alloca.~~

These criteria are hardcoded now so that simple C/C++ ternary
expressions could be eventually lowered to a `cir.select` operation
instead.
xlauko pushed a commit to trailofbits/instafix-llvm that referenced this pull request Mar 28, 2025
As mentioned at
llvm/clangir#809 (comment) , this
PR adds more simplify transformations for select op:

- `cir.select if %0 then x else x` -> `x`
- `cir.select if %0 then #true else #false` -> `%0`
- `cir.select if %0 then #false else #true` -> `cir.unary not %0`
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