LLVM 20.0.0git
OMP.cpp
Go to the documentation of this file.
1//===- OMP.cpp ------ Collection of helpers for OpenMP --------------------===//
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
10
11#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
17
18#include <algorithm>
19#include <cstdio>
20#include <iterator>
21#include <string>
22#include <type_traits>
23
24using namespace llvm;
25using namespace llvm::omp;
26
27#define GEN_DIRECTIVES_IMPL
28#include "llvm/Frontend/OpenMP/OMP.inc"
29
32 // OpenMP Spec 5.2: [17.3, 8-9]
33 // If directive-name-A and directive-name-B both correspond to loop-
34 // associated constructs then directive-name is a composite construct
35 // otherwise directive-name is a combined construct.
36 //
37 // In the list of leaf constructs, find the first loop-associated construct,
38 // this is the beginning of the returned range. Then, starting from the
39 // immediately following leaf construct, find the first sequence of adjacent
40 // loop-associated constructs. The last of those is the last one of the
41 // range, that is, the end of the range is one past that element.
42 // If such a sequence of adjacent loop-associated directives does not exist,
43 // return an empty range.
44 //
45 // The end of the returned range (including empty range) is intended to be
46 // a point from which the search for the next range could resume.
47 //
48 // Consequently, this function can't return a range with a single leaf
49 // construct in it.
50
51 auto firstLoopAssociated =
53 for (auto It = List.begin(), End = List.end(); It != End; ++It) {
54 if (getDirectiveAssociation(*It) == Association::Loop)
55 return It;
56 }
57 return List.end();
58 };
59
60 auto Empty = llvm::make_range(Leafs.end(), Leafs.end());
61
62 auto Begin = firstLoopAssociated(Leafs);
63 if (Begin == Leafs.end())
64 return Empty;
65
66 auto End =
67 firstLoopAssociated(llvm::make_range(std::next(Begin), Leafs.end()));
68 if (End == Leafs.end())
69 return Empty;
70
71 for (; End != Leafs.end(); ++End) {
72 if (getDirectiveAssociation(*End) != Association::Loop)
73 break;
74 }
75 return llvm::make_range(Begin, End);
76}
77
78namespace llvm::omp {
80 auto Idx = static_cast<std::size_t>(D);
81 if (Idx >= Directive_enumSize)
82 return {};
83 const auto *Row = LeafConstructTable[LeafConstructTableOrdering[Idx]];
84 return ArrayRef(&Row[2], static_cast<int>(Row[1]));
85}
86
88 if (auto Leafs = getLeafConstructs(D); !Leafs.empty())
89 return Leafs;
90 auto Idx = static_cast<size_t>(D);
91 assert(Idx < Directive_enumSize && "Invalid directive");
92 const auto *Row = LeafConstructTable[LeafConstructTableOrdering[Idx]];
93 // The first entry in the row is the directive itself.
94 return ArrayRef(&Row[0], &Row[0] + 1);
95}
96
99 using ArrayTy = ArrayRef<Directive>;
100 using IteratorTy = ArrayTy::iterator;
102
103 IteratorTy Iter = Leafs.begin();
104 do {
105 auto Range = getFirstCompositeRange(llvm::make_range(Iter, Leafs.end()));
106 // All directives before the range are leaf constructs.
107 for (; Iter != Range.begin(); ++Iter)
108 Output.push_back(*Iter);
109 if (!Range.empty()) {
110 Directive Comp =
111 getCompoundConstruct(ArrayTy(Range.begin(), Range.end()));
112 assert(Comp != OMPD_unknown);
113 Output.push_back(Comp);
114 Iter = Range.end();
115 // As of now, a composite construct must contain all constituent leaf
116 // constructs from some point until the end of all constituent leaf
117 // constructs.
118 assert(Iter == Leafs.end() && "Malformed directive");
119 }
120 } while (Iter != Leafs.end());
121
122 return Output;
123}
124
126 if (Parts.empty())
127 return OMPD_unknown;
128
129 // Parts don't have to be leafs, so expand them into leafs first.
130 // Store the expanded leafs in the same format as rows in the leaf
131 // table (generated by tablegen).
132 SmallVector<Directive> RawLeafs(2);
133 for (Directive P : Parts) {
135 if (!Ls.empty())
136 RawLeafs.append(Ls.begin(), Ls.end());
137 else
138 RawLeafs.push_back(P);
139 }
140
141 // RawLeafs will be used as key in the binary search. The search doesn't
142 // guarantee that the exact same entry will be found (since RawLeafs may
143 // not correspond to any compound directive). Because of that, we will
144 // need to compare the search result with the given set of leafs.
145 // Also, if there is only one leaf in the list, it corresponds to itself,
146 // no search is necessary.
147 auto GivenLeafs{ArrayRef<Directive>(RawLeafs).drop_front(2)};
148 if (GivenLeafs.size() == 1)
149 return GivenLeafs.front();
150 RawLeafs[1] = static_cast<Directive>(GivenLeafs.size());
151
152 auto Iter = std::lower_bound(
153 LeafConstructTable, LeafConstructTableEndDirective,
154 static_cast<std::decay_t<decltype(*LeafConstructTable)>>(RawLeafs.data()),
155 [](const llvm::omp::Directive *RowA, const llvm::omp::Directive *RowB) {
156 const auto *BeginA = &RowA[2];
157 const auto *EndA = BeginA + static_cast<int>(RowA[1]);
158 const auto *BeginB = &RowB[2];
159 const auto *EndB = BeginB + static_cast<int>(RowB[1]);
160 if (BeginA == EndA && BeginB == EndB)
161 return static_cast<int>(RowA[0]) < static_cast<int>(RowB[0]);
162 return std::lexicographical_compare(BeginA, EndA, BeginB, EndB);
163 });
164
165 if (Iter == std::end(LeafConstructTable))
166 return OMPD_unknown;
167
168 // Verify that we got a match.
169 Directive Found = (*Iter)[0];
170 ArrayRef<Directive> FoundLeafs = getLeafConstructs(Found);
171 if (FoundLeafs == GivenLeafs)
172 return Found;
173 return OMPD_unknown;
174}
175
177
180 if (Leafs.size() <= 1)
181 return false;
182 auto Range = getFirstCompositeRange(Leafs);
183 return Range.begin() == Leafs.begin() && Range.end() == Leafs.end();
184}
185
187 // OpenMP Spec 5.2: [17.3, 9-10]
188 // Otherwise directive-name is a combined construct.
189 return !getLeafConstructs(D).empty() && !isCompositeConstruct(D);
190}
191
193 static unsigned Versions[]{45, 50, 51, 52, 60};
194 return Versions;
195}
196
197std::string prettifyFunctionName(StringRef FunctionName) {
198 // Internalized functions have the right name, but simply a suffix.
199 if (FunctionName.ends_with(".internalized"))
200 return FunctionName.drop_back(sizeof("internalized")).str() +
201 " (internalized)";
202 unsigned LineNo = 0;
203 auto ParentName = deconstructOpenMPKernelName(FunctionName, LineNo);
204 if (LineNo == 0)
205 return FunctionName.str();
206 return ("omp target in " + ParentName + " @ " + std::to_string(LineNo) +
207 " (" + FunctionName + ")")
208 .str();
209}
210
212 unsigned &LineNo) {
213
214 // Only handle functions with an OpenMP kernel prefix for now. Naming scheme:
215 // __omp_offloading_<hex_hash1>_<hex_hash2>_<name>_l<line>_[<count>_]<suffix>
217 return "";
218
219 auto PrettyName = KernelName.drop_front(
220 sizeof(TargetRegionEntryInfo::KernelNamePrefix) - /*'\0'*/ 1);
221 for (int I = 0; I < 3; ++I) {
222 PrettyName = PrettyName.drop_while([](char c) { return c != '_'; });
223 PrettyName = PrettyName.drop_front();
224 }
225
226 // Look for the last '_l<line>'.
227 size_t LineIdx = PrettyName.rfind("_l");
228 if (LineIdx == StringRef::npos)
229 return "";
230 if (PrettyName.drop_front(LineIdx + 2).consumeInteger(10, LineNo))
231 return "";
232 return demangle(PrettyName.take_front(LineIdx));
233}
234} // namespace llvm::omp
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
#define I(x, y, z)
Definition: MD5.cpp:58
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static iterator_range< ArrayRef< Directive >::iterator > getFirstCompositeRange(iterator_range< ArrayRef< Directive >::iterator > Leafs)
Definition: OMP.cpp:31
#define P(N)
const NodeList & List
Definition: RDFGraph.cpp:200
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:207
iterator end() const
Definition: ArrayRef.h:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:286
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:609
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:347
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:277
static constexpr size_t npos
Definition: StringRef.h:53
StringRef drop_while(function_ref< bool(char)> F) const
Return a StringRef equal to 'this', but with all characters satisfying the given predicate dropped fr...
Definition: StringRef.h:623
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:616
A range adaptor for a pair of iterators.
Definition: OMP.h:22
ArrayRef< unsigned > getOpenMPVersions()
Definition: OMP.cpp:192
bool isCombinedConstruct(Directive D)
Definition: OMP.cpp:186
std::string deconstructOpenMPKernelName(StringRef KernelName, unsigned &LineNo)
Deconstruct an OpenMP kernel name into the parent function name and the line number.
Definition: OMP.cpp:211
ArrayRef< Directive > getLeafOrCompositeConstructs(Directive D, SmallVectorImpl< Directive > &Output)
Definition: OMP.cpp:98
bool isCompositeConstruct(Directive D)
Definition: OMP.cpp:178
Directive getCompoundConstruct(ArrayRef< Directive > Parts)
Definition: OMP.cpp:125
bool isLeafConstruct(Directive D)
Definition: OMP.cpp:176
ArrayRef< Directive > getLeafConstructsOrSelf(Directive D)
Definition: OMP.cpp:87
std::string prettifyFunctionName(StringRef FunctionName)
Create a nicer version of a function name for humans to look at.
Definition: OMP.cpp:197
ArrayRef< Directive > getLeafConstructs(Directive D)
Definition: OMP.cpp:79
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:20
static constexpr const char * KernelNamePrefix
The prefix used for kernel names.
Definition: OMPIRBuilder.h:205