[flang-commits] [flang] [flang][NFC] do not copy fields in fir::RecordType::getTypeList (PR #145530)

via flang-commits flang-commits at lists.llvm.org
Tue Jun 24 08:17:49 PDT 2025


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/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 fix should speed-up WRF compilation by 10-30%.

>From bb2744fb229edcf8a263a904b7b4ab117a9ff864 Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Tue, 24 Jun 2025 08:09:02 -0700
Subject: [PATCH] [flang][NFC] do not copy fields in
 fir::RecordType::getTypeList

---
 flang/include/flang/Optimizer/Dialect/FIRTypes.td | 3 ++-
 flang/lib/Optimizer/Dialect/FIRType.cpp           | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/flang/include/flang/Optimizer/Dialect/FIRTypes.td b/flang/include/flang/Optimizer/Dialect/FIRTypes.td
index 6fad77dffd9bc..0ead54df3ca97 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRTypes.td
+++ b/flang/include/flang/Optimizer/Dialect/FIRTypes.td
@@ -330,7 +330,8 @@ def fir_RecordType : FIR_Type<"Record", "type"> {
 
   let extraClassDeclaration = [{
     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 getLenParamList() const;
 
diff --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp
index 78571f1f4bc2d..2ff1d6d945ba3 100644
--- a/flang/lib/Optimizer/Dialect/FIRType.cpp
+++ b/flang/lib/Optimizer/Dialect/FIRType.cpp
@@ -261,6 +261,8 @@ mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t) {
 }
 
 static bool hasDynamicSize(fir::RecordType recTy) {
+  if (recTy.getLenParamList().empty())
+    return false;
   for (auto field : recTy.getTypeList()) {
     if (auto arr = mlir::dyn_cast<fir::SequenceType>(field.second)) {
       if (sequenceWithNonConstantShape(arr))
@@ -1006,7 +1008,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
     return {};
   RecordType result = RecordType::get(parser.getContext(), name);
 
-  RecordType::TypeList lenParamList;
+  RecordType::TypeVector lenParamList;
   if (!parser.parseOptionalLParen()) {
     while (true) {
       llvm::StringRef lenparam;
@@ -1024,7 +1026,7 @@ mlir::Type fir::RecordType::parse(mlir::AsmParser &parser) {
       return {};
   }
 
-  RecordType::TypeList typeList;
+  RecordType::TypeVector typeList;
   if (!parser.parseOptionalLess()) {
     result.pack(true);
   }



More information about the flang-commits mailing list