Commit 1da964c
[CIR][CodeGen][Bugfix] Generate field index with respect to layout (#263)
There is a bug in the code generation: the field index for the
`GetMemberOp` is taken from the `FieldDecl`, with no respect to the
record layout. One of the manifestation of the bug is the wrong index
generated for a field in a derived class that does not take into the
account the instance of the base class (that has index 0).
You can take a look at the example in
`test/CIR/CodeGen/derived-to-base.cpp`, i.e. the current test is not the
correct one
```
// CHECK-DAG: !ty_22C23A3ALayer22 = !cir.struct<class "C2::Layer" {!ty_22C13A3ALayer22, !cir.ptr<!ty_22C222>
// CHECK-DAG: !ty_22C33A3ALayer22 = !cir.struct<struct "C3::Layer" {!ty_22C23A3ALayer22
// CHECK: cir.func @_ZN2C35Layer10InitializeEv
// CHECK: cir.scope {
// CHECK: %2 = cir.base_class_addr(%1 : cir.ptr <!ty_22C33A3ALayer22>) -> cir.ptr <!ty_22C23A3ALayer22>
// CHECK: %3 = cir.get_member %2[0] {name = "m_C1"} : !cir.ptr<!ty_22C23A3ALayer22> -> !cir.ptr<!cir.ptr<!ty_22C222>>
```
As one can see, the result (of ptr type to `!ty_22C222` ) must have the
index `1` in the corresponded struct `ty_22C23A3ALayer22`.
Basically the same is done in the `clang/CodeGen/CGExpr.cpp`, so we
don't invent something new here.
Note, this fix doesn't affect anything related to the usage of
`buildPreserveStructAccess` where the `field->getFieldIndex()` is used.1 parent 3da0a05 commit 1da964c
File tree
2 files changed
+4
-2
lines changed- clang
- lib/CIR/CodeGen
- test/CIR/CodeGen
2 files changed
+4
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
274 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
275 | 277 | | |
276 | 278 | | |
277 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| |||
0 commit comments