Skip to content

[flang] Use llvm::any_of and llvm::none_of (NFC) #102797

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 1 commit into from
Aug 12, 2024

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Aug 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 11, 2024

@llvm/pr-subscribers-flang-semantics

Author: Kazu Hirata (kazutakahirata)

Changes

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

4 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp (+2-3)
  • (modified) flang/lib/Semantics/data-to-inits.cpp (+4-5)
  • (modified) flang/lib/Semantics/rewrite-directives.cpp (+3-5)
  • (modified) flang/lib/Semantics/symbol.cpp (+4-5)
diff --git a/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp b/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
index 7d0b8b3d7bc53d..1e44288c784c2a 100644
--- a/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
+++ b/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
@@ -98,9 +98,8 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
       assert(!builder.getNamedGlobal(globalName) &&
              "We should have a unique name here");
 
-      if (std::find_if(allocas.begin(), allocas.end(), [alloca](auto x) {
-            return x.first == alloca;
-          }) == allocas.end()) {
+      if (llvm::none_of(allocas,
+                        [alloca](auto x) { return x.first == alloca; })) {
         allocas.push_back(std::make_pair(alloca, store));
       }
 
diff --git a/flang/lib/Semantics/data-to-inits.cpp b/flang/lib/Semantics/data-to-inits.cpp
index b6dbbbf63138ec..0f8b36de460865 100644
--- a/flang/lib/Semantics/data-to-inits.cpp
+++ b/flang/lib/Semantics/data-to-inits.cpp
@@ -522,11 +522,10 @@ static const DerivedTypeSpec *HasDefaultInitialization(const Symbol &symbol) {
     } else if (!object->isDummy() && object->type()) {
       if (const DerivedTypeSpec * derived{object->type()->AsDerived()}) {
         DirectComponentIterator directs{*derived};
-        if (std::find_if(
-                directs.begin(), directs.end(), [](const Symbol &component) {
-                  return !IsAllocatable(component) &&
-                      HasDeclarationInitializer(component);
-                }) != directs.end()) {
+        if (llvm::any_of(directs, [](const Symbol &component) {
+              return !IsAllocatable(component) &&
+                  HasDeclarationInitializer(component);
+            })) {
           return derived;
         }
       }
diff --git a/flang/lib/Semantics/rewrite-directives.cpp b/flang/lib/Semantics/rewrite-directives.cpp
index 2c3c87f2546a35..c94d0f3855bee3 100644
--- a/flang/lib/Semantics/rewrite-directives.cpp
+++ b/flang/lib/Semantics/rewrite-directives.cpp
@@ -88,11 +88,9 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {
 
   auto findMemOrderClause =
       [](const std::list<parser::OmpAtomicClause> &clauses) {
-        return std::find_if(
-                   clauses.begin(), clauses.end(), [](const auto &clause) {
-                     return std::get_if<parser::OmpMemoryOrderClause>(
-                         &clause.u);
-                   }) != clauses.end();
+        return llvm::any_of(clauses, [](const auto &clause) {
+          return std::get_if<parser::OmpMemoryOrderClause>(&clause.u);
+        });
       };
 
   // Get the clause list to which the new memory order clause must be added,
diff --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp
index 31e91ee7355e33..b593bf89b18bc9 100644
--- a/flang/lib/Semantics/symbol.cpp
+++ b/flang/lib/Semantics/symbol.cpp
@@ -230,11 +230,10 @@ void GenericDetails::CopyFrom(const GenericDetails &from) {
     derivedType_ = from.derivedType_;
   }
   for (std::size_t i{0}; i < from.specificProcs_.size(); ++i) {
-    if (std::find_if(specificProcs_.begin(), specificProcs_.end(),
-            [&](const Symbol &mySymbol) {
-              return &mySymbol.GetUltimate() ==
-                  &from.specificProcs_[i]->GetUltimate();
-            }) == specificProcs_.end()) {
+    if (llvm::none_of(specificProcs_, [&](const Symbol &mySymbol) {
+          return &mySymbol.GetUltimate() ==
+              &from.specificProcs_[i]->GetUltimate();
+        })) {
       specificProcs_.push_back(from.specificProcs_[i]);
       bindingNames_.push_back(from.bindingNames_[i]);
     }

@llvmbot
Copy link
Member

llvmbot commented Aug 11, 2024

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

Author: Kazu Hirata (kazutakahirata)

Changes

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

4 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp (+2-3)
  • (modified) flang/lib/Semantics/data-to-inits.cpp (+4-5)
  • (modified) flang/lib/Semantics/rewrite-directives.cpp (+3-5)
  • (modified) flang/lib/Semantics/symbol.cpp (+4-5)
diff --git a/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp b/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
index 7d0b8b3d7bc53d..1e44288c784c2a 100644
--- a/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
+++ b/flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp
@@ -98,9 +98,8 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
       assert(!builder.getNamedGlobal(globalName) &&
              "We should have a unique name here");
 
-      if (std::find_if(allocas.begin(), allocas.end(), [alloca](auto x) {
-            return x.first == alloca;
-          }) == allocas.end()) {
+      if (llvm::none_of(allocas,
+                        [alloca](auto x) { return x.first == alloca; })) {
         allocas.push_back(std::make_pair(alloca, store));
       }
 
diff --git a/flang/lib/Semantics/data-to-inits.cpp b/flang/lib/Semantics/data-to-inits.cpp
index b6dbbbf63138ec..0f8b36de460865 100644
--- a/flang/lib/Semantics/data-to-inits.cpp
+++ b/flang/lib/Semantics/data-to-inits.cpp
@@ -522,11 +522,10 @@ static const DerivedTypeSpec *HasDefaultInitialization(const Symbol &symbol) {
     } else if (!object->isDummy() && object->type()) {
       if (const DerivedTypeSpec * derived{object->type()->AsDerived()}) {
         DirectComponentIterator directs{*derived};
-        if (std::find_if(
-                directs.begin(), directs.end(), [](const Symbol &component) {
-                  return !IsAllocatable(component) &&
-                      HasDeclarationInitializer(component);
-                }) != directs.end()) {
+        if (llvm::any_of(directs, [](const Symbol &component) {
+              return !IsAllocatable(component) &&
+                  HasDeclarationInitializer(component);
+            })) {
           return derived;
         }
       }
diff --git a/flang/lib/Semantics/rewrite-directives.cpp b/flang/lib/Semantics/rewrite-directives.cpp
index 2c3c87f2546a35..c94d0f3855bee3 100644
--- a/flang/lib/Semantics/rewrite-directives.cpp
+++ b/flang/lib/Semantics/rewrite-directives.cpp
@@ -88,11 +88,9 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {
 
   auto findMemOrderClause =
       [](const std::list<parser::OmpAtomicClause> &clauses) {
-        return std::find_if(
-                   clauses.begin(), clauses.end(), [](const auto &clause) {
-                     return std::get_if<parser::OmpMemoryOrderClause>(
-                         &clause.u);
-                   }) != clauses.end();
+        return llvm::any_of(clauses, [](const auto &clause) {
+          return std::get_if<parser::OmpMemoryOrderClause>(&clause.u);
+        });
       };
 
   // Get the clause list to which the new memory order clause must be added,
diff --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp
index 31e91ee7355e33..b593bf89b18bc9 100644
--- a/flang/lib/Semantics/symbol.cpp
+++ b/flang/lib/Semantics/symbol.cpp
@@ -230,11 +230,10 @@ void GenericDetails::CopyFrom(const GenericDetails &from) {
     derivedType_ = from.derivedType_;
   }
   for (std::size_t i{0}; i < from.specificProcs_.size(); ++i) {
-    if (std::find_if(specificProcs_.begin(), specificProcs_.end(),
-            [&](const Symbol &mySymbol) {
-              return &mySymbol.GetUltimate() ==
-                  &from.specificProcs_[i]->GetUltimate();
-            }) == specificProcs_.end()) {
+    if (llvm::none_of(specificProcs_, [&](const Symbol &mySymbol) {
+          return &mySymbol.GetUltimate() ==
+              &from.specificProcs_[i]->GetUltimate();
+        })) {
       specificProcs_.push_back(from.specificProcs_[i]);
       bindingNames_.push_back(from.bindingNames_[i]);
     }

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

LGTM

@kazutakahirata kazutakahirata merged commit 8470cdd into llvm:main Aug 12, 2024
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_flang_any_of branch August 12, 2024 17:24
Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

Why is this a good change? Replacing standard C++ with local idioms doesn't improve readability.

@kazutakahirata
Copy link
Contributor Author

Why is this a good change? Replacing standard C++ with local idioms doesn't improve readability.

The change avoids repeating the same container names multiple times. Your comment about the local idiom is well taken, but we can also look at it as using std::ranges::none_of ahead of time before C++20 is available in our codebase. Many utility functions in llvm/include/llvm/ADT/{STLExtras.h,STLForwardCompat.h,bit.h} offer such functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants