Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for OpenMP runtime code generation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 16 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 17 | #include "CGValue.h" |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 19 | #include "clang/Basic/OpenMPKinds.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceLocation.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Benjamin Kramer | 8fdba91 | 2016-02-02 14:24:21 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringMap.h" |
Benjamin Kramer | 8fdba91 | 2016-02-02 14:24:21 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 25 | #include "llvm/IR/ValueHandle.h" |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 26 | |
| 27 | namespace llvm { |
| 28 | class ArrayType; |
| 29 | class Constant; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 30 | class FunctionType; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 31 | class GlobalVariable; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 32 | class StructType; |
| 33 | class Type; |
| 34 | class Value; |
| 35 | } // namespace llvm |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 36 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 37 | namespace clang { |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 38 | class Expr; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 39 | class GlobalDecl; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 40 | class OMPDependClause; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 41 | class OMPExecutableDirective; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 42 | class OMPLoopDirective; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 43 | class VarDecl; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 44 | class OMPDeclareReductionDecl; |
| 45 | class IdentifierInfo; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 46 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 47 | namespace CodeGen { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 48 | class Address; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 49 | class CodeGenFunction; |
| 50 | class CodeGenModule; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 51 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 52 | /// A basic class for pre|post-action for advanced codegen sequence for OpenMP |
| 53 | /// region. |
| 54 | class PrePostActionTy { |
| 55 | public: |
| 56 | explicit PrePostActionTy() {} |
| 57 | virtual void Enter(CodeGenFunction &CGF) {} |
| 58 | virtual void Exit(CodeGenFunction &CGF) {} |
| 59 | virtual ~PrePostActionTy() {} |
| 60 | }; |
| 61 | |
| 62 | /// Class provides a way to call simple version of codegen for OpenMP region, or |
| 63 | /// an advanced with possible pre|post-actions in codegen. |
| 64 | class RegionCodeGenTy final { |
| 65 | intptr_t CodeGen; |
| 66 | typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &); |
| 67 | CodeGenTy Callback; |
| 68 | mutable PrePostActionTy *PrePostAction; |
| 69 | RegionCodeGenTy() = delete; |
| 70 | RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete; |
| 71 | template <typename Callable> |
| 72 | static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF, |
| 73 | PrePostActionTy &Action) { |
| 74 | return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action); |
| 75 | } |
| 76 | |
| 77 | public: |
| 78 | template <typename Callable> |
| 79 | RegionCodeGenTy( |
| 80 | Callable &&CodeGen, |
| 81 | typename std::enable_if< |
| 82 | !std::is_same<typename std::remove_reference<Callable>::type, |
| 83 | RegionCodeGenTy>::value>::type * = nullptr) |
| 84 | : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)), |
| 85 | Callback(CallbackFn<typename std::remove_reference<Callable>::type>), |
| 86 | PrePostAction(nullptr) {} |
| 87 | void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; } |
| 88 | void operator()(CodeGenFunction &CGF) const; |
| 89 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 90 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 91 | struct OMPTaskDataTy final { |
| 92 | SmallVector<const Expr *, 4> PrivateVars; |
| 93 | SmallVector<const Expr *, 4> PrivateCopies; |
| 94 | SmallVector<const Expr *, 4> FirstprivateVars; |
| 95 | SmallVector<const Expr *, 4> FirstprivateCopies; |
| 96 | SmallVector<const Expr *, 4> FirstprivateInits; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 97 | SmallVector<const Expr *, 4> LastprivateVars; |
| 98 | SmallVector<const Expr *, 4> LastprivateCopies; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 99 | SmallVector<const Expr *, 4> ReductionVars; |
| 100 | SmallVector<const Expr *, 4> ReductionCopies; |
| 101 | SmallVector<const Expr *, 4> ReductionOps; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 102 | SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences; |
| 103 | llvm::PointerIntPair<llvm::Value *, 1, bool> Final; |
| 104 | llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule; |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 105 | llvm::PointerIntPair<llvm::Value *, 1, bool> Priority; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 106 | llvm::Value *Reductions = nullptr; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 107 | unsigned NumberOfParts = 0; |
| 108 | bool Tied = true; |
| 109 | bool Nogroup = false; |
| 110 | }; |
| 111 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 112 | /// Class intended to support codegen of all kind of the reduction clauses. |
| 113 | class ReductionCodeGen { |
| 114 | private: |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 115 | /// Data required for codegen of reduction clauses. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 116 | struct ReductionData { |
| 117 | /// Reference to the original shared item. |
| 118 | const Expr *Ref = nullptr; |
| 119 | /// Helper expression for generation of private copy. |
| 120 | const Expr *Private = nullptr; |
| 121 | /// Helper expression for generation reduction operation. |
| 122 | const Expr *ReductionOp = nullptr; |
| 123 | ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp) |
| 124 | : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {} |
| 125 | }; |
| 126 | /// List of reduction-based clauses. |
| 127 | SmallVector<ReductionData, 4> ClausesData; |
| 128 | |
| 129 | /// List of addresses of original shared variables/expressions. |
| 130 | SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses; |
| 131 | /// Sizes of the reduction items in chars. |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 132 | SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 133 | /// Base declarations for the reduction items. |
| 134 | SmallVector<const VarDecl *, 4> BaseDecls; |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 135 | |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 136 | /// Emits lvalue for shared expresion. |
| 137 | LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E); |
| 138 | /// Emits upper bound for shared expression (if array section). |
| 139 | LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E); |
| 140 | /// Performs aggregate initialization. |
| 141 | /// \param N Number of reduction item in the common list. |
| 142 | /// \param PrivateAddr Address of the corresponding private item. |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 143 | /// \param SharedLVal Address of the original shared variable. |
| 144 | /// \param DRD Declare reduction construct used for reduction item. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 145 | void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N, |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 146 | Address PrivateAddr, LValue SharedLVal, |
| 147 | const OMPDeclareReductionDecl *DRD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 148 | |
| 149 | public: |
| 150 | ReductionCodeGen(ArrayRef<const Expr *> Shareds, |
| 151 | ArrayRef<const Expr *> Privates, |
| 152 | ArrayRef<const Expr *> ReductionOps); |
| 153 | /// Emits lvalue for a reduction item. |
| 154 | /// \param N Number of the reduction item. |
| 155 | void emitSharedLValue(CodeGenFunction &CGF, unsigned N); |
| 156 | /// Emits the code for the variable-modified type, if required. |
| 157 | /// \param N Number of the reduction item. |
| 158 | void emitAggregateType(CodeGenFunction &CGF, unsigned N); |
| 159 | /// Emits the code for the variable-modified type, if required. |
| 160 | /// \param N Number of the reduction item. |
| 161 | /// \param Size Size of the type in chars. |
| 162 | void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size); |
| 163 | /// Performs initialization of the private copy for the reduction item. |
| 164 | /// \param N Number of the reduction item. |
| 165 | /// \param PrivateAddr Address of the corresponding private item. |
| 166 | /// \param DefaultInit Default initialization sequence that should be |
| 167 | /// performed if no reduction specific initialization is found. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 168 | /// \param SharedLVal Address of the original shared variable. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 169 | void |
| 170 | emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr, |
| 171 | LValue SharedLVal, |
| 172 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit); |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 173 | /// Returns true if the private copy requires cleanups. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 174 | bool needCleanups(unsigned N); |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 175 | /// Emits cleanup code for the reduction item. |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 176 | /// \param N Number of the reduction item. |
| 177 | /// \param PrivateAddr Address of the corresponding private item. |
| 178 | void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr); |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 179 | /// Adjusts \p PrivatedAddr for using instead of the original variable |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 180 | /// address in normal operations. |
| 181 | /// \param N Number of the reduction item. |
| 182 | /// \param PrivateAddr Address of the corresponding private item. |
| 183 | Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N, |
| 184 | Address PrivateAddr); |
| 185 | /// Returns LValue for the reduction item. |
| 186 | LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 187 | /// Returns the size of the reduction item (in chars and total number of |
| 188 | /// elements in the item), or nullptr, if the size is a constant. |
| 189 | std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const { |
| 190 | return Sizes[N]; |
| 191 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 192 | /// Returns the base declaration of the reduction item. |
| 193 | const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 194 | /// Returns true if the initialization of the reduction item uses initializer |
| 195 | /// from declare reduction construct. |
| 196 | bool usesReductionInitializer(unsigned N) const; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 199 | class CGOpenMPRuntime { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 200 | protected: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 201 | CodeGenModule &CGM; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 202 | |
| 203 | /// \brief Creates offloading entry for the provided entry ID \a ID, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 204 | /// address \a Addr, size \a Size, and flags \a Flags. |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 205 | virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 206 | uint64_t Size, int32_t Flags = 0); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 207 | |
| 208 | /// \brief Helper to emit outlined function for 'target' directive. |
| 209 | /// \param D Directive to emit. |
| 210 | /// \param ParentName Name of the function that encloses the target region. |
| 211 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 212 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 213 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 214 | /// \param CodeGen Lambda codegen specific to an accelerator device. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 215 | /// An outlined function may not be an entry if, e.g. the if clause always |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 216 | /// evaluates to false. |
| 217 | virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D, |
| 218 | StringRef ParentName, |
| 219 | llvm::Function *&OutlinedFn, |
| 220 | llvm::Constant *&OutlinedFnID, |
| 221 | bool IsOffloadEntry, |
| 222 | const RegionCodeGenTy &CodeGen); |
| 223 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 224 | /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen |
| 225 | /// function. Here is the logic: |
| 226 | /// if (Cond) { |
| 227 | /// ThenGen(); |
| 228 | /// } else { |
| 229 | /// ElseGen(); |
| 230 | /// } |
| 231 | void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 232 | const RegionCodeGenTy &ThenGen, |
| 233 | const RegionCodeGenTy &ElseGen); |
| 234 | |
| 235 | /// \brief Emits object of ident_t type with info for source location. |
| 236 | /// \param Flags Flags for OpenMP location. |
| 237 | /// |
| 238 | llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc, |
| 239 | unsigned Flags = 0); |
| 240 | |
| 241 | /// \brief Returns pointer to ident_t type. |
| 242 | llvm::Type *getIdentTyPointerTy(); |
| 243 | |
| 244 | /// \brief Gets thread id value for the current thread. |
| 245 | /// |
| 246 | llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc); |
| 247 | |
| 248 | /// \brief Get the function name of an outlined region. |
| 249 | // The name can be customized depending on the target. |
| 250 | // |
| 251 | virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; } |
| 252 | |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 253 | /// Emits \p Callee function call with arguments \p Args with location \p Loc. |
| 254 | void emitCall(CodeGenFunction &CGF, llvm::Value *Callee, |
| 255 | ArrayRef<llvm::Value *> Args = llvm::None, |
| 256 | SourceLocation Loc = SourceLocation()) const; |
| 257 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 258 | private: |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 259 | /// \brief Default const ident_t object used for initialization of all other |
| 260 | /// ident_t objects. |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 261 | llvm::Constant *DefaultOpenMPPSource = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 262 | /// \brief Map of flags and corresponding default locations. |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 263 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy; |
| 264 | OpenMPDefaultLocMapTy OpenMPDefaultLocMap; |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 265 | Address getOrCreateDefaultLocation(unsigned Flags); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 266 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 267 | llvm::StructType *IdentTy = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 268 | /// \brief Map for SourceLocation and OpenMP runtime library debug locations. |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 269 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy; |
| 270 | OpenMPDebugLocMapTy OpenMPDebugLocMap; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 271 | /// \brief The type for a microtask which gets passed to __kmpc_fork_call(). |
| 272 | /// Original representation is: |
| 273 | /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 274 | llvm::FunctionType *Kmpc_MicroTy = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 275 | /// \brief Stores debug location and ThreadID for the function. |
| 276 | struct DebugLocThreadIdTy { |
| 277 | llvm::Value *DebugLoc; |
| 278 | llvm::Value *ThreadID; |
| 279 | }; |
| 280 | /// \brief Map of local debug location, ThreadId and functions. |
| 281 | typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy> |
| 282 | OpenMPLocThreadIDMapTy; |
| 283 | OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap; |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 284 | /// Map of UDRs and corresponding combiner/initializer. |
| 285 | typedef llvm::DenseMap<const OMPDeclareReductionDecl *, |
| 286 | std::pair<llvm::Function *, llvm::Function *>> |
| 287 | UDRMapTy; |
| 288 | UDRMapTy UDRMap; |
| 289 | /// Map of functions and locally defined UDRs. |
| 290 | typedef llvm::DenseMap<llvm::Function *, |
| 291 | SmallVector<const OMPDeclareReductionDecl *, 4>> |
| 292 | FunctionUDRMapTy; |
| 293 | FunctionUDRMapTy FunctionUDRMap; |
| 294 | IdentifierInfo *In = nullptr; |
| 295 | IdentifierInfo *Out = nullptr; |
| 296 | IdentifierInfo *Priv = nullptr; |
| 297 | IdentifierInfo *Orig = nullptr; |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 298 | /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32 |
| 299 | /// kmp_critical_name[8]; |
| 300 | llvm::ArrayType *KmpCriticalNameTy; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 301 | /// \brief An ordered map of auto-generated variables to their unique names. |
| 302 | /// It stores variables with the following names: 1) ".gomp_critical_user_" + |
| 303 | /// <critical_section_name> + ".var" for "omp critical" directives; 2) |
| 304 | /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate |
| 305 | /// variables. |
| 306 | llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator> |
| 307 | InternalVars; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 308 | /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 309 | llvm::Type *KmpRoutineEntryPtrTy = nullptr; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 310 | QualType KmpRoutineEntryPtrQTy; |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 311 | /// \brief Type typedef struct kmp_task { |
| 312 | /// void * shareds; /**< pointer to block of pointers to |
| 313 | /// shared vars */ |
| 314 | /// kmp_routine_entry_t routine; /**< pointer to routine to call for |
| 315 | /// executing task */ |
| 316 | /// kmp_int32 part_id; /**< part id for the task */ |
| 317 | /// kmp_routine_entry_t destructors; /* pointer to function to invoke |
| 318 | /// deconstructors of firstprivate C++ objects */ |
| 319 | /// } kmp_task_t; |
| 320 | QualType KmpTaskTQTy; |
Alexey Bataev | e213f3e | 2017-10-11 15:29:40 +0000 | [diff] [blame] | 321 | /// Saved kmp_task_t for task directive. |
| 322 | QualType SavedKmpTaskTQTy; |
| 323 | /// Saved kmp_task_t for taskloop-based directive. |
| 324 | QualType SavedKmpTaskloopTQTy; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 325 | /// \brief Type typedef struct kmp_depend_info { |
| 326 | /// kmp_intptr_t base_addr; |
| 327 | /// size_t len; |
| 328 | /// struct { |
| 329 | /// bool in:1; |
| 330 | /// bool out:1; |
| 331 | /// } flags; |
| 332 | /// } kmp_depend_info_t; |
| 333 | QualType KmpDependInfoTy; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 334 | /// struct kmp_dim { // loop bounds info casted to kmp_int64 |
| 335 | /// kmp_int64 lo; // lower |
| 336 | /// kmp_int64 up; // upper |
| 337 | /// kmp_int64 st; // stride |
| 338 | /// }; |
| 339 | QualType KmpDimTy; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 340 | /// \brief Type struct __tgt_offload_entry{ |
| 341 | /// void *addr; // Pointer to the offload entry info. |
| 342 | /// // (function or global) |
| 343 | /// char *name; // Name of the function or global. |
| 344 | /// size_t size; // Size of the entry info (0 if it a function). |
| 345 | /// }; |
| 346 | QualType TgtOffloadEntryQTy; |
| 347 | /// struct __tgt_device_image{ |
| 348 | /// void *ImageStart; // Pointer to the target code start. |
| 349 | /// void *ImageEnd; // Pointer to the target code end. |
| 350 | /// // We also add the host entries to the device image, as it may be useful |
| 351 | /// // for the target runtime to have access to that information. |
| 352 | /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 353 | /// // the entries. |
| 354 | /// __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 355 | /// // entries (non inclusive). |
| 356 | /// }; |
| 357 | QualType TgtDeviceImageQTy; |
| 358 | /// struct __tgt_bin_desc{ |
| 359 | /// int32_t NumDevices; // Number of devices supported. |
| 360 | /// __tgt_device_image *DeviceImages; // Arrays of device images |
| 361 | /// // (one per device). |
| 362 | /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 363 | /// // entries. |
| 364 | /// __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 365 | /// // entries (non inclusive). |
| 366 | /// }; |
| 367 | QualType TgtBinaryDescriptorQTy; |
| 368 | /// \brief Entity that registers the offloading constants that were emitted so |
| 369 | /// far. |
| 370 | class OffloadEntriesInfoManagerTy { |
| 371 | CodeGenModule &CGM; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 372 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 373 | /// \brief Number of entries registered so far. |
| 374 | unsigned OffloadingEntriesNum; |
| 375 | |
| 376 | public: |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 377 | /// Base class of the entries info. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 378 | class OffloadEntryInfo { |
| 379 | public: |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 380 | /// Kind of a given entry. Currently, only target regions are |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 381 | /// supported. |
Reid Kleckner | dc78f95 | 2016-01-11 20:55:16 +0000 | [diff] [blame] | 382 | enum OffloadingEntryInfoKinds : unsigned { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 383 | // Entry is a target region. |
| 384 | OFFLOAD_ENTRY_INFO_TARGET_REGION = 0, |
| 385 | // Invalid entry info. |
| 386 | OFFLOAD_ENTRY_INFO_INVALID = ~0u |
| 387 | }; |
| 388 | |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 389 | OffloadEntryInfo() |
| 390 | : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {} |
| 391 | explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, |
| 392 | int32_t Flags) |
| 393 | : Flags(Flags), Order(Order), Kind(Kind) {} |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 394 | |
| 395 | bool isValid() const { return Order != ~0u; } |
| 396 | unsigned getOrder() const { return Order; } |
| 397 | OffloadingEntryInfoKinds getKind() const { return Kind; } |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 398 | int32_t getFlags() const { return Flags; } |
| 399 | void setFlags(int32_t NewFlags) { Flags = NewFlags; } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 400 | static bool classof(const OffloadEntryInfo *Info) { return true; } |
| 401 | |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 402 | private: |
| 403 | /// Flags associated with the device global. |
| 404 | int32_t Flags; |
| 405 | |
| 406 | /// Order this entry was emitted. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 407 | unsigned Order; |
| 408 | |
| 409 | OffloadingEntryInfoKinds Kind; |
| 410 | }; |
| 411 | |
| 412 | /// \brief Return true if a there are no entries defined. |
| 413 | bool empty() const; |
| 414 | /// \brief Return number of entries defined so far. |
| 415 | unsigned size() const { return OffloadingEntriesNum; } |
| 416 | OffloadEntriesInfoManagerTy(CodeGenModule &CGM) |
| 417 | : CGM(CGM), OffloadingEntriesNum(0) {} |
| 418 | |
| 419 | /// |
| 420 | /// Target region entries related. |
| 421 | /// |
| 422 | /// \brief Target region entries info. |
| 423 | class OffloadEntryInfoTargetRegion : public OffloadEntryInfo { |
| 424 | // \brief Address of the entity that has to be mapped for offloading. |
| 425 | llvm::Constant *Addr; |
| 426 | // \brief Address that can be used as the ID of the entry. |
| 427 | llvm::Constant *ID; |
| 428 | |
| 429 | public: |
| 430 | OffloadEntryInfoTargetRegion() |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 431 | : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u, |
| 432 | /*Flags=*/0), |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 433 | Addr(nullptr), ID(nullptr) {} |
| 434 | explicit OffloadEntryInfoTargetRegion(unsigned Order, |
| 435 | llvm::Constant *Addr, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 436 | llvm::Constant *ID, int32_t Flags) |
| 437 | : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags), |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 438 | Addr(Addr), ID(ID) {} |
| 439 | |
| 440 | llvm::Constant *getAddress() const { return Addr; } |
| 441 | llvm::Constant *getID() const { return ID; } |
| 442 | void setAddress(llvm::Constant *V) { |
| 443 | assert(!Addr && "Address as been set before!"); |
| 444 | Addr = V; |
| 445 | } |
| 446 | void setID(llvm::Constant *V) { |
| 447 | assert(!ID && "ID as been set before!"); |
| 448 | ID = V; |
| 449 | } |
| 450 | static bool classof(const OffloadEntryInfo *Info) { |
| 451 | return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION; |
| 452 | } |
| 453 | }; |
| 454 | /// \brief Initialize target region entry. |
| 455 | void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 456 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 457 | unsigned Order); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 458 | /// \brief Register target region entry. |
| 459 | void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 460 | StringRef ParentName, unsigned LineNum, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 461 | llvm::Constant *Addr, llvm::Constant *ID, |
| 462 | int32_t Flags); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 463 | /// \brief Return true if a target region entry with the provided |
| 464 | /// information exists. |
| 465 | bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 466 | StringRef ParentName, unsigned LineNum) const; |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 467 | /// brief Applies action \a Action on all registered entries. |
| 468 | typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned, |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 469 | OffloadEntryInfoTargetRegion &)> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 470 | OffloadTargetRegionEntryInfoActTy; |
| 471 | void actOnTargetRegionEntriesInfo( |
| 472 | const OffloadTargetRegionEntryInfoActTy &Action); |
| 473 | |
| 474 | private: |
| 475 | // Storage for target region entries kind. The storage is to be indexed by |
Samuel Antao | 2de62b0 | 2016-02-13 23:35:10 +0000 | [diff] [blame] | 476 | // file ID, device ID, parent function name and line number. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 477 | typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion> |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 478 | OffloadEntriesTargetRegionPerLine; |
| 479 | typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine> |
| 480 | OffloadEntriesTargetRegionPerParentName; |
| 481 | typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName> |
| 482 | OffloadEntriesTargetRegionPerFile; |
| 483 | typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile> |
| 484 | OffloadEntriesTargetRegionPerDevice; |
| 485 | typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy; |
| 486 | OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion; |
| 487 | }; |
| 488 | OffloadEntriesInfoManagerTy OffloadEntriesInfoManager; |
| 489 | |
| 490 | /// \brief Creates and registers offloading binary descriptor for the current |
| 491 | /// compilation unit. The function that does the registration is returned. |
| 492 | llvm::Function *createOffloadingBinaryDescriptorRegistration(); |
| 493 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 494 | /// \brief Creates all the offload entries in the current compilation unit |
| 495 | /// along with the associated metadata. |
| 496 | void createOffloadEntriesAndInfoMetadata(); |
| 497 | |
| 498 | /// \brief Loads all the offload entries information from the host IR |
| 499 | /// metadata. |
| 500 | void loadOffloadInfoMetadata(); |
| 501 | |
| 502 | /// \brief Returns __tgt_offload_entry type. |
| 503 | QualType getTgtOffloadEntryQTy(); |
| 504 | |
| 505 | /// \brief Returns __tgt_device_image type. |
| 506 | QualType getTgtDeviceImageQTy(); |
| 507 | |
| 508 | /// \brief Returns __tgt_bin_desc type. |
| 509 | QualType getTgtBinaryDescriptorQTy(); |
| 510 | |
| 511 | /// \brief Start scanning from statement \a S and and emit all target regions |
| 512 | /// found along the way. |
| 513 | /// \param S Starting statement. |
| 514 | /// \param ParentName Name of the function declaration that is being scanned. |
| 515 | void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 516 | |
| 517 | /// \brief Build type kmp_routine_entry_t (if not built yet). |
| 518 | void emitKmpRoutineEntryT(QualType KmpInt32Ty); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 519 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 520 | /// \brief Returns pointer to kmpc_micro type. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 521 | llvm::Type *getKmpc_MicroPointerTy(); |
| 522 | |
| 523 | /// \brief Returns specified OpenMP runtime function. |
| 524 | /// \param Function OpenMP runtime function. |
| 525 | /// \return Specified function. |
Alexey Bataev | 50b3c95 | 2016-02-19 10:38:26 +0000 | [diff] [blame] | 526 | llvm::Constant *createRuntimeFunction(unsigned Function); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 527 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 528 | /// \brief Returns __kmpc_for_static_init_* runtime function for the specified |
| 529 | /// size \a IVSize and sign \a IVSigned. |
| 530 | llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned); |
| 531 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 532 | /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified |
| 533 | /// size \a IVSize and sign \a IVSigned. |
| 534 | llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned); |
| 535 | |
| 536 | /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified |
| 537 | /// size \a IVSize and sign \a IVSigned. |
| 538 | llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned); |
| 539 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 540 | /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified |
| 541 | /// size \a IVSize and sign \a IVSigned. |
| 542 | llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned); |
| 543 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 544 | /// \brief If the specified mangled name is not in the module, create and |
| 545 | /// return threadprivate cache object. This object is a pointer's worth of |
| 546 | /// storage that's reserved for use by the OpenMP runtime. |
NAKAMURA Takumi | cdcbfba | 2014-11-11 07:58:06 +0000 | [diff] [blame] | 547 | /// \param VD Threadprivate variable. |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 548 | /// \return Cache variable for the specified threadprivate. |
| 549 | llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD); |
| 550 | |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 551 | /// \brief Emits address of the word in a memory where current thread id is |
| 552 | /// stored. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 553 | virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 554 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 555 | /// \brief Gets (if variable with the given name already exist) or creates |
| 556 | /// internal global variable with the specified Name. The created variable has |
| 557 | /// linkage CommonLinkage by default and is initialized by null value. |
| 558 | /// \param Ty Type of the global variable. If it is exist already the type |
| 559 | /// must be the same. |
| 560 | /// \param Name Name of the variable. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 561 | llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 562 | const llvm::Twine &Name); |
| 563 | |
| 564 | /// \brief Set of threadprivate variables with the generated initializer. |
Benjamin Kramer | 8fdba91 | 2016-02-02 14:24:21 +0000 | [diff] [blame] | 565 | llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 566 | |
| 567 | /// \brief Emits initialization code for the threadprivate variables. |
| 568 | /// \param VDAddr Address of the global variable \a VD. |
| 569 | /// \param Ctor Pointer to a global init function for \a VD. |
| 570 | /// \param CopyCtor Pointer to a global copy function for \a VD. |
| 571 | /// \param Dtor Pointer to a global destructor function for \a VD. |
| 572 | /// \param Loc Location of threadprivate declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 573 | void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 574 | llvm::Value *Ctor, llvm::Value *CopyCtor, |
| 575 | llvm::Value *Dtor, SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 576 | |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 577 | /// \brief Returns corresponding lock object for the specified critical region |
| 578 | /// name. If the lock object does not exist it is created, otherwise the |
| 579 | /// reference to the existing copy is returned. |
| 580 | /// \param CriticalName Name of the critical region. |
| 581 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 582 | llvm::Value *getCriticalRegionLock(StringRef CriticalName); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 583 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 584 | struct TaskResultTy { |
| 585 | llvm::Value *NewTask = nullptr; |
| 586 | llvm::Value *TaskEntry = nullptr; |
| 587 | llvm::Value *NewTaskNewTaskTTy = nullptr; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 588 | LValue TDBase; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 589 | RecordDecl *KmpTaskTQTyRD = nullptr; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 590 | llvm::Value *TaskDupFn = nullptr; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 591 | }; |
| 592 | /// Emit task region for the task directive. The task region is emitted in |
| 593 | /// several steps: |
| 594 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
| 595 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 596 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
| 597 | /// function: |
| 598 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
| 599 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
| 600 | /// return 0; |
| 601 | /// } |
| 602 | /// 2. Copy a list of shared variables to field shareds of the resulting |
| 603 | /// structure kmp_task_t returned by the previous call (if any). |
| 604 | /// 3. Copy a pointer to destructions function to field destructions of the |
| 605 | /// resulting structure kmp_task_t. |
| 606 | /// \param D Current task directive. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 607 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
| 608 | /// /*part_id*/, captured_struct */*__context*/); |
| 609 | /// \param SharedsTy A type which contains references the shared variables. |
| 610 | /// \param Shareds Context with the list of shared variables from the \p |
| 611 | /// TaskFunction. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 612 | /// \param Data Additional data for task generation like tiednsee, final |
| 613 | /// state, list of privates etc. |
| 614 | TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
| 615 | const OMPExecutableDirective &D, |
| 616 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 617 | Address Shareds, const OMPTaskDataTy &Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 618 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 619 | public: |
| 620 | explicit CGOpenMPRuntime(CodeGenModule &CGM); |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 621 | virtual ~CGOpenMPRuntime() {} |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 622 | virtual void clear(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 623 | |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 624 | /// Emit code for the specified user defined reduction construct. |
| 625 | virtual void emitUserDefinedReduction(CodeGenFunction *CGF, |
| 626 | const OMPDeclareReductionDecl *D); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 627 | /// Get combiner/initializer for the specified user-defined reduction, if any. |
| 628 | virtual std::pair<llvm::Function *, llvm::Function *> |
| 629 | getUserDefinedReduction(const OMPDeclareReductionDecl *D); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 630 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 631 | /// \brief Emits outlined function for the specified OpenMP parallel directive |
| 632 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 633 | /// kmp_int32 BoundID, struct context_vars*). |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 634 | /// \param D OpenMP directive. |
| 635 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 636 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 637 | /// is a directive itself, for combined - its innermost directive). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 638 | /// \param CodeGen Code generation sequence for the \a D directive. |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 639 | virtual llvm::Value *emitParallelOutlinedFunction( |
| 640 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 641 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen); |
| 642 | |
| 643 | /// \brief Emits outlined function for the specified OpenMP teams directive |
| 644 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 645 | /// kmp_int32 BoundID, struct context_vars*). |
| 646 | /// \param D OpenMP directive. |
| 647 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 648 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 649 | /// is a directive itself, for combined - its innermost directive). |
| 650 | /// \param CodeGen Code generation sequence for the \a D directive. |
| 651 | virtual llvm::Value *emitTeamsOutlinedFunction( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 652 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 653 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 654 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 655 | /// \brief Emits outlined function for the OpenMP task directive \a D. This |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 656 | /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t* |
| 657 | /// TaskT). |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 658 | /// \param D OpenMP directive. |
| 659 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 660 | /// \param PartIDVar Variable for partition id in the current OpenMP untied |
| 661 | /// task region. |
| 662 | /// \param TaskTVar Variable for task_t argument. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 663 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 664 | /// is a directive itself, for combined - its innermost directive). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 665 | /// \param CodeGen Code generation sequence for the \a D directive. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 666 | /// \param Tied true if task is generated for tied task, false otherwise. |
| 667 | /// \param NumberOfParts Number of parts in untied task. Ignored for tied |
| 668 | /// tasks. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 669 | /// |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 670 | virtual llvm::Value *emitTaskOutlinedFunction( |
| 671 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 672 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
| 673 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 674 | bool Tied, unsigned &NumberOfParts); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 675 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 676 | /// \brief Cleans up references to the objects in finished function. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 677 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 678 | void functionFinished(CodeGenFunction &CGF); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 679 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 680 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 681 | /// variables captured in a record which address is stored in \a |
| 682 | /// CapturedStruct. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 683 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 684 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
NAKAMURA Takumi | 62f0eb5 | 2015-09-11 08:13:32 +0000 | [diff] [blame] | 685 | /// \param CapturedVars A pointer to the record with the references to |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 686 | /// variables used in \a OutlinedFn function. |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 687 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 688 | /// specified, nullptr otherwise. |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 689 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 690 | virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 691 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 692 | ArrayRef<llvm::Value *> CapturedVars, |
| 693 | const Expr *IfCond); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 694 | |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 695 | /// \brief Emits a critical region. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 696 | /// \param CriticalName Name of the critical region. |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 697 | /// \param CriticalOpGen Generator for the statement associated with the given |
| 698 | /// critical region. |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 699 | /// \param Hint Value of the 'hint' clause (optional). |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 700 | virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 701 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 702 | SourceLocation Loc, |
| 703 | const Expr *Hint = nullptr); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 704 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 705 | /// \brief Emits a master region. |
| 706 | /// \param MasterOpGen Generator for the statement associated with the given |
| 707 | /// master region. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 708 | virtual void emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 709 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 710 | SourceLocation Loc); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 711 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 712 | /// \brief Emits code for a taskyield directive. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 713 | virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 714 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 715 | /// \brief Emit a taskgroup region. |
| 716 | /// \param TaskgroupOpGen Generator for the statement associated with the |
| 717 | /// given taskgroup region. |
| 718 | virtual void emitTaskgroupRegion(CodeGenFunction &CGF, |
| 719 | const RegionCodeGenTy &TaskgroupOpGen, |
| 720 | SourceLocation Loc); |
| 721 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 722 | /// \brief Emits a single region. |
| 723 | /// \param SingleOpGen Generator for the statement associated with the given |
| 724 | /// single region. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 725 | virtual void emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 726 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 727 | SourceLocation Loc, |
| 728 | ArrayRef<const Expr *> CopyprivateVars, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 729 | ArrayRef<const Expr *> DestExprs, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 730 | ArrayRef<const Expr *> SrcExprs, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 731 | ArrayRef<const Expr *> AssignmentOps); |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 732 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 733 | /// \brief Emit an ordered region. |
| 734 | /// \param OrderedOpGen Generator for the statement associated with the given |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 735 | /// ordered region. |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 736 | virtual void emitOrderedRegion(CodeGenFunction &CGF, |
| 737 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 738 | SourceLocation Loc, bool IsThreads); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 739 | |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 740 | /// \brief Emit an implicit/explicit barrier for OpenMP threads. |
| 741 | /// \param Kind Directive for which this implicit barrier call must be |
| 742 | /// generated. Must be OMPD_barrier for explicit barrier generation. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 743 | /// \param EmitChecks true if need to emit checks for cancellation barriers. |
| 744 | /// \param ForceSimpleCall true simple barrier call must be emitted, false if |
| 745 | /// runtime class decides which one to emit (simple or with cancellation |
| 746 | /// checks). |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 747 | /// |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 748 | virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 749 | OpenMPDirectiveKind Kind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 750 | bool EmitChecks = true, |
| 751 | bool ForceSimpleCall = false); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 752 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 753 | /// \brief Check if the specified \a ScheduleKind is static non-chunked. |
| 754 | /// This kind of worksharing directive is emitted without outer loop. |
| 755 | /// \param ScheduleKind Schedule kind specified in the 'schedule' clause. |
| 756 | /// \param Chunked True if chunk is specified in the clause. |
| 757 | /// |
| 758 | virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 759 | bool Chunked) const; |
| 760 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 761 | /// \brief Check if the specified \a ScheduleKind is static non-chunked. |
| 762 | /// This kind of distribute directive is emitted without outer loop. |
| 763 | /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause. |
| 764 | /// \param Chunked True if chunk is specified in the clause. |
| 765 | /// |
| 766 | virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind, |
| 767 | bool Chunked) const; |
| 768 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 769 | /// \brief Check if the specified \a ScheduleKind is dynamic. |
| 770 | /// This kind of worksharing directive is emitted without outer loop. |
| 771 | /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause. |
| 772 | /// |
| 773 | virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const; |
| 774 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 775 | /// struct with the values to be passed to the dispatch runtime function |
| 776 | struct DispatchRTInput { |
| 777 | /// Loop lower bound |
| 778 | llvm::Value *LB = nullptr; |
| 779 | /// Loop upper bound |
| 780 | llvm::Value *UB = nullptr; |
| 781 | /// Chunk size specified using 'schedule' clause (nullptr if chunk |
| 782 | /// was not specified) |
| 783 | llvm::Value *Chunk = nullptr; |
| 784 | DispatchRTInput() = default; |
| 785 | DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk) |
| 786 | : LB(LB), UB(UB), Chunk(Chunk) {} |
| 787 | }; |
| 788 | |
| 789 | /// Call the appropriate runtime routine to initialize it before start |
| 790 | /// of loop. |
| 791 | |
| 792 | /// This is used for non static scheduled types and when the ordered |
| 793 | /// clause is present on the loop construct. |
| 794 | /// Depending on the loop schedule, it is necessary to call some runtime |
| 795 | /// routine before start of the OpenMP loop to get the loop upper / lower |
| 796 | /// bounds \a LB and \a UB and stride \a ST. |
| 797 | /// |
| 798 | /// \param CGF Reference to current CodeGenFunction. |
| 799 | /// \param Loc Clang source location. |
| 800 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
| 801 | /// \param IVSize Size of the iteration variable in bits. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 802 | /// \param IVSigned Sign of the iteration variable. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 803 | /// \param Ordered true if loop is ordered, false otherwise. |
| 804 | /// \param DispatchValues struct containing llvm values for lower bound, upper |
| 805 | /// bound, and chunk expression. |
| 806 | /// For the default (nullptr) value, the chunk 1 will be used. |
| 807 | /// |
NAKAMURA Takumi | ff7a925 | 2015-09-08 09:42:41 +0000 | [diff] [blame] | 808 | virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 809 | const OpenMPScheduleTy &ScheduleKind, |
| 810 | unsigned IVSize, bool IVSigned, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 811 | const DispatchRTInput &DispatchValues); |
NAKAMURA Takumi | ff7a925 | 2015-09-08 09:42:41 +0000 | [diff] [blame] | 812 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 813 | /// Struct with the values to be passed to the static runtime function |
| 814 | struct StaticRTInput { |
| 815 | /// Size of the iteration variable in bits. |
| 816 | unsigned IVSize = 0; |
| 817 | /// Sign of the iteration variable. |
| 818 | bool IVSigned = false; |
| 819 | /// true if loop is ordered, false otherwise. |
| 820 | bool Ordered = false; |
| 821 | /// Address of the output variable in which the flag of the last iteration |
| 822 | /// is returned. |
| 823 | Address IL = Address::invalid(); |
| 824 | /// Address of the output variable in which the lower iteration number is |
| 825 | /// returned. |
| 826 | Address LB = Address::invalid(); |
| 827 | /// Address of the output variable in which the upper iteration number is |
| 828 | /// returned. |
| 829 | Address UB = Address::invalid(); |
| 830 | /// Address of the output variable in which the stride value is returned |
| 831 | /// necessary to generated the static_chunked scheduled loop. |
| 832 | Address ST = Address::invalid(); |
| 833 | /// Value of the chunk for the static_chunked scheduled loop. For the |
| 834 | /// default (nullptr) value, the chunk 1 will be used. |
| 835 | llvm::Value *Chunk = nullptr; |
| 836 | StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL, |
| 837 | Address LB, Address UB, Address ST, |
| 838 | llvm::Value *Chunk = nullptr) |
| 839 | : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB), |
| 840 | UB(UB), ST(ST), Chunk(Chunk) {} |
| 841 | }; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 842 | /// \brief Call the appropriate runtime routine to initialize it before start |
| 843 | /// of loop. |
| 844 | /// |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 845 | /// This is used only in case of static schedule, when the user did not |
| 846 | /// specify a ordered clause on the loop construct. |
| 847 | /// Depending on the loop schedule, it is necessary to call some runtime |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 848 | /// routine before start of the OpenMP loop to get the loop upper / lower |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 849 | /// bounds LB and UB and stride ST. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 850 | /// |
| 851 | /// \param CGF Reference to current CodeGenFunction. |
| 852 | /// \param Loc Clang source location. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 853 | /// \param DKind Kind of the directive. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 854 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 855 | /// \param Values Input arguments for the construct. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 856 | /// |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 857 | virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 858 | OpenMPDirectiveKind DKind, |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 859 | const OpenMPScheduleTy &ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 860 | const StaticRTInput &Values); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 861 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 862 | /// |
| 863 | /// \param CGF Reference to current CodeGenFunction. |
| 864 | /// \param Loc Clang source location. |
| 865 | /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 866 | /// \param Values Input arguments for the construct. |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 867 | /// |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 868 | virtual void emitDistributeStaticInit(CodeGenFunction &CGF, |
| 869 | SourceLocation Loc, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 870 | OpenMPDistScheduleClauseKind SchedKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 871 | const StaticRTInput &Values); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 872 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 873 | /// \brief Call the appropriate runtime routine to notify that we finished |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 874 | /// iteration of the ordered loop with the dynamic scheduling. |
| 875 | /// |
| 876 | /// \param CGF Reference to current CodeGenFunction. |
| 877 | /// \param Loc Clang source location. |
| 878 | /// \param IVSize Size of the iteration variable in bits. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 879 | /// \param IVSigned Sign of the iteration variable. |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 880 | /// |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 881 | virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 882 | SourceLocation Loc, unsigned IVSize, |
| 883 | bool IVSigned); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 884 | |
| 885 | /// \brief Call the appropriate runtime routine to notify that we finished |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 886 | /// all the work with current loop. |
| 887 | /// |
| 888 | /// \param CGF Reference to current CodeGenFunction. |
| 889 | /// \param Loc Clang source location. |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 890 | /// \param DKind Kind of the directive for which the static finish is emitted. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 891 | /// |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 892 | virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc, |
| 893 | OpenMPDirectiveKind DKind); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 894 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 895 | /// Call __kmpc_dispatch_next( |
| 896 | /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 897 | /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 898 | /// kmp_int[32|64] *p_stride); |
| 899 | /// \param IVSize Size of the iteration variable in bits. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 900 | /// \param IVSigned Sign of the iteration variable. |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 901 | /// \param IL Address of the output variable in which the flag of the |
| 902 | /// last iteration is returned. |
| 903 | /// \param LB Address of the output variable in which the lower iteration |
| 904 | /// number is returned. |
| 905 | /// \param UB Address of the output variable in which the upper iteration |
| 906 | /// number is returned. |
| 907 | /// \param ST Address of the output variable in which the stride value is |
| 908 | /// returned. |
| 909 | virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc, |
| 910 | unsigned IVSize, bool IVSigned, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 911 | Address IL, Address LB, |
| 912 | Address UB, Address ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 913 | |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 914 | /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
| 915 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
| 916 | /// clause. |
| 917 | /// \param NumThreads An integer value of threads. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 918 | virtual void emitNumThreadsClause(CodeGenFunction &CGF, |
| 919 | llvm::Value *NumThreads, |
| 920 | SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 921 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 922 | /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 |
| 923 | /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. |
| 924 | virtual void emitProcBindClause(CodeGenFunction &CGF, |
| 925 | OpenMPProcBindClauseKind ProcBind, |
| 926 | SourceLocation Loc); |
| 927 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 928 | /// \brief Returns address of the threadprivate variable for the current |
| 929 | /// thread. |
NAKAMURA Takumi | cdcbfba | 2014-11-11 07:58:06 +0000 | [diff] [blame] | 930 | /// \param VD Threadprivate variable. |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 931 | /// \param VDAddr Address of the global variable \a VD. |
| 932 | /// \param Loc Location of the reference to threadprivate var. |
| 933 | /// \return Address of the threadprivate variable for the current thread. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 934 | virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 935 | const VarDecl *VD, |
| 936 | Address VDAddr, |
| 937 | SourceLocation Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 938 | |
| 939 | /// \brief Emit a code for initialization of threadprivate variable. It emits |
| 940 | /// a call to runtime library which adds initial value to the newly created |
| 941 | /// threadprivate variable (if it is not constant) and registers destructor |
| 942 | /// for the variable (if any). |
| 943 | /// \param VD Threadprivate variable. |
| 944 | /// \param VDAddr Address of the global variable \a VD. |
| 945 | /// \param Loc Location of threadprivate declaration. |
| 946 | /// \param PerformInit true if initialization expression is not constant. |
| 947 | virtual llvm::Function * |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 948 | emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 949 | SourceLocation Loc, bool PerformInit, |
| 950 | CodeGenFunction *CGF = nullptr); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 951 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 952 | /// Creates artificial threadprivate variable with name \p Name and type \p |
| 953 | /// VarType. |
| 954 | /// \param VarType Type of the artificial threadprivate variable. |
| 955 | /// \param Name Name of the artificial threadprivate variable. |
| 956 | virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
| 957 | QualType VarType, |
| 958 | StringRef Name); |
| 959 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 960 | /// \brief Emit flush of the variables specified in 'omp flush' directive. |
| 961 | /// \param Vars List of variables to flush. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 962 | virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars, |
| 963 | SourceLocation Loc); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 964 | |
| 965 | /// \brief Emit task region for the task directive. The task region is |
Nico Weber | 20b0ce3 | 2015-04-28 18:19:18 +0000 | [diff] [blame] | 966 | /// emitted in several steps: |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 967 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
| 968 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 969 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
| 970 | /// function: |
| 971 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
| 972 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
| 973 | /// return 0; |
| 974 | /// } |
| 975 | /// 2. Copy a list of shared variables to field shareds of the resulting |
| 976 | /// structure kmp_task_t returned by the previous call (if any). |
| 977 | /// 3. Copy a pointer to destructions function to field destructions of the |
| 978 | /// resulting structure kmp_task_t. |
| 979 | /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, |
| 980 | /// kmp_task_t *new_task), where new_task is a resulting structure from |
| 981 | /// previous items. |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 982 | /// \param D Current task directive. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 983 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
| 984 | /// /*part_id*/, captured_struct */*__context*/); |
| 985 | /// \param SharedsTy A type which contains references the shared variables. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 986 | /// \param Shareds Context with the list of shared variables from the \p |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 987 | /// TaskFunction. |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 988 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
| 989 | /// otherwise. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 990 | /// \param Data Additional data for task generation like tiednsee, final |
| 991 | /// state, list of privates etc. |
| 992 | virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 993 | const OMPExecutableDirective &D, |
| 994 | llvm::Value *TaskFunction, QualType SharedsTy, |
| 995 | Address Shareds, const Expr *IfCond, |
| 996 | const OMPTaskDataTy &Data); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 997 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 998 | /// Emit task region for the taskloop directive. The taskloop region is |
| 999 | /// emitted in several steps: |
| 1000 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
| 1001 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 1002 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
| 1003 | /// function: |
| 1004 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
| 1005 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
| 1006 | /// return 0; |
| 1007 | /// } |
| 1008 | /// 2. Copy a list of shared variables to field shareds of the resulting |
| 1009 | /// structure kmp_task_t returned by the previous call (if any). |
| 1010 | /// 3. Copy a pointer to destructions function to field destructions of the |
| 1011 | /// resulting structure kmp_task_t. |
| 1012 | /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t |
| 1013 | /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int |
| 1014 | /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task |
| 1015 | /// is a resulting structure from |
| 1016 | /// previous items. |
| 1017 | /// \param D Current task directive. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1018 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
| 1019 | /// /*part_id*/, captured_struct */*__context*/); |
| 1020 | /// \param SharedsTy A type which contains references the shared variables. |
| 1021 | /// \param Shareds Context with the list of shared variables from the \p |
| 1022 | /// TaskFunction. |
| 1023 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
| 1024 | /// otherwise. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 1025 | /// \param Data Additional data for task generation like tiednsee, final |
| 1026 | /// state, list of privates etc. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1027 | virtual void emitTaskLoopCall( |
| 1028 | CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 1029 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
| 1030 | const Expr *IfCond, const OMPTaskDataTy &Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1031 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1032 | /// \brief Emit code for the directive that does not require outlining. |
| 1033 | /// |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1034 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 1035 | /// is a directive itself, for combined - its innermost directive). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1036 | /// \param CodeGen Code generation sequence for the \a D directive. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1037 | /// \param HasCancel true if region has inner cancel directive, false |
| 1038 | /// otherwise. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1039 | virtual void emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1040 | OpenMPDirectiveKind InnermostKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1041 | const RegionCodeGenTy &CodeGen, |
| 1042 | bool HasCancel = false); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1043 | |
| 1044 | /// Emits reduction function. |
| 1045 | /// \param ArgsType Array type containing pointers to reduction variables. |
| 1046 | /// \param Privates List of private copies for original reduction arguments. |
| 1047 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
| 1048 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
| 1049 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
| 1050 | /// or 'operator binop(LHS, RHS)'. |
| 1051 | llvm::Value *emitReductionFunction(CodeGenModule &CGM, llvm::Type *ArgsType, |
| 1052 | ArrayRef<const Expr *> Privates, |
| 1053 | ArrayRef<const Expr *> LHSExprs, |
| 1054 | ArrayRef<const Expr *> RHSExprs, |
| 1055 | ArrayRef<const Expr *> ReductionOps); |
| 1056 | |
| 1057 | /// Emits single reduction combiner |
| 1058 | void emitSingleReductionCombiner(CodeGenFunction &CGF, |
| 1059 | const Expr *ReductionOp, |
| 1060 | const Expr *PrivateRef, |
| 1061 | const DeclRefExpr *LHS, |
| 1062 | const DeclRefExpr *RHS); |
| 1063 | |
| 1064 | struct ReductionOptionsTy { |
| 1065 | bool WithNowait; |
| 1066 | bool SimpleReduction; |
| 1067 | OpenMPDirectiveKind ReductionKind; |
| 1068 | }; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1069 | /// \brief Emit a code for reduction clause. Next code should be emitted for |
| 1070 | /// reduction: |
| 1071 | /// \code |
| 1072 | /// |
| 1073 | /// static kmp_critical_name lock = { 0 }; |
| 1074 | /// |
| 1075 | /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 1076 | /// ... |
| 1077 | /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 1078 | /// ... |
| 1079 | /// } |
| 1080 | /// |
| 1081 | /// ... |
| 1082 | /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 1083 | /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 1084 | /// RedList, reduce_func, &<lock>)) { |
| 1085 | /// case 1: |
| 1086 | /// ... |
| 1087 | /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 1088 | /// ... |
| 1089 | /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 1090 | /// break; |
| 1091 | /// case 2: |
| 1092 | /// ... |
| 1093 | /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 1094 | /// ... |
| 1095 | /// break; |
| 1096 | /// default:; |
| 1097 | /// } |
| 1098 | /// \endcode |
| 1099 | /// |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1100 | /// \param Privates List of private copies for original reduction arguments. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1101 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
| 1102 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
| 1103 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
| 1104 | /// or 'operator binop(LHS, RHS)'. |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1105 | /// \param Options List of options for reduction codegen: |
| 1106 | /// WithNowait true if parent directive has also nowait clause, false |
| 1107 | /// otherwise. |
| 1108 | /// SimpleReduction Emit reduction operation only. Used for omp simd |
| 1109 | /// directive on the host. |
| 1110 | /// ReductionKind The kind of reduction to perform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1111 | virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1112 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1113 | ArrayRef<const Expr *> LHSExprs, |
| 1114 | ArrayRef<const Expr *> RHSExprs, |
| 1115 | ArrayRef<const Expr *> ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1116 | ReductionOptionsTy Options); |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1117 | |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 1118 | /// Emit a code for initialization of task reduction clause. Next code |
| 1119 | /// should be emitted for reduction: |
| 1120 | /// \code |
| 1121 | /// |
| 1122 | /// _task_red_item_t red_data[n]; |
| 1123 | /// ... |
| 1124 | /// red_data[i].shar = &origs[i]; |
| 1125 | /// red_data[i].size = sizeof(origs[i]); |
| 1126 | /// red_data[i].f_init = (void*)RedInit<i>; |
| 1127 | /// red_data[i].f_fini = (void*)RedDest<i>; |
| 1128 | /// red_data[i].f_comb = (void*)RedOp<i>; |
| 1129 | /// red_data[i].flags = <Flag_i>; |
| 1130 | /// ... |
| 1131 | /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data); |
| 1132 | /// \endcode |
| 1133 | /// |
| 1134 | /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations. |
| 1135 | /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations. |
| 1136 | /// \param Data Additional data for task generation like tiedness, final |
| 1137 | /// state, list of privates, reductions etc. |
| 1138 | virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, |
| 1139 | SourceLocation Loc, |
| 1140 | ArrayRef<const Expr *> LHSExprs, |
| 1141 | ArrayRef<const Expr *> RHSExprs, |
| 1142 | const OMPTaskDataTy &Data); |
| 1143 | |
| 1144 | /// Required to resolve existing problems in the runtime. Emits threadprivate |
| 1145 | /// variables to store the size of the VLAs/array sections for |
| 1146 | /// initializer/combiner/finalizer functions + emits threadprivate variable to |
| 1147 | /// store the pointer to the original reduction item for the custom |
| 1148 | /// initializer defined by declare reduction construct. |
| 1149 | /// \param RCG Allows to reuse an existing data for the reductions. |
| 1150 | /// \param N Reduction item for which fixups must be emitted. |
| 1151 | virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc, |
| 1152 | ReductionCodeGen &RCG, unsigned N); |
| 1153 | |
| 1154 | /// Get the address of `void *` type of the privatue copy of the reduction |
| 1155 | /// item specified by the \p SharedLVal. |
| 1156 | /// \param ReductionsPtr Pointer to the reduction data returned by the |
| 1157 | /// emitTaskReductionInit function. |
| 1158 | /// \param SharedLVal Address of the original reduction item. |
| 1159 | virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc, |
| 1160 | llvm::Value *ReductionsPtr, |
| 1161 | LValue SharedLVal); |
| 1162 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1163 | /// \brief Emit code for 'taskwait' directive. |
| 1164 | virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1165 | |
| 1166 | /// \brief Emit code for 'cancellation point' construct. |
| 1167 | /// \param CancelRegion Region kind for which the cancellation point must be |
| 1168 | /// emitted. |
| 1169 | /// |
| 1170 | virtual void emitCancellationPointCall(CodeGenFunction &CGF, |
| 1171 | SourceLocation Loc, |
| 1172 | OpenMPDirectiveKind CancelRegion); |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1173 | |
| 1174 | /// \brief Emit code for 'cancel' construct. |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 1175 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 1176 | /// specified, nullptr otherwise. |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1177 | /// \param CancelRegion Region kind for which the cancel must be emitted. |
| 1178 | /// |
| 1179 | virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 1180 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 1181 | OpenMPDirectiveKind CancelRegion); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 1182 | |
| 1183 | /// \brief Emit outilined function for 'target' directive. |
| 1184 | /// \param D Directive to emit. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1185 | /// \param ParentName Name of the function that encloses the target region. |
| 1186 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 1187 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 1188 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1189 | /// \param CodeGen Code generation sequence for the \a D directive. |
Simon Pilgrim | 6c0eeff | 2017-07-13 17:34:44 +0000 | [diff] [blame] | 1190 | /// An outlined function may not be an entry if, e.g. the if clause always |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1191 | /// evaluates to false. |
| 1192 | virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D, |
| 1193 | StringRef ParentName, |
| 1194 | llvm::Function *&OutlinedFn, |
| 1195 | llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1196 | bool IsOffloadEntry, |
| 1197 | const RegionCodeGenTy &CodeGen); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 1198 | |
| 1199 | /// \brief Emit the target offloading code associated with \a D. The emitted |
| 1200 | /// code attempts offloading the execution to the device, an the event of |
| 1201 | /// a failure it executes the host version outlined in \a OutlinedFn. |
| 1202 | /// \param D Directive to emit. |
| 1203 | /// \param OutlinedFn Host version of the code to be offloaded. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1204 | /// \param OutlinedFnID ID of host version of the code to be offloaded. |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 1205 | /// \param IfCond Expression evaluated in if clause associated with the target |
| 1206 | /// directive, or null if no if clause is used. |
| 1207 | /// \param Device Expression evaluated in device clause associated with the |
| 1208 | /// target directive, or null if no device clause is used. |
| 1209 | /// \param CapturedVars Values captured in the current region. |
| 1210 | virtual void emitTargetCall(CodeGenFunction &CGF, |
| 1211 | const OMPExecutableDirective &D, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1212 | llvm::Value *OutlinedFn, |
| 1213 | llvm::Value *OutlinedFnID, const Expr *IfCond, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 1214 | const Expr *Device, |
| 1215 | ArrayRef<llvm::Value *> CapturedVars); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1216 | |
| 1217 | /// \brief Emit the target regions enclosed in \a GD function definition or |
| 1218 | /// the function itself in case it is a valid device function. Returns true if |
| 1219 | /// \a GD was dealt with successfully. |
Nico Weber | a2abe8c | 2016-01-06 19:13:49 +0000 | [diff] [blame] | 1220 | /// \param GD Function to scan. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1221 | virtual bool emitTargetFunctions(GlobalDecl GD); |
| 1222 | |
| 1223 | /// \brief Emit the global variable if it is a valid device global variable. |
| 1224 | /// Returns true if \a GD was dealt with successfully. |
| 1225 | /// \param GD Variable declaration to emit. |
| 1226 | virtual bool emitTargetGlobalVariable(GlobalDecl GD); |
| 1227 | |
| 1228 | /// \brief Emit the global \a GD if it is meaningful for the target. Returns |
Simon Pilgrim | 2c51880 | 2017-03-30 14:13:19 +0000 | [diff] [blame] | 1229 | /// if it was emitted successfully. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1230 | /// \param GD Global to scan. |
| 1231 | virtual bool emitTargetGlobal(GlobalDecl GD); |
| 1232 | |
| 1233 | /// \brief Creates the offloading descriptor in the event any target region |
| 1234 | /// was emitted in the current module and return the function that registers |
| 1235 | /// it. |
| 1236 | virtual llvm::Function *emitRegistrationFunction(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 1237 | |
| 1238 | /// \brief Emits code for teams call of the \a OutlinedFn with |
| 1239 | /// variables captured in a record which address is stored in \a |
| 1240 | /// CapturedStruct. |
| 1241 | /// \param OutlinedFn Outlined function to be run by team masters. Type of |
| 1242 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 1243 | /// \param CapturedVars A pointer to the record with the references to |
| 1244 | /// variables used in \a OutlinedFn function. |
| 1245 | /// |
| 1246 | virtual void emitTeamsCall(CodeGenFunction &CGF, |
| 1247 | const OMPExecutableDirective &D, |
| 1248 | SourceLocation Loc, llvm::Value *OutlinedFn, |
| 1249 | ArrayRef<llvm::Value *> CapturedVars); |
| 1250 | |
| 1251 | /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 |
| 1252 | /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code |
| 1253 | /// for num_teams clause. |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 1254 | /// \param NumTeams An integer expression of teams. |
| 1255 | /// \param ThreadLimit An integer expression of threads. |
| 1256 | virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, |
| 1257 | const Expr *ThreadLimit, SourceLocation Loc); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 1258 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 1259 | /// Struct that keeps all the relevant information that should be kept |
| 1260 | /// throughout a 'target data' region. |
| 1261 | class TargetDataInfo { |
| 1262 | /// Set to true if device pointer information have to be obtained. |
| 1263 | bool RequiresDevicePointerInfo = false; |
| 1264 | |
| 1265 | public: |
| 1266 | /// The array of base pointer passed to the runtime library. |
| 1267 | llvm::Value *BasePointersArray = nullptr; |
| 1268 | /// The array of section pointers passed to the runtime library. |
| 1269 | llvm::Value *PointersArray = nullptr; |
| 1270 | /// The array of sizes passed to the runtime library. |
| 1271 | llvm::Value *SizesArray = nullptr; |
| 1272 | /// The array of map types passed to the runtime library. |
| 1273 | llvm::Value *MapTypesArray = nullptr; |
| 1274 | /// The total number of pointers passed to the runtime library. |
| 1275 | unsigned NumberOfPtrs = 0u; |
| 1276 | /// Map between the a declaration of a capture and the corresponding base |
| 1277 | /// pointer address where the runtime returns the device pointers. |
| 1278 | llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap; |
| 1279 | |
| 1280 | explicit TargetDataInfo() {} |
| 1281 | explicit TargetDataInfo(bool RequiresDevicePointerInfo) |
| 1282 | : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {} |
| 1283 | /// Clear information about the data arrays. |
| 1284 | void clearArrayInfo() { |
| 1285 | BasePointersArray = nullptr; |
| 1286 | PointersArray = nullptr; |
| 1287 | SizesArray = nullptr; |
| 1288 | MapTypesArray = nullptr; |
| 1289 | NumberOfPtrs = 0u; |
| 1290 | } |
| 1291 | /// Return true if the current target data information has valid arrays. |
| 1292 | bool isValid() { |
| 1293 | return BasePointersArray && PointersArray && SizesArray && |
| 1294 | MapTypesArray && NumberOfPtrs; |
| 1295 | } |
| 1296 | bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; } |
| 1297 | }; |
| 1298 | |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 1299 | /// \brief Emit the target data mapping code associated with \a D. |
| 1300 | /// \param D Directive to emit. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 1301 | /// \param IfCond Expression evaluated in if clause associated with the |
| 1302 | /// target directive, or null if no device clause is used. |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 1303 | /// \param Device Expression evaluated in device clause associated with the |
| 1304 | /// target directive, or null if no device clause is used. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 1305 | /// \param Info A record used to store information that needs to be preserved |
| 1306 | /// until the region is closed. |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 1307 | virtual void emitTargetDataCalls(CodeGenFunction &CGF, |
| 1308 | const OMPExecutableDirective &D, |
| 1309 | const Expr *IfCond, const Expr *Device, |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 1310 | const RegionCodeGenTy &CodeGen, |
| 1311 | TargetDataInfo &Info); |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 1312 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 1313 | /// \brief Emit the data mapping/movement code associated with the directive |
| 1314 | /// \a D that should be of the form 'target [{enter|exit} data | update]'. |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 1315 | /// \param D Directive to emit. |
| 1316 | /// \param IfCond Expression evaluated in if clause associated with the target |
| 1317 | /// directive, or null if no if clause is used. |
| 1318 | /// \param Device Expression evaluated in device clause associated with the |
| 1319 | /// target directive, or null if no device clause is used. |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 1320 | virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF, |
| 1321 | const OMPExecutableDirective &D, |
| 1322 | const Expr *IfCond, |
| 1323 | const Expr *Device); |
Alexey Bataev | c7a82b4 | 2016-05-06 09:40:08 +0000 | [diff] [blame] | 1324 | |
| 1325 | /// Marks function \a Fn with properly mangled versions of vector functions. |
| 1326 | /// \param FD Function marked as 'declare simd'. |
| 1327 | /// \param Fn LLVM function that must be marked with 'declare simd' |
| 1328 | /// attributes. |
| 1329 | virtual void emitDeclareSimdFunction(const FunctionDecl *FD, |
| 1330 | llvm::Function *Fn); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 1331 | |
| 1332 | /// Emit initialization for doacross loop nesting support. |
| 1333 | /// \param D Loop-based construct used in doacross nesting construct. |
| 1334 | virtual void emitDoacrossInit(CodeGenFunction &CGF, |
| 1335 | const OMPLoopDirective &D); |
| 1336 | |
| 1337 | /// Emit code for doacross ordered directive with 'depend' clause. |
| 1338 | /// \param C 'depend' clause with 'sink|source' dependency kind. |
| 1339 | virtual void emitDoacrossOrdered(CodeGenFunction &CGF, |
| 1340 | const OMPDependClause *C); |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 1341 | |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 1342 | /// Translates the native parameter of outlined function if this is required |
| 1343 | /// for target. |
| 1344 | /// \param FD Field decl from captured record for the paramater. |
| 1345 | /// \param NativeParam Parameter itself. |
| 1346 | virtual const VarDecl *translateParameter(const FieldDecl *FD, |
| 1347 | const VarDecl *NativeParam) const { |
| 1348 | return NativeParam; |
| 1349 | } |
| 1350 | |
| 1351 | /// Gets the address of the native argument basing on the address of the |
| 1352 | /// target-specific parameter. |
| 1353 | /// \param NativeParam Parameter itself. |
| 1354 | /// \param TargetParam Corresponding target-specific parameter. |
| 1355 | virtual Address getParameterAddress(CodeGenFunction &CGF, |
| 1356 | const VarDecl *NativeParam, |
| 1357 | const VarDecl *TargetParam) const; |
| 1358 | |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 1359 | /// Emits call of the outlined function with the provided arguments, |
| 1360 | /// translating these arguments to correct target-specific arguments. |
| 1361 | virtual void |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 1362 | emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 1363 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 1364 | ArrayRef<llvm::Value *> Args = llvm::None) const; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1365 | }; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1366 | |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 1367 | } // namespace CodeGen |
| 1368 | } // namespace clang |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1369 | |
| 1370 | #endif |