<div dir="ltr">Looks like this fails on Windows: <a href="http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/11207/steps/stage%201%20check/logs/stdio">http://lab.llvm.org:8011/builders/clang-x64-windows-msvc/builds/11207/steps/stage%201%20check/logs/stdio</a></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 3, 2019 at 4:47 PM Alexey Bataev via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: abataev<br>
Date: Thu Oct  3 13:49:48 2019<br>
New Revision: 373661<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=373661&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=373661&view=rev</a><br>
Log:<br>
[OPENMP50]Codegen support for scores in context selectors.<br>
<br>
If the context selector has associated score and several contexts<br>
selectors matches current context, the function with the highest score<br>
must be selected.<br>
<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp<br>
    cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp?rev=373661&r1=373660&r2=373661&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp?rev=373661&r1=373660&r2=373661&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGOpenMPRuntime.cpp Thu Oct  3 13:49:48 2019<br>
@@ -11175,37 +11175,87 @@ bool checkContext<OMPDeclareVariantAttr:<br>
   return !A->getImplVendor().compare("llvm");<br>
 }<br>
<br>
+static bool greaterCtxScore(ASTContext &Ctx, const Expr *LHS, const Expr *RHS) {<br>
+  // If both scores are unknown, choose the very first one.<br>
+  if (!LHS && !RHS)<br>
+    return true;<br>
+  // If only one is known, return this one.<br>
+  if (LHS && !RHS)<br>
+    return true;<br>
+  if (!LHS && RHS)<br>
+    return false;<br>
+  llvm::APSInt LHSVal = LHS->EvaluateKnownConstInt(Ctx);<br>
+  llvm::APSInt RHSVal = RHS->EvaluateKnownConstInt(Ctx);<br>
+  return llvm::APSInt::compareValues(LHSVal, RHSVal) <= 0;<br>
+}<br>
+<br>
+namespace {<br>
+/// Comparator for the priority queue for context selector.<br>
+class OMPDeclareVariantAttrComparer<br>
+    : public std::greater<const OMPDeclareVariantAttr *> {<br>
+private:<br>
+  ASTContext &Ctx;<br>
+<br>
+public:<br>
+  OMPDeclareVariantAttrComparer(ASTContext &Ctx) : Ctx(Ctx) {}<br>
+  bool operator()(const OMPDeclareVariantAttr *LHS,<br>
+                  const OMPDeclareVariantAttr *RHS) const {<br>
+    const Expr *LHSExpr = nullptr;<br>
+    const Expr *RHSExpr = nullptr;<br>
+    if (LHS->getCtxScore() == OMPDeclareVariantAttr::ScoreSpecified)<br>
+      LHSExpr = LHS->getScore();<br>
+    if (RHS->getCtxScore() == OMPDeclareVariantAttr::ScoreSpecified)<br>
+      RHSExpr = RHS->getScore();<br>
+    return greaterCtxScore(Ctx, LHSExpr, RHSExpr);<br>
+  }<br>
+};<br>
+} // anonymous namespace<br>
+<br>
 /// Finds the variant function that matches current context with its context<br>
 /// selector.<br>
