1 //===- Utility.h - Collection of geneirc offloading utilities -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_FRONTEND_OFFLOADING_UTILITY_H
10 #define LLVM_FRONTEND_OFFLOADING_UTILITY_H
11 
12 #include "llvm/IR/Module.h"
13 #include "llvm/Object/OffloadBinary.h"
14 
15 namespace llvm {
16 namespace offloading {
17 
18 /// Offloading entry flags for CUDA / HIP. The first three bits indicate the
19 /// type of entry while the others are a bit field for additional information.
20 enum OffloadEntryKindFlag : uint32_t {
21   /// Mark the entry as a global entry. This indicates the presense of a
22   /// kernel if the size size field is zero and a variable otherwise.
23   OffloadGlobalEntry = 0x0,
24   /// Mark the entry as a managed global variable.
25   OffloadGlobalManagedEntry = 0x1,
26   /// Mark the entry as a surface variable.
27   OffloadGlobalSurfaceEntry = 0x2,
28   /// Mark the entry as a texture variable.
29   OffloadGlobalTextureEntry = 0x3,
30   /// Mark the entry as being extern.
31   OffloadGlobalExtern = 0x1 << 3,
32   /// Mark the entry as being constant.
33   OffloadGlobalConstant = 0x1 << 4,
34   /// Mark the entry as being a normalized surface.
35   OffloadGlobalNormalized = 0x1 << 5,
36 };
37 
38 /// Returns the type of the offloading entry we use to store kernels and
39 /// globals that will be registered with the offloading runtime.
40 StructType *getEntryTy(Module &M);
41 
42 /// Create an offloading section struct used to register this global at
43 /// runtime.
44 ///
45 /// Type struct __tgt_offload_entry {
46 ///   void    *addr;      // Pointer to the offload entry info.
47 ///                       // (function or global)
48 ///   char    *name;      // Name of the function or global.
49 ///   size_t  size;       // Size of the entry info (0 if it a function).
50 ///   int32_t flags;
51 ///   int32_t data;
52 /// };
53 ///
54 /// \param M The module to be used
55 /// \param Addr The pointer to the global being registered.
56 /// \param Name The symbol name associated with the global.
57 /// \param Size The size in bytes of the global (0 for functions).
58 /// \param Flags Flags associated with the entry.
59 /// \param Data Extra data storage associated with the entry.
60 /// \param SectionName The section this entry will be placed at.
61 void emitOffloadingEntry(Module &M, Constant *Addr, StringRef Name,
62                          uint64_t Size, int32_t Flags, int32_t Data,
63                          StringRef SectionName);
64 /// Create a constant struct initializer used to register this global at
65 /// runtime.
66 /// \return the constant struct and the global variable holding the symbol name.
67 std::pair<Constant *, GlobalVariable *>
68 getOffloadingEntryInitializer(Module &M, Constant *Addr, StringRef Name,
69                               uint64_t Size, int32_t Flags, int32_t Data);
70 
71 /// Creates a pair of globals used to iterate the array of offloading entries by
72 /// accessing the section variables provided by the linker.
73 std::pair<GlobalVariable *, GlobalVariable *>
74 getOffloadEntryArray(Module &M, StringRef SectionName);
75 
76 } // namespace offloading
77 } // namespace llvm
78 
79 #endif // LLVM_FRONTEND_OFFLOADING_UTILITY_H
80