New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 35763 - -O0 bug: long bitfields triggering incorrect code
Summary: -O0 bug: long bitfields triggering incorrect code
Status: RESOLVED FIXED
Alias: None
Product: new-bugs
Classification: Unclassified
Component: new bugs (show other bugs)
Version: trunk
Hardware: PC All
: P enhancement
Assignee: Sam Parker
URL:
Keywords:
Depends on:
Blocks: release-6.0
  Show dependency tree
 
Reported: 2017-12-28 02:40 PST by Dmitry Babokin
Modified: 2018-01-17 07:54 PST (History)
7 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Babokin 2017-12-28 02:40:46 PST
clang trunk, x86_64, rev321482.

> cat f.cpp
struct S {
    short m0;
    unsigned long long int m2 : 21;
    short m3 : 16;
    short m5 : 17;
    short : 16;
};

S z = {-724, 388702ULL, 26720, 2792};

unsigned long long int tf_3_var_136 = 0;

void foo() {
  tf_3_var_136 = ((unsigned short)(z.m0 | z.m2));
  z.m5 = 0;
}

int main() {
    foo();
    __builtin_printf("%llu\n", tf_3_var_136);
    return 0;
}

> clang++ -w f.cpp -o outc; g++ -w f.cpp -o outg; ./outc; ./outg
3999203198
65406

Note that clang 5.0 also produces correct result (as gcc in the example above).

Slight massaging of the test case make the bug go. You may notice that one of the bit fields is longer than "base" type, i.e. short : 17. But this is a valid C++, according to the standard bits after the "base" type size are just "padding".

Anyway, effects triggered by this example are definitely considered as a bug by regular clang users.
Comment 1 Craig Topper 2017-12-28 16:41:40 PST
This looks related to r320962   [DAGCombine] Move AND nodes to multiple load leaves.

Though different than PR35765. 

Looks like the IR contains an i40 store which triggers a fast-isel abort leading to falling back to SelectionDAG and triggering optimizations. Really strange that -O0 does that when fast-isel aborts.
Comment 2 Sam Parker 2017-12-29 04:51:49 PST
Patch for review: https://reviews.llvm.org/D41628
Comment 3 Sam Parker 2018-01-05 00:49:51 PST
Fix committed in https://reviews.llvm.org/rL321862
Comment 4 Simon Pilgrim 2018-01-05 02:19:52 PST
Keeping this open until merged into 6.00
Comment 5 Hans Wennborg 2018-01-17 07:54:56 PST
Merged in r322671.