-static const FunctionDecl *getDeclareVariantFunction(const FunctionDecl *FD) {<br>
+static const FunctionDecl *getDeclareVariantFunction(ASTContext &Ctx,<br>
+                                                     const FunctionDecl *FD) {<br>
   if (!FD->hasAttrs() || !FD->hasAttr<OMPDeclareVariantAttr>())<br>
     return FD;<br>
   // Iterate through all DeclareVariant attributes and check context selectors.<br>
-  SmallVector<const OMPDeclareVariantAttr *, 4> MatchingAttributes;<br>
-  for (const auto * A : FD->specific_attrs<OMPDeclareVariantAttr>()) {<br>
+  auto &&Comparer = [&Ctx](const OMPDeclareVariantAttr *LHS,<br>
+                           const OMPDeclareVariantAttr *RHS) {<br>
+    const Expr *LHSExpr = nullptr;<br>
+    const Expr *RHSExpr = nullptr;<br>
+    if (LHS->getCtxScore() == OMPDeclareVariantAttr::ScoreSpecified)<br>
+      LHSExpr = LHS->getScore();<br>
+    if (RHS->getCtxScore() == OMPDeclareVariantAttr::ScoreSpecified)<br>
+      RHSExpr = RHS->getScore();<br>
+    return greaterCtxScore(Ctx, LHSExpr, RHSExpr);<br>
+  };<br>
+  const OMPDeclareVariantAttr *TopMostAttr = nullptr;<br>
+  for (const auto *A : FD->specific_attrs<OMPDeclareVariantAttr>()) {<br>
+    const OMPDeclareVariantAttr *SelectedAttr = nullptr;<br>
     switch (A->getCtxSelectorSet()) {<br>
     case OMPDeclareVariantAttr::CtxSetImplementation:<br>
       switch (A->getCtxSelector()) {<br>
       case OMPDeclareVariantAttr::CtxVendor:<br>
         if (checkContext<OMPDeclareVariantAttr::CtxSetImplementation,<br>
                          OMPDeclareVariantAttr::CtxVendor>(A))<br>
-          MatchingAttributes.push_back(A);<br>
+          SelectedAttr = A;<br>
         break;<br>
       case OMPDeclareVariantAttr::CtxUnknown:<br>
         llvm_unreachable(<br>
-            "Unknown context selector in implementation selctor set.");<br>
+            "Unknown context selector in implementation selector set.");<br>
       }<br>
       break;<br>
     case OMPDeclareVariantAttr::CtxSetUnknown:<br>
       llvm_unreachable("Unknown context selector set.");<br>
     }<br>
+    // If the attribute matches the context, find the attribute with the highest<br>
+    // score.<br>
+    if (SelectedAttr && (!TopMostAttr || Comparer(TopMostAttr, SelectedAttr)))<br>
+      TopMostAttr = SelectedAttr;<br>
   }<br>
-  if (MatchingAttributes.empty())<br>
+  if (!TopMostAttr)<br>
     return FD;<br>
-  // TODO: implement score analysis of multiple context selectors.<br>
-  const OMPDeclareVariantAttr *MainAttr = MatchingAttributes.front();<br>
   return cast<FunctionDecl>(<br>
-      cast<DeclRefExpr>(MainAttr->getVariantFuncRef()->IgnoreParenImpCasts())<br>
+      cast<DeclRefExpr>(TopMostAttr->getVariantFuncRef()->IgnoreParenImpCasts())<br>
           ->getDecl());<br>
 }<br>
<br>
@@ -11216,7 +11266,7 @@ bool CGOpenMPRuntime::emitDeclareVariant<br>
   llvm::GlobalValue *Orig = CGM.GetGlobalValue(MangledName);<br>
   if (Orig && !Orig->isDeclaration())<br>
     return false;<br>
-  const FunctionDecl *NewFD = getDeclareVariantFunction(D);<br>
+  const FunctionDecl *NewFD = getDeclareVariantFunction(CGM.getContext(), D);<br>
   // Emit original function if it does not have declare variant attribute or the<br>
   // context does not match.<br>
   if (NewFD == D)<br>
<br>
Modified: cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp?rev=373661&r1=373660&r2=373661&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp?rev=373661&r1=373660&r2=373661&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp (original)<br>
+++ cfe/trunk/test/OpenMP/declare_variant_implementation_vendor_codegen.cpp Thu Oct  3 13:49:48 2019<br>
@@ -3,11 +3,13 @@<br>
 // RUN: %clang_cc1 -fopenmp -x c++ -triple %itanium_abi_triple -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - -fopenmp-version=50 | FileCheck %s<br>
 // expected-no-diagnostics<br>
<br>
-// CHECK-NOT: ret i32 {{1|4}}<br>
+// CHECK-NOT: ret i32 {{1|4|81|84}}<br>
 // CHECK-DAG: @_Z3barv = {{.*}}alias i32 (), i32 ()* @_Z3foov<br>
 // CHECK-DAG: @_ZN16SpecSpecialFuncs6MethodEv = {{.*}}alias i32 (%struct.SpecSpecialFuncs*), i32 (%struct.SpecSpecialFuncs*)* @_ZN16SpecSpecialFuncs7method_Ev<br>
 // CHECK-DAG: @_ZN16SpecSpecialFuncs6methodEv = linkonce_odr {{.*}}alias i32 (%struct.SpecSpecialFuncs*), i32 (%struct.SpecSpecialFuncs*)* @_ZN16SpecSpecialFuncs7method_Ev<br>
 // CHECK-DAG: @_ZN12SpecialFuncs6methodEv = linkonce_odr {{.*}}alias i32 (%struct.SpecialFuncs*), i32 (%struct.SpecialFuncs*)* @_ZN12SpecialFuncs7method_Ev<br>
+// CHECK-DAG: @_Z5prio_v = alias i32 (), i32 ()* @_Z4priov<br>
+// CHECK-DAG: @_ZL6prio1_v = internal alias i32 (), i32 ()* @_ZL5prio2v<br>
 // CHECK-DAG: @_Z4callv = {{.*}}alias i32 (), i32 ()* @_Z4testv<br>
 // CHECK-DAG: @_ZL9stat_usedv = internal alias i32 (), i32 ()* @_ZL10stat_used_v<br>
 // CHECK-DAG: @_ZN12SpecialFuncs6MethodEv =  {{.*}}alias i32 (%struct.SpecialFuncs*), i32 (%struct.SpecialFuncs*)* @_ZN12SpecialFuncs7method_Ev<br>
@@ -18,7 +20,9 @@<br>
 // CHECK-DAG: ret i32 5<br>
 // CHECK-DAG: ret i32 6<br>
 // CHECK-DAG: ret i32 7<br>
-// CHECK-NOT: ret i32 {{1|4}}<br>
+// CHECK-DAG: ret i32 82<br>
+// CHECK-DAG: ret i32 83<br>
+// CHECK-NOT: ret i32 {{1|4|81|84}}<br>
<br>
 #ifndef HEADER<br>
 #define HEADER<br>
@@ -88,4 +92,22 @@ void xxx() {<br>
   (void)s1.method();<br>
 }<br>
<br>
+int prio() { return 81; }<br>
+int prio1() { return 82; }<br>
+<br>
+#pragma omp declare variant(prio) match(implementation = {vendor(llvm)})<br>
+#pragma omp declare variant(prio1) match(implementation = {vendor(score(1): llvm)})<br>
+int prio_() { return 1; }<br>
+<br>
+static int prio2() { return 83; }<br>
+static int prio3() { return 84; }<br>
+static int prio4() { return 84; }<br>
+<br>
+#pragma omp declare variant(prio4) match(implementation = {vendor(score(3): llvm)})<br>
+#pragma omp declare variant(prio2) match(implementation = {vendor(score(5): llvm)})<br>
+#pragma omp declare variant(prio3) match(implementation = {vendor(score(1): llvm)})<br>
+static int prio1_() { return 1; }<br>
+<br>
+int int_fn() { return prio1_(); }<br>
+<br>
 #endif // HEADER<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>