[llvm] [flang-rt] - Reduce ShallowCopy template instantiations to improve LTO time (PR #209915)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 16 04:00:51 PDT 2026


================
@@ -201,37 +201,36 @@ RT_API_ATTRS void ShallowCopyInner(const Descriptor &to, const Descriptor &from,
   }
 }
 
-// Most arrays are much closer to rank-1 than to maxRank.
-// Doing the recursion upwards instead of downwards puts the more common
-// cases earlier in the if-chain and has a tangible impact on performance.
-template <typename P, int RANK> struct ShallowCopyRankSpecialize {
-  static RT_API_ATTRS bool execute(const Descriptor &to, const Descriptor &from,
-      bool toIsContiguous, bool fromIsContiguous) {
-    if (to.rank() == RANK && from.rank() == RANK) {
-      ShallowCopyInner<P, RANK>(to, from, toIsContiguous, fromIsContiguous);
-      return true;
-    }
-    return ShallowCopyRankSpecialize<P, RANK + 1>::execute(
-        to, from, toIsContiguous, fromIsContiguous);
-  }
-};
-
-template <typename P> struct ShallowCopyRankSpecialize<P, maxRank + 1> {
-  static RT_API_ATTRS bool execute(const Descriptor &to, const Descriptor &from,
-      bool toIsContiguous, bool fromIsContiguous) {
-    return false;
-  }
-};
+// Specialize only for common ranks (1-4) to reduce code size.
+// Higher ranks use the generic fallback which handles any rank at runtime.
+// Most real-world Fortran arrays are rank 1-3; rank 4+ is rare.
+// This trades a small amount of potential optimization for high-rank arrays
+// in exchange for significantly reduced code size (~60% reduction in
+// ShallowCopy template instantiations).
----------------
Meinersbur wrote:

This comment is written like a commit message. Someone who reads this code does not know the code used to specialize for all 15 ranks.

Since this is an implementation detail (i.e. someone who just wants to call `ShallowCopyRank` does not need to know this), and it basically already mentioned in the comments of the switch, I'd just remove this.

https://github.com/llvm/llvm-project/pull/209915


More information about the llvm-commits mailing list