| 1 |
//===-- Decomposer.cpp -- Compound directive decomposition ----------------===// |
| 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 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "Decomposer.h" |
| 14 |
|
| 15 |
#include "Clauses.h" |
| 16 |
#include "Utils.h" |
| 17 |
#include "flang/Lower/PFTBuilder.h" |
| 18 |
#include "flang/Semantics/semantics.h" |
| 19 |
#include "flang/Tools/CrossToolHelpers.h" |
| 20 |
#include "mlir/IR/BuiltinOps.h" |
| 21 |
#include "llvm/ADT/ArrayRef.h" |
| 22 |
#include "llvm/ADT/STLExtras.h" |
| 23 |
#include "llvm/ADT/SmallVector.h" |
| 24 |
#include "llvm/Frontend/OpenMP/ClauseT.h" |
| 25 |
#include "llvm/Frontend/OpenMP/ConstructCompositionT.h" |
| 26 |
#include "llvm/Frontend/OpenMP/ConstructDecompositionT.h" |
| 27 |
#include "llvm/Frontend/OpenMP/OMP.h" |
| 28 |
#include "llvm/Support/raw_ostream.h" |
| 29 |
|
| 30 |
#include <optional> |
| 31 |
#include <utility> |
| 32 |
#include <variant> |
| 33 |
|
| 34 |
using namespace Fortran; |
| 35 |
|
| 36 |
namespace { |
| 37 |
using namespace Fortran::lower::omp; |
| 38 |
|
| 39 |
struct ConstructDecomposition { |
| 40 |
ConstructDecomposition(mlir::ModuleOp modOp, |
| 41 |
semantics::SemanticsContext &semaCtx, |
| 42 |
lower::pft::Evaluation &ev, |
| 43 |
llvm::omp::Directive compound, |
| 44 |
const List<Clause> &clauses) |
| 45 |
: semaCtx(semaCtx), mod(modOp), eval(ev) { |
| 46 |
tomp::ConstructDecompositionT decompose(getOpenMPVersionAttribute(modOp), |
| 47 |
*this, compound, |
| 48 |
llvm::ArrayRef(clauses)); |
| 49 |
output = std::move(decompose.output); |
| 50 |
} |
| 51 |
|
| 52 |
// Given an object, return its base object if one exists. |
| 53 |
std::optional<Object> getBaseObject(const Object &object) { |
| 54 |
return lower::omp::getBaseObject(object, semaCtx); |
| 55 |
} |
| 56 |
|
| 57 |
// Return the iteration variable of the associated loop if any. |
| 58 |
std::optional<Object> getLoopIterVar() { |
| 59 |
if (semantics::Symbol *symbol = getIterationVariableSymbol(eval)) |
| 60 |
return Object{symbol, /*designator=*/{}}; |
| 61 |
return std::nullopt; |
| 62 |
} |
| 63 |
|
| 64 |
semantics::SemanticsContext &semaCtx; |
| 65 |
mlir::ModuleOp mod; |
| 66 |
lower::pft::Evaluation &eval; |
| 67 |
List<UnitConstruct> output; |
| 68 |
}; |
| 69 |
} // namespace |
| 70 |
|
| 71 |
static UnitConstruct mergeConstructs(uint32_t version, |
| 72 |
llvm::ArrayRef<UnitConstruct> units) { |
| 73 |
tomp::ConstructCompositionT compose(version, units); |
| 74 |
return compose.merged; |
| 75 |
} |
| 76 |
|
| 77 |
namespace Fortran::lower::omp { |
| 78 |
LLVM_DUMP_METHOD llvm::raw_ostream &operator<<(llvm::raw_ostream &os, |
| 79 |
const UnitConstruct &uc) { |
| 80 |
os << llvm::omp::getOpenMPDirectiveName(uc.id); |
| 81 |
for (auto [index, clause] : llvm::enumerate(uc.clauses)) { |
| 82 |
os << (index == 0 ? '\t' : ' '); |
| 83 |
os << llvm::omp::getOpenMPClauseName(clause.id); |
| 84 |
} |
| 85 |
return os; |
| 86 |
} |
| 87 |
|
| 88 |
ConstructQueue buildConstructQueue( |
| 89 |
mlir::ModuleOp modOp, Fortran::semantics::SemanticsContext &semaCtx, |
| 90 |
Fortran::lower::pft::Evaluation &eval, const parser::CharBlock &source, |
| 91 |
llvm::omp::Directive compound, const List<Clause> &clauses) { |
| 92 |
|
| 93 |
List<UnitConstruct> constructs; |
| 94 |
|
| 95 |
ConstructDecomposition decompose(modOp, semaCtx, eval, compound, clauses); |
| 96 |
assert(!decompose.output.empty() && "Construct decomposition failed"); |
| 97 |
|
| 98 |
llvm::SmallVector<llvm::omp::Directive> loweringUnits; |
| 99 |
std::ignore = |
| 100 |
llvm::omp::getLeafOrCompositeConstructs(compound, loweringUnits); |
| 101 |
uint32_t version = getOpenMPVersionAttribute(modOp); |
| 102 |
|
| 103 |
int leafIndex = 0; |
| 104 |
for (llvm::omp::Directive dir_id : loweringUnits) { |
| 105 |
llvm::ArrayRef<llvm::omp::Directive> leafsOrSelf = |
| 106 |
llvm::omp::getLeafConstructsOrSelf(dir_id); |
| 107 |
size_t numLeafs = leafsOrSelf.size(); |
| 108 |
|
| 109 |
llvm::ArrayRef<UnitConstruct> toMerge{&decompose.output[leafIndex], |
| 110 |
numLeafs}; |
| 111 |
auto &uc = constructs.emplace_back(mergeConstructs(version, toMerge)); |
| 112 |
|
| 113 |
if (!transferLocations(clauses, uc.clauses)) { |
| 114 |
// If some clauses are left without source information, use the |
| 115 |
// directive's source. |
| 116 |
for (auto &clause : uc.clauses) { |
| 117 |
if (clause.source.empty()) |
| 118 |
clause.source = source; |
| 119 |
} |
| 120 |
} |
| 121 |
leafIndex += numLeafs; |
| 122 |
} |
| 123 |
|
| 124 |
return constructs; |
| 125 |
} |
| 126 |
|
| 127 |
bool isLastItemInQueue(ConstructQueue::iterator item, |
| 128 |
const ConstructQueue &queue) { |
| 129 |
return std::next(item) == queue.end(); |
| 130 |
} |
| 131 |
} // namespace Fortran::lower::omp |