Unverified Commit 078ae8cd authored by Joseph Huber's avatar Joseph Huber Committed by GitHub
Browse files

[Offloading][NFC] Move creation of offloading entries from OpenMP (#70116)

Summary:
This patch is a first step to remove dependencies on the OpenMPIRBuilder
for creating generic offloading entries. This patch changes no
functionality and merely moves the code around. In the future the
interface will be changed to allow for more code re-use in the
registration and creation of offloading entries as well as a more
generic interface for CUDA, HIP, OpenMP, and SYCL(?). Doing this as a
first step to reduce the noise involved in the functional changes.
parent 42c25fdd
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "clang/Basic/Cuda.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/ConstantInitBuilder.h"
#include "llvm/Frontend/Offloading/Utility.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
@@ -1129,13 +1130,12 @@ void CGNVCUDARuntime::transformManagedVars() {
// registered. The linker will provide a pointer to this section so we can
// register the symbols with the linked device image.
void CGNVCUDARuntime::createOffloadingEntries() {
  llvm::OpenMPIRBuilder OMPBuilder(CGM.getModule());
  OMPBuilder.initialize();

  StringRef Section = CGM.getLangOpts().HIP ? "hip_offloading_entries"
                                            : "cuda_offloading_entries";
  llvm::Module &M = CGM.getModule();
  for (KernelInfo &I : EmittedKernels)
    OMPBuilder.emitOffloadingEntry(KernelHandles[I.Kernel->getName()],
    llvm::offloading::emitOffloadingEntry(
        M, KernelHandles[I.Kernel->getName()],
        getDeviceSideName(cast<NamedDecl>(I.D)), 0,
        DeviceVarFlags::OffloadGlobalEntry, Section);

@@ -1143,19 +1143,19 @@ void CGNVCUDARuntime::createOffloadingEntries() {
    uint64_t VarSize =
        CGM.getDataLayout().getTypeAllocSize(I.Var->getValueType());
    if (I.Flags.getKind() == DeviceVarFlags::Variable) {
      OMPBuilder.emitOffloadingEntry(
          I.Var, getDeviceSideName(I.D), VarSize,
      llvm::offloading::emitOffloadingEntry(
          M, I.Var, getDeviceSideName(I.D), VarSize,
          I.Flags.isManaged() ? DeviceVarFlags::OffloadGlobalManagedEntry
                              : DeviceVarFlags::OffloadGlobalEntry,
          Section);
    } else if (I.Flags.getKind() == DeviceVarFlags::Surface) {
      OMPBuilder.emitOffloadingEntry(I.Var, getDeviceSideName(I.D), VarSize,
                                     DeviceVarFlags::OffloadGlobalSurfaceEntry,
                                     Section);
      llvm::offloading::emitOffloadingEntry(
          M, I.Var, getDeviceSideName(I.D), VarSize,
          DeviceVarFlags::OffloadGlobalSurfaceEntry, Section);
    } else if (I.Flags.getKind() == DeviceVarFlags::Texture) {
      OMPBuilder.emitOffloadingEntry(I.Var, getDeviceSideName(I.D), VarSize,
                                     DeviceVarFlags::OffloadGlobalTextureEntry,
                                     Section);
      llvm::offloading::emitOffloadingEntry(
          M, I.Var, getDeviceSideName(I.D), VarSize,
          DeviceVarFlags::OffloadGlobalTextureEntry, Section);
    }
  }
}
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS
  Extensions
  FrontendHLSL
  FrontendOpenMP
  FrontendOffloading
  HIPStdPar
  IPO
  IRPrinter
+37 −0
Original line number Diff line number Diff line
//===- Utility.h - Collection of geneirc offloading utilities -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/Module.h"
#include "llvm/Object/OffloadBinary.h"

namespace llvm {
namespace offloading {

/// Create an offloading section struct used to register this global at
/// runtime.
///
/// Type struct __tgt_offload_entry{
///   void    *addr;      // Pointer to the offload entry info.
///                       // (function or global)
///   char    *name;      // Name of the function or global.
///   size_t  size;       // Size of the entry info (0 if it a function).
///   int32_t flags;
///   int32_t reserved;
/// };
///
/// \param M The module to be used
/// \param Addr The pointer to the global being registered.
/// \param Name The symbol name associated with the global.
/// \param Size The size in bytes of the global (0 for functions).
/// \param Flags Flags associated with the entry.
/// \param SectionName The section this entry will be placed at.
void emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name,
                         uint64_t Size, int32_t Flags, StringRef SectionName);

} // namespace offloading
} // namespace llvm
+0 −21
Original line number Diff line number Diff line
@@ -1358,27 +1358,6 @@ public:
  /// Value.
  GlobalValue *createGlobalFlag(unsigned Value, StringRef Name);

  /// Create an offloading section struct used to register this global at
  /// runtime.
  ///
  /// Type struct __tgt_offload_entry{
  ///   void    *addr;      // Pointer to the offload entry info.
  ///                       // (function or global)
  ///   char    *name;      // Name of the function or global.
  ///   size_t  size;       // Size of the entry info (0 if it a function).
  ///   int32_t flags;
  ///   int32_t reserved;
  /// };
  ///
  /// \param Addr The pointer to the global being registered.
  /// \param Name The symbol name associated with the global.
  /// \param Size The size in bytes of the global (0 for functions).
  /// \param Flags Flags associated with the entry.
  /// \param SectionName The section this entry will be placed at.
  void emitOffloadingEntry(Constant *Addr, StringRef Name, uint64_t Size,
                           int32_t Flags,
                           StringRef SectionName = "omp_offloading_entries");

  /// Generate control flow and cleanup for cancellation.
  ///
  /// \param CancelFlag Flag indicating if the cancellation is performed.
+0 −2
Original line number Diff line number Diff line
@@ -88,8 +88,6 @@ __OMP_ARRAY_TYPE(Int32Arr3, Int32, 3)
  OMP_STRUCT_TYPE(VarName, "struct." #Name, Packed, __VA_ARGS__)

__OMP_STRUCT_TYPE(Ident, ident_t, false, Int32, Int32, Int32, Int32, Int8Ptr)
__OMP_STRUCT_TYPE(OffloadEntry, __tgt_offload_entry, false, Int8Ptr, Int8Ptr, SizeTy,
                  Int32, Int32)
__OMP_STRUCT_TYPE(KernelArgs, __tgt_kernel_arguments, false, Int32, Int32, VoidPtrPtr,
		  VoidPtrPtr, Int64Ptr, Int64Ptr, VoidPtrPtr, VoidPtrPtr,
		  Int64, Int64, Int32Arr3Ty, Int32Arr3Ty, Int32)
Loading