Bug 53970 - -ftree-vectorization does not handle well unaligned data
Summary: -ftree-vectorization does not handle well unaligned data
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.6.3
: P3 normal
Target Milestone: 4.8.0
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-07-15 08:40 UTC by Hayim Shaul
Modified: 2012-07-19 08:48 UTC (History)
2 users (show)

See Also:
Host:
Target: x86_64-*-*
Build:
Known to work:
Known to fail: 4.6.3, 4.7.1, 4.8.0
Last reconfirmed: 2012-07-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hayim Shaul 2012-07-15 08:40:25 UTC
When compiling this code:

#pragma pack(1)
struct mystruct {
    char c;
    unsigned long l[1024];
};
#pragma pack()

int main(int argc, char **argv) {
    mystruct *a = new mystruct;

    unsigned long long i;

    for (i = 0; i < 1024; ++i)
        a->l[i] = 0xdeadbeaf;
}


with -O2 -ftree-vectorize -Wall, the code crashes.
More specifically, the loop is vectorized assuming alignment (movdqa), which causes the crash.

I think the proper behaviour should be to print a warning message at compile time that an unaligned data is used, or even vectorize the loop without assuming alignment.


(co-finders: Tomer Cohen and Ofek Ben-Arie)
Comment 1 H.J. Lu 2012-07-15 13:54:21 UTC
Dup.

*** This bug has been marked as a duplicate of bug 36159 ***
Comment 2 Hans-Peter Nilsson 2012-07-15 16:48:16 UTC
(In reply to comment #1)
> Dup.

No...?  I think you missed the explicit "char" member in the struct being new:ed, making the data *known* misaligned.  Or was it I who missed something?
(Changing back to unconfirmed as I didn't confirm the bahavior.)
Comment 3 Richard Biener 2012-07-16 09:40:20 UTC
Confirmed, mine.
Comment 4 Richard Biener 2012-07-16 14:53:21 UTC
The only hint we have on the a->l[i] expression that it involves a packed
aggregate is DECL_ALIGN of the 'l' FIELD_DECL and TYPE_ALIGN of mystruct
itself.  Nowhere we have a TYPE/DECL_PACKED flag set.  That is because how
pragma pack works - it simply adjusts the global maximum_field_alignment
of stor-layout.c.

That means that expr.c:contains_packed_reference does not work.
Comment 5 Eric Botcazou 2012-07-18 08:32:53 UTC
> That means that expr.c:contains_packed_reference does not work.

Absolutely, contains_packed_reference is wrong since DECL_PACKED is only meant to drive stor-layout.c (and dwarf2out.c), i.e. it's only a declarative flag.
Comment 6 Richard Biener 2012-07-18 10:46:11 UTC
Author: rguenth
Date: Wed Jul 18 10:46:05 2012
New Revision: 189609

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189609
Log:
2012-07-18  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/53970
	* tree.h (contains_packed_reference): Remove.
	* expr.c (contains_packed_reference): Likewise.
	* tree-vect-data-refs.c (not_size_aligned): New function.
	(vector_alignment_reachable_p): Use it.
	(vect_supportable_dr_alignment): Likewise.

	* g++.dg/torture/pr53970.C: New testcase.

Added:
    trunk/gcc/testsuite/g++.dg/torture/pr53970.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/expr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vect-data-refs.c
    trunk/gcc/tree.h
Comment 7 Richard Biener 2012-07-18 10:46:34 UTC
Fixed for GCC 4.8.
Comment 8 Richard Biener 2012-07-19 08:48:05 UTC
Author: rguenth
Date: Thu Jul 19 08:48:01 2012
New Revision: 189647

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189647
Log:
2012-07-19  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/53970
	* tree-vect-data-refs.c (not_size_aligned): Avoid sign-compare
	warning.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/tree-vect-data-refs.c