From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 279803858D39; Wed, 22 Sep 2021 13:22:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 279803858D39 From: "peter at peterzhu dot ca" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/102452] New: Structs with flexible array members are not optimized on stack Date: Wed, 22 Sep 2021 13:22:07 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 9.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: peter at peterzhu dot ca X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Sep 2021 13:22:08 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102452 Bug ID: 102452 Summary: Structs with flexible array members are not optimized on stack Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: peter at peterzhu dot ca Target Milestone: --- Created attachment 51494 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D51494&action=3Dedit test.c Structs with flexible array members are not optimized on the stack. Testing with the test.c file shown below (which outputs the runtime in seconds), we= see that the compiled program is significantly slower when the struct has a flexible array member than when it does not. This was tested on GCC 9.3.0 a= nd 10.3.0 on Ubuntu 20.04. $ gcc -O3 test.c $ ./a.out 0.302769 $ gcc -O3 -DUSE_FLEX_ARR=3D1 test.c $ ./a.out 0.728760 clang does not have this issue. $ clang -O3 test.c $ ./a.out 0.312194 $ clang -O3 -DUSE_FLEX_ARR=3D1 test.c $ ./a.out 0.301175 This is what test.c looks like: #include #include #include #include struct Test { long is_a; union { struct { long one; long two; long three; } a; struct { int one; int two; int three; int four; #if USE_FLEX_ARR char arr[]; #endif } b; } as; }; #define COUNT 100000000 static inline struct Test make_test_a(struct Test *test) { if (test->is_a) { return *test; } else { struct Test ret; ret.as.a.one =3D test->as.b.one; ret.as.a.two =3D test->as.b.two; ret.as.a.three =3D test->as.b.three; return ret; } } /* This function should be optimized to not allocate struct Test on the sta= ck * since it only uses attribute "three". */ static inline long get_three(struct Test *test) { return make_test_a(test).as.a.three; } int main(int argc, char *argv[]) { struct timespec start, end; struct Test *mem =3D malloc(sizeof(struct Test) * COUNT); memset(mem, 0, sizeof(struct Test) * COUNT); clock_gettime(CLOCK_MONOTONIC, &start); { for (int i =3D 0; i < COUNT; i++) { long three =3D get_three(&mem[i]); if (three) { /* Impossible case. */ printf("what\n"); } } } clock_gettime(CLOCK_MONOTONIC, &end); double time =3D (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_n= sec) / 1000000000.0; printf("%f\n", time); return 0; }=