Stream: git-wasmtime

Topic: wasmtime / PR #5546 cranelift: Optimize `select+icmp` int...


view this post on Zulip Wasmtime GitHub notifications bot (Jan 07 2023 at 12:23):

afonso360 opened PR #5546 from select-optimizations to main:

:wave: Hey,

This PR adds some egraph rules to optimize a select + icmp into the appropriate version of the min/max instructions.

We transform the following code:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = icmp sgt v0, v1
    v3 = select v2, v0, v1
    return v3
}

Into:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = smax v0, v1
    return v2
}

Which is usually a single instruction in the backends.

I've been running this in fuzzgen for a while and it hasn't complained so far!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 07 2023 at 12:33):

afonso360 edited PR #5546 from select-optimizations to main:

:wave: Hey,

This PR adds some egraph rules to optimize a select + icmp into the appropriate version of the min/max instructions.

We transform the following code:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = icmp sgt v0, v1
    v3 = select v2, v0, v1
    return v3
}

Into:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = smax v0, v1
    return v2
}

I've been running this in fuzzgen for a while and it hasn't complained so far!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 07 2023 at 12:37):

afonso360 edited PR #5546 from select-optimizations to main:

:wave: Hey,

This PR adds some egraph rules to optimize a select + icmp into the appropriate version of the min/max instructions.

We transform the following code:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = icmp sgt v0, v1
    v3 = select v2, v0, v1
    return v3
}

Into:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = smax v0, v1
    return v2
}

This is beneficial at least for the riscv backend, where we can emit the max instruction in these cases instead of branching.

I've been running this in fuzzgen for a while and it hasn't complained so far!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 07 2023 at 12:37):

afonso360 edited PR #5546 from select-optimizations to main:

:wave: Hey,

This PR adds some egraph rules to optimize a select + icmp into the appropriate version of the min/max instructions.

We transform the following code:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = icmp sgt v0, v1
    v3 = select v2, v0, v1
    return v3
}

Into:

function %select_sgt_to_smax(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
    v2 = smax v0, v1
    return v2
}

This is beneficial at least for the RISC-V backend, where we can emit the max instruction in these cases instead of branching.

I've been running this in fuzzgen for a while and it hasn't complained so far!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 09 2023 at 20:29):

jameysharp submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 15 2023 at 17:57):

afonso360 updated PR #5546 from select-optimizations to main.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 29 2023 at 18:59):

afonso360 updated PR #5546 from select-optimizations to main.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 09 2023 at 00:01):

cfallin submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 09 2023 at 00:01):

cfallin created PR review comment:

@jameysharp and I talked a little further about this today and we agreed that we probably don't want to double the number of icmp nodes unconditionally; it's cheaper to have more rules, some with inverted matching forms on the left-hand sides, than it is to grow the egraph and then have to match on the result of that. Would you be willing to remove this rule and compose it into the below manually instead?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 09 2023 at 00:01):

cfallin submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 09 2023 at 20:41):

afonso360 updated PR #5546 from select-optimizations to main.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 15 2023 at 11:57):

afonso360 updated PR #5546 from select-optimizations to main.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 15 2023 at 23:06):

jameysharp merged PR #5546.


Last updated: Nov 22 2024 at 17:03 UTC