Skip to content

Conversation

Saldivarcher
Copy link
Contributor

Going through and doing convertToAttribute for all elements, if they are the same can be costly. If the elements are the same, we can just call convertToAttribute once.

This does give us a significant speed-up:

$ hyperfine --warmup 1 --runs 5 ./slow.sh ./fast.sh
Benchmark 1: ./slow.sh
  Time (mean ± σ):      1.606 s ±  0.014 s    [User: 1.393 s, System: 0.087 s]
  Range (min … max):    1.591 s …  1.628 s    5 runs

Benchmark 2: ./fast.sh
  Time (mean ± σ):     452.9 ms ±   7.6 ms    [User: 249.9 ms, System: 83.3 ms]
  Range (min … max):   443.9 ms … 461.7 ms    5 runs

Summary
  ./fast.sh ran
    3.55 ± 0.07 times faster than ./slow.sh

Fixes #125444

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

llvmbot commented Sep 2, 2025

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

Author: Miguel Saldivar (Saldivarcher)

Changes

Going through and doing convertToAttribute for all elements, if they are the same can be costly. If the elements are the same, we can just call convertToAttribute once.

This does give us a significant speed-up:

$ hyperfine --warmup 1 --runs 5 ./slow.sh ./fast.sh
Benchmark 1: ./slow.sh
  Time (mean ± σ):      1.606 s ±  0.014 s    [User: 1.393 s, System: 0.087 s]
  Range (min … max):    1.591 s …  1.628 s    5 runs

Benchmark 2: ./fast.sh
  Time (mean ± σ):     452.9 ms ±   7.6 ms    [User: 249.9 ms, System: 83.3 ms]
  Range (min … max):   443.9 ms … 461.7 ms    5 runs

Summary
  ./fast.sh ran
    3.55 ± 0.07 times faster than ./slow.sh

Fixes #125444


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

1 Files Affected:

  • (modified) flang/lib/Lower/ConvertConstant.cpp (+17)
diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index 768a237c92396..068c81be7c45d 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -152,6 +152,23 @@ class DenseGlobalBuilder {
                       : TC;
     attributeElementType =
         Fortran::lower::getFIRType(builder.getContext(), attrTc, KIND, {});
+
+    auto values = constant.values();
+    auto sameElements = [&]() -> bool {
+      if (values.empty())
+        return false;
+
+      return std::all_of(values.begin(), values.end(),
+                         [&](const auto &v) { return v == values.front(); });
+    };
+
+    if (sameElements()) {
+      auto attr = convertToAttribute<TC, KIND>(builder, values.front(),
+                                               attributeElementType);
+      attributes.assign(values.size(), attr);
+      return;
+    }
+
     for (auto element : constant.values())
       attributes.push_back(
           convertToAttribute<TC, KIND>(builder, element, attributeElementType));

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thanks for the fix

Going through and doing `convertToAttribute` for all elements, if they
are the same can be costly. If the elements are the same, we can just
call `convertToAttribute` once.

This does give us a significant speed-up:
```console
$ hyperfine --warmup 1 --runs 5 ./slow.sh ./fast.sh
Benchmark 1: ./slow.sh
  Time (mean ± σ):      1.606 s ±  0.014 s    [User: 1.393 s, System: 0.087 s]
  Range (min … max):    1.591 s …  1.628 s    5 runs

Benchmark 2: ./fast.sh
  Time (mean ± σ):     452.9 ms ±   7.6 ms    [User: 249.9 ms, System: 83.3 ms]
  Range (min … max):   443.9 ms … 461.7 ms    5 runs

Summary
  ./fast.sh ran
    3.55 ± 0.07 times faster than ./slow.sh
```

Fixes llvm#125444
Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thanks for the update

@Saldivarcher
Copy link
Contributor Author

@tblah would you be able to merge this for me, please. Thanks again for the review, I appreciate it 👍

@tblah tblah merged commit be1e50f into llvm:main Sep 3, 2025
9 checks passed
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.

[Flang] Initialization of large arrays increases compilation time
3 participants