hliao created this revision.
hliao added a reviewer: yaxunl.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- https://reviews.llvm.org/D68578 revises the `GlobalDecl` constructors to 
ensure all GPU kernels have `ReferenceKenelKind` initialized properly with an 
explicit constructor and static one. But, there are lots of places using the 
implicit constructor triggering the assertion on non-GPU kernels. That's found 
in compilation of many tests and workloads.
- Fixing all of them may change more code and, more importantly, all of them 
assumes the default kernel reference kind. This patch changes that constructor 
to tell `CUDAGlobalAttr` and construct `GlobalDecl` properly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76344

Files:
  clang/include/clang/AST/GlobalDecl.h
  clang/lib/AST/Expr.cpp
  clang/lib/AST/ItaniumMangle.cpp
  clang/lib/AST/Mangle.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h

Index: clang/lib/CodeGen/CodeGenModule.h
===================================================================
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -711,9 +711,6 @@
   CtorList &getGlobalCtors() { return GlobalCtors; }
   CtorList &getGlobalDtors() { return GlobalDtors; }
 
-  /// get GlobalDecl for non-ctor/dtor functions.
-  GlobalDecl getGlobalDecl(const FunctionDecl *FD);
-
   /// getTBAATypeInfo - Get metadata used to describe accesses to objects of
   /// the given type.
   llvm::MDNode *getTBAATypeInfo(QualType QTy);
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -5302,7 +5302,7 @@
   case Decl::CXXConversion:
   case Decl::CXXMethod:
   case Decl::Function:
-    EmitGlobal(getGlobalDecl(cast<FunctionDecl>(D)));
+    EmitGlobal(cast<FunctionDecl>(D));
     // Always provide some coverage mapping
     // even for the functions that aren't emitted.
     AddDeferredUnusedCoverageMapping(D);
@@ -5964,10 +5964,3 @@
                                 "__translate_sampler_initializer"),
                                 {C});
 }
-
-GlobalDecl CodeGenModule::getGlobalDecl(const FunctionDecl *FD) {
-  if (FD->hasAttr<CUDAGlobalAttr>())
-    return GlobalDecl::getDefaultKernelReference(FD);
-  else
-    return GlobalDecl(FD);
-}
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -4678,12 +4678,12 @@
   // Resolve direct calls.
   } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) {
     if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
-      return EmitDirectCallee(*this, CGM.getGlobalDecl(FD));
+      return EmitDirectCallee(*this, FD);
     }
   } else if (auto ME = dyn_cast<MemberExpr>(E)) {
     if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) {
       EmitIgnoredExpr(ME->getBase());
-      return EmitDirectCallee(*this, CGM.getGlobalDecl(FD));
+      return EmitDirectCallee(*this, FD);
     }
 
   // Look through template substitutions.
Index: clang/lib/CodeGen/CGDecl.cpp
===================================================================
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -297,7 +297,7 @@
   else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
     GD = GlobalDecl(DD, Dtor_Base);
   else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
-    GD = getGlobalDecl(FD);
+    GD = GlobalDecl(FD);
   else {
     // Don't do anything for Obj-C method decls or global closures. We should
     // never defer them.
Index: clang/lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- clang/lib/CodeGen/CGDebugInfo.cpp
+++ clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3833,8 +3833,7 @@
   // create the one describing the function in order to have complete
   // call site debug info.
   if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined())
-    EmitFunctionDecl(CGM.getGlobalDecl(CalleeDecl), CalleeDecl->getLocation(),
-                     CalleeType, Func);
+    EmitFunctionDecl(CalleeDecl, CalleeDecl->getLocation(), CalleeType, Func);
 }
 
 void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
Index: clang/lib/AST/Mangle.cpp
===================================================================
--- clang/lib/AST/Mangle.cpp
+++ clang/lib/AST/Mangle.cpp
@@ -444,7 +444,7 @@
       else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
         GD = GlobalDecl(DtorD, Dtor_Complete);
       else if (D->hasAttr<CUDAGlobalAttr>())
-        GD = GlobalDecl::getDefaultKernelReference(cast<FunctionDecl>(D));
+        GD = GlobalDecl(cast<FunctionDecl>(D));
       else
         GD = GlobalDecl(D);
       MC->mangleName(GD, OS);
Index: clang/lib/AST/ItaniumMangle.cpp
===================================================================
--- clang/lib/AST/ItaniumMangle.cpp
+++ clang/lib/AST/ItaniumMangle.cpp
@@ -1575,14 +1575,8 @@
     GD = GlobalDecl(CD, Ctor_Complete);
   else if (auto *DD = dyn_cast<CXXDestructorDecl>(DC))
     GD = GlobalDecl(DD, Dtor_Complete);
-  else {
-    auto *FD = cast<FunctionDecl>(DC);
-    // Local variables can only exist in real kernels.
-    if (FD->hasAttr<CUDAGlobalAttr>())
-      GD = GlobalDecl(FD, KernelReferenceKind::Kernel);
-    else
-      GD = GlobalDecl(FD);
-  }
+  else
+    GD = GlobalDecl(cast<FunctionDecl>(DC));
   return GD;
 }
 
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -567,7 +567,7 @@
         else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
           GD = GlobalDecl(DD, Dtor_Base);
         else if (ND->hasAttr<CUDAGlobalAttr>())
-          GD = GlobalDecl::getDefaultKernelReference(cast<FunctionDecl>(ND));
+          GD = GlobalDecl(cast<FunctionDecl>(ND));
         else
           GD = GlobalDecl(ND);
         MC->mangleName(GD, Out);
Index: clang/include/clang/AST/GlobalDecl.h
===================================================================
--- clang/include/clang/AST/GlobalDecl.h
+++ clang/include/clang/AST/GlobalDecl.h
@@ -68,7 +68,15 @@
   GlobalDecl(const VarDecl *D) { Init(D);}
   GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
       : MultiVersionIndex(MVIndex) {
-    Init(D);
+    if (!D->hasAttr<CUDAGlobalAttr>()) {
+      Init(D);
+      return;
+    }
+    Value.setPointerAndInt(D, unsigned(getDefaultKernelReference(D)));
+  }
+  GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
+      : Value(D, unsigned(Kind)) {
+    assert(D->hasAttr<CUDAGlobalAttr>() && "Decl is not a GPU kernel!");
   }
   GlobalDecl(const NamedDecl *D) { Init(D); }
   GlobalDecl(const BlockDecl *D) { Init(D); }
@@ -80,10 +88,6 @@
   GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
   GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
       : Value(D, unsigned(StubKind)) {}
-  GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
-      : Value(D, unsigned(Kind)) {
-    assert(D->hasAttr<CUDAGlobalAttr>() && "Decl is not a GPU kernel!");
-  }
 
   GlobalDecl getCanonicalDecl() const {
     GlobalDecl CanonGD;
@@ -145,10 +149,10 @@
     return GD;
   }
 
-  static GlobalDecl getDefaultKernelReference(const FunctionDecl *D) {
-    return GlobalDecl(D, D->getASTContext().getLangOpts().CUDAIsDevice
-                             ? KernelReferenceKind::Kernel
-                             : KernelReferenceKind::Stub);
+  static KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D) {
+    return D->getASTContext().getLangOpts().CUDAIsDevice
+               ? KernelReferenceKind::Kernel
+               : KernelReferenceKind::Stub;
   }
 
   GlobalDecl getWithDecl(const Decl *D) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to