Skip to content

Commit 22ee837

Browse files
authored
[flang][NFC] do not copy fields in fir::RecordType::getTypeList (#145530)
For historical reason, `fir::RecordType::getTypeList` was returning an std::vector, causing the entire field list to be copied when called. It is called a lot indirectly in all type helpers, which themselves are called a lot in derived type heavy code like WRF. The `fir::hasDynamicType` helper is also called a lot, and it can just check for length parameters to avoid looping on all derived type components in most cases.
1 parent 5d2ece1 commit 22ee837

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

flang/include/flang/Optimizer/Dialect/FIRTypes.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ def fir_RecordType : FIR_Type<"Record", "type"> {
330330

331331
let extraClassDeclaration = [{
332332
using TypePair = std::pair<std::string, mlir::Type>;
333-
using TypeList = std::vector<TypePair>;
333+
using TypeList = llvm::ArrayRef<TypePair>;
334+
using TypeVector = llvm::SmallVector<TypePair>;
334335
TypeList getTypeList() const;
335336
TypeList getLenParamList() const;
336337

flang/lib/Optimizer/Dialect/FIRType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t) {
261261
}
262262

263263
static bool hasDynamicSize(fir::RecordType recTy) {
264+
if (recTy.getLenParamList().empty())
265+
return false;
264266
for (auto field : recTy.getTypeList()) {
265267
if (auto arr = mlir::dyn_cast<fir::SequenceType>(field.second)) {
266268
if (sequenceWithNonConstantShape(arr))
@@ -1006,7 +1008,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
10061008
return {};
10071009
RecordType result = RecordType::get(parser.getContext(), name);
10081010

1009-
RecordType::TypeList lenParamList;
1011+
RecordType::TypeVector lenParamList;
10101012
if (!parser.parseOptionalLParen()) {
10111013
while (true) {
10121014
llvm::StringRef lenparam;
@@ -1024,7 +1026,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
10241026
return {};
10251027
}
10261028

1027-
RecordType::TypeList typeList;
1029+
RecordType::TypeVector typeList;
10281030
if (!parser.parseOptionalLess()) {
10291031
result.pack(true);
10301032
}

flang/test/Fir/convert-to-llvm.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,8 @@ func.func private @custom_typeP.field_1.offset() -> i32
18171817
func.func private @custom_typeP.field_2.offset() -> i32
18181818

18191819
func.func @field_index_dynamic_size() -> () {
1820-
%1 = fir.field_index field_1, !fir.type<custom_type{field_1:i32, field_2:!fir.array<?xf32>}>
1821-
%2 = fir.field_index field_2, !fir.type<custom_type{field_1:i32, field_2:!fir.array<?xf32>}>
1820+
%1 = fir.field_index field_1, !fir.type<custom_type(l:i32){field_1:i32, field_2:!fir.array<?xf32>}>
1821+
%2 = fir.field_index field_2, !fir.type<custom_type(l:i32){field_1:i32, field_2:!fir.array<?xf32>}>
18221822
return
18231823
}
18241824

0 commit comments

Comments
 (0)