[PATCH] D121837: [OpenMP][FIX] Allow device constructors for AMD GPU

Johannes Doerfert via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 16 11:47:29 PDT 2022


jdoerfert created this revision.
jdoerfert added a reviewer: JonChesterfield.
Herald added subscribers: guansong, bollu, yaxunl, jvesely.
Herald added a project: All.
jdoerfert requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1.
Herald added a project: clang.

In AMD GPU device code the globals are in AS(1). Before, we crashed if
the global was a structure. Now we simply cast away the AS before we
generate the code to initialize the global.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121837

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/test/OpenMP/amdgcn_target_global_constructor.cpp


Index: clang/test/OpenMP/amdgcn_target_global_constructor.cpp
===================================================================
--- /dev/null
+++ clang/test/OpenMP/amdgcn_target_global_constructor.cpp
@@ -0,0 +1,21 @@
+// REQUIRES: amdgpu-registered-target
+
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-ppc-host.bc
+// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck %s
+// expected-no-diagnostics
+#ifndef HEADER
+#define HEADER
+
+void foo(void);
+
+struct S {
+  int a;
+  S() : a(1) {}
+  ~S() { foo(); }
+};
+
+#pragma omp declare target
+S A;
+#pragma omp end declare target
+
+#endif
Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -35,6 +35,7 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/GlobalValue.h"
+#include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Value.h"
 #include "llvm/Support/AtomicOrdering.h"
 #include "llvm/Support/Format.h"
@@ -1940,8 +1941,12 @@
       CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
                             FunctionArgList(), Loc, Loc);
       auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF);
+      auto *AddrInAS0 = llvm::ConstantExpr::getAddrSpaceCast(
+          Addr, llvm::PointerType::getWithSamePointeeType(
+                    cast<llvm::PointerType>(Addr->getType()), 0));
       CtorCGF.EmitAnyExprToMem(
-          Init, Address::deprecated(Addr, CGM.getContext().getDeclAlign(VD)),
+          Init,
+          Address::deprecated(AddrInAS0, CGM.getContext().getDeclAlign(VD)),
           Init->getType().getQualifiers(),
           /*IsInitializer=*/true);
       CtorCGF.FinishFunction();
@@ -1980,9 +1985,12 @@
       // Create a scope with an artificial location for the body of this
       // function.
       auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
+      auto *AddrInAS0 = llvm::ConstantExpr::getAddrSpaceCast(
+          Addr, llvm::PointerType::getWithSamePointeeType(
+                    cast<llvm::PointerType>(Addr->getType()), 0));
       DtorCGF.emitDestroy(
-          Address::deprecated(Addr, CGM.getContext().getDeclAlign(VD)), ASTTy,
-          DtorCGF.getDestroyer(ASTTy.isDestructedType()),
+          Address::deprecated(AddrInAS0, CGM.getContext().getDeclAlign(VD)),
+          ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()),
           DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
       DtorCGF.FinishFunction();
       Dtor = Fn;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121837.415925.patch
Type: text/x-patch
Size: 2815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220316/7f02268f/attachment.bin>


More information about the cfe-commits mailing list