|
| 1 | +//===- LowerWorkshare.cpp - special cases for bufferization -------===// |
| 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 implements the lowering of omp.workdistribute. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include <flang/Optimizer/Builder/FIRBuilder.h> |
| 14 | +#include <flang/Optimizer/Dialect/FIROps.h> |
| 15 | +#include <flang/Optimizer/Dialect/FIRType.h> |
| 16 | +#include <flang/Optimizer/HLFIR/HLFIROps.h> |
| 17 | +#include <flang/Optimizer/OpenMP/Passes.h> |
| 18 | +#include <llvm/ADT/BreadthFirstIterator.h> |
| 19 | +#include <llvm/ADT/STLExtras.h> |
| 20 | +#include <llvm/ADT/SmallVectorExtras.h> |
| 21 | +#include <llvm/ADT/iterator_range.h> |
| 22 | +#include <llvm/Support/ErrorHandling.h> |
| 23 | +#include <mlir/Dialect/Arith/IR/Arith.h> |
| 24 | +#include <mlir/Dialect/LLVMIR/LLVMTypes.h> |
| 25 | +#include <mlir/Dialect/OpenMP/OpenMPClauseOperands.h> |
| 26 | +#include <mlir/Dialect/OpenMP/OpenMPDialect.h> |
| 27 | +#include <mlir/Dialect/SCF/IR/SCF.h> |
| 28 | +#include <mlir/IR/BuiltinOps.h> |
| 29 | +#include <mlir/IR/IRMapping.h> |
| 30 | +#include <mlir/IR/OpDefinition.h> |
| 31 | +#include <mlir/IR/PatternMatch.h> |
| 32 | +#include <mlir/IR/Value.h> |
| 33 | +#include <mlir/IR/Visitors.h> |
| 34 | +#include <mlir/Interfaces/SideEffectInterfaces.h> |
| 35 | +#include <mlir/Support/LLVM.h> |
| 36 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 37 | + |
| 38 | +#include <variant> |
| 39 | + |
| 40 | +namespace flangomp { |
| 41 | +#define GEN_PASS_DEF_LOWERWORKDISTRIBUTE |
| 42 | +#include "flang/Optimizer/OpenMP/Passes.h.inc" |
| 43 | +} // namespace flangomp |
| 44 | + |
| 45 | +#define DEBUG_TYPE "lower-workdistribute" |
| 46 | + |
| 47 | +using namespace mlir; |
| 48 | + |
| 49 | +namespace { |
| 50 | + |
| 51 | +struct WorkdistributeToSingle : public mlir::OpRewritePattern<mlir::omp::WorkdistributeOp> { |
| 52 | +using OpRewritePattern::OpRewritePattern; |
| 53 | +mlir::LogicalResult |
| 54 | + matchAndRewrite(mlir::omp::WorkdistributeOp workdistribute, |
| 55 | + mlir::PatternRewriter &rewriter) const override { |
| 56 | + auto loc = workdistribute->getLoc(); |
| 57 | + auto teams = llvm::dyn_cast<mlir::omp::TeamsOp>(workdistribute->getParentOp()); |
| 58 | + if (!teams) { |
| 59 | + mlir::emitError(loc, "workdistribute not nested in teams\n"); |
| 60 | + return mlir::failure(); |
| 61 | + } |
| 62 | + if (workdistribute.getRegion().getBlocks().size() != 1) { |
| 63 | + mlir::emitError(loc, "workdistribute with multiple blocks\n"); |
| 64 | + return mlir::failure(); |
| 65 | + } |
| 66 | + if (teams.getRegion().getBlocks().size() != 1) { |
| 67 | + mlir::emitError(loc, "teams with multiple blocks\n"); |
| 68 | + return mlir::failure(); |
| 69 | + } |
| 70 | + if (teams.getRegion().getBlocks().front().getOperations().size() != 2) { |
| 71 | + mlir::emitError(loc, "teams with multiple nested ops\n"); |
| 72 | + return mlir::failure(); |
| 73 | + } |
| 74 | + mlir::Block *workdistributeBlock = &workdistribute.getRegion().front(); |
| 75 | + rewriter.eraseOp(workdistributeBlock->getTerminator()); |
| 76 | + rewriter.inlineBlockBefore(workdistributeBlock, teams); |
| 77 | + rewriter.eraseOp(teams); |
| 78 | + return mlir::success(); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +class LowerWorkdistributePass |
| 83 | + : public flangomp::impl::LowerWorkdistributeBase<LowerWorkdistributePass> { |
| 84 | +public: |
| 85 | + void runOnOperation() override { |
| 86 | + mlir::MLIRContext &context = getContext(); |
| 87 | + mlir::RewritePatternSet patterns(&context); |
| 88 | + mlir::GreedyRewriteConfig config; |
| 89 | + // prevent the pattern driver form merging blocks |
| 90 | + config.setRegionSimplificationLevel( |
| 91 | + mlir::GreedySimplifyRegionLevel::Disabled); |
| 92 | + |
| 93 | + patterns.insert<WorkdistributeToSingle>(&context); |
| 94 | + mlir::Operation *op = getOperation(); |
| 95 | + if (mlir::failed(mlir::applyPatternsGreedily(op, std::move(patterns), config))) { |
| 96 | + mlir::emitError(op->getLoc(), DEBUG_TYPE " pass failed\n"); |
| 97 | + signalPassFailure(); |
| 98 | + } |
| 99 | + } |
| 100 | +}; |
| 101 | +} |
0 commit comments