1 //===- IPO/OpenMPOpt.h - Collection of OpenMP optimizations -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_TRANSFORMS_IPO_OPENMP_OPT_H
10 #define LLVM_TRANSFORMS_IPO_OPENMP_OPT_H
11 
12 #include "llvm/Analysis/CGSCCPassManager.h"
13 #include "llvm/Analysis/LazyCallGraph.h"
14 #include "llvm/IR/PassManager.h"
15 
16 namespace llvm {
17 
18 namespace omp {
19 
20 /// Summary of a kernel (=entry point for target offloading).
21 using Kernel = Function *;
22 
23 /// Helper to remember if the module contains OpenMP (runtime calls), to be used
24 /// foremost with containsOpenMP.
25 struct OpenMPInModule {
26   OpenMPInModule &operator=(bool Found) {
27     if (Found)
28       Value = OpenMPInModule::OpenMP::FOUND;
29     else
30       Value = OpenMPInModule::OpenMP::NOT_FOUND;
31     return *this;
32   }
33   bool isKnown() { return Value != OpenMP::UNKNOWN; }
34   operator bool() { return Value != OpenMP::NOT_FOUND; }
35 
36   /// Does this function \p F contain any OpenMP runtime calls?
37   bool containsOMPRuntimeCalls(Function *F) const {
38     return FuncsWithOMPRuntimeCalls.contains(F);
39   }
40 
41   /// Return the known kernels (=GPU entry points) in the module.
42   SmallPtrSetImpl<Kernel> &getKernels() { return Kernels; }
43 
44   /// Identify kernels in the module and populate the Kernels set.
45   void identifyKernels(Module &M);
46 
47 private:
48   enum class OpenMP { FOUND, NOT_FOUND, UNKNOWN } Value = OpenMP::UNKNOWN;
49 
50   friend bool containsOpenMP(Module &M, OpenMPInModule &OMPInModule);
51 
52   /// In which functions are OpenMP runtime calls present?
53   SmallPtrSet<Function *, 32> FuncsWithOMPRuntimeCalls;
54 
55   /// Collection of known kernels (=GPU entry points) in the module.
56   SmallPtrSet<Kernel, 8> Kernels;
57 };
58 
59 /// Helper to determine if \p M contains OpenMP (runtime calls).
60 bool containsOpenMP(Module &M, OpenMPInModule &OMPInModule);
61 
62 } // namespace omp
63 
64 /// OpenMP optimizations pass.
65 class OpenMPOptPass : public PassInfoMixin<OpenMPOptPass> {
66   /// Helper to remember if the module contains OpenMP (runtime calls).
67   omp::OpenMPInModule OMPInModule;
68 
69 public:
70   PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
71                         LazyCallGraph &CG, CGSCCUpdateResult &UR);
72 };
73 
74 } // end namespace llvm
75 
76 #endif // LLVM_TRANSFORMS_IPO_OPENMP_OPT_H
77