Commit a922f1a
authored
[CIR][CodeGen] Updates GlobalViewAttr's indices computation for the union type (#1584)
This PR introduces a new attribute for the explicit byte offset for
`GlobalViewAttr`. It's a proposal, so feel free to reject it once it's
not good from your point of view.
The problem is (as usually) with globals and unions: looks like we can
not really use indexes in the `GlobalView` to address an array of
unions. For example, the next program prints `4` now, but it should be
`42`:
```
typedef struct {
long s0;
int s1;
} S;
typedef union {
int f0;
S f1;
} U;
static U g1[3] = {{42},{42},{42}};
int* g2 = &g1[1].f1.s1;
int main() {
(*g2) = 4;
printf("%d\n", g1[1].f0);
return0;
}
```
The problem is that we compute wrong indices in
`CIRGenBuilder::computeGlobalViewIndicesFromFlatOffset`. Maybe it can be
even fixed in this case, but I have a feeling that the fix would be a
bit fragile.
So instead of trying to support indexes for the array of unions I
suggest to use the offset explicitly.
From the implementation point of view there are some changes in
`CIRGenBuilder ` - but nothing really new is in there - I just did not
want to introduce copy-pasta for the `isOffsetInUnion` function that is
pretty the same as former `computeGlobalViewIndicesFromFlatOffset`.1 parent c154087 commit a922f1a
File tree
2 files changed
+28
-2
lines changed- clang
- lib/CIR/CodeGen
- test/CIR/CodeGen
2 files changed
+28
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
100 | 104 | | |
101 | 105 | | |
102 | 106 | | |
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
106 | 110 | | |
107 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
108 | 114 | | |
109 | 115 | | |
110 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
0 commit comments