|
8 | 8 |
|
9 | 9 | #include "llvm/Frontend/OpenMP/OMP.h"
|
10 | 10 |
|
| 11 | +#include "llvm/ADT/ArrayRef.h" |
| 12 | +#include "llvm/ADT/STLExtras.h" |
| 13 | +#include "llvm/ADT/SmallVector.h" |
11 | 14 | #include "llvm/ADT/StringRef.h"
|
12 | 15 | #include "llvm/ADT/StringSwitch.h"
|
13 | 16 | #include "llvm/Support/ErrorHandling.h"
|
14 | 17 |
|
| 18 | +#include <algorithm> |
| 19 | +#include <iterator> |
| 20 | +#include <type_traits> |
| 21 | + |
15 | 22 | using namespace llvm;
|
16 |
| -using namespace omp; |
| 23 | +using namespace llvm::omp; |
17 | 24 |
|
18 | 25 | #define GEN_DIRECTIVES_IMPL
|
19 | 26 | #include "llvm/Frontend/OpenMP/OMP.inc"
|
| 27 | + |
| 28 | +namespace llvm::omp { |
| 29 | +ArrayRef<Directive> getLeafConstructs(Directive D) { |
| 30 | + auto Idx = static_cast<int>(D); |
| 31 | + if (Idx < 0 || Idx >= static_cast<int>(Directive_enumSize)) |
| 32 | + return {}; |
| 33 | + const auto *Row = LeafConstructTable[LeafConstructTableOrdering[Idx]]; |
| 34 | + return ArrayRef(&Row[2], &Row[2] + static_cast<int>(Row[1])); |
| 35 | +} |
| 36 | + |
| 37 | +Directive getCompoundConstruct(ArrayRef<Directive> Parts) { |
| 38 | + if (Parts.empty()) |
| 39 | + return OMPD_unknown; |
| 40 | + |
| 41 | + // Parts don't have to be leafs, so expand them into leafs first. |
| 42 | + // Store the expanded leafs in the same format as rows in the leaf |
| 43 | + // table (generated by tablegen). |
| 44 | + SmallVector<Directive> RawLeafs(2); |
| 45 | + for (Directive P : Parts) { |
| 46 | + ArrayRef<Directive> Ls = getLeafConstructs(P); |
| 47 | + if (!Ls.empty()) |
| 48 | + RawLeafs.append(Ls.begin(), Ls.end()); |
| 49 | + else |
| 50 | + RawLeafs.push_back(P); |
| 51 | + } |
| 52 | + |
| 53 | + auto GivenLeafs{ArrayRef<Directive>(RawLeafs).drop_front(2)}; |
| 54 | + if (GivenLeafs.size() == 1) |
| 55 | + return GivenLeafs.front(); |
| 56 | + RawLeafs[1] = static_cast<Directive>(GivenLeafs.size()); |
| 57 | + |
| 58 | + auto Iter = llvm::lower_bound( |
| 59 | + LeafConstructTable, |
| 60 | + static_cast<std::decay_t<decltype(*LeafConstructTable)>>(RawLeafs.data()), |
| 61 | + [](const auto *RowA, const auto *RowB) { |
| 62 | + const auto *BeginA = &RowA[2]; |
| 63 | + const auto *EndA = BeginA + static_cast<int>(RowA[1]); |
| 64 | + const auto *BeginB = &RowB[2]; |
| 65 | + const auto *EndB = BeginB + static_cast<int>(RowB[1]); |
| 66 | + if (BeginA == EndA && BeginB == EndB) |
| 67 | + return static_cast<int>(RowA[0]) < static_cast<int>(RowB[0]); |
| 68 | + return std::lexicographical_compare(BeginA, EndA, BeginB, EndB); |
| 69 | + }); |
| 70 | + |
| 71 | + if (Iter == std::end(LeafConstructTable)) |
| 72 | + return OMPD_unknown; |
| 73 | + |
| 74 | + // Verify that we got a match. |
| 75 | + Directive Found = (*Iter)[0]; |
| 76 | + ArrayRef<Directive> FoundLeafs = getLeafConstructs(Found); |
| 77 | + if (FoundLeafs == GivenLeafs) |
| 78 | + return Found; |
| 79 | + return OMPD_unknown; |
| 80 | +} |
| 81 | +} // namespace llvm::omp |
0 commit comments