Skip to content

Commit 1215888

Browse files
gitoleglanza
authored andcommitted
[CIR][Codegen][Bugfix] use record layout to generate index for a field (llvm#270)
This is a minor fix similar to the one introduced in llvm#263. Basically, all calls to the `buildLValueForFieldInitialization` are even with the origin codegen `emitLValueForFieldInitialization` calls, i.e. the field index is calculated from the record layout, but not from the decl `field->getFieldIndex()`. Added just one test, because looks like we need to implement some `NYI` features first to test another places e.g. in `CIRGenExprAgg.cpp`, though I could miss something. Anyway, given the original codegen doesn't use `getFieldIndex` in these places, we also should not. All the remaining usages of `getFieldIndex` are ok.
1 parent 5309378 commit 1215888

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ static void buildLValueForAnyFieldInitialization(CIRGenFunction &CGF,
207207
if (MemberInit->isIndirectMemberInitializer()) {
208208
llvm_unreachable("NYI");
209209
} else {
210-
LHS = CGF.buildLValueForFieldInitialization(LHS, Field, Field->getName(),
211-
Field->getFieldIndex());
210+
LHS = CGF.buildLValueForFieldInitialization(LHS, Field, Field->getName());
212211
}
213212
}
214213

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,15 @@ LValue CIRGenFunction::buildLValueForField(LValue base,
388388
}
389389

390390
LValue CIRGenFunction::buildLValueForFieldInitialization(
391-
LValue Base, const clang::FieldDecl *Field, llvm::StringRef FieldName,
392-
unsigned FieldIndex) {
391+
LValue Base, const clang::FieldDecl *Field, llvm::StringRef FieldName) {
393392
QualType FieldType = Field->getType();
394393

395394
if (!FieldType->isReferenceType())
396395
return buildLValueForField(Base, Field);
397396

397+
auto& layout = CGM.getTypes().getCIRGenRecordLayout(Field->getParent());
398+
unsigned FieldIndex = layout.getCIRFieldNo(Field);
399+
398400
Address V = buildAddrOfFieldStorage(*this, Base.getAddress(), Field,
399401
FieldName, FieldIndex);
400402

clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// This contains code to emit Aggregate Expr nodes as CIR code.
1010
//
1111
//===----------------------------------------------------------------------===//
12-
1312
#include "CIRGenCall.h"
1413
#include "CIRGenFunction.h"
1514
#include "CIRGenModule.h"
@@ -553,7 +552,7 @@ void AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {
553552

554553
// Emit initialization
555554
LValue LV = CGF.buildLValueForFieldInitialization(
556-
SlotLV, *CurField, fieldName, CurField->getFieldIndex());
555+
SlotLV, *CurField, fieldName);
557556
if (CurField->hasCapturedVLAType()) {
558557
llvm_unreachable("NYI");
559558
}
@@ -818,9 +817,8 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
818817
if (curInitIndex == NumInitElements && Dest.isZeroed() &&
819818
CGF.getTypes().isZeroInitializable(ExprToVisit->getType()))
820819
break;
821-
822820
LValue LV = CGF.buildLValueForFieldInitialization(
823-
DestLV, field, field->getName(), field->getFieldIndex());
821+
DestLV, field, field->getName());
824822
// We never generate write-barries for initialized fields.
825823
assert(!UnimplementedFeature::setNonGC());
826824

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,7 @@ class CIRGenFunction : public CIRGenTypeCache {
15311531
/// stored in the reference.
15321532
LValue buildLValueForFieldInitialization(LValue Base,
15331533
const clang::FieldDecl *Field,
1534-
llvm::StringRef FieldName,
1535-
unsigned FieldIndex);
1534+
llvm::StringRef FieldName);
15361535

15371536
void buildInitializerForField(clang::FieldDecl *Field, LValue LHS,
15381537
clang::Expr *Init);

clang/test/CIR/CodeGen/derived-to-base.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,16 @@ void t() {
156156
B b;
157157
b.foo();
158158
}
159+
160+
struct C : public A {
161+
int& ref;
162+
C(int& x) : ref(x) {}
163+
};
164+
165+
// CHECK: cir.func @_Z8test_refv()
166+
// CHECK: cir.get_member %2[1] {name = "ref"}
167+
int test_ref() {
168+
int x = 42;
169+
C c(x);
170+
return c.ref;
171+
}

0 commit comments

Comments
 (0)