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

[DAGCombiner] Treat extracts from build_vectors that are splats as free #65773

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented Sep 8, 2023

When scalarizing bin ops of splats, we treat the extract as free for a
splat_vector because its operand is already scalar, i.e.

(extract idx, (splat_vector x)) -> x.

The same also applies for a build_vector that's a splat:

(extract idx, (build_vector x x x x)) -> x.

This patch takes this into account, which enables scalarization for fixed
length vectors, since the current canonical form for a splatted fixed length
vector is still build_vector.

This improves what we were seeing on RISC-V in #65068, but unfortunately causes
some patterns to be missed on other targets. One big one is that on AArch64
and X86 scalarizing (xor (splat x), (splat -1)) to (splat (xor x, -1)) prevents
vnot from being matched, which for example prevents bif from being matched.

Posting this patch as a WIP to show my findings.

This adds tests for fixed and scalable vectors where we have a binary op on two
splats that could be scalarized. Normally this would be scalarized in the
middle-end by VectorCombine, but as noted in https://reviews.llvm.org/D159190,
this pattern can crop up during CodeGen afterwards.

Note that a combine already exists for this, but currently it only works on
scalable vectors where the element type == XLEN. See llvm#65068 and llvm#65072
When scalarizing bin ops of splats, we treat the extract as free for a
splat_vector because its operand is already scalar, i.e.

(extract idx, (splat_vector x)) -> x.

The same also applies for a build_vector that's a splat:

(extract idx, (build_vector x x x x)) -> x.

This patch takes this into account, which enables scalarization for fixed
length vectors, since the current canonical form for a splatted fixed length
vector is still build_vector.

This improves what we were seeing on RISC-V in llvm#65068, but unfortunately causes
some patterns to be missed on other targets.  One big one is that on AArch64
and X86 scalarizing (xor (splat x), (splat -1)) to (splat (xor x, -1)) prevents
vnot from being matched, which for example prevents bif from being matched.

Posting this patch as a WIP to show my findings.
@lukel97 lukel97 force-pushed the scalarize-binop-of-splats-build-vector/free branch from 59caba7 to 7980f5b Compare September 13, 2023 12:21
@@ -244,7 +244,8 @@ define void @undef_hi_op_v2f16(half %arg0) {
; GFX9-LABEL: undef_hi_op_v2f16:
; GFX9: ; %bb.0:
; GFX9-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
; GFX9-NEXT: v_pk_add_f16 v0, v0, 1.0 op_sel_hi:[1,0]
; GFX9-NEXT: v_add_f16_e32 v0, 1.0, v0
; GFX9-NEXT: v_pack_b32_f16 v0, v0, v0
Copy link
Contributor

Choose a reason for hiding this comment

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

These are all regressions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I've marked this patch as a draft since I just want to share my findings. Hoping others might have some ideas on additional patterns that can be matched to mitigate all these regressions

; SI-NEXT: v_mov_b32_e32 v0, s4
; SI-NEXT: v_alignbit_b32 v0, s5, v0, 16
Copy link
Contributor

Choose a reason for hiding this comment

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

Regression

; GFX9-NEXT: v_pk_ashrrev_i16 v0, 15, v5 op_sel_hi:[0,0]
; GFX9-NEXT: s_movk_i32 s4, 0x8000
; GFX9-NEXT: v_ashrrev_i16_e32 v0, 15, v5
; GFX9-NEXT: s_mov_b32 s4, 0x5040100
Copy link
Contributor

Choose a reason for hiding this comment

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

More regression

@lukel97 lukel97 changed the title Scalarize binop of splats build vector/free [DAGCombiner] Treat extracts from build_vectors that are splats as free Sep 13, 2023
@RKSimon
Copy link
Collaborator

RKSimon commented Sep 13, 2023

We already have several places where we early-out if we'd break a NOT pattern

@RKSimon
Copy link
Collaborator

RKSimon commented Feb 18, 2024

@lukel97 reverse-ping - are you still looking at this?

@lukel97
Copy link
Contributor Author

lukel97 commented Feb 18, 2024

@lukel97 reverse-ping - are you still looking at this?

No, feel free to take over or close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants