Skip to content

[flang][NFC] do not copy fields in fir::RecordType::getTypeList #145530

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 2 commits into from
Jun 25, 2025

Conversation

jeanPerier
Copy link
Contributor

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%.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

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%.


Full diff: https://github.com/llvm/llvm-project/pull/145530.diff

2 Files Affected:

  • (modified) flang/include/flang/Optimizer/Dialect/FIRTypes.td (+2-1)
  • (modified) flang/lib/Optimizer/Dialect/FIRType.cpp (+4-2)
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);
   }

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jeanPerier jeanPerier merged commit 22ee837 into llvm:main Jun 25, 2025
7 checks passed
@jeanPerier jeanPerier deleted the speed_up_derived_compilation branch June 25, 2025 09:51
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
…#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants