Skip to content

Commit 15ee147

Browse files
sitio-coutolanza
authored andcommitted
[CIR][CIRGen][NFC] Refactor struct type building
Updates every struct type creation to use the CIRGenBuilder API. ghstack-source-id: b868f82 Pull Request resolved: #225
1 parent 0629e48 commit 15ee147

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
#include "mlir/IR/Location.h"
2828
#include "mlir/IR/Types.h"
2929
#include "llvm/ADT/APSInt.h"
30+
#include "llvm/ADT/ArrayRef.h"
3031
#include "llvm/ADT/FloatingPointMode.h"
3132
#include "llvm/ADT/StringMap.h"
3233
#include "llvm/Support/ErrorHandling.h"
3334
#include <cassert>
35+
#include <optional>
3436
#include <string>
3537

3638
namespace cir {
@@ -185,11 +187,10 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
185187
assert(ta && "expected typed attribute member");
186188
members.push_back(ta.getType());
187189
}
188-
auto *ctx = arrayAttr.getContext();
190+
189191
if (!ty)
190-
ty = mlir::cir::StructType::get(ctx, members, mlir::StringAttr::get(ctx),
191-
/*body=*/true, packed,
192-
/*ast=*/std::nullopt);
192+
ty = getAnonStructTy(members, /*body=*/true, packed);
193+
193194
auto sTy = ty.dyn_cast<mlir::cir::StructType>();
194195
assert(sTy && "expected struct type");
195196
return mlir::cir::ConstStructAttr::get(sTy, arrayAttr);
@@ -376,6 +377,25 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
376377
return typeCache.VoidPtrTy;
377378
}
378379

