Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:48:11

0001 //===- LoopGeneratorsKMP.h - IR helper to create loops ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains functions to create scalar and OpenMP parallel loops
0010 // as LLVM-IR.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 #ifndef POLLY_LOOP_GENERATORS_KMP_H
0014 #define POLLY_LOOP_GENERATORS_KMP_H
0015 
0016 #include "polly/CodeGen/IRBuilder.h"
0017 #include "polly/CodeGen/LoopGenerators.h"
0018 #include "polly/Support/ScopHelper.h"
0019 #include "llvm/ADT/SetVector.h"
0020 
0021 namespace polly {
0022 using llvm::GlobalValue;
0023 using llvm::GlobalVariable;
0024 
0025 /// This ParallelLoopGenerator subclass handles the generation of parallelized
0026 /// code, utilizing the LLVM OpenMP library.
0027 class ParallelLoopGeneratorKMP final : public ParallelLoopGenerator {
0028 public:
0029   /// Create a parallel loop generator for the current function.
0030   ParallelLoopGeneratorKMP(PollyIRBuilder &Builder, const DataLayout &DL)
0031       : ParallelLoopGenerator(Builder, DL) {
0032     SourceLocationInfo = createSourceLocation();
0033   }
0034 
0035 protected:
0036   /// The source location struct of this loop.
0037   /// ident_t = type { i32, i32, i32, i32, i8* }
0038   GlobalValue *SourceLocationInfo;
0039 
0040   /// Convert the combination of given chunk size and scheduling type (which
0041   /// might have been set via the command line) into the corresponding
0042   /// scheduling type. This may result (e.g.) in a 'change' from
0043   /// "static chunked" scheduling to "static non-chunked" (regarding the
0044   /// provided and returned scheduling types).
0045   ///
0046   /// @param ChunkSize    The chunk size, set via command line or its default.
0047   /// @param Scheduling   The scheduling, set via command line or its default.
0048   ///
0049   /// @return The corresponding OMPGeneralSchedulingType.
0050   OMPGeneralSchedulingType
0051   getSchedType(int ChunkSize, OMPGeneralSchedulingType Scheduling) const;
0052 
0053   /// Returns True if 'LongType' is 64bit wide, otherwise: False.
0054   bool is64BitArch();
0055 
0056 public:
0057   // The functions below may be used if one does not want to generate a
0058   // specific OpenMP parallel loop, but generate individual parts of it
0059   // (e.g. the subfunction definition).
0060 
0061   /// Create a runtime library call to spawn the worker threads.
0062   ///
0063   /// @param SubFn      The subfunction which holds the loop body.
0064   /// @param SubFnParam The parameter for the subfunction (basically the struct
0065   ///                   filled with the outside values).
0066   /// @param LB         The lower bound for the loop we parallelize.
0067   /// @param UB         The upper bound for the loop we parallelize.
0068   /// @param Stride     The stride of the loop we parallelize.
0069   void createCallSpawnThreads(Value *SubFn, Value *SubFnParam, Value *LB,
0070                               Value *UB, Value *Stride);
0071 
0072   void deployParallelExecution(Function *SubFn, Value *SubFnParam, Value *LB,
0073                                Value *UB, Value *Stride) override;
0074 
0075   Function *prepareSubFnDefinition(Function *F) const override;
0076 
0077   std::tuple<Value *, Function *> createSubFn(Value *Stride, AllocaInst *Struct,
0078                                               SetVector<Value *> UsedValues,
0079                                               ValueMapT &VMap) override;
0080 
0081   /// Create a runtime library call to get the current global thread number.
0082   ///
0083   /// @return A Value ref which holds the current global thread number.
0084   Value *createCallGlobalThreadNum();
0085 
0086   /// Create a runtime library call to request a number of threads.
0087   /// Which will be used in the next OpenMP section (by the next fork).
0088   ///
0089   /// @param GlobalThreadID   The global thread ID.
0090   /// @param NumThreads       The number of threads to use.
0091   void createCallPushNumThreads(Value *GlobalThreadID, Value *NumThreads);
0092 
0093   /// Create a runtime library call to prepare the OpenMP runtime.
0094   /// For dynamically scheduled loops, saving the loop arguments.
0095   ///
0096   /// @param GlobalThreadID   The global thread ID.
0097   /// @param LB               The loop's lower bound.
0098   /// @param UB               The loop's upper bound.
0099   /// @param Inc              The loop increment.
0100   /// @param ChunkSize        The chunk size of the parallel loop.
0101   void createCallDispatchInit(Value *GlobalThreadID, Value *LB, Value *UB,
0102                               Value *Inc, Value *ChunkSize);
0103 
0104   /// Create a runtime library call to retrieve the next (dynamically)
0105   /// allocated chunk of work for this thread.
0106   ///
0107   /// @param GlobalThreadID   The global thread ID.
0108   /// @param IsLastPtr        Pointer to a flag, which is set to 1 if this is
0109   ///                         the last chunk of work, or 0 otherwise.
0110   /// @param LBPtr            Pointer to the lower bound for the next chunk.
0111   /// @param UBPtr            Pointer to the upper bound for the next chunk.
0112   /// @param StridePtr        Pointer to the stride for the next chunk.
0113   ///
0114   /// @return A Value which holds 1 if there is work to be done, 0 otherwise.
0115   Value *createCallDispatchNext(Value *GlobalThreadID, Value *IsLastPtr,
0116                                 Value *LBPtr, Value *UBPtr, Value *StridePtr);
0117 
0118   /// Create a runtime library call to prepare the OpenMP runtime.
0119   /// For statically scheduled loops, saving the loop arguments.
0120   ///
0121   /// @param GlobalThreadID   The global thread ID.
0122   /// @param IsLastPtr        Pointer to a flag, which is set to 1 if this is
0123   ///                         the last chunk of work, or 0 otherwise.
0124   /// @param LBPtr            Pointer to the lower bound for the next chunk.
0125   /// @param UBPtr            Pointer to the upper bound for the next chunk.
0126   /// @param StridePtr        Pointer to the stride for the next chunk.
0127   /// @param ChunkSize        The chunk size of the parallel loop.
0128   void createCallStaticInit(Value *GlobalThreadID, Value *IsLastPtr,
0129                             Value *LBPtr, Value *UBPtr, Value *StridePtr,
0130                             Value *ChunkSize);
0131 
0132   /// Create a runtime library call to mark the end of
0133   /// a statically scheduled loop.
0134   ///
0135   /// @param GlobalThreadID   The global thread ID.
0136   void createCallStaticFini(Value *GlobalThreadID);
0137 
0138   /// Create the current source location.
0139   ///
0140   /// TODO: Generates only(!) dummy values.
0141   GlobalVariable *createSourceLocation();
0142 };
0143 } // end namespace polly
0144 #endif