[flang-commits] [flang] [flang][NFC] speedup BoxedProcedure for derived types with many components (PR #86144)

via flang-commits flang-commits at lists.llvm.org
Thu Mar 21 09:10:22 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: None (jeanPerier)

<details>
<summary>Changes</summary>

This patch speeds up the compilation time of the example in https://github.com/llvm/llvm-project/issues/76478#issuecomment-2011023289 from 2 minutes with my builds to about 2 seconds.

MLIR timers showed more than 98% of the time was spend in BoxedProcedure trying to figure out if a type needs to be converted.

This is because walking the fir.type members is very expansive for types containing many components and/or components with many sub-components.

Increase the caching time of visited types from "the type being visited" to "the whole pass". Use DenseMap since it is not ok anymore to assume this container will only have a few elements.

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


1 Files Affected:

- (modified) flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp (+10-9) 


``````````diff
diff --git a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
index 746c275f37eaca..7a1cc3536b263b 100644
--- a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
+++ b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
@@ -69,17 +69,20 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
       return false;
     }
     if (auto recTy = ty.dyn_cast<RecordType>()) {
-      if (llvm::is_contained(visitedTypes, recTy))
-        return false;
+      auto visited = visitedTypes.find(ty);
+      if (visited != visitedTypes.end())
+        return visited->second;
+      [[maybe_unused]] auto newIt = visitedTypes.try_emplace(ty, false);
+      assert(newIt.second && "expected ty to not be in the map");
       bool result = false;
-      visitedTypes.push_back(recTy);
       for (auto t : recTy.getTypeList()) {
         if (needsConversion(t.second)) {
           result = true;
           break;
         }
       }
-      visitedTypes.pop_back();
+      // newIt may have been invalidated.
+      visitedTypes.find(ty)->second = result;
       return result;
     }
     if (auto boxTy = ty.dyn_cast<BaseBoxType>())
@@ -140,9 +143,7 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
       if (rec.isFinalized())
         return rec;
       auto it = convertedTypes.try_emplace(ty, rec);
-      if (!it.second) {
-        llvm::errs() << "failed\n" << ty << "\n";
-      }
+      assert(it.second && "expected ty to not be in the map");
       std::vector<RecordType::TypePair> ps = ty.getLenParamList();
       std::vector<RecordType::TypePair> cs;
       for (auto t : ty.getTypeList()) {
@@ -171,11 +172,11 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
   void setLocation(mlir::Location location) { loc = location; }
 
 private:
-  llvm::SmallVector<mlir::Type> visitedTypes;
-  // Map to deal with recursive derived types (avoid infinite loops).
+  // Maps to deal with recursive derived types (avoid infinite loops).
   // Caching is also beneficial for apps with big types (dozens of
   // components and or parent types), so the lifetime of the cache
   // is the whole pass.
+  llvm::DenseMap<mlir::Type, bool> visitedTypes;
   llvm::DenseMap<mlir::Type, mlir::Type> convertedTypes;
   mlir::Location loc;
 };

``````````

</details>


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


More information about the flang-commits mailing list