Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntime.cpp - Interface to OpenMP 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. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 14 | #include "CGCXXABI.h" |
| 15 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 16 | #include "CGOpenMPRuntime.h" |
| 17 | #include "CodeGenFunction.h" |
| 18 | #include "clang/AST/Decl.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/GlobalValue.h" |
| 25 | #include "llvm/IR/Value.h" |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Format.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 28 | #include <cassert> |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame] | 33 | namespace { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 34 | /// \brief Base class for handling code generation inside OpenMP regions. |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 35 | class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo { |
| 36 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 37 | /// \brief Kinds of OpenMP regions used in codegen. |
| 38 | enum CGOpenMPRegionKind { |
| 39 | /// \brief Region with outlined function for standalone 'parallel' |
| 40 | /// directive. |
| 41 | ParallelOutlinedRegion, |
| 42 | /// \brief Region with outlined function for standalone 'task' directive. |
| 43 | TaskOutlinedRegion, |
| 44 | /// \brief Region for constructs that do not require function outlining, |
| 45 | /// like 'for', 'sections', 'atomic' etc. directives. |
| 46 | InlinedRegion, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 47 | /// \brief Region with outlined function for standalone 'target' directive. |
| 48 | TargetRegion, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 49 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 50 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 51 | CGOpenMPRegionInfo(const CapturedStmt &CS, |
| 52 | const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 53 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 54 | bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 55 | : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 56 | CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 57 | |
| 58 | CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 59 | const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind, |
| 60 | bool HasCancel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 61 | : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen), |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 62 | Kind(Kind), HasCancel(HasCancel) {} |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 63 | |
| 64 | /// \brief Get a variable or parameter for storing global thread id |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 65 | /// inside OpenMP construct. |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 66 | virtual const VarDecl *getThreadIDVariable() const = 0; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 67 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 68 | /// \brief Emit the captured statement body. |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 69 | void EmitBody(CodeGenFunction &CGF, const Stmt *S) override; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 70 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 71 | /// \brief Get an LValue for the current ThreadID variable. |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 72 | /// \return LValue for thread id variable. This LValue always has type int32*. |
| 73 | virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 74 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 75 | CGOpenMPRegionKind getRegionKind() const { return RegionKind; } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 76 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 77 | OpenMPDirectiveKind getDirectiveKind() const { return Kind; } |
| 78 | |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 79 | bool hasCancel() const { return HasCancel; } |
| 80 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 81 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 82 | return Info->getKind() == CR_OpenMP; |
| 83 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 84 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 85 | protected: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 86 | CGOpenMPRegionKind RegionKind; |
Hans Wennborg | 45c7439 | 2016-01-12 20:54:36 +0000 | [diff] [blame] | 87 | RegionCodeGenTy CodeGen; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 88 | OpenMPDirectiveKind Kind; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 89 | bool HasCancel; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 90 | }; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 91 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 92 | /// \brief API for captured statement code generation in OpenMP constructs. |
| 93 | class CGOpenMPOutlinedRegionInfo : public CGOpenMPRegionInfo { |
| 94 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 95 | CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 96 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 97 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 98 | : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind, |
| 99 | HasCancel), |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 100 | ThreadIDVar(ThreadIDVar) { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 101 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 102 | } |
| 103 | /// \brief Get a variable or parameter for storing global thread id |
| 104 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 105 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 106 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 107 | /// \brief Get the name of the capture helper. |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame] | 108 | StringRef getHelperName() const override { return ".omp_outlined."; } |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 109 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 110 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 111 | return CGOpenMPRegionInfo::classof(Info) && |
| 112 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 113 | ParallelOutlinedRegion; |
| 114 | } |
| 115 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 116 | private: |
| 117 | /// \brief A variable or parameter storing global thread id for OpenMP |
| 118 | /// constructs. |
| 119 | const VarDecl *ThreadIDVar; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 122 | /// \brief API for captured statement code generation in OpenMP constructs. |
| 123 | class CGOpenMPTaskOutlinedRegionInfo : public CGOpenMPRegionInfo { |
| 124 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 125 | CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS, |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 126 | const VarDecl *ThreadIDVar, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 127 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 128 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 129 | : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel), |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 130 | ThreadIDVar(ThreadIDVar) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 131 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 132 | } |
| 133 | /// \brief Get a variable or parameter for storing global thread id |
| 134 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 135 | const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 136 | |
| 137 | /// \brief Get an LValue for the current ThreadID variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 138 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 139 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 140 | /// \brief Get the name of the capture helper. |
| 141 | StringRef getHelperName() const override { return ".omp_outlined."; } |
| 142 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 143 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 144 | return CGOpenMPRegionInfo::classof(Info) && |
| 145 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == |
| 146 | TaskOutlinedRegion; |
| 147 | } |
| 148 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 149 | private: |
| 150 | /// \brief A variable or parameter storing global thread id for OpenMP |
| 151 | /// constructs. |
| 152 | const VarDecl *ThreadIDVar; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 155 | /// \brief API for inlined captured statement code generation in OpenMP |
| 156 | /// constructs. |
| 157 | class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo { |
| 158 | public: |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 159 | CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 160 | const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 161 | OpenMPDirectiveKind Kind, bool HasCancel) |
| 162 | : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel), |
| 163 | OldCSI(OldCSI), |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 164 | OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {} |
| 165 | // \brief Retrieve the value of the context parameter. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 166 | llvm::Value *getContextValue() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 167 | if (OuterRegionInfo) |
| 168 | return OuterRegionInfo->getContextValue(); |
| 169 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 170 | } |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 171 | void setContextValue(llvm::Value *V) override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 172 | if (OuterRegionInfo) { |
| 173 | OuterRegionInfo->setContextValue(V); |
| 174 | return; |
| 175 | } |
| 176 | llvm_unreachable("No context value for inlined OpenMP region"); |
| 177 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 178 | /// \brief Lookup the captured field decl for a variable. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 179 | const FieldDecl *lookup(const VarDecl *VD) const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 180 | if (OuterRegionInfo) |
| 181 | return OuterRegionInfo->lookup(VD); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 182 | // If there is no outer outlined region,no need to lookup in a list of |
| 183 | // captured variables, we can use the original one. |
| 184 | return nullptr; |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 185 | } |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 186 | FieldDecl *getThisFieldDecl() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 187 | if (OuterRegionInfo) |
| 188 | return OuterRegionInfo->getThisFieldDecl(); |
| 189 | return nullptr; |
| 190 | } |
| 191 | /// \brief Get a variable or parameter for storing global thread id |
| 192 | /// inside OpenMP construct. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 193 | const VarDecl *getThreadIDVariable() const override { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 194 | if (OuterRegionInfo) |
| 195 | return OuterRegionInfo->getThreadIDVariable(); |
| 196 | return nullptr; |
| 197 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 198 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 199 | /// \brief Get the name of the capture helper. |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 200 | StringRef getHelperName() const override { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 201 | if (auto *OuterRegionInfo = getOldCSI()) |
| 202 | return OuterRegionInfo->getHelperName(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 203 | llvm_unreachable("No helper name for inlined OpenMP construct"); |
| 204 | } |
| 205 | |
| 206 | CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; } |
| 207 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 208 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 209 | return CGOpenMPRegionInfo::classof(Info) && |
| 210 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion; |
| 211 | } |
| 212 | |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 213 | private: |
| 214 | /// \brief CodeGen info about outer OpenMP region. |
| 215 | CodeGenFunction::CGCapturedStmtInfo *OldCSI; |
| 216 | CGOpenMPRegionInfo *OuterRegionInfo; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 217 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 218 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 219 | /// \brief API for captured statement code generation in OpenMP target |
| 220 | /// constructs. For this captures, implicit parameters are used instead of the |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 221 | /// captured fields. The name of the target region has to be unique in a given |
| 222 | /// application so it is provided by the client, because only the client has |
| 223 | /// the information to generate that. |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 224 | class CGOpenMPTargetRegionInfo : public CGOpenMPRegionInfo { |
| 225 | public: |
| 226 | CGOpenMPTargetRegionInfo(const CapturedStmt &CS, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 227 | const RegionCodeGenTy &CodeGen, StringRef HelperName) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 228 | : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 229 | /*HasCancel=*/false), |
| 230 | HelperName(HelperName) {} |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 231 | |
| 232 | /// \brief This is unused for target regions because each starts executing |
| 233 | /// with a single thread. |
| 234 | const VarDecl *getThreadIDVariable() const override { return nullptr; } |
| 235 | |
| 236 | /// \brief Get the name of the capture helper. |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 237 | StringRef getHelperName() const override { return HelperName; } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 238 | |
| 239 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 240 | return CGOpenMPRegionInfo::classof(Info) && |
| 241 | cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion; |
| 242 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 243 | |
| 244 | private: |
| 245 | StringRef HelperName; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 248 | /// \brief RAII for emitting code of OpenMP constructs. |
| 249 | class InlinedOpenMPRegionRAII { |
| 250 | CodeGenFunction &CGF; |
| 251 | |
| 252 | public: |
| 253 | /// \brief Constructs region for combined constructs. |
| 254 | /// \param CodeGen Code generation sequence for combined directives. Includes |
| 255 | /// a list of functions used for code generation of implicitly inlined |
| 256 | /// regions. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 257 | InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 258 | OpenMPDirectiveKind Kind, bool HasCancel) |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 259 | : CGF(CGF) { |
| 260 | // Start emission for the construct. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 261 | CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo( |
| 262 | CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 263 | } |
| 264 | ~InlinedOpenMPRegionRAII() { |
| 265 | // Restore original CapturedStmtInfo only if we're done with code emission. |
| 266 | auto *OldCSI = |
| 267 | cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI(); |
| 268 | delete CGF.CapturedStmtInfo; |
| 269 | CGF.CapturedStmtInfo = OldCSI; |
| 270 | } |
| 271 | }; |
| 272 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 273 | } // anonymous namespace |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 274 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 275 | static LValue emitLoadOfPointerLValue(CodeGenFunction &CGF, Address PtrAddr, |
| 276 | QualType Ty) { |
| 277 | AlignmentSource Source; |
| 278 | CharUnits Align = CGF.getNaturalPointeeTypeAlignment(Ty, &Source); |
| 279 | return CGF.MakeAddrLValue(Address(CGF.Builder.CreateLoad(PtrAddr), Align), |
| 280 | Ty->getPointeeType(), Source); |
| 281 | } |
| 282 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 283 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 284 | return emitLoadOfPointerLValue(CGF, |
| 285 | CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 286 | getThreadIDVariable()->getType()); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 289 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 290 | if (!CGF.HaveInsertPoint()) |
| 291 | return; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 292 | // 1.2.2 OpenMP Language Terminology |
| 293 | // Structured block - An executable statement with a single entry at the |
| 294 | // top and a single exit at the bottom. |
| 295 | // The point of exit cannot be a branch out of the structured block. |
| 296 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 297 | CGF.EHStack.pushTerminate(); |
| 298 | { |
| 299 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 300 | CodeGen(CGF); |
| 301 | } |
| 302 | CGF.EHStack.popTerminate(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 305 | LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue( |
| 306 | CodeGenFunction &CGF) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 307 | return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()), |
| 308 | getThreadIDVariable()->getType(), |
| 309 | AlignmentSource::Decl); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 312 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 313 | : CGM(CGM), DefaultOpenMPPSource(nullptr), KmpRoutineEntryPtrTy(nullptr), |
| 314 | OffloadEntriesInfoManager(CGM) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 315 | IdentTy = llvm::StructType::create( |
| 316 | "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */, |
| 317 | CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */, |
Alexander Musman | fdfa855 | 2014-09-11 08:10:57 +0000 | [diff] [blame] | 318 | CGM.Int8PtrTy /* psource */, nullptr); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 319 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 320 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 321 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 322 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 323 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 324 | |
| 325 | loadOffloadInfoMetadata(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Alexey Bataev | 9179755 | 2015-03-18 04:13:55 +0000 | [diff] [blame] | 328 | void CGOpenMPRuntime::clear() { |
| 329 | InternalVars.clear(); |
| 330 | } |
| 331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 332 | // Layout information for ident_t. |
| 333 | static CharUnits getIdentAlign(CodeGenModule &CGM) { |
| 334 | return CGM.getPointerAlign(); |
| 335 | } |
| 336 | static CharUnits getIdentSize(CodeGenModule &CGM) { |
| 337 | assert((4 * CGM.getPointerSize()).isMultipleOf(CGM.getPointerAlign())); |
| 338 | return CharUnits::fromQuantity(16) + CGM.getPointerSize(); |
| 339 | } |
| 340 | static CharUnits getOffsetOfIdentField(CGOpenMPRuntime::IdentFieldIndex Field) { |
| 341 | // All the fields except the last are i32, so this works beautifully. |
| 342 | return unsigned(Field) * CharUnits::fromQuantity(4); |
| 343 | } |
| 344 | static Address createIdentFieldGEP(CodeGenFunction &CGF, Address Addr, |
| 345 | CGOpenMPRuntime::IdentFieldIndex Field, |
| 346 | const llvm::Twine &Name = "") { |
| 347 | auto Offset = getOffsetOfIdentField(Field); |
| 348 | return CGF.Builder.CreateStructGEP(Addr, Field, Offset, Name); |
| 349 | } |
| 350 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 351 | llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction( |
| 352 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 353 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 354 | assert(ThreadIDVar->getType()->isPointerType() && |
| 355 | "thread id variable must be of type kmp_int32 *"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 356 | const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt()); |
| 357 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 358 | bool HasCancel = false; |
| 359 | if (auto *OPD = dyn_cast<OMPParallelDirective>(&D)) |
| 360 | HasCancel = OPD->hasCancel(); |
| 361 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D)) |
| 362 | HasCancel = OPSD->hasCancel(); |
| 363 | else if (auto *OPFD = dyn_cast<OMPParallelForDirective>(&D)) |
| 364 | HasCancel = OPFD->hasCancel(); |
| 365 | CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind, |
| 366 | HasCancel); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 367 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 368 | return CGF.GenerateOpenMPCapturedStmtFunction(*CS); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 371 | llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction( |
| 372 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
| 373 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 374 | assert(!ThreadIDVar->getType()->isPointerType() && |
| 375 | "thread id variable must be of type kmp_int32 for tasks"); |
| 376 | auto *CS = cast<CapturedStmt>(D.getAssociatedStmt()); |
| 377 | CodeGenFunction CGF(CGM, true); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 378 | CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 379 | InnermostKind, |
| 380 | cast<OMPTaskDirective>(D).hasCancel()); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 381 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 382 | return CGF.GenerateCapturedStmtFunction(*CS); |
| 383 | } |
| 384 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 385 | Address CGOpenMPRuntime::getOrCreateDefaultLocation(OpenMPLocationFlags Flags) { |
| 386 | CharUnits Align = getIdentAlign(CGM); |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 387 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 388 | if (!Entry) { |
| 389 | if (!DefaultOpenMPPSource) { |
| 390 | // Initialize default location for psource field of ident_t structure of |
| 391 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 392 | // Taken from |
| 393 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 394 | DefaultOpenMPPSource = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 395 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 396 | DefaultOpenMPPSource = |
| 397 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 398 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 399 | auto DefaultOpenMPLocation = new llvm::GlobalVariable( |
| 400 | CGM.getModule(), IdentTy, /*isConstant*/ true, |
| 401 | llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 402 | DefaultOpenMPLocation->setUnnamedAddr(true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 403 | DefaultOpenMPLocation->setAlignment(Align.getQuantity()); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 404 | |
| 405 | llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 406 | llvm::Constant *Values[] = {Zero, |
| 407 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 408 | Zero, Zero, DefaultOpenMPPSource}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 409 | llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values); |
| 410 | DefaultOpenMPLocation->setInitializer(Init); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 411 | OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 412 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 413 | return Address(Entry, Align); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 416 | llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF, |
| 417 | SourceLocation Loc, |
| 418 | OpenMPLocationFlags Flags) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 419 | // If no debug info is generated - return global default location. |
| 420 | if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo || |
| 421 | Loc.isInvalid()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 422 | return getOrCreateDefaultLocation(Flags).getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 423 | |
| 424 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 425 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 426 | Address LocValue = Address::invalid(); |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 427 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 428 | if (I != OpenMPLocThreadIDMap.end()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 429 | LocValue = Address(I->second.DebugLoc, getIdentAlign(CGF.CGM)); |
| 430 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 431 | // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if |
| 432 | // GetOpenMPThreadID was called before this routine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 433 | if (!LocValue.isValid()) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 434 | // Generate "ident_t .kmpc_loc.addr;" |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 435 | Address AI = CGF.CreateTempAlloca(IdentTy, getIdentAlign(CGF.CGM), |
| 436 | ".kmpc_loc.addr"); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 437 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 438 | Elem.second.DebugLoc = AI.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 439 | LocValue = AI; |
| 440 | |
| 441 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 442 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 443 | CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 444 | CGM.getSize(getIdentSize(CGF.CGM))); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 448 | Address PSource = createIdentFieldGEP(CGF, LocValue, IdentField_PSource); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 449 | |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 450 | auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
| 451 | if (OMPDebugLoc == nullptr) { |
| 452 | SmallString<128> Buffer2; |
| 453 | llvm::raw_svector_ostream OS2(Buffer2); |
| 454 | // Build debug location |
| 455 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 456 | OS2 << ";" << PLoc.getFilename() << ";"; |
| 457 | if (const FunctionDecl *FD = |
| 458 | dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) { |
| 459 | OS2 << FD->getQualifiedNameAsString(); |
| 460 | } |
| 461 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 462 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 463 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 464 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 465 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 466 | CGF.Builder.CreateStore(OMPDebugLoc, PSource); |
| 467 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 468 | // Our callers always pass this to a runtime function, so for |
| 469 | // convenience, go ahead and return a naked pointer. |
| 470 | return LocValue.getPointer(); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 473 | llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF, |
| 474 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 475 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 476 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 477 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 478 | // Check whether we've already cached a load of the thread id in this |
| 479 | // function. |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 480 | auto I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 481 | if (I != OpenMPLocThreadIDMap.end()) { |
| 482 | ThreadID = I->second.ThreadID; |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 483 | if (ThreadID != nullptr) |
| 484 | return ThreadID; |
| 485 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 486 | if (auto *OMPRegionInfo = |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 487 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 488 | if (OMPRegionInfo->getThreadIDVariable()) { |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 489 | // Check if this an outlined function with thread id passed as argument. |
| 490 | auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 491 | ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal(); |
| 492 | // If value loaded in entry block, cache it and use it everywhere in |
| 493 | // function. |
| 494 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 495 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 496 | Elem.second.ThreadID = ThreadID; |
| 497 | } |
| 498 | return ThreadID; |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 499 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 500 | } |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 501 | |
| 502 | // This is not an outlined function region - need to call __kmpc_int32 |
| 503 | // kmpc_global_thread_num(ident_t *loc). |
| 504 | // Generate thread id value and cache this value for use across the |
| 505 | // function. |
| 506 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 507 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
| 508 | ThreadID = |
| 509 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
| 510 | emitUpdateLocation(CGF, Loc)); |
| 511 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 512 | Elem.second.ThreadID = ThreadID; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 513 | return ThreadID; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 516 | void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 517 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 518 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) |
| 519 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
| 523 | return llvm::PointerType::getUnqual(IdentTy); |
| 524 | } |
| 525 | |
| 526 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
| 527 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 528 | } |
| 529 | |
| 530 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 531 | CGOpenMPRuntime::createRuntimeFunction(OpenMPRTLFunction Function) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 532 | llvm::Constant *RTLFn = nullptr; |
| 533 | switch (Function) { |
| 534 | case OMPRTL__kmpc_fork_call: { |
| 535 | // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro |
| 536 | // microtask, ...); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 537 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 538 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 539 | llvm::FunctionType *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 540 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 541 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call"); |
| 542 | break; |
| 543 | } |
| 544 | case OMPRTL__kmpc_global_thread_num: { |
| 545 | // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 546 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 547 | llvm::FunctionType *FnTy = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 548 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 549 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 550 | break; |
| 551 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 552 | case OMPRTL__kmpc_threadprivate_cached: { |
| 553 | // Build void *__kmpc_threadprivate_cached(ident_t *loc, |
| 554 | // kmp_int32 global_tid, void *data, size_t size, void ***cache); |
| 555 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 556 | CGM.VoidPtrTy, CGM.SizeTy, |
| 557 | CGM.VoidPtrTy->getPointerTo()->getPointerTo()}; |
| 558 | llvm::FunctionType *FnTy = |
| 559 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false); |
| 560 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached"); |
| 561 | break; |
| 562 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 563 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 564 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 565 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 566 | llvm::Type *TypeParams[] = { |
| 567 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 568 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 569 | llvm::FunctionType *FnTy = |
| 570 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 571 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical"); |
| 572 | break; |
| 573 | } |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 574 | case OMPRTL__kmpc_critical_with_hint: { |
| 575 | // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid, |
| 576 | // kmp_critical_name *crit, uintptr_t hint); |
| 577 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 578 | llvm::PointerType::getUnqual(KmpCriticalNameTy), |
| 579 | CGM.IntPtrTy}; |
| 580 | llvm::FunctionType *FnTy = |
| 581 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 582 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint"); |
| 583 | break; |
| 584 | } |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 585 | case OMPRTL__kmpc_threadprivate_register: { |
| 586 | // Build void __kmpc_threadprivate_register(ident_t *, void *data, |
| 587 | // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor); |
| 588 | // typedef void *(*kmpc_ctor)(void *); |
| 589 | auto KmpcCtorTy = |
| 590 | llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 591 | /*isVarArg*/ false)->getPointerTo(); |
| 592 | // typedef void *(*kmpc_cctor)(void *, void *); |
| 593 | llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 594 | auto KmpcCopyCtorTy = |
| 595 | llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs, |
| 596 | /*isVarArg*/ false)->getPointerTo(); |
| 597 | // typedef void (*kmpc_dtor)(void *); |
| 598 | auto KmpcDtorTy = |
| 599 | llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false) |
| 600 | ->getPointerTo(); |
| 601 | llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy, |
| 602 | KmpcCopyCtorTy, KmpcDtorTy}; |
| 603 | auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs, |
| 604 | /*isVarArg*/ false); |
| 605 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register"); |
| 606 | break; |
| 607 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 608 | case OMPRTL__kmpc_end_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 609 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 610 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 611 | llvm::Type *TypeParams[] = { |
| 612 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 613 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 614 | llvm::FunctionType *FnTy = |
| 615 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 616 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical"); |
| 617 | break; |
| 618 | } |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 619 | case OMPRTL__kmpc_cancel_barrier: { |
| 620 | // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32 |
| 621 | // global_tid); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 622 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 623 | llvm::FunctionType *FnTy = |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 624 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 625 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier"); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 626 | break; |
| 627 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 628 | case OMPRTL__kmpc_barrier: { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 629 | // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 630 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 631 | llvm::FunctionType *FnTy = |
| 632 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 633 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier"); |
| 634 | break; |
| 635 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 636 | case OMPRTL__kmpc_for_static_fini: { |
| 637 | // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid); |
| 638 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 639 | llvm::FunctionType *FnTy = |
| 640 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 641 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini"); |
| 642 | break; |
| 643 | } |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 644 | case OMPRTL__kmpc_push_num_threads: { |
| 645 | // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, |
| 646 | // kmp_int32 num_threads) |
| 647 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 648 | CGM.Int32Ty}; |
| 649 | llvm::FunctionType *FnTy = |
| 650 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 651 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads"); |
| 652 | break; |
| 653 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 654 | case OMPRTL__kmpc_serialized_parallel: { |
| 655 | // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32 |
| 656 | // global_tid); |
| 657 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 658 | llvm::FunctionType *FnTy = |
| 659 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 660 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel"); |
| 661 | break; |
| 662 | } |
| 663 | case OMPRTL__kmpc_end_serialized_parallel: { |
| 664 | // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32 |
| 665 | // global_tid); |
| 666 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 667 | llvm::FunctionType *FnTy = |
| 668 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 669 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel"); |
| 670 | break; |
| 671 | } |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 672 | case OMPRTL__kmpc_flush: { |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 673 | // Build void __kmpc_flush(ident_t *loc); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 674 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
| 675 | llvm::FunctionType *FnTy = |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 676 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 677 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush"); |
| 678 | break; |
| 679 | } |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 680 | case OMPRTL__kmpc_master: { |
| 681 | // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid); |
| 682 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 683 | llvm::FunctionType *FnTy = |
| 684 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 685 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master"); |
| 686 | break; |
| 687 | } |
| 688 | case OMPRTL__kmpc_end_master: { |
| 689 | // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid); |
| 690 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 691 | llvm::FunctionType *FnTy = |
| 692 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 693 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master"); |
| 694 | break; |
| 695 | } |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 696 | case OMPRTL__kmpc_omp_taskyield: { |
| 697 | // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid, |
| 698 | // int end_part); |
| 699 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| 700 | llvm::FunctionType *FnTy = |
| 701 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 702 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield"); |
| 703 | break; |
| 704 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 705 | case OMPRTL__kmpc_single: { |
| 706 | // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid); |
| 707 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 708 | llvm::FunctionType *FnTy = |
| 709 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 710 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single"); |
| 711 | break; |
| 712 | } |
| 713 | case OMPRTL__kmpc_end_single: { |
| 714 | // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid); |
| 715 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 716 | llvm::FunctionType *FnTy = |
| 717 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 718 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single"); |
| 719 | break; |
| 720 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 721 | case OMPRTL__kmpc_omp_task_alloc: { |
| 722 | // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 723 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 724 | // kmp_routine_entry_t *task_entry); |
| 725 | assert(KmpRoutineEntryPtrTy != nullptr && |
| 726 | "Type kmp_routine_entry_t must be created."); |
| 727 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, |
| 728 | CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy}; |
| 729 | // Return void * and then cast to particular kmp_task_t type. |
| 730 | llvm::FunctionType *FnTy = |
| 731 | llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false); |
| 732 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc"); |
| 733 | break; |
| 734 | } |
| 735 | case OMPRTL__kmpc_omp_task: { |
| 736 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 737 | // *new_task); |
| 738 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 739 | CGM.VoidPtrTy}; |
| 740 | llvm::FunctionType *FnTy = |
| 741 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 742 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task"); |
| 743 | break; |
| 744 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 745 | case OMPRTL__kmpc_copyprivate: { |
| 746 | // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid, |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 747 | // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *), |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 748 | // kmp_int32 didit); |
| 749 | llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 750 | auto *CpyFnTy = |
| 751 | llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false); |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 752 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 753 | CGM.VoidPtrTy, CpyFnTy->getPointerTo(), |
| 754 | CGM.Int32Ty}; |
| 755 | llvm::FunctionType *FnTy = |
| 756 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 757 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate"); |
| 758 | break; |
| 759 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 760 | case OMPRTL__kmpc_reduce: { |
| 761 | // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, |
| 762 | // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void |
| 763 | // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck); |
| 764 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 765 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 766 | /*isVarArg=*/false); |
| 767 | llvm::Type *TypeParams[] = { |
| 768 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 769 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 770 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 771 | llvm::FunctionType *FnTy = |
| 772 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 773 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce"); |
| 774 | break; |
| 775 | } |
| 776 | case OMPRTL__kmpc_reduce_nowait: { |
| 777 | // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32 |
| 778 | // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data, |
| 779 | // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name |
| 780 | // *lck); |
| 781 | llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 782 | auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams, |
| 783 | /*isVarArg=*/false); |
| 784 | llvm::Type *TypeParams[] = { |
| 785 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy, |
| 786 | CGM.VoidPtrTy, ReduceFnTy->getPointerTo(), |
| 787 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 788 | llvm::FunctionType *FnTy = |
| 789 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 790 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait"); |
| 791 | break; |
| 792 | } |
| 793 | case OMPRTL__kmpc_end_reduce: { |
| 794 | // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid, |
| 795 | // kmp_critical_name *lck); |
| 796 | llvm::Type *TypeParams[] = { |
| 797 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 798 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 799 | llvm::FunctionType *FnTy = |
| 800 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 801 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce"); |
| 802 | break; |
| 803 | } |
| 804 | case OMPRTL__kmpc_end_reduce_nowait: { |
| 805 | // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid, |
| 806 | // kmp_critical_name *lck); |
| 807 | llvm::Type *TypeParams[] = { |
| 808 | getIdentTyPointerTy(), CGM.Int32Ty, |
| 809 | llvm::PointerType::getUnqual(KmpCriticalNameTy)}; |
| 810 | llvm::FunctionType *FnTy = |
| 811 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 812 | RTLFn = |
| 813 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait"); |
| 814 | break; |
| 815 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 816 | case OMPRTL__kmpc_omp_task_begin_if0: { |
| 817 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 818 | // *new_task); |
| 819 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 820 | CGM.VoidPtrTy}; |
| 821 | llvm::FunctionType *FnTy = |
| 822 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 823 | RTLFn = |
| 824 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0"); |
| 825 | break; |
| 826 | } |
| 827 | case OMPRTL__kmpc_omp_task_complete_if0: { |
| 828 | // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 829 | // *new_task); |
| 830 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 831 | CGM.VoidPtrTy}; |
| 832 | llvm::FunctionType *FnTy = |
| 833 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 834 | RTLFn = CGM.CreateRuntimeFunction(FnTy, |
| 835 | /*Name=*/"__kmpc_omp_task_complete_if0"); |
| 836 | break; |
| 837 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 838 | case OMPRTL__kmpc_ordered: { |
| 839 | // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid); |
| 840 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 841 | llvm::FunctionType *FnTy = |
| 842 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 843 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered"); |
| 844 | break; |
| 845 | } |
| 846 | case OMPRTL__kmpc_end_ordered: { |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 847 | // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 848 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 849 | llvm::FunctionType *FnTy = |
| 850 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 851 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered"); |
| 852 | break; |
| 853 | } |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 854 | case OMPRTL__kmpc_omp_taskwait: { |
| 855 | // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid); |
| 856 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 857 | llvm::FunctionType *FnTy = |
| 858 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 859 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait"); |
| 860 | break; |
| 861 | } |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 862 | case OMPRTL__kmpc_taskgroup: { |
| 863 | // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 864 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 865 | llvm::FunctionType *FnTy = |
| 866 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 867 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup"); |
| 868 | break; |
| 869 | } |
| 870 | case OMPRTL__kmpc_end_taskgroup: { |
| 871 | // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid); |
| 872 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty}; |
| 873 | llvm::FunctionType *FnTy = |
| 874 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 875 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup"); |
| 876 | break; |
| 877 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 878 | case OMPRTL__kmpc_push_proc_bind: { |
| 879 | // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, |
| 880 | // int proc_bind) |
| 881 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| 882 | llvm::FunctionType *FnTy = |
| 883 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 884 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind"); |
| 885 | break; |
| 886 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 887 | case OMPRTL__kmpc_omp_task_with_deps: { |
| 888 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 889 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 890 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list); |
| 891 | llvm::Type *TypeParams[] = { |
| 892 | getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty, |
| 893 | CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy}; |
| 894 | llvm::FunctionType *FnTy = |
| 895 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false); |
| 896 | RTLFn = |
| 897 | CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps"); |
| 898 | break; |
| 899 | } |
| 900 | case OMPRTL__kmpc_omp_wait_deps: { |
| 901 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 902 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias, |
| 903 | // kmp_depend_info_t *noalias_dep_list); |
| 904 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 905 | CGM.Int32Ty, CGM.VoidPtrTy, |
| 906 | CGM.Int32Ty, CGM.VoidPtrTy}; |
| 907 | llvm::FunctionType *FnTy = |
| 908 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 909 | RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps"); |
| 910 | break; |
| 911 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 912 | case OMPRTL__kmpc_cancellationpoint: { |
| 913 | // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 914 | // global_tid, kmp_int32 cncl_kind) |
| 915 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| 916 | llvm::FunctionType *FnTy = |
| 917 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 918 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint"); |
| 919 | break; |
| 920 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 921 | case OMPRTL__kmpc_cancel: { |
| 922 | // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 923 | // kmp_int32 cncl_kind) |
| 924 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy}; |
| 925 | llvm::FunctionType *FnTy = |
| 926 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 927 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel"); |
| 928 | break; |
| 929 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 930 | case OMPRTL__tgt_target: { |
| 931 | // Build int32_t __tgt_target(int32_t device_id, void *host_ptr, int32_t |
| 932 | // arg_num, void** args_base, void **args, size_t *arg_sizes, int32_t |
| 933 | // *arg_types); |
| 934 | llvm::Type *TypeParams[] = {CGM.Int32Ty, |
| 935 | CGM.VoidPtrTy, |
| 936 | CGM.Int32Ty, |
| 937 | CGM.VoidPtrPtrTy, |
| 938 | CGM.VoidPtrPtrTy, |
| 939 | CGM.SizeTy->getPointerTo(), |
| 940 | CGM.Int32Ty->getPointerTo()}; |
| 941 | llvm::FunctionType *FnTy = |
| 942 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 943 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target"); |
| 944 | break; |
| 945 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 946 | case OMPRTL__tgt_register_lib: { |
| 947 | // Build void __tgt_register_lib(__tgt_bin_desc *desc); |
| 948 | QualType ParamTy = |
| 949 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 950 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
| 951 | llvm::FunctionType *FnTy = |
| 952 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 953 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib"); |
| 954 | break; |
| 955 | } |
| 956 | case OMPRTL__tgt_unregister_lib: { |
| 957 | // Build void __tgt_unregister_lib(__tgt_bin_desc *desc); |
| 958 | QualType ParamTy = |
| 959 | CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy()); |
| 960 | llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)}; |
| 961 | llvm::FunctionType *FnTy = |
| 962 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 963 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib"); |
| 964 | break; |
| 965 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 966 | } |
| 967 | return RTLFn; |
| 968 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 969 | |
Alexander Musman | 21212e4 | 2015-03-13 10:38:23 +0000 | [diff] [blame] | 970 | llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, |
| 971 | bool IVSigned) { |
| 972 | assert((IVSize == 32 || IVSize == 64) && |
| 973 | "IV size is not compatible with the omp runtime"); |
| 974 | auto Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4" |
| 975 | : "__kmpc_for_static_init_4u") |
| 976 | : (IVSigned ? "__kmpc_for_static_init_8" |
| 977 | : "__kmpc_for_static_init_8u"); |
| 978 | auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 979 | auto PtrTy = llvm::PointerType::getUnqual(ITy); |
| 980 | llvm::Type *TypeParams[] = { |
| 981 | getIdentTyPointerTy(), // loc |
| 982 | CGM.Int32Ty, // tid |
| 983 | CGM.Int32Ty, // schedtype |
| 984 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 985 | PtrTy, // p_lower |
| 986 | PtrTy, // p_upper |
| 987 | PtrTy, // p_stride |
| 988 | ITy, // incr |
| 989 | ITy // chunk |
| 990 | }; |
| 991 | llvm::FunctionType *FnTy = |
| 992 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 993 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 994 | } |
| 995 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 996 | llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, |
| 997 | bool IVSigned) { |
| 998 | assert((IVSize == 32 || IVSize == 64) && |
| 999 | "IV size is not compatible with the omp runtime"); |
| 1000 | auto Name = |
| 1001 | IVSize == 32 |
| 1002 | ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u") |
| 1003 | : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u"); |
| 1004 | auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 1005 | llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc |
| 1006 | CGM.Int32Ty, // tid |
| 1007 | CGM.Int32Ty, // schedtype |
| 1008 | ITy, // lower |
| 1009 | ITy, // upper |
| 1010 | ITy, // stride |
| 1011 | ITy // chunk |
| 1012 | }; |
| 1013 | llvm::FunctionType *FnTy = |
| 1014 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false); |
| 1015 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 1016 | } |
| 1017 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1018 | llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, |
| 1019 | bool IVSigned) { |
| 1020 | assert((IVSize == 32 || IVSize == 64) && |
| 1021 | "IV size is not compatible with the omp runtime"); |
| 1022 | auto Name = |
| 1023 | IVSize == 32 |
| 1024 | ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u") |
| 1025 | : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u"); |
| 1026 | llvm::Type *TypeParams[] = { |
| 1027 | getIdentTyPointerTy(), // loc |
| 1028 | CGM.Int32Ty, // tid |
| 1029 | }; |
| 1030 | llvm::FunctionType *FnTy = |
| 1031 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false); |
| 1032 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 1033 | } |
| 1034 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1035 | llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, |
| 1036 | bool IVSigned) { |
| 1037 | assert((IVSize == 32 || IVSize == 64) && |
| 1038 | "IV size is not compatible with the omp runtime"); |
| 1039 | auto Name = |
| 1040 | IVSize == 32 |
| 1041 | ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u") |
| 1042 | : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u"); |
| 1043 | auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty; |
| 1044 | auto PtrTy = llvm::PointerType::getUnqual(ITy); |
| 1045 | llvm::Type *TypeParams[] = { |
| 1046 | getIdentTyPointerTy(), // loc |
| 1047 | CGM.Int32Ty, // tid |
| 1048 | llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter |
| 1049 | PtrTy, // p_lower |
| 1050 | PtrTy, // p_upper |
| 1051 | PtrTy // p_stride |
| 1052 | }; |
| 1053 | llvm::FunctionType *FnTy = |
| 1054 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false); |
| 1055 | return CGM.CreateRuntimeFunction(FnTy, Name); |
| 1056 | } |
| 1057 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1058 | llvm::Constant * |
| 1059 | CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1060 | assert(!CGM.getLangOpts().OpenMPUseTLS || |
| 1061 | !CGM.getContext().getTargetInfo().isTLSSupported()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1062 | // Lookup the entry, lazily creating it if necessary. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1063 | return getOrCreateInternalVariable(CGM.Int8PtrPtrTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1064 | Twine(CGM.getMangledName(VD)) + ".cache."); |
| 1065 | } |
| 1066 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1067 | Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF, |
| 1068 | const VarDecl *VD, |
| 1069 | Address VDAddr, |
| 1070 | SourceLocation Loc) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1071 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 1072 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 1073 | return VDAddr; |
| 1074 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1075 | auto VarTy = VDAddr.getElementType(); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1076 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1077 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 1078 | CGM.Int8PtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1079 | CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)), |
| 1080 | getOrCreateThreadPrivateCache(VD)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1081 | return Address(CGF.EmitRuntimeCall( |
| 1082 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args), |
| 1083 | VDAddr.getAlignment()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1086 | void CGOpenMPRuntime::emitThreadPrivateVarInit( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1087 | CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1088 | llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) { |
| 1089 | // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime |
| 1090 | // library. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1091 | auto OMPLoc = emitUpdateLocation(CGF, Loc); |
| 1092 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1093 | OMPLoc); |
| 1094 | // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor) |
| 1095 | // to register constructor/destructor for variable. |
| 1096 | llvm::Value *Args[] = {OMPLoc, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1097 | CGF.Builder.CreatePointerCast(VDAddr.getPointer(), |
| 1098 | CGM.VoidPtrTy), |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1099 | Ctor, CopyCtor, Dtor}; |
Alexey Bataev | 1e4b713 | 2014-12-03 12:11:24 +0000 | [diff] [blame] | 1100 | CGF.EmitRuntimeCall( |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1101 | createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1104 | llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1105 | const VarDecl *VD, Address VDAddr, SourceLocation Loc, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1106 | bool PerformInit, CodeGenFunction *CGF) { |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1107 | if (CGM.getLangOpts().OpenMPUseTLS && |
| 1108 | CGM.getContext().getTargetInfo().isTLSSupported()) |
| 1109 | return nullptr; |
| 1110 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1111 | VD = VD->getDefinition(CGM.getContext()); |
| 1112 | if (VD && ThreadPrivateWithDefinition.count(VD) == 0) { |
| 1113 | ThreadPrivateWithDefinition.insert(VD); |
| 1114 | QualType ASTTy = VD->getType(); |
| 1115 | |
| 1116 | llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr; |
| 1117 | auto Init = VD->getAnyInitializer(); |
| 1118 | if (CGM.getLangOpts().CPlusPlus && PerformInit) { |
| 1119 | // Generate function that re-emits the declaration's initializer into the |
| 1120 | // threadprivate copy of the variable VD |
| 1121 | CodeGenFunction CtorCGF(CGM); |
| 1122 | FunctionArgList Args; |
| 1123 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(), |
| 1124 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy); |
| 1125 | Args.push_back(&Dst); |
| 1126 | |
| 1127 | auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 1128 | CGM.getContext().VoidPtrTy, Args, FunctionType::ExtInfo(), |
| 1129 | /*isVariadic=*/false); |
| 1130 | auto FTy = CGM.getTypes().GetFunctionType(FI); |
| 1131 | auto Fn = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 1132 | FTy, ".__kmpc_global_ctor_.", FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1133 | CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI, |
| 1134 | Args, SourceLocation()); |
| 1135 | auto ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1136 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1137 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1138 | Address Arg = Address(ArgVal, VDAddr.getAlignment()); |
| 1139 | Arg = CtorCGF.Builder.CreateElementBitCast(Arg, |
| 1140 | CtorCGF.ConvertTypeForMem(ASTTy)); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1141 | CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(), |
| 1142 | /*IsInitializer=*/true); |
| 1143 | ArgVal = CtorCGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1144 | CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1145 | CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 1146 | CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue); |
| 1147 | CtorCGF.FinishFunction(); |
| 1148 | Ctor = Fn; |
| 1149 | } |
| 1150 | if (VD->getType().isDestructedType() != QualType::DK_none) { |
| 1151 | // Generate function that emits destructor call for the threadprivate copy |
| 1152 | // of the variable VD |
| 1153 | CodeGenFunction DtorCGF(CGM); |
| 1154 | FunctionArgList Args; |
| 1155 | ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(), |
| 1156 | /*Id=*/nullptr, CGM.getContext().VoidPtrTy); |
| 1157 | Args.push_back(&Dst); |
| 1158 | |
| 1159 | auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 1160 | CGM.getContext().VoidTy, Args, FunctionType::ExtInfo(), |
| 1161 | /*isVariadic=*/false); |
| 1162 | auto FTy = CGM.getTypes().GetFunctionType(FI); |
| 1163 | auto Fn = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 1164 | FTy, ".__kmpc_global_dtor_.", FI, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1165 | DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args, |
| 1166 | SourceLocation()); |
| 1167 | auto ArgVal = DtorCGF.EmitLoadOfScalar( |
| 1168 | DtorCGF.GetAddrOfLocalVar(&Dst), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1169 | /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation()); |
| 1170 | DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1171 | DtorCGF.getDestroyer(ASTTy.isDestructedType()), |
| 1172 | DtorCGF.needsEHCleanup(ASTTy.isDestructedType())); |
| 1173 | DtorCGF.FinishFunction(); |
| 1174 | Dtor = Fn; |
| 1175 | } |
| 1176 | // Do not emit init function if it is not required. |
| 1177 | if (!Ctor && !Dtor) |
| 1178 | return nullptr; |
| 1179 | |
| 1180 | llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy}; |
| 1181 | auto CopyCtorTy = |
| 1182 | llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs, |
| 1183 | /*isVarArg=*/false)->getPointerTo(); |
| 1184 | // Copying constructor for the threadprivate variable. |
| 1185 | // Must be NULL - reserved by runtime, but currently it requires that this |
| 1186 | // parameter is always NULL. Otherwise it fires assertion. |
| 1187 | CopyCtor = llvm::Constant::getNullValue(CopyCtorTy); |
| 1188 | if (Ctor == nullptr) { |
| 1189 | auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy, |
| 1190 | /*isVarArg=*/false)->getPointerTo(); |
| 1191 | Ctor = llvm::Constant::getNullValue(CtorTy); |
| 1192 | } |
| 1193 | if (Dtor == nullptr) { |
| 1194 | auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, |
| 1195 | /*isVarArg=*/false)->getPointerTo(); |
| 1196 | Dtor = llvm::Constant::getNullValue(DtorTy); |
| 1197 | } |
| 1198 | if (!CGF) { |
| 1199 | auto InitFunctionTy = |
| 1200 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false); |
| 1201 | auto InitFunction = CGM.CreateGlobalInitOrDestructFunction( |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 1202 | InitFunctionTy, ".__omp_threadprivate_init_.", |
| 1203 | CGM.getTypes().arrangeNullaryFunction()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1204 | CodeGenFunction InitCGF(CGM); |
| 1205 | FunctionArgList ArgList; |
| 1206 | InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction, |
| 1207 | CGM.getTypes().arrangeNullaryFunction(), ArgList, |
| 1208 | Loc); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1209 | emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1210 | InitCGF.FinishFunction(); |
| 1211 | return InitFunction; |
| 1212 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1213 | emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1214 | } |
| 1215 | return nullptr; |
| 1216 | } |
| 1217 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1218 | /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen |
| 1219 | /// function. Here is the logic: |
| 1220 | /// if (Cond) { |
| 1221 | /// ThenGen(); |
| 1222 | /// } else { |
| 1223 | /// ElseGen(); |
| 1224 | /// } |
| 1225 | static void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, |
| 1226 | const RegionCodeGenTy &ThenGen, |
| 1227 | const RegionCodeGenTy &ElseGen) { |
| 1228 | CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); |
| 1229 | |
| 1230 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1231 | // the condition and the dead arm of the if/else. |
| 1232 | bool CondConstant; |
| 1233 | if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { |
| 1234 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1235 | if (CondConstant) { |
| 1236 | ThenGen(CGF); |
| 1237 | } else { |
| 1238 | ElseGen(CGF); |
| 1239 | } |
| 1240 | return; |
| 1241 | } |
| 1242 | |
| 1243 | // Otherwise, the condition did not fold, or we couldn't elide it. Just |
| 1244 | // emit the conditional branch. |
| 1245 | auto ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 1246 | auto ElseBlock = CGF.createBasicBlock("omp_if.else"); |
| 1247 | auto ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 1248 | CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0); |
| 1249 | |
| 1250 | // Emit the 'then' code. |
| 1251 | CGF.EmitBlock(ThenBlock); |
| 1252 | { |
| 1253 | CodeGenFunction::RunCleanupsScope ThenScope(CGF); |
| 1254 | ThenGen(CGF); |
| 1255 | } |
| 1256 | CGF.EmitBranch(ContBlock); |
| 1257 | // Emit the 'else' code if present. |
| 1258 | { |
| 1259 | // There is no need to emit line number for unconditional branch. |
| 1260 | auto NL = ApplyDebugLocation::CreateEmpty(CGF); |
| 1261 | CGF.EmitBlock(ElseBlock); |
| 1262 | } |
| 1263 | { |
| 1264 | CodeGenFunction::RunCleanupsScope ThenScope(CGF); |
| 1265 | ElseGen(CGF); |
| 1266 | } |
| 1267 | { |
| 1268 | // There is no need to emit line number for unconditional branch. |
| 1269 | auto NL = ApplyDebugLocation::CreateEmpty(CGF); |
| 1270 | CGF.EmitBranch(ContBlock); |
| 1271 | } |
| 1272 | // Emit the continuation block for code after the if. |
| 1273 | CGF.EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1276 | void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 1277 | llvm::Value *OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1278 | ArrayRef<llvm::Value *> CapturedVars, |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1279 | const Expr *IfCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1280 | if (!CGF.HaveInsertPoint()) |
| 1281 | return; |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1282 | auto *RTLoc = emitUpdateLocation(CGF, Loc); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1283 | auto &&ThenGen = [this, OutlinedFn, CapturedVars, |
| 1284 | RTLoc](CodeGenFunction &CGF) { |
| 1285 | // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn); |
| 1286 | llvm::Value *Args[] = { |
| 1287 | RTLoc, |
| 1288 | CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars |
| 1289 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())}; |
| 1290 | llvm::SmallVector<llvm::Value *, 16> RealArgs; |
| 1291 | RealArgs.append(std::begin(Args), std::end(Args)); |
| 1292 | RealArgs.append(CapturedVars.begin(), CapturedVars.end()); |
| 1293 | |
| 1294 | auto RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_call); |
| 1295 | CGF.EmitRuntimeCall(RTLFn, RealArgs); |
| 1296 | }; |
| 1297 | auto &&ElseGen = [this, OutlinedFn, CapturedVars, RTLoc, |
| 1298 | Loc](CodeGenFunction &CGF) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1299 | auto ThreadID = getThreadID(CGF, Loc); |
| 1300 | // Build calls: |
| 1301 | // __kmpc_serialized_parallel(&Loc, GTid); |
| 1302 | llvm::Value *Args[] = {RTLoc, ThreadID}; |
| 1303 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), |
| 1304 | Args); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1305 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1306 | // OutlinedFn(>id, &zero, CapturedStruct); |
| 1307 | auto ThreadIDAddr = emitThreadIDAddress(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1308 | Address ZeroAddr = |
| 1309 | CGF.CreateTempAlloca(CGF.Int32Ty, CharUnits::fromQuantity(4), |
| 1310 | /*Name*/ ".zero.addr"); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1311 | CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1312 | llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs; |
| 1313 | OutlinedFnArgs.push_back(ThreadIDAddr.getPointer()); |
| 1314 | OutlinedFnArgs.push_back(ZeroAddr.getPointer()); |
| 1315 | OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1316 | CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1317 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1318 | // __kmpc_end_serialized_parallel(&Loc, GTid); |
| 1319 | llvm::Value *EndArgs[] = {emitUpdateLocation(CGF, Loc), ThreadID}; |
| 1320 | CGF.EmitRuntimeCall( |
| 1321 | createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), EndArgs); |
| 1322 | }; |
| 1323 | if (IfCond) { |
| 1324 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
| 1325 | } else { |
| 1326 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1327 | ThenGen(CGF); |
| 1328 | } |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
NAKAMURA Takumi | 59c74b22 | 2014-10-27 08:08:18 +0000 | [diff] [blame] | 1331 | // If we're inside an (outlined) parallel region, use the region info's |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1332 | // thread-ID variable (it is passed in a first argument of the outlined function |
| 1333 | // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in |
| 1334 | // regular serial code region, get thread ID by calling kmp_int32 |
| 1335 | // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and |
| 1336 | // return the address of that temp. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1337 | Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF, |
| 1338 | SourceLocation Loc) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1339 | if (auto *OMPRegionInfo = |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1340 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1341 | if (OMPRegionInfo->getThreadIDVariable()) |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1342 | return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress(); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 1343 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1344 | auto ThreadID = getThreadID(CGF, Loc); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1345 | auto Int32Ty = |
| 1346 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true); |
| 1347 | auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp."); |
| 1348 | CGF.EmitStoreOfScalar(ThreadID, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1349 | CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty)); |
Alexey Bataev | d74d060 | 2014-10-13 06:02:40 +0000 | [diff] [blame] | 1350 | |
| 1351 | return ThreadIDTemp; |
| 1352 | } |
| 1353 | |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1354 | llvm::Constant * |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1355 | CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty, |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1356 | const llvm::Twine &Name) { |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1357 | SmallString<256> Buffer; |
| 1358 | llvm::raw_svector_ostream Out(Buffer); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1359 | Out << Name; |
| 1360 | auto RuntimeName = Out.str(); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 1361 | auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first; |
| 1362 | if (Elem.second) { |
| 1363 | assert(Elem.second->getType()->getPointerElementType() == Ty && |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1364 | "OMP internal variable has different type than requested"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 1365 | return &*Elem.second; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1366 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1367 | |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 1368 | return Elem.second = new llvm::GlobalVariable( |
| 1369 | CGM.getModule(), Ty, /*IsConstant*/ false, |
| 1370 | llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty), |
| 1371 | Elem.first()); |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1374 | llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) { |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 1375 | llvm::Twine Name(".gomp_critical_user_", CriticalName); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1376 | return getOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var")); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1379 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1380 | template <size_t N> class CallEndCleanup final : public EHScopeStack::Cleanup { |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 1381 | llvm::Value *Callee; |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 1382 | llvm::Value *Args[N]; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1383 | |
| 1384 | public: |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 1385 | CallEndCleanup(llvm::Value *Callee, ArrayRef<llvm::Value *> CleanupArgs) |
| 1386 | : Callee(Callee) { |
| 1387 | assert(CleanupArgs.size() == N); |
| 1388 | std::copy(CleanupArgs.begin(), CleanupArgs.end(), std::begin(Args)); |
| 1389 | } |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 1390 | void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1391 | if (!CGF.HaveInsertPoint()) |
| 1392 | return; |
Alexey Bataev | 3e6124b | 2015-04-10 07:48:12 +0000 | [diff] [blame] | 1393 | CGF.EmitRuntimeCall(Callee, Args); |
| 1394 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1395 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 1396 | } // anonymous namespace |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1397 | |
| 1398 | void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF, |
| 1399 | StringRef CriticalName, |
| 1400 | const RegionCodeGenTy &CriticalOpGen, |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1401 | SourceLocation Loc, const Expr *Hint) { |
| 1402 | // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]); |
Alexey Bataev | 75ddfab | 2014-12-01 11:32:38 +0000 | [diff] [blame] | 1403 | // CriticalOpGen(); |
| 1404 | // __kmpc_end_critical(ident_t *, gtid, Lock); |
| 1405 | // Prepare arguments and build a call to __kmpc_critical |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1406 | if (!CGF.HaveInsertPoint()) |
| 1407 | return; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1408 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1409 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 1410 | getCriticalRegionLock(CriticalName)}; |
| 1411 | if (Hint) { |
| 1412 | llvm::SmallVector<llvm::Value *, 8> ArgsWithHint(std::begin(Args), |
| 1413 | std::end(Args)); |
| 1414 | auto *HintVal = CGF.EmitScalarExpr(Hint); |
| 1415 | ArgsWithHint.push_back( |
| 1416 | CGF.Builder.CreateIntCast(HintVal, CGM.IntPtrTy, /*isSigned=*/false)); |
| 1417 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_critical_with_hint), |
| 1418 | ArgsWithHint); |
| 1419 | } else |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1420 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_critical), Args); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1421 | // Build a call to __kmpc_end_critical |
| 1422 | CGF.EHStack.pushCleanup<CallEndCleanup<std::extent<decltype(Args)>::value>>( |
| 1423 | NormalAndEHCleanup, createRuntimeFunction(OMPRTL__kmpc_end_critical), |
| 1424 | llvm::makeArrayRef(Args)); |
| 1425 | emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1426 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1427 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1428 | static void emitIfStmt(CodeGenFunction &CGF, llvm::Value *IfCond, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1429 | OpenMPDirectiveKind Kind, SourceLocation Loc, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1430 | const RegionCodeGenTy &BodyOpGen) { |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1431 | llvm::Value *CallBool = CGF.EmitScalarConversion( |
| 1432 | IfCond, |
| 1433 | CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1434 | CGF.getContext().BoolTy, Loc); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1435 | |
| 1436 | auto *ThenBlock = CGF.createBasicBlock("omp_if.then"); |
| 1437 | auto *ContBlock = CGF.createBasicBlock("omp_if.end"); |
| 1438 | // Generate the branch (If-stmt) |
| 1439 | CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock); |
| 1440 | CGF.EmitBlock(ThenBlock); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1441 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, Kind, BodyOpGen); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1442 | // Emit the rest of bblocks/branches |
| 1443 | CGF.EmitBranch(ContBlock); |
| 1444 | CGF.EmitBlock(ContBlock, true); |
| 1445 | } |
| 1446 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1447 | void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1448 | const RegionCodeGenTy &MasterOpGen, |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1449 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1450 | if (!CGF.HaveInsertPoint()) |
| 1451 | return; |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1452 | // if(__kmpc_master(ident_t *, gtid)) { |
| 1453 | // MasterOpGen(); |
| 1454 | // __kmpc_end_master(ident_t *, gtid); |
| 1455 | // } |
| 1456 | // Prepare arguments and build a call to __kmpc_master |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 1457 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1458 | auto *IsMaster = |
| 1459 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_master), Args); |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 1460 | typedef CallEndCleanup<std::extent<decltype(Args)>::value> |
| 1461 | MasterCallEndCleanup; |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1462 | emitIfStmt( |
| 1463 | CGF, IsMaster, OMPD_master, Loc, [&](CodeGenFunction &CGF) -> void { |
| 1464 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1465 | CGF.EHStack.pushCleanup<MasterCallEndCleanup>( |
| 1466 | NormalAndEHCleanup, createRuntimeFunction(OMPRTL__kmpc_end_master), |
| 1467 | llvm::makeArrayRef(Args)); |
| 1468 | MasterOpGen(CGF); |
| 1469 | }); |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1472 | void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF, |
| 1473 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1474 | if (!CGF.HaveInsertPoint()) |
| 1475 | return; |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1476 | // Build call __kmpc_omp_taskyield(loc, thread_id, 0); |
| 1477 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1478 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1479 | llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1480 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args); |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1483 | void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF, |
| 1484 | const RegionCodeGenTy &TaskgroupOpGen, |
| 1485 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1486 | if (!CGF.HaveInsertPoint()) |
| 1487 | return; |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1488 | // __kmpc_taskgroup(ident_t *, gtid); |
| 1489 | // TaskgroupOpGen(); |
| 1490 | // __kmpc_end_taskgroup(ident_t *, gtid); |
| 1491 | // Prepare arguments and build a call to __kmpc_taskgroup |
| 1492 | { |
| 1493 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1494 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 1495 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args); |
| 1496 | // Build a call to __kmpc_end_taskgroup |
| 1497 | CGF.EHStack.pushCleanup<CallEndCleanup<std::extent<decltype(Args)>::value>>( |
| 1498 | NormalAndEHCleanup, createRuntimeFunction(OMPRTL__kmpc_end_taskgroup), |
| 1499 | llvm::makeArrayRef(Args)); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1500 | emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1504 | /// Given an array of pointers to variables, project the address of a |
| 1505 | /// given variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1506 | static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array, |
| 1507 | unsigned Index, const VarDecl *Var) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1508 | // Pull out the pointer to the variable. |
| 1509 | Address PtrAddr = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1510 | CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1511 | llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr); |
| 1512 | |
| 1513 | Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1514 | Addr = CGF.Builder.CreateElementBitCast( |
| 1515 | Addr, CGF.ConvertTypeForMem(Var->getType())); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1516 | return Addr; |
| 1517 | } |
| 1518 | |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1519 | static llvm::Value *emitCopyprivateCopyFunction( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1520 | CodeGenModule &CGM, llvm::Type *ArgsType, |
| 1521 | ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs, |
| 1522 | ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1523 | auto &C = CGM.getContext(); |
| 1524 | // void copy_func(void *LHSArg, void *RHSArg); |
| 1525 | FunctionArgList Args; |
| 1526 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr, |
| 1527 | C.VoidPtrTy); |
| 1528 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr, |
| 1529 | C.VoidPtrTy); |
| 1530 | Args.push_back(&LHSArg); |
| 1531 | Args.push_back(&RHSArg); |
| 1532 | FunctionType::ExtInfo EI; |
| 1533 | auto &CGFI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 1534 | C.VoidTy, Args, EI, /*isVariadic=*/false); |
| 1535 | auto *Fn = llvm::Function::Create( |
| 1536 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 1537 | ".omp.copyprivate.copy_func", &CGM.getModule()); |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 1538 | CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, CGFI); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1539 | CodeGenFunction CGF(CGM); |
| 1540 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1541 | // Dest = (void*[n])(LHSArg); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1542 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1543 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1544 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 1545 | ArgsType), CGF.getPointerAlign()); |
| 1546 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 1547 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 1548 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1549 | // *(Type0*)Dst[0] = *(Type0*)Src[0]; |
| 1550 | // *(Type1*)Dst[1] = *(Type1*)Src[1]; |
| 1551 | // ... |
| 1552 | // *(Typen*)Dst[n] = *(Typen*)Src[n]; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1553 | for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1554 | auto DestVar = cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl()); |
| 1555 | Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar); |
| 1556 | |
| 1557 | auto SrcVar = cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl()); |
| 1558 | Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar); |
| 1559 | |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 1560 | auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl(); |
| 1561 | QualType Type = VD->getType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1562 | CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1563 | } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1564 | CGF.FinishFunction(); |
| 1565 | return Fn; |
| 1566 | } |
| 1567 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1568 | void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1569 | const RegionCodeGenTy &SingleOpGen, |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1570 | SourceLocation Loc, |
| 1571 | ArrayRef<const Expr *> CopyprivateVars, |
| 1572 | ArrayRef<const Expr *> SrcExprs, |
| 1573 | ArrayRef<const Expr *> DstExprs, |
| 1574 | ArrayRef<const Expr *> AssignmentOps) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1575 | if (!CGF.HaveInsertPoint()) |
| 1576 | return; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1577 | assert(CopyprivateVars.size() == SrcExprs.size() && |
| 1578 | CopyprivateVars.size() == DstExprs.size() && |
| 1579 | CopyprivateVars.size() == AssignmentOps.size()); |
| 1580 | auto &C = CGM.getContext(); |
| 1581 | // int32 did_it = 0; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1582 | // if(__kmpc_single(ident_t *, gtid)) { |
| 1583 | // SingleOpGen(); |
| 1584 | // __kmpc_end_single(ident_t *, gtid); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1585 | // did_it = 1; |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1586 | // } |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1587 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 1588 | // <copy_func>, did_it); |
| 1589 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1590 | Address DidIt = Address::invalid(); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1591 | if (!CopyprivateVars.empty()) { |
| 1592 | // int32 did_it = 0; |
| 1593 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1594 | DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1595 | CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1596 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1597 | // Prepare arguments and build a call to __kmpc_single |
Alexey Bataev | d7614fb | 2015-04-10 06:33:45 +0000 | [diff] [blame] | 1598 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1599 | auto *IsSingle = |
| 1600 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_single), Args); |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 1601 | typedef CallEndCleanup<std::extent<decltype(Args)>::value> |
| 1602 | SingleCallEndCleanup; |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1603 | emitIfStmt( |
| 1604 | CGF, IsSingle, OMPD_single, Loc, [&](CodeGenFunction &CGF) -> void { |
| 1605 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1606 | CGF.EHStack.pushCleanup<SingleCallEndCleanup>( |
| 1607 | NormalAndEHCleanup, createRuntimeFunction(OMPRTL__kmpc_end_single), |
| 1608 | llvm::makeArrayRef(Args)); |
| 1609 | SingleOpGen(CGF); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1610 | if (DidIt.isValid()) { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1611 | // did_it = 1; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1612 | CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt); |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1613 | } |
| 1614 | }); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1615 | // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>, |
| 1616 | // <copy_func>, did_it); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1617 | if (DidIt.isValid()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1618 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size()); |
| 1619 | auto CopyprivateArrayTy = |
| 1620 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 1621 | /*IndexTypeQuals=*/0); |
| 1622 | // Create a list of all private variables for copyprivate. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1623 | Address CopyprivateList = |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1624 | CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list"); |
| 1625 | for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1626 | Address Elem = CGF.Builder.CreateConstArrayGEP( |
| 1627 | CopyprivateList, I, CGF.getPointerSize()); |
| 1628 | CGF.Builder.CreateStore( |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1629 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1630 | CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy), |
| 1631 | Elem); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1632 | } |
| 1633 | // Build function that copies private values from single region to all other |
| 1634 | // threads in the corresponding parallel region. |
| 1635 | auto *CpyFn = emitCopyprivateCopyFunction( |
| 1636 | CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(), |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1637 | CopyprivateVars, SrcExprs, DstExprs, AssignmentOps); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 1638 | auto *BufSize = CGF.getTypeSize(CopyprivateArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1639 | Address CL = |
| 1640 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList, |
| 1641 | CGF.VoidPtrTy); |
| 1642 | auto *DidItVal = CGF.Builder.CreateLoad(DidIt); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1643 | llvm::Value *Args[] = { |
| 1644 | emitUpdateLocation(CGF, Loc), // ident_t *<loc> |
| 1645 | getThreadID(CGF, Loc), // i32 <gtid> |
Alexey Bataev | 66beaa9 | 2015-04-30 03:47:32 +0000 | [diff] [blame] | 1646 | BufSize, // size_t <buf_size> |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1647 | CL.getPointer(), // void *<copyprivate list> |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1648 | CpyFn, // void (*) (void *, void *) <copy_func> |
| 1649 | DidItVal // i32 did_it |
| 1650 | }; |
| 1651 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args); |
| 1652 | } |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1655 | void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF, |
| 1656 | const RegionCodeGenTy &OrderedOpGen, |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 1657 | SourceLocation Loc, bool IsThreads) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1658 | if (!CGF.HaveInsertPoint()) |
| 1659 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1660 | // __kmpc_ordered(ident_t *, gtid); |
| 1661 | // OrderedOpGen(); |
| 1662 | // __kmpc_end_ordered(ident_t *, gtid); |
| 1663 | // Prepare arguments and build a call to __kmpc_ordered |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 1664 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 1665 | if (IsThreads) { |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1666 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 1667 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_ordered), Args); |
| 1668 | // Build a call to __kmpc_end_ordered |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 1669 | CGF.EHStack.pushCleanup<CallEndCleanup<std::extent<decltype(Args)>::value>>( |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1670 | NormalAndEHCleanup, createRuntimeFunction(OMPRTL__kmpc_end_ordered), |
| 1671 | llvm::makeArrayRef(Args)); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1672 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 1673 | emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1676 | void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1677 | OpenMPDirectiveKind Kind, bool EmitChecks, |
| 1678 | bool ForceSimpleCall) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1679 | if (!CGF.HaveInsertPoint()) |
| 1680 | return; |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1681 | // Build call __kmpc_cancel_barrier(loc, thread_id); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1682 | // Build call __kmpc_barrier(loc, thread_id); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1683 | OpenMPLocationFlags Flags = OMP_IDENT_KMPC; |
| 1684 | if (Kind == OMPD_for) { |
| 1685 | Flags = |
| 1686 | static_cast<OpenMPLocationFlags>(Flags | OMP_IDENT_BARRIER_IMPL_FOR); |
| 1687 | } else if (Kind == OMPD_sections) { |
| 1688 | Flags = static_cast<OpenMPLocationFlags>(Flags | |
| 1689 | OMP_IDENT_BARRIER_IMPL_SECTIONS); |
| 1690 | } else if (Kind == OMPD_single) { |
| 1691 | Flags = |
| 1692 | static_cast<OpenMPLocationFlags>(Flags | OMP_IDENT_BARRIER_IMPL_SINGLE); |
| 1693 | } else if (Kind == OMPD_barrier) { |
| 1694 | Flags = static_cast<OpenMPLocationFlags>(Flags | OMP_IDENT_BARRIER_EXPL); |
| 1695 | } else { |
| 1696 | Flags = static_cast<OpenMPLocationFlags>(Flags | OMP_IDENT_BARRIER_IMPL); |
| 1697 | } |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1698 | // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc, |
| 1699 | // thread_id); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1700 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags), |
| 1701 | getThreadID(CGF, Loc)}; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1702 | if (auto *OMPRegionInfo = |
| 1703 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1704 | if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1705 | auto *Result = CGF.EmitRuntimeCall( |
| 1706 | createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1707 | if (EmitChecks) { |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1708 | // if (__kmpc_cancel_barrier()) { |
| 1709 | // exit from construct; |
| 1710 | // } |
| 1711 | auto *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 1712 | auto *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 1713 | auto *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| 1714 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 1715 | CGF.EmitBlock(ExitBB); |
| 1716 | // exit from construct; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1717 | auto CancelDestination = |
| 1718 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1719 | CGF.EmitBranchThroughCleanup(CancelDestination); |
| 1720 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 1721 | } |
| 1722 | return; |
| 1723 | } |
| 1724 | } |
| 1725 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1728 | /// \brief Schedule types for 'omp for' loops (these enumerators are taken from |
| 1729 | /// the enum sched_type in kmp.h). |
| 1730 | enum OpenMPSchedType { |
| 1731 | /// \brief Lower bound for default (unordered) versions. |
| 1732 | OMP_sch_lower = 32, |
| 1733 | OMP_sch_static_chunked = 33, |
| 1734 | OMP_sch_static = 34, |
| 1735 | OMP_sch_dynamic_chunked = 35, |
| 1736 | OMP_sch_guided_chunked = 36, |
| 1737 | OMP_sch_runtime = 37, |
| 1738 | OMP_sch_auto = 38, |
| 1739 | /// \brief Lower bound for 'ordered' versions. |
| 1740 | OMP_ord_lower = 64, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1741 | OMP_ord_static_chunked = 65, |
| 1742 | OMP_ord_static = 66, |
| 1743 | OMP_ord_dynamic_chunked = 67, |
| 1744 | OMP_ord_guided_chunked = 68, |
| 1745 | OMP_ord_runtime = 69, |
| 1746 | OMP_ord_auto = 70, |
| 1747 | OMP_sch_default = OMP_sch_static, |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1748 | }; |
| 1749 | |
| 1750 | /// \brief Map the OpenMP loop schedule to the runtime enumeration. |
| 1751 | static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1752 | bool Chunked, bool Ordered) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1753 | switch (ScheduleKind) { |
| 1754 | case OMPC_SCHEDULE_static: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1755 | return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked) |
| 1756 | : (Ordered ? OMP_ord_static : OMP_sch_static); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1757 | case OMPC_SCHEDULE_dynamic: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1758 | return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1759 | case OMPC_SCHEDULE_guided: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1760 | return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1761 | case OMPC_SCHEDULE_runtime: |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1762 | return Ordered ? OMP_ord_runtime : OMP_sch_runtime; |
| 1763 | case OMPC_SCHEDULE_auto: |
| 1764 | return Ordered ? OMP_ord_auto : OMP_sch_auto; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1765 | case OMPC_SCHEDULE_unknown: |
| 1766 | assert(!Chunked && "chunk was specified but schedule kind not known"); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1767 | return Ordered ? OMP_ord_static : OMP_sch_static; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1768 | } |
| 1769 | llvm_unreachable("Unexpected runtime schedule"); |
| 1770 | } |
| 1771 | |
| 1772 | bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
| 1773 | bool Chunked) const { |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1774 | auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1775 | return Schedule == OMP_sch_static; |
| 1776 | } |
| 1777 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1778 | bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const { |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1779 | auto Schedule = |
| 1780 | getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1781 | assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here"); |
| 1782 | return Schedule != OMP_sch_static; |
| 1783 | } |
| 1784 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1785 | void CGOpenMPRuntime::emitForDispatchInit(CodeGenFunction &CGF, |
| 1786 | SourceLocation Loc, |
| 1787 | OpenMPScheduleClauseKind ScheduleKind, |
| 1788 | unsigned IVSize, bool IVSigned, |
| 1789 | bool Ordered, llvm::Value *UB, |
| 1790 | llvm::Value *Chunk) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1791 | if (!CGF.HaveInsertPoint()) |
| 1792 | return; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1793 | OpenMPSchedType Schedule = |
| 1794 | getRuntimeSchedule(ScheduleKind, Chunk != nullptr, Ordered); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1795 | assert(Ordered || |
| 1796 | (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked && |
| 1797 | Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked)); |
| 1798 | // Call __kmpc_dispatch_init( |
| 1799 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedule, |
| 1800 | // kmp_int[32|64] lower, kmp_int[32|64] upper, |
| 1801 | // kmp_int[32|64] stride, kmp_int[32|64] chunk); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1802 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1803 | // If the Chunk was not specified in the clause - use default value 1. |
| 1804 | if (Chunk == nullptr) |
| 1805 | Chunk = CGF.Builder.getIntN(IVSize, 1); |
| 1806 | llvm::Value *Args[] = { |
| 1807 | emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), |
| 1808 | getThreadID(CGF, Loc), |
| 1809 | CGF.Builder.getInt32(Schedule), // Schedule type |
| 1810 | CGF.Builder.getIntN(IVSize, 0), // Lower |
| 1811 | UB, // Upper |
| 1812 | CGF.Builder.getIntN(IVSize, 1), // Stride |
| 1813 | Chunk // Chunk |
| 1814 | }; |
| 1815 | CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args); |
| 1816 | } |
| 1817 | |
| 1818 | void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF, |
| 1819 | SourceLocation Loc, |
| 1820 | OpenMPScheduleClauseKind ScheduleKind, |
| 1821 | unsigned IVSize, bool IVSigned, |
| 1822 | bool Ordered, Address IL, Address LB, |
| 1823 | Address UB, Address ST, |
| 1824 | llvm::Value *Chunk) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1825 | if (!CGF.HaveInsertPoint()) |
| 1826 | return; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1827 | OpenMPSchedType Schedule = |
| 1828 | getRuntimeSchedule(ScheduleKind, Chunk != nullptr, Ordered); |
| 1829 | assert(!Ordered); |
| 1830 | assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked || |
| 1831 | Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked); |
| 1832 | |
| 1833 | // Call __kmpc_for_static_init( |
| 1834 | // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype, |
| 1835 | // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower, |
| 1836 | // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride, |
| 1837 | // kmp_int[32|64] incr, kmp_int[32|64] chunk); |
| 1838 | if (Chunk == nullptr) { |
| 1839 | assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static) && |
| 1840 | "expected static non-chunked schedule"); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1841 | // If the Chunk was not specified in the clause - use default value 1. |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1842 | Chunk = CGF.Builder.getIntN(IVSize, 1); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1843 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1844 | assert((Schedule == OMP_sch_static_chunked || |
| 1845 | Schedule == OMP_ord_static_chunked) && |
| 1846 | "expected static chunked schedule"); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1847 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1848 | llvm::Value *Args[] = { |
| 1849 | emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), |
| 1850 | getThreadID(CGF, Loc), |
| 1851 | CGF.Builder.getInt32(Schedule), // Schedule type |
| 1852 | IL.getPointer(), // &isLastIter |
| 1853 | LB.getPointer(), // &LB |
| 1854 | UB.getPointer(), // &UB |
| 1855 | ST.getPointer(), // &Stride |
| 1856 | CGF.Builder.getIntN(IVSize, 1), // Incr |
| 1857 | Chunk // Chunk |
| 1858 | }; |
| 1859 | CGF.EmitRuntimeCall(createForStaticInitFunction(IVSize, IVSigned), Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1862 | void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF, |
| 1863 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1864 | if (!CGF.HaveInsertPoint()) |
| 1865 | return; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1866 | // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid); |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1867 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), |
| 1868 | getThreadID(CGF, Loc)}; |
| 1869 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini), |
| 1870 | Args); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1873 | void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF, |
| 1874 | SourceLocation Loc, |
| 1875 | unsigned IVSize, |
| 1876 | bool IVSigned) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1877 | if (!CGF.HaveInsertPoint()) |
| 1878 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1879 | // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid); |
| 1880 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), |
| 1881 | getThreadID(CGF, Loc)}; |
| 1882 | CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args); |
| 1883 | } |
| 1884 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1885 | llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF, |
| 1886 | SourceLocation Loc, unsigned IVSize, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1887 | bool IVSigned, Address IL, |
| 1888 | Address LB, Address UB, |
| 1889 | Address ST) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1890 | // Call __kmpc_dispatch_next( |
| 1891 | // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
| 1892 | // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
| 1893 | // kmp_int[32|64] *p_stride); |
| 1894 | llvm::Value *Args[] = { |
| 1895 | emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), getThreadID(CGF, Loc), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1896 | IL.getPointer(), // &isLastIter |
| 1897 | LB.getPointer(), // &Lower |
| 1898 | UB.getPointer(), // &Upper |
| 1899 | ST.getPointer() // &Stride |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1900 | }; |
| 1901 | llvm::Value *Call = |
| 1902 | CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args); |
| 1903 | return CGF.EmitScalarConversion( |
| 1904 | Call, CGF.getContext().getIntTypeForBitwidth(32, /* Signed */ true), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1905 | CGF.getContext().BoolTy, Loc); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1908 | void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF, |
| 1909 | llvm::Value *NumThreads, |
| 1910 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1911 | if (!CGF.HaveInsertPoint()) |
| 1912 | return; |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1913 | // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads) |
| 1914 | llvm::Value *Args[] = { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1915 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1916 | CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)}; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1917 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads), |
| 1918 | Args); |
Alexey Bataev | b205978 | 2014-10-13 08:23:51 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1921 | void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF, |
| 1922 | OpenMPProcBindClauseKind ProcBind, |
| 1923 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1924 | if (!CGF.HaveInsertPoint()) |
| 1925 | return; |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1926 | // Constants for proc bind value accepted by the runtime. |
| 1927 | enum ProcBindTy { |
| 1928 | ProcBindFalse = 0, |
| 1929 | ProcBindTrue, |
| 1930 | ProcBindMaster, |
| 1931 | ProcBindClose, |
| 1932 | ProcBindSpread, |
| 1933 | ProcBindIntel, |
| 1934 | ProcBindDefault |
| 1935 | } RuntimeProcBind; |
| 1936 | switch (ProcBind) { |
| 1937 | case OMPC_PROC_BIND_master: |
| 1938 | RuntimeProcBind = ProcBindMaster; |
| 1939 | break; |
| 1940 | case OMPC_PROC_BIND_close: |
| 1941 | RuntimeProcBind = ProcBindClose; |
| 1942 | break; |
| 1943 | case OMPC_PROC_BIND_spread: |
| 1944 | RuntimeProcBind = ProcBindSpread; |
| 1945 | break; |
| 1946 | case OMPC_PROC_BIND_unknown: |
| 1947 | llvm_unreachable("Unsupported proc_bind value."); |
| 1948 | } |
| 1949 | // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind) |
| 1950 | llvm::Value *Args[] = { |
| 1951 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 1952 | llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)}; |
| 1953 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args); |
| 1954 | } |
| 1955 | |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1956 | void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>, |
| 1957 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1958 | if (!CGF.HaveInsertPoint()) |
| 1959 | return; |
Alexey Bataev | d76df6d | 2015-02-24 12:55:09 +0000 | [diff] [blame] | 1960 | // Build call void __kmpc_flush(ident_t *loc) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1961 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush), |
| 1962 | emitUpdateLocation(CGF, Loc)); |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1963 | } |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1964 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1965 | namespace { |
| 1966 | /// \brief Indexes of fields for type kmp_task_t. |
| 1967 | enum KmpTaskTFields { |
| 1968 | /// \brief List of shared variables. |
| 1969 | KmpTaskTShareds, |
| 1970 | /// \brief Task routine. |
| 1971 | KmpTaskTRoutine, |
| 1972 | /// \brief Partition id for the untied tasks. |
| 1973 | KmpTaskTPartId, |
| 1974 | /// \brief Function with call of destructors for private variables. |
| 1975 | KmpTaskTDestructors, |
| 1976 | }; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 1977 | } // anonymous namespace |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1978 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 1979 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const { |
| 1980 | // FIXME: Add other entries type when they become supported. |
| 1981 | return OffloadEntriesTargetRegion.empty(); |
| 1982 | } |
| 1983 | |
| 1984 | /// \brief Initialize target region entry. |
| 1985 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 1986 | initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 1987 | StringRef ParentName, unsigned LineNum, |
| 1988 | unsigned ColNum, unsigned Order) { |
| 1989 | assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is " |
| 1990 | "only required for the device " |
| 1991 | "code generation."); |
| 1992 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum][ColNum] = |
| 1993 | OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr); |
| 1994 | ++OffloadingEntriesNum; |
| 1995 | } |
| 1996 | |
| 1997 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy:: |
| 1998 | registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
| 1999 | StringRef ParentName, unsigned LineNum, |
| 2000 | unsigned ColNum, llvm::Constant *Addr, |
| 2001 | llvm::Constant *ID) { |
| 2002 | // If we are emitting code for a target, the entry is already initialized, |
| 2003 | // only has to be registered. |
| 2004 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 2005 | assert(hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum, |
| 2006 | ColNum) && |
| 2007 | "Entry must exist."); |
| 2008 | auto &Entry = OffloadEntriesTargetRegion[DeviceID][FileID][ParentName] |
| 2009 | [LineNum][ColNum]; |
| 2010 | assert(Entry.isValid() && "Entry not initialized!"); |
| 2011 | Entry.setAddress(Addr); |
| 2012 | Entry.setID(ID); |
| 2013 | return; |
| 2014 | } else { |
| 2015 | OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum++, Addr, ID); |
| 2016 | OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum][ColNum] = |
| 2017 | Entry; |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo( |
| 2022 | unsigned DeviceID, unsigned FileID, StringRef ParentName, unsigned LineNum, |
| 2023 | unsigned ColNum) const { |
| 2024 | auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID); |
| 2025 | if (PerDevice == OffloadEntriesTargetRegion.end()) |
| 2026 | return false; |
| 2027 | auto PerFile = PerDevice->second.find(FileID); |
| 2028 | if (PerFile == PerDevice->second.end()) |
| 2029 | return false; |
| 2030 | auto PerParentName = PerFile->second.find(ParentName); |
| 2031 | if (PerParentName == PerFile->second.end()) |
| 2032 | return false; |
| 2033 | auto PerLine = PerParentName->second.find(LineNum); |
| 2034 | if (PerLine == PerParentName->second.end()) |
| 2035 | return false; |
| 2036 | auto PerColumn = PerLine->second.find(ColNum); |
| 2037 | if (PerColumn == PerLine->second.end()) |
| 2038 | return false; |
| 2039 | // Fail if this entry is already registered. |
| 2040 | if (PerColumn->second.getAddress() || PerColumn->second.getID()) |
| 2041 | return false; |
| 2042 | return true; |
| 2043 | } |
| 2044 | |
| 2045 | void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo( |
| 2046 | const OffloadTargetRegionEntryInfoActTy &Action) { |
| 2047 | // Scan all target region entries and perform the provided action. |
| 2048 | for (auto &D : OffloadEntriesTargetRegion) |
| 2049 | for (auto &F : D.second) |
| 2050 | for (auto &P : F.second) |
| 2051 | for (auto &L : P.second) |
| 2052 | for (auto &C : L.second) |
| 2053 | Action(D.first, F.first, P.first(), L.first, C.first, C.second); |
| 2054 | } |
| 2055 | |
| 2056 | /// \brief Create a Ctor/Dtor-like function whose body is emitted through |
| 2057 | /// \a Codegen. This is used to emit the two functions that register and |
| 2058 | /// unregister the descriptor of the current compilation unit. |
| 2059 | static llvm::Function * |
| 2060 | createOffloadingBinaryDescriptorFunction(CodeGenModule &CGM, StringRef Name, |
| 2061 | const RegionCodeGenTy &Codegen) { |
| 2062 | auto &C = CGM.getContext(); |
| 2063 | FunctionArgList Args; |
| 2064 | ImplicitParamDecl DummyPtr(C, /*DC=*/nullptr, SourceLocation(), |
| 2065 | /*Id=*/nullptr, C.VoidPtrTy); |
| 2066 | Args.push_back(&DummyPtr); |
| 2067 | |
| 2068 | CodeGenFunction CGF(CGM); |
| 2069 | GlobalDecl(); |
| 2070 | auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 2071 | C.VoidTy, Args, FunctionType::ExtInfo(), |
| 2072 | /*isVariadic=*/false); |
| 2073 | auto FTy = CGM.getTypes().GetFunctionType(FI); |
| 2074 | auto *Fn = |
| 2075 | CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, SourceLocation()); |
| 2076 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FI, Args, SourceLocation()); |
| 2077 | Codegen(CGF); |
| 2078 | CGF.FinishFunction(); |
| 2079 | return Fn; |
| 2080 | } |
| 2081 | |
| 2082 | llvm::Function * |
| 2083 | CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() { |
| 2084 | |
| 2085 | // If we don't have entries or if we are emitting code for the device, we |
| 2086 | // don't need to do anything. |
| 2087 | if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty()) |
| 2088 | return nullptr; |
| 2089 | |
| 2090 | auto &M = CGM.getModule(); |
| 2091 | auto &C = CGM.getContext(); |
| 2092 | |
| 2093 | // Get list of devices we care about |
| 2094 | auto &Devices = CGM.getLangOpts().OMPTargetTriples; |
| 2095 | |
| 2096 | // We should be creating an offloading descriptor only if there are devices |
| 2097 | // specified. |
| 2098 | assert(!Devices.empty() && "No OpenMP offloading devices??"); |
| 2099 | |
| 2100 | // Create the external variables that will point to the begin and end of the |
| 2101 | // host entries section. These will be defined by the linker. |
| 2102 | auto *OffloadEntryTy = |
| 2103 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()); |
| 2104 | llvm::GlobalVariable *HostEntriesBegin = new llvm::GlobalVariable( |
| 2105 | M, OffloadEntryTy, /*isConstant=*/true, |
| 2106 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/0, |
| 2107 | ".omp_offloading.entries_begin"); |
| 2108 | llvm::GlobalVariable *HostEntriesEnd = new llvm::GlobalVariable( |
| 2109 | M, OffloadEntryTy, /*isConstant=*/true, |
| 2110 | llvm::GlobalValue::ExternalLinkage, /*Initializer=*/0, |
| 2111 | ".omp_offloading.entries_end"); |
| 2112 | |
| 2113 | // Create all device images |
| 2114 | llvm::SmallVector<llvm::Constant *, 4> DeviceImagesEntires; |
| 2115 | auto *DeviceImageTy = cast<llvm::StructType>( |
| 2116 | CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy())); |
| 2117 | |
| 2118 | for (unsigned i = 0; i < Devices.size(); ++i) { |
| 2119 | StringRef T = Devices[i].getTriple(); |
| 2120 | auto *ImgBegin = new llvm::GlobalVariable( |
| 2121 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
| 2122 | /*Initializer=*/0, Twine(".omp_offloading.img_start.") + Twine(T)); |
| 2123 | auto *ImgEnd = new llvm::GlobalVariable( |
| 2124 | M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, |
| 2125 | /*Initializer=*/0, Twine(".omp_offloading.img_end.") + Twine(T)); |
| 2126 | |
| 2127 | llvm::Constant *Dev = |
| 2128 | llvm::ConstantStruct::get(DeviceImageTy, ImgBegin, ImgEnd, |
| 2129 | HostEntriesBegin, HostEntriesEnd, nullptr); |
| 2130 | DeviceImagesEntires.push_back(Dev); |
| 2131 | } |
| 2132 | |
| 2133 | // Create device images global array. |
| 2134 | llvm::ArrayType *DeviceImagesInitTy = |
| 2135 | llvm::ArrayType::get(DeviceImageTy, DeviceImagesEntires.size()); |
| 2136 | llvm::Constant *DeviceImagesInit = |
| 2137 | llvm::ConstantArray::get(DeviceImagesInitTy, DeviceImagesEntires); |
| 2138 | |
| 2139 | llvm::GlobalVariable *DeviceImages = new llvm::GlobalVariable( |
| 2140 | M, DeviceImagesInitTy, /*isConstant=*/true, |
| 2141 | llvm::GlobalValue::InternalLinkage, DeviceImagesInit, |
| 2142 | ".omp_offloading.device_images"); |
| 2143 | DeviceImages->setUnnamedAddr(true); |
| 2144 | |
| 2145 | // This is a Zero array to be used in the creation of the constant expressions |
| 2146 | llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty), |
| 2147 | llvm::Constant::getNullValue(CGM.Int32Ty)}; |
| 2148 | |
| 2149 | // Create the target region descriptor. |
| 2150 | auto *BinaryDescriptorTy = cast<llvm::StructType>( |
| 2151 | CGM.getTypes().ConvertTypeForMem(getTgtBinaryDescriptorQTy())); |
| 2152 | llvm::Constant *TargetRegionsDescriptorInit = llvm::ConstantStruct::get( |
| 2153 | BinaryDescriptorTy, llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()), |
| 2154 | llvm::ConstantExpr::getGetElementPtr(DeviceImagesInitTy, DeviceImages, |
| 2155 | Index), |
| 2156 | HostEntriesBegin, HostEntriesEnd, nullptr); |
| 2157 | |
| 2158 | auto *Desc = new llvm::GlobalVariable( |
| 2159 | M, BinaryDescriptorTy, /*isConstant=*/true, |
| 2160 | llvm::GlobalValue::InternalLinkage, TargetRegionsDescriptorInit, |
| 2161 | ".omp_offloading.descriptor"); |
| 2162 | |
| 2163 | // Emit code to register or unregister the descriptor at execution |
| 2164 | // startup or closing, respectively. |
| 2165 | |
| 2166 | // Create a variable to drive the registration and unregistration of the |
| 2167 | // descriptor, so we can reuse the logic that emits Ctors and Dtors. |
| 2168 | auto *IdentInfo = &C.Idents.get(".omp_offloading.reg_unreg_var"); |
| 2169 | ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), SourceLocation(), |
| 2170 | IdentInfo, C.CharTy); |
| 2171 | |
| 2172 | auto *UnRegFn = createOffloadingBinaryDescriptorFunction( |
| 2173 | CGM, ".omp_offloading.descriptor_unreg", [&](CodeGenFunction &CGF) { |
| 2174 | CGF.EmitCallOrInvoke(createRuntimeFunction(OMPRTL__tgt_unregister_lib), |
| 2175 | Desc); |
| 2176 | }); |
| 2177 | auto *RegFn = createOffloadingBinaryDescriptorFunction( |
| 2178 | CGM, ".omp_offloading.descriptor_reg", [&](CodeGenFunction &CGF) { |
| 2179 | CGF.EmitCallOrInvoke(createRuntimeFunction(OMPRTL__tgt_register_lib), |
| 2180 | Desc); |
| 2181 | CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc); |
| 2182 | }); |
| 2183 | return RegFn; |
| 2184 | } |
| 2185 | |
| 2186 | void CGOpenMPRuntime::createOffloadEntry(llvm::Constant *Addr, StringRef Name, |
| 2187 | uint64_t Size) { |
| 2188 | auto *TgtOffloadEntryType = cast<llvm::StructType>( |
| 2189 | CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy())); |
| 2190 | llvm::LLVMContext &C = CGM.getModule().getContext(); |
| 2191 | llvm::Module &M = CGM.getModule(); |
| 2192 | |
| 2193 | // Make sure the address has the right type. |
| 2194 | llvm::Constant *AddrPtr = llvm::ConstantExpr::getBitCast(Addr, CGM.VoidPtrTy); |
| 2195 | |
| 2196 | // Create constant string with the name. |
| 2197 | llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name); |
| 2198 | |
| 2199 | llvm::GlobalVariable *Str = |
| 2200 | new llvm::GlobalVariable(M, StrPtrInit->getType(), /*isConstant=*/true, |
| 2201 | llvm::GlobalValue::InternalLinkage, StrPtrInit, |
| 2202 | ".omp_offloading.entry_name"); |
| 2203 | Str->setUnnamedAddr(true); |
| 2204 | llvm::Constant *StrPtr = llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy); |
| 2205 | |
| 2206 | // Create the entry struct. |
| 2207 | llvm::Constant *EntryInit = llvm::ConstantStruct::get( |
| 2208 | TgtOffloadEntryType, AddrPtr, StrPtr, |
| 2209 | llvm::ConstantInt::get(CGM.SizeTy, Size), nullptr); |
| 2210 | llvm::GlobalVariable *Entry = new llvm::GlobalVariable( |
| 2211 | M, TgtOffloadEntryType, true, llvm::GlobalValue::ExternalLinkage, |
| 2212 | EntryInit, ".omp_offloading.entry"); |
| 2213 | |
| 2214 | // The entry has to be created in the section the linker expects it to be. |
| 2215 | Entry->setSection(".omp_offloading.entries"); |
| 2216 | // We can't have any padding between symbols, so we need to have 1-byte |
| 2217 | // alignment. |
| 2218 | Entry->setAlignment(1); |
| 2219 | return; |
| 2220 | } |
| 2221 | |
| 2222 | void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() { |
| 2223 | // Emit the offloading entries and metadata so that the device codegen side |
| 2224 | // can |
| 2225 | // easily figure out what to emit. The produced metadata looks like this: |
| 2226 | // |
| 2227 | // !omp_offload.info = !{!1, ...} |
| 2228 | // |
| 2229 | // Right now we only generate metadata for function that contain target |
| 2230 | // regions. |
| 2231 | |
| 2232 | // If we do not have entries, we dont need to do anything. |
| 2233 | if (OffloadEntriesInfoManager.empty()) |
| 2234 | return; |
| 2235 | |
| 2236 | llvm::Module &M = CGM.getModule(); |
| 2237 | llvm::LLVMContext &C = M.getContext(); |
| 2238 | SmallVector<OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16> |
| 2239 | OrderedEntries(OffloadEntriesInfoManager.size()); |
| 2240 | |
| 2241 | // Create the offloading info metadata node. |
| 2242 | llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info"); |
| 2243 | |
| 2244 | // Auxiliar methods to create metadata values and strings. |
| 2245 | auto getMDInt = [&](unsigned v) { |
| 2246 | return llvm::ConstantAsMetadata::get( |
| 2247 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(C), v)); |
| 2248 | }; |
| 2249 | |
| 2250 | auto getMDString = [&](StringRef v) { return llvm::MDString::get(C, v); }; |
| 2251 | |
| 2252 | // Create function that emits metadata for each target region entry; |
| 2253 | auto &&TargetRegionMetadataEmitter = [&]( |
| 2254 | unsigned DeviceID, unsigned FileID, StringRef ParentName, unsigned Line, |
| 2255 | unsigned Column, |
| 2256 | OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) { |
| 2257 | llvm::SmallVector<llvm::Metadata *, 32> Ops; |
| 2258 | // Generate metadata for target regions. Each entry of this metadata |
| 2259 | // contains: |
| 2260 | // - Entry 0 -> Kind of this type of metadata (0). |
| 2261 | // - Entry 1 -> Device ID of the file where the entry was identified. |
| 2262 | // - Entry 2 -> File ID of the file where the entry was identified. |
| 2263 | // - Entry 3 -> Mangled name of the function where the entry was identified. |
| 2264 | // - Entry 4 -> Line in the file where the entry was identified. |
| 2265 | // - Entry 5 -> Column in the file where the entry was identified. |
| 2266 | // - Entry 6 -> Order the entry was created. |
| 2267 | // The first element of the metadata node is the kind. |
| 2268 | Ops.push_back(getMDInt(E.getKind())); |
| 2269 | Ops.push_back(getMDInt(DeviceID)); |
| 2270 | Ops.push_back(getMDInt(FileID)); |
| 2271 | Ops.push_back(getMDString(ParentName)); |
| 2272 | Ops.push_back(getMDInt(Line)); |
| 2273 | Ops.push_back(getMDInt(Column)); |
| 2274 | Ops.push_back(getMDInt(E.getOrder())); |
| 2275 | |
| 2276 | // Save this entry in the right position of the ordered entries array. |
| 2277 | OrderedEntries[E.getOrder()] = &E; |
| 2278 | |
| 2279 | // Add metadata to the named metadata node. |
| 2280 | MD->addOperand(llvm::MDNode::get(C, Ops)); |
| 2281 | }; |
| 2282 | |
| 2283 | OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo( |
| 2284 | TargetRegionMetadataEmitter); |
| 2285 | |
| 2286 | for (auto *E : OrderedEntries) { |
| 2287 | assert(E && "All ordered entries must exist!"); |
| 2288 | if (auto *CE = |
| 2289 | dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>( |
| 2290 | E)) { |
| 2291 | assert(CE->getID() && CE->getAddress() && |
| 2292 | "Entry ID and Addr are invalid!"); |
| 2293 | createOffloadEntry(CE->getID(), CE->getAddress()->getName(), /*Size=*/0); |
| 2294 | } else |
| 2295 | llvm_unreachable("Unsupported entry kind."); |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | /// \brief Loads all the offload entries information from the host IR |
| 2300 | /// metadata. |
| 2301 | void CGOpenMPRuntime::loadOffloadInfoMetadata() { |
| 2302 | // If we are in target mode, load the metadata from the host IR. This code has |
| 2303 | // to match the metadaata creation in createOffloadEntriesAndInfoMetadata(). |
| 2304 | |
| 2305 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 2306 | return; |
| 2307 | |
| 2308 | if (CGM.getLangOpts().OMPHostIRFile.empty()) |
| 2309 | return; |
| 2310 | |
| 2311 | auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile); |
| 2312 | if (Buf.getError()) |
| 2313 | return; |
| 2314 | |
| 2315 | llvm::LLVMContext C; |
| 2316 | auto ME = llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C); |
| 2317 | |
| 2318 | if (ME.getError()) |
| 2319 | return; |
| 2320 | |
| 2321 | llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info"); |
| 2322 | if (!MD) |
| 2323 | return; |
| 2324 | |
| 2325 | for (auto I : MD->operands()) { |
| 2326 | llvm::MDNode *MN = cast<llvm::MDNode>(I); |
| 2327 | |
| 2328 | auto getMDInt = [&](unsigned Idx) { |
| 2329 | llvm::ConstantAsMetadata *V = |
| 2330 | cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx)); |
| 2331 | return cast<llvm::ConstantInt>(V->getValue())->getZExtValue(); |
| 2332 | }; |
| 2333 | |
| 2334 | auto getMDString = [&](unsigned Idx) { |
| 2335 | llvm::MDString *V = cast<llvm::MDString>(MN->getOperand(Idx)); |
| 2336 | return V->getString(); |
| 2337 | }; |
| 2338 | |
| 2339 | switch (getMDInt(0)) { |
| 2340 | default: |
| 2341 | llvm_unreachable("Unexpected metadata!"); |
| 2342 | break; |
| 2343 | case OffloadEntriesInfoManagerTy::OffloadEntryInfo:: |
| 2344 | OFFLOAD_ENTRY_INFO_TARGET_REGION: |
| 2345 | OffloadEntriesInfoManager.initializeTargetRegionEntryInfo( |
| 2346 | /*DeviceID=*/getMDInt(1), /*FileID=*/getMDInt(2), |
| 2347 | /*ParentName=*/getMDString(3), /*Line=*/getMDInt(4), |
| 2348 | /*Column=*/getMDInt(5), /*Order=*/getMDInt(6)); |
| 2349 | break; |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2354 | void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) { |
| 2355 | if (!KmpRoutineEntryPtrTy) { |
| 2356 | // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type. |
| 2357 | auto &C = CGM.getContext(); |
| 2358 | QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy}; |
| 2359 | FunctionProtoType::ExtProtoInfo EPI; |
| 2360 | KmpRoutineEntryPtrQTy = C.getPointerType( |
| 2361 | C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI)); |
| 2362 | KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy); |
| 2363 | } |
| 2364 | } |
| 2365 | |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2366 | static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC, |
| 2367 | QualType FieldTy) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2368 | auto *Field = FieldDecl::Create( |
| 2369 | C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy, |
| 2370 | C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()), |
| 2371 | /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit); |
| 2372 | Field->setAccess(AS_public); |
| 2373 | DC->addDecl(Field); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2374 | return Field; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2377 | QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() { |
| 2378 | |
| 2379 | // Make sure the type of the entry is already created. This is the type we |
| 2380 | // have to create: |
| 2381 | // struct __tgt_offload_entry{ |
| 2382 | // void *addr; // Pointer to the offload entry info. |
| 2383 | // // (function or global) |
| 2384 | // char *name; // Name of the function or global. |
| 2385 | // size_t size; // Size of the entry info (0 if it a function). |
| 2386 | // }; |
| 2387 | if (TgtOffloadEntryQTy.isNull()) { |
| 2388 | ASTContext &C = CGM.getContext(); |
| 2389 | auto *RD = C.buildImplicitRecord("__tgt_offload_entry"); |
| 2390 | RD->startDefinition(); |
| 2391 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 2392 | addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy)); |
| 2393 | addFieldToRecordDecl(C, RD, C.getSizeType()); |
| 2394 | RD->completeDefinition(); |
| 2395 | TgtOffloadEntryQTy = C.getRecordType(RD); |
| 2396 | } |
| 2397 | return TgtOffloadEntryQTy; |
| 2398 | } |
| 2399 | |
| 2400 | QualType CGOpenMPRuntime::getTgtDeviceImageQTy() { |
| 2401 | // These are the types we need to build: |
| 2402 | // struct __tgt_device_image{ |
| 2403 | // void *ImageStart; // Pointer to the target code start. |
| 2404 | // void *ImageEnd; // Pointer to the target code end. |
| 2405 | // // We also add the host entries to the device image, as it may be useful |
| 2406 | // // for the target runtime to have access to that information. |
| 2407 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
| 2408 | // // the entries. |
| 2409 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 2410 | // // entries (non inclusive). |
| 2411 | // }; |
| 2412 | if (TgtDeviceImageQTy.isNull()) { |
| 2413 | ASTContext &C = CGM.getContext(); |
| 2414 | auto *RD = C.buildImplicitRecord("__tgt_device_image"); |
| 2415 | RD->startDefinition(); |
| 2416 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 2417 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 2418 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 2419 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 2420 | RD->completeDefinition(); |
| 2421 | TgtDeviceImageQTy = C.getRecordType(RD); |
| 2422 | } |
| 2423 | return TgtDeviceImageQTy; |
| 2424 | } |
| 2425 | |
| 2426 | QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() { |
| 2427 | // struct __tgt_bin_desc{ |
| 2428 | // int32_t NumDevices; // Number of devices supported. |
| 2429 | // __tgt_device_image *DeviceImages; // Arrays of device images |
| 2430 | // // (one per device). |
| 2431 | // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
| 2432 | // // entries. |
| 2433 | // __tgt_offload_entry *EntriesEnd; // End of the table with all the |
| 2434 | // // entries (non inclusive). |
| 2435 | // }; |
| 2436 | if (TgtBinaryDescriptorQTy.isNull()) { |
| 2437 | ASTContext &C = CGM.getContext(); |
| 2438 | auto *RD = C.buildImplicitRecord("__tgt_bin_desc"); |
| 2439 | RD->startDefinition(); |
| 2440 | addFieldToRecordDecl( |
| 2441 | C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true)); |
| 2442 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy())); |
| 2443 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 2444 | addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy())); |
| 2445 | RD->completeDefinition(); |
| 2446 | TgtBinaryDescriptorQTy = C.getRecordType(RD); |
| 2447 | } |
| 2448 | return TgtBinaryDescriptorQTy; |
| 2449 | } |
| 2450 | |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2451 | namespace { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2452 | struct PrivateHelpersTy { |
| 2453 | PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy, |
| 2454 | const VarDecl *PrivateElemInit) |
| 2455 | : Original(Original), PrivateCopy(PrivateCopy), |
| 2456 | PrivateElemInit(PrivateElemInit) {} |
| 2457 | const VarDecl *Original; |
| 2458 | const VarDecl *PrivateCopy; |
| 2459 | const VarDecl *PrivateElemInit; |
| 2460 | }; |
| 2461 | typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy; |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 2462 | } // anonymous namespace |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2463 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2464 | static RecordDecl * |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 2465 | createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2466 | if (!Privates.empty()) { |
| 2467 | auto &C = CGM.getContext(); |
| 2468 | // Build struct .kmp_privates_t. { |
| 2469 | // /* private vars */ |
| 2470 | // }; |
| 2471 | auto *RD = C.buildImplicitRecord(".kmp_privates.t"); |
| 2472 | RD->startDefinition(); |
| 2473 | for (auto &&Pair : Privates) { |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2474 | auto *VD = Pair.second.Original; |
| 2475 | auto Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 2476 | Type = Type.getNonReferenceType(); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2477 | auto *FD = addFieldToRecordDecl(C, RD, Type); |
| 2478 | if (VD->hasAttrs()) { |
| 2479 | for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()), |
| 2480 | E(VD->getAttrs().end()); |
| 2481 | I != E; ++I) |
| 2482 | FD->addAttr(*I); |
| 2483 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2484 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2485 | RD->completeDefinition(); |
| 2486 | return RD; |
| 2487 | } |
| 2488 | return nullptr; |
| 2489 | } |
| 2490 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2491 | static RecordDecl * |
| 2492 | createKmpTaskTRecordDecl(CodeGenModule &CGM, QualType KmpInt32Ty, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2493 | QualType KmpRoutineEntryPointerQTy) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2494 | auto &C = CGM.getContext(); |
| 2495 | // Build struct kmp_task_t { |
| 2496 | // void * shareds; |
| 2497 | // kmp_routine_entry_t routine; |
| 2498 | // kmp_int32 part_id; |
| 2499 | // kmp_routine_entry_t destructors; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2500 | // }; |
| 2501 | auto *RD = C.buildImplicitRecord("kmp_task_t"); |
| 2502 | RD->startDefinition(); |
| 2503 | addFieldToRecordDecl(C, RD, C.VoidPtrTy); |
| 2504 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
| 2505 | addFieldToRecordDecl(C, RD, KmpInt32Ty); |
| 2506 | addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2507 | RD->completeDefinition(); |
| 2508 | return RD; |
| 2509 | } |
| 2510 | |
| 2511 | static RecordDecl * |
| 2512 | createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 2513 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2514 | auto &C = CGM.getContext(); |
| 2515 | // Build struct kmp_task_t_with_privates { |
| 2516 | // kmp_task_t task_data; |
| 2517 | // .kmp_privates_t. privates; |
| 2518 | // }; |
| 2519 | auto *RD = C.buildImplicitRecord("kmp_task_t_with_privates"); |
| 2520 | RD->startDefinition(); |
| 2521 | addFieldToRecordDecl(C, RD, KmpTaskTQTy); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2522 | if (auto *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) { |
| 2523 | addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD)); |
| 2524 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2525 | RD->completeDefinition(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2526 | return RD; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | /// \brief Emit a proxy function which accepts kmp_task_t as the second |
| 2530 | /// argument. |
| 2531 | /// \code |
| 2532 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2533 | /// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, |
| 2534 | /// tt->shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2535 | /// return 0; |
| 2536 | /// } |
| 2537 | /// \endcode |
| 2538 | static llvm::Value * |
| 2539 | emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc, |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2540 | QualType KmpInt32Ty, QualType KmpTaskTWithPrivatesPtrQTy, |
| 2541 | QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2542 | QualType SharedsPtrTy, llvm::Value *TaskFunction, |
| 2543 | llvm::Value *TaskPrivatesMap) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2544 | auto &C = CGM.getContext(); |
| 2545 | FunctionArgList Args; |
| 2546 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty); |
| 2547 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2548 | /*Id=*/nullptr, |
| 2549 | KmpTaskTWithPrivatesPtrQTy.withRestrict()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2550 | Args.push_back(&GtidArg); |
| 2551 | Args.push_back(&TaskTypeArg); |
| 2552 | FunctionType::ExtInfo Info; |
| 2553 | auto &TaskEntryFnInfo = |
| 2554 | CGM.getTypes().arrangeFreeFunctionDeclaration(KmpInt32Ty, Args, Info, |
| 2555 | /*isVariadic=*/false); |
| 2556 | auto *TaskEntryTy = CGM.getTypes().GetFunctionType(TaskEntryFnInfo); |
| 2557 | auto *TaskEntry = |
| 2558 | llvm::Function::Create(TaskEntryTy, llvm::GlobalValue::InternalLinkage, |
| 2559 | ".omp_task_entry.", &CGM.getModule()); |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 2560 | CGM.SetInternalFunctionAttributes(/*D=*/nullptr, TaskEntry, TaskEntryFnInfo); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2561 | CodeGenFunction CGF(CGM); |
| 2562 | CGF.disableDebugInfo(); |
| 2563 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args); |
| 2564 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2565 | // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map, |
| 2566 | // tt->task_data.shareds); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2567 | auto *GtidParam = CGF.EmitLoadOfScalar( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2568 | CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2569 | LValue TDBase = emitLoadOfPointerLValue( |
| 2570 | CGF, CGF.GetAddrOfLocalVar(&TaskTypeArg), KmpTaskTWithPrivatesPtrQTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2571 | auto *KmpTaskTWithPrivatesQTyRD = |
| 2572 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2573 | LValue Base = |
| 2574 | CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2575 | auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
| 2576 | auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId); |
| 2577 | auto PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI); |
| 2578 | auto *PartidParam = CGF.EmitLoadOfLValue(PartIdLVal, Loc).getScalarVal(); |
| 2579 | |
| 2580 | auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds); |
| 2581 | auto SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2582 | auto *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2583 | CGF.EmitLoadOfLValue(SharedsLVal, Loc).getScalarVal(), |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2584 | CGF.ConvertTypeForMem(SharedsPtrTy)); |
| 2585 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2586 | auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1); |
| 2587 | llvm::Value *PrivatesParam; |
| 2588 | if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) { |
| 2589 | auto PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI); |
| 2590 | PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2591 | PrivatesLVal.getPointer(), CGF.VoidPtrTy); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2592 | } else { |
| 2593 | PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 2594 | } |
| 2595 | |
| 2596 | llvm::Value *CallArgs[] = {GtidParam, PartidParam, PrivatesParam, |
| 2597 | TaskPrivatesMap, SharedsParam}; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2598 | CGF.EmitCallOrInvoke(TaskFunction, CallArgs); |
| 2599 | CGF.EmitStoreThroughLValue( |
| 2600 | RValue::get(CGF.Builder.getInt32(/*C=*/0)), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2601 | CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty)); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2602 | CGF.FinishFunction(); |
| 2603 | return TaskEntry; |
| 2604 | } |
| 2605 | |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2606 | static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM, |
| 2607 | SourceLocation Loc, |
| 2608 | QualType KmpInt32Ty, |
| 2609 | QualType KmpTaskTWithPrivatesPtrQTy, |
| 2610 | QualType KmpTaskTWithPrivatesQTy) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2611 | auto &C = CGM.getContext(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2612 | FunctionArgList Args; |
| 2613 | ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty); |
| 2614 | ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2615 | /*Id=*/nullptr, |
| 2616 | KmpTaskTWithPrivatesPtrQTy.withRestrict()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2617 | Args.push_back(&GtidArg); |
| 2618 | Args.push_back(&TaskTypeArg); |
| 2619 | FunctionType::ExtInfo Info; |
| 2620 | auto &DestructorFnInfo = |
| 2621 | CGM.getTypes().arrangeFreeFunctionDeclaration(KmpInt32Ty, Args, Info, |
| 2622 | /*isVariadic=*/false); |
| 2623 | auto *DestructorFnTy = CGM.getTypes().GetFunctionType(DestructorFnInfo); |
| 2624 | auto *DestructorFn = |
| 2625 | llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage, |
| 2626 | ".omp_task_destructor.", &CGM.getModule()); |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 2627 | CGM.SetInternalFunctionAttributes(/*D=*/nullptr, DestructorFn, |
| 2628 | DestructorFnInfo); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2629 | CodeGenFunction CGF(CGM); |
| 2630 | CGF.disableDebugInfo(); |
| 2631 | CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo, |
| 2632 | Args); |
| 2633 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2634 | LValue Base = emitLoadOfPointerLValue( |
| 2635 | CGF, CGF.GetAddrOfLocalVar(&TaskTypeArg), KmpTaskTWithPrivatesPtrQTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2636 | auto *KmpTaskTWithPrivatesQTyRD = |
| 2637 | cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl()); |
| 2638 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2639 | Base = CGF.EmitLValueForField(Base, *FI); |
| 2640 | for (auto *Field : |
| 2641 | cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) { |
| 2642 | if (auto DtorKind = Field->getType().isDestructedType()) { |
| 2643 | auto FieldLValue = CGF.EmitLValueForField(Base, Field); |
| 2644 | CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType()); |
| 2645 | } |
| 2646 | } |
| 2647 | CGF.FinishFunction(); |
| 2648 | return DestructorFn; |
| 2649 | } |
| 2650 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2651 | /// \brief Emit a privates mapping function for correct handling of private and |
| 2652 | /// firstprivate variables. |
| 2653 | /// \code |
| 2654 | /// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1> |
| 2655 | /// **noalias priv1,..., <tyn> **noalias privn) { |
| 2656 | /// *priv1 = &.privates.priv1; |
| 2657 | /// ...; |
| 2658 | /// *privn = &.privates.privn; |
| 2659 | /// } |
| 2660 | /// \endcode |
| 2661 | static llvm::Value * |
| 2662 | emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 2663 | ArrayRef<const Expr *> PrivateVars, |
| 2664 | ArrayRef<const Expr *> FirstprivateVars, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2665 | QualType PrivatesQTy, |
Craig Topper | 8674c5c | 2015-09-29 04:30:07 +0000 | [diff] [blame] | 2666 | ArrayRef<PrivateDataTy> Privates) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2667 | auto &C = CGM.getContext(); |
| 2668 | FunctionArgList Args; |
| 2669 | ImplicitParamDecl TaskPrivatesArg( |
| 2670 | C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, |
| 2671 | C.getPointerType(PrivatesQTy).withConst().withRestrict()); |
| 2672 | Args.push_back(&TaskPrivatesArg); |
| 2673 | llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos; |
| 2674 | unsigned Counter = 1; |
| 2675 | for (auto *E: PrivateVars) { |
| 2676 | Args.push_back(ImplicitParamDecl::Create( |
| 2677 | C, /*DC=*/nullptr, Loc, |
| 2678 | /*Id=*/nullptr, C.getPointerType(C.getPointerType(E->getType())) |
| 2679 | .withConst() |
| 2680 | .withRestrict())); |
| 2681 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2682 | PrivateVarsPos[VD] = Counter; |
| 2683 | ++Counter; |
| 2684 | } |
| 2685 | for (auto *E : FirstprivateVars) { |
| 2686 | Args.push_back(ImplicitParamDecl::Create( |
| 2687 | C, /*DC=*/nullptr, Loc, |
| 2688 | /*Id=*/nullptr, C.getPointerType(C.getPointerType(E->getType())) |
| 2689 | .withConst() |
| 2690 | .withRestrict())); |
| 2691 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2692 | PrivateVarsPos[VD] = Counter; |
| 2693 | ++Counter; |
| 2694 | } |
| 2695 | FunctionType::ExtInfo Info; |
| 2696 | auto &TaskPrivatesMapFnInfo = |
| 2697 | CGM.getTypes().arrangeFreeFunctionDeclaration(C.VoidTy, Args, Info, |
| 2698 | /*isVariadic=*/false); |
| 2699 | auto *TaskPrivatesMapTy = |
| 2700 | CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo); |
| 2701 | auto *TaskPrivatesMap = llvm::Function::Create( |
| 2702 | TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, |
| 2703 | ".omp_task_privates_map.", &CGM.getModule()); |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 2704 | CGM.SetInternalFunctionAttributes(/*D=*/nullptr, TaskPrivatesMap, |
| 2705 | TaskPrivatesMapFnInfo); |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 2706 | TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2707 | CodeGenFunction CGF(CGM); |
| 2708 | CGF.disableDebugInfo(); |
| 2709 | CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap, |
| 2710 | TaskPrivatesMapFnInfo, Args); |
| 2711 | |
| 2712 | // *privi = &.privates.privi; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2713 | LValue Base = emitLoadOfPointerLValue( |
| 2714 | CGF, CGF.GetAddrOfLocalVar(&TaskPrivatesArg), TaskPrivatesArg.getType()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2715 | auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl()); |
| 2716 | Counter = 0; |
| 2717 | for (auto *Field : PrivatesQTyRD->fields()) { |
| 2718 | auto FieldLVal = CGF.EmitLValueForField(Base, Field); |
| 2719 | auto *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]]; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2720 | auto RefLVal = CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2721 | auto RefLoadLVal = |
| 2722 | emitLoadOfPointerLValue(CGF, RefLVal.getAddress(), RefLVal.getType()); |
| 2723 | CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2724 | ++Counter; |
| 2725 | } |
| 2726 | CGF.FinishFunction(); |
| 2727 | return TaskPrivatesMap; |
| 2728 | } |
| 2729 | |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2730 | static int array_pod_sort_comparator(const PrivateDataTy *P1, |
| 2731 | const PrivateDataTy *P2) { |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2732 | return P1->first < P2->first ? 1 : (P2->first < P1->first ? -1 : 0); |
| 2733 | } |
| 2734 | |
| 2735 | void CGOpenMPRuntime::emitTaskCall( |
| 2736 | CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D, |
| 2737 | bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2738 | llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds, |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2739 | const Expr *IfCond, ArrayRef<const Expr *> PrivateVars, |
| 2740 | ArrayRef<const Expr *> PrivateCopies, |
| 2741 | ArrayRef<const Expr *> FirstprivateVars, |
| 2742 | ArrayRef<const Expr *> FirstprivateCopies, |
| 2743 | ArrayRef<const Expr *> FirstprivateInits, |
| 2744 | ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2745 | if (!CGF.HaveInsertPoint()) |
| 2746 | return; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2747 | auto &C = CGM.getContext(); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2748 | llvm::SmallVector<PrivateDataTy, 8> Privates; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2749 | // Aggregate privates and sort them by the alignment. |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2750 | auto I = PrivateCopies.begin(); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2751 | for (auto *E : PrivateVars) { |
| 2752 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2753 | Privates.push_back(std::make_pair( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2754 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2755 | PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
| 2756 | /*PrivateElemInit=*/nullptr))); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2757 | ++I; |
| 2758 | } |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2759 | I = FirstprivateCopies.begin(); |
| 2760 | auto IElemInitRef = FirstprivateInits.begin(); |
| 2761 | for (auto *E : FirstprivateVars) { |
| 2762 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2763 | Privates.push_back(std::make_pair( |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2764 | C.getDeclAlign(VD), |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2765 | PrivateHelpersTy( |
| 2766 | VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()), |
| 2767 | cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl())))); |
| 2768 | ++I, ++IElemInitRef; |
| 2769 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2770 | llvm::array_pod_sort(Privates.begin(), Privates.end(), |
| 2771 | array_pod_sort_comparator); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2772 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 2773 | // Build type kmp_routine_entry_t (if not built yet). |
| 2774 | emitKmpRoutineEntryT(KmpInt32Ty); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2775 | // Build type kmp_task_t (if not built yet). |
| 2776 | if (KmpTaskTQTy.isNull()) { |
| 2777 | KmpTaskTQTy = C.getRecordType( |
| 2778 | createKmpTaskTRecordDecl(CGM, KmpInt32Ty, KmpRoutineEntryPtrQTy)); |
| 2779 | } |
| 2780 | auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2781 | // Build particular struct kmp_task_t for the given task. |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2782 | auto *KmpTaskTWithPrivatesQTyRD = |
| 2783 | createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates); |
| 2784 | auto KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD); |
| 2785 | QualType KmpTaskTWithPrivatesPtrQTy = |
| 2786 | C.getPointerType(KmpTaskTWithPrivatesQTy); |
| 2787 | auto *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy); |
| 2788 | auto *KmpTaskTWithPrivatesPtrTy = KmpTaskTWithPrivatesTy->getPointerTo(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 2789 | auto *KmpTaskTWithPrivatesTySize = CGF.getTypeSize(KmpTaskTWithPrivatesQTy); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2790 | QualType SharedsPtrTy = C.getPointerType(SharedsTy); |
| 2791 | |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2792 | // Emit initial values for private copies (if any). |
| 2793 | llvm::Value *TaskPrivatesMap = nullptr; |
| 2794 | auto *TaskPrivatesMapTy = |
| 2795 | std::next(cast<llvm::Function>(TaskFunction)->getArgumentList().begin(), |
| 2796 | 3) |
| 2797 | ->getType(); |
| 2798 | if (!Privates.empty()) { |
| 2799 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 2800 | TaskPrivatesMap = emitTaskPrivateMappingFunction( |
| 2801 | CGM, Loc, PrivateVars, FirstprivateVars, FI->getType(), Privates); |
| 2802 | TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2803 | TaskPrivatesMap, TaskPrivatesMapTy); |
| 2804 | } else { |
| 2805 | TaskPrivatesMap = llvm::ConstantPointerNull::get( |
| 2806 | cast<llvm::PointerType>(TaskPrivatesMapTy)); |
| 2807 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2808 | // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid, |
| 2809 | // kmp_task_t *tt); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2810 | auto *TaskEntry = emitProxyTaskFunction( |
| 2811 | CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2812 | KmpTaskTQTy, SharedsPtrTy, TaskFunction, TaskPrivatesMap); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2813 | |
| 2814 | // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid, |
| 2815 | // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
| 2816 | // kmp_routine_entry_t *task_entry); |
| 2817 | // Task flags. Format is taken from |
| 2818 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h, |
| 2819 | // description of kmp_tasking_flags struct. |
| 2820 | const unsigned TiedFlag = 0x1; |
| 2821 | const unsigned FinalFlag = 0x2; |
| 2822 | unsigned Flags = Tied ? TiedFlag : 0; |
| 2823 | auto *TaskFlags = |
| 2824 | Final.getPointer() |
| 2825 | ? CGF.Builder.CreateSelect(Final.getPointer(), |
| 2826 | CGF.Builder.getInt32(FinalFlag), |
| 2827 | CGF.Builder.getInt32(/*C=*/0)) |
| 2828 | : CGF.Builder.getInt32(Final.getInt() ? FinalFlag : 0); |
| 2829 | TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags)); |
Alexey Bataev | 40e36f1 | 2015-11-24 13:01:44 +0000 | [diff] [blame] | 2830 | auto *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2831 | llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc), |
| 2832 | getThreadID(CGF, Loc), TaskFlags, |
| 2833 | KmpTaskTWithPrivatesTySize, SharedsSize, |
| 2834 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2835 | TaskEntry, KmpRoutineEntryPtrTy)}; |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2836 | auto *NewTask = CGF.EmitRuntimeCall( |
| 2837 | createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2838 | auto *NewTaskNewTaskTTy = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2839 | NewTask, KmpTaskTWithPrivatesPtrTy); |
| 2840 | LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy, |
| 2841 | KmpTaskTWithPrivatesQTy); |
| 2842 | LValue TDBase = |
| 2843 | CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2844 | // Fill the data in the resulting kmp_task_t record. |
| 2845 | // Copy shareds if there are any. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2846 | Address KmpTaskSharedsPtr = Address::invalid(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2847 | if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 2848 | KmpTaskSharedsPtr = |
| 2849 | Address(CGF.EmitLoadOfScalar( |
| 2850 | CGF.EmitLValueForField( |
| 2851 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), |
| 2852 | KmpTaskTShareds)), |
| 2853 | Loc), |
| 2854 | CGF.getNaturalTypeAlignment(SharedsTy)); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2855 | CGF.EmitAggregateCopy(KmpTaskSharedsPtr, Shareds, SharedsTy); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2856 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2857 | // Emit initial values for private copies (if any). |
| 2858 | bool NeedsCleanup = false; |
| 2859 | if (!Privates.empty()) { |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2860 | auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin()); |
| 2861 | auto PrivatesBase = CGF.EmitLValueForField(Base, *FI); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2862 | FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2863 | LValue SharedsBase; |
| 2864 | if (!FirstprivateVars.empty()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2865 | SharedsBase = CGF.MakeAddrLValue( |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2866 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2867 | KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)), |
| 2868 | SharedsTy); |
| 2869 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2870 | CodeGenFunction::CGCapturedStmtInfo CapturesInfo( |
| 2871 | cast<CapturedStmt>(*D.getAssociatedStmt())); |
| 2872 | for (auto &&Pair : Privates) { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2873 | auto *VD = Pair.second.PrivateCopy; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2874 | auto *Init = VD->getAnyInitializer(); |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2875 | LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI); |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2876 | if (Init) { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2877 | if (auto *Elem = Pair.second.PrivateElemInit) { |
| 2878 | auto *OriginalVD = Pair.second.Original; |
| 2879 | auto *SharedField = CapturesInfo.lookup(OriginalVD); |
| 2880 | auto SharedRefLValue = |
| 2881 | CGF.EmitLValueForField(SharedsBase, SharedField); |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 2882 | SharedRefLValue = CGF.MakeAddrLValue( |
| 2883 | Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)), |
| 2884 | SharedRefLValue.getType(), AlignmentSource::Decl); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 2885 | QualType Type = OriginalVD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 2886 | if (Type->isArrayType()) { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2887 | // Initialize firstprivate array. |
| 2888 | if (!isa<CXXConstructExpr>(Init) || |
| 2889 | CGF.isTrivialInitializer(Init)) { |
| 2890 | // Perform simple memcpy. |
| 2891 | CGF.EmitAggregateAssign(PrivateLValue.getAddress(), |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 2892 | SharedRefLValue.getAddress(), Type); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2893 | } else { |
| 2894 | // Initialize firstprivate array using element-by-element |
| 2895 | // intialization. |
| 2896 | CGF.EmitOMPAggregateAssign( |
| 2897 | PrivateLValue.getAddress(), SharedRefLValue.getAddress(), |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 2898 | Type, [&CGF, Elem, Init, &CapturesInfo]( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2899 | Address DestElement, Address SrcElement) { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2900 | // Clean up any temporaries needed by the initialization. |
| 2901 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2902 | InitScope.addPrivate(Elem, [SrcElement]() -> Address { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2903 | return SrcElement; |
| 2904 | }); |
| 2905 | (void)InitScope.Privatize(); |
| 2906 | // Emit initialization for single element. |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 2907 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII( |
| 2908 | CGF, &CapturesInfo); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2909 | CGF.EmitAnyExprToMem(Init, DestElement, |
| 2910 | Init->getType().getQualifiers(), |
| 2911 | /*IsInitializer=*/false); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2912 | }); |
| 2913 | } |
| 2914 | } else { |
| 2915 | CodeGenFunction::OMPPrivateScope InitScope(CGF); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2916 | InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address { |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2917 | return SharedRefLValue.getAddress(); |
| 2918 | }); |
| 2919 | (void)InitScope.Privatize(); |
Alexey Bataev | d157d47 | 2015-06-24 03:35:38 +0000 | [diff] [blame] | 2920 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2921 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, |
| 2922 | /*capturedByInit=*/false); |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2923 | } |
| 2924 | } else { |
| 2925 | CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false); |
| 2926 | } |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2927 | } |
| 2928 | NeedsCleanup = NeedsCleanup || FI->getType().isDestructedType(); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2929 | ++FI; |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2930 | } |
| 2931 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2932 | // Provide pointer to function with destructors for privates. |
Alexey Bataev | 36c1eb9 | 2015-04-30 06:51:57 +0000 | [diff] [blame] | 2933 | llvm::Value *DestructorFn = |
Alexey Bataev | 8fc69dc | 2015-05-18 07:54:53 +0000 | [diff] [blame] | 2934 | NeedsCleanup ? emitDestructorsFunction(CGM, Loc, KmpInt32Ty, |
| 2935 | KmpTaskTWithPrivatesPtrQTy, |
| 2936 | KmpTaskTWithPrivatesQTy) |
| 2937 | : llvm::ConstantPointerNull::get( |
| 2938 | cast<llvm::PointerType>(KmpRoutineEntryPtrTy)); |
| 2939 | LValue Destructor = CGF.EmitLValueForField( |
| 2940 | TDBase, *std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTDestructors)); |
| 2941 | CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2942 | DestructorFn, KmpRoutineEntryPtrTy), |
| 2943 | Destructor); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2944 | |
| 2945 | // Process list of dependences. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2946 | Address DependenciesArray = Address::invalid(); |
| 2947 | unsigned NumDependencies = Dependences.size(); |
| 2948 | if (NumDependencies) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2949 | // Dependence kind for RTL. |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 2950 | enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 }; |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2951 | enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags }; |
| 2952 | RecordDecl *KmpDependInfoRD; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2953 | QualType FlagsTy = |
| 2954 | C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2955 | llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy); |
| 2956 | if (KmpDependInfoTy.isNull()) { |
| 2957 | KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info"); |
| 2958 | KmpDependInfoRD->startDefinition(); |
| 2959 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType()); |
| 2960 | addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType()); |
| 2961 | addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy); |
| 2962 | KmpDependInfoRD->completeDefinition(); |
| 2963 | KmpDependInfoTy = C.getRecordType(KmpDependInfoRD); |
| 2964 | } else { |
| 2965 | KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl()); |
| 2966 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2967 | CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2968 | // Define type kmp_depend_info[<Dependences.size()>]; |
| 2969 | QualType KmpDependInfoArrayTy = C.getConstantArrayType( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2970 | KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2971 | ArrayType::Normal, /*IndexTypeQuals=*/0); |
| 2972 | // kmp_depend_info[<Dependences.size()>] deps; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2973 | DependenciesArray = CGF.CreateMemTemp(KmpDependInfoArrayTy); |
| 2974 | for (unsigned i = 0; i < NumDependencies; ++i) { |
| 2975 | const Expr *E = Dependences[i].second; |
| 2976 | auto Addr = CGF.EmitLValue(E); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2977 | llvm::Value *Size; |
| 2978 | QualType Ty = E->getType(); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2979 | if (auto *ASE = dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) { |
| 2980 | LValue UpAddrLVal = |
| 2981 | CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false); |
| 2982 | llvm::Value *UpAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2983 | CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2984 | llvm::Value *LowIntPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2985 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy); |
Alexey Bataev | d6fdc8b | 2015-08-31 07:32:19 +0000 | [diff] [blame] | 2986 | llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy); |
| 2987 | Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2988 | } else |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 2989 | Size = CGF.getTypeSize(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2990 | auto Base = CGF.MakeAddrLValue( |
| 2991 | CGF.Builder.CreateConstArrayGEP(DependenciesArray, i, DependencySize), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2992 | KmpDependInfoTy); |
| 2993 | // deps[i].base_addr = &<Dependences[i].second>; |
| 2994 | auto BaseAddrLVal = CGF.EmitLValueForField( |
| 2995 | Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2996 | CGF.EmitStoreOfScalar( |
| 2997 | CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy), |
| 2998 | BaseAddrLVal); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2999 | // deps[i].len = sizeof(<Dependences[i].second>); |
| 3000 | auto LenLVal = CGF.EmitLValueForField( |
| 3001 | Base, *std::next(KmpDependInfoRD->field_begin(), Len)); |
| 3002 | CGF.EmitStoreOfScalar(Size, LenLVal); |
| 3003 | // deps[i].flags = <Dependences[i].first>; |
| 3004 | RTLDependenceKindTy DepKind; |
| 3005 | switch (Dependences[i].first) { |
| 3006 | case OMPC_DEPEND_in: |
| 3007 | DepKind = DepIn; |
| 3008 | break; |
Alexey Bataev | 92e82f9 | 2015-11-23 13:33:42 +0000 | [diff] [blame] | 3009 | // Out and InOut dependencies must use the same code. |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3010 | case OMPC_DEPEND_out: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3011 | case OMPC_DEPEND_inout: |
| 3012 | DepKind = DepInOut; |
| 3013 | break; |
Alexey Bataev | eb48235 | 2015-12-18 05:05:56 +0000 | [diff] [blame] | 3014 | case OMPC_DEPEND_source: |
Alexey Bataev | a636c7f | 2015-12-23 10:27:45 +0000 | [diff] [blame] | 3015 | case OMPC_DEPEND_sink: |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3016 | case OMPC_DEPEND_unknown: |
| 3017 | llvm_unreachable("Unknown task dependence type"); |
| 3018 | } |
| 3019 | auto FlagsLVal = CGF.EmitLValueForField( |
| 3020 | Base, *std::next(KmpDependInfoRD->field_begin(), Flags)); |
| 3021 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind), |
| 3022 | FlagsLVal); |
| 3023 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3024 | DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3025 | CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()), |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3026 | CGF.VoidPtrTy); |
| 3027 | } |
| 3028 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3029 | // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc() |
| 3030 | // libcall. |
| 3031 | // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t |
| 3032 | // *new_task); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3033 | // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid, |
| 3034 | // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list, |
| 3035 | // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence |
| 3036 | // list is not empty |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3037 | auto *ThreadID = getThreadID(CGF, Loc); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3038 | auto *UpLoc = emitUpdateLocation(CGF, Loc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3039 | llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask }; |
| 3040 | llvm::Value *DepTaskArgs[7]; |
| 3041 | if (NumDependencies) { |
| 3042 | DepTaskArgs[0] = UpLoc; |
| 3043 | DepTaskArgs[1] = ThreadID; |
| 3044 | DepTaskArgs[2] = NewTask; |
| 3045 | DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies); |
| 3046 | DepTaskArgs[4] = DependenciesArray.getPointer(); |
| 3047 | DepTaskArgs[5] = CGF.Builder.getInt32(0); |
| 3048 | DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 3049 | } |
| 3050 | auto &&ThenCodeGen = [this, NumDependencies, |
| 3051 | &TaskArgs, &DepTaskArgs](CodeGenFunction &CGF) { |
| 3052 | // TODO: add check for untied tasks. |
| 3053 | if (NumDependencies) { |
| 3054 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), |
| 3055 | DepTaskArgs); |
| 3056 | } else { |
| 3057 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), |
| 3058 | TaskArgs); |
| 3059 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3060 | }; |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 3061 | typedef CallEndCleanup<std::extent<decltype(TaskArgs)>::value> |
| 3062 | IfCallEndCleanup; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3063 | |
| 3064 | llvm::Value *DepWaitTaskArgs[6]; |
| 3065 | if (NumDependencies) { |
| 3066 | DepWaitTaskArgs[0] = UpLoc; |
| 3067 | DepWaitTaskArgs[1] = ThreadID; |
| 3068 | DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies); |
| 3069 | DepWaitTaskArgs[3] = DependenciesArray.getPointer(); |
| 3070 | DepWaitTaskArgs[4] = CGF.Builder.getInt32(0); |
| 3071 | DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); |
| 3072 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3073 | auto &&ElseCodeGen = [this, &TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3074 | NumDependencies, &DepWaitTaskArgs](CodeGenFunction &CGF) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3075 | CodeGenFunction::RunCleanupsScope LocalScope(CGF); |
| 3076 | // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid, |
| 3077 | // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 |
| 3078 | // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info |
| 3079 | // is specified. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3080 | if (NumDependencies) |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3081 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps), |
| 3082 | DepWaitTaskArgs); |
| 3083 | // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid, |
| 3084 | // kmp_task_t *new_task); |
| 3085 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), |
| 3086 | TaskArgs); |
| 3087 | // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid, |
| 3088 | // kmp_task_t *new_task); |
| 3089 | CGF.EHStack.pushCleanup<IfCallEndCleanup>( |
| 3090 | NormalAndEHCleanup, |
| 3091 | createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), |
| 3092 | llvm::makeArrayRef(TaskArgs)); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3093 | |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3094 | // Call proxy_task_entry(gtid, new_task); |
| 3095 | llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy}; |
| 3096 | CGF.EmitCallOrInvoke(TaskEntry, OutlinedFnArgs); |
| 3097 | }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3098 | |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3099 | if (IfCond) { |
| 3100 | emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen); |
| 3101 | } else { |
| 3102 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 3103 | ThenCodeGen(CGF); |
| 3104 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3107 | /// \brief Emit reduction operation for each element of array (required for |
| 3108 | /// array sections) LHS op = RHS. |
| 3109 | /// \param Type Type of array. |
| 3110 | /// \param LHSVar Variable on the left side of the reduction operation |
| 3111 | /// (references element of array in original variable). |
| 3112 | /// \param RHSVar Variable on the right side of the reduction operation |
| 3113 | /// (references element of array in original variable). |
| 3114 | /// \param RedOpGen Generator of reduction operation with use of LHSVar and |
| 3115 | /// RHSVar. |
Benjamin Kramer | e003ca2 | 2015-10-28 13:54:16 +0000 | [diff] [blame] | 3116 | static void EmitOMPAggregateReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3117 | CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar, |
| 3118 | const VarDecl *RHSVar, |
| 3119 | const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *, |
| 3120 | const Expr *, const Expr *)> &RedOpGen, |
| 3121 | const Expr *XExpr = nullptr, const Expr *EExpr = nullptr, |
| 3122 | const Expr *UpExpr = nullptr) { |
| 3123 | // Perform element-by-element initialization. |
| 3124 | QualType ElementTy; |
| 3125 | Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar); |
| 3126 | Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar); |
| 3127 | |
| 3128 | // Drill down to the base element type on both arrays. |
| 3129 | auto ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 3130 | auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr); |
| 3131 | |
| 3132 | auto RHSBegin = RHSAddr.getPointer(); |
| 3133 | auto LHSBegin = LHSAddr.getPointer(); |
| 3134 | // Cast from pointer to array type to pointer to single element. |
| 3135 | auto LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements); |
| 3136 | // The basic structure here is a while-do loop. |
| 3137 | auto BodyBB = CGF.createBasicBlock("omp.arraycpy.body"); |
| 3138 | auto DoneBB = CGF.createBasicBlock("omp.arraycpy.done"); |
| 3139 | auto IsEmpty = |
| 3140 | CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty"); |
| 3141 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 3142 | |
| 3143 | // Enter the loop body, making that address the current address. |
| 3144 | auto EntryBB = CGF.Builder.GetInsertBlock(); |
| 3145 | CGF.EmitBlock(BodyBB); |
| 3146 | |
| 3147 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 3148 | |
| 3149 | llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI( |
| 3150 | RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 3151 | RHSElementPHI->addIncoming(RHSBegin, EntryBB); |
| 3152 | Address RHSElementCurrent = |
| 3153 | Address(RHSElementPHI, |
| 3154 | RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 3155 | |
| 3156 | llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI( |
| 3157 | LHSBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 3158 | LHSElementPHI->addIncoming(LHSBegin, EntryBB); |
| 3159 | Address LHSElementCurrent = |
| 3160 | Address(LHSElementPHI, |
| 3161 | LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 3162 | |
| 3163 | // Emit copy. |
| 3164 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
| 3165 | Scope.addPrivate(LHSVar, [=]() -> Address { return LHSElementCurrent; }); |
| 3166 | Scope.addPrivate(RHSVar, [=]() -> Address { return RHSElementCurrent; }); |
| 3167 | Scope.Privatize(); |
| 3168 | RedOpGen(CGF, XExpr, EExpr, UpExpr); |
| 3169 | Scope.ForceCleanup(); |
| 3170 | |
| 3171 | // Shift the address forward by one element. |
| 3172 | auto LHSElementNext = CGF.Builder.CreateConstGEP1_32( |
| 3173 | LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 3174 | auto RHSElementNext = CGF.Builder.CreateConstGEP1_32( |
| 3175 | RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 3176 | // Check whether we've reached the end. |
| 3177 | auto Done = |
| 3178 | CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done"); |
| 3179 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 3180 | LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock()); |
| 3181 | RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock()); |
| 3182 | |
| 3183 | // Done. |
| 3184 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 3185 | } |
| 3186 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3187 | static llvm::Value *emitReductionFunction(CodeGenModule &CGM, |
| 3188 | llvm::Type *ArgsType, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3189 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3190 | ArrayRef<const Expr *> LHSExprs, |
| 3191 | ArrayRef<const Expr *> RHSExprs, |
| 3192 | ArrayRef<const Expr *> ReductionOps) { |
| 3193 | auto &C = CGM.getContext(); |
| 3194 | |
| 3195 | // void reduction_func(void *LHSArg, void *RHSArg); |
| 3196 | FunctionArgList Args; |
| 3197 | ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr, |
| 3198 | C.VoidPtrTy); |
| 3199 | ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr, |
| 3200 | C.VoidPtrTy); |
| 3201 | Args.push_back(&LHSArg); |
| 3202 | Args.push_back(&RHSArg); |
| 3203 | FunctionType::ExtInfo EI; |
| 3204 | auto &CGFI = CGM.getTypes().arrangeFreeFunctionDeclaration( |
| 3205 | C.VoidTy, Args, EI, /*isVariadic=*/false); |
| 3206 | auto *Fn = llvm::Function::Create( |
| 3207 | CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage, |
| 3208 | ".omp.reduction.reduction_func", &CGM.getModule()); |
Akira Hatanaka | 44a59f8 | 2015-10-28 02:30:47 +0000 | [diff] [blame] | 3209 | CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, CGFI); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3210 | CodeGenFunction CGF(CGM); |
| 3211 | CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args); |
| 3212 | |
| 3213 | // Dst = (void*[n])(LHSArg); |
| 3214 | // Src = (void*[n])(RHSArg); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3215 | Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3216 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)), |
| 3217 | ArgsType), CGF.getPointerAlign()); |
| 3218 | Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 3219 | CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)), |
| 3220 | ArgsType), CGF.getPointerAlign()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3221 | |
| 3222 | // ... |
| 3223 | // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
| 3224 | // ... |
| 3225 | CodeGenFunction::OMPPrivateScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3226 | auto IPriv = Privates.begin(); |
| 3227 | unsigned Idx = 0; |
| 3228 | for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3229 | auto RHSVar = cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl()); |
| 3230 | Scope.addPrivate(RHSVar, [&]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3231 | return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3232 | }); |
| 3233 | auto LHSVar = cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl()); |
| 3234 | Scope.addPrivate(LHSVar, [&]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3235 | return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3236 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3237 | QualType PrivTy = (*IPriv)->getType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3238 | if (PrivTy->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3239 | // Get array size and emit VLA type. |
| 3240 | ++Idx; |
| 3241 | Address Elem = |
| 3242 | CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize()); |
| 3243 | llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3244 | auto *VLA = CGF.getContext().getAsVariableArrayType(PrivTy); |
| 3245 | auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3246 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3247 | CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy))); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3248 | CGF.EmitVariablyModifiedType(PrivTy); |
| 3249 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3250 | } |
| 3251 | Scope.Privatize(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3252 | IPriv = Privates.begin(); |
| 3253 | auto ILHS = LHSExprs.begin(); |
| 3254 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3255 | for (auto *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3256 | if ((*IPriv)->getType()->isArrayType()) { |
| 3257 | // Emit reduction for array section. |
| 3258 | auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 3259 | auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 3260 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 3261 | [=](CodeGenFunction &CGF, const Expr *, |
| 3262 | const Expr *, |
| 3263 | const Expr *) { CGF.EmitIgnoredExpr(E); }); |
| 3264 | } else |
| 3265 | // Emit reduction for array subscript or single variable. |
| 3266 | CGF.EmitIgnoredExpr(E); |
| 3267 | ++IPriv, ++ILHS, ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3268 | } |
| 3269 | Scope.ForceCleanup(); |
| 3270 | CGF.FinishFunction(); |
| 3271 | return Fn; |
| 3272 | } |
| 3273 | |
| 3274 | void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3275 | ArrayRef<const Expr *> Privates, |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3276 | ArrayRef<const Expr *> LHSExprs, |
| 3277 | ArrayRef<const Expr *> RHSExprs, |
| 3278 | ArrayRef<const Expr *> ReductionOps, |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 3279 | bool WithNowait, bool SimpleReduction) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3280 | if (!CGF.HaveInsertPoint()) |
| 3281 | return; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3282 | // Next code should be emitted for reduction: |
| 3283 | // |
| 3284 | // static kmp_critical_name lock = { 0 }; |
| 3285 | // |
| 3286 | // void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
| 3287 | // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]); |
| 3288 | // ... |
| 3289 | // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1], |
| 3290 | // *(Type<n>-1*)rhs[<n>-1]); |
| 3291 | // } |
| 3292 | // |
| 3293 | // ... |
| 3294 | // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
| 3295 | // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 3296 | // RedList, reduce_func, &<lock>)) { |
| 3297 | // case 1: |
| 3298 | // ... |
| 3299 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 3300 | // ... |
| 3301 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 3302 | // break; |
| 3303 | // case 2: |
| 3304 | // ... |
| 3305 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 3306 | // ... |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 3307 | // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);] |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3308 | // break; |
| 3309 | // default:; |
| 3310 | // } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 3311 | // |
| 3312 | // if SimpleReduction is true, only the next code is generated: |
| 3313 | // ... |
| 3314 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 3315 | // ... |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3316 | |
| 3317 | auto &C = CGM.getContext(); |
| 3318 | |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 3319 | if (SimpleReduction) { |
| 3320 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3321 | auto IPriv = Privates.begin(); |
| 3322 | auto ILHS = LHSExprs.begin(); |
| 3323 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 3324 | for (auto *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3325 | if ((*IPriv)->getType()->isArrayType()) { |
| 3326 | auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 3327 | auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 3328 | EmitOMPAggregateReduction( |
| 3329 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 3330 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, |
| 3331 | const Expr *) { CGF.EmitIgnoredExpr(E); }); |
| 3332 | } else |
| 3333 | CGF.EmitIgnoredExpr(E); |
| 3334 | ++IPriv, ++ILHS, ++IRHS; |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 3335 | } |
| 3336 | return; |
| 3337 | } |
| 3338 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3339 | // 1. Build a list of reduction variables. |
| 3340 | // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]}; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3341 | auto Size = RHSExprs.size(); |
| 3342 | for (auto *E : Privates) { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3343 | if (E->getType()->isVariablyModifiedType()) |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3344 | // Reserve place for array size. |
| 3345 | ++Size; |
| 3346 | } |
| 3347 | llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3348 | QualType ReductionArrayTy = |
| 3349 | C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal, |
| 3350 | /*IndexTypeQuals=*/0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3351 | Address ReductionList = |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3352 | CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3353 | auto IPriv = Privates.begin(); |
| 3354 | unsigned Idx = 0; |
| 3355 | for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3356 | Address Elem = |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3357 | CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3358 | CGF.Builder.CreateStore( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3359 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3360 | CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy), |
| 3361 | Elem); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3362 | if ((*IPriv)->getType()->isVariablyModifiedType()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3363 | // Store array size. |
| 3364 | ++Idx; |
| 3365 | Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, |
| 3366 | CGF.getPointerSize()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3367 | llvm::Value *Size = CGF.Builder.CreateIntCast( |
| 3368 | CGF.getVLASize( |
| 3369 | CGF.getContext().getAsVariableArrayType((*IPriv)->getType())) |
| 3370 | .first, |
| 3371 | CGF.SizeTy, /*isSigned=*/false); |
| 3372 | CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy), |
| 3373 | Elem); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3374 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | // 2. Emit reduce_func(). |
| 3378 | auto *ReductionFn = emitReductionFunction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3379 | CGM, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates, |
| 3380 | LHSExprs, RHSExprs, ReductionOps); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3381 | |
| 3382 | // 3. Create static kmp_critical_name lock = { 0 }; |
| 3383 | auto *Lock = getCriticalRegionLock(".reduction"); |
| 3384 | |
| 3385 | // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
| 3386 | // RedList, reduce_func, &<lock>); |
| 3387 | auto *IdentTLoc = emitUpdateLocation( |
| 3388 | CGF, Loc, |
| 3389 | static_cast<OpenMPLocationFlags>(OMP_IDENT_KMPC | OMP_ATOMIC_REDUCE)); |
| 3390 | auto *ThreadId = getThreadID(CGF, Loc); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3391 | auto *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3392 | auto *RL = |
| 3393 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(ReductionList.getPointer(), |
| 3394 | CGF.VoidPtrTy); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3395 | llvm::Value *Args[] = { |
| 3396 | IdentTLoc, // ident_t *<loc> |
| 3397 | ThreadId, // i32 <gtid> |
| 3398 | CGF.Builder.getInt32(RHSExprs.size()), // i32 <n> |
| 3399 | ReductionArrayTySize, // size_type sizeof(RedList) |
| 3400 | RL, // void *RedList |
| 3401 | ReductionFn, // void (*) (void *, void *) <reduce_func> |
| 3402 | Lock // kmp_critical_name *&<lock> |
| 3403 | }; |
| 3404 | auto Res = CGF.EmitRuntimeCall( |
| 3405 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait |
| 3406 | : OMPRTL__kmpc_reduce), |
| 3407 | Args); |
| 3408 | |
| 3409 | // 5. Build switch(res) |
| 3410 | auto *DefaultBB = CGF.createBasicBlock(".omp.reduction.default"); |
| 3411 | auto *SwInst = CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2); |
| 3412 | |
| 3413 | // 6. Build case 1: |
| 3414 | // ... |
| 3415 | // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
| 3416 | // ... |
| 3417 | // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 3418 | // break; |
| 3419 | auto *Case1BB = CGF.createBasicBlock(".omp.reduction.case1"); |
| 3420 | SwInst->addCase(CGF.Builder.getInt32(1), Case1BB); |
| 3421 | CGF.EmitBlock(Case1BB); |
| 3422 | |
| 3423 | { |
| 3424 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 3425 | // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
| 3426 | llvm::Value *EndArgs[] = { |
| 3427 | IdentTLoc, // ident_t *<loc> |
| 3428 | ThreadId, // i32 <gtid> |
| 3429 | Lock // kmp_critical_name *&<lock> |
| 3430 | }; |
Alexey Bataev | a744ff5 | 2015-05-05 09:24:37 +0000 | [diff] [blame] | 3431 | CGF.EHStack |
| 3432 | .pushCleanup<CallEndCleanup<std::extent<decltype(EndArgs)>::value>>( |
| 3433 | NormalAndEHCleanup, |
| 3434 | createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait |
| 3435 | : OMPRTL__kmpc_end_reduce), |
| 3436 | llvm::makeArrayRef(EndArgs)); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3437 | auto IPriv = Privates.begin(); |
| 3438 | auto ILHS = LHSExprs.begin(); |
| 3439 | auto IRHS = RHSExprs.begin(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3440 | for (auto *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3441 | if ((*IPriv)->getType()->isArrayType()) { |
| 3442 | // Emit reduction for array section. |
| 3443 | auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 3444 | auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 3445 | EmitOMPAggregateReduction( |
| 3446 | CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 3447 | [=](CodeGenFunction &CGF, const Expr *, const Expr *, |
| 3448 | const Expr *) { CGF.EmitIgnoredExpr(E); }); |
| 3449 | } else |
| 3450 | // Emit reduction for array subscript or single variable. |
| 3451 | CGF.EmitIgnoredExpr(E); |
| 3452 | ++IPriv, ++ILHS, ++IRHS; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | CGF.EmitBranch(DefaultBB); |
| 3457 | |
| 3458 | // 7. Build case 2: |
| 3459 | // ... |
| 3460 | // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
| 3461 | // ... |
| 3462 | // break; |
| 3463 | auto *Case2BB = CGF.createBasicBlock(".omp.reduction.case2"); |
| 3464 | SwInst->addCase(CGF.Builder.getInt32(2), Case2BB); |
| 3465 | CGF.EmitBlock(Case2BB); |
| 3466 | |
| 3467 | { |
| 3468 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 3469 | if (!WithNowait) { |
| 3470 | // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>); |
| 3471 | llvm::Value *EndArgs[] = { |
| 3472 | IdentTLoc, // ident_t *<loc> |
| 3473 | ThreadId, // i32 <gtid> |
| 3474 | Lock // kmp_critical_name *&<lock> |
| 3475 | }; |
| 3476 | CGF.EHStack |
| 3477 | .pushCleanup<CallEndCleanup<std::extent<decltype(EndArgs)>::value>>( |
| 3478 | NormalAndEHCleanup, |
| 3479 | createRuntimeFunction(OMPRTL__kmpc_end_reduce), |
| 3480 | llvm::makeArrayRef(EndArgs)); |
| 3481 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3482 | auto ILHS = LHSExprs.begin(); |
| 3483 | auto IRHS = RHSExprs.begin(); |
| 3484 | auto IPriv = Privates.begin(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3485 | for (auto *E : ReductionOps) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3486 | const Expr *XExpr = nullptr; |
| 3487 | const Expr *EExpr = nullptr; |
| 3488 | const Expr *UpExpr = nullptr; |
| 3489 | BinaryOperatorKind BO = BO_Comma; |
| 3490 | if (auto *BO = dyn_cast<BinaryOperator>(E)) { |
| 3491 | if (BO->getOpcode() == BO_Assign) { |
| 3492 | XExpr = BO->getLHS(); |
| 3493 | UpExpr = BO->getRHS(); |
| 3494 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3495 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3496 | // Try to emit update expression as a simple atomic. |
| 3497 | auto *RHSExpr = UpExpr; |
| 3498 | if (RHSExpr) { |
| 3499 | // Analyze RHS part of the whole expression. |
| 3500 | if (auto *ACO = dyn_cast<AbstractConditionalOperator>( |
| 3501 | RHSExpr->IgnoreParenImpCasts())) { |
| 3502 | // If this is a conditional operator, analyze its condition for |
| 3503 | // min/max reduction operator. |
| 3504 | RHSExpr = ACO->getCond(); |
| 3505 | } |
| 3506 | if (auto *BORHS = |
| 3507 | dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) { |
| 3508 | EExpr = BORHS->getRHS(); |
| 3509 | BO = BORHS->getOpcode(); |
| 3510 | } |
Alexey Bataev | 69a4779 | 2015-05-07 03:54:03 +0000 | [diff] [blame] | 3511 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3512 | if (XExpr) { |
| 3513 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 3514 | auto &&AtomicRedGen = [this, BO, VD, IPriv, |
| 3515 | Loc](CodeGenFunction &CGF, const Expr *XExpr, |
| 3516 | const Expr *EExpr, const Expr *UpExpr) { |
| 3517 | LValue X = CGF.EmitLValue(XExpr); |
| 3518 | RValue E; |
| 3519 | if (EExpr) |
| 3520 | E = CGF.EmitAnyExpr(EExpr); |
| 3521 | CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3522 | X, E, BO, /*IsXLHSInRHSPart=*/true, llvm::Monotonic, Loc, |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3523 | [&CGF, UpExpr, VD, IPriv, Loc](RValue XRValue) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3524 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3525 | PrivateScope.addPrivate( |
| 3526 | VD, [&CGF, VD, XRValue, Loc]() -> Address { |
| 3527 | Address LHSTemp = CGF.CreateMemTemp(VD->getType()); |
| 3528 | CGF.emitOMPSimpleStore( |
| 3529 | CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue, |
| 3530 | VD->getType().getNonReferenceType(), Loc); |
| 3531 | return LHSTemp; |
| 3532 | }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3533 | (void)PrivateScope.Privatize(); |
| 3534 | return CGF.EmitAnyExpr(UpExpr); |
| 3535 | }); |
| 3536 | }; |
| 3537 | if ((*IPriv)->getType()->isArrayType()) { |
| 3538 | // Emit atomic reduction for array section. |
| 3539 | auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 3540 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar, |
| 3541 | AtomicRedGen, XExpr, EExpr, UpExpr); |
| 3542 | } else |
| 3543 | // Emit atomic reduction for array subscript or single variable. |
| 3544 | AtomicRedGen(CGF, XExpr, EExpr, UpExpr); |
| 3545 | } else { |
| 3546 | // Emit as a critical region. |
| 3547 | auto &&CritRedGen = [this, E, Loc](CodeGenFunction &CGF, const Expr *, |
| 3548 | const Expr *, const Expr *) { |
| 3549 | emitCriticalRegion( |
| 3550 | CGF, ".atomic_reduction", |
| 3551 | [E](CodeGenFunction &CGF) { CGF.EmitIgnoredExpr(E); }, Loc); |
| 3552 | }; |
| 3553 | if ((*IPriv)->getType()->isArrayType()) { |
| 3554 | auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 3555 | auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 3556 | EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar, |
| 3557 | CritRedGen); |
| 3558 | } else |
| 3559 | CritRedGen(CGF, nullptr, nullptr, nullptr); |
| 3560 | } |
| 3561 | ++ILHS, ++IRHS, ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | CGF.EmitBranch(DefaultBB); |
| 3566 | CGF.EmitBlock(DefaultBB, /*IsFinished=*/true); |
| 3567 | } |
| 3568 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3569 | void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF, |
| 3570 | SourceLocation Loc) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3571 | if (!CGF.HaveInsertPoint()) |
| 3572 | return; |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3573 | // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 |
| 3574 | // global_tid); |
| 3575 | llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)}; |
| 3576 | // Ignore return result until untied tasks are supported. |
| 3577 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args); |
| 3578 | } |
| 3579 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3580 | void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3581 | OpenMPDirectiveKind InnerKind, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3582 | const RegionCodeGenTy &CodeGen, |
| 3583 | bool HasCancel) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3584 | if (!CGF.HaveInsertPoint()) |
| 3585 | return; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3586 | InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3587 | CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr); |
Alexey Bataev | 8cbe0a6 | 2015-02-26 10:27:34 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3590 | namespace { |
| 3591 | enum RTCancelKind { |
| 3592 | CancelNoreq = 0, |
| 3593 | CancelParallel = 1, |
| 3594 | CancelLoop = 2, |
| 3595 | CancelSections = 3, |
| 3596 | CancelTaskgroup = 4 |
| 3597 | }; |
| 3598 | } |
| 3599 | |
| 3600 | static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) { |
| 3601 | RTCancelKind CancelKind = CancelNoreq; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 3602 | if (CancelRegion == OMPD_parallel) |
| 3603 | CancelKind = CancelParallel; |
| 3604 | else if (CancelRegion == OMPD_for) |
| 3605 | CancelKind = CancelLoop; |
| 3606 | else if (CancelRegion == OMPD_sections) |
| 3607 | CancelKind = CancelSections; |
| 3608 | else { |
| 3609 | assert(CancelRegion == OMPD_taskgroup); |
| 3610 | CancelKind = CancelTaskgroup; |
| 3611 | } |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3612 | return CancelKind; |
| 3613 | } |
| 3614 | |
| 3615 | void CGOpenMPRuntime::emitCancellationPointCall( |
| 3616 | CodeGenFunction &CGF, SourceLocation Loc, |
| 3617 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3618 | if (!CGF.HaveInsertPoint()) |
| 3619 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3620 | // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32 |
| 3621 | // global_tid, kmp_int32 cncl_kind); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3622 | if (auto *OMPRegionInfo = |
| 3623 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3624 | if (OMPRegionInfo->hasCancel()) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3625 | llvm::Value *Args[] = { |
| 3626 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3627 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3628 | // Ignore return result until untied tasks are supported. |
| 3629 | auto *Result = CGF.EmitRuntimeCall( |
| 3630 | createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args); |
| 3631 | // if (__kmpc_cancellationpoint()) { |
| 3632 | // __kmpc_cancel_barrier(); |
| 3633 | // exit from construct; |
| 3634 | // } |
| 3635 | auto *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3636 | auto *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3637 | auto *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| 3638 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3639 | CGF.EmitBlock(ExitBB); |
| 3640 | // __kmpc_cancel_barrier(); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3641 | emitBarrierCall(CGF, Loc, OMPD_unknown, /*EmitChecks=*/false); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3642 | // exit from construct; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3643 | auto CancelDest = |
| 3644 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3645 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 3646 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3647 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 3648 | } |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3651 | void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 3652 | const Expr *IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3653 | OpenMPDirectiveKind CancelRegion) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3654 | if (!CGF.HaveInsertPoint()) |
| 3655 | return; |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3656 | // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid, |
| 3657 | // kmp_int32 cncl_kind); |
| 3658 | if (auto *OMPRegionInfo = |
| 3659 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 3660 | auto &&ThenGen = [this, Loc, CancelRegion, |
| 3661 | OMPRegionInfo](CodeGenFunction &CGF) { |
| 3662 | llvm::Value *Args[] = { |
| 3663 | emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc), |
| 3664 | CGF.Builder.getInt32(getCancellationKind(CancelRegion))}; |
| 3665 | // Ignore return result until untied tasks are supported. |
| 3666 | auto *Result = |
| 3667 | CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_cancel), Args); |
| 3668 | // if (__kmpc_cancel()) { |
| 3669 | // __kmpc_cancel_barrier(); |
| 3670 | // exit from construct; |
| 3671 | // } |
| 3672 | auto *ExitBB = CGF.createBasicBlock(".cancel.exit"); |
| 3673 | auto *ContBB = CGF.createBasicBlock(".cancel.continue"); |
| 3674 | auto *Cmp = CGF.Builder.CreateIsNotNull(Result); |
| 3675 | CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB); |
| 3676 | CGF.EmitBlock(ExitBB); |
| 3677 | // __kmpc_cancel_barrier(); |
| 3678 | emitBarrierCall(CGF, Loc, OMPD_unknown, /*EmitChecks=*/false); |
| 3679 | // exit from construct; |
| 3680 | auto CancelDest = |
| 3681 | CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind()); |
| 3682 | CGF.EmitBranchThroughCleanup(CancelDest); |
| 3683 | CGF.EmitBlock(ContBB, /*IsFinished=*/true); |
| 3684 | }; |
| 3685 | if (IfCond) |
| 3686 | emitOMPIfClause(CGF, IfCond, ThenGen, [](CodeGenFunction &) {}); |
| 3687 | else |
| 3688 | ThenGen(CGF); |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3689 | } |
| 3690 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3691 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3692 | /// \brief Obtain information that uniquely identifies a target entry. This |
| 3693 | /// consists of the file and device IDs as well as line and column numbers |
| 3694 | /// associated with the relevant entry source location. |
| 3695 | static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc, |
| 3696 | unsigned &DeviceID, unsigned &FileID, |
| 3697 | unsigned &LineNum, unsigned &ColumnNum) { |
| 3698 | |
| 3699 | auto &SM = C.getSourceManager(); |
| 3700 | |
| 3701 | // The loc should be always valid and have a file ID (the user cannot use |
| 3702 | // #pragma directives in macros) |
| 3703 | |
| 3704 | assert(Loc.isValid() && "Source location is expected to be always valid."); |
| 3705 | assert(Loc.isFileID() && "Source location is expected to refer to a file."); |
| 3706 | |
| 3707 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 3708 | assert(PLoc.isValid() && "Source location is expected to be always valid."); |
| 3709 | |
| 3710 | llvm::sys::fs::UniqueID ID; |
| 3711 | if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) |
| 3712 | llvm_unreachable("Source file with target region no longer exists!"); |
| 3713 | |
| 3714 | DeviceID = ID.getDevice(); |
| 3715 | FileID = ID.getFile(); |
| 3716 | LineNum = PLoc.getLine(); |
| 3717 | ColumnNum = PLoc.getColumn(); |
| 3718 | return; |
| 3719 | } |
| 3720 | |
| 3721 | void CGOpenMPRuntime::emitTargetOutlinedFunction( |
| 3722 | const OMPExecutableDirective &D, StringRef ParentName, |
| 3723 | llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, |
| 3724 | bool IsOffloadEntry) { |
| 3725 | |
| 3726 | assert(!ParentName.empty() && "Invalid target region parent name!"); |
| 3727 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3728 | const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt()); |
| 3729 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3730 | // Emit target region as a standalone region. |
| 3731 | auto &&CodeGen = [&CS](CodeGenFunction &CGF) { |
| 3732 | CGF.EmitStmt(CS.getCapturedStmt()); |
| 3733 | }; |
| 3734 | |
| 3735 | // Create a unique name for the proxy/entry function that using the source |
| 3736 | // location information of the current target region. The name will be |
| 3737 | // something like: |
| 3738 | // |
| 3739 | // .omp_offloading.DD_FFFF.PP.lBB.cCC |
| 3740 | // |
| 3741 | // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the |
| 3742 | // mangled name of the function that encloses the target region, BB is the |
| 3743 | // line number of the target region, and CC is the column number of the target |
| 3744 | // region. |
| 3745 | |
| 3746 | unsigned DeviceID; |
| 3747 | unsigned FileID; |
| 3748 | unsigned Line; |
| 3749 | unsigned Column; |
| 3750 | getTargetEntryUniqueInfo(CGM.getContext(), D.getLocStart(), DeviceID, FileID, |
| 3751 | Line, Column); |
| 3752 | SmallString<64> EntryFnName; |
| 3753 | { |
| 3754 | llvm::raw_svector_ostream OS(EntryFnName); |
| 3755 | OS << ".omp_offloading" << llvm::format(".%x", DeviceID) |
| 3756 | << llvm::format(".%x.", FileID) << ParentName << ".l" << Line << ".c" |
| 3757 | << Column; |
| 3758 | } |
| 3759 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3760 | CodeGenFunction CGF(CGM, true); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3761 | CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3762 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3763 | |
| 3764 | OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS); |
| 3765 | |
| 3766 | // If this target outline function is not an offload entry, we don't need to |
| 3767 | // register it. |
| 3768 | if (!IsOffloadEntry) |
| 3769 | return; |
| 3770 | |
| 3771 | // The target region ID is used by the runtime library to identify the current |
| 3772 | // target region, so it only has to be unique and not necessarily point to |
| 3773 | // anything. It could be the pointer to the outlined function that implements |
| 3774 | // the target region, but we aren't using that so that the compiler doesn't |
| 3775 | // need to keep that, and could therefore inline the host function if proven |
| 3776 | // worthwhile during optimization. In the other hand, if emitting code for the |
| 3777 | // device, the ID has to be the function address so that it can retrieved from |
| 3778 | // the offloading entry and launched by the runtime library. We also mark the |
| 3779 | // outlined function to have external linkage in case we are emitting code for |
| 3780 | // the device, because these functions will be entry points to the device. |
| 3781 | |
| 3782 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3783 | OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy); |
| 3784 | OutlinedFn->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3785 | } else |
| 3786 | OutlinedFnID = new llvm::GlobalVariable( |
| 3787 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true, |
| 3788 | llvm::GlobalValue::PrivateLinkage, |
| 3789 | llvm::Constant::getNullValue(CGM.Int8Ty), ".omp_offload.region_id"); |
| 3790 | |
| 3791 | // Register the information for the entry associated with this target region. |
| 3792 | OffloadEntriesInfoManager.registerTargetRegionEntryInfo( |
| 3793 | DeviceID, FileID, ParentName, Line, Column, OutlinedFn, OutlinedFnID); |
| 3794 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF, |
| 3798 | const OMPExecutableDirective &D, |
| 3799 | llvm::Value *OutlinedFn, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3800 | llvm::Value *OutlinedFnID, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3801 | const Expr *IfCond, const Expr *Device, |
| 3802 | ArrayRef<llvm::Value *> CapturedVars) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3803 | if (!CGF.HaveInsertPoint()) |
| 3804 | return; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3805 | /// \brief Values for bit flags used to specify the mapping type for |
| 3806 | /// offloading. |
| 3807 | enum OpenMPOffloadMappingFlags { |
| 3808 | /// \brief Allocate memory on the device and move data from host to device. |
| 3809 | OMP_MAP_TO = 0x01, |
| 3810 | /// \brief Allocate memory on the device and move data from device to host. |
| 3811 | OMP_MAP_FROM = 0x02, |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3812 | /// \brief The element passed to the device is a pointer. |
| 3813 | OMP_MAP_PTR = 0x20, |
| 3814 | /// \brief Pass the element to the device by value. |
| 3815 | OMP_MAP_BYCOPY = 0x80, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3816 | }; |
| 3817 | |
| 3818 | enum OpenMPOffloadingReservedDeviceIDs { |
| 3819 | /// \brief Device ID if the device was not defined, runtime should get it |
| 3820 | /// from environment variables in the spec. |
| 3821 | OMP_DEVICEID_UNDEF = -1, |
| 3822 | }; |
| 3823 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3824 | assert(OutlinedFn && "Invalid outlined function!"); |
| 3825 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3826 | auto &Ctx = CGF.getContext(); |
| 3827 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3828 | // Fill up the arrays with the all the captured variables. |
| 3829 | SmallVector<llvm::Value *, 16> BasePointers; |
| 3830 | SmallVector<llvm::Value *, 16> Pointers; |
| 3831 | SmallVector<llvm::Value *, 16> Sizes; |
| 3832 | SmallVector<unsigned, 16> MapTypes; |
| 3833 | |
| 3834 | bool hasVLACaptures = false; |
| 3835 | |
| 3836 | const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt()); |
| 3837 | auto RI = CS.getCapturedRecordDecl()->field_begin(); |
| 3838 | // auto II = CS.capture_init_begin(); |
| 3839 | auto CV = CapturedVars.begin(); |
| 3840 | for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(), |
| 3841 | CE = CS.capture_end(); |
| 3842 | CI != CE; ++CI, ++RI, ++CV) { |
| 3843 | StringRef Name; |
| 3844 | QualType Ty; |
| 3845 | llvm::Value *BasePointer; |
| 3846 | llvm::Value *Pointer; |
| 3847 | llvm::Value *Size; |
| 3848 | unsigned MapType; |
| 3849 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3850 | // VLA sizes are passed to the outlined region by copy. |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3851 | if (CI->capturesVariableArrayType()) { |
| 3852 | BasePointer = Pointer = *CV; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3853 | Size = CGF.getTypeSize(RI->getType()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3854 | // Copy to the device as an argument. No need to retrieve it. |
| 3855 | MapType = OMP_MAP_BYCOPY; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3856 | hasVLACaptures = true; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3857 | } else if (CI->capturesThis()) { |
| 3858 | BasePointer = Pointer = *CV; |
| 3859 | const PointerType *PtrTy = cast<PointerType>(RI->getType().getTypePtr()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3860 | Size = CGF.getTypeSize(PtrTy->getPointeeType()); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3861 | // Default map type. |
| 3862 | MapType = OMP_MAP_TO | OMP_MAP_FROM; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3863 | } else if (CI->capturesVariableByCopy()) { |
| 3864 | MapType = OMP_MAP_BYCOPY; |
| 3865 | if (!RI->getType()->isAnyPointerType()) { |
| 3866 | // If the field is not a pointer, we need to save the actual value and |
| 3867 | // load it as a void pointer. |
| 3868 | auto DstAddr = CGF.CreateMemTemp( |
| 3869 | Ctx.getUIntPtrType(), |
| 3870 | Twine(CI->getCapturedVar()->getName()) + ".casted"); |
| 3871 | LValue DstLV = CGF.MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 3872 | |
| 3873 | auto *SrcAddrVal = CGF.EmitScalarConversion( |
| 3874 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
| 3875 | Ctx.getPointerType(RI->getType()), SourceLocation()); |
| 3876 | LValue SrcLV = |
| 3877 | CGF.MakeNaturalAlignAddrLValue(SrcAddrVal, RI->getType()); |
| 3878 | |
| 3879 | // Store the value using the source type pointer. |
| 3880 | CGF.EmitStoreThroughLValue(RValue::get(*CV), SrcLV); |
| 3881 | |
| 3882 | // Load the value using the destination type pointer. |
| 3883 | BasePointer = Pointer = |
| 3884 | CGF.EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal(); |
| 3885 | } else { |
| 3886 | MapType |= OMP_MAP_PTR; |
| 3887 | BasePointer = Pointer = *CV; |
| 3888 | } |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3889 | Size = CGF.getTypeSize(RI->getType()); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3890 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3891 | assert(CI->capturesVariable() && "Expected captured reference."); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3892 | BasePointer = Pointer = *CV; |
| 3893 | |
| 3894 | const ReferenceType *PtrTy = |
| 3895 | cast<ReferenceType>(RI->getType().getTypePtr()); |
| 3896 | QualType ElementType = PtrTy->getPointeeType(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame^] | 3897 | Size = CGF.getTypeSize(ElementType); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3898 | // The default map type for a scalar/complex type is 'to' because by |
| 3899 | // default the value doesn't have to be retrieved. For an aggregate type, |
| 3900 | // the default is 'tofrom'. |
| 3901 | MapType = ElementType->isAggregateType() ? (OMP_MAP_TO | OMP_MAP_FROM) |
| 3902 | : OMP_MAP_TO; |
| 3903 | if (ElementType->isAnyPointerType()) |
| 3904 | MapType |= OMP_MAP_PTR; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | BasePointers.push_back(BasePointer); |
| 3908 | Pointers.push_back(Pointer); |
| 3909 | Sizes.push_back(Size); |
| 3910 | MapTypes.push_back(MapType); |
| 3911 | } |
| 3912 | |
| 3913 | // Keep track on whether the host function has to be executed. |
| 3914 | auto OffloadErrorQType = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3915 | Ctx.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3916 | auto OffloadError = CGF.MakeAddrLValue( |
| 3917 | CGF.CreateMemTemp(OffloadErrorQType, ".run_host_version"), |
| 3918 | OffloadErrorQType); |
| 3919 | CGF.EmitStoreOfScalar(llvm::Constant::getNullValue(CGM.Int32Ty), |
| 3920 | OffloadError); |
| 3921 | |
| 3922 | // Fill up the pointer arrays and transfer execution to the device. |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3923 | auto &&ThenGen = [this, &Ctx, &BasePointers, &Pointers, &Sizes, &MapTypes, |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3924 | hasVLACaptures, Device, OutlinedFnID, OffloadError, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3925 | OffloadErrorQType](CodeGenFunction &CGF) { |
| 3926 | unsigned PointerNumVal = BasePointers.size(); |
| 3927 | llvm::Value *PointerNum = CGF.Builder.getInt32(PointerNumVal); |
| 3928 | llvm::Value *BasePointersArray; |
| 3929 | llvm::Value *PointersArray; |
| 3930 | llvm::Value *SizesArray; |
| 3931 | llvm::Value *MapTypesArray; |
| 3932 | |
| 3933 | if (PointerNumVal) { |
| 3934 | llvm::APInt PointerNumAP(32, PointerNumVal, /*isSigned=*/true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3935 | QualType PointerArrayType = Ctx.getConstantArrayType( |
| 3936 | Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3937 | /*IndexTypeQuals=*/0); |
| 3938 | |
| 3939 | BasePointersArray = |
| 3940 | CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer(); |
| 3941 | PointersArray = |
| 3942 | CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer(); |
| 3943 | |
| 3944 | // If we don't have any VLA types, we can use a constant array for the map |
| 3945 | // sizes, otherwise we need to fill up the arrays as we do for the |
| 3946 | // pointers. |
| 3947 | if (hasVLACaptures) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3948 | QualType SizeArrayType = Ctx.getConstantArrayType( |
| 3949 | Ctx.getSizeType(), PointerNumAP, ArrayType::Normal, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3950 | /*IndexTypeQuals=*/0); |
| 3951 | SizesArray = |
| 3952 | CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer(); |
| 3953 | } else { |
| 3954 | // We expect all the sizes to be constant, so we collect them to create |
| 3955 | // a constant array. |
| 3956 | SmallVector<llvm::Constant *, 16> ConstSizes; |
| 3957 | for (auto S : Sizes) |
| 3958 | ConstSizes.push_back(cast<llvm::Constant>(S)); |
| 3959 | |
| 3960 | auto *SizesArrayInit = llvm::ConstantArray::get( |
| 3961 | llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes); |
| 3962 | auto *SizesArrayGbl = new llvm::GlobalVariable( |
| 3963 | CGM.getModule(), SizesArrayInit->getType(), |
| 3964 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| 3965 | SizesArrayInit, ".offload_sizes"); |
| 3966 | SizesArrayGbl->setUnnamedAddr(true); |
| 3967 | SizesArray = SizesArrayGbl; |
| 3968 | } |
| 3969 | |
| 3970 | // The map types are always constant so we don't need to generate code to |
| 3971 | // fill arrays. Instead, we create an array constant. |
| 3972 | llvm::Constant *MapTypesArrayInit = |
| 3973 | llvm::ConstantDataArray::get(CGF.Builder.getContext(), MapTypes); |
| 3974 | auto *MapTypesArrayGbl = new llvm::GlobalVariable( |
| 3975 | CGM.getModule(), MapTypesArrayInit->getType(), |
| 3976 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, |
| 3977 | MapTypesArrayInit, ".offload_maptypes"); |
| 3978 | MapTypesArrayGbl->setUnnamedAddr(true); |
| 3979 | MapTypesArray = MapTypesArrayGbl; |
| 3980 | |
| 3981 | for (unsigned i = 0; i < PointerNumVal; ++i) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3982 | |
| 3983 | llvm::Value *BPVal = BasePointers[i]; |
| 3984 | if (BPVal->getType()->isPointerTy()) |
| 3985 | BPVal = CGF.Builder.CreateBitCast(BPVal, CGM.VoidPtrTy); |
| 3986 | else { |
| 3987 | assert(BPVal->getType()->isIntegerTy() && |
| 3988 | "If not a pointer, the value type must be an integer."); |
| 3989 | BPVal = CGF.Builder.CreateIntToPtr(BPVal, CGM.VoidPtrTy); |
| 3990 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3991 | llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 3992 | llvm::ArrayType::get(CGM.VoidPtrTy, PointerNumVal), |
| 3993 | BasePointersArray, 0, i); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3994 | Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 3995 | CGF.Builder.CreateStore(BPVal, BPAddr); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3996 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3997 | llvm::Value *PVal = Pointers[i]; |
| 3998 | if (PVal->getType()->isPointerTy()) |
| 3999 | PVal = CGF.Builder.CreateBitCast(PVal, CGM.VoidPtrTy); |
| 4000 | else { |
| 4001 | assert(PVal->getType()->isIntegerTy() && |
| 4002 | "If not a pointer, the value type must be an integer."); |
| 4003 | PVal = CGF.Builder.CreateIntToPtr(PVal, CGM.VoidPtrTy); |
| 4004 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4005 | llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4006 | llvm::ArrayType::get(CGM.VoidPtrTy, PointerNumVal), PointersArray, |
| 4007 | 0, i); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 4008 | Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy)); |
| 4009 | CGF.Builder.CreateStore(PVal, PAddr); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4010 | |
| 4011 | if (hasVLACaptures) { |
| 4012 | llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4013 | llvm::ArrayType::get(CGM.SizeTy, PointerNumVal), SizesArray, |
| 4014 | /*Idx0=*/0, |
| 4015 | /*Idx1=*/i); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 4016 | Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType())); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4017 | CGF.Builder.CreateStore(CGF.Builder.CreateIntCast( |
| 4018 | Sizes[i], CGM.SizeTy, /*isSigned=*/true), |
| 4019 | SAddr); |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | BasePointersArray = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4024 | llvm::ArrayType::get(CGM.VoidPtrTy, PointerNumVal), BasePointersArray, |
| 4025 | /*Idx0=*/0, /*Idx1=*/0); |
| 4026 | PointersArray = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4027 | llvm::ArrayType::get(CGM.VoidPtrTy, PointerNumVal), PointersArray, |
| 4028 | /*Idx0=*/0, |
| 4029 | /*Idx1=*/0); |
| 4030 | SizesArray = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4031 | llvm::ArrayType::get(CGM.SizeTy, PointerNumVal), SizesArray, |
| 4032 | /*Idx0=*/0, /*Idx1=*/0); |
| 4033 | MapTypesArray = CGF.Builder.CreateConstInBoundsGEP2_32( |
| 4034 | llvm::ArrayType::get(CGM.Int32Ty, PointerNumVal), MapTypesArray, |
| 4035 | /*Idx0=*/0, |
| 4036 | /*Idx1=*/0); |
| 4037 | |
| 4038 | } else { |
| 4039 | BasePointersArray = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 4040 | PointersArray = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy); |
| 4041 | SizesArray = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo()); |
| 4042 | MapTypesArray = |
| 4043 | llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo()); |
| 4044 | } |
| 4045 | |
| 4046 | // On top of the arrays that were filled up, the target offloading call |
| 4047 | // takes as arguments the device id as well as the host pointer. The host |
| 4048 | // pointer is used by the runtime library to identify the current target |
| 4049 | // region, so it only has to be unique and not necessarily point to |
| 4050 | // anything. It could be the pointer to the outlined function that |
| 4051 | // implements the target region, but we aren't using that so that the |
| 4052 | // compiler doesn't need to keep that, and could therefore inline the host |
| 4053 | // function if proven worthwhile during optimization. |
| 4054 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4055 | // From this point on, we need to have an ID of the target region defined. |
| 4056 | assert(OutlinedFnID && "Invalid outlined function ID!"); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4057 | |
| 4058 | // Emit device ID if any. |
| 4059 | llvm::Value *DeviceID; |
| 4060 | if (Device) |
| 4061 | DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device), |
| 4062 | CGM.Int32Ty, /*isSigned=*/true); |
| 4063 | else |
| 4064 | DeviceID = CGF.Builder.getInt32(OMP_DEVICEID_UNDEF); |
| 4065 | |
| 4066 | llvm::Value *OffloadingArgs[] = { |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4067 | DeviceID, OutlinedFnID, PointerNum, BasePointersArray, |
| 4068 | PointersArray, SizesArray, MapTypesArray}; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4069 | auto Return = CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target), |
| 4070 | OffloadingArgs); |
| 4071 | |
| 4072 | CGF.EmitStoreOfScalar(Return, OffloadError); |
| 4073 | }; |
| 4074 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4075 | // Notify that the host version must be executed. |
| 4076 | auto &&ElseGen = [this, OffloadError, |
| 4077 | OffloadErrorQType](CodeGenFunction &CGF) { |
| 4078 | CGF.EmitStoreOfScalar(llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/-1u), |
| 4079 | OffloadError); |
| 4080 | }; |
| 4081 | |
| 4082 | // If we have a target function ID it means that we need to support |
| 4083 | // offloading, otherwise, just execute on the host. We need to execute on host |
| 4084 | // regardless of the conditional in the if clause if, e.g., the user do not |
| 4085 | // specify target triples. |
| 4086 | if (OutlinedFnID) { |
| 4087 | if (IfCond) { |
| 4088 | emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen); |
| 4089 | } else { |
| 4090 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 4091 | ThenGen(CGF); |
| 4092 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4093 | } else { |
| 4094 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4095 | ElseGen(CGF); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | // Check the error code and execute the host version if required. |
| 4099 | auto OffloadFailedBlock = CGF.createBasicBlock("omp_offload.failed"); |
| 4100 | auto OffloadContBlock = CGF.createBasicBlock("omp_offload.cont"); |
| 4101 | auto OffloadErrorVal = CGF.EmitLoadOfScalar(OffloadError, SourceLocation()); |
| 4102 | auto Failed = CGF.Builder.CreateIsNotNull(OffloadErrorVal); |
| 4103 | CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock); |
| 4104 | |
| 4105 | CGF.EmitBlock(OffloadFailedBlock); |
| 4106 | CGF.Builder.CreateCall(OutlinedFn, BasePointers); |
| 4107 | CGF.EmitBranch(OffloadContBlock); |
| 4108 | |
| 4109 | CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true); |
| 4110 | return; |
| 4111 | } |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4112 | |
| 4113 | void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, |
| 4114 | StringRef ParentName) { |
| 4115 | if (!S) |
| 4116 | return; |
| 4117 | |
| 4118 | // If we find a OMP target directive, codegen the outline function and |
| 4119 | // register the result. |
| 4120 | // FIXME: Add other directives with target when they become supported. |
| 4121 | bool isTargetDirective = isa<OMPTargetDirective>(S); |
| 4122 | |
| 4123 | if (isTargetDirective) { |
| 4124 | auto *E = cast<OMPExecutableDirective>(S); |
| 4125 | unsigned DeviceID; |
| 4126 | unsigned FileID; |
| 4127 | unsigned Line; |
| 4128 | unsigned Column; |
| 4129 | getTargetEntryUniqueInfo(CGM.getContext(), E->getLocStart(), DeviceID, |
| 4130 | FileID, Line, Column); |
| 4131 | |
| 4132 | // Is this a target region that should not be emitted as an entry point? If |
| 4133 | // so just signal we are done with this target region. |
| 4134 | if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo( |
| 4135 | DeviceID, FileID, ParentName, Line, Column)) |
| 4136 | return; |
| 4137 | |
| 4138 | llvm::Function *Fn; |
| 4139 | llvm::Constant *Addr; |
| 4140 | emitTargetOutlinedFunction(*E, ParentName, Fn, Addr, |
| 4141 | /*isOffloadEntry=*/true); |
| 4142 | assert(Fn && Addr && "Target region emission failed."); |
| 4143 | return; |
| 4144 | } |
| 4145 | |
| 4146 | if (const OMPExecutableDirective *E = dyn_cast<OMPExecutableDirective>(S)) { |
| 4147 | if (!E->getAssociatedStmt()) |
| 4148 | return; |
| 4149 | |
| 4150 | scanForTargetRegionsFunctions( |
| 4151 | cast<CapturedStmt>(E->getAssociatedStmt())->getCapturedStmt(), |
| 4152 | ParentName); |
| 4153 | return; |
| 4154 | } |
| 4155 | |
| 4156 | // If this is a lambda function, look into its body. |
| 4157 | if (auto *L = dyn_cast<LambdaExpr>(S)) |
| 4158 | S = L->getBody(); |
| 4159 | |
| 4160 | // Keep looking for target regions recursively. |
| 4161 | for (auto *II : S->children()) |
| 4162 | scanForTargetRegionsFunctions(II, ParentName); |
| 4163 | |
| 4164 | return; |
| 4165 | } |
| 4166 | |
| 4167 | bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) { |
| 4168 | auto &FD = *cast<FunctionDecl>(GD.getDecl()); |
| 4169 | |
| 4170 | // If emitting code for the host, we do not process FD here. Instead we do |
| 4171 | // the normal code generation. |
| 4172 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4173 | return false; |
| 4174 | |
| 4175 | // Try to detect target regions in the function. |
| 4176 | scanForTargetRegionsFunctions(FD.getBody(), CGM.getMangledName(GD)); |
| 4177 | |
| 4178 | // We should not emit any function othen that the ones created during the |
| 4179 | // scanning. Therefore, we signal that this function is completely dealt |
| 4180 | // with. |
| 4181 | return true; |
| 4182 | } |
| 4183 | |
| 4184 | bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) { |
| 4185 | if (!CGM.getLangOpts().OpenMPIsDevice) |
| 4186 | return false; |
| 4187 | |
| 4188 | // Check if there are Ctors/Dtors in this declaration and look for target |
| 4189 | // regions in it. We use the complete variant to produce the kernel name |
| 4190 | // mangling. |
| 4191 | QualType RDTy = cast<VarDecl>(GD.getDecl())->getType(); |
| 4192 | if (auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 4193 | for (auto *Ctor : RD->ctors()) { |
| 4194 | StringRef ParentName = |
| 4195 | CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete)); |
| 4196 | scanForTargetRegionsFunctions(Ctor->getBody(), ParentName); |
| 4197 | } |
| 4198 | auto *Dtor = RD->getDestructor(); |
| 4199 | if (Dtor) { |
| 4200 | StringRef ParentName = |
| 4201 | CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete)); |
| 4202 | scanForTargetRegionsFunctions(Dtor->getBody(), ParentName); |
| 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | // If we are in target mode we do not emit any global (declare target is not |
| 4207 | // implemented yet). Therefore we signal that GD was processed in this case. |
| 4208 | return true; |
| 4209 | } |
| 4210 | |
| 4211 | bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) { |
| 4212 | auto *VD = GD.getDecl(); |
| 4213 | if (isa<FunctionDecl>(VD)) |
| 4214 | return emitTargetFunctions(GD); |
| 4215 | |
| 4216 | return emitTargetGlobalVariable(GD); |
| 4217 | } |
| 4218 | |
| 4219 | llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() { |
| 4220 | // If we have offloading in the current module, we need to emit the entries |
| 4221 | // now and register the offloading descriptor. |
| 4222 | createOffloadEntriesAndInfoMetadata(); |
| 4223 | |
| 4224 | // Create and register the offloading binary descriptors. This is the main |
| 4225 | // entity that captures all the information about offloading in the current |
| 4226 | // compilation unit. |
| 4227 | return createOffloadingBinaryDescriptorRegistration(); |
| 4228 | } |