1119545f4SAlex Zinenko //===- SCFToOpenMP.cpp - Structured Control Flow to OpenMP conversion -----===//
2119545f4SAlex Zinenko //
3119545f4SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4119545f4SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
5119545f4SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6119545f4SAlex Zinenko //
7119545f4SAlex Zinenko //===----------------------------------------------------------------------===//
8119545f4SAlex Zinenko //
9119545f4SAlex Zinenko // This file implements a pass to convert scf.parallel operations into OpenMP
10119545f4SAlex Zinenko // parallel loops.
11119545f4SAlex Zinenko //
12119545f4SAlex Zinenko //===----------------------------------------------------------------------===//
13119545f4SAlex Zinenko 
14119545f4SAlex Zinenko #include "mlir/Conversion/SCFToOpenMP/SCFToOpenMP.h"
15119545f4SAlex Zinenko #include "../PassDetail.h"
16755dc07dSRiver Riddle #include "mlir/Analysis/SliceAnalysis.h"
17755dc07dSRiver Riddle #include "mlir/Dialect/Affine/Analysis/LoopAnalysis.h"
18a54f4eaeSMogball #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
191ce752b7SAlex Zinenko #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2078fb4f9dSWilliam S. Moses #include "mlir/Dialect/MemRef/IR/MemRef.h"
21119545f4SAlex Zinenko #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
22*8b68da2cSAlex Zinenko #include "mlir/Dialect/SCF/IR/SCF.h"
231ce752b7SAlex Zinenko #include "mlir/IR/ImplicitLocOpBuilder.h"
241ce752b7SAlex Zinenko #include "mlir/IR/SymbolTable.h"
25119545f4SAlex Zinenko #include "mlir/Transforms/DialectConversion.h"
26119545f4SAlex Zinenko 
27119545f4SAlex Zinenko using namespace mlir;
28119545f4SAlex Zinenko 
291ce752b7SAlex Zinenko /// Matches a block containing a "simple" reduction. The expected shape of the
301ce752b7SAlex Zinenko /// block is as follows.
311ce752b7SAlex Zinenko ///
321ce752b7SAlex Zinenko ///   ^bb(%arg0, %arg1):
331ce752b7SAlex Zinenko ///     %0 = OpTy(%arg0, %arg1)
341ce752b7SAlex Zinenko ///     scf.reduce.return %0
351ce752b7SAlex Zinenko template <typename... OpTy>
matchSimpleReduction(Block & block)361ce752b7SAlex Zinenko static bool matchSimpleReduction(Block &block) {
371ce752b7SAlex Zinenko   if (block.empty() || llvm::hasSingleElement(block) ||
381ce752b7SAlex Zinenko       std::next(block.begin(), 2) != block.end())
391ce752b7SAlex Zinenko     return false;
402a876a71SDiego Caballero 
412a876a71SDiego Caballero   if (block.getNumArguments() != 2)
422a876a71SDiego Caballero     return false;
432a876a71SDiego Caballero 
442a876a71SDiego Caballero   SmallVector<Operation *, 4> combinerOps;
452a876a71SDiego Caballero   Value reducedVal = matchReduction({block.getArguments()[1]},
462a876a71SDiego Caballero                                     /*redPos=*/0, combinerOps);
472a876a71SDiego Caballero 
482a876a71SDiego Caballero   if (!reducedVal || !reducedVal.isa<BlockArgument>() ||
492a876a71SDiego Caballero       combinerOps.size() != 1)
502a876a71SDiego Caballero     return false;
512a876a71SDiego Caballero 
522a876a71SDiego Caballero   return isa<OpTy...>(combinerOps[0]) &&
531ce752b7SAlex Zinenko          isa<scf::ReduceReturnOp>(block.back()) &&
542a876a71SDiego Caballero          block.front().getOperands() == block.getArguments();
551ce752b7SAlex Zinenko }
561ce752b7SAlex Zinenko 
571ce752b7SAlex Zinenko /// Matches a block containing a select-based min/max reduction. The types of
581ce752b7SAlex Zinenko /// select and compare operations are provided as template arguments. The
591ce752b7SAlex Zinenko /// comparison predicates suitable for min and max are provided as function
601ce752b7SAlex Zinenko /// arguments. If a reduction is matched, `ifMin` will be set if the reduction
611ce752b7SAlex Zinenko /// compute the minimum and unset if it computes the maximum, otherwise it
621ce752b7SAlex Zinenko /// remains unmodified. The expected shape of the block is as follows.
631ce752b7SAlex Zinenko ///
641ce752b7SAlex Zinenko ///   ^bb(%arg0, %arg1):
651ce752b7SAlex Zinenko ///     %0 = CompareOpTy(<one-of-predicates>, %arg0, %arg1)
661ce752b7SAlex Zinenko ///     %1 = SelectOpTy(%0, %arg0, %arg1)  // %arg0, %arg1 may be swapped here.
671ce752b7SAlex Zinenko ///     scf.reduce.return %1
681ce752b7SAlex Zinenko template <
691ce752b7SAlex Zinenko     typename CompareOpTy, typename SelectOpTy,
7062fea88bSJacques Pienaar     typename Predicate = decltype(std::declval<CompareOpTy>().getPredicate())>
711ce752b7SAlex Zinenko static bool
matchSelectReduction(Block & block,ArrayRef<Predicate> lessThanPredicates,ArrayRef<Predicate> greaterThanPredicates,bool & isMin)721ce752b7SAlex Zinenko matchSelectReduction(Block &block, ArrayRef<Predicate> lessThanPredicates,
731ce752b7SAlex Zinenko                      ArrayRef<Predicate> greaterThanPredicates, bool &isMin) {
74dec8af70SRiver Riddle   static_assert(
75dec8af70SRiver Riddle       llvm::is_one_of<SelectOpTy, arith::SelectOp, LLVM::SelectOp>::value,
76dec8af70SRiver Riddle       "only arithmetic and llvm select ops are supported");
771ce752b7SAlex Zinenko 
781ce752b7SAlex Zinenko   // Expect exactly three operations in the block.
791ce752b7SAlex Zinenko   if (block.empty() || llvm::hasSingleElement(block) ||
801ce752b7SAlex Zinenko       std::next(block.begin(), 2) == block.end() ||
811ce752b7SAlex Zinenko       std::next(block.begin(), 3) != block.end())
821ce752b7SAlex Zinenko     return false;
831ce752b7SAlex Zinenko 
841ce752b7SAlex Zinenko   // Check op kinds.
851ce752b7SAlex Zinenko   auto compare = dyn_cast<CompareOpTy>(block.front());
861ce752b7SAlex Zinenko   auto select = dyn_cast<SelectOpTy>(block.front().getNextNode());
871ce752b7SAlex Zinenko   auto terminator = dyn_cast<scf::ReduceReturnOp>(block.back());
881ce752b7SAlex Zinenko   if (!compare || !select || !terminator)
891ce752b7SAlex Zinenko     return false;
901ce752b7SAlex Zinenko 
911ce752b7SAlex Zinenko   // Block arguments must be compared.
921ce752b7SAlex Zinenko   if (compare->getOperands() != block.getArguments())
931ce752b7SAlex Zinenko     return false;
941ce752b7SAlex Zinenko 
951ce752b7SAlex Zinenko   // Detect whether the comparison is less-than or greater-than, otherwise bail.
961ce752b7SAlex Zinenko   bool isLess;
97ad44495aSjacquesguan   if (llvm::is_contained(lessThanPredicates, compare.getPredicate())) {
981ce752b7SAlex Zinenko     isLess = true;
99ad44495aSjacquesguan   } else if (llvm::is_contained(greaterThanPredicates,
100ad44495aSjacquesguan                                 compare.getPredicate())) {
1011ce752b7SAlex Zinenko     isLess = false;
1021ce752b7SAlex Zinenko   } else {
1031ce752b7SAlex Zinenko     return false;
1041ce752b7SAlex Zinenko   }
1051ce752b7SAlex Zinenko 
106cfb72fd3SJacques Pienaar   if (select.getCondition() != compare.getResult())
1071ce752b7SAlex Zinenko     return false;
1081ce752b7SAlex Zinenko 
1091ce752b7SAlex Zinenko   // Detect if the operands are swapped between cmpf and select. Match the
1101ce752b7SAlex Zinenko   // comparison type with the requested type or with the opposite of the
1111ce752b7SAlex Zinenko   // requested type if the operands are swapped. Use generic accessors because
1121ce752b7SAlex Zinenko   // std and LLVM versions of select have different operand names but identical
1131ce752b7SAlex Zinenko   // positions.
1141ce752b7SAlex Zinenko   constexpr unsigned kTrueValue = 1;
1151ce752b7SAlex Zinenko   constexpr unsigned kFalseValue = 2;
116cfb72fd3SJacques Pienaar   bool sameOperands = select.getOperand(kTrueValue) == compare.getLhs() &&
117cfb72fd3SJacques Pienaar                       select.getOperand(kFalseValue) == compare.getRhs();
118cfb72fd3SJacques Pienaar   bool swappedOperands = select.getOperand(kTrueValue) == compare.getRhs() &&
119cfb72fd3SJacques Pienaar                          select.getOperand(kFalseValue) == compare.getLhs();
1201ce752b7SAlex Zinenko   if (!sameOperands && !swappedOperands)
1211ce752b7SAlex Zinenko     return false;
1221ce752b7SAlex Zinenko 
123c0342a2dSJacques Pienaar   if (select.getResult() != terminator.getResult())
1241ce752b7SAlex Zinenko     return false;
1251ce752b7SAlex Zinenko 
1261ce752b7SAlex Zinenko   // The reduction is a min if it uses less-than predicates with same operands
1271ce752b7SAlex Zinenko   // or greather-than predicates with swapped operands. Similarly for max.
1281ce752b7SAlex Zinenko   isMin = (isLess && sameOperands) || (!isLess && swappedOperands);
1291ce752b7SAlex Zinenko   return isMin || (isLess & swappedOperands) || (!isLess && sameOperands);
1301ce752b7SAlex Zinenko }
1311ce752b7SAlex Zinenko 
1321ce752b7SAlex Zinenko /// Returns the float semantics for the given float type.
fltSemanticsForType(FloatType type)1331ce752b7SAlex Zinenko static const llvm::fltSemantics &fltSemanticsForType(FloatType type) {
1341ce752b7SAlex Zinenko   if (type.isF16())
1351ce752b7SAlex Zinenko     return llvm::APFloat::IEEEhalf();
1361ce752b7SAlex Zinenko   if (type.isF32())
1371ce752b7SAlex Zinenko     return llvm::APFloat::IEEEsingle();
1381ce752b7SAlex Zinenko   if (type.isF64())
1391ce752b7SAlex Zinenko     return llvm::APFloat::IEEEdouble();
1401ce752b7SAlex Zinenko   if (type.isF128())
1411ce752b7SAlex Zinenko     return llvm::APFloat::IEEEquad();
1421ce752b7SAlex Zinenko   if (type.isBF16())
1431ce752b7SAlex Zinenko     return llvm::APFloat::BFloat();
1441ce752b7SAlex Zinenko   if (type.isF80())
1451ce752b7SAlex Zinenko     return llvm::APFloat::x87DoubleExtended();
1461ce752b7SAlex Zinenko   llvm_unreachable("unknown float type");
1471ce752b7SAlex Zinenko }
1481ce752b7SAlex Zinenko 
1491ce752b7SAlex Zinenko /// Returns an attribute with the minimum (if `min` is set) or the maximum value
1501ce752b7SAlex Zinenko /// (otherwise) for the given float type.
minMaxValueForFloat(Type type,bool min)1511ce752b7SAlex Zinenko static Attribute minMaxValueForFloat(Type type, bool min) {
1521ce752b7SAlex Zinenko   auto fltType = type.cast<FloatType>();
1531ce752b7SAlex Zinenko   return FloatAttr::get(
1541ce752b7SAlex Zinenko       type, llvm::APFloat::getLargest(fltSemanticsForType(fltType), min));
1551ce752b7SAlex Zinenko }
1561ce752b7SAlex Zinenko 
1571ce752b7SAlex Zinenko /// Returns an attribute with the signed integer minimum (if `min` is set) or
1581ce752b7SAlex Zinenko /// the maximum value (otherwise) for the given integer type, regardless of its
1591ce752b7SAlex Zinenko /// signedness semantics (only the width is considered).
minMaxValueForSignedInt(Type type,bool min)1601ce752b7SAlex Zinenko static Attribute minMaxValueForSignedInt(Type type, bool min) {
1611ce752b7SAlex Zinenko   auto intType = type.cast<IntegerType>();
1621ce752b7SAlex Zinenko   unsigned bitwidth = intType.getWidth();
1631ce752b7SAlex Zinenko   return IntegerAttr::get(type, min ? llvm::APInt::getSignedMinValue(bitwidth)
1641ce752b7SAlex Zinenko                                     : llvm::APInt::getSignedMaxValue(bitwidth));
1651ce752b7SAlex Zinenko }
1661ce752b7SAlex Zinenko 
1671ce752b7SAlex Zinenko /// Returns an attribute with the unsigned integer minimum (if `min` is set) or
1681ce752b7SAlex Zinenko /// the maximum value (otherwise) for the given integer type, regardless of its
1691ce752b7SAlex Zinenko /// signedness semantics (only the width is considered).
minMaxValueForUnsignedInt(Type type,bool min)1701ce752b7SAlex Zinenko static Attribute minMaxValueForUnsignedInt(Type type, bool min) {
1711ce752b7SAlex Zinenko   auto intType = type.cast<IntegerType>();
1721ce752b7SAlex Zinenko   unsigned bitwidth = intType.getWidth();
1731ce752b7SAlex Zinenko   return IntegerAttr::get(type, min ? llvm::APInt::getNullValue(bitwidth)
1741ce752b7SAlex Zinenko                                     : llvm::APInt::getAllOnesValue(bitwidth));
1751ce752b7SAlex Zinenko }
1761ce752b7SAlex Zinenko 
1771ce752b7SAlex Zinenko /// Creates an OpenMP reduction declaration and inserts it into the provided
1781ce752b7SAlex Zinenko /// symbol table. The declaration has a constant initializer with the neutral
1791ce752b7SAlex Zinenko /// value `initValue`, and the reduction combiner carried over from `reduce`.
createDecl(PatternRewriter & builder,SymbolTable & symbolTable,scf::ReduceOp reduce,Attribute initValue)1801ce752b7SAlex Zinenko static omp::ReductionDeclareOp createDecl(PatternRewriter &builder,
1811ce752b7SAlex Zinenko                                           SymbolTable &symbolTable,
1821ce752b7SAlex Zinenko                                           scf::ReduceOp reduce,
1831ce752b7SAlex Zinenko                                           Attribute initValue) {
1841ce752b7SAlex Zinenko   OpBuilder::InsertionGuard guard(builder);
1851ce752b7SAlex Zinenko   auto decl = builder.create<omp::ReductionDeclareOp>(
186c0342a2dSJacques Pienaar       reduce.getLoc(), "__scf_reduction", reduce.getOperand().getType());
1871ce752b7SAlex Zinenko   symbolTable.insert(decl);
1881ce752b7SAlex Zinenko 
189c0342a2dSJacques Pienaar   Type type = reduce.getOperand().getType();
1901ce752b7SAlex Zinenko   builder.createBlock(&decl.initializerRegion(), decl.initializerRegion().end(),
191e084679fSRiver Riddle                       {type}, {reduce.getOperand().getLoc()});
1921ce752b7SAlex Zinenko   builder.setInsertionPointToEnd(&decl.initializerRegion().back());
1931ce752b7SAlex Zinenko   Value init =
1941ce752b7SAlex Zinenko       builder.create<LLVM::ConstantOp>(reduce.getLoc(), type, initValue);
1951ce752b7SAlex Zinenko   builder.create<omp::YieldOp>(reduce.getLoc(), init);
1961ce752b7SAlex Zinenko 
1971ce752b7SAlex Zinenko   Operation *terminator = &reduce.getRegion().front().back();
1981ce752b7SAlex Zinenko   assert(isa<scf::ReduceReturnOp>(terminator) &&
1991ce752b7SAlex Zinenko          "expected reduce op to be terminated by redure return");
2001ce752b7SAlex Zinenko   builder.setInsertionPoint(terminator);
2011ce752b7SAlex Zinenko   builder.replaceOpWithNewOp<omp::YieldOp>(terminator,
2021ce752b7SAlex Zinenko                                            terminator->getOperands());
2031ce752b7SAlex Zinenko   builder.inlineRegionBefore(reduce.getRegion(), decl.reductionRegion(),
2041ce752b7SAlex Zinenko                              decl.reductionRegion().end());
2051ce752b7SAlex Zinenko   return decl;
2061ce752b7SAlex Zinenko }
2071ce752b7SAlex Zinenko 
2081ce752b7SAlex Zinenko /// Adds an atomic reduction combiner to the given OpenMP reduction declaration
2091ce752b7SAlex Zinenko /// using llvm.atomicrmw of the given kind.
addAtomicRMW(OpBuilder & builder,LLVM::AtomicBinOp atomicKind,omp::ReductionDeclareOp decl,scf::ReduceOp reduce)2101ce752b7SAlex Zinenko static omp::ReductionDeclareOp addAtomicRMW(OpBuilder &builder,
2111ce752b7SAlex Zinenko                                             LLVM::AtomicBinOp atomicKind,
2121ce752b7SAlex Zinenko                                             omp::ReductionDeclareOp decl,
2131ce752b7SAlex Zinenko                                             scf::ReduceOp reduce) {
2141ce752b7SAlex Zinenko   OpBuilder::InsertionGuard guard(builder);
215c0342a2dSJacques Pienaar   Type type = reduce.getOperand().getType();
2161ce752b7SAlex Zinenko   Type ptrType = LLVM::LLVMPointerType::get(type);
217e084679fSRiver Riddle   Location reduceOperandLoc = reduce.getOperand().getLoc();
2181ce752b7SAlex Zinenko   builder.createBlock(&decl.atomicReductionRegion(),
219e084679fSRiver Riddle                       decl.atomicReductionRegion().end(), {ptrType, ptrType},
220e084679fSRiver Riddle                       {reduceOperandLoc, reduceOperandLoc});
2211ce752b7SAlex Zinenko   Block *atomicBlock = &decl.atomicReductionRegion().back();
2221ce752b7SAlex Zinenko   builder.setInsertionPointToEnd(atomicBlock);
2231ce752b7SAlex Zinenko   Value loaded = builder.create<LLVM::LoadOp>(reduce.getLoc(),
2241ce752b7SAlex Zinenko                                               atomicBlock->getArgument(1));
2251ce752b7SAlex Zinenko   builder.create<LLVM::AtomicRMWOp>(reduce.getLoc(), type, atomicKind,
2261ce752b7SAlex Zinenko                                     atomicBlock->getArgument(0), loaded,
2271ce752b7SAlex Zinenko                                     LLVM::AtomicOrdering::monotonic);
2281ce752b7SAlex Zinenko   builder.create<omp::YieldOp>(reduce.getLoc(), ArrayRef<Value>());
2291ce752b7SAlex Zinenko   return decl;
2301ce752b7SAlex Zinenko }
2311ce752b7SAlex Zinenko 
2321ce752b7SAlex Zinenko /// Creates an OpenMP reduction declaration that corresponds to the given SCF
2331ce752b7SAlex Zinenko /// reduction and returns it. Recognizes common reductions in order to identify
2341ce752b7SAlex Zinenko /// the neutral value, necessary for the OpenMP declaration. If the reduction
2351ce752b7SAlex Zinenko /// cannot be recognized, returns null.
declareReduction(PatternRewriter & builder,scf::ReduceOp reduce)2361ce752b7SAlex Zinenko static omp::ReductionDeclareOp declareReduction(PatternRewriter &builder,
2371ce752b7SAlex Zinenko                                                 scf::ReduceOp reduce) {
2381ce752b7SAlex Zinenko   Operation *container = SymbolTable::getNearestSymbolTable(reduce);
2391ce752b7SAlex Zinenko   SymbolTable symbolTable(container);
2401ce752b7SAlex Zinenko 
2411ce752b7SAlex Zinenko   // Insert reduction declarations in the symbol-table ancestor before the
2421ce752b7SAlex Zinenko   // ancestor of the current insertion point.
2431ce752b7SAlex Zinenko   Operation *insertionPoint = reduce;
2441ce752b7SAlex Zinenko   while (insertionPoint->getParentOp() != container)
2451ce752b7SAlex Zinenko     insertionPoint = insertionPoint->getParentOp();
2461ce752b7SAlex Zinenko   OpBuilder::InsertionGuard guard(builder);
2471ce752b7SAlex Zinenko   builder.setInsertionPoint(insertionPoint);
2481ce752b7SAlex Zinenko 
2491ce752b7SAlex Zinenko   assert(llvm::hasSingleElement(reduce.getRegion()) &&
2501ce752b7SAlex Zinenko          "expected reduction region to have a single element");
2511ce752b7SAlex Zinenko 
2521ce752b7SAlex Zinenko   // Match simple binary reductions that can be expressed with atomicrmw.
253c0342a2dSJacques Pienaar   Type type = reduce.getOperand().getType();
2541ce752b7SAlex Zinenko   Block &reduction = reduce.getRegion().front();
255a54f4eaeSMogball   if (matchSimpleReduction<arith::AddFOp, LLVM::FAddOp>(reduction)) {
2561ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(builder, symbolTable, reduce,
2571ce752b7SAlex Zinenko                                               builder.getFloatAttr(type, 0.0));
2581ce752b7SAlex Zinenko     return addAtomicRMW(builder, LLVM::AtomicBinOp::fadd, decl, reduce);
2591ce752b7SAlex Zinenko   }
260a54f4eaeSMogball   if (matchSimpleReduction<arith::AddIOp, LLVM::AddOp>(reduction)) {
2611ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(builder, symbolTable, reduce,
2621ce752b7SAlex Zinenko                                               builder.getIntegerAttr(type, 0));
2631ce752b7SAlex Zinenko     return addAtomicRMW(builder, LLVM::AtomicBinOp::add, decl, reduce);
2641ce752b7SAlex Zinenko   }
265a54f4eaeSMogball   if (matchSimpleReduction<arith::OrIOp, LLVM::OrOp>(reduction)) {
2661ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(builder, symbolTable, reduce,
2671ce752b7SAlex Zinenko                                               builder.getIntegerAttr(type, 0));
2681ce752b7SAlex Zinenko     return addAtomicRMW(builder, LLVM::AtomicBinOp::_or, decl, reduce);
2691ce752b7SAlex Zinenko   }
270a54f4eaeSMogball   if (matchSimpleReduction<arith::XOrIOp, LLVM::XOrOp>(reduction)) {
2711ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(builder, symbolTable, reduce,
2721ce752b7SAlex Zinenko                                               builder.getIntegerAttr(type, 0));
2731ce752b7SAlex Zinenko     return addAtomicRMW(builder, LLVM::AtomicBinOp::_xor, decl, reduce);
2741ce752b7SAlex Zinenko   }
275a54f4eaeSMogball   if (matchSimpleReduction<arith::AndIOp, LLVM::AndOp>(reduction)) {
2761ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(
2771ce752b7SAlex Zinenko         builder, symbolTable, reduce,
2781ce752b7SAlex Zinenko         builder.getIntegerAttr(
2791ce752b7SAlex Zinenko             type, llvm::APInt::getAllOnesValue(type.getIntOrFloatBitWidth())));
2801ce752b7SAlex Zinenko     return addAtomicRMW(builder, LLVM::AtomicBinOp::_and, decl, reduce);
2811ce752b7SAlex Zinenko   }
2821ce752b7SAlex Zinenko 
2831ce752b7SAlex Zinenko   // Match simple binary reductions that cannot be expressed with atomicrmw.
2841ce752b7SAlex Zinenko   // TODO: add atomic region using cmpxchg (which needs atomic load to be
2851ce752b7SAlex Zinenko   // available as an op).
286a54f4eaeSMogball   if (matchSimpleReduction<arith::MulFOp, LLVM::FMulOp>(reduction)) {
2871ce752b7SAlex Zinenko     return createDecl(builder, symbolTable, reduce,
2881ce752b7SAlex Zinenko                       builder.getFloatAttr(type, 1.0));
2891ce752b7SAlex Zinenko   }
2901ce752b7SAlex Zinenko 
2911ce752b7SAlex Zinenko   // Match select-based min/max reductions.
2921ce752b7SAlex Zinenko   bool isMin;
293dec8af70SRiver Riddle   if (matchSelectReduction<arith::CmpFOp, arith::SelectOp>(
294a54f4eaeSMogball           reduction, {arith::CmpFPredicate::OLT, arith::CmpFPredicate::OLE},
295a54f4eaeSMogball           {arith::CmpFPredicate::OGT, arith::CmpFPredicate::OGE}, isMin) ||
2961ce752b7SAlex Zinenko       matchSelectReduction<LLVM::FCmpOp, LLVM::SelectOp>(
2971ce752b7SAlex Zinenko           reduction, {LLVM::FCmpPredicate::olt, LLVM::FCmpPredicate::ole},
2981ce752b7SAlex Zinenko           {LLVM::FCmpPredicate::ogt, LLVM::FCmpPredicate::oge}, isMin)) {
2991ce752b7SAlex Zinenko     return createDecl(builder, symbolTable, reduce,
3001ce752b7SAlex Zinenko                       minMaxValueForFloat(type, !isMin));
3011ce752b7SAlex Zinenko   }
302dec8af70SRiver Riddle   if (matchSelectReduction<arith::CmpIOp, arith::SelectOp>(
303a54f4eaeSMogball           reduction, {arith::CmpIPredicate::slt, arith::CmpIPredicate::sle},
304a54f4eaeSMogball           {arith::CmpIPredicate::sgt, arith::CmpIPredicate::sge}, isMin) ||
3051ce752b7SAlex Zinenko       matchSelectReduction<LLVM::ICmpOp, LLVM::SelectOp>(
3061ce752b7SAlex Zinenko           reduction, {LLVM::ICmpPredicate::slt, LLVM::ICmpPredicate::sle},
3071ce752b7SAlex Zinenko           {LLVM::ICmpPredicate::sgt, LLVM::ICmpPredicate::sge}, isMin)) {
3081ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(
3091ce752b7SAlex Zinenko         builder, symbolTable, reduce, minMaxValueForSignedInt(type, !isMin));
3101ce752b7SAlex Zinenko     return addAtomicRMW(builder,
3111ce752b7SAlex Zinenko                         isMin ? LLVM::AtomicBinOp::min : LLVM::AtomicBinOp::max,
3121ce752b7SAlex Zinenko                         decl, reduce);
3131ce752b7SAlex Zinenko   }
314dec8af70SRiver Riddle   if (matchSelectReduction<arith::CmpIOp, arith::SelectOp>(
315a54f4eaeSMogball           reduction, {arith::CmpIPredicate::ult, arith::CmpIPredicate::ule},
316a54f4eaeSMogball           {arith::CmpIPredicate::ugt, arith::CmpIPredicate::uge}, isMin) ||
3171ce752b7SAlex Zinenko       matchSelectReduction<LLVM::ICmpOp, LLVM::SelectOp>(
3181ce752b7SAlex Zinenko           reduction, {LLVM::ICmpPredicate::ugt, LLVM::ICmpPredicate::ule},
3191ce752b7SAlex Zinenko           {LLVM::ICmpPredicate::ugt, LLVM::ICmpPredicate::uge}, isMin)) {
3201ce752b7SAlex Zinenko     omp::ReductionDeclareOp decl = createDecl(
3211ce752b7SAlex Zinenko         builder, symbolTable, reduce, minMaxValueForUnsignedInt(type, !isMin));
3221ce752b7SAlex Zinenko     return addAtomicRMW(
3231ce752b7SAlex Zinenko         builder, isMin ? LLVM::AtomicBinOp::umin : LLVM::AtomicBinOp::umax,
3241ce752b7SAlex Zinenko         decl, reduce);
3251ce752b7SAlex Zinenko   }
3261ce752b7SAlex Zinenko 
3271ce752b7SAlex Zinenko   return nullptr;
3281ce752b7SAlex Zinenko }
3291ce752b7SAlex Zinenko 
330119545f4SAlex Zinenko namespace {
331119545f4SAlex Zinenko 
332119545f4SAlex Zinenko struct ParallelOpLowering : public OpRewritePattern<scf::ParallelOp> {
333119545f4SAlex Zinenko   using OpRewritePattern<scf::ParallelOp>::OpRewritePattern;
334119545f4SAlex Zinenko 
matchAndRewrite__anona2dd55d90111::ParallelOpLowering335119545f4SAlex Zinenko   LogicalResult matchAndRewrite(scf::ParallelOp parallelOp,
336119545f4SAlex Zinenko                                 PatternRewriter &rewriter) const override {
3371ce752b7SAlex Zinenko     // Declare reductions.
3381ce752b7SAlex Zinenko     // TODO: consider checking it here is already a compatible reduction
3391ce752b7SAlex Zinenko     // declaration and use it instead of redeclaring.
3401ce752b7SAlex Zinenko     SmallVector<Attribute> reductionDeclSymbols;
3411ce752b7SAlex Zinenko     for (auto reduce : parallelOp.getOps<scf::ReduceOp>()) {
3421ce752b7SAlex Zinenko       omp::ReductionDeclareOp decl = declareReduction(rewriter, reduce);
3431ce752b7SAlex Zinenko       if (!decl)
3441ce752b7SAlex Zinenko         return failure();
3451ce752b7SAlex Zinenko       reductionDeclSymbols.push_back(
3461ce752b7SAlex Zinenko           SymbolRefAttr::get(rewriter.getContext(), decl.sym_name()));
3471ce752b7SAlex Zinenko     }
3481ce752b7SAlex Zinenko 
3491ce752b7SAlex Zinenko     // Allocate reduction variables. Make sure the we don't overflow the stack
3501ce752b7SAlex Zinenko     // with local `alloca`s by saving and restoring the stack pointer.
3511ce752b7SAlex Zinenko     Location loc = parallelOp.getLoc();
3521ce752b7SAlex Zinenko     Value one = rewriter.create<LLVM::ConstantOp>(
3531ce752b7SAlex Zinenko         loc, rewriter.getIntegerType(64), rewriter.getI64IntegerAttr(1));
3541ce752b7SAlex Zinenko     SmallVector<Value> reductionVariables;
3551ce752b7SAlex Zinenko     reductionVariables.reserve(parallelOp.getNumReductions());
356c0342a2dSJacques Pienaar     for (Value init : parallelOp.getInitVals()) {
3571ce752b7SAlex Zinenko       assert((LLVM::isCompatibleType(init.getType()) ||
3581ce752b7SAlex Zinenko               init.getType().isa<LLVM::PointerElementTypeInterface>()) &&
3591ce752b7SAlex Zinenko              "cannot create a reduction variable if the type is not an LLVM "
3601ce752b7SAlex Zinenko              "pointer element");
3611ce752b7SAlex Zinenko       Value storage = rewriter.create<LLVM::AllocaOp>(
3621ce752b7SAlex Zinenko           loc, LLVM::LLVMPointerType::get(init.getType()), one, 0);
3631ce752b7SAlex Zinenko       rewriter.create<LLVM::StoreOp>(loc, init, storage);
3641ce752b7SAlex Zinenko       reductionVariables.push_back(storage);
3651ce752b7SAlex Zinenko     }
3661ce752b7SAlex Zinenko 
3671ce752b7SAlex Zinenko     // Replace the reduction operations contained in this loop. Must be done
3681ce752b7SAlex Zinenko     // here rather than in a separate pattern to have access to the list of
3691ce752b7SAlex Zinenko     // reduction variables.
3701ce752b7SAlex Zinenko     for (auto pair :
3711ce752b7SAlex Zinenko          llvm::zip(parallelOp.getOps<scf::ReduceOp>(), reductionVariables)) {
3721ce752b7SAlex Zinenko       OpBuilder::InsertionGuard guard(rewriter);
3731ce752b7SAlex Zinenko       scf::ReduceOp reduceOp = std::get<0>(pair);
3741ce752b7SAlex Zinenko       rewriter.setInsertionPoint(reduceOp);
3751ce752b7SAlex Zinenko       rewriter.replaceOpWithNewOp<omp::ReductionOp>(
376c0342a2dSJacques Pienaar           reduceOp, reduceOp.getOperand(), std::get<1>(pair));
3771ce752b7SAlex Zinenko     }
3781ce752b7SAlex Zinenko 
3791ce752b7SAlex Zinenko     // Create the parallel wrapper.
3801ce752b7SAlex Zinenko     auto ompParallel = rewriter.create<omp::ParallelOp>(loc);
3811ce752b7SAlex Zinenko     {
38278fb4f9dSWilliam S. Moses 
3831ce752b7SAlex Zinenko       OpBuilder::InsertionGuard guard(rewriter);
3841ce752b7SAlex Zinenko       rewriter.createBlock(&ompParallel.region());
3851ce752b7SAlex Zinenko 
386119545f4SAlex Zinenko       // Replace the loop.
387bf6477ebSWilliam S. Moses       {
388bf6477ebSWilliam S. Moses         OpBuilder::InsertionGuard allocaGuard(rewriter);
389119545f4SAlex Zinenko         auto loop = rewriter.create<omp::WsLoopOp>(
390c0342a2dSJacques Pienaar             parallelOp.getLoc(), parallelOp.getLowerBound(),
391c0342a2dSJacques Pienaar             parallelOp.getUpperBound(), parallelOp.getStep());
392bf6477ebSWilliam S. Moses         rewriter.create<omp::TerminatorOp>(loc);
3931ce752b7SAlex Zinenko 
394c0342a2dSJacques Pienaar         rewriter.inlineRegionBefore(parallelOp.getRegion(), loop.region(),
395119545f4SAlex Zinenko                                     loop.region().begin());
396bf6477ebSWilliam S. Moses 
397bf6477ebSWilliam S. Moses         Block *ops = rewriter.splitBlock(&*loop.region().begin(),
398bf6477ebSWilliam S. Moses                                          loop.region().begin()->begin());
399bf6477ebSWilliam S. Moses 
400bf6477ebSWilliam S. Moses         rewriter.setInsertionPointToStart(&*loop.region().begin());
401bf6477ebSWilliam S. Moses 
402bf6477ebSWilliam S. Moses         auto scope = rewriter.create<memref::AllocaScopeOp>(parallelOp.getLoc(),
403bf6477ebSWilliam S. Moses                                                             TypeRange());
404bf6477ebSWilliam S. Moses         rewriter.create<omp::YieldOp>(loc, ValueRange());
405bf6477ebSWilliam S. Moses         Block *scopeBlock = rewriter.createBlock(&scope.getBodyRegion());
406bf6477ebSWilliam S. Moses         rewriter.mergeBlocks(ops, scopeBlock);
407bf6477ebSWilliam S. Moses         auto oldYield = cast<scf::YieldOp>(scopeBlock->getTerminator());
408bf6477ebSWilliam S. Moses         rewriter.setInsertionPointToEnd(&*scope.getBodyRegion().begin());
409bf6477ebSWilliam S. Moses         rewriter.replaceOpWithNewOp<memref::AllocaScopeReturnOp>(
410bf6477ebSWilliam S. Moses             oldYield, oldYield->getOperands());
4111ce752b7SAlex Zinenko         if (!reductionVariables.empty()) {
4121ce752b7SAlex Zinenko           loop.reductionsAttr(
4131ce752b7SAlex Zinenko               ArrayAttr::get(rewriter.getContext(), reductionDeclSymbols));
4141ce752b7SAlex Zinenko           loop.reduction_varsMutable().append(reductionVariables);
4151ce752b7SAlex Zinenko         }
4161ce752b7SAlex Zinenko       }
41778fb4f9dSWilliam S. Moses     }
418973cb2c3SWilliam S. Moses 
4191ce752b7SAlex Zinenko     // Load loop results.
4201ce752b7SAlex Zinenko     SmallVector<Value> results;
4211ce752b7SAlex Zinenko     results.reserve(reductionVariables.size());
4221ce752b7SAlex Zinenko     for (Value variable : reductionVariables) {
4231ce752b7SAlex Zinenko       Value res = rewriter.create<LLVM::LoadOp>(loc, variable);
4241ce752b7SAlex Zinenko       results.push_back(res);
4251ce752b7SAlex Zinenko     }
4261ce752b7SAlex Zinenko     rewriter.replaceOp(parallelOp, results);
4271ce752b7SAlex Zinenko 
428119545f4SAlex Zinenko     return success();
429119545f4SAlex Zinenko   }
430119545f4SAlex Zinenko };
431119545f4SAlex Zinenko 
432119545f4SAlex Zinenko /// Applies the conversion patterns in the given function.
applyPatterns(ModuleOp module)4331ce752b7SAlex Zinenko static LogicalResult applyPatterns(ModuleOp module) {
4341ce752b7SAlex Zinenko   ConversionTarget target(*module.getContext());
4351ce752b7SAlex Zinenko   target.addIllegalOp<scf::ReduceOp, scf::ReduceReturnOp, scf::ParallelOp>();
43678fb4f9dSWilliam S. Moses   target.addLegalDialect<omp::OpenMPDialect, LLVM::LLVMDialect,
43778fb4f9dSWilliam S. Moses                          memref::MemRefDialect>();
438119545f4SAlex Zinenko 
4391ce752b7SAlex Zinenko   RewritePatternSet patterns(module.getContext());
4401ce752b7SAlex Zinenko   patterns.add<ParallelOpLowering>(module.getContext());
44179d7f618SChris Lattner   FrozenRewritePatternSet frozen(std::move(patterns));
4421ce752b7SAlex Zinenko   return applyPartialConversion(module, target, frozen);
443119545f4SAlex Zinenko }
444119545f4SAlex Zinenko 
445119545f4SAlex Zinenko /// A pass converting SCF operations to OpenMP operations.
446119545f4SAlex Zinenko struct SCFToOpenMPPass : public ConvertSCFToOpenMPBase<SCFToOpenMPPass> {
447119545f4SAlex Zinenko   /// Pass entry point.
runOnOperation__anona2dd55d90111::SCFToOpenMPPass4481ce752b7SAlex Zinenko   void runOnOperation() override {
4491ce752b7SAlex Zinenko     if (failed(applyPatterns(getOperation())))
450119545f4SAlex Zinenko       signalPassFailure();
451119545f4SAlex Zinenko   }
452119545f4SAlex Zinenko };
453119545f4SAlex Zinenko 
454be0a7e9fSMehdi Amini } // namespace
455119545f4SAlex Zinenko 
createConvertSCFToOpenMPPass()4561ce752b7SAlex Zinenko std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertSCFToOpenMPPass() {
457119545f4SAlex Zinenko   return std::make_unique<SCFToOpenMPPass>();
458119545f4SAlex Zinenko }
459