[PATCH] D96519: [Clang][OpenMP] Fixed an issue that `target team` is emitted incorrectly

Shilei Tian via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 11 09:08:58 PST 2021


tianshilei1992 created this revision.
tianshilei1992 added reviewers: jdoerfert, JonChesterfield, ABataev, grokos.
Herald added subscribers: guansong, yaxunl.
tianshilei1992 requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

This patch is trying to fix the bug 47039 (https://bugs.llvm.org/show_bug.cgi?id=47039).
The root cause is, in clang, if `NumTeams` is not `nullptr`, it will emit call
to `target teams`, even if the value of `NumTeams` is 0. This is wrong, but at
least the team number is 0, so if the runtime can handle this correct, it should
still be fine. However, in the runtime, we only check the boolean variable
`IsTeamConstruct`, and don't check the team number. The last straw is, in CUDA
plugin, if the team number is 0, and the loop count is also 0, then it will set
the team number to a default value, which is 128 now.

In this patch, instead of emitting `TeamNum` of value 0, we emit `nullptr`
instead, which is spec conformant at least, aside from the discussion on the
weekly meeting that no team can also mean one team with one thread.

Failed tests will be fixed accordingly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96519

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp


Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6574,24 +6574,20 @@
         CGOpenMPRuntime::getSingleCompoundChild(CGF.getContext(), Body);
     if (const auto *NestedDir =
             dyn_cast_or_null<OMPExecutableDirective>(ChildStmt)) {
-      if (isOpenMPTeamsDirective(NestedDir->getDirectiveKind())) {
-        if (NestedDir->hasClausesOfKind<OMPNumTeamsClause>()) {
-          CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
-          CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
-          const Expr *NumTeams =
-              NestedDir->getSingleClause<OMPNumTeamsClause>()->getNumTeams();
-          llvm::Value *NumTeamsVal =
-              CGF.EmitScalarExpr(NumTeams,
-                                 /*IgnoreResultAssign*/ true);
-          return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty,
-                                   /*isSigned=*/true);
-        }
-        return Bld.getInt32(0);
-      }
-      if (isOpenMPParallelDirective(NestedDir->getDirectiveKind()) ||
-          isOpenMPSimdDirective(NestedDir->getDirectiveKind()))
+      if (isOpenMPTeamsDirective(NestedDir->getDirectiveKind()) &&
+          NestedDir->hasClausesOfKind<OMPNumTeamsClause>()) {
+        CGOpenMPInnerExprInfo CGInfo(CGF, *CS);
+        CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
+        const Expr *NumTeams =
+            NestedDir->getSingleClause<OMPNumTeamsClause>()->getNumTeams();
+        llvm::Value *NumTeamsVal =
+            CGF.EmitScalarExpr(NumTeams,
+                               /*IgnoreResultAssign*/ true);
+        return Bld.CreateIntCast(NumTeamsVal, CGF.Int32Ty,
+                                 /*isSigned=*/true);
+      } else if (isOpenMPParallelDirective(NestedDir->getDirectiveKind()) ||
+                 isOpenMPSimdDirective(NestedDir->getDirectiveKind()))
         return Bld.getInt32(1);
-      return Bld.getInt32(0);
     }
     return nullptr;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96519.323043.patch
Type: text/x-patch
Size: 2082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210211/0a51f279/attachment.bin>


More information about the cfe-commits mailing list