Bug 97351 - gcc.dg/vect/bb-slp-subgroups-3.c bad vectorization with AVX
Summary: gcc.dg/vect/bb-slp-subgroups-3.c bad vectorization with AVX
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: vectorizer
  Show dependency treegraph
 
Reported: 2020-10-09 10:54 UTC by Richard Biener
Modified: 2021-09-27 08:25 UTC (History)
0 users

See Also:
Host:
Target: x86_64-*-* i?86-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2020-10-09 10:54:44 UTC
int __attribute__((__aligned__(8))) a[8];
int __attribute__((__aligned__(8))) b[8];

void
test ()
{
    a[0] = b[0] + 1;
    a[1] = b[1] + 2;
    a[2] = b[2] + 3;
    a[3] = b[3] + 4;
    a[4] = b[0] * 3;
    a[5] = b[2] * 4;
    a[6] = b[4] * 5;
    a[7] = b[6] * 7;
}

should be vectorized using V4SI vectors in two SLP groups so we can
vectorize not only the store but also the loads and the add.  When
using -mavx2 we instead get only the store vectorized (even with
cost modeling enabled) because we try vectorizing that first.

It might be possible to guide SLP splitting during the SLP build
in a similar way how we try commutating operands. So when we figure

/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: note:   Build SLP for _9 = _1 * 3;
/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: note:   get vectype for scalar type (group size 8): int
/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: note:   vectype: vector(8) int
/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: note:   nunits = 8
/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: missed:   Build SLP failed: different operation in stmt _9 = _1 * 3;
/home/rguenther/src/gcc3/gcc/testsuite/gcc.dg/vect/bb-slp-subgroups-3.c:12:10: missed:   original stmt _2 = _1 + 1;

and see the parent op (the store in this case) cannot be commutated we
can see whether matches[] divides the vector with some constraints
and whether the other lanes with matches[] == false form a valid SLP
operand (we know the == true ones likely would).  The results would then
be concatenated via a permute node.

This should eventually also replace the splitting done in SLP instance
analysis (though splitting stores might still be necessary there).

The other option is to somehow tackle this with vector size iteration,
doing multiple analyses and comparing costs/benefit though it's hard
to not compare apples & oranges since the amount of code vectorized will
usually differ (as compared to loop vectorization)
Comment 1 Andrew Pinski 2021-08-29 04:58:44 UTC
Confirmed.
Comment 2 GCC Commits 2021-09-27 08:24:33 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:6390c5047adb75960f86d56582e6322aaa4d9281

commit r12-3893-g6390c5047adb75960f86d56582e6322aaa4d9281
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Nov 18 09:36:57 2020 +0100

    Allow different vector types for stmt groups
    
    This allows vectorization (in practice non-loop vectorization) to
    have a stmt participate in different vector type vectorizations.
    It allows us to remove vect_update_shared_vectype and replace it
    by pushing/popping STMT_VINFO_VECTYPE from SLP_TREE_VECTYPE around
    vect_analyze_stmt and vect_transform_stmt.
    
    For data-ref the situation is a bit more complicated since we
    analyze alignment info with a specific vector type in mind which
    doesn't play well when that changes.
    
    So the bulk of the change is passing down the actual vector type
    used for a vectorized access to the various accessors of alignment
    info, first and foremost dr_misalignment but also aligned_access_p,
    known_alignment_for_access_p, vect_known_alignment_in_bytes and
    vect_supportable_dr_alignment.  I took the liberty to replace
    ALL_CAPS macro accessors with the lower-case function invocations.
    
    The actual changes to the behavior are in dr_misalignment which now
    is the place factoring in the negative step adjustment as well as
    handling alignment queries for a vector type with bigger alignment
    requirements than what we can (or have) analyze(d).
    
    vect_slp_analyze_node_alignment makes use of this and upon receiving
    a vector type with a bigger alingment desire re-analyzes the DR
    with respect to it but keeps an older more precise result if possible.
    In this context it might be possible to do the analysis just once
    but instead of analyzing with respect to a specific desired alignment
    look for the biggest alignment we can compute a not unknown alignment.
    
    The ChangeLog includes the functional changes but not the bulk due
    to the alignment accessor API changes - I hope that's something good.
    
    2021-09-17  Richard Biener  <rguenther@suse.de>
    
            PR tree-optimization/97351
            PR tree-optimization/97352
            PR tree-optimization/82426
            * tree-vectorizer.h (dr_misalignment): Add vector type
            argument.
            (aligned_access_p): Likewise.
            (known_alignment_for_access_p): Likewise.
            (vect_supportable_dr_alignment): Likewise.
            (vect_known_alignment_in_bytes): Likewise.  Refactor.
            (DR_MISALIGNMENT): Remove.
            (vect_update_shared_vectype): Likewise.
            * tree-vect-data-refs.c (dr_misalignment): Refactor, handle
            a vector type with larger alignment requirement and apply
            the negative step adjustment here.
            (vect_calculate_target_alignment): Remove.
            (vect_compute_data_ref_alignment): Get explicit vector type
            argument, do not apply a negative step alignment adjustment
            here.
            (vect_slp_analyze_node_alignment): Re-analyze alignment
            when we re-visit the DR with a bigger desired alignment but
            keep more precise results from smaller alignments.
            * tree-vect-slp.c (vect_update_shared_vectype): Remove.
            (vect_slp_analyze_node_operations_1): Do not update the
            shared vector type on stmts.
            * tree-vect-stmts.c (vect_analyze_stmt): Push/pop the
            vector type of an SLP node to the representative stmt-info.
            (vect_transform_stmt): Likewise.
    
            * gcc.target/i386/vect-pr82426.c: New testcase.
            * gcc.target/i386/vect-pr97352.c: Likewise.
Comment 3 Richard Biener 2021-09-27 08:25:53 UTC
Fixed for GCC 12.