|
| 1 | +//===- IR/OpenMPIRBuilder.h - OpenMP encoding builder for LLVM IR - 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 | +// This file defines the OpenMPIRBuilder class and helpers used as a convenient |
| 10 | +// way to create LLVM instructions for OpenMP directives. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef LLVM_OPENMP_IR_IRBUILDER_H |
| 15 | +#define LLVM_OPENMP_IR_IRBUILDER_H |
| 16 | + |
| 17 | +#include "llvm/IR/DebugLoc.h" |
| 18 | +#include "llvm/IR/IRBuilder.h" |
| 19 | +#include "llvm/Frontend/OpenMP/OMPConstants.h" |
| 20 | + |
| 21 | +namespace llvm { |
| 22 | + |
| 23 | +/// An interface to create LLVM-IR for OpenMP directives. |
| 24 | +/// |
| 25 | +/// Each OpenMP directive has a corresponding public generator method. |
| 26 | +class OpenMPIRBuilder { |
| 27 | +public: |
| 28 | + /// Create a new OpenMPIRBuilder operating on the given module \p M. This will |
| 29 | + /// not have an effect on \p M (see initialize). |
| 30 | + OpenMPIRBuilder(Module &M) : M(M), Builder(M.getContext()) {} |
| 31 | + |
| 32 | + /// Initialize the internal state, this will put structures types and |
| 33 | + /// potentially other helpers into the underlying module. Must be called |
| 34 | + /// before any other method and only once! |
| 35 | + void initialize(); |
| 36 | + |
| 37 | + /// Add attributes known for \p FnID to \p Fn. |
| 38 | + void addAttributes(omp::RuntimeFunction FnID, Function &Fn); |
| 39 | + |
| 40 | + /// Set the cancellation block to \p CBB. |
| 41 | + void setCancellationBlock(BasicBlock *CBB) { CancellationBlock = CBB; } |
| 42 | + |
| 43 | + /// Type used throughout for insertion points. |
| 44 | + using InsertPointTy = IRBuilder<>::InsertPoint; |
| 45 | + |
| 46 | + /// Description of a LLVM-IR insertion point (IP) and a debug/source location |
| 47 | + /// (filename, line, column, ...). |
| 48 | + struct LocationDescription { |
| 49 | + template <typename T, typename U> |
| 50 | + LocationDescription(const IRBuilder<T, U> &IRB) |
| 51 | + : IP(IRB.saveIP()), DL(IRB.getCurrentDebugLocation()) {} |
| 52 | + LocationDescription(const InsertPointTy &IP) : IP(IP) {} |
| 53 | + LocationDescription(const InsertPointTy &IP, const DebugLoc &DL) |
| 54 | + : IP(IP), DL(DL) {} |
| 55 | + InsertPointTy IP; |
| 56 | + DebugLoc DL; |
| 57 | + }; |
| 58 | + |
| 59 | + /// Emitter methods for OpenMP directives. |
| 60 | + /// |
| 61 | + ///{ |
| 62 | + |
| 63 | + /// Generator for '#omp barrier' |
| 64 | + /// |
| 65 | + /// \param Loc The location where the barrier directive was encountered. |
| 66 | + /// \param DK The kind of directive that caused the barrier. |
| 67 | + /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier. |
| 68 | + /// \param CheckCancelFlag Flag to indicate a cancel barrier return value |
| 69 | + /// should be checked and acted upon. |
| 70 | + /// |
| 71 | + /// \returns The insertion point after the barrier. |
| 72 | + InsertPointTy CreateBarrier(const LocationDescription &Loc, omp::Directive DK, |
| 73 | + bool ForceSimpleCall = false, |
| 74 | + bool CheckCancelFlag = true); |
| 75 | + |
| 76 | + ///} |
| 77 | + |
| 78 | +private: |
| 79 | + /// Update the internal location to \p Loc. |
| 80 | + bool updateToLocation(const LocationDescription &Loc) { |
| 81 | + Builder.restoreIP(Loc.IP); |
| 82 | + Builder.SetCurrentDebugLocation(Loc.DL); |
| 83 | + return Loc.IP.getBlock() != nullptr; |
| 84 | + } |
| 85 | + |
| 86 | + /// Return the function declaration for the runtime function with \p FnID. |
| 87 | + Function *getOrCreateRuntimeFunction(omp::RuntimeFunction FnID); |
| 88 | + |
| 89 | + /// Return the (LLVM-IR) string describing the source location \p LocStr. |
| 90 | + Constant *getOrCreateSrcLocStr(StringRef LocStr); |
| 91 | + |
| 92 | + /// Return the (LLVM-IR) string describing the default source location. |
| 93 | + Constant *getOrCreateDefaultSrcLocStr(); |
| 94 | + |
| 95 | + /// Return the (LLVM-IR) string describing the source location \p Loc. |
| 96 | + Constant *getOrCreateSrcLocStr(const LocationDescription &Loc); |
| 97 | + |
| 98 | + /// Return an ident_t* encoding the source location \p SrcLocStr and \p Flags. |
| 99 | + Value *getOrCreateIdent(Constant *SrcLocStr, |
| 100 | + omp::IdentFlag Flags = omp::IdentFlag(0)); |
| 101 | + |
| 102 | + /// Generate a barrier runtime call. |
| 103 | + /// |
| 104 | + /// \param Loc The location at which the request originated and is fulfilled. |
| 105 | + /// \param DK The directive which caused the barrier |
| 106 | + /// \param ForceSimpleCall Flag to force a simple (=non-cancellation) barrier. |
| 107 | + /// \param CheckCancelFlag Flag to indicate a cancel barrier return value |
| 108 | + /// should be checked and acted upon. |
| 109 | + /// |
| 110 | + /// \returns The insertion point after the barrier. |
| 111 | + InsertPointTy emitBarrierImpl(const LocationDescription &Loc, |
| 112 | + omp::Directive DK, bool ForceSimpleCall, |
| 113 | + bool CheckCancelFlag); |
| 114 | + |
| 115 | + /// Return the current thread ID. |
| 116 | + /// |
| 117 | + /// \param Ident The ident (ident_t*) describing the query origin. |
| 118 | + Value *getOrCreateThreadID(Value *Ident); |
| 119 | + |
| 120 | + /// The underlying LLVM-IR module |
| 121 | + Module &M; |
| 122 | + |
| 123 | + /// The LLVM-IR Builder used to create IR. |
| 124 | + IRBuilder<> Builder; |
| 125 | + |
| 126 | + /// TODO: Stub for a cancellation block stack. |
| 127 | + BasicBlock *CancellationBlock = nullptr; |
| 128 | + |
| 129 | + /// Map to remember source location strings |
| 130 | + StringMap<Constant *> SrcLocStrMap; |
| 131 | + |
| 132 | + /// Map to remember existing ident_t*. |
| 133 | + DenseMap<std::pair<Constant *, uint64_t>, GlobalVariable *> IdentMap; |
| 134 | +}; |
| 135 | + |
| 136 | +} // end namespace llvm |
| 137 | + |
| 138 | +#endif // LLVM_IR_IRBUILDER_H |
0 commit comments