================
@@ -702,6 +745,157 @@ void OmpStructureChecker::CheckTraitSimd(
   }
 }
 
+void OmpStructureChecker::CollectMetadirectiveConstructSelectors(
+    const parser::ProgramUnit &programUnit) {
+  metadirectiveConstructSelectors_.clear();
+  MetadirectiveConstructSelectorCollector collector{
+      metadirectiveConstructSelectors_};
+  parser::Walk(programUnit, collector);
+}
+
+OmpStructureChecker::ConstructTraitSequence
+OmpStructureChecker::GetConstructTraitsForPath(
+    const EffectiveDirectivePath &path) const {
+  ConstructTraitSequence constructTraits;
+  for (auto directive{path.rbegin()}; directive != path.rend(); ++directive) {
+    // The construct trait set starts at the innermost target construct.
+    if (llvm::omp::allTargetSet.test(*directive)) {
+      constructTraits.clear();
+    }
+    llvm::omp::VariantMatchInfo directiveVMI;
+    AppendConstructTraitsForDirective(*directive, directiveVMI);
+    constructTraits.append(directiveVMI.ConstructTraits.begin(),
+        directiveVMI.ConstructTraits.end());
+  }
+  return constructTraits;
+}
+
+llvm::SmallVector<OmpStructureChecker::EffectiveDirectivePath, 4>
+OmpStructureChecker::GetUniqueEffectiveDirectivePaths(
+    llvm::SmallVector<EffectiveDirectivePath, 4> paths) const {
+  if (paths.size() < 2) {
+    return paths;
+  }
+
+  auto getSignature = [&](const EffectiveDirectivePath &path) {
+    ConstructTraitSequence contextTraits{GetConstructTraitsForPath(path)};
+
+    // Matching depends on trait presence and ordered match positions. Retain
+    // matches after a failure for match_any scoring, as well as successful
+    // prefixes for matching after inner directives are appended.
+    std::vector<unsigned> signature;
+    // Device scores depend on context depth even without construct selectors.
+    signature.push_back(contextTraits.size());
+    for (const ConstructTraitSequence &selector :
+        metadirectiveConstructSelectors_) {
+      for (llvm::omp::TraitProperty property : selector) {
+        signature.push_back(llvm::is_contained(contextTraits, property));
+      }
+
+      std::size_t contextIndex{0};
+      for (llvm::omp::TraitProperty property : selector) {
+        std::size_t searchStart{contextIndex};
+        while (contextIndex < contextTraits.size() &&
+            contextTraits[contextIndex] != property) {
+          ++contextIndex;
+        }
+        if (contextIndex == contextTraits.size()) {
+          signature.push_back(0);
+          // Like match_any, skip absent traits without consuming the context.
+          contextIndex = searchStart;
+        } else {
+          // Reserve zero for an unmatched property.
+          signature.push_back(++contextIndex);
+        }
+      }
+
+      // Scoring uses the highest-valued complete ordered match. Retain that
+      // match for every selector prefix, since appended inner constructs can
+      // complete a selector that does not yet match the current context.
+      for (std::size_t prefixSize{1}; prefixSize <= selector.size();
+          ++prefixSize) {
+        contextIndex = contextTraits.size();
+        for (std::size_t i{prefixSize}; i > 0; --i) {
+          while (contextIndex > 0 &&
+              contextTraits[contextIndex - 1] != selector[i - 1]) {
+            --contextIndex;
+          }
+          signature.push_back(contextIndex);
+          if (contextIndex > 0) {
+            --contextIndex;
+          }
+        }
+      }
+    }
+    return signature;
+  };
+
+  std::set<std::vector<unsigned>> signatures;
+  llvm::SmallVector<EffectiveDirectivePath, 4> uniquePaths;
+  uniquePaths.reserve(paths.size());
+  for (EffectiveDirectivePath &path : paths) {
+    if (signatures.insert(getSignature(path)).second) {
+      uniquePaths.push_back(std::move(path));
+    }
+  }
+  return uniquePaths;
+}
+
+llvm::SmallVector<OmpStructureChecker::MetadirectiveReplacementBranch, 4>
+OmpStructureChecker::GetReachableMetadirectiveReplacements(
+    const parser::OmpClauseList &clauses) {
+  llvm::SmallVector<MetadirectiveReplacementBranch, 4> result;
+
+  for (const EffectiveDirectivePath &path : GetEnclosingDirectivePaths()) {
+    ConstructTraitSequence constructTraits{GetConstructTraitsForPath(path)};
+    OmpVariantMatchContext matchContext{context_, constructTraits};
----------------
MattPD wrote:

Fixed at 5d059516: The reproducer in this thread now reports the COLLAPSE 
diagnostic under `bbc`.

https://github.com/llvm/llvm-project/pull/219014
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to