[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.
This commit is contained in:
jeanPerier
2025-06-25 11:51:07 +02:00
committed by GitHub
parent 5d2ece1437
commit 22ee837ec0
3 changed files with 8 additions and 5 deletions

View File

@@ -330,7 +330,8 @@ def fir_RecordType : FIR_Type<"Record", "type"> {
let extraClassDeclaration = [{ let extraClassDeclaration = [{
using TypePair = std::pair<std::string, mlir::Type>; using TypePair = std::pair<std::string, mlir::Type>;
using TypeList = std::vector<TypePair>; using TypeList = llvm::ArrayRef<TypePair>;
using TypeVector = llvm::SmallVector<TypePair>;
TypeList getTypeList() const; TypeList getTypeList() const;
TypeList getLenParamList() const; TypeList getLenParamList() const;

View File

@@ -261,6 +261,8 @@ mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t) {
} }
static bool hasDynamicSize(fir::RecordType recTy) { static bool hasDynamicSize(fir::RecordType recTy) {
if (recTy.getLenParamList().empty())
return false;
for (auto field : recTy.getTypeList()) { for (auto field : recTy.getTypeList()) {
if (auto arr = mlir::dyn_cast<fir::SequenceType>(field.second)) { if (auto arr = mlir::dyn_cast<fir::SequenceType>(field.second)) {
if (sequenceWithNonConstantShape(arr)) if (sequenceWithNonConstantShape(arr))
@@ -1006,7 +1008,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
return {}; return {};
RecordType result = RecordType::get(parser.getContext(), name); RecordType result = RecordType::get(parser.getContext(), name);
RecordType::TypeList lenParamList; RecordType::TypeVector lenParamList;
if (!parser.parseOptionalLParen()) { if (!parser.parseOptionalLParen()) {
while (true) { while (true) {
llvm::StringRef lenparam; llvm::StringRef lenparam;
@@ -1024,7 +1026,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
return {}; return {};
} }
RecordType::TypeList typeList; RecordType::TypeVector typeList;
if (!parser.parseOptionalLess()) { if (!parser.parseOptionalLess()) {
result.pack(true); result.pack(true);
} }

View File

@@ -1817,8 +1817,8 @@ func.func private @custom_typeP.field_1.offset() -> i32
func.func private @custom_typeP.field_2.offset() -> i32 func.func private @custom_typeP.field_2.offset() -> i32
func.func @field_index_dynamic_size() -> () { func.func @field_index_dynamic_size() -> () {
%1 = fir.field_index field_1, !fir.type<custom_type{field_1:i32, field_2:!fir.array<?xf32>}> %1 = fir.field_index field_1, !fir.type<custom_type(l:i32){field_1:i32, field_2:!fir.array<?xf32>}>
%2 = fir.field_index field_2, !fir.type<custom_type{field_1:i32, field_2:!fir.array<?xf32>}> %2 = fir.field_index field_2, !fir.type<custom_type(l:i32){field_1:i32, field_2:!fir.array<?xf32>}>
return return
} }