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

D94745 <https://reviews.llvm.org/D94745> rewrites the `deviceRTLs` using OpenMP 
and compiles it by directly
calling the device compilation. `clang` crashes because entry in
`OffloadEntriesDeviceGlobalVar` is unintialized. Current design supposes the
device compilation can only be invoked after host compilation with the host IR
such that `clang` can initialize `OffloadEntriesDeviceGlobalVar` from host IR.
This avoids us using device compilation directly, especially when we only have
code wrapped into `declare target` which are all device code.

In this patch, we updated the logic that if a declaration is in `declare 
target`,
and it is not in the map, we will first initialize it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94871

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h


Index: clang/lib/CodeGen/CGOpenMPRuntime.h
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.h
+++ clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -674,7 +674,8 @@
     registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
                                      CharUnits VarSize,
                                      OMPTargetGlobalVarEntryKind Flags,
-                                     llvm::GlobalValue::LinkageTypes Linkage);
+                                     llvm::GlobalValue::LinkageTypes Linkage,
+                                     bool IsDeclareTargetDeclaration = false);
     /// Checks if the variable with the given name has been registered already.
     bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
       return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -3015,8 +3015,13 @@
     registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
                                      CharUnits VarSize,
                                      OMPTargetGlobalVarEntryKind Flags,
-                                     llvm::GlobalValue::LinkageTypes Linkage) {
+                                     llvm::GlobalValue::LinkageTypes Linkage,
+                                     bool IsDeclareTargetDeclaration) {
   if (CGM.getLangOpts().OpenMPIsDevice) {
+    // A global variable might be not in the map if it is declared in declare
+    // target and device compilation is invoked without host IR.
+    if (IsDeclareTargetDeclaration && !hasDeviceGlobalVarEntryInfo(VarName))
+      initializeDeviceGlobalVarEntryInfo(VarName, Flags, OffloadingEntriesNum);
     auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
     assert(Entry.isValid() && Entry.getFlags() == Flags &&
            "Entry not initialized!");
@@ -10514,7 +10519,7 @@
   }
 
   OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
-      VarName, Addr, VarSize, Flags, Linkage);
+      VarName, Addr, VarSize, Flags, Linkage, static_cast<bool>(Res));
 }
 
 bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {


Index: clang/lib/CodeGen/CGOpenMPRuntime.h
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.h
+++ clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -674,7 +674,8 @@
     registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
                                      CharUnits VarSize,
                                      OMPTargetGlobalVarEntryKind Flags,
-                                     llvm::GlobalValue::LinkageTypes Linkage);
+                                     llvm::GlobalValue::LinkageTypes Linkage,
+                                     bool IsDeclareTargetDeclaration = false);
     /// Checks if the variable with the given name has been registered already.
     bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
       return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -3015,8 +3015,13 @@
     registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
                                      CharUnits VarSize,
                                      OMPTargetGlobalVarEntryKind Flags,
-                                     llvm::GlobalValue::LinkageTypes Linkage) {
+                                     llvm::GlobalValue::LinkageTypes Linkage,
+                                     bool IsDeclareTargetDeclaration) {
   if (CGM.getLangOpts().OpenMPIsDevice) {
+    // A global variable might be not in the map if it is declared in declare
+    // target and device compilation is invoked without host IR.
+    if (IsDeclareTargetDeclaration && !hasDeviceGlobalVarEntryInfo(VarName))
+      initializeDeviceGlobalVarEntryInfo(VarName, Flags, OffloadingEntriesNum);
     auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
     assert(Entry.isValid() && Entry.getFlags() == Flags &&
            "Entry not initialized!");
@@ -10514,7 +10519,7 @@
   }
 
   OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
-      VarName, Addr, VarSize, Flags, Linkage);
+      VarName, Addr, VarSize, Flags, Linkage, static_cast<bool>(Res));
 }
 
 bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to