380+
/// Get a CIR anonymous struct type.
381+
mlir::cir::StructType
382+
getAnonStructTy(llvm::ArrayRef<mlir::Type> members, bool body,
383+
bool packed = false, const clang::RecordDecl *ast = nullptr) {
384+
return getStructTy(members, "", body, packed, ast);
385+
}
386+
387+
/// Get a CIR named struct type.
388+
mlir::cir::StructType getStructTy(llvm::ArrayRef<mlir::Type> members,
389+
llvm::StringRef name, bool body,
390+
bool packed, const clang::RecordDecl *ast) {
391+
const auto nameAttr = getStringAttr(name);
392+
std::optional<mlir::cir::ASTRecordDeclAttr> astAttr = std::nullopt;
393+
if (ast)
394+
astAttr = getAttr<mlir::cir::ASTRecordDeclAttr>(ast);
395+
return mlir::cir::StructType::get(getContext(), members, nameAttr, body,
396+
packed, astAttr);
397+
}
398+
379399
//
380400
// Constant creation helpers
381401
// -------------------------

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *RD) {
169169
// Handle forward decl / incomplete types.
170170
if (!entry) {
171171
auto name = getRecordTypeName(RD, "");
172-
auto identifier = mlir::StringAttr::get(&getMLIRContext(), name);
173-
entry = mlir::cir::StructType::get(
174-
&getMLIRContext(), {}, identifier, /*body=*/false, /**packed=*/false,
175-
mlir::cir::ASTRecordDeclAttr::get(&getMLIRContext(), RD));
172+
entry = Builder.getStructTy({}, name, /*body=*/false,
173+
/*packed=*/false, RD);
176174
recordDeclTypes[key] = entry;
177175
}
178176

clang/lib/CIR/CodeGen/CIRGenVTables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mlir::Type CIRGenVTables::getVTableType(const VTableLayout &layout) {
5858

5959
// FIXME(cir): should VTableLayout be encoded like we do for some
6060
// AST nodes?
61-
return mlir::cir::StructType::get(ctx, tys, "", /*body=*/true);
61+
return CGM.getBuilder().getAnonStructTy(tys, /*body=*/true);
6262
}
6363

6464
/// At this point in the translation unit, does it appear that can we

clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,6 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
571571

572572
builder.lower(/*nonVirtualBaseType=*/false);
573573

574-
auto name = getRecordTypeName(D, "");
575-
auto identifier = mlir::StringAttr::get(&getMLIRContext(), name);
576-
577574
// If we're in C++, compute the base subobject type.
578575
mlir::cir::StructType *BaseTy = nullptr;
579576
if (llvm::isa<CXXRecordDecl>(D) && !D->isUnion() &&
@@ -582,12 +579,9 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
582579
if (builder.astRecordLayout.getNonVirtualSize() !=
583580
builder.astRecordLayout.getSize()) {
584581
CIRRecordLowering baseBuilder(*this, D, /*Packed=*/builder.isPacked);
585-
auto baseIdentifier =
586-
mlir::StringAttr::get(&getMLIRContext(), name + ".base");
587-
*BaseTy = mlir::cir::StructType::get(
588-
&getMLIRContext(), baseBuilder.fieldTypes, baseIdentifier,
589-
/*body=*/true, /**packed=*/false,
590-
mlir::cir::ASTRecordDeclAttr::get(&getMLIRContext(), D));
582+
auto baseIdentifier = getRecordTypeName(D, ".base");
583+
*BaseTy = Builder.getStructTy(baseBuilder.fieldTypes, baseIdentifier,
584+
/*body=*/true, /*packed=*/false, D);
591585
// TODO(cir): add something like addRecordTypeName
592586

593587
// BaseTy and Ty must agree on their packedness for getCIRFieldNo to work
@@ -600,10 +594,8 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D,
600594
// Fill in the struct *after* computing the base type. Filling in the body
601595
// signifies that the type is no longer opaque and record layout is complete,
602596
// but we may need to recursively layout D while laying D out as a base type.
603-
*Ty = mlir::cir::StructType::get(
604-
&getMLIRContext(), builder.fieldTypes, identifier,
605-
/*body=*/true, /**packed=*/false,
606-
mlir::cir::ASTRecordDeclAttr::get(&getMLIRContext(), D));
597+
*Ty = Builder.getStructTy(builder.fieldTypes, getRecordTypeName(D, ""),
598+
/*body=*/true, /*packed=*/false, D);
607599

608600
auto RL = std::make_unique<CIRGenRecordLayout>(
609601
Ty ? *Ty : mlir::cir::StructType{},

clang/lib/CIR/CodeGen/ConstantInitBuilder.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,6 @@ static mlir::cir::ConstArrayAttr getConstArray(mlir::Attribute attrs,
277277
mlir::cir::ArrayType arrayTy) {
278278
return mlir::cir::ConstArrayAttr::get(arrayTy, attrs);
279279
}
280-
static mlir::Attribute getAnonConstStruct(mlir::ArrayAttr arrayAttr,
281-
bool packed = false) {
282-
assert(!packed && "NYI");
283-
llvm::SmallVector<mlir::Type, 4> members;
284-
for (auto &f : arrayAttr) {
285-
auto ta = f.dyn_cast<mlir::TypedAttr>();
286-
assert(ta && "expected typed attribute member");
287-
members.push_back(ta.getType());
288-
}
289-
auto sTy = mlir::cir::StructType::get(arrayAttr.getContext(), members, "",
290-
/*body=*/true);
291-
return mlir::cir::ConstStructAttr::get(sTy, arrayAttr);
292-
}
293280

294281
mlir::Attribute ConstantAggregateBuilderBase::finishArray(mlir::Type eltTy) {
295282
markFinished();
@@ -323,8 +310,6 @@ ConstantAggregateBuilderBase::finishStruct(mlir::MLIRContext *ctx,
323310

324311
if (ty == nullptr && elts.empty()) {
325312
llvm_unreachable("NYI");
326-
// ty = mlir::cir::StructType::get(Builder.CGM.getLLVMContext(), {},
327-
// Packed);
328313
}
329314

330315
mlir::Attribute constant;
@@ -333,8 +318,8 @@ ConstantAggregateBuilderBase::finishStruct(mlir::MLIRContext *ctx,
333318
// assert(ty->isPacked() == Packed);
334319
// constant = llvm::ConstantStruct::get(ty, elts);
335320
} else {
336-
assert(!Packed && "NYI");
337-
constant = getAnonConstStruct(mlir::ArrayAttr::get(ctx, elts), Packed);
321+
const auto members = mlir::ArrayAttr::get(ctx, elts);
322+
constant = Builder.CGM.getBuilder().getAnonConstStruct(members, Packed);
338323
}
339324

340325
buffer.erase(buffer.begin() + Begin, buffer.end());

0 commit comments

Comments
 (0)