| 1 | //===- ControlFlowToSCF.h - ControlFlow to SCF -------------*- C++ ------*-===// |
| 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 | // Define conversions from the ControlFlow dialect to the SCF dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/ControlFlowToSCF/ControlFlowToSCF.h" |
| 14 | |
| 15 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 16 | #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h" |
| 17 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 18 | #include "mlir/Dialect/UB/IR/UBOps.h" |
| 19 | #include "mlir/Interfaces/FunctionInterfaces.h" |
| 20 | #include "mlir/Pass/Pass.h" |
| 21 | #include "mlir/Transforms/CFGToSCF.h" |
| 22 | |
| 23 | namespace mlir { |
| 24 | #define GEN_PASS_DEF_LIFTCONTROLFLOWTOSCFPASS |
| 25 | #include "mlir/Conversion/Passes.h.inc" |
| 26 | } // namespace mlir |
| 27 | |
| 28 | using namespace mlir; |
| 29 | |
| 30 | bool ControlFlowToSCFTransformation::canConvertMultiSuccessorBranchOp( |
| 31 | Operation *op) { |
| 32 | return isa<cf::CondBranchOp, cf::SwitchOp>(Val: op); |
| 33 | } |
| 34 | |
| 35 | FailureOr<Operation *> |
| 36 | ControlFlowToSCFTransformation::createStructuredBranchRegionOp( |
| 37 | OpBuilder &builder, Operation *controlFlowCondOp, TypeRange resultTypes, |
| 38 | MutableArrayRef<Region> regions) { |
| 39 | if (auto condBrOp = dyn_cast<cf::CondBranchOp>(Val: controlFlowCondOp)) { |
| 40 | assert(regions.size() == 2); |
| 41 | auto ifOp = scf::IfOp::create(builder, location: controlFlowCondOp->getLoc(), |
| 42 | resultTypes, cond: condBrOp.getCondition()); |
| 43 | ifOp.getThenRegion().takeBody(other&: regions[0]); |
| 44 | ifOp.getElseRegion().takeBody(other&: regions[1]); |
| 45 | return ifOp.getOperation(); |
| 46 | } |
| 47 | |
| 48 | if (auto switchOp = dyn_cast<cf::SwitchOp>(Val: controlFlowCondOp)) { |
| 49 | // `getCFGSwitchValue` returns an i32 that we need to convert to index |
| 50 | // fist. |
| 51 | auto cast = arith::IndexCastUIOp::create( |
| 52 | builder, location: controlFlowCondOp->getLoc(), out: builder.getIndexType(), |
| 53 | in: switchOp.getFlag()); |
| 54 | SmallVector<int64_t> cases; |
| 55 | if (auto caseValues = switchOp.getCaseValues()) |
| 56 | llvm::append_range( |
| 57 | C&: cases, R: llvm::map_range(C&: *caseValues, F: [](const llvm::APInt &apInt) { |
| 58 | return apInt.getZExtValue(); |
| 59 | })); |
| 60 | |
| 61 | assert(regions.size() == cases.size() + 1); |
| 62 | |
| 63 | auto indexSwitchOp = |
| 64 | scf::IndexSwitchOp::create(builder, location: controlFlowCondOp->getLoc(), |
| 65 | results: resultTypes, arg: cast, cases, caseRegionsCount: cases.size()); |
| 66 | |
| 67 | indexSwitchOp.getDefaultRegion().takeBody(other&: regions[0]); |
| 68 | for (auto &&[targetRegion, sourceRegion] : |
| 69 | llvm::zip(t: indexSwitchOp.getCaseRegions(), u: llvm::drop_begin(RangeOrContainer&: regions))) |
| 70 | targetRegion.takeBody(other&: sourceRegion); |
| 71 | |
| 72 | return indexSwitchOp.getOperation(); |
| 73 | } |
| 74 | |
| 75 | controlFlowCondOp->emitOpError( |
| 76 | message: "Cannot convert unknown control flow op to structured control flow" ); |
| 77 | return failure(); |
| 78 | } |
| 79 | |
| 80 | LogicalResult |
| 81 | ControlFlowToSCFTransformation::createStructuredBranchRegionTerminatorOp( |
| 82 | Location loc, OpBuilder &builder, Operation *branchRegionOp, |
| 83 | Operation *replacedControlFlowOp, ValueRange results) { |
| 84 | scf::YieldOp::create(builder, location: loc, results); |
| 85 | return success(); |
| 86 | } |
| 87 | |
| 88 | FailureOr<Operation *> |
| 89 | ControlFlowToSCFTransformation::createStructuredDoWhileLoopOp( |
| 90 | OpBuilder &builder, Operation *replacedOp, ValueRange loopVariablesInit, |
| 91 | Value condition, ValueRange loopVariablesNextIter, Region &&loopBody) { |
| 92 | Location loc = replacedOp->getLoc(); |
| 93 | auto whileOp = scf::WhileOp::create( |
| 94 | builder, location: loc, resultTypes: loopVariablesInit.getTypes(), operands: loopVariablesInit); |
| 95 | |
| 96 | whileOp.getBefore().takeBody(other&: loopBody); |
| 97 | |
| 98 | builder.setInsertionPointToEnd(&whileOp.getBefore().back()); |
| 99 | // `getCFGSwitchValue` returns a i32. We therefore need to truncate the |
| 100 | // condition to i1 first. It is guaranteed to be either 0 or 1 already. |
| 101 | scf::ConditionOp::create( |
| 102 | builder, location: loc, |
| 103 | condition: arith::TruncIOp::create(builder, location: loc, out: builder.getI1Type(), in: condition), |
| 104 | args: loopVariablesNextIter); |
| 105 | |
| 106 | Block *afterBlock = builder.createBlock(parent: &whileOp.getAfter()); |
| 107 | afterBlock->addArguments( |
| 108 | types: loopVariablesInit.getTypes(), |
| 109 | locs: SmallVector<Location>(loopVariablesInit.size(), loc)); |
| 110 | scf::YieldOp::create(builder, location: loc, results: afterBlock->getArguments()); |
| 111 | |
| 112 | return whileOp.getOperation(); |
| 113 | } |
| 114 | |
| 115 | Value ControlFlowToSCFTransformation::getCFGSwitchValue(Location loc, |
| 116 | OpBuilder &builder, |
| 117 | unsigned int value) { |
| 118 | return arith::ConstantOp::create(builder, location: loc, |
| 119 | value: builder.getI32IntegerAttr(value)); |
| 120 | } |
| 121 | |
| 122 | void ControlFlowToSCFTransformation::createCFGSwitchOp( |
| 123 | Location loc, OpBuilder &builder, Value flag, |
| 124 | ArrayRef<unsigned int> caseValues, BlockRange caseDestinations, |
| 125 | ArrayRef<ValueRange> caseArguments, Block *defaultDest, |
| 126 | ValueRange defaultArgs) { |
| 127 | cf::SwitchOp::create(builder, location: loc, flag, defaultDestination: defaultDest, defaultOperands: defaultArgs, |
| 128 | caseValues: llvm::to_vector_of<int32_t>(Range&: caseValues), |
| 129 | caseDestinations, caseOperands: caseArguments); |
| 130 | } |
| 131 | |
| 132 | Value ControlFlowToSCFTransformation::getUndefValue(Location loc, |
| 133 | OpBuilder &builder, |
| 134 | Type type) { |
| 135 | return ub::PoisonOp::create(builder, location: loc, result: type, value: nullptr); |
| 136 | } |
| 137 | |
| 138 | FailureOr<Operation *> |
| 139 | ControlFlowToSCFTransformation::createUnreachableTerminator(Location loc, |
| 140 | OpBuilder &builder, |
| 141 | Region ®ion) { |
| 142 | return ub::UnreachableOp::create(builder, location: loc).getOperation(); |
| 143 | } |
| 144 | |
| 145 | bool ControlFlowToSCFTransformation::isUnreachableTerminator(Operation *op) { |
| 146 | return isa<ub::UnreachableOp>(Val: op); |
| 147 | } |
| 148 | |
| 149 | namespace { |
| 150 | |
| 151 | struct LiftControlFlowToSCF |
| 152 | : public impl::LiftControlFlowToSCFPassBase<LiftControlFlowToSCF> { |
| 153 | |
| 154 | using Base::Base; |
| 155 | |
| 156 | void runOnOperation() override { |
| 157 | ControlFlowToSCFTransformation transformation; |
| 158 | |
| 159 | bool changed = false; |
| 160 | Operation *op = getOperation(); |
| 161 | WalkResult result = op->walk(callback: [&](FunctionOpInterface funcOp) { |
| 162 | if (funcOp.getFunctionBody().empty()) |
| 163 | return WalkResult::advance(); |
| 164 | |
| 165 | auto &domInfo = funcOp != op ? getChildAnalysis<DominanceInfo>(child: funcOp) |
| 166 | : getAnalysis<DominanceInfo>(); |
| 167 | |
| 168 | auto visitor = [&](Operation *innerOp) -> WalkResult { |
| 169 | for (Region ® : innerOp->getRegions()) { |
| 170 | FailureOr<bool> changedFunc = |
| 171 | transformCFGToSCF(region&: reg, interface&: transformation, dominanceInfo&: domInfo); |
| 172 | if (failed(Result: changedFunc)) |
| 173 | return WalkResult::interrupt(); |
| 174 | |
| 175 | changed |= *changedFunc; |
| 176 | } |
| 177 | return WalkResult::advance(); |
| 178 | }; |
| 179 | |
| 180 | if (funcOp->walk<WalkOrder::PostOrder>(callback&: visitor).wasInterrupted()) |
| 181 | return WalkResult::interrupt(); |
| 182 | |
| 183 | return WalkResult::advance(); |
| 184 | }); |
| 185 | if (result.wasInterrupted()) |
| 186 | return signalPassFailure(); |
| 187 | |
| 188 | if (!changed) |
| 189 | markAllAnalysesPreserved(); |
| 190 | } |
| 191 | }; |
| 192 | } // namespace |
| 193 | |