1 //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides a class for OpenMP runtime code generation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
16 
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/OpenMPKinds.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/IR/ValueHandle.h"
24 
25 namespace llvm {
26 class ArrayType;
27 class Constant;
28 class Function;
29 class FunctionType;
30 class GlobalVariable;
31 class StructType;
32 class Type;
33 class Value;
34 } // namespace llvm
35 
36 namespace clang {
37 class Expr;
38 class OMPExecutableDirective;
39 class VarDecl;
40 
41 namespace CodeGen {
42 
43 class CodeGenFunction;
44 class CodeGenModule;
45 
46 typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
47 
48 class CGOpenMPRuntime {
49 private:
50   enum OpenMPRTLFunction {
51     /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
52     /// kmpc_micro microtask, ...);
53     OMPRTL__kmpc_fork_call,
54     /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
55     /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
56     OMPRTL__kmpc_threadprivate_cached,
57     /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
58     /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
59     OMPRTL__kmpc_threadprivate_register,
60     // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
61     OMPRTL__kmpc_global_thread_num,
62     // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
63     // kmp_critical_name *crit);
64     OMPRTL__kmpc_critical,
65     // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
66     // kmp_critical_name *crit);
67     OMPRTL__kmpc_end_critical,
68     // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
69     // global_tid);
70     OMPRTL__kmpc_cancel_barrier,
71     // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
72     OMPRTL__kmpc_for_static_fini,
73     // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
74     // global_tid);
75     OMPRTL__kmpc_serialized_parallel,
76     // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
77     // global_tid);
78     OMPRTL__kmpc_end_serialized_parallel,
79     // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
80     // kmp_int32 num_threads);
81     OMPRTL__kmpc_push_num_threads,
82     // Call to void __kmpc_flush(ident_t *loc);
83     OMPRTL__kmpc_flush,
84     // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
85     OMPRTL__kmpc_master,
86     // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
87     OMPRTL__kmpc_end_master,
88     // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
89     // int end_part);
90     OMPRTL__kmpc_omp_taskyield,
91     // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
92     OMPRTL__kmpc_single,
93     // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
94     OMPRTL__kmpc_end_single,
95     // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
96     // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
97     // kmp_routine_entry_t *task_entry);
98     OMPRTL__kmpc_omp_task_alloc,
99     // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
100     // new_task);
101     OMPRTL__kmpc_omp_task,
102     // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
103     // kmp_int32 cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
104     // kmp_int32 didit);
105     OMPRTL__kmpc_copyprivate,
106     // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
107     // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
108     // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
109     OMPRTL__kmpc_reduce,
110     // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
111     // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
112     // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
113     // *lck);
114     OMPRTL__kmpc_reduce_nowait,
115     // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
116     // kmp_critical_name *lck);
117     OMPRTL__kmpc_end_reduce,
118     // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
119     // kmp_critical_name *lck);
120     OMPRTL__kmpc_end_reduce_nowait,
121   };
122 
123   /// \brief Values for bit flags used in the ident_t to describe the fields.
124   /// All enumeric elements are named and described in accordance with the code
125   /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
126   enum OpenMPLocationFlags {
127     /// \brief Use trampoline for internal microtask.
128     OMP_IDENT_IMD = 0x01,
129     /// \brief Use c-style ident structure.
130     OMP_IDENT_KMPC = 0x02,
131     /// \brief Atomic reduction option for kmpc_reduce.
132     OMP_ATOMIC_REDUCE = 0x10,
133     /// \brief Explicit 'barrier' directive.
134     OMP_IDENT_BARRIER_EXPL = 0x20,
135     /// \brief Implicit barrier in code.
136     OMP_IDENT_BARRIER_IMPL = 0x40,
137     /// \brief Implicit barrier in 'for' directive.
138     OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
139     /// \brief Implicit barrier in 'sections' directive.
140     OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
141     /// \brief Implicit barrier in 'single' directive.
142     OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
143   };
144   CodeGenModule &CGM;
145   /// \brief Default const ident_t object used for initialization of all other
146   /// ident_t objects.
147   llvm::Constant *DefaultOpenMPPSource;
148   /// \brief Map of flags and corresponding default locations.
149   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
150   OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
151   llvm::Value *getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
152   /// \brief Describes ident structure that describes a source location.
153   /// All descriptions are taken from
154   /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
155   /// Original structure:
156   /// typedef struct ident {
157   ///    kmp_int32 reserved_1;   /**<  might be used in Fortran;
158   ///                                  see above  */
159   ///    kmp_int32 flags;        /**<  also f.flags; KMP_IDENT_xxx flags;
160   ///                                  KMP_IDENT_KMPC identifies this union
161   ///                                  member  */
162   ///    kmp_int32 reserved_2;   /**<  not really used in Fortran any more;
163   ///                                  see above */
164   ///#if USE_ITT_BUILD
165   ///                            /*  but currently used for storing
166   ///                                region-specific ITT */
167   ///                            /*  contextual information. */
168   ///#endif /* USE_ITT_BUILD */
169   ///    kmp_int32 reserved_3;   /**< source[4] in Fortran, do not use for
170   ///                                 C++  */
171   ///    char const *psource;    /**< String describing the source location.
172   ///                            The string is composed of semi-colon separated
173   //                             fields which describe the source file,
174   ///                            the function and a pair of line numbers that
175   ///                            delimit the construct.
176   ///                             */
177   /// } ident_t;
178   enum IdentFieldIndex {
179     /// \brief might be used in Fortran
180     IdentField_Reserved_1,
181     /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
182     IdentField_Flags,
183     /// \brief Not really used in Fortran any more
184     IdentField_Reserved_2,
185     /// \brief Source[4] in Fortran, do not use for C++
186     IdentField_Reserved_3,
187     /// \brief String describing the source location. The string is composed of
188     /// semi-colon separated fields which describe the source file, the function
189     /// and a pair of line numbers that delimit the construct.
190     IdentField_PSource
191   };
192   llvm::StructType *IdentTy;
193   /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
194   typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
195   OpenMPDebugLocMapTy OpenMPDebugLocMap;
196   /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
197   /// Original representation is:
198   /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
199   llvm::FunctionType *Kmpc_MicroTy;
200   /// \brief Stores debug location and ThreadID for the function.
201   struct DebugLocThreadIdTy {
202     llvm::Value *DebugLoc;
203     llvm::Value *ThreadID;
204   };
205   /// \brief Map of local debug location, ThreadId and functions.
206   typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
207       OpenMPLocThreadIDMapTy;
208   OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
209   /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
210   /// kmp_critical_name[8];
211   llvm::ArrayType *KmpCriticalNameTy;
212   /// \brief An ordered map of auto-generated variables to their unique names.
213   /// It stores variables with the following names: 1) ".gomp_critical_user_" +
214   /// <critical_section_name> + ".var" for "omp critical" directives; 2)
215   /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
216   /// variables.
217   llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
218       InternalVars;
219   /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
220   llvm::Type *KmpRoutineEntryPtrTy;
221   QualType KmpRoutineEntryPtrQTy;
222 
223   /// \brief Build type kmp_routine_entry_t (if not built yet).
224   void emitKmpRoutineEntryT(QualType KmpInt32Ty);
225 
226   /// \brief Emits object of ident_t type with info for source location.
227   /// \param Flags Flags for OpenMP location.
228   ///
229   llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
230                                   OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
231 
232   /// \brief Returns pointer to ident_t type.
233   llvm::Type *getIdentTyPointerTy();
234 
235   /// \brief Returns pointer to kmpc_micro type.
236   llvm::Type *getKmpc_MicroPointerTy();
237 
238   /// \brief Returns specified OpenMP runtime function.
239   /// \param Function OpenMP runtime function.
240   /// \return Specified function.
241   llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
242 
243   /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
244   /// size \a IVSize and sign \a IVSigned.
245   llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
246 
247   /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
248   /// size \a IVSize and sign \a IVSigned.
249   llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
250 
251   /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
252   /// size \a IVSize and sign \a IVSigned.
253   llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
254 
255   /// \brief If the specified mangled name is not in the module, create and
256   /// return threadprivate cache object. This object is a pointer's worth of
257   /// storage that's reserved for use by the OpenMP runtime.
258   /// \param VD Threadprivate variable.
259   /// \return Cache variable for the specified threadprivate.
260   llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
261 
262   /// \brief Emits address of the word in a memory where current thread id is
263   /// stored.
264   virtual llvm::Value *emitThreadIDAddress(CodeGenFunction &CGF,
265                                            SourceLocation Loc);
266 
267   /// \brief Gets thread id value for the current thread.
268   ///
269   llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
270 
271   /// \brief Gets (if variable with the given name already exist) or creates
272   /// internal global variable with the specified Name. The created variable has
273   /// linkage CommonLinkage by default and is initialized by null value.
274   /// \param Ty Type of the global variable. If it is exist already the type
275   /// must be the same.
276   /// \param Name Name of the variable.
277   llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
278                                               const llvm::Twine &Name);
279 
280   /// \brief Set of threadprivate variables with the generated initializer.
281   llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
282 
283   /// \brief Emits initialization code for the threadprivate variables.
284   /// \param VDAddr Address of the global variable \a VD.
285   /// \param Ctor Pointer to a global init function for \a VD.
286   /// \param CopyCtor Pointer to a global copy function for \a VD.
287   /// \param Dtor Pointer to a global destructor function for \a VD.
288   /// \param Loc Location of threadprivate declaration.
289   void emitThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
290                                 llvm::Value *Ctor, llvm::Value *CopyCtor,
291                                 llvm::Value *Dtor, SourceLocation Loc);
292 
293   /// \brief Returns corresponding lock object for the specified critical region
294   /// name. If the lock object does not exist it is created, otherwise the
295   /// reference to the existing copy is returned.
296   /// \param CriticalName Name of the critical region.
297   ///
298   llvm::Value *getCriticalRegionLock(StringRef CriticalName);
299 
300 public:
301   explicit CGOpenMPRuntime(CodeGenModule &CGM);
~CGOpenMPRuntime()302   virtual ~CGOpenMPRuntime() {}
303   virtual void clear();
304 
305   /// \brief Emits outlined function for the specified OpenMP parallel directive
306   /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
307   /// kmp_int32 BoundID, struct context_vars*).
308   /// \param D OpenMP directive.
309   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
310   /// \param CodeGen Code generation sequence for the \a D directive.
311   virtual llvm::Value *
312   emitParallelOutlinedFunction(const OMPExecutableDirective &D,
313                                const VarDecl *ThreadIDVar,
314                                const RegionCodeGenTy &CodeGen);
315 
316   /// \brief Emits outlined function for the OpenMP task directive \a D. This
317   /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
318   /// PartID, struct context_vars*).
319   /// \param D OpenMP directive.
320   /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
321   /// \param CodeGen Code generation sequence for the \a D directive.
322   ///
323   virtual llvm::Value *emitTaskOutlinedFunction(const OMPExecutableDirective &D,
324                                                 const VarDecl *ThreadIDVar,
325                                                 const RegionCodeGenTy &CodeGen);
326 
327   /// \brief Cleans up references to the objects in finished function.
328   ///
329   void functionFinished(CodeGenFunction &CGF);
330 
331   /// \brief Emits code for parallel call of the \a OutlinedFn with variables
332   /// captured in a record which address is stored in \a CapturedStruct.
333   /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
334   /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
335   /// \param CapturedStruct A pointer to the record with the references to
336   /// variables used in \a OutlinedFn function.
337   ///
338   virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
339                                 llvm::Value *OutlinedFn,
340                                 llvm::Value *CapturedStruct);
341 
342   /// \brief Emits code for serial call of the \a OutlinedFn with variables
343   /// captured in a record which address is stored in \a CapturedStruct.
344   /// \param OutlinedFn Outlined function to be run in serial mode.
345   /// \param CapturedStruct A pointer to the record with the references to
346   /// variables used in \a OutlinedFn function.
347   ///
348   virtual void emitSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
349                               llvm::Value *OutlinedFn,
350                               llvm::Value *CapturedStruct);
351 
352   /// \brief Emits a critical region.
353   /// \param CriticalName Name of the critical region.
354   /// \param CriticalOpGen Generator for the statement associated with the given
355   /// critical region.
356   virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
357                                   const RegionCodeGenTy &CriticalOpGen,
358                                   SourceLocation Loc);
359 
360   /// \brief Emits a master region.
361   /// \param MasterOpGen Generator for the statement associated with the given
362   /// master region.
363   virtual void emitMasterRegion(CodeGenFunction &CGF,
364                                 const RegionCodeGenTy &MasterOpGen,
365                                 SourceLocation Loc);
366 
367   /// \brief Emits code for a taskyield directive.
368   virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
369 
370   /// \brief Emits a single region.
371   /// \param SingleOpGen Generator for the statement associated with the given
372   /// single region.
373   virtual void emitSingleRegion(CodeGenFunction &CGF,
374                                 const RegionCodeGenTy &SingleOpGen,
375                                 SourceLocation Loc,
376                                 ArrayRef<const Expr *> CopyprivateVars,
377                                 ArrayRef<const Expr *> DestExprs,
378                                 ArrayRef<const Expr *> SrcExprs,
379                                 ArrayRef<const Expr *> AssignmentOps);
380 
381   /// \brief Emit an implicit/explicit barrier for OpenMP threads.
382   /// \param Kind Directive for which this implicit barrier call must be
383   /// generated. Must be OMPD_barrier for explicit barrier generation.
384   ///
385   virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
386                                OpenMPDirectiveKind Kind);
387 
388   /// \brief Check if the specified \a ScheduleKind is static non-chunked.
389   /// This kind of worksharing directive is emitted without outer loop.
390   /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
391   /// \param Chunked True if chunk is specified in the clause.
392   ///
393   virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
394                                   bool Chunked) const;
395 
396   /// \brief Check if the specified \a ScheduleKind is dynamic.
397   /// This kind of worksharing directive is emitted without outer loop.
398   /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
399   ///
400   virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
401 
402   /// \brief Call the appropriate runtime routine to initialize it before start
403   /// of loop.
404   ///
405   /// Depending on the loop schedule, it is nesessary to call some runtime
406   /// routine before start of the OpenMP loop to get the loop upper / lower
407   /// bounds \a LB and \a UB and stride \a ST.
408   ///
409   /// \param CGF Reference to current CodeGenFunction.
410   /// \param Loc Clang source location.
411   /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
412   /// \param IVSize Size of the iteration variable in bits.
413   /// \param IVSigned Sign of the interation variable.
414   /// \param IL Address of the output variable in which the flag of the
415   /// last iteration is returned.
416   /// \param LB Address of the output variable in which the lower iteration
417   /// number is returned.
418   /// \param UB Address of the output variable in which the upper iteration
419   /// number is returned.
420   /// \param ST Address of the output variable in which the stride value is
421   /// returned nesessary to generated the static_chunked scheduled loop.
422   /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
423   /// For the default (nullptr) value, the chunk 1 will be used.
424   ///
425   virtual void emitForInit(CodeGenFunction &CGF, SourceLocation Loc,
426                            OpenMPScheduleClauseKind SchedKind, unsigned IVSize,
427                            bool IVSigned, llvm::Value *IL, llvm::Value *LB,
428                            llvm::Value *UB, llvm::Value *ST,
429                            llvm::Value *Chunk = nullptr);
430 
431   /// \brief Call the appropriate runtime routine to notify that we finished
432   /// all the work with current loop.
433   ///
434   /// \param CGF Reference to current CodeGenFunction.
435   /// \param Loc Clang source location.
436   /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
437   ///
438   virtual void emitForFinish(CodeGenFunction &CGF, SourceLocation Loc,
439                              OpenMPScheduleClauseKind ScheduleKind);
440 
441   /// Call __kmpc_dispatch_next(
442   ///          ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
443   ///          kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
444   ///          kmp_int[32|64] *p_stride);
445   /// \param IVSize Size of the iteration variable in bits.
446   /// \param IVSigned Sign of the interation variable.
447   /// \param IL Address of the output variable in which the flag of the
448   /// last iteration is returned.
449   /// \param LB Address of the output variable in which the lower iteration
450   /// number is returned.
451   /// \param UB Address of the output variable in which the upper iteration
452   /// number is returned.
453   /// \param ST Address of the output variable in which the stride value is
454   /// returned.
455   virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
456                                    unsigned IVSize, bool IVSigned,
457                                    llvm::Value *IL, llvm::Value *LB,
458                                    llvm::Value *UB, llvm::Value *ST);
459 
460   /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
461   /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
462   /// clause.
463   /// \param NumThreads An integer value of threads.
464   virtual void emitNumThreadsClause(CodeGenFunction &CGF,
465                                     llvm::Value *NumThreads,
466                                     SourceLocation Loc);
467 
468   /// \brief Returns address of the threadprivate variable for the current
469   /// thread.
470   /// \param VD Threadprivate variable.
471   /// \param VDAddr Address of the global variable \a VD.
472   /// \param Loc Location of the reference to threadprivate var.
473   /// \return Address of the threadprivate variable for the current thread.
474   virtual llvm::Value *getAddrOfThreadPrivate(CodeGenFunction &CGF,
475                                               const VarDecl *VD,
476                                               llvm::Value *VDAddr,
477                                               SourceLocation Loc);
478 
479   /// \brief Emit a code for initialization of threadprivate variable. It emits
480   /// a call to runtime library which adds initial value to the newly created
481   /// threadprivate variable (if it is not constant) and registers destructor
482   /// for the variable (if any).
483   /// \param VD Threadprivate variable.
484   /// \param VDAddr Address of the global variable \a VD.
485   /// \param Loc Location of threadprivate declaration.
486   /// \param PerformInit true if initialization expression is not constant.
487   virtual llvm::Function *
488   emitThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
489                                  SourceLocation Loc, bool PerformInit,
490                                  CodeGenFunction *CGF = nullptr);
491 
492   /// \brief Emit flush of the variables specified in 'omp flush' directive.
493   /// \param Vars List of variables to flush.
494   virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
495                          SourceLocation Loc);
496 
497   /// \brief Emit task region for the task directive. The task region is
498   /// emmitted in several steps:
499   /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
500   /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
501   /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
502   /// function:
503   /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
504   ///   TaskFunction(gtid, tt->part_id, tt->shareds);
505   ///   return 0;
506   /// }
507   /// 2. Copy a list of shared variables to field shareds of the resulting
508   /// structure kmp_task_t returned by the previous call (if any).
509   /// 3. Copy a pointer to destructions function to field destructions of the
510   /// resulting structure kmp_task_t.
511   /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
512   /// kmp_task_t *new_task), where new_task is a resulting structure from
513   /// previous items.
514   /// \param Tied true if the task is tied (the task is tied to the thread that
515   /// can suspend its task region), false - untied (the task is not tied to any
516   /// thread).
517   /// \param Final Contains either constant bool value, or llvm::Value * of i1
518   /// type for final clause. If the value is true, the task forces all of its
519   /// child tasks to become final and included tasks.
520   /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
521   /// /*part_id*/, captured_struct */*__context*/);
522   /// \param SharedsTy A type which contains references the shared variables.
523   /// \param Shareds Context with the list of shared variables from the \a
524   /// TaskFunction.
525   virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, bool Tied,
526                             llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
527                             llvm::Value *TaskFunction, QualType SharedsTy,
528                             llvm::Value *Shareds);
529 
530   /// \brief Emit code for the directive that does not require outlining.
531   ///
532   /// \param CodeGen Code generation sequence for the \a D directive.
533   virtual void emitInlinedDirective(CodeGenFunction &CGF,
534                                     const RegionCodeGenTy &CodeGen);
535   /// \brief Emit a code for reduction clause. Next code should be emitted for
536   /// reduction:
537   /// \code
538   ///
539   /// static kmp_critical_name lock = { 0 };
540   ///
541   /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
542   ///  ...
543   ///  *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
544   ///  ...
545   /// }
546   ///
547   /// ...
548   /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
549   /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
550   /// RedList, reduce_func, &<lock>)) {
551   /// case 1:
552   ///  ...
553   ///  <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
554   ///  ...
555   /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
556   /// break;
557   /// case 2:
558   ///  ...
559   ///  Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
560   ///  ...
561   /// break;
562   /// default:;
563   /// }
564   /// \endcode
565   ///
566   /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
567   /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
568   /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
569   /// or 'operator binop(LHS, RHS)'.
570   /// \param WithNowait true if parent directive has also nowait clause, false
571   /// otherwise.
572   virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
573                              ArrayRef<const Expr *> LHSExprs,
574                              ArrayRef<const Expr *> RHSExprs,
575                              ArrayRef<const Expr *> ReductionOps,
576                              bool WithNowait);
577 };
578 
579 } // namespace CodeGen
580 } // namespace clang
581 
582 #endif
583