Skip to content

[CIR][Codegen][Bugfix] use record layout to generate index for a field #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang/lib/CIR/CodeGen/CIRGenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ static void buildLValueForAnyFieldInitialization(CIRGenFunction &CGF,
if (MemberInit->isIndirectMemberInitializer()) {
llvm_unreachable("NYI");
} else {
LHS = CGF.buildLValueForFieldInitialization(LHS, Field, Field->getName(),
Field->getFieldIndex());
LHS = CGF.buildLValueForFieldInitialization(LHS, Field, Field->getName());
}
}

Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,15 @@ LValue CIRGenFunction::buildLValueForField(LValue base,
}

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

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

auto& layout = CGM.getTypes().getCIRGenRecordLayout(Field->getParent());
unsigned FieldIndex = layout.getCIRFieldNo(Field);

Address V = buildAddrOfFieldStorage(*this, Base.getAddress(), Field,
FieldName, FieldIndex);

Expand Down
6 changes: 2 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// This contains code to emit Aggregate Expr nodes as CIR code.
//
//===----------------------------------------------------------------------===//

#include "CIRGenCall.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
Expand Down Expand Up @@ -551,7 +550,7 @@ void AggExprEmitter::VisitLambdaExpr(LambdaExpr *E) {

// Emit initialization
LValue LV = CGF.buildLValueForFieldInitialization(
SlotLV, *CurField, fieldName, CurField->getFieldIndex());
SlotLV, *CurField, fieldName);
if (CurField->hasCapturedVLAType()) {
llvm_unreachable("NYI");
}
Expand Down Expand Up @@ -816,9 +815,8 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
if (curInitIndex == NumInitElements && Dest.isZeroed() &&
CGF.getTypes().isZeroInitializable(ExprToVisit->getType()))
break;

LValue LV = CGF.buildLValueForFieldInitialization(
DestLV, field, field->getName(), field->getFieldIndex());
DestLV, field, field->getName());
// We never generate write-barries for initialized fields.
assert(!UnimplementedFeature::setNonGC());

Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CIR/CodeGen/CIRGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,7 @@ class CIRGenFunction : public CIRGenTypeCache {
/// stored in the reference.
LValue buildLValueForFieldInitialization(LValue Base,
const clang::FieldDecl *Field,
llvm::StringRef FieldName,
unsigned FieldIndex);
llvm::StringRef FieldName);

void buildInitializerForField(clang::FieldDecl *Field, LValue LHS,
clang::Expr *Init);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CIR/CodeGen/derived-to-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,16 @@ void t() {
B b;
b.foo();
}

struct C : public A {
int& ref;
C(int& x) : ref(x) {}
};

// CHECK: cir.func @_Z8test_refv()
// CHECK: cir.get_member %2[1] {name = "ref"}
int test_ref() {
int x = 42;
C c(x);
return c.ref;
}