Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 1 | //===---- CGOpenMPRuntimeNVPTX.cpp - Interface to OpenMP NVPTX Runtimes ---===// |
| 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 specialized to NVPTX |
| 11 | // targets. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGOpenMPRuntimeNVPTX.h" |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclOpenMP.h" |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtOpenMP.h" |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Cuda.h" |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | enum OpenMPRTLFunctionNVPTX { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 28 | /// Call to void __kmpc_kernel_init(kmp_int32 thread_limit, |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 29 | /// int16_t RequiresOMPRuntime); |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 30 | OMPRTL_NVPTX__kmpc_kernel_init, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 31 | /// Call to void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 32 | OMPRTL_NVPTX__kmpc_kernel_deinit, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 33 | /// Call to void __kmpc_spmd_kernel_init(kmp_int32 thread_limit, |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 34 | /// int16_t RequiresOMPRuntime, int16_t RequiresDataSharing); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 35 | OMPRTL_NVPTX__kmpc_spmd_kernel_init, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 36 | /// Call to void __kmpc_spmd_kernel_deinit(); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 37 | OMPRTL_NVPTX__kmpc_spmd_kernel_deinit, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 38 | /// Call to void __kmpc_kernel_prepare_parallel(void |
Gheorghe-Teodor Bercea | 7d80da1 | 2018-03-07 21:59:50 +0000 | [diff] [blame] | 39 | /// *outlined_function, int16_t |
Jonas Hahnfeld | fa059ba | 2017-12-27 10:39:56 +0000 | [diff] [blame] | 40 | /// IsOMPRuntimeInitialized); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 41 | OMPRTL_NVPTX__kmpc_kernel_prepare_parallel, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 42 | /// Call to bool __kmpc_kernel_parallel(void **outlined_function, |
Gheorghe-Teodor Bercea | 7d80da1 | 2018-03-07 21:59:50 +0000 | [diff] [blame] | 43 | /// int16_t IsOMPRuntimeInitialized); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 44 | OMPRTL_NVPTX__kmpc_kernel_parallel, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Call to void __kmpc_kernel_end_parallel(); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 46 | OMPRTL_NVPTX__kmpc_kernel_end_parallel, |
| 47 | /// Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 48 | /// global_tid); |
| 49 | OMPRTL_NVPTX__kmpc_serialized_parallel, |
| 50 | /// Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 51 | /// global_tid); |
| 52 | OMPRTL_NVPTX__kmpc_end_serialized_parallel, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 53 | /// Call to int32_t __kmpc_shuffle_int32(int32_t element, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 54 | /// int16_t lane_offset, int16_t warp_size); |
| 55 | OMPRTL_NVPTX__kmpc_shuffle_int32, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 56 | /// Call to int64_t __kmpc_shuffle_int64(int64_t element, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 57 | /// int16_t lane_offset, int16_t warp_size); |
| 58 | OMPRTL_NVPTX__kmpc_shuffle_int64, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 59 | /// Call to __kmpc_nvptx_parallel_reduce_nowait(kmp_int32 |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 60 | /// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data, |
| 61 | /// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t |
| 62 | /// lane_offset, int16_t shortCircuit), |
| 63 | /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num)); |
| 64 | OMPRTL_NVPTX__kmpc_parallel_reduce_nowait, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// Call to __kmpc_nvptx_simd_reduce_nowait(kmp_int32 |
Alexey Bataev | fac26cf | 2018-05-02 20:03:27 +0000 | [diff] [blame] | 66 | /// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data, |
| 67 | /// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t |
| 68 | /// lane_offset, int16_t shortCircuit), |
| 69 | /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num)); |
| 70 | OMPRTL_NVPTX__kmpc_simd_reduce_nowait, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 71 | /// Call to __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid, |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 72 | /// int32_t num_vars, size_t reduce_size, void *reduce_data, |
| 73 | /// void (*kmp_ShuffleReductFctPtr)(void *rhs, int16_t lane_id, int16_t |
| 74 | /// lane_offset, int16_t shortCircuit), |
| 75 | /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num), |
| 76 | /// void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad, |
| 77 | /// int32_t index, int32_t width), |
| 78 | /// void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad, int32_t |
| 79 | /// index, int32_t width, int32_t reduce)) |
| 80 | OMPRTL_NVPTX__kmpc_teams_reduce_nowait, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 81 | /// Call to __kmpc_nvptx_end_reduce_nowait(int32_t global_tid); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 82 | OMPRTL_NVPTX__kmpc_end_reduce_nowait, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 83 | /// Call to void __kmpc_data_sharing_init_stack(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 84 | OMPRTL_NVPTX__kmpc_data_sharing_init_stack, |
Gheorghe-Teodor Bercea | ad4e579 | 2018-07-13 16:18:24 +0000 | [diff] [blame] | 85 | /// Call to void __kmpc_data_sharing_init_stack_spmd(); |
| 86 | OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd, |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 87 | /// Call to void* __kmpc_data_sharing_coalesced_push_stack(size_t size, |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 88 | /// int16_t UseSharedMemory); |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 89 | OMPRTL_NVPTX__kmpc_data_sharing_coalesced_push_stack, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 90 | /// Call to void __kmpc_data_sharing_pop_stack(void *a); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 91 | OMPRTL_NVPTX__kmpc_data_sharing_pop_stack, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 92 | /// Call to void __kmpc_begin_sharing_variables(void ***args, |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 93 | /// size_t n_args); |
| 94 | OMPRTL_NVPTX__kmpc_begin_sharing_variables, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 95 | /// Call to void __kmpc_end_sharing_variables(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 96 | OMPRTL_NVPTX__kmpc_end_sharing_variables, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 97 | /// Call to void __kmpc_get_shared_variables(void ***GlobalArgs) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 98 | OMPRTL_NVPTX__kmpc_get_shared_variables, |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 99 | /// Call to uint16_t __kmpc_parallel_level(ident_t *loc, kmp_int32 |
| 100 | /// global_tid); |
| 101 | OMPRTL_NVPTX__kmpc_parallel_level, |
Alexey Bataev | 673110d | 2018-05-16 13:36:30 +0000 | [diff] [blame] | 102 | /// Call to int8_t __kmpc_is_spmd_exec_mode(); |
| 103 | OMPRTL_NVPTX__kmpc_is_spmd_exec_mode, |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 104 | /// Call to void __kmpc_get_team_static_memory(const void *buf, size_t size, |
| 105 | /// int16_t is_shared, const void **res); |
| 106 | OMPRTL_NVPTX__kmpc_get_team_static_memory, |
| 107 | /// Call to void __kmpc_restore_team_static_memory(int16_t is_shared); |
| 108 | OMPRTL_NVPTX__kmpc_restore_team_static_memory, |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 109 | }; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 110 | |
| 111 | /// Pre(post)-action for different OpenMP constructs specialized for NVPTX. |
| 112 | class NVPTXActionTy final : public PrePostActionTy { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 113 | llvm::Value *EnterCallee = nullptr; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 114 | ArrayRef<llvm::Value *> EnterArgs; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 115 | llvm::Value *ExitCallee = nullptr; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 116 | ArrayRef<llvm::Value *> ExitArgs; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 117 | bool Conditional = false; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 118 | llvm::BasicBlock *ContBlock = nullptr; |
| 119 | |
| 120 | public: |
| 121 | NVPTXActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs, |
| 122 | llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs, |
| 123 | bool Conditional = false) |
| 124 | : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee), |
| 125 | ExitArgs(ExitArgs), Conditional(Conditional) {} |
| 126 | void Enter(CodeGenFunction &CGF) override { |
| 127 | llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs); |
| 128 | if (Conditional) { |
| 129 | llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes); |
| 130 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 131 | ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 132 | // Generate the branch (If-stmt) |
| 133 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 134 | CGF.EmitBlock(ThenBlock); |
| 135 | } |
| 136 | } |
| 137 | void Done(CodeGenFunction &CGF) { |
| 138 | // Emit the rest of blocks/branches |
| 139 | CGF.EmitBranch(ContBlock); |
| 140 | CGF.EmitBlock(ContBlock, true); |
| 141 | } |
| 142 | void Exit(CodeGenFunction &CGF) override { |
| 143 | CGF.EmitRuntimeCall(ExitCallee, ExitArgs); |
| 144 | } |
| 145 | }; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 146 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 147 | /// A class to track the execution mode when codegening directives within |
| 148 | /// a target region. The appropriate mode (SPMD|NON-SPMD) is set on entry |
| 149 | /// to the target region and used by containing directives such as 'parallel' |
| 150 | /// to emit optimized code. |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 151 | class ExecutionModeRAII { |
| 152 | private: |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 153 | CGOpenMPRuntimeNVPTX::ExecutionMode SavedMode; |
| 154 | CGOpenMPRuntimeNVPTX::ExecutionMode &Mode; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 155 | |
| 156 | public: |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 157 | ExecutionModeRAII(CGOpenMPRuntimeNVPTX::ExecutionMode &Mode, bool IsSPMD) |
| 158 | : Mode(Mode) { |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 159 | SavedMode = Mode; |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 160 | Mode = IsSPMD ? CGOpenMPRuntimeNVPTX::EM_SPMD |
| 161 | : CGOpenMPRuntimeNVPTX::EM_NonSPMD; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 162 | } |
| 163 | ~ExecutionModeRAII() { Mode = SavedMode; } |
| 164 | }; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 165 | |
| 166 | /// GPU Configuration: This information can be derived from cuda registers, |
| 167 | /// however, providing compile time constants helps generate more efficient |
| 168 | /// code. For all practical purposes this is fine because the configuration |
| 169 | /// is the same for all known NVPTX architectures. |
| 170 | enum MachineConfiguration : unsigned { |
| 171 | WarpSize = 32, |
| 172 | /// Number of bits required to represent a lane identifier, which is |
| 173 | /// computed as log_2(WarpSize). |
| 174 | LaneIDBits = 5, |
| 175 | LaneIDMask = WarpSize - 1, |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 176 | |
| 177 | /// Global memory alignment for performance. |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 178 | GlobalMemoryAlignment = 128, |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 179 | |
| 180 | /// Maximal size of the shared memory buffer. |
| 181 | SharedMemorySize = 128, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | enum NamedBarrier : unsigned { |
| 185 | /// Synchronize on this barrier #ID using a named barrier primitive. |
| 186 | /// Only the subset of active threads in a parallel region arrive at the |
| 187 | /// barrier. |
| 188 | NB_Parallel = 1, |
| 189 | }; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 190 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 191 | static const ValueDecl *getPrivateItem(const Expr *RefExpr) { |
| 192 | RefExpr = RefExpr->IgnoreParens(); |
| 193 | if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr)) { |
| 194 | const Expr *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 195 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 196 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 197 | RefExpr = Base; |
| 198 | } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr)) { |
| 199 | const Expr *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 200 | while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| 201 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| 202 | while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 203 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 204 | RefExpr = Base; |
| 205 | } |
| 206 | RefExpr = RefExpr->IgnoreParenImpCasts(); |
| 207 | if (const auto *DE = dyn_cast<DeclRefExpr>(RefExpr)) |
| 208 | return cast<ValueDecl>(DE->getDecl()->getCanonicalDecl()); |
| 209 | const auto *ME = cast<MemberExpr>(RefExpr); |
| 210 | return cast<ValueDecl>(ME->getMemberDecl()->getCanonicalDecl()); |
| 211 | } |
| 212 | |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 213 | typedef std::pair<CharUnits /*Align*/, const ValueDecl *> VarsDataTy; |
| 214 | static bool stable_sort_comparator(const VarsDataTy P1, const VarsDataTy P2) { |
| 215 | return P1.first > P2.first; |
| 216 | } |
| 217 | |
| 218 | static RecordDecl *buildRecordForGlobalizedVars( |
| 219 | ASTContext &C, ArrayRef<const ValueDecl *> EscapedDecls, |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 220 | ArrayRef<const ValueDecl *> EscapedDeclsForTeams, |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 221 | llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> |
| 222 | &MappedDeclsFields) { |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 223 | if (EscapedDecls.empty() && EscapedDeclsForTeams.empty()) |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 224 | return nullptr; |
| 225 | SmallVector<VarsDataTy, 4> GlobalizedVars; |
| 226 | for (const ValueDecl *D : EscapedDecls) |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 227 | GlobalizedVars.emplace_back( |
| 228 | CharUnits::fromQuantity(std::max( |
| 229 | C.getDeclAlign(D).getQuantity(), |
| 230 | static_cast<CharUnits::QuantityType>(GlobalMemoryAlignment))), |
| 231 | D); |
| 232 | for (const ValueDecl *D : EscapedDeclsForTeams) |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 233 | GlobalizedVars.emplace_back(C.getDeclAlign(D), D); |
| 234 | std::stable_sort(GlobalizedVars.begin(), GlobalizedVars.end(), |
| 235 | stable_sort_comparator); |
| 236 | // Build struct _globalized_locals_ty { |
Alexey Bataev | ff23bb6 | 2018-10-11 18:30:31 +0000 | [diff] [blame] | 237 | // /* globalized vars */[WarSize] align (max(decl_align, |
| 238 | // GlobalMemoryAlignment)) |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 239 | // /* globalized vars */ for EscapedDeclsForTeams |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 240 | // }; |
| 241 | RecordDecl *GlobalizedRD = C.buildImplicitRecord("_globalized_locals_ty"); |
| 242 | GlobalizedRD->startDefinition(); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 243 | llvm::SmallPtrSet<const ValueDecl *, 16> SingleEscaped( |
| 244 | EscapedDeclsForTeams.begin(), EscapedDeclsForTeams.end()); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 245 | for (const auto &Pair : GlobalizedVars) { |
| 246 | const ValueDecl *VD = Pair.second; |
| 247 | QualType Type = VD->getType(); |
| 248 | if (Type->isLValueReferenceType()) |
| 249 | Type = C.getPointerType(Type.getNonReferenceType()); |
| 250 | else |
| 251 | Type = Type.getNonReferenceType(); |
| 252 | SourceLocation Loc = VD->getLocation(); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 253 | FieldDecl *Field; |
| 254 | if (SingleEscaped.count(VD)) { |
| 255 | Field = FieldDecl::Create( |
| 256 | C, GlobalizedRD, Loc, Loc, VD->getIdentifier(), Type, |
| 257 | C.getTrivialTypeSourceInfo(Type, SourceLocation()), |
| 258 | /*BW=*/nullptr, /*Mutable=*/false, |
| 259 | /*InitStyle=*/ICIS_NoInit); |
| 260 | Field->setAccess(AS_public); |
| 261 | if (VD->hasAttrs()) { |
| 262 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 263 | E(VD->getAttrs().end()); |
| 264 | I != E; ++I) |
| 265 | Field->addAttr(*I); |
| 266 | } |
| 267 | } else { |
| 268 | llvm::APInt ArraySize(32, WarpSize); |
| 269 | Type = C.getConstantArrayType(Type, ArraySize, ArrayType::Normal, 0); |
| 270 | Field = FieldDecl::Create( |
| 271 | C, GlobalizedRD, Loc, Loc, VD->getIdentifier(), Type, |
| 272 | C.getTrivialTypeSourceInfo(Type, SourceLocation()), |
| 273 | /*BW=*/nullptr, /*Mutable=*/false, |
| 274 | /*InitStyle=*/ICIS_NoInit); |
| 275 | Field->setAccess(AS_public); |
| 276 | llvm::APInt Align(32, std::max(C.getDeclAlign(VD).getQuantity(), |
| 277 | static_cast<CharUnits::QuantityType>( |
| 278 | GlobalMemoryAlignment))); |
| 279 | Field->addAttr(AlignedAttr::CreateImplicit( |
| 280 | C, AlignedAttr::GNU_aligned, /*IsAlignmentExpr=*/true, |
| 281 | IntegerLiteral::Create(C, Align, |
| 282 | C.getIntTypeForBitwidth(32, /*Signed=*/0), |
| 283 | SourceLocation()))); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 284 | } |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 285 | GlobalizedRD->addDecl(Field); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 286 | MappedDeclsFields.try_emplace(VD, Field); |
| 287 | } |
| 288 | GlobalizedRD->completeDefinition(); |
| 289 | return GlobalizedRD; |
| 290 | } |
| 291 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 292 | /// Get the list of variables that can escape their declaration context. |
| 293 | class CheckVarsEscapingDeclContext final |
| 294 | : public ConstStmtVisitor<CheckVarsEscapingDeclContext> { |
| 295 | CodeGenFunction &CGF; |
| 296 | llvm::SetVector<const ValueDecl *> EscapedDecls; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 297 | llvm::SetVector<const ValueDecl *> EscapedVariableLengthDecls; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 298 | llvm::SmallPtrSet<const Decl *, 4> EscapedParameters; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 299 | RecordDecl *GlobalizedRD = nullptr; |
| 300 | llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> MappedDeclsFields; |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 301 | bool AllEscaped = false; |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 302 | bool IsForCombinedParallelRegion = false; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 303 | |
| 304 | void markAsEscaped(const ValueDecl *VD) { |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 305 | // Do not globalize declare target variables. |
Alexey Bataev | 97b7221 | 2018-08-14 18:31:20 +0000 | [diff] [blame] | 306 | if (!isa<VarDecl>(VD) || |
| 307 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 308 | return; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 309 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 310 | // Variables captured by value must be globalized. |
| 311 | if (auto *CSI = CGF.CapturedStmtInfo) { |
Mikael Holmen | 9f373a3 | 2018-03-16 07:27:57 +0000 | [diff] [blame] | 312 | if (const FieldDecl *FD = CSI->lookup(cast<VarDecl>(VD))) { |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 313 | // Check if need to capture the variable that was already captured by |
| 314 | // value in the outer region. |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 315 | if (!IsForCombinedParallelRegion) { |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 316 | if (!FD->hasAttrs()) |
| 317 | return; |
| 318 | const auto *Attr = FD->getAttr<OMPCaptureKindAttr>(); |
| 319 | if (!Attr) |
| 320 | return; |
| 321 | if (!isOpenMPPrivate( |
| 322 | static_cast<OpenMPClauseKind>(Attr->getCaptureKind())) || |
| 323 | Attr->getCaptureKind() == OMPC_map) |
| 324 | return; |
| 325 | } |
| 326 | if (!FD->getType()->isReferenceType()) { |
| 327 | assert(!VD->getType()->isVariablyModifiedType() && |
| 328 | "Parameter captured by value with variably modified type"); |
| 329 | EscapedParameters.insert(VD); |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 330 | } else if (!IsForCombinedParallelRegion) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 331 | return; |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 332 | } |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 333 | } |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 334 | } |
| 335 | if ((!CGF.CapturedStmtInfo || |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 336 | (IsForCombinedParallelRegion && CGF.CapturedStmtInfo)) && |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 337 | VD->getType()->isReferenceType()) |
| 338 | // Do not globalize variables with reference type. |
Alexey Bataev | 2a3320a | 2018-05-15 18:01:01 +0000 | [diff] [blame] | 339 | return; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 340 | if (VD->getType()->isVariablyModifiedType()) |
| 341 | EscapedVariableLengthDecls.insert(VD); |
| 342 | else |
| 343 | EscapedDecls.insert(VD); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void VisitValueDecl(const ValueDecl *VD) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 347 | if (VD->getType()->isLValueReferenceType()) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 348 | markAsEscaped(VD); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 349 | if (const auto *VarD = dyn_cast<VarDecl>(VD)) { |
| 350 | if (!isa<ParmVarDecl>(VarD) && VarD->hasInit()) { |
| 351 | const bool SavedAllEscaped = AllEscaped; |
| 352 | AllEscaped = VD->getType()->isLValueReferenceType(); |
| 353 | Visit(VarD->getInit()); |
| 354 | AllEscaped = SavedAllEscaped; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 358 | void VisitOpenMPCapturedStmt(const CapturedStmt *S, |
| 359 | ArrayRef<OMPClause *> Clauses, |
| 360 | bool IsCombinedParallelRegion) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 361 | if (!S) |
| 362 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 363 | for (const CapturedStmt::Capture &C : S->captures()) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 364 | if (C.capturesVariable() && !C.capturesVariableByCopy()) { |
| 365 | const ValueDecl *VD = C.getCapturedVar(); |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 366 | bool SavedIsForCombinedParallelRegion = IsForCombinedParallelRegion; |
| 367 | if (IsCombinedParallelRegion) { |
| 368 | // Check if the variable is privatized in the combined construct and |
| 369 | // those private copies must be shared in the inner parallel |
| 370 | // directive. |
| 371 | IsForCombinedParallelRegion = false; |
| 372 | for (const OMPClause *C : Clauses) { |
| 373 | if (!isOpenMPPrivate(C->getClauseKind()) || |
| 374 | C->getClauseKind() == OMPC_reduction || |
| 375 | C->getClauseKind() == OMPC_linear || |
| 376 | C->getClauseKind() == OMPC_private) |
| 377 | continue; |
| 378 | ArrayRef<const Expr *> Vars; |
| 379 | if (const auto *PC = dyn_cast<OMPFirstprivateClause>(C)) |
| 380 | Vars = PC->getVarRefs(); |
| 381 | else if (const auto *PC = dyn_cast<OMPLastprivateClause>(C)) |
| 382 | Vars = PC->getVarRefs(); |
| 383 | else |
| 384 | llvm_unreachable("Unexpected clause."); |
| 385 | for (const auto *E : Vars) { |
| 386 | const Decl *D = |
| 387 | cast<DeclRefExpr>(E)->getDecl()->getCanonicalDecl(); |
| 388 | if (D == VD->getCanonicalDecl()) { |
| 389 | IsForCombinedParallelRegion = true; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | if (IsForCombinedParallelRegion) |
| 394 | break; |
| 395 | } |
| 396 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 397 | markAsEscaped(VD); |
| 398 | if (isa<OMPCapturedExprDecl>(VD)) |
| 399 | VisitValueDecl(VD); |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 400 | IsForCombinedParallelRegion = SavedIsForCombinedParallelRegion; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 405 | void buildRecordForGlobalizedVars(bool IsInTTDRegion) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 406 | assert(!GlobalizedRD && |
| 407 | "Record for globalized variables is built already."); |
Alexey Bataev | ff23bb6 | 2018-10-11 18:30:31 +0000 | [diff] [blame] | 408 | ArrayRef<const ValueDecl *> EscapedDeclsForParallel, EscapedDeclsForTeams; |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 409 | if (IsInTTDRegion) |
Alexey Bataev | ff23bb6 | 2018-10-11 18:30:31 +0000 | [diff] [blame] | 410 | EscapedDeclsForTeams = EscapedDecls.getArrayRef(); |
| 411 | else |
| 412 | EscapedDeclsForParallel = EscapedDecls.getArrayRef(); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 413 | GlobalizedRD = ::buildRecordForGlobalizedVars( |
Alexey Bataev | ff23bb6 | 2018-10-11 18:30:31 +0000 | [diff] [blame] | 414 | CGF.getContext(), EscapedDeclsForParallel, EscapedDeclsForTeams, |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 415 | MappedDeclsFields); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | public: |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 419 | CheckVarsEscapingDeclContext(CodeGenFunction &CGF, |
| 420 | ArrayRef<const ValueDecl *> TeamsReductions) |
| 421 | : CGF(CGF), EscapedDecls(TeamsReductions.begin(), TeamsReductions.end()) { |
| 422 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 423 | virtual ~CheckVarsEscapingDeclContext() = default; |
| 424 | void VisitDeclStmt(const DeclStmt *S) { |
| 425 | if (!S) |
| 426 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 427 | for (const Decl *D : S->decls()) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 428 | if (const auto *VD = dyn_cast_or_null<ValueDecl>(D)) |
| 429 | VisitValueDecl(VD); |
| 430 | } |
| 431 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D) { |
| 432 | if (!D) |
| 433 | return; |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 434 | if (!D->hasAssociatedStmt()) |
| 435 | return; |
| 436 | if (const auto *S = |
| 437 | dyn_cast_or_null<CapturedStmt>(D->getAssociatedStmt())) { |
| 438 | // Do not analyze directives that do not actually require capturing, |
| 439 | // like `omp for` or `omp simd` directives. |
| 440 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 441 | getOpenMPCaptureRegions(CaptureRegions, D->getDirectiveKind()); |
| 442 | if (CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown) { |
| 443 | VisitStmt(S->getCapturedStmt()); |
| 444 | return; |
Alexey Bataev | 673110d | 2018-05-16 13:36:30 +0000 | [diff] [blame] | 445 | } |
Alexey Bataev | 91433f6 | 2018-06-26 17:24:03 +0000 | [diff] [blame] | 446 | VisitOpenMPCapturedStmt( |
| 447 | S, D->clauses(), |
| 448 | CaptureRegions.back() == OMPD_parallel && |
| 449 | isOpenMPDistributeDirective(D->getDirectiveKind())); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | void VisitCapturedStmt(const CapturedStmt *S) { |
| 453 | if (!S) |
| 454 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 455 | for (const CapturedStmt::Capture &C : S->captures()) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 456 | if (C.capturesVariable() && !C.capturesVariableByCopy()) { |
| 457 | const ValueDecl *VD = C.getCapturedVar(); |
| 458 | markAsEscaped(VD); |
| 459 | if (isa<OMPCapturedExprDecl>(VD)) |
| 460 | VisitValueDecl(VD); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | void VisitLambdaExpr(const LambdaExpr *E) { |
| 465 | if (!E) |
| 466 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 467 | for (const LambdaCapture &C : E->captures()) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 468 | if (C.capturesVariable()) { |
| 469 | if (C.getCaptureKind() == LCK_ByRef) { |
| 470 | const ValueDecl *VD = C.getCapturedVar(); |
| 471 | markAsEscaped(VD); |
| 472 | if (E->isInitCapture(&C) || isa<OMPCapturedExprDecl>(VD)) |
| 473 | VisitValueDecl(VD); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | void VisitBlockExpr(const BlockExpr *E) { |
| 479 | if (!E) |
| 480 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 481 | for (const BlockDecl::Capture &C : E->getBlockDecl()->captures()) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 482 | if (C.isByRef()) { |
| 483 | const VarDecl *VD = C.getVariable(); |
| 484 | markAsEscaped(VD); |
| 485 | if (isa<OMPCapturedExprDecl>(VD) || VD->isInitCapture()) |
| 486 | VisitValueDecl(VD); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | void VisitCallExpr(const CallExpr *E) { |
| 491 | if (!E) |
| 492 | return; |
| 493 | for (const Expr *Arg : E->arguments()) { |
| 494 | if (!Arg) |
| 495 | continue; |
| 496 | if (Arg->isLValue()) { |
| 497 | const bool SavedAllEscaped = AllEscaped; |
| 498 | AllEscaped = true; |
| 499 | Visit(Arg); |
| 500 | AllEscaped = SavedAllEscaped; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 501 | } else { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 502 | Visit(Arg); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 503 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 504 | } |
| 505 | Visit(E->getCallee()); |
| 506 | } |
| 507 | void VisitDeclRefExpr(const DeclRefExpr *E) { |
| 508 | if (!E) |
| 509 | return; |
| 510 | const ValueDecl *VD = E->getDecl(); |
| 511 | if (AllEscaped) |
| 512 | markAsEscaped(VD); |
| 513 | if (isa<OMPCapturedExprDecl>(VD)) |
| 514 | VisitValueDecl(VD); |
| 515 | else if (const auto *VarD = dyn_cast<VarDecl>(VD)) |
| 516 | if (VarD->isInitCapture()) |
| 517 | VisitValueDecl(VD); |
| 518 | } |
| 519 | void VisitUnaryOperator(const UnaryOperator *E) { |
| 520 | if (!E) |
| 521 | return; |
| 522 | if (E->getOpcode() == UO_AddrOf) { |
| 523 | const bool SavedAllEscaped = AllEscaped; |
| 524 | AllEscaped = true; |
| 525 | Visit(E->getSubExpr()); |
| 526 | AllEscaped = SavedAllEscaped; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 527 | } else { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 528 | Visit(E->getSubExpr()); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 529 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 530 | } |
| 531 | void VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 532 | if (!E) |
| 533 | return; |
| 534 | if (E->getCastKind() == CK_ArrayToPointerDecay) { |
| 535 | const bool SavedAllEscaped = AllEscaped; |
| 536 | AllEscaped = true; |
| 537 | Visit(E->getSubExpr()); |
| 538 | AllEscaped = SavedAllEscaped; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 539 | } else { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 540 | Visit(E->getSubExpr()); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 541 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 542 | } |
| 543 | void VisitExpr(const Expr *E) { |
| 544 | if (!E) |
| 545 | return; |
| 546 | bool SavedAllEscaped = AllEscaped; |
| 547 | if (!E->isLValue()) |
| 548 | AllEscaped = false; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 549 | for (const Stmt *Child : E->children()) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 550 | if (Child) |
| 551 | Visit(Child); |
| 552 | AllEscaped = SavedAllEscaped; |
| 553 | } |
| 554 | void VisitStmt(const Stmt *S) { |
| 555 | if (!S) |
| 556 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 557 | for (const Stmt *Child : S->children()) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 558 | if (Child) |
| 559 | Visit(Child); |
| 560 | } |
| 561 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 562 | /// Returns the record that handles all the escaped local variables and used |
| 563 | /// instead of their original storage. |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 564 | const RecordDecl *getGlobalizedRecord(bool IsInTTDRegion) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 565 | if (!GlobalizedRD) |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 566 | buildRecordForGlobalizedVars(IsInTTDRegion); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 567 | return GlobalizedRD; |
| 568 | } |
| 569 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 570 | /// Returns the field in the globalized record for the escaped variable. |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 571 | const FieldDecl *getFieldForGlobalizedVar(const ValueDecl *VD) const { |
| 572 | assert(GlobalizedRD && |
| 573 | "Record for globalized variables must be generated already."); |
| 574 | auto I = MappedDeclsFields.find(VD); |
| 575 | if (I == MappedDeclsFields.end()) |
| 576 | return nullptr; |
| 577 | return I->getSecond(); |
| 578 | } |
| 579 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 580 | /// Returns the list of the escaped local variables/parameters. |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 581 | ArrayRef<const ValueDecl *> getEscapedDecls() const { |
| 582 | return EscapedDecls.getArrayRef(); |
| 583 | } |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 584 | |
| 585 | /// Checks if the escaped local variable is actually a parameter passed by |
| 586 | /// value. |
| 587 | const llvm::SmallPtrSetImpl<const Decl *> &getEscapedParameters() const { |
| 588 | return EscapedParameters; |
| 589 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 590 | |
| 591 | /// Returns the list of the escaped variables with the variably modified |
| 592 | /// types. |
| 593 | ArrayRef<const ValueDecl *> getEscapedVariableLengthDecls() const { |
| 594 | return EscapedVariableLengthDecls.getArrayRef(); |
| 595 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 596 | }; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 597 | } // anonymous namespace |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 598 | |
| 599 | /// Get the GPU warp size. |
| 600 | static llvm::Value *getNVPTXWarpSize(CodeGenFunction &CGF) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 601 | return CGF.EmitRuntimeCall( |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 602 | llvm::Intrinsic::getDeclaration( |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 603 | &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 604 | "nvptx_warp_size"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 607 | /// Get the id of the current thread on the GPU. |
| 608 | static llvm::Value *getNVPTXThreadID(CodeGenFunction &CGF) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 609 | return CGF.EmitRuntimeCall( |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 610 | llvm::Intrinsic::getDeclaration( |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 611 | &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 612 | "nvptx_tid"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 615 | /// Get the id of the warp in the block. |
| 616 | /// We assume that the warp size is 32, which is always the case |
| 617 | /// on the NVPTX device, to generate more efficient code. |
| 618 | static llvm::Value *getNVPTXWarpID(CodeGenFunction &CGF) { |
| 619 | CGBuilderTy &Bld = CGF.Builder; |
| 620 | return Bld.CreateAShr(getNVPTXThreadID(CGF), LaneIDBits, "nvptx_warp_id"); |
| 621 | } |
| 622 | |
| 623 | /// Get the id of the current lane in the Warp. |
| 624 | /// We assume that the warp size is 32, which is always the case |
| 625 | /// on the NVPTX device, to generate more efficient code. |
| 626 | static llvm::Value *getNVPTXLaneID(CodeGenFunction &CGF) { |
| 627 | CGBuilderTy &Bld = CGF.Builder; |
| 628 | return Bld.CreateAnd(getNVPTXThreadID(CGF), Bld.getInt32(LaneIDMask), |
| 629 | "nvptx_lane_id"); |
| 630 | } |
| 631 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 632 | /// Get the maximum number of threads in a block of the GPU. |
| 633 | static llvm::Value *getNVPTXNumThreads(CodeGenFunction &CGF) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 634 | return CGF.EmitRuntimeCall( |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 635 | llvm::Intrinsic::getDeclaration( |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 636 | &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 637 | "nvptx_num_threads"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 640 | /// Get barrier to synchronize all threads in a block. |
| 641 | static void getNVPTXCTABarrier(CodeGenFunction &CGF) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 642 | llvm::Function *F = llvm::Intrinsic::getDeclaration( |
| 643 | &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier0); |
| 644 | F->addFnAttr(llvm::Attribute::Convergent); |
| 645 | CGF.EmitRuntimeCall(F); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 648 | /// Get barrier #ID to synchronize selected (multiple of warp size) threads in |
| 649 | /// a CTA. |
| 650 | static void getNVPTXBarrier(CodeGenFunction &CGF, int ID, |
| 651 | llvm::Value *NumThreads) { |
| 652 | CGBuilderTy &Bld = CGF.Builder; |
| 653 | llvm::Value *Args[] = {Bld.getInt32(ID), NumThreads}; |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 654 | llvm::Function *F = llvm::Intrinsic::getDeclaration( |
| 655 | &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier); |
| 656 | F->addFnAttr(llvm::Attribute::Convergent); |
| 657 | CGF.EmitRuntimeCall(F, Args); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 660 | /// Synchronize all GPU threads in a block. |
| 661 | static void syncCTAThreads(CodeGenFunction &CGF) { getNVPTXCTABarrier(CGF); } |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 662 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 663 | /// Synchronize worker threads in a parallel region. |
| 664 | static void syncParallelThreads(CodeGenFunction &CGF, llvm::Value *NumThreads) { |
| 665 | return getNVPTXBarrier(CGF, NB_Parallel, NumThreads); |
| 666 | } |
| 667 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 668 | /// Get the value of the thread_limit clause in the teams directive. |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 669 | /// For the 'generic' execution mode, the runtime encodes thread_limit in |
| 670 | /// the launch parameters, always starting thread_limit+warpSize threads per |
| 671 | /// CTA. The threads in the last warp are reserved for master execution. |
| 672 | /// For the 'spmd' execution mode, all threads in a CTA are part of the team. |
| 673 | static llvm::Value *getThreadLimit(CodeGenFunction &CGF, |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 674 | bool IsInSPMDExecutionMode = false) { |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 675 | CGBuilderTy &Bld = CGF.Builder; |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 676 | return IsInSPMDExecutionMode |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 677 | ? getNVPTXNumThreads(CGF) |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 678 | : Bld.CreateNUWSub(getNVPTXNumThreads(CGF), getNVPTXWarpSize(CGF), |
| 679 | "thread_limit"); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 682 | /// Get the thread id of the OMP master thread. |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 683 | /// The master thread id is the first thread (lane) of the last warp in the |
| 684 | /// GPU block. Warp size is assumed to be some power of 2. |
| 685 | /// Thread id is 0 indexed. |
| 686 | /// E.g: If NumThreads is 33, master id is 32. |
| 687 | /// If NumThreads is 64, master id is 32. |
| 688 | /// If NumThreads is 1024, master id is 992. |
Arpith Chacko Jacob | ccf2f73 | 2017-01-03 20:19:56 +0000 | [diff] [blame] | 689 | static llvm::Value *getMasterThreadID(CodeGenFunction &CGF) { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 690 | CGBuilderTy &Bld = CGF.Builder; |
| 691 | llvm::Value *NumThreads = getNVPTXNumThreads(CGF); |
| 692 | |
| 693 | // We assume that the warp size is a power of 2. |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 694 | llvm::Value *Mask = Bld.CreateNUWSub(getNVPTXWarpSize(CGF), Bld.getInt32(1)); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 695 | |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 696 | return Bld.CreateAnd(Bld.CreateNUWSub(NumThreads, Bld.getInt32(1)), |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 697 | Bld.CreateNot(Mask), "master_tid"); |
| 698 | } |
| 699 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 700 | CGOpenMPRuntimeNVPTX::WorkerFunctionState::WorkerFunctionState( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 701 | CodeGenModule &CGM, SourceLocation Loc) |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 702 | : WorkerFn(nullptr), CGFI(CGM.getTypes().arrangeNullaryFunction()), |
| 703 | Loc(Loc) { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 704 | createWorkerFunction(CGM); |
Vasileios Kalintiris | e5c0959 | 2016-03-22 10:41:20 +0000 | [diff] [blame] | 705 | } |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 706 | |
| 707 | void CGOpenMPRuntimeNVPTX::WorkerFunctionState::createWorkerFunction( |
| 708 | CodeGenModule &CGM) { |
| 709 | // Create an worker function with no arguments. |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 710 | |
| 711 | WorkerFn = llvm::Function::Create( |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 712 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | aee9389 | 2018-01-08 20:09:47 +0000 | [diff] [blame] | 713 | /*placeholder=*/"_worker", &CGM.getModule()); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 714 | CGM.SetInternalFunctionAttributes(GlobalDecl(), WorkerFn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 715 | WorkerFn->setDoesNotRecurse(); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 718 | CGOpenMPRuntimeNVPTX::ExecutionMode |
| 719 | CGOpenMPRuntimeNVPTX::getExecutionMode() const { |
| 720 | return CurrentExecutionMode; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 723 | static CGOpenMPRuntimeNVPTX::DataSharingMode |
| 724 | getDataSharingMode(CodeGenModule &CGM) { |
| 725 | return CGM.getLangOpts().OpenMPCUDAMode ? CGOpenMPRuntimeNVPTX::CUDA |
| 726 | : CGOpenMPRuntimeNVPTX::Generic; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 729 | // Checks if the expression is constant or does not have non-trivial function |
| 730 | // calls. |
| 731 | static bool isTrivial(ASTContext &Ctx, const Expr * E) { |
| 732 | // We can skip constant expressions. |
| 733 | // We can skip expressions with trivial calls or simple expressions. |
| 734 | return (E->isEvaluatable(Ctx, Expr::SE_AllowUndefinedBehavior) || |
| 735 | !E->hasNonTrivialCall(Ctx)) && |
| 736 | !E->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true); |
| 737 | } |
| 738 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 739 | /// Checks if the \p Body is the \a CompoundStmt and returns its child statement |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 740 | /// iff there is only one that is not evaluatable at the compile time. |
| 741 | static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body) { |
| 742 | if (const auto *C = dyn_cast<CompoundStmt>(Body)) { |
| 743 | const Stmt *Child = nullptr; |
| 744 | for (const Stmt *S : C->body()) { |
| 745 | if (const auto *E = dyn_cast<Expr>(S)) { |
| 746 | if (isTrivial(Ctx, E)) |
| 747 | continue; |
| 748 | } |
| 749 | // Some of the statements can be ignored. |
| 750 | if (isa<AsmStmt>(S) || isa<NullStmt>(S) || isa<OMPFlushDirective>(S) || |
| 751 | isa<OMPBarrierDirective>(S) || isa<OMPTaskyieldDirective>(S)) |
| 752 | continue; |
| 753 | // Analyze declarations. |
| 754 | if (const auto *DS = dyn_cast<DeclStmt>(S)) { |
| 755 | if (llvm::all_of(DS->decls(), [&Ctx](const Decl *D) { |
| 756 | if (isa<EmptyDecl>(D) || isa<DeclContext>(D) || |
| 757 | isa<TypeDecl>(D) || isa<PragmaCommentDecl>(D) || |
| 758 | isa<PragmaDetectMismatchDecl>(D) || isa<UsingDecl>(D) || |
| 759 | isa<UsingDirectiveDecl>(D) || |
| 760 | isa<OMPDeclareReductionDecl>(D) || |
| 761 | isa<OMPThreadPrivateDecl>(D)) |
| 762 | return true; |
| 763 | const auto *VD = dyn_cast<VarDecl>(D); |
| 764 | if (!VD) |
| 765 | return false; |
| 766 | return VD->isConstexpr() || |
| 767 | ((VD->getType().isTrivialType(Ctx) || |
| 768 | VD->getType()->isReferenceType()) && |
| 769 | (!VD->hasInit() || isTrivial(Ctx, VD->getInit()))); |
| 770 | })) |
| 771 | continue; |
| 772 | } |
| 773 | // Found multiple children - cannot get the one child only. |
| 774 | if (Child) |
| 775 | return Body; |
| 776 | Child = S; |
| 777 | } |
| 778 | if (Child) |
| 779 | return Child; |
| 780 | } |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 781 | return Body; |
| 782 | } |
| 783 | |
| 784 | /// Check if the parallel directive has an 'if' clause with non-constant or |
Alexey Bataev | 2a3320a | 2018-05-15 18:01:01 +0000 | [diff] [blame] | 785 | /// false condition. Also, check if the number of threads is strictly specified |
| 786 | /// and run those directives in non-SPMD mode. |
| 787 | static bool hasParallelIfNumThreadsClause(ASTContext &Ctx, |
| 788 | const OMPExecutableDirective &D) { |
| 789 | if (D.hasClausesOfKind<OMPNumThreadsClause>()) |
| 790 | return true; |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 791 | for (const auto *C : D.getClausesOfKind<OMPIfClause>()) { |
| 792 | OpenMPDirectiveKind NameModifier = C->getNameModifier(); |
| 793 | if (NameModifier != OMPD_parallel && NameModifier != OMPD_unknown) |
| 794 | continue; |
| 795 | const Expr *Cond = C->getCondition(); |
| 796 | bool Result; |
| 797 | if (!Cond->EvaluateAsBooleanCondition(Result, Ctx) || !Result) |
| 798 | return true; |
| 799 | } |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | /// Check for inner (nested) SPMD construct, if any |
| 804 | static bool hasNestedSPMDDirective(ASTContext &Ctx, |
| 805 | const OMPExecutableDirective &D) { |
| 806 | const auto *CS = D.getInnermostCapturedStmt(); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 807 | const auto *Body = |
| 808 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 809 | const Stmt *ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 810 | |
| 811 | if (const auto *NestedDir = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 812 | OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind(); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 813 | switch (D.getDirectiveKind()) { |
| 814 | case OMPD_target: |
Alexey Bataev | df093e7 | 2018-05-11 19:45:14 +0000 | [diff] [blame] | 815 | if (isOpenMPParallelDirective(DKind) && |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 816 | !hasParallelIfNumThreadsClause(Ctx, *NestedDir)) |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 817 | return true; |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 818 | if (DKind == OMPD_teams) { |
| 819 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 820 | /*IgnoreCaptured=*/true); |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 821 | if (!Body) |
| 822 | return false; |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 823 | ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 824 | if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 825 | DKind = NND->getDirectiveKind(); |
Alexey Bataev | df093e7 | 2018-05-11 19:45:14 +0000 | [diff] [blame] | 826 | if (isOpenMPParallelDirective(DKind) && |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 827 | !hasParallelIfNumThreadsClause(Ctx, *NND)) |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 828 | return true; |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | return false; |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 832 | case OMPD_target_teams: |
Alexey Bataev | df093e7 | 2018-05-11 19:45:14 +0000 | [diff] [blame] | 833 | return isOpenMPParallelDirective(DKind) && |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 834 | !hasParallelIfNumThreadsClause(Ctx, *NestedDir); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 835 | case OMPD_target_simd: |
| 836 | case OMPD_target_parallel: |
| 837 | case OMPD_target_parallel_for: |
| 838 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 839 | case OMPD_target_teams_distribute: |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 840 | case OMPD_target_teams_distribute_simd: |
| 841 | case OMPD_target_teams_distribute_parallel_for: |
| 842 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 843 | case OMPD_parallel: |
| 844 | case OMPD_for: |
| 845 | case OMPD_parallel_for: |
| 846 | case OMPD_parallel_sections: |
| 847 | case OMPD_for_simd: |
| 848 | case OMPD_parallel_for_simd: |
| 849 | case OMPD_cancel: |
| 850 | case OMPD_cancellation_point: |
| 851 | case OMPD_ordered: |
| 852 | case OMPD_threadprivate: |
| 853 | case OMPD_task: |
| 854 | case OMPD_simd: |
| 855 | case OMPD_sections: |
| 856 | case OMPD_section: |
| 857 | case OMPD_single: |
| 858 | case OMPD_master: |
| 859 | case OMPD_critical: |
| 860 | case OMPD_taskyield: |
| 861 | case OMPD_barrier: |
| 862 | case OMPD_taskwait: |
| 863 | case OMPD_taskgroup: |
| 864 | case OMPD_atomic: |
| 865 | case OMPD_flush: |
| 866 | case OMPD_teams: |
| 867 | case OMPD_target_data: |
| 868 | case OMPD_target_exit_data: |
| 869 | case OMPD_target_enter_data: |
| 870 | case OMPD_distribute: |
| 871 | case OMPD_distribute_simd: |
| 872 | case OMPD_distribute_parallel_for: |
| 873 | case OMPD_distribute_parallel_for_simd: |
| 874 | case OMPD_teams_distribute: |
| 875 | case OMPD_teams_distribute_simd: |
| 876 | case OMPD_teams_distribute_parallel_for: |
| 877 | case OMPD_teams_distribute_parallel_for_simd: |
| 878 | case OMPD_target_update: |
| 879 | case OMPD_declare_simd: |
| 880 | case OMPD_declare_target: |
| 881 | case OMPD_end_declare_target: |
| 882 | case OMPD_declare_reduction: |
| 883 | case OMPD_taskloop: |
| 884 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 885 | case OMPD_requires: |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 886 | case OMPD_unknown: |
| 887 | llvm_unreachable("Unexpected directive."); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return false; |
| 892 | } |
| 893 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 894 | static bool supportsSPMDExecutionMode(ASTContext &Ctx, |
| 895 | const OMPExecutableDirective &D) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 896 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 897 | switch (DirectiveKind) { |
| 898 | case OMPD_target: |
| 899 | case OMPD_target_teams: |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 900 | return hasNestedSPMDDirective(Ctx, D); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 901 | case OMPD_target_parallel: |
| 902 | case OMPD_target_parallel_for: |
| 903 | case OMPD_target_parallel_for_simd: |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 904 | case OMPD_target_teams_distribute_parallel_for: |
| 905 | case OMPD_target_teams_distribute_parallel_for_simd: |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 906 | return !hasParallelIfNumThreadsClause(Ctx, D); |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 907 | case OMPD_target_simd: |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 908 | case OMPD_target_teams_distribute: |
| 909 | case OMPD_target_teams_distribute_simd: |
| 910 | return false; |
| 911 | case OMPD_parallel: |
| 912 | case OMPD_for: |
| 913 | case OMPD_parallel_for: |
| 914 | case OMPD_parallel_sections: |
| 915 | case OMPD_for_simd: |
| 916 | case OMPD_parallel_for_simd: |
| 917 | case OMPD_cancel: |
| 918 | case OMPD_cancellation_point: |
| 919 | case OMPD_ordered: |
| 920 | case OMPD_threadprivate: |
| 921 | case OMPD_task: |
| 922 | case OMPD_simd: |
| 923 | case OMPD_sections: |
| 924 | case OMPD_section: |
| 925 | case OMPD_single: |
| 926 | case OMPD_master: |
| 927 | case OMPD_critical: |
| 928 | case OMPD_taskyield: |
| 929 | case OMPD_barrier: |
| 930 | case OMPD_taskwait: |
| 931 | case OMPD_taskgroup: |
| 932 | case OMPD_atomic: |
| 933 | case OMPD_flush: |
| 934 | case OMPD_teams: |
| 935 | case OMPD_target_data: |
| 936 | case OMPD_target_exit_data: |
| 937 | case OMPD_target_enter_data: |
| 938 | case OMPD_distribute: |
| 939 | case OMPD_distribute_simd: |
| 940 | case OMPD_distribute_parallel_for: |
| 941 | case OMPD_distribute_parallel_for_simd: |
| 942 | case OMPD_teams_distribute: |
| 943 | case OMPD_teams_distribute_simd: |
| 944 | case OMPD_teams_distribute_parallel_for: |
| 945 | case OMPD_teams_distribute_parallel_for_simd: |
| 946 | case OMPD_target_update: |
| 947 | case OMPD_declare_simd: |
| 948 | case OMPD_declare_target: |
| 949 | case OMPD_end_declare_target: |
| 950 | case OMPD_declare_reduction: |
| 951 | case OMPD_taskloop: |
| 952 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 953 | case OMPD_requires: |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 954 | case OMPD_unknown: |
| 955 | break; |
| 956 | } |
| 957 | llvm_unreachable( |
| 958 | "Unknown programming model for OpenMP directive on NVPTX target."); |
| 959 | } |
| 960 | |
| 961 | /// Check if the directive is loops based and has schedule clause at all or has |
| 962 | /// static scheduling. |
| 963 | static bool hasStaticScheduling(const OMPExecutableDirective &D) { |
| 964 | assert(isOpenMPWorksharingDirective(D.getDirectiveKind()) && |
| 965 | isOpenMPLoopDirective(D.getDirectiveKind()) && |
| 966 | "Expected loop-based directive."); |
| 967 | return !D.hasClausesOfKind<OMPOrderedClause>() && |
| 968 | (!D.hasClausesOfKind<OMPScheduleClause>() || |
| 969 | llvm::any_of(D.getClausesOfKind<OMPScheduleClause>(), |
| 970 | [](const OMPScheduleClause *C) { |
| 971 | return C->getScheduleKind() == OMPC_SCHEDULE_static; |
| 972 | })); |
| 973 | } |
| 974 | |
| 975 | /// Check for inner (nested) lightweight runtime construct, if any |
| 976 | static bool hasNestedLightweightDirective(ASTContext &Ctx, |
| 977 | const OMPExecutableDirective &D) { |
| 978 | assert(supportsSPMDExecutionMode(Ctx, D) && "Expected SPMD mode directive."); |
| 979 | const auto *CS = D.getInnermostCapturedStmt(); |
| 980 | const auto *Body = |
| 981 | CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 982 | const Stmt *ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 983 | |
| 984 | if (const auto *NestedDir = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 985 | OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind(); |
| 986 | switch (D.getDirectiveKind()) { |
| 987 | case OMPD_target: |
| 988 | if (isOpenMPParallelDirective(DKind) && |
| 989 | isOpenMPWorksharingDirective(DKind) && isOpenMPLoopDirective(DKind) && |
| 990 | hasStaticScheduling(*NestedDir)) |
| 991 | return true; |
| 992 | if (DKind == OMPD_parallel) { |
| 993 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 994 | /*IgnoreCaptured=*/true); |
| 995 | if (!Body) |
| 996 | return false; |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 997 | ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 998 | if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 999 | DKind = NND->getDirectiveKind(); |
| 1000 | if (isOpenMPWorksharingDirective(DKind) && |
| 1001 | isOpenMPLoopDirective(DKind) && hasStaticScheduling(*NND)) |
| 1002 | return true; |
| 1003 | } |
| 1004 | } else if (DKind == OMPD_teams) { |
| 1005 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 1006 | /*IgnoreCaptured=*/true); |
| 1007 | if (!Body) |
| 1008 | return false; |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1009 | ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1010 | if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 1011 | DKind = NND->getDirectiveKind(); |
| 1012 | if (isOpenMPParallelDirective(DKind) && |
| 1013 | isOpenMPWorksharingDirective(DKind) && |
| 1014 | isOpenMPLoopDirective(DKind) && hasStaticScheduling(*NND)) |
| 1015 | return true; |
| 1016 | if (DKind == OMPD_parallel) { |
| 1017 | Body = NND->getInnermostCapturedStmt()->IgnoreContainers( |
| 1018 | /*IgnoreCaptured=*/true); |
| 1019 | if (!Body) |
| 1020 | return false; |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1021 | ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1022 | if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 1023 | DKind = NND->getDirectiveKind(); |
| 1024 | if (isOpenMPWorksharingDirective(DKind) && |
| 1025 | isOpenMPLoopDirective(DKind) && hasStaticScheduling(*NND)) |
| 1026 | return true; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | return false; |
| 1032 | case OMPD_target_teams: |
| 1033 | if (isOpenMPParallelDirective(DKind) && |
| 1034 | isOpenMPWorksharingDirective(DKind) && isOpenMPLoopDirective(DKind) && |
| 1035 | hasStaticScheduling(*NestedDir)) |
| 1036 | return true; |
| 1037 | if (DKind == OMPD_parallel) { |
| 1038 | Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers( |
| 1039 | /*IgnoreCaptured=*/true); |
| 1040 | if (!Body) |
| 1041 | return false; |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1042 | ChildStmt = getSingleCompoundChild(Ctx, Body); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1043 | if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) { |
| 1044 | DKind = NND->getDirectiveKind(); |
| 1045 | if (isOpenMPWorksharingDirective(DKind) && |
| 1046 | isOpenMPLoopDirective(DKind) && hasStaticScheduling(*NND)) |
| 1047 | return true; |
| 1048 | } |
| 1049 | } |
| 1050 | return false; |
| 1051 | case OMPD_target_parallel: |
| 1052 | return isOpenMPWorksharingDirective(DKind) && |
| 1053 | isOpenMPLoopDirective(DKind) && hasStaticScheduling(*NestedDir); |
| 1054 | case OMPD_target_teams_distribute: |
| 1055 | case OMPD_target_simd: |
| 1056 | case OMPD_target_parallel_for: |
| 1057 | case OMPD_target_parallel_for_simd: |
| 1058 | case OMPD_target_teams_distribute_simd: |
| 1059 | case OMPD_target_teams_distribute_parallel_for: |
| 1060 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 1061 | case OMPD_parallel: |
| 1062 | case OMPD_for: |
| 1063 | case OMPD_parallel_for: |
| 1064 | case OMPD_parallel_sections: |
| 1065 | case OMPD_for_simd: |
| 1066 | case OMPD_parallel_for_simd: |
| 1067 | case OMPD_cancel: |
| 1068 | case OMPD_cancellation_point: |
| 1069 | case OMPD_ordered: |
| 1070 | case OMPD_threadprivate: |
| 1071 | case OMPD_task: |
| 1072 | case OMPD_simd: |
| 1073 | case OMPD_sections: |
| 1074 | case OMPD_section: |
| 1075 | case OMPD_single: |
| 1076 | case OMPD_master: |
| 1077 | case OMPD_critical: |
| 1078 | case OMPD_taskyield: |
| 1079 | case OMPD_barrier: |
| 1080 | case OMPD_taskwait: |
| 1081 | case OMPD_taskgroup: |
| 1082 | case OMPD_atomic: |
| 1083 | case OMPD_flush: |
| 1084 | case OMPD_teams: |
| 1085 | case OMPD_target_data: |
| 1086 | case OMPD_target_exit_data: |
| 1087 | case OMPD_target_enter_data: |
| 1088 | case OMPD_distribute: |
| 1089 | case OMPD_distribute_simd: |
| 1090 | case OMPD_distribute_parallel_for: |
| 1091 | case OMPD_distribute_parallel_for_simd: |
| 1092 | case OMPD_teams_distribute: |
| 1093 | case OMPD_teams_distribute_simd: |
| 1094 | case OMPD_teams_distribute_parallel_for: |
| 1095 | case OMPD_teams_distribute_parallel_for_simd: |
| 1096 | case OMPD_target_update: |
| 1097 | case OMPD_declare_simd: |
| 1098 | case OMPD_declare_target: |
| 1099 | case OMPD_end_declare_target: |
| 1100 | case OMPD_declare_reduction: |
| 1101 | case OMPD_taskloop: |
| 1102 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1103 | case OMPD_requires: |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1104 | case OMPD_unknown: |
| 1105 | llvm_unreachable("Unexpected directive."); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | /// Checks if the construct supports lightweight runtime. It must be SPMD |
| 1113 | /// construct + inner loop-based construct with static scheduling. |
| 1114 | static bool supportsLightweightRuntime(ASTContext &Ctx, |
| 1115 | const OMPExecutableDirective &D) { |
| 1116 | if (!supportsSPMDExecutionMode(Ctx, D)) |
| 1117 | return false; |
| 1118 | OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind(); |
| 1119 | switch (DirectiveKind) { |
| 1120 | case OMPD_target: |
| 1121 | case OMPD_target_teams: |
| 1122 | case OMPD_target_parallel: |
| 1123 | return hasNestedLightweightDirective(Ctx, D); |
| 1124 | case OMPD_target_parallel_for: |
| 1125 | case OMPD_target_parallel_for_simd: |
| 1126 | case OMPD_target_teams_distribute_parallel_for: |
| 1127 | case OMPD_target_teams_distribute_parallel_for_simd: |
| 1128 | // (Last|First)-privates must be shared in parallel region. |
| 1129 | return hasStaticScheduling(D); |
| 1130 | case OMPD_target_simd: |
| 1131 | case OMPD_target_teams_distribute: |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1132 | case OMPD_target_teams_distribute_simd: |
Alexey Bataev | df093e7 | 2018-05-11 19:45:14 +0000 | [diff] [blame] | 1133 | return false; |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1134 | case OMPD_parallel: |
| 1135 | case OMPD_for: |
| 1136 | case OMPD_parallel_for: |
| 1137 | case OMPD_parallel_sections: |
| 1138 | case OMPD_for_simd: |
| 1139 | case OMPD_parallel_for_simd: |
| 1140 | case OMPD_cancel: |
| 1141 | case OMPD_cancellation_point: |
| 1142 | case OMPD_ordered: |
| 1143 | case OMPD_threadprivate: |
| 1144 | case OMPD_task: |
| 1145 | case OMPD_simd: |
| 1146 | case OMPD_sections: |
| 1147 | case OMPD_section: |
| 1148 | case OMPD_single: |
| 1149 | case OMPD_master: |
| 1150 | case OMPD_critical: |
| 1151 | case OMPD_taskyield: |
| 1152 | case OMPD_barrier: |
| 1153 | case OMPD_taskwait: |
| 1154 | case OMPD_taskgroup: |
| 1155 | case OMPD_atomic: |
| 1156 | case OMPD_flush: |
| 1157 | case OMPD_teams: |
| 1158 | case OMPD_target_data: |
| 1159 | case OMPD_target_exit_data: |
| 1160 | case OMPD_target_enter_data: |
| 1161 | case OMPD_distribute: |
| 1162 | case OMPD_distribute_simd: |
| 1163 | case OMPD_distribute_parallel_for: |
| 1164 | case OMPD_distribute_parallel_for_simd: |
| 1165 | case OMPD_teams_distribute: |
| 1166 | case OMPD_teams_distribute_simd: |
| 1167 | case OMPD_teams_distribute_parallel_for: |
| 1168 | case OMPD_teams_distribute_parallel_for_simd: |
| 1169 | case OMPD_target_update: |
| 1170 | case OMPD_declare_simd: |
| 1171 | case OMPD_declare_target: |
| 1172 | case OMPD_end_declare_target: |
| 1173 | case OMPD_declare_reduction: |
| 1174 | case OMPD_taskloop: |
| 1175 | case OMPD_taskloop_simd: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 1176 | case OMPD_requires: |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1177 | case OMPD_unknown: |
| 1178 | break; |
| 1179 | } |
| 1180 | llvm_unreachable( |
| 1181 | "Unknown programming model for OpenMP directive on NVPTX target."); |
| 1182 | } |
| 1183 | |
| 1184 | void CGOpenMPRuntimeNVPTX::emitNonSPMDKernel(const OMPExecutableDirective &D, |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1185 | StringRef ParentName, |
| 1186 | llvm::Function *&OutlinedFn, |
| 1187 | llvm::Constant *&OutlinedFnID, |
| 1188 | bool IsOffloadEntry, |
| 1189 | const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1190 | ExecutionModeRAII ModeRAII(CurrentExecutionMode, /*IsSPMD=*/false); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1191 | EntryFunctionState EST; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1192 | WorkerFunctionState WST(CGM, D.getBeginLoc()); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1193 | Work.clear(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1194 | WrapperFunctionsMap.clear(); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1195 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1196 | // Emit target region as a standalone region. |
| 1197 | class NVPTXPrePostActionTy : public PrePostActionTy { |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1198 | CGOpenMPRuntimeNVPTX::EntryFunctionState &EST; |
| 1199 | CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1200 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1201 | public: |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 1202 | NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX::EntryFunctionState &EST, |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1203 | CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST) |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 1204 | : EST(EST), WST(WST) {} |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1205 | void Enter(CodeGenFunction &CGF) override { |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1206 | auto &RT = |
| 1207 | static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime()); |
Alexey Bataev | 6bc2732 | 2018-10-05 15:27:47 +0000 | [diff] [blame] | 1208 | RT.emitNonSPMDEntryHeader(CGF, EST, WST); |
| 1209 | // Skip target region initialization. |
| 1210 | RT.setLocThreadIdInsertPt(CGF, /*AtCurrentPoint=*/true); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1211 | } |
| 1212 | void Exit(CodeGenFunction &CGF) override { |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1213 | auto &RT = |
| 1214 | static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime()); |
Alexey Bataev | 6bc2732 | 2018-10-05 15:27:47 +0000 | [diff] [blame] | 1215 | RT.clearLocThreadIdInsertPt(CGF); |
| 1216 | RT.emitNonSPMDEntryFooter(CGF, EST); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1217 | } |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 1218 | } Action(EST, WST); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1219 | CodeGen.setAction(Action); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1220 | IsInTTDRegion = true; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1221 | // Reserve place for the globalized memory. |
| 1222 | GlobalizedRecords.emplace_back(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1223 | if (!KernelStaticGlobalized) { |
| 1224 | KernelStaticGlobalized = new llvm::GlobalVariable( |
| 1225 | CGM.getModule(), CGM.VoidPtrTy, /*isConstant=*/false, |
| 1226 | llvm::GlobalValue::InternalLinkage, |
| 1227 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 1228 | "_openmp_kernel_static_glob_rd$ptr", /*InsertBefore=*/nullptr, |
| 1229 | llvm::GlobalValue::NotThreadLocal, |
| 1230 | CGM.getContext().getTargetAddressSpace(LangAS::cuda_shared)); |
| 1231 | } |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1232 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 1233 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1234 | IsInTTDRegion = false; |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1235 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1236 | // Now change the name of the worker function to correspond to this target |
| 1237 | // region's entry function. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1238 | WST.WorkerFn->setName(Twine(OutlinedFn->getName(), "_worker")); |
Alexey Bataev | aee9389 | 2018-01-08 20:09:47 +0000 | [diff] [blame] | 1239 | |
| 1240 | // Create the worker function |
| 1241 | emitWorkerFunction(WST); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | // Setup NVPTX threads for master-worker OpenMP scheme. |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1245 | void CGOpenMPRuntimeNVPTX::emitNonSPMDEntryHeader(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1246 | EntryFunctionState &EST, |
| 1247 | WorkerFunctionState &WST) { |
| 1248 | CGBuilderTy &Bld = CGF.Builder; |
| 1249 | |
| 1250 | llvm::BasicBlock *WorkerBB = CGF.createBasicBlock(".worker"); |
| 1251 | llvm::BasicBlock *MasterCheckBB = CGF.createBasicBlock(".mastercheck"); |
| 1252 | llvm::BasicBlock *MasterBB = CGF.createBasicBlock(".master"); |
| 1253 | EST.ExitBB = CGF.createBasicBlock(".exit"); |
| 1254 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1255 | llvm::Value *IsWorker = |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1256 | Bld.CreateICmpULT(getNVPTXThreadID(CGF), getThreadLimit(CGF)); |
| 1257 | Bld.CreateCondBr(IsWorker, WorkerBB, MasterCheckBB); |
| 1258 | |
| 1259 | CGF.EmitBlock(WorkerBB); |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 1260 | emitCall(CGF, WST.Loc, WST.WorkerFn); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1261 | CGF.EmitBranch(EST.ExitBB); |
| 1262 | |
| 1263 | CGF.EmitBlock(MasterCheckBB); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1264 | llvm::Value *IsMaster = |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1265 | Bld.CreateICmpEQ(getNVPTXThreadID(CGF), getMasterThreadID(CGF)); |
| 1266 | Bld.CreateCondBr(IsMaster, MasterBB, EST.ExitBB); |
| 1267 | |
| 1268 | CGF.EmitBlock(MasterBB); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1269 | IsInTargetMasterThreadRegion = true; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1270 | // SEQUENTIAL (MASTER) REGION START |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1271 | // First action in sequential region: |
| 1272 | // Initialize the state of the OpenMP runtime library on the GPU. |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1273 | // TODO: Optimize runtime initialization and pass in correct value. |
| 1274 | llvm::Value *Args[] = {getThreadLimit(CGF), |
| 1275 | Bld.getInt16(/*RequiresOMPRuntime=*/1)}; |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1276 | CGF.EmitRuntimeCall( |
| 1277 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_init), Args); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1278 | |
| 1279 | // For data sharing, we need to initialize the stack. |
| 1280 | CGF.EmitRuntimeCall( |
| 1281 | createNVPTXRuntimeFunction( |
| 1282 | OMPRTL_NVPTX__kmpc_data_sharing_init_stack)); |
| 1283 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1284 | emitGenericVarsProlog(CGF, WST.Loc); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1287 | void CGOpenMPRuntimeNVPTX::emitNonSPMDEntryFooter(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1288 | EntryFunctionState &EST) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1289 | IsInTargetMasterThreadRegion = false; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1290 | if (!CGF.HaveInsertPoint()) |
| 1291 | return; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1292 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1293 | emitGenericVarsEpilog(CGF); |
| 1294 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1295 | if (!EST.ExitBB) |
| 1296 | EST.ExitBB = CGF.createBasicBlock(".exit"); |
| 1297 | |
| 1298 | llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".termination.notifier"); |
| 1299 | CGF.EmitBranch(TerminateBB); |
| 1300 | |
| 1301 | CGF.EmitBlock(TerminateBB); |
| 1302 | // Signal termination condition. |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1303 | // TODO: Optimize runtime initialization and pass in correct value. |
| 1304 | llvm::Value *Args[] = {CGF.Builder.getInt16(/*IsOMPRuntimeInitialized=*/1)}; |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1305 | CGF.EmitRuntimeCall( |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1306 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_deinit), Args); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1307 | // Barrier to terminate worker threads. |
| 1308 | syncCTAThreads(CGF); |
| 1309 | // Master thread jumps to exit point. |
| 1310 | CGF.EmitBranch(EST.ExitBB); |
| 1311 | |
| 1312 | CGF.EmitBlock(EST.ExitBB); |
| 1313 | EST.ExitBB = nullptr; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1316 | void CGOpenMPRuntimeNVPTX::emitSPMDKernel(const OMPExecutableDirective &D, |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1317 | StringRef ParentName, |
| 1318 | llvm::Function *&OutlinedFn, |
| 1319 | llvm::Constant *&OutlinedFnID, |
| 1320 | bool IsOffloadEntry, |
| 1321 | const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1322 | ExecutionModeRAII ModeRAII(CurrentExecutionMode, /*IsSPMD=*/true); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1323 | EntryFunctionState EST; |
| 1324 | |
| 1325 | // Emit target region as a standalone region. |
| 1326 | class NVPTXPrePostActionTy : public PrePostActionTy { |
| 1327 | CGOpenMPRuntimeNVPTX &RT; |
| 1328 | CGOpenMPRuntimeNVPTX::EntryFunctionState &EST; |
| 1329 | const OMPExecutableDirective &D; |
| 1330 | |
| 1331 | public: |
| 1332 | NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX &RT, |
| 1333 | CGOpenMPRuntimeNVPTX::EntryFunctionState &EST, |
| 1334 | const OMPExecutableDirective &D) |
| 1335 | : RT(RT), EST(EST), D(D) {} |
| 1336 | void Enter(CodeGenFunction &CGF) override { |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1337 | RT.emitSPMDEntryHeader(CGF, EST, D); |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1338 | // Skip target region initialization. |
| 1339 | RT.setLocThreadIdInsertPt(CGF, /*AtCurrentPoint=*/true); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1340 | } |
| 1341 | void Exit(CodeGenFunction &CGF) override { |
Alexey Bataev | fd006c4 | 2018-10-05 15:08:53 +0000 | [diff] [blame] | 1342 | RT.clearLocThreadIdInsertPt(CGF); |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1343 | RT.emitSPMDEntryFooter(CGF, EST); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } Action(*this, EST, D); |
| 1346 | CodeGen.setAction(Action); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1347 | IsInTTDRegion = true; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1348 | // Reserve place for the globalized memory. |
| 1349 | GlobalizedRecords.emplace_back(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1350 | if (!KernelStaticGlobalized) { |
| 1351 | KernelStaticGlobalized = new llvm::GlobalVariable( |
| 1352 | CGM.getModule(), CGM.VoidPtrTy, /*isConstant=*/false, |
| 1353 | llvm::GlobalValue::InternalLinkage, |
| 1354 | llvm::ConstantPointerNull::get(CGM.VoidPtrTy), |
| 1355 | "_openmp_kernel_static_glob_rd$ptr", /*InsertBefore=*/nullptr, |
| 1356 | llvm::GlobalValue::NotThreadLocal, |
| 1357 | CGM.getContext().getTargetAddressSpace(LangAS::cuda_shared)); |
| 1358 | } |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1359 | emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID, |
| 1360 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1361 | IsInTTDRegion = false; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1364 | void CGOpenMPRuntimeNVPTX::emitSPMDEntryHeader( |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1365 | CodeGenFunction &CGF, EntryFunctionState &EST, |
| 1366 | const OMPExecutableDirective &D) { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1367 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Setup BBs in entry function. |
| 1370 | llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute"); |
| 1371 | EST.ExitBB = CGF.createBasicBlock(".exit"); |
| 1372 | |
| 1373 | // Initialize the OMP state in the runtime; called by all active threads. |
Alexey Bataev | 80a9a61 | 2018-08-30 14:45:24 +0000 | [diff] [blame] | 1374 | bool RequiresFullRuntime = CGM.getLangOpts().OpenMPCUDAForceFullRuntime || |
| 1375 | !supportsLightweightRuntime(CGF.getContext(), D); |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1376 | llvm::Value *Args[] = {getThreadLimit(CGF, /*IsInSPMDExecutionMode=*/true), |
| 1377 | /*RequiresOMPRuntime=*/ |
| 1378 | Bld.getInt16(RequiresFullRuntime ? 1 : 0), |
| 1379 | /*RequiresDataSharing=*/Bld.getInt16(0)}; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1380 | CGF.EmitRuntimeCall( |
| 1381 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_init), Args); |
Gheorghe-Teodor Bercea | ad4e579 | 2018-07-13 16:18:24 +0000 | [diff] [blame] | 1382 | |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1383 | if (RequiresFullRuntime) { |
| 1384 | // For data sharing, we need to initialize the stack. |
| 1385 | CGF.EmitRuntimeCall(createNVPTXRuntimeFunction( |
| 1386 | OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd)); |
| 1387 | } |
Gheorghe-Teodor Bercea | ad4e579 | 2018-07-13 16:18:24 +0000 | [diff] [blame] | 1388 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1389 | CGF.EmitBranch(ExecuteBB); |
| 1390 | |
| 1391 | CGF.EmitBlock(ExecuteBB); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1392 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1393 | IsInTargetMasterThreadRegion = true; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1396 | void CGOpenMPRuntimeNVPTX::emitSPMDEntryFooter(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1397 | EntryFunctionState &EST) { |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1398 | IsInTargetMasterThreadRegion = false; |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1399 | if (!CGF.HaveInsertPoint()) |
| 1400 | return; |
| 1401 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1402 | if (!EST.ExitBB) |
| 1403 | EST.ExitBB = CGF.createBasicBlock(".exit"); |
| 1404 | |
| 1405 | llvm::BasicBlock *OMPDeInitBB = CGF.createBasicBlock(".omp.deinit"); |
| 1406 | CGF.EmitBranch(OMPDeInitBB); |
| 1407 | |
| 1408 | CGF.EmitBlock(OMPDeInitBB); |
| 1409 | // DeInitialize the OMP state in the runtime; called by all active threads. |
| 1410 | CGF.EmitRuntimeCall( |
| 1411 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_deinit), None); |
| 1412 | CGF.EmitBranch(EST.ExitBB); |
| 1413 | |
| 1414 | CGF.EmitBlock(EST.ExitBB); |
| 1415 | EST.ExitBB = nullptr; |
| 1416 | } |
| 1417 | |
| 1418 | // Create a unique global variable to indicate the execution mode of this target |
| 1419 | // region. The execution mode is either 'generic', or 'spmd' depending on the |
| 1420 | // target directive. This variable is picked up by the offload library to setup |
| 1421 | // the device appropriately before kernel launch. If the execution mode is |
| 1422 | // 'generic', the runtime reserves one warp for the master, otherwise, all |
| 1423 | // warps participate in parallel work. |
| 1424 | static void setPropertyExecutionMode(CodeGenModule &CGM, StringRef Name, |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1425 | bool Mode) { |
| 1426 | auto *GVMode = |
| 1427 | new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 1428 | llvm::GlobalValue::WeakAnyLinkage, |
| 1429 | llvm::ConstantInt::get(CGM.Int8Ty, Mode ? 0 : 1), |
| 1430 | Twine(Name, "_exec_mode")); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1431 | CGM.addCompilerUsedGlobal(GVMode); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1434 | void CGOpenMPRuntimeNVPTX::emitWorkerFunction(WorkerFunctionState &WST) { |
Gheorghe-Teodor Bercea | eb89b1d | 2017-11-21 15:54:54 +0000 | [diff] [blame] | 1435 | ASTContext &Ctx = CGM.getContext(); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1436 | |
| 1437 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1438 | CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, WST.WorkerFn, WST.CGFI, {}, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 1439 | WST.Loc, WST.Loc); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1440 | emitWorkerLoop(CGF, WST); |
| 1441 | CGF.FinishFunction(); |
| 1442 | } |
| 1443 | |
| 1444 | void CGOpenMPRuntimeNVPTX::emitWorkerLoop(CodeGenFunction &CGF, |
| 1445 | WorkerFunctionState &WST) { |
| 1446 | // |
| 1447 | // The workers enter this loop and wait for parallel work from the master. |
| 1448 | // When the master encounters a parallel region it sets up the work + variable |
| 1449 | // arguments, and wakes up the workers. The workers first check to see if |
| 1450 | // they are required for the parallel region, i.e., within the # of requested |
| 1451 | // parallel threads. The activated workers load the variable arguments and |
| 1452 | // execute the parallel work. |
| 1453 | // |
| 1454 | |
| 1455 | CGBuilderTy &Bld = CGF.Builder; |
| 1456 | |
| 1457 | llvm::BasicBlock *AwaitBB = CGF.createBasicBlock(".await.work"); |
| 1458 | llvm::BasicBlock *SelectWorkersBB = CGF.createBasicBlock(".select.workers"); |
| 1459 | llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute.parallel"); |
| 1460 | llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".terminate.parallel"); |
| 1461 | llvm::BasicBlock *BarrierBB = CGF.createBasicBlock(".barrier.parallel"); |
| 1462 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit"); |
| 1463 | |
| 1464 | CGF.EmitBranch(AwaitBB); |
| 1465 | |
| 1466 | // Workers wait for work from master. |
| 1467 | CGF.EmitBlock(AwaitBB); |
| 1468 | // Wait for parallel work |
| 1469 | syncCTAThreads(CGF); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1470 | |
| 1471 | Address WorkFn = |
| 1472 | CGF.CreateDefaultAlignTempAlloca(CGF.Int8PtrTy, /*Name=*/"work_fn"); |
| 1473 | Address ExecStatus = |
| 1474 | CGF.CreateDefaultAlignTempAlloca(CGF.Int8Ty, /*Name=*/"exec_status"); |
| 1475 | CGF.InitTempAlloca(ExecStatus, Bld.getInt8(/*C=*/0)); |
| 1476 | CGF.InitTempAlloca(WorkFn, llvm::Constant::getNullValue(CGF.Int8PtrTy)); |
| 1477 | |
Jonas Hahnfeld | fa059ba | 2017-12-27 10:39:56 +0000 | [diff] [blame] | 1478 | // TODO: Optimize runtime initialization and pass in correct value. |
Gheorghe-Teodor Bercea | 7d80da1 | 2018-03-07 21:59:50 +0000 | [diff] [blame] | 1479 | llvm::Value *Args[] = {WorkFn.getPointer(), |
Jonas Hahnfeld | fa059ba | 2017-12-27 10:39:56 +0000 | [diff] [blame] | 1480 | /*RequiresOMPRuntime=*/Bld.getInt16(1)}; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1481 | llvm::Value *Ret = CGF.EmitRuntimeCall( |
| 1482 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_parallel), Args); |
| 1483 | Bld.CreateStore(Bld.CreateZExt(Ret, CGF.Int8Ty), ExecStatus); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1484 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1485 | // On termination condition (workid == 0), exit loop. |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1486 | llvm::Value *WorkID = Bld.CreateLoad(WorkFn); |
| 1487 | llvm::Value *ShouldTerminate = Bld.CreateIsNull(WorkID, "should_terminate"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1488 | Bld.CreateCondBr(ShouldTerminate, ExitBB, SelectWorkersBB); |
| 1489 | |
| 1490 | // Activate requested workers. |
| 1491 | CGF.EmitBlock(SelectWorkersBB); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1492 | llvm::Value *IsActive = |
| 1493 | Bld.CreateIsNotNull(Bld.CreateLoad(ExecStatus), "is_active"); |
| 1494 | Bld.CreateCondBr(IsActive, ExecuteBB, BarrierBB); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1495 | |
| 1496 | // Signal start of parallel region. |
| 1497 | CGF.EmitBlock(ExecuteBB); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1498 | |
| 1499 | // Process work items: outlined parallel functions. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1500 | for (llvm::Function *W : Work) { |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1501 | // Try to match this outlined function. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1502 | llvm::Value *ID = Bld.CreatePointerBitCastOrAddrSpaceCast(W, CGM.Int8PtrTy); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1503 | |
| 1504 | llvm::Value *WorkFnMatch = |
| 1505 | Bld.CreateICmpEQ(Bld.CreateLoad(WorkFn), ID, "work_match"); |
| 1506 | |
| 1507 | llvm::BasicBlock *ExecuteFNBB = CGF.createBasicBlock(".execute.fn"); |
| 1508 | llvm::BasicBlock *CheckNextBB = CGF.createBasicBlock(".check.next"); |
| 1509 | Bld.CreateCondBr(WorkFnMatch, ExecuteFNBB, CheckNextBB); |
| 1510 | |
| 1511 | // Execute this outlined function. |
| 1512 | CGF.EmitBlock(ExecuteFNBB); |
| 1513 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1514 | // Insert call to work function via shared wrapper. The shared |
| 1515 | // wrapper takes two arguments: |
| 1516 | // - the parallelism level; |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 1517 | // - the thread ID; |
| 1518 | emitCall(CGF, WST.Loc, W, |
| 1519 | {Bld.getInt16(/*ParallelLevel=*/0), getThreadID(CGF, WST.Loc)}); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1520 | |
| 1521 | // Go to end of parallel region. |
| 1522 | CGF.EmitBranch(TerminateBB); |
| 1523 | |
| 1524 | CGF.EmitBlock(CheckNextBB); |
| 1525 | } |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1526 | // Default case: call to outlined function through pointer if the target |
| 1527 | // region makes a declare target call that may contain an orphaned parallel |
| 1528 | // directive. |
| 1529 | auto *ParallelFnTy = |
| 1530 | llvm::FunctionType::get(CGM.VoidTy, {CGM.Int16Ty, CGM.Int32Ty}, |
| 1531 | /*isVarArg=*/false) |
| 1532 | ->getPointerTo(); |
| 1533 | llvm::Value *WorkFnCast = Bld.CreateBitCast(WorkID, ParallelFnTy); |
| 1534 | // Insert call to work function via shared wrapper. The shared |
| 1535 | // wrapper takes two arguments: |
| 1536 | // - the parallelism level; |
| 1537 | // - the thread ID; |
| 1538 | emitCall(CGF, WST.Loc, WorkFnCast, |
| 1539 | {Bld.getInt16(/*ParallelLevel=*/0), getThreadID(CGF, WST.Loc)}); |
| 1540 | // Go to end of parallel region. |
| 1541 | CGF.EmitBranch(TerminateBB); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Signal end of parallel region. |
| 1544 | CGF.EmitBlock(TerminateBB); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1545 | CGF.EmitRuntimeCall( |
| 1546 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_end_parallel), |
| 1547 | llvm::None); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1548 | CGF.EmitBranch(BarrierBB); |
| 1549 | |
| 1550 | // All active and inactive workers wait at a barrier after parallel region. |
| 1551 | CGF.EmitBlock(BarrierBB); |
| 1552 | // Barrier after parallel region. |
| 1553 | syncCTAThreads(CGF); |
| 1554 | CGF.EmitBranch(AwaitBB); |
| 1555 | |
| 1556 | // Exit target region. |
| 1557 | CGF.EmitBlock(ExitBB); |
| 1558 | } |
| 1559 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1560 | /// Returns specified OpenMP runtime function for the current OpenMP |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1561 | /// implementation. Specialized for the NVPTX device. |
| 1562 | /// \param Function OpenMP runtime function. |
| 1563 | /// \return Specified function. |
| 1564 | llvm::Constant * |
| 1565 | CGOpenMPRuntimeNVPTX::createNVPTXRuntimeFunction(unsigned Function) { |
| 1566 | llvm::Constant *RTLFn = nullptr; |
| 1567 | switch (static_cast<OpenMPRTLFunctionNVPTX>(Function)) { |
| 1568 | case OMPRTL_NVPTX__kmpc_kernel_init: { |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1569 | // Build void __kmpc_kernel_init(kmp_int32 thread_limit, int16_t |
| 1570 | // RequiresOMPRuntime); |
| 1571 | llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1572 | auto *FnTy = |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1573 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1574 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_init"); |
| 1575 | break; |
| 1576 | } |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1577 | case OMPRTL_NVPTX__kmpc_kernel_deinit: { |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1578 | // Build void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized); |
| 1579 | llvm::Type *TypeParams[] = {CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1580 | auto *FnTy = |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1581 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 1582 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_deinit"); |
| 1583 | break; |
| 1584 | } |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1585 | case OMPRTL_NVPTX__kmpc_spmd_kernel_init: { |
| 1586 | // Build void __kmpc_spmd_kernel_init(kmp_int32 thread_limit, |
Jonas Hahnfeld | 891c7fb | 2017-11-22 14:46:49 +0000 | [diff] [blame] | 1587 | // int16_t RequiresOMPRuntime, int16_t RequiresDataSharing); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1588 | llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1589 | auto *FnTy = |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1590 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1591 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_init"); |
| 1592 | break; |
| 1593 | } |
| 1594 | case OMPRTL_NVPTX__kmpc_spmd_kernel_deinit: { |
| 1595 | // Build void __kmpc_spmd_kernel_deinit(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1596 | auto *FnTy = |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1597 | llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false); |
| 1598 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_deinit"); |
| 1599 | break; |
| 1600 | } |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1601 | case OMPRTL_NVPTX__kmpc_kernel_prepare_parallel: { |
| 1602 | /// Build void __kmpc_kernel_prepare_parallel( |
Gheorghe-Teodor Bercea | 7d80da1 | 2018-03-07 21:59:50 +0000 | [diff] [blame] | 1603 | /// void *outlined_function, int16_t IsOMPRuntimeInitialized); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1604 | llvm::Type *TypeParams[] = {CGM.Int8PtrTy, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1605 | auto *FnTy = |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1606 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1607 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_prepare_parallel"); |
| 1608 | break; |
| 1609 | } |
| 1610 | case OMPRTL_NVPTX__kmpc_kernel_parallel: { |
Gheorghe-Teodor Bercea | 7d80da1 | 2018-03-07 21:59:50 +0000 | [diff] [blame] | 1611 | /// Build bool __kmpc_kernel_parallel(void **outlined_function, |
| 1612 | /// int16_t IsOMPRuntimeInitialized); |
| 1613 | llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy, CGM.Int16Ty}; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1614 | llvm::Type *RetTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1615 | auto *FnTy = |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1616 | llvm::FunctionType::get(RetTy, TypeParams, /*isVarArg*/ false); |
| 1617 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_parallel"); |
| 1618 | break; |
| 1619 | } |
| 1620 | case OMPRTL_NVPTX__kmpc_kernel_end_parallel: { |
| 1621 | /// Build void __kmpc_kernel_end_parallel(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1622 | auto *FnTy = |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1623 | llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false); |
| 1624 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_end_parallel"); |
| 1625 | break; |
| 1626 | } |
| 1627 | case OMPRTL_NVPTX__kmpc_serialized_parallel: { |
| 1628 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 1629 | // global_tid); |
| 1630 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1631 | auto *FnTy = |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1632 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1633 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 1634 | break; |
| 1635 | } |
| 1636 | case OMPRTL_NVPTX__kmpc_end_serialized_parallel: { |
| 1637 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 1638 | // global_tid); |
| 1639 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1640 | auto *FnTy = |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 1641 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1642 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 1643 | break; |
| 1644 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1645 | case OMPRTL_NVPTX__kmpc_shuffle_int32: { |
| 1646 | // Build int32_t __kmpc_shuffle_int32(int32_t element, |
| 1647 | // int16_t lane_offset, int16_t warp_size); |
| 1648 | llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1649 | auto *FnTy = |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1650 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1651 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int32"); |
| 1652 | break; |
| 1653 | } |
| 1654 | case OMPRTL_NVPTX__kmpc_shuffle_int64: { |
| 1655 | // Build int64_t __kmpc_shuffle_int64(int64_t element, |
| 1656 | // int16_t lane_offset, int16_t warp_size); |
| 1657 | llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int16Ty, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1658 | auto *FnTy = |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1659 | llvm::FunctionType::get(CGM.Int64Ty, TypeParams, /*isVarArg*/ false); |
| 1660 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int64"); |
| 1661 | break; |
| 1662 | } |
| 1663 | case OMPRTL_NVPTX__kmpc_parallel_reduce_nowait: { |
| 1664 | // Build int32_t kmpc_nvptx_parallel_reduce_nowait(kmp_int32 global_tid, |
| 1665 | // kmp_int32 num_vars, size_t reduce_size, void* reduce_data, |
| 1666 | // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t |
| 1667 | // lane_offset, int16_t Algorithm Version), |
| 1668 | // void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num)); |
| 1669 | llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty, |
| 1670 | CGM.Int16Ty, CGM.Int16Ty}; |
| 1671 | auto *ShuffleReduceFnTy = |
| 1672 | llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams, |
| 1673 | /*isVarArg=*/false); |
| 1674 | llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty}; |
| 1675 | auto *InterWarpCopyFnTy = |
| 1676 | llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams, |
| 1677 | /*isVarArg=*/false); |
| 1678 | llvm::Type *TypeParams[] = {CGM.Int32Ty, |
| 1679 | CGM.Int32Ty, |
| 1680 | CGM.SizeTy, |
| 1681 | CGM.VoidPtrTy, |
| 1682 | ShuffleReduceFnTy->getPointerTo(), |
| 1683 | InterWarpCopyFnTy->getPointerTo()}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1684 | auto *FnTy = |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1685 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1686 | RTLFn = CGM.CreateRuntimeFunction( |
| 1687 | FnTy, /*Name=*/"__kmpc_nvptx_parallel_reduce_nowait"); |
| 1688 | break; |
| 1689 | } |
Alexey Bataev | fac26cf | 2018-05-02 20:03:27 +0000 | [diff] [blame] | 1690 | case OMPRTL_NVPTX__kmpc_simd_reduce_nowait: { |
| 1691 | // Build int32_t kmpc_nvptx_simd_reduce_nowait(kmp_int32 global_tid, |
| 1692 | // kmp_int32 num_vars, size_t reduce_size, void* reduce_data, |
| 1693 | // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t |
| 1694 | // lane_offset, int16_t Algorithm Version), |
| 1695 | // void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num)); |
| 1696 | llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty, |
| 1697 | CGM.Int16Ty, CGM.Int16Ty}; |
| 1698 | auto *ShuffleReduceFnTy = |
| 1699 | llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams, |
| 1700 | /*isVarArg=*/false); |
| 1701 | llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty}; |
| 1702 | auto *InterWarpCopyFnTy = |
| 1703 | llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams, |
| 1704 | /*isVarArg=*/false); |
| 1705 | llvm::Type *TypeParams[] = {CGM.Int32Ty, |
| 1706 | CGM.Int32Ty, |
| 1707 | CGM.SizeTy, |
| 1708 | CGM.VoidPtrTy, |
| 1709 | ShuffleReduceFnTy->getPointerTo(), |
| 1710 | InterWarpCopyFnTy->getPointerTo()}; |
| 1711 | auto *FnTy = |
| 1712 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1713 | RTLFn = CGM.CreateRuntimeFunction( |
| 1714 | FnTy, /*Name=*/"__kmpc_nvptx_simd_reduce_nowait"); |
| 1715 | break; |
| 1716 | } |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 1717 | case OMPRTL_NVPTX__kmpc_teams_reduce_nowait: { |
| 1718 | // Build int32_t __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid, |
| 1719 | // int32_t num_vars, size_t reduce_size, void *reduce_data, |
| 1720 | // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t |
| 1721 | // lane_offset, int16_t shortCircuit), |
| 1722 | // void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num), |
| 1723 | // void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad, |
| 1724 | // int32_t index, int32_t width), |
| 1725 | // void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad, |
| 1726 | // int32_t index, int32_t width, int32_t reduce)) |
| 1727 | llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty, |
| 1728 | CGM.Int16Ty, CGM.Int16Ty}; |
| 1729 | auto *ShuffleReduceFnTy = |
| 1730 | llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams, |
| 1731 | /*isVarArg=*/false); |
| 1732 | llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty}; |
| 1733 | auto *InterWarpCopyFnTy = |
| 1734 | llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams, |
| 1735 | /*isVarArg=*/false); |
| 1736 | llvm::Type *CopyToScratchpadTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1737 | CGM.Int32Ty, CGM.Int32Ty}; |
| 1738 | auto *CopyToScratchpadFnTy = |
| 1739 | llvm::FunctionType::get(CGM.VoidTy, CopyToScratchpadTypeParams, |
| 1740 | /*isVarArg=*/false); |
| 1741 | llvm::Type *LoadReduceTypeParams[] = { |
| 1742 | CGM.VoidPtrTy, CGM.VoidPtrTy, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty}; |
| 1743 | auto *LoadReduceFnTy = |
| 1744 | llvm::FunctionType::get(CGM.VoidTy, LoadReduceTypeParams, |
| 1745 | /*isVarArg=*/false); |
| 1746 | llvm::Type *TypeParams[] = {CGM.Int32Ty, |
| 1747 | CGM.Int32Ty, |
| 1748 | CGM.SizeTy, |
| 1749 | CGM.VoidPtrTy, |
| 1750 | ShuffleReduceFnTy->getPointerTo(), |
| 1751 | InterWarpCopyFnTy->getPointerTo(), |
| 1752 | CopyToScratchpadFnTy->getPointerTo(), |
| 1753 | LoadReduceFnTy->getPointerTo()}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1754 | auto *FnTy = |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 1755 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 1756 | RTLFn = CGM.CreateRuntimeFunction( |
| 1757 | FnTy, /*Name=*/"__kmpc_nvptx_teams_reduce_nowait"); |
| 1758 | break; |
| 1759 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1760 | case OMPRTL_NVPTX__kmpc_end_reduce_nowait: { |
| 1761 | // Build __kmpc_end_reduce_nowait(kmp_int32 global_tid); |
| 1762 | llvm::Type *TypeParams[] = {CGM.Int32Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1763 | auto *FnTy = |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1764 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1765 | RTLFn = CGM.CreateRuntimeFunction( |
| 1766 | FnTy, /*Name=*/"__kmpc_nvptx_end_reduce_nowait"); |
| 1767 | break; |
| 1768 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1769 | case OMPRTL_NVPTX__kmpc_data_sharing_init_stack: { |
| 1770 | /// Build void __kmpc_data_sharing_init_stack(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1771 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1772 | llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false); |
| 1773 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_data_sharing_init_stack"); |
| 1774 | break; |
| 1775 | } |
Gheorghe-Teodor Bercea | ad4e579 | 2018-07-13 16:18:24 +0000 | [diff] [blame] | 1776 | case OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd: { |
| 1777 | /// Build void __kmpc_data_sharing_init_stack_spmd(); |
| 1778 | auto *FnTy = |
| 1779 | llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 1780 | RTLFn = |
| 1781 | CGM.CreateRuntimeFunction(FnTy, "__kmpc_data_sharing_init_stack_spmd"); |
Gheorghe-Teodor Bercea | ad4e579 | 2018-07-13 16:18:24 +0000 | [diff] [blame] | 1782 | break; |
| 1783 | } |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 1784 | case OMPRTL_NVPTX__kmpc_data_sharing_coalesced_push_stack: { |
| 1785 | // Build void *__kmpc_data_sharing_coalesced_push_stack(size_t size, |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1786 | // int16_t UseSharedMemory); |
| 1787 | llvm::Type *TypeParams[] = {CGM.SizeTy, CGM.Int16Ty}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1788 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1789 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 1790 | RTLFn = CGM.CreateRuntimeFunction( |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 1791 | FnTy, /*Name=*/"__kmpc_data_sharing_coalesced_push_stack"); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1792 | break; |
| 1793 | } |
| 1794 | case OMPRTL_NVPTX__kmpc_data_sharing_pop_stack: { |
| 1795 | // Build void __kmpc_data_sharing_pop_stack(void *a); |
| 1796 | llvm::Type *TypeParams[] = {CGM.VoidPtrTy}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1797 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1798 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1799 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 1800 | /*Name=*/"__kmpc_data_sharing_pop_stack"); |
| 1801 | break; |
| 1802 | } |
| 1803 | case OMPRTL_NVPTX__kmpc_begin_sharing_variables: { |
| 1804 | /// Build void __kmpc_begin_sharing_variables(void ***args, |
| 1805 | /// size_t n_args); |
| 1806 | llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo(), CGM.SizeTy}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1807 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1808 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1809 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_begin_sharing_variables"); |
| 1810 | break; |
| 1811 | } |
| 1812 | case OMPRTL_NVPTX__kmpc_end_sharing_variables: { |
| 1813 | /// Build void __kmpc_end_sharing_variables(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1814 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1815 | llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false); |
| 1816 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_sharing_variables"); |
| 1817 | break; |
| 1818 | } |
| 1819 | case OMPRTL_NVPTX__kmpc_get_shared_variables: { |
| 1820 | /// Build void __kmpc_get_shared_variables(void ***GlobalArgs); |
| 1821 | llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo()}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1822 | auto *FnTy = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1823 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1824 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_get_shared_variables"); |
| 1825 | break; |
| 1826 | } |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1827 | case OMPRTL_NVPTX__kmpc_parallel_level: { |
| 1828 | // Build uint16_t __kmpc_parallel_level(ident_t *loc, kmp_int32 global_tid); |
| 1829 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 1830 | auto *FnTy = |
| 1831 | llvm::FunctionType::get(CGM.Int16Ty, TypeParams, /*isVarArg*/ false); |
| 1832 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_parallel_level"); |
| 1833 | break; |
| 1834 | } |
Alexey Bataev | 673110d | 2018-05-16 13:36:30 +0000 | [diff] [blame] | 1835 | case OMPRTL_NVPTX__kmpc_is_spmd_exec_mode: { |
| 1836 | // Build int8_t __kmpc_is_spmd_exec_mode(); |
| 1837 | auto *FnTy = llvm::FunctionType::get(CGM.Int8Ty, /*isVarArg=*/false); |
| 1838 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_is_spmd_exec_mode"); |
| 1839 | break; |
| 1840 | } |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 1841 | case OMPRTL_NVPTX__kmpc_get_team_static_memory: { |
| 1842 | // Build void __kmpc_get_team_static_memory(const void *buf, size_t size, |
| 1843 | // int16_t is_shared, const void **res); |
| 1844 | llvm::Type *TypeParams[] = {CGM.VoidPtrTy, CGM.SizeTy, CGM.Int16Ty, |
| 1845 | CGM.VoidPtrPtrTy}; |
| 1846 | auto *FnTy = |
| 1847 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1848 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_get_team_static_memory"); |
| 1849 | break; |
| 1850 | } |
| 1851 | case OMPRTL_NVPTX__kmpc_restore_team_static_memory: { |
| 1852 | // Build void __kmpc_restore_team_static_memory(int16_t is_shared); |
| 1853 | auto *FnTy = |
| 1854 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int16Ty, /*isVarArg=*/false); |
| 1855 | RTLFn = |
| 1856 | CGM.CreateRuntimeFunction(FnTy, "__kmpc_restore_team_static_memory"); |
| 1857 | break; |
| 1858 | } |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1859 | } |
| 1860 | return RTLFn; |
| 1861 | } |
| 1862 | |
| 1863 | void CGOpenMPRuntimeNVPTX::createOffloadEntry(llvm::Constant *ID, |
| 1864 | llvm::Constant *Addr, |
Alexey Bataev | 03f270c | 2018-03-30 18:31:07 +0000 | [diff] [blame] | 1865 | uint64_t Size, int32_t, |
| 1866 | llvm::GlobalValue::LinkageTypes) { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1867 | // TODO: Add support for global variables on the device after declare target |
| 1868 | // support. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1869 | if (!isa<llvm::Function>(Addr)) |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1870 | return; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1871 | llvm::Module &M = CGM.getModule(); |
| 1872 | llvm::LLVMContext &Ctx = CGM.getLLVMContext(); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Get "nvvm.annotations" metadata node |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1875 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("nvvm.annotations"); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1876 | |
| 1877 | llvm::Metadata *MDVals[] = { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 1878 | llvm::ConstantAsMetadata::get(Addr), llvm::MDString::get(Ctx, "kernel"), |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1879 | llvm::ConstantAsMetadata::get( |
| 1880 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1))}; |
| 1881 | // Append metadata to nvvm.annotations |
| 1882 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 1883 | } |
| 1884 | |
| 1885 | void CGOpenMPRuntimeNVPTX::emitTargetOutlinedFunction( |
| 1886 | const OMPExecutableDirective &D, StringRef ParentName, |
| 1887 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1888 | bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1889 | if (!IsOffloadEntry) // Nothing to do. |
| 1890 | return; |
| 1891 | |
| 1892 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
| 1893 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1894 | bool Mode = supportsSPMDExecutionMode(CGM.getContext(), D); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1895 | if (Mode) |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1896 | emitSPMDKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1897 | CodeGen); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1898 | else |
| 1899 | emitNonSPMDKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, |
| 1900 | CodeGen); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 1901 | |
| 1902 | setPropertyExecutionMode(CGM, OutlinedFn->getName(), Mode); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 1905 | CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM) |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1906 | : CGOpenMPRuntime(CGM, "_", "$") { |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1907 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 1908 | llvm_unreachable("OpenMP NVPTX can only handle device code."); |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 1909 | } |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 1910 | |
Arpith Chacko Jacob | 2cd6eea | 2017-01-25 16:55:10 +0000 | [diff] [blame] | 1911 | void CGOpenMPRuntimeNVPTX::emitProcBindClause(CodeGenFunction &CGF, |
| 1912 | OpenMPProcBindClauseKind ProcBind, |
| 1913 | SourceLocation Loc) { |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1914 | // Do nothing in case of SPMD mode and L0 parallel. |
Alexey Bataev | 2a3320a | 2018-05-15 18:01:01 +0000 | [diff] [blame] | 1915 | if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) |
Arpith Chacko Jacob | 2cd6eea | 2017-01-25 16:55:10 +0000 | [diff] [blame] | 1916 | return; |
| 1917 | |
| 1918 | CGOpenMPRuntime::emitProcBindClause(CGF, ProcBind, Loc); |
| 1919 | } |
| 1920 | |
Arpith Chacko Jacob | e04da5d | 2017-01-25 01:18:34 +0000 | [diff] [blame] | 1921 | void CGOpenMPRuntimeNVPTX::emitNumThreadsClause(CodeGenFunction &CGF, |
| 1922 | llvm::Value *NumThreads, |
| 1923 | SourceLocation Loc) { |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 1924 | // Do nothing in case of SPMD mode and L0 parallel. |
Alexey Bataev | 2a3320a | 2018-05-15 18:01:01 +0000 | [diff] [blame] | 1925 | if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) |
Arpith Chacko Jacob | e04da5d | 2017-01-25 01:18:34 +0000 | [diff] [blame] | 1926 | return; |
| 1927 | |
| 1928 | CGOpenMPRuntime::emitNumThreadsClause(CGF, NumThreads, Loc); |
| 1929 | } |
| 1930 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 1931 | void CGOpenMPRuntimeNVPTX::emitNumTeamsClause(CodeGenFunction &CGF, |
| 1932 | const Expr *NumTeams, |
| 1933 | const Expr *ThreadLimit, |
| 1934 | SourceLocation Loc) {} |
| 1935 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1936 | llvm::Value *CGOpenMPRuntimeNVPTX::emitParallelOutlinedFunction( |
| 1937 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 1938 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1939 | // Emit target region as a standalone region. |
| 1940 | class NVPTXPrePostActionTy : public PrePostActionTy { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1941 | bool &IsInParallelRegion; |
| 1942 | bool PrevIsInParallelRegion; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1943 | |
| 1944 | public: |
Alexey Bataev | b99dcb5 | 2018-07-09 17:43:58 +0000 | [diff] [blame] | 1945 | NVPTXPrePostActionTy(bool &IsInParallelRegion) |
| 1946 | : IsInParallelRegion(IsInParallelRegion) {} |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1947 | void Enter(CodeGenFunction &CGF) override { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1948 | PrevIsInParallelRegion = IsInParallelRegion; |
| 1949 | IsInParallelRegion = true; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1950 | } |
| 1951 | void Exit(CodeGenFunction &CGF) override { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1952 | IsInParallelRegion = PrevIsInParallelRegion; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1953 | } |
Alexey Bataev | b99dcb5 | 2018-07-09 17:43:58 +0000 | [diff] [blame] | 1954 | } Action(IsInParallelRegion); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1955 | CodeGen.setAction(Action); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1956 | bool PrevIsInTTDRegion = IsInTTDRegion; |
| 1957 | IsInTTDRegion = false; |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1958 | bool PrevIsInTargetMasterThreadRegion = IsInTargetMasterThreadRegion; |
| 1959 | IsInTargetMasterThreadRegion = false; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1960 | auto *OutlinedFun = |
| 1961 | cast<llvm::Function>(CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 1962 | D, ThreadIDVar, InnermostKind, CodeGen)); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 1963 | IsInTargetMasterThreadRegion = PrevIsInTargetMasterThreadRegion; |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 1964 | IsInTTDRegion = PrevIsInTTDRegion; |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 1965 | if (getExecutionMode() != CGOpenMPRuntimeNVPTX::EM_SPMD && |
| 1966 | !IsInParallelRegion) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 1967 | llvm::Function *WrapperFun = |
| 1968 | createParallelDataSharingWrapper(OutlinedFun, D); |
| 1969 | WrapperFunctionsMap[OutlinedFun] = WrapperFun; |
| 1970 | } |
| 1971 | |
| 1972 | return OutlinedFun; |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 1975 | /// Get list of lastprivate variables from the teams distribute ... or |
| 1976 | /// teams {distribute ...} directives. |
| 1977 | static void |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1978 | getDistributeLastprivateVars(ASTContext &Ctx, const OMPExecutableDirective &D, |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 1979 | llvm::SmallVectorImpl<const ValueDecl *> &Vars) { |
| 1980 | assert(isOpenMPTeamsDirective(D.getDirectiveKind()) && |
| 1981 | "expected teams directive."); |
| 1982 | const OMPExecutableDirective *Dir = &D; |
| 1983 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1984 | if (const Stmt *S = getSingleCompoundChild( |
Alexey Bataev | 8bcc69c | 2018-11-09 20:03:19 +0000 | [diff] [blame] | 1985 | Ctx, |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 1986 | D.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers( |
| 1987 | /*IgnoreCaptured=*/true))) { |
| 1988 | Dir = dyn_cast<OMPExecutableDirective>(S); |
| 1989 | if (Dir && !isOpenMPDistributeDirective(Dir->getDirectiveKind())) |
| 1990 | Dir = nullptr; |
| 1991 | } |
| 1992 | } |
| 1993 | if (!Dir) |
| 1994 | return; |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 1995 | for (const auto *C : Dir->getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 1996 | for (const Expr *E : C->getVarRefs()) |
| 1997 | Vars.push_back(getPrivateItem(E)); |
| 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | /// Get list of reduction variables from the teams ... directives. |
| 2002 | static void |
| 2003 | getTeamsReductionVars(ASTContext &Ctx, const OMPExecutableDirective &D, |
| 2004 | llvm::SmallVectorImpl<const ValueDecl *> &Vars) { |
| 2005 | assert(isOpenMPTeamsDirective(D.getDirectiveKind()) && |
| 2006 | "expected teams directive."); |
| 2007 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
| 2008 | for (const Expr *E : C->privates()) |
| 2009 | Vars.push_back(getPrivateItem(E)); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2013 | llvm::Value *CGOpenMPRuntimeNVPTX::emitTeamsOutlinedFunction( |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2014 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 2015 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2016 | SourceLocation Loc = D.getBeginLoc(); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2017 | |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2018 | const RecordDecl *GlobalizedRD = nullptr; |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2019 | llvm::SmallVector<const ValueDecl *, 4> LastPrivatesReductions; |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2020 | llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> MappedDeclsFields; |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2021 | // Globalize team reductions variable unconditionally in all modes. |
| 2022 | getTeamsReductionVars(CGM.getContext(), D, LastPrivatesReductions); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2023 | if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2024 | getDistributeLastprivateVars(CGM.getContext(), D, LastPrivatesReductions); |
| 2025 | if (!LastPrivatesReductions.empty()) { |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2026 | GlobalizedRD = ::buildRecordForGlobalizedVars( |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2027 | CGM.getContext(), llvm::None, LastPrivatesReductions, |
| 2028 | MappedDeclsFields); |
| 2029 | } |
| 2030 | } else if (!LastPrivatesReductions.empty()) { |
| 2031 | assert(!TeamAndReductions.first && |
| 2032 | "Previous team declaration is not expected."); |
| 2033 | TeamAndReductions.first = D.getCapturedStmt(OMPD_teams)->getCapturedDecl(); |
| 2034 | std::swap(TeamAndReductions.second, LastPrivatesReductions); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2037 | // Emit target region as a standalone region. |
| 2038 | class NVPTXPrePostActionTy : public PrePostActionTy { |
| 2039 | SourceLocation &Loc; |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2040 | const RecordDecl *GlobalizedRD; |
| 2041 | llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> |
| 2042 | &MappedDeclsFields; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2043 | |
| 2044 | public: |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2045 | NVPTXPrePostActionTy( |
| 2046 | SourceLocation &Loc, const RecordDecl *GlobalizedRD, |
| 2047 | llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> |
| 2048 | &MappedDeclsFields) |
| 2049 | : Loc(Loc), GlobalizedRD(GlobalizedRD), |
| 2050 | MappedDeclsFields(MappedDeclsFields) {} |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2051 | void Enter(CodeGenFunction &CGF) override { |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2052 | auto &Rt = |
| 2053 | static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime()); |
| 2054 | if (GlobalizedRD) { |
| 2055 | auto I = Rt.FunctionGlobalizedDecls.try_emplace(CGF.CurFn).first; |
| 2056 | I->getSecond().GlobalRecord = GlobalizedRD; |
| 2057 | I->getSecond().MappedParams = |
| 2058 | llvm::make_unique<CodeGenFunction::OMPMapVars>(); |
| 2059 | DeclToAddrMapTy &Data = I->getSecond().LocalVarData; |
| 2060 | for (const auto &Pair : MappedDeclsFields) { |
| 2061 | assert(Pair.getFirst()->isCanonicalDecl() && |
| 2062 | "Expected canonical declaration"); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2063 | Data.insert(std::make_pair(Pair.getFirst(), |
| 2064 | MappedVarData(Pair.getSecond(), |
| 2065 | /*IsOnePerTeam=*/true))); |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | Rt.emitGenericVarsProlog(CGF, Loc); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2069 | } |
| 2070 | void Exit(CodeGenFunction &CGF) override { |
| 2071 | static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime()) |
| 2072 | .emitGenericVarsEpilog(CGF); |
| 2073 | } |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2074 | } Action(Loc, GlobalizedRD, MappedDeclsFields); |
| 2075 | CodeGen.setAction(Action); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2076 | llvm::Value *OutlinedFunVal = CGOpenMPRuntime::emitTeamsOutlinedFunction( |
| 2077 | D, ThreadIDVar, InnermostKind, CodeGen); |
| 2078 | llvm::Function *OutlinedFun = cast<llvm::Function>(OutlinedFunVal); |
| 2079 | OutlinedFun->removeFnAttr(llvm::Attribute::NoInline); |
Mehdi Amini | 6aa9e9b | 2017-05-29 05:38:20 +0000 | [diff] [blame] | 2080 | OutlinedFun->removeFnAttr(llvm::Attribute::OptimizeNone); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 2081 | OutlinedFun->addFnAttr(llvm::Attribute::AlwaysInline); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2082 | |
| 2083 | return OutlinedFun; |
| 2084 | } |
| 2085 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2086 | void CGOpenMPRuntimeNVPTX::emitGenericVarsProlog(CodeGenFunction &CGF, |
Alexey Bataev | bd8ff9b | 2018-08-30 18:56:11 +0000 | [diff] [blame] | 2087 | SourceLocation Loc, |
| 2088 | bool WithSPMDCheck) { |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2089 | if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic && |
| 2090 | getExecutionMode() != CGOpenMPRuntimeNVPTX::EM_SPMD) |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2093 | CGBuilderTy &Bld = CGF.Builder; |
| 2094 | |
| 2095 | const auto I = FunctionGlobalizedDecls.find(CGF.CurFn); |
| 2096 | if (I == FunctionGlobalizedDecls.end()) |
| 2097 | return; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2098 | if (const RecordDecl *GlobalizedVarsRecord = I->getSecond().GlobalRecord) { |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2099 | QualType GlobalRecTy = CGM.getContext().getRecordType(GlobalizedVarsRecord); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2100 | QualType SecGlobalRecTy; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2101 | |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2102 | // Recover pointer to this function's global record. The runtime will |
| 2103 | // handle the specifics of the allocation of the memory. |
| 2104 | // Use actual memory size of the record including the padding |
| 2105 | // for alignment purposes. |
| 2106 | unsigned Alignment = |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2107 | CGM.getContext().getTypeAlignInChars(GlobalRecTy).getQuantity(); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2108 | unsigned GlobalRecordSize = |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2109 | CGM.getContext().getTypeSizeInChars(GlobalRecTy).getQuantity(); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2110 | GlobalRecordSize = llvm::alignTo(GlobalRecordSize, Alignment); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2111 | |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2112 | llvm::PointerType *GlobalRecPtrTy = |
| 2113 | CGF.ConvertTypeForMem(GlobalRecTy)->getPointerTo(); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2114 | llvm::Value *GlobalRecCastAddr; |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2115 | llvm::Value *IsTTD = nullptr; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2116 | if (!IsInTTDRegion && |
| 2117 | (WithSPMDCheck || |
| 2118 | getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_Unknown)) { |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2119 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit"); |
| 2120 | llvm::BasicBlock *SPMDBB = CGF.createBasicBlock(".spmd"); |
| 2121 | llvm::BasicBlock *NonSPMDBB = CGF.createBasicBlock(".non-spmd"); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2122 | if (I->getSecond().SecondaryGlobalRecord.hasValue()) { |
| 2123 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| 2124 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2125 | llvm::Value *PL = CGF.EmitRuntimeCall( |
| 2126 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_level), |
| 2127 | {RTLoc, ThreadID}); |
| 2128 | IsTTD = Bld.CreateIsNull(PL); |
| 2129 | } |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2130 | llvm::Value *IsSPMD = Bld.CreateIsNotNull(CGF.EmitNounwindRuntimeCall( |
| 2131 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_is_spmd_exec_mode))); |
| 2132 | Bld.CreateCondBr(IsSPMD, SPMDBB, NonSPMDBB); |
| 2133 | // There is no need to emit line number for unconditional branch. |
| 2134 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2135 | CGF.EmitBlock(SPMDBB); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2136 | Address RecPtr = Address(llvm::ConstantPointerNull::get(GlobalRecPtrTy), |
| 2137 | CharUnits::fromQuantity(Alignment)); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2138 | CGF.EmitBranch(ExitBB); |
| 2139 | // There is no need to emit line number for unconditional branch. |
| 2140 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2141 | CGF.EmitBlock(NonSPMDBB); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2142 | llvm::Value *Size = llvm::ConstantInt::get(CGM.SizeTy, GlobalRecordSize); |
| 2143 | if (const RecordDecl *SecGlobalizedVarsRecord = |
| 2144 | I->getSecond().SecondaryGlobalRecord.getValueOr(nullptr)) { |
| 2145 | SecGlobalRecTy = |
| 2146 | CGM.getContext().getRecordType(SecGlobalizedVarsRecord); |
| 2147 | |
| 2148 | // Recover pointer to this function's global record. The runtime will |
| 2149 | // handle the specifics of the allocation of the memory. |
| 2150 | // Use actual memory size of the record including the padding |
| 2151 | // for alignment purposes. |
| 2152 | unsigned Alignment = |
| 2153 | CGM.getContext().getTypeAlignInChars(SecGlobalRecTy).getQuantity(); |
| 2154 | unsigned GlobalRecordSize = |
| 2155 | CGM.getContext().getTypeSizeInChars(SecGlobalRecTy).getQuantity(); |
| 2156 | GlobalRecordSize = llvm::alignTo(GlobalRecordSize, Alignment); |
| 2157 | Size = Bld.CreateSelect( |
| 2158 | IsTTD, llvm::ConstantInt::get(CGM.SizeTy, GlobalRecordSize), Size); |
| 2159 | } |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2160 | // TODO: allow the usage of shared memory to be controlled by |
| 2161 | // the user, for now, default to global. |
| 2162 | llvm::Value *GlobalRecordSizeArg[] = { |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2163 | Size, CGF.Builder.getInt16(/*UseSharedMemory=*/0)}; |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 2164 | llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall( |
| 2165 | createNVPTXRuntimeFunction( |
| 2166 | OMPRTL_NVPTX__kmpc_data_sharing_coalesced_push_stack), |
| 2167 | GlobalRecordSizeArg); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2168 | GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2169 | GlobalRecValue, GlobalRecPtrTy); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2170 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2171 | auto *Phi = Bld.CreatePHI(GlobalRecPtrTy, |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2172 | /*NumReservedValues=*/2, "_select_stack"); |
| 2173 | Phi->addIncoming(RecPtr.getPointer(), SPMDBB); |
| 2174 | Phi->addIncoming(GlobalRecCastAddr, NonSPMDBB); |
| 2175 | GlobalRecCastAddr = Phi; |
| 2176 | I->getSecond().GlobalRecordAddr = Phi; |
| 2177 | I->getSecond().IsInSPMDModeFlag = IsSPMD; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2178 | } else if (IsInTTDRegion) { |
| 2179 | assert(GlobalizedRecords.back().Records.size() < 2 && |
| 2180 | "Expected less than 2 globalized records: one for target and one " |
| 2181 | "for teams."); |
| 2182 | unsigned Offset = 0; |
| 2183 | for (const RecordDecl *RD : GlobalizedRecords.back().Records) { |
| 2184 | QualType RDTy = CGM.getContext().getRecordType(RD); |
| 2185 | unsigned Alignment = |
| 2186 | CGM.getContext().getTypeAlignInChars(RDTy).getQuantity(); |
| 2187 | unsigned Size = CGM.getContext().getTypeSizeInChars(RDTy).getQuantity(); |
| 2188 | Offset = |
| 2189 | llvm::alignTo(llvm::alignTo(Offset, Alignment) + Size, Alignment); |
| 2190 | } |
| 2191 | unsigned Alignment = |
| 2192 | CGM.getContext().getTypeAlignInChars(GlobalRecTy).getQuantity(); |
| 2193 | Offset = llvm::alignTo(Offset, Alignment); |
| 2194 | GlobalizedRecords.back().Records.push_back(GlobalizedVarsRecord); |
| 2195 | ++GlobalizedRecords.back().RegionCounter; |
| 2196 | if (GlobalizedRecords.back().Records.size() == 1) { |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2197 | assert(KernelStaticGlobalized && |
| 2198 | "Kernel static pointer must be initialized already."); |
| 2199 | auto *UseSharedMemory = new llvm::GlobalVariable( |
| 2200 | CGM.getModule(), CGM.Int16Ty, /*isConstant=*/true, |
| 2201 | llvm::GlobalValue::InternalLinkage, nullptr, |
| 2202 | "_openmp_static_kernel$is_shared"); |
| 2203 | UseSharedMemory->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 2204 | QualType Int16Ty = CGM.getContext().getIntTypeForBitwidth( |
| 2205 | /*DestWidth=*/16, /*Signed=*/0); |
| 2206 | llvm::Value *IsInSharedMemory = CGF.EmitLoadOfScalar( |
| 2207 | Address(UseSharedMemory, |
| 2208 | CGM.getContext().getTypeAlignInChars(Int16Ty)), |
| 2209 | /*Volatile=*/false, Int16Ty, Loc); |
| 2210 | auto *StaticGlobalized = new llvm::GlobalVariable( |
| 2211 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2212 | llvm::GlobalValue::CommonLinkage, nullptr); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2213 | auto *RecSize = new llvm::GlobalVariable( |
| 2214 | CGM.getModule(), CGM.SizeTy, /*isConstant=*/true, |
| 2215 | llvm::GlobalValue::InternalLinkage, nullptr, |
| 2216 | "_openmp_static_kernel$size"); |
| 2217 | RecSize->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 2218 | llvm::Value *Ld = CGF.EmitLoadOfScalar( |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2219 | Address(RecSize, CGM.getSizeAlign()), /*Volatile=*/false, |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2220 | CGM.getContext().getSizeType(), Loc); |
| 2221 | llvm::Value *ResAddr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2222 | KernelStaticGlobalized, CGM.VoidPtrPtrTy); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2223 | llvm::Value *GlobalRecordSizeArg[] = {StaticGlobalized, Ld, |
| 2224 | IsInSharedMemory, ResAddr}; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2225 | CGF.EmitRuntimeCall(createNVPTXRuntimeFunction( |
| 2226 | OMPRTL_NVPTX__kmpc_get_team_static_memory), |
| 2227 | GlobalRecordSizeArg); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2228 | GlobalizedRecords.back().Buffer = StaticGlobalized; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2229 | GlobalizedRecords.back().RecSize = RecSize; |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2230 | GlobalizedRecords.back().UseSharedMemory = UseSharedMemory; |
| 2231 | GlobalizedRecords.back().Loc = Loc; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2232 | } |
| 2233 | assert(KernelStaticGlobalized && "Global address must be set already."); |
| 2234 | Address FrameAddr = CGF.EmitLoadOfPointer( |
| 2235 | Address(KernelStaticGlobalized, CGM.getPointerAlign()), |
| 2236 | CGM.getContext() |
| 2237 | .getPointerType(CGM.getContext().VoidPtrTy) |
| 2238 | .castAs<PointerType>()); |
| 2239 | llvm::Value *GlobalRecValue = |
| 2240 | Bld.CreateConstInBoundsGEP(FrameAddr, Offset, CharUnits::One()) |
| 2241 | .getPointer(); |
| 2242 | I->getSecond().GlobalRecordAddr = GlobalRecValue; |
| 2243 | I->getSecond().IsInSPMDModeFlag = nullptr; |
| 2244 | GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2245 | GlobalRecValue, CGF.ConvertTypeForMem(GlobalRecTy)->getPointerTo()); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2246 | } else { |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2247 | // TODO: allow the usage of shared memory to be controlled by |
| 2248 | // the user, for now, default to global. |
| 2249 | llvm::Value *GlobalRecordSizeArg[] = { |
| 2250 | llvm::ConstantInt::get(CGM.SizeTy, GlobalRecordSize), |
| 2251 | CGF.Builder.getInt16(/*UseSharedMemory=*/0)}; |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 2252 | llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall( |
| 2253 | createNVPTXRuntimeFunction( |
| 2254 | OMPRTL_NVPTX__kmpc_data_sharing_coalesced_push_stack), |
| 2255 | GlobalRecordSizeArg); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2256 | GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2257 | GlobalRecValue, GlobalRecPtrTy); |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2258 | I->getSecond().GlobalRecordAddr = GlobalRecValue; |
| 2259 | I->getSecond().IsInSPMDModeFlag = nullptr; |
| 2260 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2261 | LValue Base = |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2262 | CGF.MakeNaturalAlignPointeeAddrLValue(GlobalRecCastAddr, GlobalRecTy); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2263 | |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2264 | // Emit the "global alloca" which is a GEP from the global declaration |
| 2265 | // record using the pointer returned by the runtime. |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2266 | LValue SecBase; |
| 2267 | decltype(I->getSecond().LocalVarData)::const_iterator SecIt; |
| 2268 | if (IsTTD) { |
| 2269 | SecIt = I->getSecond().SecondaryLocalVarData->begin(); |
| 2270 | llvm::PointerType *SecGlobalRecPtrTy = |
| 2271 | CGF.ConvertTypeForMem(SecGlobalRecTy)->getPointerTo(); |
| 2272 | SecBase = CGF.MakeNaturalAlignPointeeAddrLValue( |
| 2273 | Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2274 | I->getSecond().GlobalRecordAddr, SecGlobalRecPtrTy), |
| 2275 | SecGlobalRecTy); |
| 2276 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2277 | for (auto &Rec : I->getSecond().LocalVarData) { |
| 2278 | bool EscapedParam = I->getSecond().EscapedParameters.count(Rec.first); |
| 2279 | llvm::Value *ParValue; |
| 2280 | if (EscapedParam) { |
| 2281 | const auto *VD = cast<VarDecl>(Rec.first); |
| 2282 | LValue ParLVal = |
| 2283 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
| 2284 | ParValue = CGF.EmitLoadOfScalar(ParLVal, Loc); |
| 2285 | } |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2286 | LValue VarAddr = CGF.EmitLValueForField(Base, Rec.second.FD); |
| 2287 | // Emit VarAddr basing on lane-id if required. |
| 2288 | QualType VarTy; |
| 2289 | if (Rec.second.IsOnePerTeam) { |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2290 | VarTy = Rec.second.FD->getType(); |
| 2291 | } else { |
| 2292 | llvm::Value *Ptr = CGF.Builder.CreateInBoundsGEP( |
| 2293 | VarAddr.getAddress().getPointer(), |
| 2294 | {Bld.getInt32(0), getNVPTXLaneID(CGF)}); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2295 | VarTy = |
| 2296 | Rec.second.FD->getType()->castAsArrayTypeUnsafe()->getElementType(); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2297 | VarAddr = CGF.MakeAddrLValue( |
| 2298 | Address(Ptr, CGM.getContext().getDeclAlign(Rec.first)), VarTy, |
| 2299 | AlignmentSource::Decl); |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2300 | } |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2301 | Rec.second.PrivateAddr = VarAddr.getAddress(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2302 | if (!IsInTTDRegion && |
| 2303 | (WithSPMDCheck || |
| 2304 | getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_Unknown)) { |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2305 | assert(I->getSecond().IsInSPMDModeFlag && |
| 2306 | "Expected unknown execution mode or required SPMD check."); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 2307 | if (IsTTD) { |
| 2308 | assert(SecIt->second.IsOnePerTeam && |
| 2309 | "Secondary glob data must be one per team."); |
| 2310 | LValue SecVarAddr = CGF.EmitLValueForField(SecBase, SecIt->second.FD); |
| 2311 | VarAddr.setAddress( |
| 2312 | Address(Bld.CreateSelect(IsTTD, SecVarAddr.getPointer(), |
| 2313 | VarAddr.getPointer()), |
| 2314 | VarAddr.getAlignment())); |
| 2315 | Rec.second.PrivateAddr = VarAddr.getAddress(); |
| 2316 | } |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 2317 | Address GlobalPtr = Rec.second.PrivateAddr; |
| 2318 | Address LocalAddr = CGF.CreateMemTemp(VarTy, Rec.second.FD->getName()); |
| 2319 | Rec.second.PrivateAddr = Address( |
| 2320 | Bld.CreateSelect(I->getSecond().IsInSPMDModeFlag, |
| 2321 | LocalAddr.getPointer(), GlobalPtr.getPointer()), |
| 2322 | LocalAddr.getAlignment()); |
| 2323 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2324 | if (EscapedParam) { |
| 2325 | const auto *VD = cast<VarDecl>(Rec.first); |
| 2326 | CGF.EmitStoreOfScalar(ParValue, VarAddr); |
| 2327 | I->getSecond().MappedParams->setVarAddr(CGF, VD, VarAddr.getAddress()); |
| 2328 | } |
Alexey Bataev | 93a38d6 | 2018-10-16 00:09:06 +0000 | [diff] [blame] | 2329 | if (IsTTD) |
| 2330 | ++SecIt; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2331 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2332 | } |
| 2333 | for (const ValueDecl *VD : I->getSecond().EscapedVariableLengthDecls) { |
| 2334 | // Recover pointer to this function's global record. The runtime will |
| 2335 | // handle the specifics of the allocation of the memory. |
| 2336 | // Use actual memory size of the record including the padding |
| 2337 | // for alignment purposes. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2338 | CGBuilderTy &Bld = CGF.Builder; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2339 | llvm::Value *Size = CGF.getTypeSize(VD->getType()); |
| 2340 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 2341 | Size = Bld.CreateNUWAdd( |
| 2342 | Size, llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity() - 1)); |
| 2343 | llvm::Value *AlignVal = |
| 2344 | llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity()); |
| 2345 | Size = Bld.CreateUDiv(Size, AlignVal); |
| 2346 | Size = Bld.CreateNUWMul(Size, AlignVal); |
| 2347 | // TODO: allow the usage of shared memory to be controlled by |
| 2348 | // the user, for now, default to global. |
| 2349 | llvm::Value *GlobalRecordSizeArg[] = { |
| 2350 | Size, CGF.Builder.getInt16(/*UseSharedMemory=*/0)}; |
| 2351 | llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall( |
Alexey Bataev | 1fc1f8e | 2018-11-02 16:08:31 +0000 | [diff] [blame] | 2352 | createNVPTXRuntimeFunction( |
| 2353 | OMPRTL_NVPTX__kmpc_data_sharing_coalesced_push_stack), |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2354 | GlobalRecordSizeArg); |
| 2355 | llvm::Value *GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2356 | GlobalRecValue, CGF.ConvertTypeForMem(VD->getType())->getPointerTo()); |
| 2357 | LValue Base = CGF.MakeAddrLValue(GlobalRecCastAddr, VD->getType(), |
| 2358 | CGM.getContext().getDeclAlign(VD), |
| 2359 | AlignmentSource::Decl); |
| 2360 | I->getSecond().MappedParams->setVarAddr(CGF, cast<VarDecl>(VD), |
| 2361 | Base.getAddress()); |
| 2362 | I->getSecond().EscapedVariableLengthDeclsAddrs.emplace_back(GlobalRecValue); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2363 | } |
| 2364 | I->getSecond().MappedParams->apply(CGF); |
| 2365 | } |
| 2366 | |
Alexey Bataev | bd8ff9b | 2018-08-30 18:56:11 +0000 | [diff] [blame] | 2367 | void CGOpenMPRuntimeNVPTX::emitGenericVarsEpilog(CodeGenFunction &CGF, |
| 2368 | bool WithSPMDCheck) { |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 2369 | if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic && |
| 2370 | getExecutionMode() != CGOpenMPRuntimeNVPTX::EM_SPMD) |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2371 | return; |
| 2372 | |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2373 | const auto I = FunctionGlobalizedDecls.find(CGF.CurFn); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2374 | if (I != FunctionGlobalizedDecls.end()) { |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2375 | I->getSecond().MappedParams->restore(CGF); |
| 2376 | if (!CGF.HaveInsertPoint()) |
| 2377 | return; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2378 | for (llvm::Value *Addr : |
| 2379 | llvm::reverse(I->getSecond().EscapedVariableLengthDeclsAddrs)) { |
| 2380 | CGF.EmitRuntimeCall( |
| 2381 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_pop_stack), |
| 2382 | Addr); |
| 2383 | } |
| 2384 | if (I->getSecond().GlobalRecordAddr) { |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2385 | if (!IsInTTDRegion && |
| 2386 | (WithSPMDCheck || |
| 2387 | getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_Unknown)) { |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2388 | CGBuilderTy &Bld = CGF.Builder; |
| 2389 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit"); |
| 2390 | llvm::BasicBlock *NonSPMDBB = CGF.createBasicBlock(".non-spmd"); |
| 2391 | Bld.CreateCondBr(I->getSecond().IsInSPMDModeFlag, ExitBB, NonSPMDBB); |
| 2392 | // There is no need to emit line number for unconditional branch. |
| 2393 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2394 | CGF.EmitBlock(NonSPMDBB); |
| 2395 | CGF.EmitRuntimeCall( |
| 2396 | createNVPTXRuntimeFunction( |
| 2397 | OMPRTL_NVPTX__kmpc_data_sharing_pop_stack), |
| 2398 | CGF.EmitCastToVoidPtr(I->getSecond().GlobalRecordAddr)); |
| 2399 | CGF.EmitBlock(ExitBB); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2400 | } else if (IsInTTDRegion) { |
| 2401 | assert(GlobalizedRecords.back().RegionCounter > 0 && |
| 2402 | "region counter must be > 0."); |
| 2403 | --GlobalizedRecords.back().RegionCounter; |
| 2404 | // Emit the restore function only in the target region. |
| 2405 | if (GlobalizedRecords.back().RegionCounter == 0) { |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2406 | QualType Int16Ty = CGM.getContext().getIntTypeForBitwidth( |
| 2407 | /*DestWidth=*/16, /*Signed=*/0); |
| 2408 | llvm::Value *IsInSharedMemory = CGF.EmitLoadOfScalar( |
| 2409 | Address(GlobalizedRecords.back().UseSharedMemory, |
| 2410 | CGM.getContext().getTypeAlignInChars(Int16Ty)), |
| 2411 | /*Volatile=*/false, Int16Ty, GlobalizedRecords.back().Loc); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2412 | CGF.EmitRuntimeCall( |
| 2413 | createNVPTXRuntimeFunction( |
| 2414 | OMPRTL_NVPTX__kmpc_restore_team_static_memory), |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 2415 | IsInSharedMemory); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 2416 | } |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2417 | } else { |
Alexey Bataev | 8d8e123 | 2018-08-29 18:32:21 +0000 | [diff] [blame] | 2418 | CGF.EmitRuntimeCall(createNVPTXRuntimeFunction( |
| 2419 | OMPRTL_NVPTX__kmpc_data_sharing_pop_stack), |
| 2420 | I->getSecond().GlobalRecordAddr); |
| 2421 | } |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 2422 | } |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2426 | void CGOpenMPRuntimeNVPTX::emitTeamsCall(CodeGenFunction &CGF, |
| 2427 | const OMPExecutableDirective &D, |
| 2428 | SourceLocation Loc, |
| 2429 | llvm::Value *OutlinedFn, |
| 2430 | ArrayRef<llvm::Value *> CapturedVars) { |
| 2431 | if (!CGF.HaveInsertPoint()) |
| 2432 | return; |
| 2433 | |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 2434 | Address ZeroAddr = CGF.CreateMemTemp( |
| 2435 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2436 | /*Name*/ ".zero.addr"); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2437 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
| 2438 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 2439 | OutlinedFnArgs.push_back(emitThreadIDAddress(CGF, Loc).getPointer()); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2440 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2441 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2442 | emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 2443 | } |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2444 | |
| 2445 | void CGOpenMPRuntimeNVPTX::emitParallelCall( |
| 2446 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 2447 | ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) { |
| 2448 | if (!CGF.HaveInsertPoint()) |
| 2449 | return; |
| 2450 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2451 | if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 2452 | emitSPMDParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond); |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 2453 | else |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2454 | emitNonSPMDParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2457 | void CGOpenMPRuntimeNVPTX::emitNonSPMDParallelCall( |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2458 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 2459 | ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) { |
| 2460 | llvm::Function *Fn = cast<llvm::Function>(OutlinedFn); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Force inline this outlined function at its call site. |
| 2463 | Fn->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2464 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2465 | Address ZeroAddr = CGF.CreateMemTemp(CGF.getContext().getIntTypeForBitwidth( |
| 2466 | /*DestWidth=*/32, /*Signed=*/1), |
| 2467 | ".zero.addr"); |
| 2468 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2469 | // ThreadId for serialized parallels is 0. |
| 2470 | Address ThreadIDAddr = ZeroAddr; |
| 2471 | auto &&CodeGen = [this, Fn, CapturedVars, Loc, ZeroAddr, &ThreadIDAddr]( |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2472 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2473 | Action.Enter(CGF); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2474 | |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2475 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| 2476 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| 2477 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2478 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2479 | emitOutlinedFunctionCall(CGF, Loc, Fn, OutlinedFnArgs); |
| 2480 | }; |
| 2481 | auto &&SeqGen = [this, &CodeGen, Loc](CodeGenFunction &CGF, |
| 2482 | PrePostActionTy &) { |
| 2483 | |
| 2484 | RegionCodeGenTy RCG(CodeGen); |
| 2485 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| 2486 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2487 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
| 2488 | |
| 2489 | NVPTXActionTy Action( |
| 2490 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel), |
| 2491 | Args, |
| 2492 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel), |
| 2493 | Args); |
| 2494 | RCG.setAction(Action); |
| 2495 | RCG(CGF); |
| 2496 | }; |
| 2497 | |
| 2498 | auto &&L0ParallelGen = [this, CapturedVars, Fn](CodeGenFunction &CGF, |
| 2499 | PrePostActionTy &Action) { |
| 2500 | CGBuilderTy &Bld = CGF.Builder; |
| 2501 | llvm::Function *WFn = WrapperFunctionsMap[Fn]; |
| 2502 | assert(WFn && "Wrapper function does not exist!"); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2503 | llvm::Value *ID = Bld.CreateBitOrPointerCast(WFn, CGM.Int8PtrTy); |
| 2504 | |
| 2505 | // Prepare for parallel region. Indicate the outlined function. |
| 2506 | llvm::Value *Args[] = {ID, /*RequiresOMPRuntime=*/Bld.getInt16(1)}; |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2507 | CGF.EmitRuntimeCall( |
| 2508 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_prepare_parallel), |
| 2509 | Args); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2510 | |
| 2511 | // Create a private scope that will globalize the arguments |
| 2512 | // passed from the outside of the target region. |
| 2513 | CodeGenFunction::OMPPrivateScope PrivateArgScope(CGF); |
| 2514 | |
| 2515 | // There's somehting to share. |
| 2516 | if (!CapturedVars.empty()) { |
| 2517 | // Prepare for parallel region. Indicate the outlined function. |
| 2518 | Address SharedArgs = |
| 2519 | CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "shared_arg_refs"); |
| 2520 | llvm::Value *SharedArgsPtr = SharedArgs.getPointer(); |
| 2521 | |
| 2522 | llvm::Value *DataSharingArgs[] = { |
| 2523 | SharedArgsPtr, |
| 2524 | llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())}; |
| 2525 | CGF.EmitRuntimeCall(createNVPTXRuntimeFunction( |
| 2526 | OMPRTL_NVPTX__kmpc_begin_sharing_variables), |
| 2527 | DataSharingArgs); |
| 2528 | |
| 2529 | // Store variable address in a list of references to pass to workers. |
| 2530 | unsigned Idx = 0; |
| 2531 | ASTContext &Ctx = CGF.getContext(); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2532 | Address SharedArgListAddress = CGF.EmitLoadOfPointer( |
| 2533 | SharedArgs, Ctx.getPointerType(Ctx.getPointerType(Ctx.VoidPtrTy)) |
| 2534 | .castAs<PointerType>()); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2535 | for (llvm::Value *V : CapturedVars) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2536 | Address Dst = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx, |
| 2537 | CGF.getPointerSize()); |
| 2538 | llvm::Value *PtrV; |
Alexey Bataev | 1731421 | 2018-03-20 15:41:05 +0000 | [diff] [blame] | 2539 | if (V->getType()->isIntegerTy()) |
| 2540 | PtrV = Bld.CreateIntToPtr(V, CGF.VoidPtrTy); |
| 2541 | else |
| 2542 | PtrV = Bld.CreatePointerBitCastOrAddrSpaceCast(V, CGF.VoidPtrTy); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2543 | CGF.EmitStoreOfScalar(PtrV, Dst, /*Volatile=*/false, |
| 2544 | Ctx.getPointerType(Ctx.VoidPtrTy)); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2545 | ++Idx; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2548 | |
| 2549 | // Activate workers. This barrier is used by the master to signal |
| 2550 | // work for the workers. |
| 2551 | syncCTAThreads(CGF); |
| 2552 | |
| 2553 | // OpenMP [2.5, Parallel Construct, p.49] |
| 2554 | // There is an implied barrier at the end of a parallel region. After the |
| 2555 | // end of a parallel region, only the master thread of the team resumes |
| 2556 | // execution of the enclosing task region. |
| 2557 | // |
| 2558 | // The master waits at this barrier until all workers are done. |
| 2559 | syncCTAThreads(CGF); |
| 2560 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2561 | if (!CapturedVars.empty()) |
| 2562 | CGF.EmitRuntimeCall( |
| 2563 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_sharing_variables)); |
| 2564 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2565 | // Remember for post-processing in worker loop. |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 2566 | Work.emplace_back(WFn); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2567 | }; |
| 2568 | |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2569 | auto &&LNParallelGen = [this, Loc, &SeqGen, &L0ParallelGen]( |
| 2570 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2571 | if (IsInParallelRegion) { |
| 2572 | SeqGen(CGF, Action); |
| 2573 | } else if (IsInTargetMasterThreadRegion) { |
| 2574 | L0ParallelGen(CGF, Action); |
| 2575 | } else { |
| 2576 | // Check for master and then parallelism: |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2577 | // if (__kmpc_is_spmd_exec_mode() || __kmpc_parallel_level(loc, gtid)) { |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2578 | // Serialized execution. |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2579 | // } else { |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2580 | // Worker call. |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2581 | // } |
| 2582 | CGBuilderTy &Bld = CGF.Builder; |
| 2583 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit"); |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2584 | llvm::BasicBlock *SeqBB = CGF.createBasicBlock(".sequential"); |
| 2585 | llvm::BasicBlock *ParallelCheckBB = CGF.createBasicBlock(".parcheck"); |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2586 | llvm::BasicBlock *MasterBB = CGF.createBasicBlock(".master"); |
Alexey Bataev | 673110d | 2018-05-16 13:36:30 +0000 | [diff] [blame] | 2587 | llvm::Value *IsSPMD = Bld.CreateIsNotNull(CGF.EmitNounwindRuntimeCall( |
| 2588 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_is_spmd_exec_mode))); |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2589 | Bld.CreateCondBr(IsSPMD, SeqBB, ParallelCheckBB); |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2590 | // There is no need to emit line number for unconditional branch. |
| 2591 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 2592 | CGF.EmitBlock(ParallelCheckBB); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2593 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| 2594 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2595 | llvm::Value *PL = CGF.EmitRuntimeCall( |
| 2596 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_level), |
| 2597 | {RTLoc, ThreadID}); |
| 2598 | llvm::Value *Res = Bld.CreateIsNotNull(PL); |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2599 | Bld.CreateCondBr(Res, SeqBB, MasterBB); |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2600 | CGF.EmitBlock(SeqBB); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2601 | SeqGen(CGF, Action); |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2602 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2603 | // There is no need to emit line number for unconditional branch. |
| 2604 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
Jonas Hahnfeld | 3ca4701 | 2018-10-02 19:12:54 +0000 | [diff] [blame] | 2605 | CGF.EmitBlock(MasterBB); |
Alexey Bataev | 0baba9e | 2018-05-25 20:16:03 +0000 | [diff] [blame] | 2606 | L0ParallelGen(CGF, Action); |
| 2607 | CGF.EmitBranch(ExitBB); |
| 2608 | // There is no need to emit line number for unconditional branch. |
| 2609 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2610 | // Emit the continuation block for code after the if. |
| 2611 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
| 2612 | } |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2613 | }; |
| 2614 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2615 | if (IfCond) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2616 | emitOMPIfClause(CGF, IfCond, LNParallelGen, SeqGen); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2617 | } else { |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2618 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 2619 | RegionCodeGenTy ThenRCG(LNParallelGen); |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 2620 | ThenRCG(CGF); |
| 2621 | } |
| 2622 | } |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 2623 | |
Alexey Bataev | 4065b9a | 2018-06-21 20:26:33 +0000 | [diff] [blame] | 2624 | void CGOpenMPRuntimeNVPTX::emitSPMDParallelCall( |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 2625 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
| 2626 | ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) { |
| 2627 | // Just call the outlined function to execute the parallel region. |
| 2628 | // OutlinedFn(>id, &zero, CapturedStruct); |
| 2629 | // |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 2630 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 2631 | |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2632 | Address ZeroAddr = CGF.CreateMemTemp(CGF.getContext().getIntTypeForBitwidth( |
| 2633 | /*DestWidth=*/32, /*Signed=*/1), |
| 2634 | ".zero.addr"); |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 2635 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2636 | // ThreadId for serialized parallels is 0. |
| 2637 | Address ThreadIDAddr = ZeroAddr; |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2638 | auto &&CodeGen = [this, OutlinedFn, CapturedVars, Loc, ZeroAddr, |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2639 | &ThreadIDAddr](CodeGenFunction &CGF, |
| 2640 | PrePostActionTy &Action) { |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2641 | Action.Enter(CGF); |
| 2642 | |
| 2643 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| 2644 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| 2645 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 2646 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 2647 | emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs); |
| 2648 | }; |
| 2649 | auto &&SeqGen = [this, &CodeGen, Loc](CodeGenFunction &CGF, |
| 2650 | PrePostActionTy &) { |
| 2651 | |
| 2652 | RegionCodeGenTy RCG(CodeGen); |
| 2653 | llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc); |
| 2654 | llvm::Value *ThreadID = getThreadID(CGF, Loc); |
| 2655 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
| 2656 | |
| 2657 | NVPTXActionTy Action( |
| 2658 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel), |
| 2659 | Args, |
| 2660 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel), |
| 2661 | Args); |
| 2662 | RCG.setAction(Action); |
| 2663 | RCG(CGF); |
| 2664 | }; |
| 2665 | |
| 2666 | if (IsInTargetMasterThreadRegion) { |
Alexey Bataev | 8521ff6 | 2018-07-25 20:03:01 +0000 | [diff] [blame] | 2667 | // In the worker need to use the real thread id. |
| 2668 | ThreadIDAddr = emitThreadIDAddress(CGF, Loc); |
Alexey Bataev | bf5c848 | 2018-05-10 18:32:08 +0000 | [diff] [blame] | 2669 | RegionCodeGenTy RCG(CodeGen); |
| 2670 | RCG(CGF); |
| 2671 | } else { |
| 2672 | // If we are not in the target region, it is definitely L2 parallelism or |
| 2673 | // more, because for SPMD mode we always has L1 parallel level, sowe don't |
| 2674 | // need to check for orphaned directives. |
| 2675 | RegionCodeGenTy RCG(SeqGen); |
| 2676 | RCG(CGF); |
| 2677 | } |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 2678 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2679 | |
Alexey Bataev | 504fc2d | 2018-05-07 17:23:05 +0000 | [diff] [blame] | 2680 | void CGOpenMPRuntimeNVPTX::emitCriticalRegion( |
| 2681 | CodeGenFunction &CGF, StringRef CriticalName, |
| 2682 | const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, |
| 2683 | const Expr *Hint) { |
| 2684 | llvm::BasicBlock *LoopBB = CGF.createBasicBlock("omp.critical.loop"); |
| 2685 | llvm::BasicBlock *TestBB = CGF.createBasicBlock("omp.critical.test"); |
| 2686 | llvm::BasicBlock *SyncBB = CGF.createBasicBlock("omp.critical.sync"); |
| 2687 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.critical.body"); |
| 2688 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock("omp.critical.exit"); |
| 2689 | |
| 2690 | // Fetch team-local id of the thread. |
| 2691 | llvm::Value *ThreadID = getNVPTXThreadID(CGF); |
| 2692 | |
| 2693 | // Get the width of the team. |
| 2694 | llvm::Value *TeamWidth = getNVPTXNumThreads(CGF); |
| 2695 | |
| 2696 | // Initialize the counter variable for the loop. |
| 2697 | QualType Int32Ty = |
| 2698 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/0); |
| 2699 | Address Counter = CGF.CreateMemTemp(Int32Ty, "critical_counter"); |
| 2700 | LValue CounterLVal = CGF.MakeAddrLValue(Counter, Int32Ty); |
| 2701 | CGF.EmitStoreOfScalar(llvm::Constant::getNullValue(CGM.Int32Ty), CounterLVal, |
| 2702 | /*isInit=*/true); |
| 2703 | |
| 2704 | // Block checks if loop counter exceeds upper bound. |
| 2705 | CGF.EmitBlock(LoopBB); |
| 2706 | llvm::Value *CounterVal = CGF.EmitLoadOfScalar(CounterLVal, Loc); |
| 2707 | llvm::Value *CmpLoopBound = CGF.Builder.CreateICmpSLT(CounterVal, TeamWidth); |
| 2708 | CGF.Builder.CreateCondBr(CmpLoopBound, TestBB, ExitBB); |
| 2709 | |
| 2710 | // Block tests which single thread should execute region, and which threads |
| 2711 | // should go straight to synchronisation point. |
| 2712 | CGF.EmitBlock(TestBB); |
| 2713 | CounterVal = CGF.EmitLoadOfScalar(CounterLVal, Loc); |
| 2714 | llvm::Value *CmpThreadToCounter = |
| 2715 | CGF.Builder.CreateICmpEQ(ThreadID, CounterVal); |
| 2716 | CGF.Builder.CreateCondBr(CmpThreadToCounter, BodyBB, SyncBB); |
| 2717 | |
| 2718 | // Block emits the body of the critical region. |
| 2719 | CGF.EmitBlock(BodyBB); |
| 2720 | |
| 2721 | // Output the critical statement. |
| 2722 | CriticalOpGen(CGF); |
| 2723 | |
| 2724 | // After the body surrounded by the critical region, the single executing |
| 2725 | // thread will jump to the synchronisation point. |
| 2726 | // Block waits for all threads in current team to finish then increments the |
| 2727 | // counter variable and returns to the loop. |
| 2728 | CGF.EmitBlock(SyncBB); |
| 2729 | getNVPTXCTABarrier(CGF); |
| 2730 | |
| 2731 | llvm::Value *IncCounterVal = |
| 2732 | CGF.Builder.CreateNSWAdd(CounterVal, CGF.Builder.getInt32(1)); |
| 2733 | CGF.EmitStoreOfScalar(IncCounterVal, CounterLVal); |
| 2734 | CGF.EmitBranch(LoopBB); |
| 2735 | |
| 2736 | // Block that is reached when all threads in the team complete the region. |
| 2737 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
| 2738 | } |
| 2739 | |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2740 | /// Cast value to the specified type. |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2741 | static llvm::Value *castValueToType(CodeGenFunction &CGF, llvm::Value *Val, |
| 2742 | QualType ValTy, QualType CastTy, |
| 2743 | SourceLocation Loc) { |
| 2744 | assert(!CGF.getContext().getTypeSizeInChars(CastTy).isZero() && |
| 2745 | "Cast type must sized."); |
| 2746 | assert(!CGF.getContext().getTypeSizeInChars(ValTy).isZero() && |
| 2747 | "Val type must sized."); |
| 2748 | llvm::Type *LLVMCastTy = CGF.ConvertTypeForMem(CastTy); |
| 2749 | if (ValTy == CastTy) |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2750 | return Val; |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2751 | if (CGF.getContext().getTypeSizeInChars(ValTy) == |
| 2752 | CGF.getContext().getTypeSizeInChars(CastTy)) |
| 2753 | return CGF.Builder.CreateBitCast(Val, LLVMCastTy); |
| 2754 | if (CastTy->isIntegerType() && ValTy->isIntegerType()) |
| 2755 | return CGF.Builder.CreateIntCast(Val, LLVMCastTy, |
| 2756 | CastTy->hasSignedIntegerRepresentation()); |
| 2757 | Address CastItem = CGF.CreateMemTemp(CastTy); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2758 | Address ValCastItem = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2759 | CastItem, Val->getType()->getPointerTo(CastItem.getAddressSpace())); |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2760 | CGF.EmitStoreOfScalar(Val, ValCastItem, /*Volatile=*/false, ValTy); |
| 2761 | return CGF.EmitLoadOfScalar(CastItem, /*Volatile=*/false, CastTy, Loc); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2764 | /// This function creates calls to one of two shuffle functions to copy |
| 2765 | /// variables between lanes in a warp. |
| 2766 | static llvm::Value *createRuntimeShuffleFunction(CodeGenFunction &CGF, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2767 | llvm::Value *Elem, |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2768 | QualType ElemType, |
| 2769 | llvm::Value *Offset, |
| 2770 | SourceLocation Loc) { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2771 | CodeGenModule &CGM = CGF.CGM; |
| 2772 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2773 | CGOpenMPRuntimeNVPTX &RT = |
| 2774 | *(static_cast<CGOpenMPRuntimeNVPTX *>(&CGM.getOpenMPRuntime())); |
| 2775 | |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2776 | CharUnits Size = CGF.getContext().getTypeSizeInChars(ElemType); |
| 2777 | assert(Size.getQuantity() <= 8 && |
| 2778 | "Unsupported bitwidth in shuffle instruction."); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2779 | |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2780 | OpenMPRTLFunctionNVPTX ShuffleFn = Size.getQuantity() <= 4 |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2781 | ? OMPRTL_NVPTX__kmpc_shuffle_int32 |
| 2782 | : OMPRTL_NVPTX__kmpc_shuffle_int64; |
| 2783 | |
| 2784 | // Cast all types to 32- or 64-bit values before calling shuffle routines. |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2785 | QualType CastTy = CGF.getContext().getIntTypeForBitwidth( |
| 2786 | Size.getQuantity() <= 4 ? 32 : 64, /*Signed=*/1); |
| 2787 | llvm::Value *ElemCast = castValueToType(CGF, Elem, ElemType, CastTy, Loc); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2788 | llvm::Value *WarpSize = |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2789 | Bld.CreateIntCast(getNVPTXWarpSize(CGF), CGM.Int16Ty, /*isSigned=*/true); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2790 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2791 | llvm::Value *ShuffledVal = CGF.EmitRuntimeCall( |
| 2792 | RT.createNVPTXRuntimeFunction(ShuffleFn), {ElemCast, Offset, WarpSize}); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2793 | |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 2794 | return castValueToType(CGF, ShuffledVal, CastTy, ElemType, Loc); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 2797 | static void shuffleAndStore(CodeGenFunction &CGF, Address SrcAddr, |
| 2798 | Address DestAddr, QualType ElemType, |
| 2799 | llvm::Value *Offset, SourceLocation Loc) { |
| 2800 | CGBuilderTy &Bld = CGF.Builder; |
| 2801 | |
| 2802 | CharUnits Size = CGF.getContext().getTypeSizeInChars(ElemType); |
| 2803 | // Create the loop over the big sized data. |
| 2804 | // ptr = (void*)Elem; |
| 2805 | // ptrEnd = (void*) Elem + 1; |
| 2806 | // Step = 8; |
| 2807 | // while (ptr + Step < ptrEnd) |
| 2808 | // shuffle((int64_t)*ptr); |
| 2809 | // Step = 4; |
| 2810 | // while (ptr + Step < ptrEnd) |
| 2811 | // shuffle((int32_t)*ptr); |
| 2812 | // ... |
| 2813 | Address ElemPtr = DestAddr; |
| 2814 | Address Ptr = SrcAddr; |
| 2815 | Address PtrEnd = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2816 | Bld.CreateConstGEP(SrcAddr, 1, Size), CGF.VoidPtrTy); |
| 2817 | for (int IntSize = 8; IntSize >= 1; IntSize /= 2) { |
| 2818 | if (Size < CharUnits::fromQuantity(IntSize)) |
| 2819 | continue; |
| 2820 | QualType IntType = CGF.getContext().getIntTypeForBitwidth( |
| 2821 | CGF.getContext().toBits(CharUnits::fromQuantity(IntSize)), |
| 2822 | /*Signed=*/1); |
| 2823 | llvm::Type *IntTy = CGF.ConvertTypeForMem(IntType); |
| 2824 | Ptr = Bld.CreatePointerBitCastOrAddrSpaceCast(Ptr, IntTy->getPointerTo()); |
| 2825 | ElemPtr = |
| 2826 | Bld.CreatePointerBitCastOrAddrSpaceCast(ElemPtr, IntTy->getPointerTo()); |
| 2827 | if (Size.getQuantity() / IntSize > 1) { |
| 2828 | llvm::BasicBlock *PreCondBB = CGF.createBasicBlock(".shuffle.pre_cond"); |
| 2829 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".shuffle.then"); |
| 2830 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".shuffle.exit"); |
| 2831 | llvm::BasicBlock *CurrentBB = Bld.GetInsertBlock(); |
| 2832 | CGF.EmitBlock(PreCondBB); |
| 2833 | llvm::PHINode *PhiSrc = |
| 2834 | Bld.CreatePHI(Ptr.getType(), /*NumReservedValues=*/2); |
| 2835 | PhiSrc->addIncoming(Ptr.getPointer(), CurrentBB); |
| 2836 | llvm::PHINode *PhiDest = |
| 2837 | Bld.CreatePHI(ElemPtr.getType(), /*NumReservedValues=*/2); |
| 2838 | PhiDest->addIncoming(ElemPtr.getPointer(), CurrentBB); |
| 2839 | Ptr = Address(PhiSrc, Ptr.getAlignment()); |
| 2840 | ElemPtr = Address(PhiDest, ElemPtr.getAlignment()); |
| 2841 | llvm::Value *PtrDiff = Bld.CreatePtrDiff( |
| 2842 | PtrEnd.getPointer(), Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 2843 | Ptr.getPointer(), CGF.VoidPtrTy)); |
| 2844 | Bld.CreateCondBr(Bld.CreateICmpSGT(PtrDiff, Bld.getInt64(IntSize - 1)), |
| 2845 | ThenBB, ExitBB); |
| 2846 | CGF.EmitBlock(ThenBB); |
| 2847 | llvm::Value *Res = createRuntimeShuffleFunction( |
| 2848 | CGF, CGF.EmitLoadOfScalar(Ptr, /*Volatile=*/false, IntType, Loc), |
| 2849 | IntType, Offset, Loc); |
| 2850 | CGF.EmitStoreOfScalar(Res, ElemPtr, /*Volatile=*/false, IntType); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2851 | Address LocalPtr = |
| 2852 | Bld.CreateConstGEP(Ptr, 1, CharUnits::fromQuantity(IntSize)); |
| 2853 | Address LocalElemPtr = |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 2854 | Bld.CreateConstGEP(ElemPtr, 1, CharUnits::fromQuantity(IntSize)); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 2855 | PhiSrc->addIncoming(LocalPtr.getPointer(), ThenBB); |
| 2856 | PhiDest->addIncoming(LocalElemPtr.getPointer(), ThenBB); |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 2857 | CGF.EmitBranch(PreCondBB); |
| 2858 | CGF.EmitBlock(ExitBB); |
| 2859 | } else { |
| 2860 | llvm::Value *Res = createRuntimeShuffleFunction( |
| 2861 | CGF, CGF.EmitLoadOfScalar(Ptr, /*Volatile=*/false, IntType, Loc), |
| 2862 | IntType, Offset, Loc); |
| 2863 | CGF.EmitStoreOfScalar(Res, ElemPtr, /*Volatile=*/false, IntType); |
| 2864 | Ptr = Bld.CreateConstGEP(Ptr, 1, CharUnits::fromQuantity(IntSize)); |
| 2865 | ElemPtr = |
| 2866 | Bld.CreateConstGEP(ElemPtr, 1, CharUnits::fromQuantity(IntSize)); |
| 2867 | } |
| 2868 | Size = Size % IntSize; |
| 2869 | } |
| 2870 | } |
| 2871 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2872 | namespace { |
| 2873 | enum CopyAction : unsigned { |
| 2874 | // RemoteLaneToThread: Copy over a Reduce list from a remote lane in |
| 2875 | // the warp using shuffle instructions. |
| 2876 | RemoteLaneToThread, |
| 2877 | // ThreadCopy: Make a copy of a Reduce list on the thread's stack. |
| 2878 | ThreadCopy, |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2879 | // ThreadToScratchpad: Copy a team-reduced array to the scratchpad. |
| 2880 | ThreadToScratchpad, |
| 2881 | // ScratchpadToThread: Copy from a scratchpad array in global memory |
| 2882 | // containing team-reduced data to a thread's stack. |
| 2883 | ScratchpadToThread, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2884 | }; |
| 2885 | } // namespace |
| 2886 | |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2887 | struct CopyOptionsTy { |
| 2888 | llvm::Value *RemoteLaneOffset; |
| 2889 | llvm::Value *ScratchpadIndex; |
| 2890 | llvm::Value *ScratchpadWidth; |
| 2891 | }; |
| 2892 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2893 | /// Emit instructions to copy a Reduce list, which contains partially |
| 2894 | /// aggregated values, in the specified direction. |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2895 | static void emitReductionListCopy( |
| 2896 | CopyAction Action, CodeGenFunction &CGF, QualType ReductionArrayTy, |
| 2897 | ArrayRef<const Expr *> Privates, Address SrcBase, Address DestBase, |
| 2898 | CopyOptionsTy CopyOptions = {nullptr, nullptr, nullptr}) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2899 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2900 | CodeGenModule &CGM = CGF.CGM; |
| 2901 | ASTContext &C = CGM.getContext(); |
| 2902 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2903 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2904 | llvm::Value *RemoteLaneOffset = CopyOptions.RemoteLaneOffset; |
| 2905 | llvm::Value *ScratchpadIndex = CopyOptions.ScratchpadIndex; |
| 2906 | llvm::Value *ScratchpadWidth = CopyOptions.ScratchpadWidth; |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2907 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2908 | // Iterates, element-by-element, through the source Reduce list and |
| 2909 | // make a copy. |
| 2910 | unsigned Idx = 0; |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2911 | unsigned Size = Privates.size(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2912 | for (const Expr *Private : Privates) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2913 | Address SrcElementAddr = Address::invalid(); |
| 2914 | Address DestElementAddr = Address::invalid(); |
| 2915 | Address DestElementPtrAddr = Address::invalid(); |
| 2916 | // Should we shuffle in an element from a remote lane? |
| 2917 | bool ShuffleInElement = false; |
| 2918 | // Set to true to update the pointer in the dest Reduce list to a |
| 2919 | // newly created element. |
| 2920 | bool UpdateDestListPtr = false; |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2921 | // Increment the src or dest pointer to the scratchpad, for each |
| 2922 | // new element. |
| 2923 | bool IncrScratchpadSrc = false; |
| 2924 | bool IncrScratchpadDest = false; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2925 | |
| 2926 | switch (Action) { |
| 2927 | case RemoteLaneToThread: { |
| 2928 | // Step 1.1: Get the address for the src element in the Reduce list. |
| 2929 | Address SrcElementPtrAddr = |
| 2930 | Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize()); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2931 | SrcElementAddr = CGF.EmitLoadOfPointer( |
| 2932 | SrcElementPtrAddr, |
| 2933 | C.getPointerType(Private->getType())->castAs<PointerType>()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2934 | |
| 2935 | // Step 1.2: Create a temporary to store the element in the destination |
| 2936 | // Reduce list. |
| 2937 | DestElementPtrAddr = |
| 2938 | Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize()); |
| 2939 | DestElementAddr = |
| 2940 | CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element"); |
| 2941 | ShuffleInElement = true; |
| 2942 | UpdateDestListPtr = true; |
| 2943 | break; |
| 2944 | } |
| 2945 | case ThreadCopy: { |
| 2946 | // Step 1.1: Get the address for the src element in the Reduce list. |
| 2947 | Address SrcElementPtrAddr = |
| 2948 | Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize()); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2949 | SrcElementAddr = CGF.EmitLoadOfPointer( |
| 2950 | SrcElementPtrAddr, |
| 2951 | C.getPointerType(Private->getType())->castAs<PointerType>()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2952 | |
| 2953 | // Step 1.2: Get the address for dest element. The destination |
| 2954 | // element has already been created on the thread's stack. |
| 2955 | DestElementPtrAddr = |
| 2956 | Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize()); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2957 | DestElementAddr = CGF.EmitLoadOfPointer( |
| 2958 | DestElementPtrAddr, |
| 2959 | C.getPointerType(Private->getType())->castAs<PointerType>()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2960 | break; |
| 2961 | } |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2962 | case ThreadToScratchpad: { |
| 2963 | // Step 1.1: Get the address for the src element in the Reduce list. |
| 2964 | Address SrcElementPtrAddr = |
| 2965 | Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize()); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2966 | SrcElementAddr = CGF.EmitLoadOfPointer( |
| 2967 | SrcElementPtrAddr, |
| 2968 | C.getPointerType(Private->getType())->castAs<PointerType>()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2969 | |
| 2970 | // Step 1.2: Get the address for dest element: |
| 2971 | // address = base + index * ElementSizeInChars. |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2972 | llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType()); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2973 | llvm::Value *CurrentOffset = |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2974 | Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2975 | llvm::Value *ScratchPadElemAbsolutePtrVal = |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2976 | Bld.CreateNUWAdd(DestBase.getPointer(), CurrentOffset); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2977 | ScratchPadElemAbsolutePtrVal = |
| 2978 | Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy); |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 2979 | DestElementAddr = Address(ScratchPadElemAbsolutePtrVal, |
| 2980 | C.getTypeAlignInChars(Private->getType())); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2981 | IncrScratchpadDest = true; |
| 2982 | break; |
| 2983 | } |
| 2984 | case ScratchpadToThread: { |
| 2985 | // Step 1.1: Get the address for the src element in the scratchpad. |
| 2986 | // address = base + index * ElementSizeInChars. |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2987 | llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType()); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2988 | llvm::Value *CurrentOffset = |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2989 | Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 2990 | llvm::Value *ScratchPadElemAbsolutePtrVal = |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 2991 | Bld.CreateNUWAdd(SrcBase.getPointer(), CurrentOffset); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 2992 | ScratchPadElemAbsolutePtrVal = |
| 2993 | Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy); |
| 2994 | SrcElementAddr = Address(ScratchPadElemAbsolutePtrVal, |
| 2995 | C.getTypeAlignInChars(Private->getType())); |
| 2996 | IncrScratchpadSrc = true; |
| 2997 | |
| 2998 | // Step 1.2: Create a temporary to store the element in the destination |
| 2999 | // Reduce list. |
| 3000 | DestElementPtrAddr = |
| 3001 | Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize()); |
| 3002 | DestElementAddr = |
| 3003 | CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element"); |
| 3004 | UpdateDestListPtr = true; |
| 3005 | break; |
| 3006 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | // Regardless of src and dest of copy, we emit the load of src |
| 3010 | // element as this is required in all directions |
| 3011 | SrcElementAddr = Bld.CreateElementBitCast( |
| 3012 | SrcElementAddr, CGF.ConvertTypeForMem(Private->getType())); |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 3013 | DestElementAddr = Bld.CreateElementBitCast(DestElementAddr, |
| 3014 | SrcElementAddr.getElementType()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Now that all active lanes have read the element in the |
| 3017 | // Reduce list, shuffle over the value from the remote lane. |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 3018 | if (ShuffleInElement) { |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 3019 | shuffleAndStore(CGF, SrcElementAddr, DestElementAddr, Private->getType(), |
| 3020 | RemoteLaneOffset, Private->getExprLoc()); |
| 3021 | } else { |
| 3022 | if (Private->getType()->isScalarType()) { |
| 3023 | llvm::Value *Elem = |
| 3024 | CGF.EmitLoadOfScalar(SrcElementAddr, /*Volatile=*/false, |
| 3025 | Private->getType(), Private->getExprLoc()); |
| 3026 | // Store the source element value to the dest element address. |
| 3027 | CGF.EmitStoreOfScalar(Elem, DestElementAddr, /*Volatile=*/false, |
| 3028 | Private->getType()); |
| 3029 | } else { |
| 3030 | CGF.EmitAggregateCopy( |
| 3031 | CGF.MakeAddrLValue(DestElementAddr, Private->getType()), |
| 3032 | CGF.MakeAddrLValue(SrcElementAddr, Private->getType()), |
| 3033 | Private->getType(), AggValueSlot::DoesNotOverlap); |
| 3034 | } |
Alexey Bataev | a453f36 | 2018-03-19 17:53:56 +0000 | [diff] [blame] | 3035 | } |
Alexey Bataev | b257593 | 2018-01-04 20:18:55 +0000 | [diff] [blame] | 3036 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3037 | // Step 3.1: Modify reference in dest Reduce list as needed. |
| 3038 | // Modifying the reference in Reduce list to point to the newly |
| 3039 | // created element. The element is live in the current function |
| 3040 | // scope and that of functions it invokes (i.e., reduce_function). |
| 3041 | // RemoteReduceData[i] = (void*)&RemoteElem |
| 3042 | if (UpdateDestListPtr) { |
| 3043 | CGF.EmitStoreOfScalar(Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3044 | DestElementAddr.getPointer(), CGF.VoidPtrTy), |
| 3045 | DestElementPtrAddr, /*Volatile=*/false, |
| 3046 | C.VoidPtrTy); |
| 3047 | } |
| 3048 | |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3049 | // Step 4.1: Increment SrcBase/DestBase so that it points to the starting |
| 3050 | // address of the next element in scratchpad memory, unless we're currently |
| 3051 | // processing the last one. Memory alignment is also taken care of here. |
| 3052 | if ((IncrScratchpadDest || IncrScratchpadSrc) && (Idx + 1 < Size)) { |
| 3053 | llvm::Value *ScratchpadBasePtr = |
| 3054 | IncrScratchpadDest ? DestBase.getPointer() : SrcBase.getPointer(); |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 3055 | llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType()); |
| 3056 | ScratchpadBasePtr = Bld.CreateNUWAdd( |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3057 | ScratchpadBasePtr, |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 3058 | Bld.CreateNUWMul(ScratchpadWidth, ElementSizeInChars)); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Take care of global memory alignment for performance |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 3061 | ScratchpadBasePtr = Bld.CreateNUWSub( |
| 3062 | ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1)); |
| 3063 | ScratchpadBasePtr = Bld.CreateUDiv( |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3064 | ScratchpadBasePtr, |
| 3065 | llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment)); |
Alexey Bataev | e290ec0 | 2018-04-06 16:03:36 +0000 | [diff] [blame] | 3066 | ScratchpadBasePtr = Bld.CreateNUWAdd( |
| 3067 | ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1)); |
| 3068 | ScratchpadBasePtr = Bld.CreateNUWMul( |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3069 | ScratchpadBasePtr, |
| 3070 | llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment)); |
| 3071 | |
| 3072 | if (IncrScratchpadDest) |
| 3073 | DestBase = Address(ScratchpadBasePtr, CGF.getPointerAlign()); |
| 3074 | else /* IncrScratchpadSrc = true */ |
| 3075 | SrcBase = Address(ScratchpadBasePtr, CGF.getPointerAlign()); |
| 3076 | } |
| 3077 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3078 | ++Idx; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3079 | } |
| 3080 | } |
| 3081 | |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3082 | /// This function emits a helper that loads data from the scratchpad array |
| 3083 | /// and (optionally) reduces it with the input operand. |
| 3084 | /// |
| 3085 | /// load_and_reduce(local, scratchpad, index, width, should_reduce) |
| 3086 | /// reduce_data remote; |
| 3087 | /// for elem in remote: |
| 3088 | /// remote.elem = Scratchpad[elem_id][index] |
| 3089 | /// if (should_reduce) |
| 3090 | /// local = local @ remote |
| 3091 | /// else |
| 3092 | /// local = remote |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3093 | static llvm::Value *emitReduceScratchpadFunction( |
| 3094 | CodeGenModule &CGM, ArrayRef<const Expr *> Privates, |
| 3095 | QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3096 | ASTContext &C = CGM.getContext(); |
| 3097 | QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Destination of the copy. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3100 | ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3101 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3102 | // Base address of the scratchpad array, with each element storing a |
| 3103 | // Reduce list per team. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3104 | ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3105 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3106 | // A source index into the scratchpad array. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3107 | ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty, |
| 3108 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3109 | // Row width of an element in the scratchpad array, typically |
| 3110 | // the number of teams. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3111 | ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty, |
| 3112 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3113 | // If should_reduce == 1, then it's load AND reduce, |
| 3114 | // If should_reduce == 0 (or otherwise), then it only loads (+ copy). |
| 3115 | // The latter case is used for initialization. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3116 | ImplicitParamDecl ShouldReduceArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3117 | Int32Ty, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3118 | |
| 3119 | FunctionArgList Args; |
| 3120 | Args.push_back(&ReduceListArg); |
| 3121 | Args.push_back(&ScratchPadArg); |
| 3122 | Args.push_back(&IndexArg); |
| 3123 | Args.push_back(&WidthArg); |
| 3124 | Args.push_back(&ShouldReduceArg); |
| 3125 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3126 | const CGFunctionInfo &CGFI = |
| 3127 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3128 | auto *Fn = llvm::Function::Create( |
| 3129 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3130 | "_omp_reduction_load_and_reduce", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3131 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3132 | Fn->setDoesNotRecurse(); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3133 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3134 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3135 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3136 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3137 | |
| 3138 | // Get local Reduce list pointer. |
| 3139 | Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); |
| 3140 | Address ReduceListAddr( |
| 3141 | Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3142 | CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3143 | C.VoidPtrTy, Loc), |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3144 | CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()), |
| 3145 | CGF.getPointerAlign()); |
| 3146 | |
| 3147 | Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg); |
| 3148 | llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar( |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3149 | AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3150 | |
| 3151 | Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3152 | llvm::Value *IndexVal = Bld.CreateIntCast( |
| 3153 | CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc), |
| 3154 | CGM.SizeTy, /*isSigned=*/true); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3155 | |
| 3156 | Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3157 | llvm::Value *WidthVal = Bld.CreateIntCast( |
| 3158 | CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false, Int32Ty, Loc), |
| 3159 | CGM.SizeTy, /*isSigned=*/true); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3160 | |
| 3161 | Address AddrShouldReduceArg = CGF.GetAddrOfLocalVar(&ShouldReduceArg); |
| 3162 | llvm::Value *ShouldReduceVal = CGF.EmitLoadOfScalar( |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3163 | AddrShouldReduceArg, /*Volatile=*/false, Int32Ty, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3164 | |
| 3165 | // The absolute ptr address to the base addr of the next element to copy. |
| 3166 | llvm::Value *CumulativeElemBasePtr = |
| 3167 | Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy); |
| 3168 | Address SrcDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign()); |
| 3169 | |
| 3170 | // Create a Remote Reduce list to store the elements read from the |
| 3171 | // scratchpad array. |
| 3172 | Address RemoteReduceList = |
| 3173 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_red_list"); |
| 3174 | |
| 3175 | // Assemble remote Reduce list from scratchpad array. |
| 3176 | emitReductionListCopy(ScratchpadToThread, CGF, ReductionArrayTy, Privates, |
| 3177 | SrcDataAddr, RemoteReduceList, |
| 3178 | {/*RemoteLaneOffset=*/nullptr, |
| 3179 | /*ScratchpadIndex=*/IndexVal, |
| 3180 | /*ScratchpadWidth=*/WidthVal}); |
| 3181 | |
| 3182 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then"); |
| 3183 | llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else"); |
| 3184 | llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont"); |
| 3185 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3186 | llvm::Value *CondReduce = Bld.CreateIsNotNull(ShouldReduceVal); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3187 | Bld.CreateCondBr(CondReduce, ThenBB, ElseBB); |
| 3188 | |
| 3189 | CGF.EmitBlock(ThenBB); |
| 3190 | // We should reduce with the local Reduce list. |
| 3191 | // reduce_function(LocalReduceList, RemoteReduceList) |
| 3192 | llvm::Value *LocalDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3193 | ReduceListAddr.getPointer(), CGF.VoidPtrTy); |
| 3194 | llvm::Value *RemoteDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3195 | RemoteReduceList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3196 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3197 | CGF, Loc, ReduceFn, {LocalDataPtr, RemoteDataPtr}); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3198 | Bld.CreateBr(MergeBB); |
| 3199 | |
| 3200 | CGF.EmitBlock(ElseBB); |
| 3201 | // No reduction; just copy: |
| 3202 | // Local Reduce list = Remote Reduce list. |
| 3203 | emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates, |
| 3204 | RemoteReduceList, ReduceListAddr); |
| 3205 | Bld.CreateBr(MergeBB); |
| 3206 | |
| 3207 | CGF.EmitBlock(MergeBB); |
| 3208 | |
| 3209 | CGF.FinishFunction(); |
| 3210 | return Fn; |
| 3211 | } |
| 3212 | |
| 3213 | /// This function emits a helper that stores reduced data from the team |
| 3214 | /// master to a scratchpad array in global memory. |
| 3215 | /// |
| 3216 | /// for elem in Reduce List: |
| 3217 | /// scratchpad[elem_id][index] = elem |
| 3218 | /// |
Benjamin Kramer | 674d579 | 2017-05-26 20:08:24 +0000 | [diff] [blame] | 3219 | static llvm::Value *emitCopyToScratchpad(CodeGenModule &CGM, |
| 3220 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3221 | QualType ReductionArrayTy, |
| 3222 | SourceLocation Loc) { |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3223 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3224 | ASTContext &C = CGM.getContext(); |
| 3225 | QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3226 | |
| 3227 | // Source of the copy. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3228 | ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3229 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3230 | // Base address of the scratchpad array, with each element storing a |
| 3231 | // Reduce list per team. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3232 | ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3233 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3234 | // A destination index into the scratchpad array, typically the team |
| 3235 | // identifier. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3236 | ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty, |
| 3237 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3238 | // Row width of an element in the scratchpad array, typically |
| 3239 | // the number of teams. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3240 | ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty, |
| 3241 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3242 | |
| 3243 | FunctionArgList Args; |
| 3244 | Args.push_back(&ReduceListArg); |
| 3245 | Args.push_back(&ScratchPadArg); |
| 3246 | Args.push_back(&IndexArg); |
| 3247 | Args.push_back(&WidthArg); |
| 3248 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3249 | const CGFunctionInfo &CGFI = |
| 3250 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3251 | auto *Fn = llvm::Function::Create( |
| 3252 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3253 | "_omp_reduction_copy_to_scratchpad", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3254 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3255 | Fn->setDoesNotRecurse(); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3256 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3257 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3258 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3259 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3260 | |
| 3261 | Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); |
| 3262 | Address SrcDataAddr( |
| 3263 | Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3264 | CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3265 | C.VoidPtrTy, Loc), |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3266 | CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()), |
| 3267 | CGF.getPointerAlign()); |
| 3268 | |
| 3269 | Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg); |
| 3270 | llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar( |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3271 | AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3272 | |
| 3273 | Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3274 | llvm::Value *IndexVal = Bld.CreateIntCast( |
| 3275 | CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc), |
| 3276 | CGF.SizeTy, /*isSigned=*/true); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3277 | |
| 3278 | Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3279 | llvm::Value *WidthVal = Bld.CreateIntCast( |
| 3280 | CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false, Int32Ty, Loc), |
| 3281 | CGF.SizeTy, /*isSigned=*/true); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3282 | |
| 3283 | // The absolute ptr address to the base addr of the next element to copy. |
| 3284 | llvm::Value *CumulativeElemBasePtr = |
| 3285 | Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy); |
| 3286 | Address DestDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign()); |
| 3287 | |
| 3288 | emitReductionListCopy(ThreadToScratchpad, CGF, ReductionArrayTy, Privates, |
| 3289 | SrcDataAddr, DestDataAddr, |
| 3290 | {/*RemoteLaneOffset=*/nullptr, |
| 3291 | /*ScratchpadIndex=*/IndexVal, |
| 3292 | /*ScratchpadWidth=*/WidthVal}); |
| 3293 | |
| 3294 | CGF.FinishFunction(); |
| 3295 | return Fn; |
| 3296 | } |
| 3297 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3298 | /// This function emits a helper that gathers Reduce lists from the first |
| 3299 | /// lane of every active warp to lanes in the first warp. |
| 3300 | /// |
| 3301 | /// void inter_warp_copy_func(void* reduce_data, num_warps) |
| 3302 | /// shared smem[warp_size]; |
| 3303 | /// For all data entries D in reduce_data: |
| 3304 | /// If (I am the first lane in each warp) |
| 3305 | /// Copy my local D to smem[warp_id] |
| 3306 | /// sync |
| 3307 | /// if (I am the first warp) |
| 3308 | /// Copy smem[thread_id] to my local D |
| 3309 | /// sync |
| 3310 | static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM, |
| 3311 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3312 | QualType ReductionArrayTy, |
| 3313 | SourceLocation Loc) { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3314 | ASTContext &C = CGM.getContext(); |
| 3315 | llvm::Module &M = CGM.getModule(); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3316 | |
| 3317 | // ReduceList: thread local Reduce list. |
| 3318 | // At the stage of the computation when this function is called, partially |
| 3319 | // aggregated values reside in the first lane of every active warp. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3320 | ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3321 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3322 | // NumWarps: number of warps active in the parallel region. This could |
| 3323 | // be smaller than 32 (max warps in a CTA) for partial block reduction. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3324 | ImplicitParamDecl NumWarpsArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 3325 | C.getIntTypeForBitwidth(32, /* Signed */ true), |
| 3326 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3327 | FunctionArgList Args; |
| 3328 | Args.push_back(&ReduceListArg); |
| 3329 | Args.push_back(&NumWarpsArg); |
| 3330 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3331 | const CGFunctionInfo &CGFI = |
| 3332 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3333 | auto *Fn = llvm::Function::Create( |
| 3334 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3335 | "_omp_reduction_inter_warp_copy_func", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3336 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3337 | Fn->setDoesNotRecurse(); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3338 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3339 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3340 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3341 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3342 | |
| 3343 | // This array is used as a medium to transfer, one reduce element at a time, |
| 3344 | // the data from the first lane of every warp to lanes in the first warp |
| 3345 | // in order to perform the final step of a reduction in a parallel region |
| 3346 | // (reduction across warps). The array is placed in NVPTX __shared__ memory |
| 3347 | // for reduced latency, as well as to have a distinct copy for concurrently |
| 3348 | // executing target regions. The array is declared with common linkage so |
| 3349 | // as to be shared across compilation units. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3350 | StringRef TransferMediumName = |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3351 | "__openmp_nvptx_data_transfer_temporary_storage"; |
| 3352 | llvm::GlobalVariable *TransferMedium = |
| 3353 | M.getGlobalVariable(TransferMediumName); |
| 3354 | if (!TransferMedium) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3355 | auto *Ty = llvm::ArrayType::get(CGM.Int32Ty, WarpSize); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3356 | unsigned SharedAddressSpace = C.getTargetAddressSpace(LangAS::cuda_shared); |
| 3357 | TransferMedium = new llvm::GlobalVariable( |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3358 | M, Ty, /*isConstant=*/false, llvm::GlobalVariable::CommonLinkage, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3359 | llvm::Constant::getNullValue(Ty), TransferMediumName, |
| 3360 | /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, |
| 3361 | SharedAddressSpace); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3362 | CGM.addCompilerUsedGlobal(TransferMedium); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | // Get the CUDA thread id of the current OpenMP thread on the GPU. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3366 | llvm::Value *ThreadID = getNVPTXThreadID(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3367 | // nvptx_lane_id = nvptx_id % warpsize |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3368 | llvm::Value *LaneID = getNVPTXLaneID(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3369 | // nvptx_warp_id = nvptx_id / warpsize |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3370 | llvm::Value *WarpID = getNVPTXWarpID(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3371 | |
| 3372 | Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); |
| 3373 | Address LocalReduceList( |
| 3374 | Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3375 | CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false, |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3376 | C.VoidPtrTy, Loc), |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3377 | CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()), |
| 3378 | CGF.getPointerAlign()); |
| 3379 | |
| 3380 | unsigned Idx = 0; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3381 | for (const Expr *Private : Privates) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3382 | // |
| 3383 | // Warp master copies reduce element to transfer medium in __shared__ |
| 3384 | // memory. |
| 3385 | // |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3386 | unsigned RealTySize = |
| 3387 | C.getTypeSizeInChars(Private->getType()) |
| 3388 | .alignTo(C.getTypeAlignInChars(Private->getType())) |
| 3389 | .getQuantity(); |
| 3390 | for (unsigned TySize = 4; TySize > 0 && RealTySize > 0; TySize /=2) { |
| 3391 | unsigned NumIters = RealTySize / TySize; |
| 3392 | if (NumIters == 0) |
| 3393 | continue; |
| 3394 | QualType CType = C.getIntTypeForBitwidth( |
| 3395 | C.toBits(CharUnits::fromQuantity(TySize)), /*Signed=*/1); |
| 3396 | llvm::Type *CopyType = CGF.ConvertTypeForMem(CType); |
| 3397 | CharUnits Align = CharUnits::fromQuantity(TySize); |
| 3398 | llvm::Value *Cnt = nullptr; |
| 3399 | Address CntAddr = Address::invalid(); |
| 3400 | llvm::BasicBlock *PrecondBB = nullptr; |
| 3401 | llvm::BasicBlock *ExitBB = nullptr; |
| 3402 | if (NumIters > 1) { |
| 3403 | CntAddr = CGF.CreateMemTemp(C.IntTy, ".cnt.addr"); |
| 3404 | CGF.EmitStoreOfScalar(llvm::Constant::getNullValue(CGM.IntTy), CntAddr, |
| 3405 | /*Volatile=*/false, C.IntTy); |
| 3406 | PrecondBB = CGF.createBasicBlock("precond"); |
| 3407 | ExitBB = CGF.createBasicBlock("exit"); |
| 3408 | llvm::BasicBlock *BodyBB = CGF.createBasicBlock("body"); |
| 3409 | // There is no need to emit line number for unconditional branch. |
| 3410 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 3411 | CGF.EmitBlock(PrecondBB); |
| 3412 | Cnt = CGF.EmitLoadOfScalar(CntAddr, /*Volatile=*/false, C.IntTy, Loc); |
| 3413 | llvm::Value *Cmp = |
| 3414 | Bld.CreateICmpULT(Cnt, llvm::ConstantInt::get(CGM.IntTy, NumIters)); |
| 3415 | Bld.CreateCondBr(Cmp, BodyBB, ExitBB); |
| 3416 | CGF.EmitBlock(BodyBB); |
| 3417 | } |
| 3418 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then"); |
| 3419 | llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else"); |
| 3420 | llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont"); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3421 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3422 | // if (lane_id == 0) |
| 3423 | llvm::Value *IsWarpMaster = Bld.CreateIsNull(LaneID, "warp_master"); |
| 3424 | Bld.CreateCondBr(IsWarpMaster, ThenBB, ElseBB); |
| 3425 | CGF.EmitBlock(ThenBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3426 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3427 | // Reduce element = LocalReduceList[i] |
| 3428 | Address ElemPtrPtrAddr = |
| 3429 | Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize()); |
| 3430 | llvm::Value *ElemPtrPtr = CGF.EmitLoadOfScalar( |
| 3431 | ElemPtrPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation()); |
| 3432 | // elemptr = ((CopyType*)(elemptrptr)) + I |
| 3433 | Address ElemPtr = Address(ElemPtrPtr, Align); |
| 3434 | ElemPtr = Bld.CreateElementBitCast(ElemPtr, CopyType); |
| 3435 | if (NumIters > 1) { |
| 3436 | ElemPtr = Address(Bld.CreateGEP(ElemPtr.getPointer(), Cnt), |
| 3437 | ElemPtr.getAlignment()); |
| 3438 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3439 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3440 | // Get pointer to location in transfer medium. |
| 3441 | // MediumPtr = &medium[warp_id] |
| 3442 | llvm::Value *MediumPtrVal = Bld.CreateInBoundsGEP( |
| 3443 | TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), WarpID}); |
| 3444 | Address MediumPtr(MediumPtrVal, Align); |
| 3445 | // Casting to actual data type. |
| 3446 | // MediumPtr = (CopyType*)MediumPtrAddr; |
| 3447 | MediumPtr = Bld.CreateElementBitCast(MediumPtr, CopyType); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3448 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3449 | // elem = *elemptr |
| 3450 | //*MediumPtr = elem |
| 3451 | llvm::Value *Elem = |
| 3452 | CGF.EmitLoadOfScalar(ElemPtr, /*Volatile=*/false, CType, Loc); |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 3453 | // Store the source element value to the dest element address. |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3454 | CGF.EmitStoreOfScalar(Elem, MediumPtr, /*Volatile=*/true, CType); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3455 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3456 | Bld.CreateBr(MergeBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3457 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3458 | CGF.EmitBlock(ElseBB); |
| 3459 | Bld.CreateBr(MergeBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3460 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3461 | CGF.EmitBlock(MergeBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3462 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3463 | Address AddrNumWarpsArg = CGF.GetAddrOfLocalVar(&NumWarpsArg); |
| 3464 | llvm::Value *NumWarpsVal = CGF.EmitLoadOfScalar( |
| 3465 | AddrNumWarpsArg, /*Volatile=*/false, C.IntTy, Loc); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3466 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3467 | llvm::Value *NumActiveThreads = Bld.CreateNSWMul( |
| 3468 | NumWarpsVal, getNVPTXWarpSize(CGF), "num_active_threads"); |
| 3469 | // named_barrier_sync(ParallelBarrierID, num_active_threads) |
| 3470 | syncParallelThreads(CGF, NumActiveThreads); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3471 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3472 | // |
| 3473 | // Warp 0 copies reduce element from transfer medium. |
| 3474 | // |
| 3475 | llvm::BasicBlock *W0ThenBB = CGF.createBasicBlock("then"); |
| 3476 | llvm::BasicBlock *W0ElseBB = CGF.createBasicBlock("else"); |
| 3477 | llvm::BasicBlock *W0MergeBB = CGF.createBasicBlock("ifcont"); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3478 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3479 | // Up to 32 threads in warp 0 are active. |
| 3480 | llvm::Value *IsActiveThread = |
| 3481 | Bld.CreateICmpULT(ThreadID, NumWarpsVal, "is_active_thread"); |
| 3482 | Bld.CreateCondBr(IsActiveThread, W0ThenBB, W0ElseBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3483 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3484 | CGF.EmitBlock(W0ThenBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3485 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3486 | // SrcMediumPtr = &medium[tid] |
| 3487 | llvm::Value *SrcMediumPtrVal = Bld.CreateInBoundsGEP( |
| 3488 | TransferMedium, |
| 3489 | {llvm::Constant::getNullValue(CGM.Int64Ty), ThreadID}); |
| 3490 | Address SrcMediumPtr(SrcMediumPtrVal, Align); |
| 3491 | // SrcMediumVal = *SrcMediumPtr; |
| 3492 | SrcMediumPtr = Bld.CreateElementBitCast(SrcMediumPtr, CopyType); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3493 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3494 | // TargetElemPtr = (CopyType*)(SrcDataAddr[i]) + I |
| 3495 | Address TargetElemPtrPtr = |
| 3496 | Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize()); |
| 3497 | llvm::Value *TargetElemPtrVal = CGF.EmitLoadOfScalar( |
| 3498 | TargetElemPtrPtr, /*Volatile=*/false, C.VoidPtrTy, Loc); |
| 3499 | Address TargetElemPtr = Address(TargetElemPtrVal, Align); |
| 3500 | TargetElemPtr = Bld.CreateElementBitCast(TargetElemPtr, CopyType); |
| 3501 | if (NumIters > 1) { |
| 3502 | TargetElemPtr = Address(Bld.CreateGEP(TargetElemPtr.getPointer(), Cnt), |
| 3503 | TargetElemPtr.getAlignment()); |
| 3504 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3505 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3506 | // *TargetElemPtr = SrcMediumVal; |
| 3507 | llvm::Value *SrcMediumValue = |
| 3508 | CGF.EmitLoadOfScalar(SrcMediumPtr, /*Volatile=*/true, CType, Loc); |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 3509 | CGF.EmitStoreOfScalar(SrcMediumValue, TargetElemPtr, /*Volatile=*/false, |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 3510 | CType); |
| 3511 | Bld.CreateBr(W0MergeBB); |
| 3512 | |
| 3513 | CGF.EmitBlock(W0ElseBB); |
| 3514 | Bld.CreateBr(W0MergeBB); |
| 3515 | |
| 3516 | CGF.EmitBlock(W0MergeBB); |
| 3517 | |
| 3518 | // While warp 0 copies values from transfer medium, all other warps must |
| 3519 | // wait. |
| 3520 | syncParallelThreads(CGF, NumActiveThreads); |
| 3521 | if (NumIters > 1) { |
| 3522 | Cnt = Bld.CreateNSWAdd(Cnt, llvm::ConstantInt::get(CGM.IntTy, /*V=*/1)); |
| 3523 | CGF.EmitStoreOfScalar(Cnt, CntAddr, /*Volatile=*/false, C.IntTy); |
| 3524 | CGF.EmitBranch(PrecondBB); |
| 3525 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 3526 | CGF.EmitBlock(ExitBB); |
| 3527 | } |
| 3528 | RealTySize %= TySize; |
Alexey Bataev | 12c6290 | 2018-06-22 19:10:38 +0000 | [diff] [blame] | 3529 | } |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3530 | ++Idx; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | CGF.FinishFunction(); |
| 3534 | return Fn; |
| 3535 | } |
| 3536 | |
| 3537 | /// Emit a helper that reduces data across two OpenMP threads (lanes) |
| 3538 | /// in the same warp. It uses shuffle instructions to copy over data from |
| 3539 | /// a remote lane's stack. The reduction algorithm performed is specified |
| 3540 | /// by the fourth parameter. |
| 3541 | /// |
| 3542 | /// Algorithm Versions. |
| 3543 | /// Full Warp Reduce (argument value 0): |
| 3544 | /// This algorithm assumes that all 32 lanes are active and gathers |
| 3545 | /// data from these 32 lanes, producing a single resultant value. |
| 3546 | /// Contiguous Partial Warp Reduce (argument value 1): |
| 3547 | /// This algorithm assumes that only a *contiguous* subset of lanes |
| 3548 | /// are active. This happens for the last warp in a parallel region |
| 3549 | /// when the user specified num_threads is not an integer multiple of |
| 3550 | /// 32. This contiguous subset always starts with the zeroth lane. |
| 3551 | /// Partial Warp Reduce (argument value 2): |
| 3552 | /// This algorithm gathers data from any number of lanes at any position. |
| 3553 | /// All reduced values are stored in the lowest possible lane. The set |
| 3554 | /// of problems every algorithm addresses is a super set of those |
| 3555 | /// addressable by algorithms with a lower version number. Overhead |
| 3556 | /// increases as algorithm version increases. |
| 3557 | /// |
| 3558 | /// Terminology |
| 3559 | /// Reduce element: |
| 3560 | /// Reduce element refers to the individual data field with primitive |
| 3561 | /// data types to be combined and reduced across threads. |
| 3562 | /// Reduce list: |
| 3563 | /// Reduce list refers to a collection of local, thread-private |
| 3564 | /// reduce elements. |
| 3565 | /// Remote Reduce list: |
| 3566 | /// Remote Reduce list refers to a collection of remote (relative to |
| 3567 | /// the current thread) reduce elements. |
| 3568 | /// |
| 3569 | /// We distinguish between three states of threads that are important to |
| 3570 | /// the implementation of this function. |
| 3571 | /// Alive threads: |
| 3572 | /// Threads in a warp executing the SIMT instruction, as distinguished from |
| 3573 | /// threads that are inactive due to divergent control flow. |
| 3574 | /// Active threads: |
| 3575 | /// The minimal set of threads that has to be alive upon entry to this |
| 3576 | /// function. The computation is correct iff active threads are alive. |
| 3577 | /// Some threads are alive but they are not active because they do not |
| 3578 | /// contribute to the computation in any useful manner. Turning them off |
| 3579 | /// may introduce control flow overheads without any tangible benefits. |
| 3580 | /// Effective threads: |
| 3581 | /// In order to comply with the argument requirements of the shuffle |
| 3582 | /// function, we must keep all lanes holding data alive. But at most |
| 3583 | /// half of them perform value aggregation; we refer to this half of |
| 3584 | /// threads as effective. The other half is simply handing off their |
| 3585 | /// data. |
| 3586 | /// |
| 3587 | /// Procedure |
| 3588 | /// Value shuffle: |
| 3589 | /// In this step active threads transfer data from higher lane positions |
| 3590 | /// in the warp to lower lane positions, creating Remote Reduce list. |
| 3591 | /// Value aggregation: |
| 3592 | /// In this step, effective threads combine their thread local Reduce list |
| 3593 | /// with Remote Reduce list and store the result in the thread local |
| 3594 | /// Reduce list. |
| 3595 | /// Value copy: |
| 3596 | /// In this step, we deal with the assumption made by algorithm 2 |
| 3597 | /// (i.e. contiguity assumption). When we have an odd number of lanes |
| 3598 | /// active, say 2k+1, only k threads will be effective and therefore k |
| 3599 | /// new values will be produced. However, the Reduce list owned by the |
| 3600 | /// (2k+1)th thread is ignored in the value aggregation. Therefore |
| 3601 | /// we copy the Reduce list from the (2k+1)th lane to (k+1)th lane so |
| 3602 | /// that the contiguity assumption still holds. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3603 | static llvm::Value *emitShuffleAndReduceFunction( |
| 3604 | CodeGenModule &CGM, ArrayRef<const Expr *> Privates, |
| 3605 | QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) { |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3606 | ASTContext &C = CGM.getContext(); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3607 | |
| 3608 | // Thread local Reduce list used to host the values of data to be reduced. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3609 | ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3610 | C.VoidPtrTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3611 | // Current lane id; could be logical. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3612 | ImplicitParamDecl LaneIDArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.ShortTy, |
| 3613 | ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3614 | // Offset of the remote source lane relative to the current lane. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3615 | ImplicitParamDecl RemoteLaneOffsetArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3616 | C.ShortTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3617 | // Algorithm version. This is expected to be known at compile time. |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3618 | ImplicitParamDecl AlgoVerArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 3619 | C.ShortTy, ImplicitParamDecl::Other); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3620 | FunctionArgList Args; |
| 3621 | Args.push_back(&ReduceListArg); |
| 3622 | Args.push_back(&LaneIDArg); |
| 3623 | Args.push_back(&RemoteLaneOffsetArg); |
| 3624 | Args.push_back(&AlgoVerArg); |
| 3625 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3626 | const CGFunctionInfo &CGFI = |
| 3627 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3628 | auto *Fn = llvm::Function::Create( |
| 3629 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3630 | "_omp_reduction_shuffle_and_reduce_func", &CGM.getModule()); |
Rafael Espindola | 51ec5a9 | 2018-02-28 23:46:35 +0000 | [diff] [blame] | 3631 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3632 | Fn->setDoesNotRecurse(); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3633 | CodeGenFunction CGF(CGM); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3634 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3635 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3636 | CGBuilderTy &Bld = CGF.Builder; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3637 | |
| 3638 | Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); |
| 3639 | Address LocalReduceList( |
| 3640 | Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3641 | CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false, |
| 3642 | C.VoidPtrTy, SourceLocation()), |
| 3643 | CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()), |
| 3644 | CGF.getPointerAlign()); |
| 3645 | |
| 3646 | Address AddrLaneIDArg = CGF.GetAddrOfLocalVar(&LaneIDArg); |
| 3647 | llvm::Value *LaneIDArgVal = CGF.EmitLoadOfScalar( |
| 3648 | AddrLaneIDArg, /*Volatile=*/false, C.ShortTy, SourceLocation()); |
| 3649 | |
| 3650 | Address AddrRemoteLaneOffsetArg = CGF.GetAddrOfLocalVar(&RemoteLaneOffsetArg); |
| 3651 | llvm::Value *RemoteLaneOffsetArgVal = CGF.EmitLoadOfScalar( |
| 3652 | AddrRemoteLaneOffsetArg, /*Volatile=*/false, C.ShortTy, SourceLocation()); |
| 3653 | |
| 3654 | Address AddrAlgoVerArg = CGF.GetAddrOfLocalVar(&AlgoVerArg); |
| 3655 | llvm::Value *AlgoVerArgVal = CGF.EmitLoadOfScalar( |
| 3656 | AddrAlgoVerArg, /*Volatile=*/false, C.ShortTy, SourceLocation()); |
| 3657 | |
| 3658 | // Create a local thread-private variable to host the Reduce list |
| 3659 | // from a remote lane. |
| 3660 | Address RemoteReduceList = |
| 3661 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_reduce_list"); |
| 3662 | |
| 3663 | // This loop iterates through the list of reduce elements and copies, |
| 3664 | // element by element, from a remote lane in the warp to RemoteReduceList, |
| 3665 | // hosted on the thread's stack. |
| 3666 | emitReductionListCopy(RemoteLaneToThread, CGF, ReductionArrayTy, Privates, |
| 3667 | LocalReduceList, RemoteReduceList, |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3668 | {/*RemoteLaneOffset=*/RemoteLaneOffsetArgVal, |
| 3669 | /*ScratchpadIndex=*/nullptr, |
| 3670 | /*ScratchpadWidth=*/nullptr}); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3671 | |
| 3672 | // The actions to be performed on the Remote Reduce list is dependent |
| 3673 | // on the algorithm version. |
| 3674 | // |
| 3675 | // if (AlgoVer==0) || (AlgoVer==1 && (LaneId < Offset)) || (AlgoVer==2 && |
| 3676 | // LaneId % 2 == 0 && Offset > 0): |
| 3677 | // do the reduction value aggregation |
| 3678 | // |
| 3679 | // The thread local variable Reduce list is mutated in place to host the |
| 3680 | // reduced data, which is the aggregated value produced from local and |
| 3681 | // remote lanes. |
| 3682 | // |
| 3683 | // Note that AlgoVer is expected to be a constant integer known at compile |
| 3684 | // time. |
| 3685 | // When AlgoVer==0, the first conjunction evaluates to true, making |
| 3686 | // the entire predicate true during compile time. |
| 3687 | // When AlgoVer==1, the second conjunction has only the second part to be |
| 3688 | // evaluated during runtime. Other conjunctions evaluates to false |
| 3689 | // during compile time. |
| 3690 | // When AlgoVer==2, the third conjunction has only the second part to be |
| 3691 | // evaluated during runtime. Other conjunctions evaluates to false |
| 3692 | // during compile time. |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3693 | llvm::Value *CondAlgo0 = Bld.CreateIsNull(AlgoVerArgVal); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3694 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3695 | llvm::Value *Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1)); |
| 3696 | llvm::Value *CondAlgo1 = Bld.CreateAnd( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3697 | Algo1, Bld.CreateICmpULT(LaneIDArgVal, RemoteLaneOffsetArgVal)); |
| 3698 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3699 | llvm::Value *Algo2 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(2)); |
| 3700 | llvm::Value *CondAlgo2 = Bld.CreateAnd( |
| 3701 | Algo2, Bld.CreateIsNull(Bld.CreateAnd(LaneIDArgVal, Bld.getInt16(1)))); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3702 | CondAlgo2 = Bld.CreateAnd( |
| 3703 | CondAlgo2, Bld.CreateICmpSGT(RemoteLaneOffsetArgVal, Bld.getInt16(0))); |
| 3704 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3705 | llvm::Value *CondReduce = Bld.CreateOr(CondAlgo0, CondAlgo1); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3706 | CondReduce = Bld.CreateOr(CondReduce, CondAlgo2); |
| 3707 | |
| 3708 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then"); |
| 3709 | llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else"); |
| 3710 | llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont"); |
| 3711 | Bld.CreateCondBr(CondReduce, ThenBB, ElseBB); |
| 3712 | |
| 3713 | CGF.EmitBlock(ThenBB); |
| 3714 | // reduce_function(LocalReduceList, RemoteReduceList) |
| 3715 | llvm::Value *LocalReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3716 | LocalReduceList.getPointer(), CGF.VoidPtrTy); |
| 3717 | llvm::Value *RemoteReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 3718 | RemoteReduceList.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 3719 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3720 | CGF, Loc, ReduceFn, {LocalReduceListPtr, RemoteReduceListPtr}); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3721 | Bld.CreateBr(MergeBB); |
| 3722 | |
| 3723 | CGF.EmitBlock(ElseBB); |
| 3724 | Bld.CreateBr(MergeBB); |
| 3725 | |
| 3726 | CGF.EmitBlock(MergeBB); |
| 3727 | |
| 3728 | // if (AlgoVer==1 && (LaneId >= Offset)) copy Remote Reduce list to local |
| 3729 | // Reduce list. |
| 3730 | Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1)); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 3731 | llvm::Value *CondCopy = Bld.CreateAnd( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3732 | Algo1, Bld.CreateICmpUGE(LaneIDArgVal, RemoteLaneOffsetArgVal)); |
| 3733 | |
| 3734 | llvm::BasicBlock *CpyThenBB = CGF.createBasicBlock("then"); |
| 3735 | llvm::BasicBlock *CpyElseBB = CGF.createBasicBlock("else"); |
| 3736 | llvm::BasicBlock *CpyMergeBB = CGF.createBasicBlock("ifcont"); |
| 3737 | Bld.CreateCondBr(CondCopy, CpyThenBB, CpyElseBB); |
| 3738 | |
| 3739 | CGF.EmitBlock(CpyThenBB); |
| 3740 | emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates, |
| 3741 | RemoteReduceList, LocalReduceList); |
| 3742 | Bld.CreateBr(CpyMergeBB); |
| 3743 | |
| 3744 | CGF.EmitBlock(CpyElseBB); |
| 3745 | Bld.CreateBr(CpyMergeBB); |
| 3746 | |
| 3747 | CGF.EmitBlock(CpyMergeBB); |
| 3748 | |
| 3749 | CGF.FinishFunction(); |
| 3750 | return Fn; |
| 3751 | } |
| 3752 | |
| 3753 | /// |
| 3754 | /// Design of OpenMP reductions on the GPU |
| 3755 | /// |
| 3756 | /// Consider a typical OpenMP program with one or more reduction |
| 3757 | /// clauses: |
| 3758 | /// |
| 3759 | /// float foo; |
| 3760 | /// double bar; |
| 3761 | /// #pragma omp target teams distribute parallel for \ |
| 3762 | /// reduction(+:foo) reduction(*:bar) |
| 3763 | /// for (int i = 0; i < N; i++) { |
| 3764 | /// foo += A[i]; bar *= B[i]; |
| 3765 | /// } |
| 3766 | /// |
| 3767 | /// where 'foo' and 'bar' are reduced across all OpenMP threads in |
| 3768 | /// all teams. In our OpenMP implementation on the NVPTX device an |
| 3769 | /// OpenMP team is mapped to a CUDA threadblock and OpenMP threads |
| 3770 | /// within a team are mapped to CUDA threads within a threadblock. |
| 3771 | /// Our goal is to efficiently aggregate values across all OpenMP |
| 3772 | /// threads such that: |
| 3773 | /// |
| 3774 | /// - the compiler and runtime are logically concise, and |
| 3775 | /// - the reduction is performed efficiently in a hierarchical |
| 3776 | /// manner as follows: within OpenMP threads in the same warp, |
| 3777 | /// across warps in a threadblock, and finally across teams on |
| 3778 | /// the NVPTX device. |
| 3779 | /// |
| 3780 | /// Introduction to Decoupling |
| 3781 | /// |
| 3782 | /// We would like to decouple the compiler and the runtime so that the |
| 3783 | /// latter is ignorant of the reduction variables (number, data types) |
| 3784 | /// and the reduction operators. This allows a simpler interface |
| 3785 | /// and implementation while still attaining good performance. |
| 3786 | /// |
| 3787 | /// Pseudocode for the aforementioned OpenMP program generated by the |
| 3788 | /// compiler is as follows: |
| 3789 | /// |
| 3790 | /// 1. Create private copies of reduction variables on each OpenMP |
| 3791 | /// thread: 'foo_private', 'bar_private' |
| 3792 | /// 2. Each OpenMP thread reduces the chunk of 'A' and 'B' assigned |
| 3793 | /// to it and writes the result in 'foo_private' and 'bar_private' |
| 3794 | /// respectively. |
| 3795 | /// 3. Call the OpenMP runtime on the GPU to reduce within a team |
| 3796 | /// and store the result on the team master: |
| 3797 | /// |
| 3798 | /// __kmpc_nvptx_parallel_reduce_nowait(..., |
| 3799 | /// reduceData, shuffleReduceFn, interWarpCpyFn) |
| 3800 | /// |
| 3801 | /// where: |
| 3802 | /// struct ReduceData { |
| 3803 | /// double *foo; |
| 3804 | /// double *bar; |
| 3805 | /// } reduceData |
| 3806 | /// reduceData.foo = &foo_private |
| 3807 | /// reduceData.bar = &bar_private |
| 3808 | /// |
| 3809 | /// 'shuffleReduceFn' and 'interWarpCpyFn' are pointers to two |
| 3810 | /// auxiliary functions generated by the compiler that operate on |
| 3811 | /// variables of type 'ReduceData'. They aid the runtime perform |
| 3812 | /// algorithmic steps in a data agnostic manner. |
| 3813 | /// |
| 3814 | /// 'shuffleReduceFn' is a pointer to a function that reduces data |
| 3815 | /// of type 'ReduceData' across two OpenMP threads (lanes) in the |
| 3816 | /// same warp. It takes the following arguments as input: |
| 3817 | /// |
| 3818 | /// a. variable of type 'ReduceData' on the calling lane, |
| 3819 | /// b. its lane_id, |
| 3820 | /// c. an offset relative to the current lane_id to generate a |
| 3821 | /// remote_lane_id. The remote lane contains the second |
| 3822 | /// variable of type 'ReduceData' that is to be reduced. |
| 3823 | /// d. an algorithm version parameter determining which reduction |
| 3824 | /// algorithm to use. |
| 3825 | /// |
| 3826 | /// 'shuffleReduceFn' retrieves data from the remote lane using |
| 3827 | /// efficient GPU shuffle intrinsics and reduces, using the |
| 3828 | /// algorithm specified by the 4th parameter, the two operands |
| 3829 | /// element-wise. The result is written to the first operand. |
| 3830 | /// |
| 3831 | /// Different reduction algorithms are implemented in different |
| 3832 | /// runtime functions, all calling 'shuffleReduceFn' to perform |
| 3833 | /// the essential reduction step. Therefore, based on the 4th |
| 3834 | /// parameter, this function behaves slightly differently to |
| 3835 | /// cooperate with the runtime to ensure correctness under |
| 3836 | /// different circumstances. |
| 3837 | /// |
| 3838 | /// 'InterWarpCpyFn' is a pointer to a function that transfers |
| 3839 | /// reduced variables across warps. It tunnels, through CUDA |
| 3840 | /// shared memory, the thread-private data of type 'ReduceData' |
| 3841 | /// from lane 0 of each warp to a lane in the first warp. |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3842 | /// 4. Call the OpenMP runtime on the GPU to reduce across teams. |
| 3843 | /// The last team writes the global reduced value to memory. |
| 3844 | /// |
| 3845 | /// ret = __kmpc_nvptx_teams_reduce_nowait(..., |
| 3846 | /// reduceData, shuffleReduceFn, interWarpCpyFn, |
| 3847 | /// scratchpadCopyFn, loadAndReduceFn) |
| 3848 | /// |
| 3849 | /// 'scratchpadCopyFn' is a helper that stores reduced |
| 3850 | /// data from the team master to a scratchpad array in |
| 3851 | /// global memory. |
| 3852 | /// |
| 3853 | /// 'loadAndReduceFn' is a helper that loads data from |
| 3854 | /// the scratchpad array and reduces it with the input |
| 3855 | /// operand. |
| 3856 | /// |
| 3857 | /// These compiler generated functions hide address |
| 3858 | /// calculation and alignment information from the runtime. |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3859 | /// 5. if ret == 1: |
| 3860 | /// The team master of the last team stores the reduced |
| 3861 | /// result to the globals in memory. |
| 3862 | /// foo += reduceData.foo; bar *= reduceData.bar |
| 3863 | /// |
| 3864 | /// |
| 3865 | /// Warp Reduction Algorithms |
| 3866 | /// |
| 3867 | /// On the warp level, we have three algorithms implemented in the |
| 3868 | /// OpenMP runtime depending on the number of active lanes: |
| 3869 | /// |
| 3870 | /// Full Warp Reduction |
| 3871 | /// |
| 3872 | /// The reduce algorithm within a warp where all lanes are active |
| 3873 | /// is implemented in the runtime as follows: |
| 3874 | /// |
| 3875 | /// full_warp_reduce(void *reduce_data, |
| 3876 | /// kmp_ShuffleReductFctPtr ShuffleReduceFn) { |
| 3877 | /// for (int offset = WARPSIZE/2; offset > 0; offset /= 2) |
| 3878 | /// ShuffleReduceFn(reduce_data, 0, offset, 0); |
| 3879 | /// } |
| 3880 | /// |
| 3881 | /// The algorithm completes in log(2, WARPSIZE) steps. |
| 3882 | /// |
| 3883 | /// 'ShuffleReduceFn' is used here with lane_id set to 0 because it is |
| 3884 | /// not used therefore we save instructions by not retrieving lane_id |
| 3885 | /// from the corresponding special registers. The 4th parameter, which |
| 3886 | /// represents the version of the algorithm being used, is set to 0 to |
| 3887 | /// signify full warp reduction. |
| 3888 | /// |
| 3889 | /// In this version, 'ShuffleReduceFn' behaves, per element, as follows: |
| 3890 | /// |
| 3891 | /// #reduce_elem refers to an element in the local lane's data structure |
| 3892 | /// #remote_elem is retrieved from a remote lane |
| 3893 | /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE); |
| 3894 | /// reduce_elem = reduce_elem REDUCE_OP remote_elem; |
| 3895 | /// |
| 3896 | /// Contiguous Partial Warp Reduction |
| 3897 | /// |
| 3898 | /// This reduce algorithm is used within a warp where only the first |
| 3899 | /// 'n' (n <= WARPSIZE) lanes are active. It is typically used when the |
| 3900 | /// number of OpenMP threads in a parallel region is not a multiple of |
| 3901 | /// WARPSIZE. The algorithm is implemented in the runtime as follows: |
| 3902 | /// |
| 3903 | /// void |
| 3904 | /// contiguous_partial_reduce(void *reduce_data, |
| 3905 | /// kmp_ShuffleReductFctPtr ShuffleReduceFn, |
| 3906 | /// int size, int lane_id) { |
| 3907 | /// int curr_size; |
| 3908 | /// int offset; |
| 3909 | /// curr_size = size; |
| 3910 | /// mask = curr_size/2; |
| 3911 | /// while (offset>0) { |
| 3912 | /// ShuffleReduceFn(reduce_data, lane_id, offset, 1); |
| 3913 | /// curr_size = (curr_size+1)/2; |
| 3914 | /// offset = curr_size/2; |
| 3915 | /// } |
| 3916 | /// } |
| 3917 | /// |
| 3918 | /// In this version, 'ShuffleReduceFn' behaves, per element, as follows: |
| 3919 | /// |
| 3920 | /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE); |
| 3921 | /// if (lane_id < offset) |
| 3922 | /// reduce_elem = reduce_elem REDUCE_OP remote_elem |
| 3923 | /// else |
| 3924 | /// reduce_elem = remote_elem |
| 3925 | /// |
| 3926 | /// This algorithm assumes that the data to be reduced are located in a |
| 3927 | /// contiguous subset of lanes starting from the first. When there is |
| 3928 | /// an odd number of active lanes, the data in the last lane is not |
| 3929 | /// aggregated with any other lane's dat but is instead copied over. |
| 3930 | /// |
| 3931 | /// Dispersed Partial Warp Reduction |
| 3932 | /// |
| 3933 | /// This algorithm is used within a warp when any discontiguous subset of |
| 3934 | /// lanes are active. It is used to implement the reduction operation |
| 3935 | /// across lanes in an OpenMP simd region or in a nested parallel region. |
| 3936 | /// |
| 3937 | /// void |
| 3938 | /// dispersed_partial_reduce(void *reduce_data, |
| 3939 | /// kmp_ShuffleReductFctPtr ShuffleReduceFn) { |
| 3940 | /// int size, remote_id; |
| 3941 | /// int logical_lane_id = number_of_active_lanes_before_me() * 2; |
| 3942 | /// do { |
| 3943 | /// remote_id = next_active_lane_id_right_after_me(); |
| 3944 | /// # the above function returns 0 of no active lane |
| 3945 | /// # is present right after the current lane. |
| 3946 | /// size = number_of_active_lanes_in_this_warp(); |
| 3947 | /// logical_lane_id /= 2; |
| 3948 | /// ShuffleReduceFn(reduce_data, logical_lane_id, |
| 3949 | /// remote_id-1-threadIdx.x, 2); |
| 3950 | /// } while (logical_lane_id % 2 == 0 && size > 1); |
| 3951 | /// } |
| 3952 | /// |
| 3953 | /// There is no assumption made about the initial state of the reduction. |
| 3954 | /// Any number of lanes (>=1) could be active at any position. The reduction |
| 3955 | /// result is returned in the first active lane. |
| 3956 | /// |
| 3957 | /// In this version, 'ShuffleReduceFn' behaves, per element, as follows: |
| 3958 | /// |
| 3959 | /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE); |
| 3960 | /// if (lane_id % 2 == 0 && offset > 0) |
| 3961 | /// reduce_elem = reduce_elem REDUCE_OP remote_elem |
| 3962 | /// else |
| 3963 | /// reduce_elem = remote_elem |
| 3964 | /// |
| 3965 | /// |
| 3966 | /// Intra-Team Reduction |
| 3967 | /// |
| 3968 | /// This function, as implemented in the runtime call |
| 3969 | /// '__kmpc_nvptx_parallel_reduce_nowait', aggregates data across OpenMP |
| 3970 | /// threads in a team. It first reduces within a warp using the |
| 3971 | /// aforementioned algorithms. We then proceed to gather all such |
| 3972 | /// reduced values at the first warp. |
| 3973 | /// |
| 3974 | /// The runtime makes use of the function 'InterWarpCpyFn', which copies |
| 3975 | /// data from each of the "warp master" (zeroth lane of each warp, where |
| 3976 | /// warp-reduced data is held) to the zeroth warp. This step reduces (in |
| 3977 | /// a mathematical sense) the problem of reduction across warp masters in |
| 3978 | /// a block to the problem of warp reduction. |
| 3979 | /// |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3980 | /// |
| 3981 | /// Inter-Team Reduction |
| 3982 | /// |
| 3983 | /// Once a team has reduced its data to a single value, it is stored in |
| 3984 | /// a global scratchpad array. Since each team has a distinct slot, this |
| 3985 | /// can be done without locking. |
| 3986 | /// |
| 3987 | /// The last team to write to the scratchpad array proceeds to reduce the |
| 3988 | /// scratchpad array. One or more workers in the last team use the helper |
| 3989 | /// 'loadAndReduceDataFn' to load and reduce values from the array, i.e., |
| 3990 | /// the k'th worker reduces every k'th element. |
| 3991 | /// |
| 3992 | /// Finally, a call is made to '__kmpc_nvptx_parallel_reduce_nowait' to |
| 3993 | /// reduce across workers and compute a globally reduced value. |
| 3994 | /// |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3995 | void CGOpenMPRuntimeNVPTX::emitReduction( |
| 3996 | CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates, |
| 3997 | ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs, |
| 3998 | ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) { |
| 3999 | if (!CGF.HaveInsertPoint()) |
| 4000 | return; |
| 4001 | |
| 4002 | bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind); |
David L. Jones | 085ec01 | 2018-11-17 04:48:54 +0000 | [diff] [blame] | 4003 | #ifndef NDEBUG |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4004 | bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind); |
David L. Jones | 085ec01 | 2018-11-17 04:48:54 +0000 | [diff] [blame] | 4005 | #endif |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4006 | |
Alexey Bataev | 7b55d2d | 2018-06-18 17:11:45 +0000 | [diff] [blame] | 4007 | if (Options.SimpleReduction) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4008 | assert(!TeamsReduction && !ParallelReduction && |
| 4009 | "Invalid reduction selection in emitReduction."); |
Alexey Bataev | 7b55d2d | 2018-06-18 17:11:45 +0000 | [diff] [blame] | 4010 | CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs, |
| 4011 | ReductionOps, Options); |
| 4012 | return; |
| 4013 | } |
| 4014 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4015 | assert((TeamsReduction || ParallelReduction) && |
| 4016 | "Invalid reduction selection in emitReduction."); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4017 | ASTContext &C = CGM.getContext(); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4018 | |
| 4019 | // 1. Build a list of reduction variables. |
| 4020 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
| 4021 | auto Size = RHSExprs.size(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4022 | for (const Expr *E : Privates) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4023 | if (E->getType()->isVariablyModifiedType()) |
| 4024 | // Reserve place for array size. |
| 4025 | ++Size; |
| 4026 | } |
| 4027 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
| 4028 | QualType ReductionArrayTy = |
| 4029 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 4030 | /*IndexTypeQuals=*/0); |
| 4031 | Address ReductionList = |
| 4032 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
| 4033 | auto IPriv = Privates.begin(); |
| 4034 | unsigned Idx = 0; |
| 4035 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
| 4036 | Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 4037 | CGF.getPointerSize()); |
| 4038 | CGF.Builder.CreateStore( |
| 4039 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4040 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 4041 | Elem); |
| 4042 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
| 4043 | // Store array size. |
| 4044 | ++Idx; |
| 4045 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 4046 | CGF.getPointerSize()); |
| 4047 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 4048 | CGF.getVLASize( |
| 4049 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 4050 | .NumElts, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4051 | CGF.SizeTy, /*isSigned=*/false); |
| 4052 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 4053 | Elem); |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | // 2. Emit reduce_func(). |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4058 | llvm::Value *ReductionFn = emitReductionFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4059 | CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), |
| 4060 | Privates, LHSExprs, RHSExprs, ReductionOps); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4061 | |
| 4062 | // 4. Build res = __kmpc_reduce{_nowait}(<gtid>, <n>, sizeof(RedList), |
| 4063 | // RedList, shuffle_reduce_func, interwarp_copy_func); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4064 | llvm::Value *ThreadId = getThreadID(CGF, Loc); |
| 4065 | llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
| 4066 | llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4067 | ReductionList.getPointer(), CGF.VoidPtrTy); |
| 4068 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4069 | llvm::Value *ShuffleAndReduceFn = emitShuffleAndReduceFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4070 | CGM, Privates, ReductionArrayTy, ReductionFn, Loc); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4071 | llvm::Value *InterWarpCopyFn = |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4072 | emitInterWarpCopyFunction(CGM, Privates, ReductionArrayTy, Loc); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4073 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4074 | llvm::Value *Res; |
| 4075 | if (ParallelReduction) { |
| 4076 | llvm::Value *Args[] = {ThreadId, |
| 4077 | CGF.Builder.getInt32(RHSExprs.size()), |
| 4078 | ReductionArrayTySize, |
| 4079 | RL, |
| 4080 | ShuffleAndReduceFn, |
| 4081 | InterWarpCopyFn}; |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4082 | |
| 4083 | Res = CGF.EmitRuntimeCall( |
| 4084 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_reduce_nowait), |
| 4085 | Args); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4086 | } else { |
| 4087 | assert(TeamsReduction && "expected teams reduction."); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4088 | llvm::Value *ScratchPadCopyFn = |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4089 | emitCopyToScratchpad(CGM, Privates, ReductionArrayTy, Loc); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4090 | llvm::Value *LoadAndReduceFn = emitReduceScratchpadFunction( |
Alexey Bataev | 7cae94e | 2018-01-04 19:45:16 +0000 | [diff] [blame] | 4091 | CGM, Privates, ReductionArrayTy, ReductionFn, Loc); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4092 | |
| 4093 | llvm::Value *Args[] = {ThreadId, |
| 4094 | CGF.Builder.getInt32(RHSExprs.size()), |
| 4095 | ReductionArrayTySize, |
| 4096 | RL, |
| 4097 | ShuffleAndReduceFn, |
| 4098 | InterWarpCopyFn, |
| 4099 | ScratchPadCopyFn, |
| 4100 | LoadAndReduceFn}; |
| 4101 | Res = CGF.EmitRuntimeCall( |
| 4102 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_teams_reduce_nowait), |
| 4103 | Args); |
| 4104 | } |
| 4105 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4106 | // 5. Build if (res == 1) |
| 4107 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.reduction.done"); |
| 4108 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.then"); |
| 4109 | llvm::Value *Cond = CGF.Builder.CreateICmpEQ( |
| 4110 | Res, llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1)); |
| 4111 | CGF.Builder.CreateCondBr(Cond, ThenBB, ExitBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4112 | |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4113 | // 6. Build then branch: where we have reduced values in the master |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4114 | // thread in each team. |
| 4115 | // __kmpc_end_reduce{_nowait}(<gtid>); |
| 4116 | // break; |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4117 | CGF.EmitBlock(ThenBB); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4118 | |
| 4119 | // Add emission of __kmpc_end_reduce{_nowait}(<gtid>); |
| 4120 | llvm::Value *EndArgs[] = {ThreadId}; |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4121 | auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4122 | this](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4123 | auto IPriv = Privates.begin(); |
| 4124 | auto ILHS = LHSExprs.begin(); |
| 4125 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4126 | for (const Expr *E : ReductionOps) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4127 | emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS), |
| 4128 | cast<DeclRefExpr>(*IRHS)); |
| 4129 | ++IPriv; |
| 4130 | ++ILHS; |
| 4131 | ++IRHS; |
| 4132 | } |
| 4133 | }; |
| 4134 | RegionCodeGenTy RCG(CodeGen); |
| 4135 | NVPTXActionTy Action( |
| 4136 | nullptr, llvm::None, |
| 4137 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_reduce_nowait), |
| 4138 | EndArgs); |
| 4139 | RCG.setAction(Action); |
| 4140 | RCG(CGF); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4141 | // There is no need to emit line number for unconditional branch. |
| 4142 | (void)ApplyDebugLocation::CreateEmpty(CGF); |
| 4143 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4144 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4145 | |
| 4146 | const VarDecl * |
| 4147 | CGOpenMPRuntimeNVPTX::translateParameter(const FieldDecl *FD, |
| 4148 | const VarDecl *NativeParam) const { |
| 4149 | if (!NativeParam->getType()->isReferenceType()) |
| 4150 | return NativeParam; |
| 4151 | QualType ArgType = NativeParam->getType(); |
| 4152 | QualifierCollector QC; |
| 4153 | const Type *NonQualTy = QC.strip(ArgType); |
| 4154 | QualType PointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType(); |
| 4155 | if (const auto *Attr = FD->getAttr<OMPCaptureKindAttr>()) { |
| 4156 | if (Attr->getCaptureKind() == OMPC_map) { |
| 4157 | PointeeTy = CGM.getContext().getAddrSpaceQualType(PointeeTy, |
| 4158 | LangAS::opencl_global); |
| 4159 | } |
| 4160 | } |
| 4161 | ArgType = CGM.getContext().getPointerType(PointeeTy); |
| 4162 | QC.addRestrict(); |
| 4163 | enum { NVPTX_local_addr = 5 }; |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4164 | QC.addAddressSpace(getLangASFromTargetAS(NVPTX_local_addr)); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4165 | ArgType = QC.apply(CGM.getContext(), ArgType); |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4166 | if (isa<ImplicitParamDecl>(NativeParam)) |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 4167 | return ImplicitParamDecl::Create( |
| 4168 | CGM.getContext(), /*DC=*/nullptr, NativeParam->getLocation(), |
| 4169 | NativeParam->getIdentifier(), ArgType, ImplicitParamDecl::Other); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 4170 | return ParmVarDecl::Create( |
| 4171 | CGM.getContext(), |
| 4172 | const_cast<DeclContext *>(NativeParam->getDeclContext()), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4173 | NativeParam->getBeginLoc(), NativeParam->getLocation(), |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 4174 | NativeParam->getIdentifier(), ArgType, |
| 4175 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | Address |
| 4179 | CGOpenMPRuntimeNVPTX::getParameterAddress(CodeGenFunction &CGF, |
| 4180 | const VarDecl *NativeParam, |
| 4181 | const VarDecl *TargetParam) const { |
| 4182 | assert(NativeParam != TargetParam && |
| 4183 | NativeParam->getType()->isReferenceType() && |
| 4184 | "Native arg must not be the same as target arg."); |
| 4185 | Address LocalAddr = CGF.GetAddrOfLocalVar(TargetParam); |
| 4186 | QualType NativeParamType = NativeParam->getType(); |
| 4187 | QualifierCollector QC; |
| 4188 | const Type *NonQualTy = QC.strip(NativeParamType); |
| 4189 | QualType NativePointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType(); |
| 4190 | unsigned NativePointeeAddrSpace = |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 4191 | CGF.getContext().getTargetAddressSpace(NativePointeeTy); |
Alexey Bataev | 36f2c4d | 2017-09-13 20:20:59 +0000 | [diff] [blame] | 4192 | QualType TargetTy = TargetParam->getType(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4193 | llvm::Value *TargetAddr = CGF.EmitLoadOfScalar( |
Alexey Bataev | 36f2c4d | 2017-09-13 20:20:59 +0000 | [diff] [blame] | 4194 | LocalAddr, /*Volatile=*/false, TargetTy, SourceLocation()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4195 | // First cast to generic. |
| 4196 | TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4197 | TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo( |
| 4198 | /*AddrSpace=*/0)); |
| 4199 | // Cast from generic to native address space. |
| 4200 | TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 4201 | TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo( |
| 4202 | NativePointeeAddrSpace)); |
| 4203 | Address NativeParamAddr = CGF.CreateMemTemp(NativeParamType); |
| 4204 | CGF.EmitStoreOfScalar(TargetAddr, NativeParamAddr, /*Volatile=*/false, |
Alexey Bataev | 36f2c4d | 2017-09-13 20:20:59 +0000 | [diff] [blame] | 4205 | NativeParamType); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4206 | return NativeParamAddr; |
| 4207 | } |
| 4208 | |
| 4209 | void CGOpenMPRuntimeNVPTX::emitOutlinedFunctionCall( |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4210 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4211 | ArrayRef<llvm::Value *> Args) const { |
| 4212 | SmallVector<llvm::Value *, 4> TargetArgs; |
Alexey Bataev | 07ed94a | 2017-08-15 14:34:04 +0000 | [diff] [blame] | 4213 | TargetArgs.reserve(Args.size()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4214 | auto *FnType = |
| 4215 | cast<llvm::FunctionType>(OutlinedFn->getType()->getPointerElementType()); |
| 4216 | for (unsigned I = 0, E = Args.size(); I < E; ++I) { |
Alexey Bataev | 07ed94a | 2017-08-15 14:34:04 +0000 | [diff] [blame] | 4217 | if (FnType->isVarArg() && FnType->getNumParams() <= I) { |
| 4218 | TargetArgs.append(std::next(Args.begin(), I), Args.end()); |
| 4219 | break; |
| 4220 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4221 | llvm::Type *TargetType = FnType->getParamType(I); |
| 4222 | llvm::Value *NativeArg = Args[I]; |
| 4223 | if (!TargetType->isPointerTy()) { |
| 4224 | TargetArgs.emplace_back(NativeArg); |
| 4225 | continue; |
| 4226 | } |
| 4227 | llvm::Value *TargetArg = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4228 | NativeArg, |
| 4229 | NativeArg->getType()->getPointerElementType()->getPointerTo()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4230 | TargetArgs.emplace_back( |
| 4231 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TargetArg, TargetType)); |
| 4232 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4233 | CGOpenMPRuntime::emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, TargetArgs); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 4234 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4235 | |
| 4236 | /// Emit function which wraps the outline parallel region |
| 4237 | /// and controls the arguments which are passed to this function. |
| 4238 | /// The wrapper ensures that the outlined function is called |
| 4239 | /// with the correct arguments when data is shared. |
| 4240 | llvm::Function *CGOpenMPRuntimeNVPTX::createParallelDataSharingWrapper( |
| 4241 | llvm::Function *OutlinedParallelFn, const OMPExecutableDirective &D) { |
| 4242 | ASTContext &Ctx = CGM.getContext(); |
| 4243 | const auto &CS = *D.getCapturedStmt(OMPD_parallel); |
| 4244 | |
| 4245 | // Create a function that takes as argument the source thread. |
| 4246 | FunctionArgList WrapperArgs; |
| 4247 | QualType Int16QTy = |
| 4248 | Ctx.getIntTypeForBitwidth(/*DestWidth=*/16, /*Signed=*/false); |
| 4249 | QualType Int32QTy = |
| 4250 | Ctx.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4251 | ImplicitParamDecl ParallelLevelArg(Ctx, /*DC=*/nullptr, D.getBeginLoc(), |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4252 | /*Id=*/nullptr, Int16QTy, |
| 4253 | ImplicitParamDecl::Other); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4254 | ImplicitParamDecl WrapperArg(Ctx, /*DC=*/nullptr, D.getBeginLoc(), |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4255 | /*Id=*/nullptr, Int32QTy, |
| 4256 | ImplicitParamDecl::Other); |
| 4257 | WrapperArgs.emplace_back(&ParallelLevelArg); |
| 4258 | WrapperArgs.emplace_back(&WrapperArg); |
| 4259 | |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4260 | const CGFunctionInfo &CGFI = |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4261 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, WrapperArgs); |
| 4262 | |
| 4263 | auto *Fn = llvm::Function::Create( |
| 4264 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
Alexey Bataev | 9ff8083 | 2018-04-16 20:16:21 +0000 | [diff] [blame] | 4265 | Twine(OutlinedParallelFn->getName(), "_wrapper"), &CGM.getModule()); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4266 | CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4267 | Fn->setLinkage(llvm::GlobalValue::InternalLinkage); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4268 | Fn->setDoesNotRecurse(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4269 | |
| 4270 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 4271 | CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, Fn, CGFI, WrapperArgs, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4272 | D.getBeginLoc(), D.getBeginLoc()); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4273 | |
| 4274 | const auto *RD = CS.getCapturedRecordDecl(); |
| 4275 | auto CurField = RD->field_begin(); |
| 4276 | |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 4277 | Address ZeroAddr = CGF.CreateMemTemp( |
| 4278 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 4279 | /*Name*/ ".zero.addr"); |
| 4280 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4281 | // Get the array of arguments. |
| 4282 | SmallVector<llvm::Value *, 8> Args; |
| 4283 | |
Alexey Bataev | b7f3cba | 2018-03-19 17:04:07 +0000 | [diff] [blame] | 4284 | Args.emplace_back(CGF.GetAddrOfLocalVar(&WrapperArg).getPointer()); |
| 4285 | Args.emplace_back(ZeroAddr.getPointer()); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4286 | |
| 4287 | CGBuilderTy &Bld = CGF.Builder; |
| 4288 | auto CI = CS.capture_begin(); |
| 4289 | |
| 4290 | // Use global memory for data sharing. |
| 4291 | // Handle passing of global args to workers. |
| 4292 | Address GlobalArgs = |
| 4293 | CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "global_args"); |
| 4294 | llvm::Value *GlobalArgsPtr = GlobalArgs.getPointer(); |
| 4295 | llvm::Value *DataSharingArgs[] = {GlobalArgsPtr}; |
| 4296 | CGF.EmitRuntimeCall( |
| 4297 | createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_get_shared_variables), |
| 4298 | DataSharingArgs); |
| 4299 | |
| 4300 | // Retrieve the shared variables from the list of references returned |
| 4301 | // by the runtime. Pass the variables to the outlined function. |
Alexey Bataev | 1731421 | 2018-03-20 15:41:05 +0000 | [diff] [blame] | 4302 | Address SharedArgListAddress = Address::invalid(); |
| 4303 | if (CS.capture_size() > 0 || |
| 4304 | isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) { |
| 4305 | SharedArgListAddress = CGF.EmitLoadOfPointer( |
| 4306 | GlobalArgs, CGF.getContext() |
| 4307 | .getPointerType(CGF.getContext().getPointerType( |
| 4308 | CGF.getContext().VoidPtrTy)) |
| 4309 | .castAs<PointerType>()); |
| 4310 | } |
| 4311 | unsigned Idx = 0; |
| 4312 | if (isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) { |
| 4313 | Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx, |
| 4314 | CGF.getPointerSize()); |
| 4315 | Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 4316 | Src, CGF.SizeTy->getPointerTo()); |
| 4317 | llvm::Value *LB = CGF.EmitLoadOfScalar( |
| 4318 | TypedAddress, |
| 4319 | /*Volatile=*/false, |
| 4320 | CGF.getContext().getPointerType(CGF.getContext().getSizeType()), |
| 4321 | cast<OMPLoopDirective>(D).getLowerBoundVariable()->getExprLoc()); |
| 4322 | Args.emplace_back(LB); |
| 4323 | ++Idx; |
| 4324 | Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx, |
| 4325 | CGF.getPointerSize()); |
| 4326 | TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast( |
| 4327 | Src, CGF.SizeTy->getPointerTo()); |
| 4328 | llvm::Value *UB = CGF.EmitLoadOfScalar( |
| 4329 | TypedAddress, |
| 4330 | /*Volatile=*/false, |
| 4331 | CGF.getContext().getPointerType(CGF.getContext().getSizeType()), |
| 4332 | cast<OMPLoopDirective>(D).getUpperBoundVariable()->getExprLoc()); |
| 4333 | Args.emplace_back(UB); |
| 4334 | ++Idx; |
| 4335 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4336 | if (CS.capture_size() > 0) { |
| 4337 | ASTContext &CGFContext = CGF.getContext(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4338 | for (unsigned I = 0, E = CS.capture_size(); I < E; ++I, ++CI, ++CurField) { |
| 4339 | QualType ElemTy = CurField->getType(); |
Alexey Bataev | 1731421 | 2018-03-20 15:41:05 +0000 | [diff] [blame] | 4340 | Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, I + Idx, |
| 4341 | CGF.getPointerSize()); |
| 4342 | Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast( |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4343 | Src, CGF.ConvertTypeForMem(CGFContext.getPointerType(ElemTy))); |
| 4344 | llvm::Value *Arg = CGF.EmitLoadOfScalar(TypedAddress, |
| 4345 | /*Volatile=*/false, |
| 4346 | CGFContext.getPointerType(ElemTy), |
| 4347 | CI->getLocation()); |
Alexey Bataev | 2091ca6 | 2018-04-23 17:33:41 +0000 | [diff] [blame] | 4348 | if (CI->capturesVariableByCopy() && |
| 4349 | !CI->getCapturedVar()->getType()->isAnyPointerType()) { |
Alexey Bataev | 1731421 | 2018-03-20 15:41:05 +0000 | [diff] [blame] | 4350 | Arg = castValueToType(CGF, Arg, ElemTy, CGFContext.getUIntPtrType(), |
| 4351 | CI->getLocation()); |
| 4352 | } |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4353 | Args.emplace_back(Arg); |
| 4354 | } |
| 4355 | } |
| 4356 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4357 | emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedParallelFn, Args); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4358 | CGF.FinishFunction(); |
| 4359 | return Fn; |
| 4360 | } |
| 4361 | |
| 4362 | void CGOpenMPRuntimeNVPTX::emitFunctionProlog(CodeGenFunction &CGF, |
| 4363 | const Decl *D) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 4364 | if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic) |
| 4365 | return; |
| 4366 | |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4367 | assert(D && "Expected function or captured|block decl."); |
| 4368 | assert(FunctionGlobalizedDecls.count(CGF.CurFn) == 0 && |
| 4369 | "Function is registered already."); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4370 | assert((!TeamAndReductions.first || TeamAndReductions.first == D) && |
| 4371 | "Team is set but not processed."); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4372 | const Stmt *Body = nullptr; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4373 | bool NeedToDelayGlobalization = false; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4374 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 4375 | Body = FD->getBody(); |
| 4376 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
| 4377 | Body = BD->getBody(); |
| 4378 | } else if (const auto *CD = dyn_cast<CapturedDecl>(D)) { |
| 4379 | Body = CD->getBody(); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4380 | NeedToDelayGlobalization = CGF.CapturedStmtInfo->getKind() == CR_OpenMP; |
Alexey Bataev | 2adecff | 2018-09-21 14:22:53 +0000 | [diff] [blame] | 4381 | if (NeedToDelayGlobalization && |
| 4382 | getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) |
| 4383 | return; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4384 | } |
| 4385 | if (!Body) |
| 4386 | return; |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4387 | CheckVarsEscapingDeclContext VarChecker(CGF, TeamAndReductions.second); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4388 | VarChecker.Visit(Body); |
Alexey Bataev | ff23bb6 | 2018-10-11 18:30:31 +0000 | [diff] [blame] | 4389 | const RecordDecl *GlobalizedVarsRecord = |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 4390 | VarChecker.getGlobalizedRecord(IsInTTDRegion); |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4391 | TeamAndReductions.first = nullptr; |
| 4392 | TeamAndReductions.second.clear(); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4393 | ArrayRef<const ValueDecl *> EscapedVariableLengthDecls = |
| 4394 | VarChecker.getEscapedVariableLengthDecls(); |
| 4395 | if (!GlobalizedVarsRecord && EscapedVariableLengthDecls.empty()) |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4396 | return; |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4397 | auto I = FunctionGlobalizedDecls.try_emplace(CGF.CurFn).first; |
| 4398 | I->getSecond().MappedParams = |
| 4399 | llvm::make_unique<CodeGenFunction::OMPMapVars>(); |
| 4400 | I->getSecond().GlobalRecord = GlobalizedVarsRecord; |
| 4401 | I->getSecond().EscapedParameters.insert( |
| 4402 | VarChecker.getEscapedParameters().begin(), |
| 4403 | VarChecker.getEscapedParameters().end()); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4404 | I->getSecond().EscapedVariableLengthDecls.append( |
| 4405 | EscapedVariableLengthDecls.begin(), EscapedVariableLengthDecls.end()); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4406 | DeclToAddrMapTy &Data = I->getSecond().LocalVarData; |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4407 | for (const ValueDecl *VD : VarChecker.getEscapedDecls()) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4408 | assert(VD->isCanonicalDecl() && "Expected canonical declaration"); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4409 | const FieldDecl *FD = VarChecker.getFieldForGlobalizedVar(VD); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 4410 | Data.insert(std::make_pair(VD, MappedVarData(FD, IsInTTDRegion))); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4411 | } |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 4412 | if (!IsInTTDRegion && !NeedToDelayGlobalization && !IsInParallelRegion) { |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4413 | CheckVarsEscapingDeclContext VarChecker(CGF, llvm::None); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 4414 | VarChecker.Visit(Body); |
| 4415 | I->getSecond().SecondaryGlobalRecord = |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 4416 | VarChecker.getGlobalizedRecord(/*IsInTTDRegion=*/true); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 4417 | I->getSecond().SecondaryLocalVarData.emplace(); |
| 4418 | DeclToAddrMapTy &Data = I->getSecond().SecondaryLocalVarData.getValue(); |
| 4419 | for (const ValueDecl *VD : VarChecker.getEscapedDecls()) { |
| 4420 | assert(VD->isCanonicalDecl() && "Expected canonical declaration"); |
| 4421 | const FieldDecl *FD = VarChecker.getFieldForGlobalizedVar(VD); |
Alexey Bataev | 4ac58d1 | 2018-10-12 20:19:59 +0000 | [diff] [blame] | 4422 | Data.insert( |
| 4423 | std::make_pair(VD, MappedVarData(FD, /*IsInTTDRegion=*/true))); |
Alexey Bataev | 9bfe91d | 2018-10-12 16:04:20 +0000 | [diff] [blame] | 4424 | } |
| 4425 | } |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4426 | if (!NeedToDelayGlobalization) { |
Alexey Bataev | bd8ff9b | 2018-08-30 18:56:11 +0000 | [diff] [blame] | 4427 | emitGenericVarsProlog(CGF, D->getBeginLoc(), /*WithSPMDCheck=*/true); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4428 | struct GlobalizationScope final : EHScopeStack::Cleanup { |
| 4429 | GlobalizationScope() = default; |
| 4430 | |
| 4431 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 4432 | static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime()) |
Alexey Bataev | bd8ff9b | 2018-08-30 18:56:11 +0000 | [diff] [blame] | 4433 | .emitGenericVarsEpilog(CGF, /*WithSPMDCheck=*/true); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4434 | } |
| 4435 | }; |
| 4436 | CGF.EHStack.pushCleanup<GlobalizationScope>(NormalAndEHCleanup); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | |
| 4440 | Address CGOpenMPRuntimeNVPTX::getAddressOfLocalVariable(CodeGenFunction &CGF, |
| 4441 | const VarDecl *VD) { |
Alexey Bataev | d7ff6d6 | 2018-05-07 14:50:05 +0000 | [diff] [blame] | 4442 | if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic) |
| 4443 | return Address::invalid(); |
| 4444 | |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4445 | VD = VD->getCanonicalDecl(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4446 | auto I = FunctionGlobalizedDecls.find(CGF.CurFn); |
| 4447 | if (I == FunctionGlobalizedDecls.end()) |
| 4448 | return Address::invalid(); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4449 | auto VDI = I->getSecond().LocalVarData.find(VD); |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4450 | if (VDI != I->getSecond().LocalVarData.end()) |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 4451 | return VDI->second.PrivateAddr; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4452 | if (VD->hasAttrs()) { |
| 4453 | for (specific_attr_iterator<OMPReferencedVarAttr> IT(VD->attr_begin()), |
| 4454 | E(VD->attr_end()); |
| 4455 | IT != E; ++IT) { |
| 4456 | auto VDI = I->getSecond().LocalVarData.find( |
| 4457 | cast<VarDecl>(cast<DeclRefExpr>(IT->getRef())->getDecl()) |
| 4458 | ->getCanonicalDecl()); |
| 4459 | if (VDI != I->getSecond().LocalVarData.end()) |
Alexey Bataev | 9ea3c38 | 2018-10-09 14:49:00 +0000 | [diff] [blame] | 4460 | return VDI->second.PrivateAddr; |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4461 | } |
| 4462 | } |
| 4463 | return Address::invalid(); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | void CGOpenMPRuntimeNVPTX::functionFinished(CodeGenFunction &CGF) { |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 4467 | FunctionGlobalizedDecls.erase(CGF.CurFn); |
| 4468 | CGOpenMPRuntime::functionFinished(CGF); |
| 4469 | } |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 4470 | |
| 4471 | void CGOpenMPRuntimeNVPTX::getDefaultDistScheduleAndChunk( |
| 4472 | CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 4473 | OpenMPDistScheduleClauseKind &ScheduleKind, |
| 4474 | llvm::Value *&Chunk) const { |
| 4475 | if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD) { |
| 4476 | ScheduleKind = OMPC_DIST_SCHEDULE_static; |
| 4477 | Chunk = CGF.EmitScalarConversion(getNVPTXNumThreads(CGF), |
| 4478 | CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 4479 | S.getIterationVariable()->getType(), S.getBeginLoc()); |
Gheorghe-Teodor Bercea | 669dbde | 2018-10-29 15:23:23 +0000 | [diff] [blame] | 4480 | return; |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 4481 | } |
Gheorghe-Teodor Bercea | 669dbde | 2018-10-29 15:23:23 +0000 | [diff] [blame] | 4482 | CGOpenMPRuntime::getDefaultDistScheduleAndChunk( |
| 4483 | CGF, S, ScheduleKind, Chunk); |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 4484 | } |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 4485 | |
| 4486 | void CGOpenMPRuntimeNVPTX::getDefaultScheduleAndChunk( |
| 4487 | CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 4488 | OpenMPScheduleClauseKind &ScheduleKind, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 4489 | const Expr *&ChunkExpr) const { |
Gheorghe-Teodor Bercea | 669dbde | 2018-10-29 15:23:23 +0000 | [diff] [blame] | 4490 | ScheduleKind = OMPC_SCHEDULE_static; |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 4491 | // Chunk size is 1 in this case. |
| 4492 | llvm::APInt ChunkSize(32, 1); |
| 4493 | ChunkExpr = IntegerLiteral::Create(CGF.getContext(), ChunkSize, |
| 4494 | CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 4495 | SourceLocation()); |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 4496 | } |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4497 | |
| 4498 | void CGOpenMPRuntimeNVPTX::adjustTargetSpecificDataForLambdas( |
| 4499 | CodeGenFunction &CGF, const OMPExecutableDirective &D) const { |
| 4500 | assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) && |
| 4501 | " Expected target-based directive."); |
| 4502 | const CapturedStmt *CS = D.getCapturedStmt(OMPD_target); |
| 4503 | for (const CapturedStmt::Capture &C : CS->captures()) { |
| 4504 | // Capture variables captured by reference in lambdas for target-based |
| 4505 | // directives. |
| 4506 | if (!C.capturesVariable()) |
| 4507 | continue; |
| 4508 | const VarDecl *VD = C.getCapturedVar(); |
| 4509 | const auto *RD = VD->getType() |
| 4510 | .getCanonicalType() |
| 4511 | .getNonReferenceType() |
| 4512 | ->getAsCXXRecordDecl(); |
| 4513 | if (!RD || !RD->isLambda()) |
| 4514 | continue; |
| 4515 | Address VDAddr = CGF.GetAddrOfLocalVar(VD); |
| 4516 | LValue VDLVal; |
| 4517 | if (VD->getType().getCanonicalType()->isReferenceType()) |
| 4518 | VDLVal = CGF.EmitLoadOfReferenceLValue(VDAddr, VD->getType()); |
| 4519 | else |
| 4520 | VDLVal = CGF.MakeAddrLValue( |
| 4521 | VDAddr, VD->getType().getCanonicalType().getNonReferenceType()); |
| 4522 | llvm::DenseMap<const VarDecl *, FieldDecl *> Captures; |
| 4523 | FieldDecl *ThisCapture = nullptr; |
| 4524 | RD->getCaptureFields(Captures, ThisCapture); |
| 4525 | if (ThisCapture && CGF.CapturedStmtInfo->isCXXThisExprCaptured()) { |
| 4526 | LValue ThisLVal = |
| 4527 | CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture); |
| 4528 | llvm::Value *CXXThis = CGF.LoadCXXThis(); |
| 4529 | CGF.EmitStoreOfScalar(CXXThis, ThisLVal); |
| 4530 | } |
| 4531 | for (const LambdaCapture &LC : RD->captures()) { |
| 4532 | if (LC.getCaptureKind() != LCK_ByRef) |
| 4533 | continue; |
| 4534 | const VarDecl *VD = LC.getCapturedVar(); |
| 4535 | if (!CS->capturesVariable(VD)) |
| 4536 | continue; |
| 4537 | auto It = Captures.find(VD); |
| 4538 | assert(It != Captures.end() && "Found lambda capture without field."); |
| 4539 | LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second); |
| 4540 | Address VDAddr = CGF.GetAddrOfLocalVar(VD); |
| 4541 | if (VD->getType().getCanonicalType()->isReferenceType()) |
| 4542 | VDAddr = CGF.EmitLoadOfReferenceLValue(VDAddr, |
| 4543 | VD->getType().getCanonicalType()) |
| 4544 | .getAddress(); |
| 4545 | CGF.EmitStoreOfScalar(VDAddr.getPointer(), VarLVal); |
| 4546 | } |
| 4547 | } |
| 4548 | } |
| 4549 | |
Patrick Lyster | 8f7f586 | 2018-11-19 15:09:33 +0000 | [diff] [blame] | 4550 | // Get current CudaArch and ignore any unknown values |
| 4551 | static CudaArch getCudaArch(CodeGenModule &CGM) { |
| 4552 | if (!CGM.getTarget().hasFeature("ptx")) |
| 4553 | return CudaArch::UNKNOWN; |
| 4554 | llvm::StringMap<bool> Features; |
| 4555 | CGM.getTarget().initFeatureMap(Features, CGM.getDiags(), |
| 4556 | CGM.getTarget().getTargetOpts().CPU, |
| 4557 | CGM.getTarget().getTargetOpts().Features); |
| 4558 | for (const auto &Feature : Features) { |
| 4559 | if (Feature.getValue()) { |
| 4560 | CudaArch Arch = StringToCudaArch(Feature.getKey()); |
| 4561 | if (Arch != CudaArch::UNKNOWN) |
| 4562 | return Arch; |
| 4563 | } |
| 4564 | } |
| 4565 | return CudaArch::UNKNOWN; |
| 4566 | } |
| 4567 | |
| 4568 | /// Check to see if target architecture supports unified addressing which is |
| 4569 | /// a restriction for OpenMP requires clause "unified_shared_memory". |
| 4570 | void CGOpenMPRuntimeNVPTX::checkArchForUnifiedAddressing( |
| 4571 | CodeGenModule &CGM, const OMPRequiresDecl *D) const { |
| 4572 | for (const OMPClause *Clause : D->clauselists()) { |
| 4573 | if (Clause->getClauseKind() == OMPC_unified_shared_memory) { |
| 4574 | switch (getCudaArch(CGM)) { |
| 4575 | case CudaArch::SM_20: |
| 4576 | case CudaArch::SM_21: |
| 4577 | case CudaArch::SM_30: |
| 4578 | case CudaArch::SM_32: |
| 4579 | case CudaArch::SM_35: |
| 4580 | case CudaArch::SM_37: |
| 4581 | case CudaArch::SM_50: |
| 4582 | case CudaArch::SM_52: |
| 4583 | case CudaArch::SM_53: |
| 4584 | case CudaArch::SM_60: |
| 4585 | case CudaArch::SM_61: |
| 4586 | case CudaArch::SM_62: |
| 4587 | CGM.Error(Clause->getBeginLoc(), |
| 4588 | "Target architecture does not support unified addressing"); |
| 4589 | return; |
| 4590 | case CudaArch::SM_70: |
| 4591 | case CudaArch::SM_72: |
| 4592 | case CudaArch::SM_75: |
| 4593 | case CudaArch::GFX600: |
| 4594 | case CudaArch::GFX601: |
| 4595 | case CudaArch::GFX700: |
| 4596 | case CudaArch::GFX701: |
| 4597 | case CudaArch::GFX702: |
| 4598 | case CudaArch::GFX703: |
| 4599 | case CudaArch::GFX704: |
| 4600 | case CudaArch::GFX801: |
| 4601 | case CudaArch::GFX802: |
| 4602 | case CudaArch::GFX803: |
| 4603 | case CudaArch::GFX810: |
| 4604 | case CudaArch::GFX900: |
| 4605 | case CudaArch::GFX902: |
| 4606 | case CudaArch::GFX904: |
| 4607 | case CudaArch::GFX906: |
| 4608 | case CudaArch::GFX909: |
| 4609 | case CudaArch::UNKNOWN: |
| 4610 | break; |
| 4611 | case CudaArch::LAST: |
| 4612 | llvm_unreachable("Unexpected Cuda arch."); |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4618 | /// Get number of SMs and number of blocks per SM. |
| 4619 | static std::pair<unsigned, unsigned> getSMsBlocksPerSM(CodeGenModule &CGM) { |
| 4620 | std::pair<unsigned, unsigned> Data; |
| 4621 | if (CGM.getLangOpts().OpenMPCUDANumSMs) |
| 4622 | Data.first = CGM.getLangOpts().OpenMPCUDANumSMs; |
| 4623 | if (CGM.getLangOpts().OpenMPCUDABlocksPerSM) |
| 4624 | Data.second = CGM.getLangOpts().OpenMPCUDABlocksPerSM; |
| 4625 | if (Data.first && Data.second) |
| 4626 | return Data; |
Patrick Lyster | 8f7f586 | 2018-11-19 15:09:33 +0000 | [diff] [blame] | 4627 | switch (getCudaArch(CGM)) { |
| 4628 | case CudaArch::SM_20: |
| 4629 | case CudaArch::SM_21: |
| 4630 | case CudaArch::SM_30: |
| 4631 | case CudaArch::SM_32: |
| 4632 | case CudaArch::SM_35: |
| 4633 | case CudaArch::SM_37: |
| 4634 | case CudaArch::SM_50: |
| 4635 | case CudaArch::SM_52: |
| 4636 | case CudaArch::SM_53: |
| 4637 | return {16, 16}; |
| 4638 | case CudaArch::SM_60: |
| 4639 | case CudaArch::SM_61: |
| 4640 | case CudaArch::SM_62: |
| 4641 | return {56, 32}; |
| 4642 | case CudaArch::SM_70: |
| 4643 | case CudaArch::SM_72: |
| 4644 | case CudaArch::SM_75: |
| 4645 | return {84, 32}; |
| 4646 | case CudaArch::GFX600: |
| 4647 | case CudaArch::GFX601: |
| 4648 | case CudaArch::GFX700: |
| 4649 | case CudaArch::GFX701: |
| 4650 | case CudaArch::GFX702: |
| 4651 | case CudaArch::GFX703: |
| 4652 | case CudaArch::GFX704: |
| 4653 | case CudaArch::GFX801: |
| 4654 | case CudaArch::GFX802: |
| 4655 | case CudaArch::GFX803: |
| 4656 | case CudaArch::GFX810: |
| 4657 | case CudaArch::GFX900: |
| 4658 | case CudaArch::GFX902: |
| 4659 | case CudaArch::GFX904: |
| 4660 | case CudaArch::GFX906: |
| 4661 | case CudaArch::GFX909: |
| 4662 | case CudaArch::UNKNOWN: |
| 4663 | break; |
| 4664 | case CudaArch::LAST: |
| 4665 | llvm_unreachable("Unexpected Cuda arch."); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4666 | } |
| 4667 | llvm_unreachable("Unexpected NVPTX target without ptx feature."); |
| 4668 | } |
| 4669 | |
| 4670 | void CGOpenMPRuntimeNVPTX::clear() { |
| 4671 | if (!GlobalizedRecords.empty()) { |
| 4672 | ASTContext &C = CGM.getContext(); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4673 | llvm::SmallVector<const GlobalPtrSizeRecsTy *, 4> GlobalRecs; |
| 4674 | llvm::SmallVector<const GlobalPtrSizeRecsTy *, 4> SharedRecs; |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4675 | RecordDecl *StaticRD = C.buildImplicitRecord( |
| 4676 | "_openmp_static_memory_type_$_", RecordDecl::TagKind::TTK_Union); |
| 4677 | StaticRD->startDefinition(); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4678 | RecordDecl *SharedStaticRD = C.buildImplicitRecord( |
| 4679 | "_shared_openmp_static_memory_type_$_", RecordDecl::TagKind::TTK_Union); |
| 4680 | SharedStaticRD->startDefinition(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4681 | for (const GlobalPtrSizeRecsTy &Records : GlobalizedRecords) { |
| 4682 | if (Records.Records.empty()) |
| 4683 | continue; |
| 4684 | unsigned Size = 0; |
| 4685 | unsigned RecAlignment = 0; |
| 4686 | for (const RecordDecl *RD : Records.Records) { |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4687 | QualType RDTy = C.getRecordType(RD); |
| 4688 | unsigned Alignment = C.getTypeAlignInChars(RDTy).getQuantity(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4689 | RecAlignment = std::max(RecAlignment, Alignment); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4690 | unsigned RecSize = C.getTypeSizeInChars(RDTy).getQuantity(); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4691 | Size = |
| 4692 | llvm::alignTo(llvm::alignTo(Size, Alignment) + RecSize, Alignment); |
| 4693 | } |
| 4694 | Size = llvm::alignTo(Size, RecAlignment); |
| 4695 | llvm::APInt ArySize(/*numBits=*/64, Size); |
| 4696 | QualType SubTy = C.getConstantArrayType( |
| 4697 | C.CharTy, ArySize, ArrayType::Normal, /*IndexTypeQuals=*/0); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4698 | const bool UseSharedMemory = Size <= SharedMemorySize; |
| 4699 | auto *Field = |
| 4700 | FieldDecl::Create(C, UseSharedMemory ? SharedStaticRD : StaticRD, |
| 4701 | SourceLocation(), SourceLocation(), nullptr, SubTy, |
| 4702 | C.getTrivialTypeSourceInfo(SubTy, SourceLocation()), |
| 4703 | /*BW=*/nullptr, /*Mutable=*/false, |
| 4704 | /*InitStyle=*/ICIS_NoInit); |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4705 | Field->setAccess(AS_public); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4706 | if (UseSharedMemory) { |
| 4707 | SharedStaticRD->addDecl(Field); |
| 4708 | SharedRecs.push_back(&Records); |
| 4709 | } else { |
| 4710 | StaticRD->addDecl(Field); |
| 4711 | GlobalRecs.push_back(&Records); |
| 4712 | } |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4713 | Records.RecSize->setInitializer(llvm::ConstantInt::get(CGM.SizeTy, Size)); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4714 | Records.UseSharedMemory->setInitializer( |
| 4715 | llvm::ConstantInt::get(CGM.Int16Ty, UseSharedMemory ? 1 : 0)); |
| 4716 | } |
| 4717 | SharedStaticRD->completeDefinition(); |
| 4718 | if (!SharedStaticRD->field_empty()) { |
| 4719 | QualType StaticTy = C.getRecordType(SharedStaticRD); |
| 4720 | llvm::Type *LLVMStaticTy = CGM.getTypes().ConvertTypeForMem(StaticTy); |
| 4721 | auto *GV = new llvm::GlobalVariable( |
| 4722 | CGM.getModule(), LLVMStaticTy, |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4723 | /*isConstant=*/false, llvm::GlobalValue::CommonLinkage, |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4724 | llvm::Constant::getNullValue(LLVMStaticTy), |
| 4725 | "_openmp_shared_static_glob_rd_$_", /*InsertBefore=*/nullptr, |
| 4726 | llvm::GlobalValue::NotThreadLocal, |
| 4727 | C.getTargetAddressSpace(LangAS::cuda_shared)); |
| 4728 | auto *Replacement = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
| 4729 | GV, CGM.VoidPtrTy); |
| 4730 | for (const GlobalPtrSizeRecsTy *Rec : SharedRecs) { |
| 4731 | Rec->Buffer->replaceAllUsesWith(Replacement); |
| 4732 | Rec->Buffer->eraseFromParent(); |
| 4733 | } |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4734 | } |
| 4735 | StaticRD->completeDefinition(); |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4736 | if (!StaticRD->field_empty()) { |
| 4737 | QualType StaticTy = C.getRecordType(StaticRD); |
| 4738 | std::pair<unsigned, unsigned> SMsBlockPerSM = getSMsBlocksPerSM(CGM); |
| 4739 | llvm::APInt Size1(32, SMsBlockPerSM.second); |
| 4740 | QualType Arr1Ty = |
| 4741 | C.getConstantArrayType(StaticTy, Size1, ArrayType::Normal, |
| 4742 | /*IndexTypeQuals=*/0); |
| 4743 | llvm::APInt Size2(32, SMsBlockPerSM.first); |
| 4744 | QualType Arr2Ty = C.getConstantArrayType(Arr1Ty, Size2, ArrayType::Normal, |
| 4745 | /*IndexTypeQuals=*/0); |
| 4746 | llvm::Type *LLVMArr2Ty = CGM.getTypes().ConvertTypeForMem(Arr2Ty); |
| 4747 | auto *GV = new llvm::GlobalVariable( |
| 4748 | CGM.getModule(), LLVMArr2Ty, |
Alexey Bataev | f2f39be | 2018-11-16 19:38:21 +0000 | [diff] [blame] | 4749 | /*isConstant=*/false, llvm::GlobalValue::CommonLinkage, |
Alexey Bataev | 09c9eea | 2018-11-09 16:18:04 +0000 | [diff] [blame] | 4750 | llvm::Constant::getNullValue(LLVMArr2Ty), |
| 4751 | "_openmp_static_glob_rd_$_"); |
| 4752 | auto *Replacement = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
| 4753 | GV, CGM.VoidPtrTy); |
| 4754 | for (const GlobalPtrSizeRecsTy *Rec : GlobalRecs) { |
| 4755 | Rec->Buffer->replaceAllUsesWith(Replacement); |
| 4756 | Rec->Buffer->eraseFromParent(); |
| 4757 | } |
| 4758 | } |
Alexey Bataev | e409018 | 2018-11-02 14:54:07 +0000 | [diff] [blame] | 4759 | } |
| 4760 | CGOpenMPRuntime::clear(); |
| 4761 | } |