| 1 | //===- SCFToEmitC.cpp - SCF to EmitC conversion ---------------------------===// |
| 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 a pass to convert scf.if ops into emitc ops. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/SCFToEmitC/SCFToEmitC.h" |
| 14 | |
| 15 | #include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h" |
| 16 | #include "mlir/Conversion/EmitCCommon/TypeConverter.h" |
| 17 | #include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 18 | #include "mlir/Dialect/EmitC/Transforms/TypeConversions.h" |
| 19 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 20 | #include "mlir/IR/Builders.h" |
| 21 | #include "mlir/IR/MLIRContext.h" |
| 22 | #include "mlir/IR/PatternMatch.h" |
| 23 | #include "mlir/Transforms/DialectConversion.h" |
| 24 | #include "mlir/Transforms/Passes.h" |
| 25 | #include "llvm/Support/LogicalResult.h" |
| 26 | |
| 27 | namespace mlir { |
| 28 | #define GEN_PASS_DEF_SCFTOEMITC |
| 29 | #include "mlir/Conversion/Passes.h.inc" |
| 30 | } // namespace mlir |
| 31 | |
| 32 | using namespace mlir; |
| 33 | using namespace mlir::scf; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | /// Implement the interface to convert SCF to EmitC. |
| 38 | struct SCFToEmitCDialectInterface : public ConvertToEmitCPatternInterface { |
| 39 | SCFToEmitCDialectInterface(Dialect *dialect) |
| 40 | : ConvertToEmitCPatternInterface(dialect) {} |
| 41 | |
| 42 | /// Hook for derived dialect interface to provide conversion patterns |
| 43 | /// and mark dialect legal for the conversion target. |
| 44 | void populateConvertToEmitCConversionPatterns( |
| 45 | ConversionTarget &target, TypeConverter &typeConverter, |
| 46 | RewritePatternSet &patterns, std::optional<bool> lowerToCpp) const final { |
| 47 | populateEmitCSizeTTypeConversions(converter&: typeConverter); |
| 48 | populateSCFToEmitCConversionPatterns(patterns, typeConverter); |
| 49 | } |
| 50 | }; |
| 51 | } // namespace |
| 52 | |
| 53 | void mlir::registerConvertSCFToEmitCInterface(DialectRegistry ®istry) { |
| 54 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, scf::SCFDialect *dialect) { |
| 55 | dialect->addInterfaces<SCFToEmitCDialectInterface>(); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | struct SCFToEmitCPass : public impl::SCFToEmitCBase<SCFToEmitCPass> { |
| 62 | void runOnOperation() override; |
| 63 | }; |
| 64 | |
| 65 | // Lower scf::for to emitc::for, implementing result values using |
| 66 | // emitc::variable's updated within the loop body. |
| 67 | struct ForLowering : public OpConversionPattern<ForOp> { |
| 68 | using OpConversionPattern<ForOp>::OpConversionPattern; |
| 69 | |
| 70 | LogicalResult |
| 71 | matchAndRewrite(ForOp forOp, OpAdaptor adaptor, |
| 72 | ConversionPatternRewriter &rewriter) const override; |
| 73 | }; |
| 74 | |
| 75 | // Create an uninitialized emitc::variable op for each result of the given op. |
| 76 | template <typename T> |
| 77 | static LogicalResult |
| 78 | createVariablesForResults(T op, const TypeConverter *typeConverter, |
| 79 | ConversionPatternRewriter &rewriter, |
| 80 | SmallVector<Value> &resultVariables) { |
| 81 | if (!op.getNumResults()) |
| 82 | return success(); |
| 83 | |
| 84 | Location loc = op->getLoc(); |
| 85 | MLIRContext *context = op.getContext(); |
| 86 | |
| 87 | OpBuilder::InsertionGuard guard(rewriter); |
| 88 | rewriter.setInsertionPoint(op); |
| 89 | |
| 90 | for (OpResult result : op.getResults()) { |
| 91 | Type resultType = typeConverter->convertType(t: result.getType()); |
| 92 | if (!resultType) |
| 93 | return rewriter.notifyMatchFailure(op, "result type conversion failed" ); |
| 94 | if (isa<emitc::ArrayType>(Val: resultType)) |
| 95 | return rewriter.notifyMatchFailure( |
| 96 | op, "cannot create variable for result of array type" ); |
| 97 | Type varType = emitc::LValueType::get(valueType: resultType); |
| 98 | emitc::OpaqueAttr noInit = emitc::OpaqueAttr::get(context, value: "" ); |
| 99 | emitc::VariableOp var = |
| 100 | emitc::VariableOp::create(builder&: rewriter, location: loc, resultType0: varType, value: noInit); |
| 101 | resultVariables.push_back(Elt: var); |
| 102 | } |
| 103 | |
| 104 | return success(); |
| 105 | } |
| 106 | |
| 107 | // Create a series of assign ops assigning given values to given variables at |
| 108 | // the current insertion point of given rewriter. |
| 109 | static void assignValues(ValueRange values, ValueRange variables, |
| 110 | ConversionPatternRewriter &rewriter, Location loc) { |
| 111 | for (auto [value, var] : llvm::zip(t&: values, u&: variables)) |
| 112 | emitc::AssignOp::create(builder&: rewriter, location: loc, var, value); |
| 113 | } |
| 114 | |
| 115 | SmallVector<Value> loadValues(ArrayRef<Value> variables, |
| 116 | PatternRewriter &rewriter, Location loc) { |
| 117 | return llvm::map_to_vector<>(C&: variables, F: [&](Value var) { |
| 118 | Type type = cast<emitc::LValueType>(Val: var.getType()).getValueType(); |
| 119 | return emitc::LoadOp::create(builder&: rewriter, location: loc, result: type, operand: var).getResult(); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | static LogicalResult lowerYield(Operation *op, ValueRange resultVariables, |
| 124 | ConversionPatternRewriter &rewriter, |
| 125 | scf::YieldOp yield, bool createYield = true) { |
| 126 | Location loc = yield.getLoc(); |
| 127 | |
| 128 | OpBuilder::InsertionGuard guard(rewriter); |
| 129 | rewriter.setInsertionPoint(yield); |
| 130 | |
| 131 | SmallVector<Value> yieldOperands; |
| 132 | if (failed(Result: rewriter.getRemappedValues(keys: yield.getOperands(), results&: yieldOperands))) |
| 133 | return rewriter.notifyMatchFailure(arg&: op, msg: "failed to lower yield operands" ); |
| 134 | |
| 135 | assignValues(values: yieldOperands, variables: resultVariables, rewriter, loc); |
| 136 | |
| 137 | emitc::YieldOp::create(builder&: rewriter, location: loc); |
| 138 | rewriter.eraseOp(op: yield); |
| 139 | |
| 140 | return success(); |
| 141 | } |
| 142 | |
| 143 | // Lower the contents of an scf::if/scf::index_switch regions to an |
| 144 | // emitc::if/emitc::switch region. The contents of the lowering region is |
| 145 | // moved into the respective lowered region, but the scf::yield is replaced not |
| 146 | // only with an emitc::yield, but also with a sequence of emitc::assign ops that |
| 147 | // set the yielded values into the result variables. |
| 148 | static LogicalResult lowerRegion(Operation *op, ValueRange resultVariables, |
| 149 | ConversionPatternRewriter &rewriter, |
| 150 | Region ®ion, Region &loweredRegion) { |
| 151 | rewriter.inlineRegionBefore(region, parent&: loweredRegion, before: loweredRegion.end()); |
| 152 | Operation *terminator = loweredRegion.back().getTerminator(); |
| 153 | return lowerYield(op, resultVariables, rewriter, |
| 154 | yield: cast<scf::YieldOp>(Val: terminator)); |
| 155 | } |
| 156 | |
| 157 | LogicalResult |
| 158 | ForLowering::matchAndRewrite(ForOp forOp, OpAdaptor adaptor, |
| 159 | ConversionPatternRewriter &rewriter) const { |
| 160 | Location loc = forOp.getLoc(); |
| 161 | |
| 162 | if (forOp.getUnsignedCmp()) |
| 163 | return rewriter.notifyMatchFailure(arg&: forOp, |
| 164 | msg: "unsigned loops are not supported" ); |
| 165 | |
| 166 | // Create an emitc::variable op for each result. These variables will be |
| 167 | // assigned to by emitc::assign ops within the loop body. |
| 168 | SmallVector<Value> resultVariables; |
| 169 | if (failed(Result: createVariablesForResults(op: forOp, typeConverter: getTypeConverter(), rewriter, |
| 170 | resultVariables))) |
| 171 | return rewriter.notifyMatchFailure(arg&: forOp, |
| 172 | msg: "create variables for results failed" ); |
| 173 | |
| 174 | assignValues(values: adaptor.getInitArgs(), variables: resultVariables, rewriter, loc); |
| 175 | |
| 176 | emitc::ForOp loweredFor = |
| 177 | emitc::ForOp::create(builder&: rewriter, location: loc, lowerBound: adaptor.getLowerBound(), |
| 178 | upperBound: adaptor.getUpperBound(), step: adaptor.getStep()); |
| 179 | |
| 180 | Block *loweredBody = loweredFor.getBody(); |
| 181 | |
| 182 | // Erase the auto-generated terminator for the lowered for op. |
| 183 | rewriter.eraseOp(op: loweredBody->getTerminator()); |
| 184 | |
| 185 | IRRewriter::InsertPoint ip = rewriter.saveInsertionPoint(); |
| 186 | rewriter.setInsertionPointToEnd(loweredBody); |
| 187 | |
| 188 | SmallVector<Value> iterArgsValues = |
| 189 | loadValues(variables: resultVariables, rewriter, loc); |
| 190 | |
| 191 | rewriter.restoreInsertionPoint(ip); |
| 192 | |
| 193 | // Convert the original region types into the new types by adding unrealized |
| 194 | // casts in the beginning of the loop. This performs the conversion in place. |
| 195 | if (failed(Result: rewriter.convertRegionTypes(region: &forOp.getRegion(), |
| 196 | converter: *getTypeConverter(), entryConversion: nullptr))) { |
| 197 | return rewriter.notifyMatchFailure(arg&: forOp, msg: "region types conversion failed" ); |
| 198 | } |
| 199 | |
| 200 | // Register the replacements for the block arguments and inline the body of |
| 201 | // the scf.for loop into the body of the emitc::for loop. |
| 202 | Block *scfBody = &(forOp.getRegion().front()); |
| 203 | SmallVector<Value> replacingValues; |
| 204 | replacingValues.push_back(Elt: loweredFor.getInductionVar()); |
| 205 | replacingValues.append(in_start: iterArgsValues.begin(), in_end: iterArgsValues.end()); |
| 206 | rewriter.mergeBlocks(source: scfBody, dest: loweredBody, argValues: replacingValues); |
| 207 | |
| 208 | auto result = lowerYield(op: forOp, resultVariables, rewriter, |
| 209 | yield: cast<scf::YieldOp>(Val: loweredBody->getTerminator())); |
| 210 | |
| 211 | if (failed(Result: result)) { |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | // Load variables into SSA values after the for loop. |
| 216 | SmallVector<Value> resultValues = loadValues(variables: resultVariables, rewriter, loc); |
| 217 | |
| 218 | rewriter.replaceOp(op: forOp, newValues: resultValues); |
| 219 | return success(); |
| 220 | } |
| 221 | |
| 222 | // Lower scf::if to emitc::if, implementing result values as emitc::variable's |
| 223 | // updated within the then and else regions. |
| 224 | struct IfLowering : public OpConversionPattern<IfOp> { |
| 225 | using OpConversionPattern<IfOp>::OpConversionPattern; |
| 226 | |
| 227 | LogicalResult |
| 228 | matchAndRewrite(IfOp ifOp, OpAdaptor adaptor, |
| 229 | ConversionPatternRewriter &rewriter) const override; |
| 230 | }; |
| 231 | |
| 232 | } // namespace |
| 233 | |
| 234 | LogicalResult |
| 235 | IfLowering::matchAndRewrite(IfOp ifOp, OpAdaptor adaptor, |
| 236 | ConversionPatternRewriter &rewriter) const { |
| 237 | Location loc = ifOp.getLoc(); |
| 238 | |
| 239 | // Create an emitc::variable op for each result. These variables will be |
| 240 | // assigned to by emitc::assign ops within the then & else regions. |
| 241 | SmallVector<Value> resultVariables; |
| 242 | if (failed(Result: createVariablesForResults(op: ifOp, typeConverter: getTypeConverter(), rewriter, |
| 243 | resultVariables))) |
| 244 | return rewriter.notifyMatchFailure(arg&: ifOp, |
| 245 | msg: "create variables for results failed" ); |
| 246 | |
| 247 | // Utility function to lower the contents of an scf::if region to an emitc::if |
| 248 | // region. The contents of the scf::if regions is moved into the respective |
| 249 | // emitc::if regions, but the scf::yield is replaced not only with an |
| 250 | // emitc::yield, but also with a sequence of emitc::assign ops that set the |
| 251 | // yielded values into the result variables. |
| 252 | auto lowerRegion = [&resultVariables, &rewriter, |
| 253 | &ifOp](Region ®ion, Region &loweredRegion) { |
| 254 | rewriter.inlineRegionBefore(region, parent&: loweredRegion, before: loweredRegion.end()); |
| 255 | Operation *terminator = loweredRegion.back().getTerminator(); |
| 256 | auto result = lowerYield(op: ifOp, resultVariables, rewriter, |
| 257 | yield: cast<scf::YieldOp>(Val: terminator)); |
| 258 | if (failed(Result: result)) { |
| 259 | return result; |
| 260 | } |
| 261 | return success(); |
| 262 | }; |
| 263 | |
| 264 | Region &thenRegion = adaptor.getThenRegion(); |
| 265 | Region &elseRegion = adaptor.getElseRegion(); |
| 266 | |
| 267 | bool hasElseBlock = !elseRegion.empty(); |
| 268 | |
| 269 | auto loweredIf = |
| 270 | emitc::IfOp::create(builder&: rewriter, location: loc, cond: adaptor.getCondition(), addThenBlock: false, addElseBlock: false); |
| 271 | |
| 272 | Region &loweredThenRegion = loweredIf.getThenRegion(); |
| 273 | auto result = lowerRegion(thenRegion, loweredThenRegion); |
| 274 | if (failed(Result: result)) { |
| 275 | return result; |
| 276 | } |
| 277 | |
| 278 | if (hasElseBlock) { |
| 279 | Region &loweredElseRegion = loweredIf.getElseRegion(); |
| 280 | auto result = lowerRegion(elseRegion, loweredElseRegion); |
| 281 | if (failed(Result: result)) { |
| 282 | return result; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | rewriter.setInsertionPointAfter(ifOp); |
| 287 | SmallVector<Value> results = loadValues(variables: resultVariables, rewriter, loc); |
| 288 | |
| 289 | rewriter.replaceOp(op: ifOp, newValues: results); |
| 290 | return success(); |
| 291 | } |
| 292 | |
| 293 | // Lower scf::index_switch to emitc::switch, implementing result values as |
| 294 | // emitc::variable's updated within the case and default regions. |
| 295 | struct IndexSwitchOpLowering : public OpConversionPattern<IndexSwitchOp> { |
| 296 | using Base::Base; |
| 297 | |
| 298 | LogicalResult |
| 299 | matchAndRewrite(IndexSwitchOp indexSwitchOp, OpAdaptor adaptor, |
| 300 | ConversionPatternRewriter &rewriter) const override; |
| 301 | }; |
| 302 | |
| 303 | LogicalResult IndexSwitchOpLowering::matchAndRewrite( |
| 304 | IndexSwitchOp indexSwitchOp, OpAdaptor adaptor, |
| 305 | ConversionPatternRewriter &rewriter) const { |
| 306 | Location loc = indexSwitchOp.getLoc(); |
| 307 | |
| 308 | // Create an emitc::variable op for each result. These variables will be |
| 309 | // assigned to by emitc::assign ops within the case and default regions. |
| 310 | SmallVector<Value> resultVariables; |
| 311 | if (failed(Result: createVariablesForResults(op: indexSwitchOp, typeConverter: getTypeConverter(), |
| 312 | rewriter, resultVariables))) { |
| 313 | return rewriter.notifyMatchFailure(arg&: indexSwitchOp, |
| 314 | msg: "create variables for results failed" ); |
| 315 | } |
| 316 | |
| 317 | auto loweredSwitch = |
| 318 | emitc::SwitchOp::create(builder&: rewriter, location: loc, arg: adaptor.getArg(), |
| 319 | cases: adaptor.getCases(), caseRegionsCount: indexSwitchOp.getNumCases()); |
| 320 | |
| 321 | // Lowering all case regions. |
| 322 | for (auto pair : |
| 323 | llvm::zip(t: adaptor.getCaseRegions(), u: loweredSwitch.getCaseRegions())) { |
| 324 | if (failed(Result: lowerRegion(op: indexSwitchOp, resultVariables, rewriter, |
| 325 | region&: *std::get<0>(t&: pair), loweredRegion&: std::get<1>(t&: pair)))) { |
| 326 | return failure(); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Lowering default region. |
| 331 | if (failed(Result: lowerRegion(op: indexSwitchOp, resultVariables, rewriter, |
| 332 | region&: adaptor.getDefaultRegion(), |
| 333 | loweredRegion&: loweredSwitch.getDefaultRegion()))) { |
| 334 | return failure(); |
| 335 | } |
| 336 | |
| 337 | rewriter.setInsertionPointAfter(indexSwitchOp); |
| 338 | SmallVector<Value> results = loadValues(variables: resultVariables, rewriter, loc); |
| 339 | |
| 340 | rewriter.replaceOp(op: indexSwitchOp, newValues: results); |
| 341 | return success(); |
| 342 | } |
| 343 | |
| 344 | // Lower scf::while to emitc::do using mutable variables to maintain loop state |
| 345 | // across iterations. The do-while structure ensures the condition is evaluated |
| 346 | // after each iteration, matching SCF while semantics. |
| 347 | struct WhileLowering : public OpConversionPattern<WhileOp> { |
| 348 | using Base::Base; |
| 349 | |
| 350 | LogicalResult |
| 351 | matchAndRewrite(WhileOp whileOp, OpAdaptor adaptor, |
| 352 | ConversionPatternRewriter &rewriter) const override { |
| 353 | Location loc = whileOp.getLoc(); |
| 354 | MLIRContext *context = loc.getContext(); |
| 355 | |
| 356 | // Create an emitc::variable op for each result. These variables will be |
| 357 | // assigned to by emitc::assign ops within the loop body. |
| 358 | SmallVector<Value> resultVariables; |
| 359 | if (failed(Result: createVariablesForResults(op: whileOp, typeConverter: getTypeConverter(), rewriter, |
| 360 | resultVariables))) |
| 361 | return rewriter.notifyMatchFailure(arg&: whileOp, |
| 362 | msg: "Failed to create result variables" ); |
| 363 | |
| 364 | // Create variable storage for loop-carried values to enable imperative |
| 365 | // updates while maintaining SSA semantics at conversion boundaries. |
| 366 | SmallVector<Value> loopVariables; |
| 367 | if (failed(Result: createVariablesForLoopCarriedValues( |
| 368 | whileOp, rewriter, loopVars&: loopVariables, loc, context))) |
| 369 | return failure(); |
| 370 | |
| 371 | if (failed(Result: lowerDoWhile(whileOp, loopVars: loopVariables, resultVars: resultVariables, context, |
| 372 | rewriter, loc))) |
| 373 | return failure(); |
| 374 | |
| 375 | rewriter.setInsertionPointAfter(whileOp); |
| 376 | |
| 377 | // Load the final result values from result variables. |
| 378 | SmallVector<Value> finalResults = |
| 379 | loadValues(variables: resultVariables, rewriter, loc); |
| 380 | rewriter.replaceOp(op: whileOp, newValues: finalResults); |
| 381 | |
| 382 | return success(); |
| 383 | } |
| 384 | |
| 385 | private: |
| 386 | // Initialize variables for loop-carried values to enable state updates |
| 387 | // across iterations without SSA argument passing. |
| 388 | LogicalResult createVariablesForLoopCarriedValues( |
| 389 | WhileOp whileOp, ConversionPatternRewriter &rewriter, |
| 390 | SmallVectorImpl<Value> &loopVars, Location loc, |
| 391 | MLIRContext *context) const { |
| 392 | OpBuilder::InsertionGuard guard(rewriter); |
| 393 | rewriter.setInsertionPoint(whileOp); |
| 394 | |
| 395 | emitc::OpaqueAttr noInit = emitc::OpaqueAttr::get(context, value: "" ); |
| 396 | |
| 397 | for (Value init : whileOp.getInits()) { |
| 398 | Type convertedType = getTypeConverter()->convertType(t: init.getType()); |
| 399 | if (!convertedType) |
| 400 | return rewriter.notifyMatchFailure(arg&: whileOp, msg: "type conversion failed" ); |
| 401 | if (isa<emitc::ArrayType>(Val: convertedType)) |
| 402 | return rewriter.notifyMatchFailure( |
| 403 | arg&: whileOp, |
| 404 | msg: "cannot create variable for loop-carried value of array type" ); |
| 405 | |
| 406 | auto var = emitc::VariableOp::create( |
| 407 | builder&: rewriter, location: loc, resultType0: emitc::LValueType::get(valueType: convertedType), value: noInit); |
| 408 | emitc::AssignOp::create(builder&: rewriter, location: loc, var: var.getResult(), value: init); |
| 409 | loopVars.push_back(Elt: var); |
| 410 | } |
| 411 | |
| 412 | return success(); |
| 413 | } |
| 414 | |
| 415 | // Lower scf.while to emitc.do. |
| 416 | LogicalResult lowerDoWhile(WhileOp whileOp, ArrayRef<Value> loopVars, |
| 417 | ArrayRef<Value> resultVars, MLIRContext *context, |
| 418 | ConversionPatternRewriter &rewriter, |
| 419 | Location loc) const { |
| 420 | // Create a global boolean variable to store the loop condition state. |
| 421 | Type i1Type = IntegerType::get(context, width: 1); |
| 422 | auto globalCondition = |
| 423 | emitc::VariableOp::create(builder&: rewriter, location: loc, resultType0: emitc::LValueType::get(valueType: i1Type), |
| 424 | value: emitc::OpaqueAttr::get(context, value: "" )); |
| 425 | Value conditionVal = globalCondition.getResult(); |
| 426 | |
| 427 | auto loweredDo = emitc::DoOp::create(builder&: rewriter, location: loc); |
| 428 | |
| 429 | // Convert region types to match the target dialect type system. |
| 430 | if (failed(Result: rewriter.convertRegionTypes(region: &whileOp.getBefore(), |
| 431 | converter: *getTypeConverter(), entryConversion: nullptr)) || |
| 432 | failed(Result: rewriter.convertRegionTypes(region: &whileOp.getAfter(), |
| 433 | converter: *getTypeConverter(), entryConversion: nullptr))) { |
| 434 | return rewriter.notifyMatchFailure(arg&: whileOp, |
| 435 | msg: "region types conversion failed" ); |
| 436 | } |
| 437 | |
| 438 | // Prepare the before region (condition evaluation) for merging. |
| 439 | Block *beforeBlock = &whileOp.getBefore().front(); |
| 440 | Block *bodyBlock = rewriter.createBlock(parent: &loweredDo.getBodyRegion()); |
| 441 | rewriter.setInsertionPointToStart(bodyBlock); |
| 442 | |
| 443 | // Load current variable values to use as initial arguments for the |
| 444 | // condition block. |
| 445 | SmallVector<Value> replacingValues = loadValues(variables: loopVars, rewriter, loc); |
| 446 | rewriter.mergeBlocks(source: beforeBlock, dest: bodyBlock, argValues: replacingValues); |
| 447 | |
| 448 | Operation *condTerminator = |
| 449 | loweredDo.getBodyRegion().back().getTerminator(); |
| 450 | scf::ConditionOp condOp = cast<scf::ConditionOp>(Val: condTerminator); |
| 451 | rewriter.setInsertionPoint(condOp); |
| 452 | |
| 453 | // Update result variables with values from scf::condition. |
| 454 | SmallVector<Value> conditionArgs; |
| 455 | for (Value arg : condOp.getArgs()) { |
| 456 | conditionArgs.push_back(Elt: rewriter.getRemappedValue(key: arg)); |
| 457 | } |
| 458 | assignValues(values: conditionArgs, variables: resultVars, rewriter, loc); |
| 459 | |
| 460 | // Convert scf.condition to condition variable assignment. |
| 461 | Value condition = rewriter.getRemappedValue(key: condOp.getCondition()); |
| 462 | emitc::AssignOp::create(builder&: rewriter, location: loc, var: conditionVal, value: condition); |
| 463 | |
| 464 | // Wrap body region in conditional to preserve scf semantics. Only create |
| 465 | // ifOp if after-region is non-empty. |
| 466 | if (whileOp.getAfterBody()->getOperations().size() > 1) { |
| 467 | auto ifOp = emitc::IfOp::create(builder&: rewriter, location: loc, cond: condition, addThenBlock: false, addElseBlock: false); |
| 468 | |
| 469 | // Prepare the after region (loop body) for merging. |
| 470 | Block *afterBlock = &whileOp.getAfter().front(); |
| 471 | Block *ifBodyBlock = rewriter.createBlock(parent: &ifOp.getBodyRegion()); |
| 472 | |
| 473 | // Replacement values for after block using condition op arguments. |
| 474 | SmallVector<Value> afterReplacingValues; |
| 475 | for (Value arg : condOp.getArgs()) |
| 476 | afterReplacingValues.push_back(Elt: rewriter.getRemappedValue(key: arg)); |
| 477 | |
| 478 | rewriter.mergeBlocks(source: afterBlock, dest: ifBodyBlock, argValues: afterReplacingValues); |
| 479 | |
| 480 | if (failed(Result: lowerYield(op: whileOp, resultVariables: loopVars, rewriter, |
| 481 | yield: cast<scf::YieldOp>(Val: ifBodyBlock->getTerminator())))) |
| 482 | return failure(); |
| 483 | } |
| 484 | |
| 485 | rewriter.eraseOp(op: condOp); |
| 486 | |
| 487 | // Create condition region that loads from the flag variable. |
| 488 | Region &condRegion = loweredDo.getConditionRegion(); |
| 489 | Block *condBlock = rewriter.createBlock(parent: &condRegion); |
| 490 | rewriter.setInsertionPointToStart(condBlock); |
| 491 | |
| 492 | auto exprOp = emitc::ExpressionOp::create( |
| 493 | builder&: rewriter, location: loc, result: i1Type, defs: conditionVal, /*do_not_inline=*/false); |
| 494 | Block *exprBlock = rewriter.createBlock(parent: &exprOp.getBodyRegion()); |
| 495 | |
| 496 | // Set up the expression block to load the condition variable. |
| 497 | exprBlock->addArgument(type: conditionVal.getType(), loc); |
| 498 | rewriter.setInsertionPointToStart(exprBlock); |
| 499 | |
| 500 | // Load the condition value and yield it as the expression result. |
| 501 | Value cond = |
| 502 | emitc::LoadOp::create(builder&: rewriter, location: loc, result: i1Type, operand: exprBlock->getArgument(i: 0)); |
| 503 | emitc::YieldOp::create(builder&: rewriter, location: loc, result: cond); |
| 504 | |
| 505 | // Yield the expression as the condition region result. |
| 506 | rewriter.setInsertionPointToEnd(condBlock); |
| 507 | emitc::YieldOp::create(builder&: rewriter, location: loc, result: exprOp); |
| 508 | |
| 509 | return success(); |
| 510 | } |
| 511 | }; |
| 512 | |
| 513 | void mlir::populateSCFToEmitCConversionPatterns(RewritePatternSet &patterns, |
| 514 | TypeConverter &typeConverter) { |
| 515 | patterns.add<ForLowering>(arg&: typeConverter, args: patterns.getContext()); |
| 516 | patterns.add<IfLowering>(arg&: typeConverter, args: patterns.getContext()); |
| 517 | patterns.add<IndexSwitchOpLowering>(arg&: typeConverter, args: patterns.getContext()); |
| 518 | patterns.add<WhileLowering>(arg&: typeConverter, args: patterns.getContext()); |
| 519 | } |
| 520 | |
| 521 | void SCFToEmitCPass::runOnOperation() { |
| 522 | RewritePatternSet patterns(&getContext()); |
| 523 | EmitCTypeConverter typeConverter(&getContext()); |
| 524 | populateEmitCSizeTTypeConversions(converter&: typeConverter); |
| 525 | populateSCFToEmitCConversionPatterns(patterns, typeConverter); |
| 526 | |
| 527 | // Configure conversion to lower out SCF operations. |
| 528 | ConversionTarget target(getContext()); |
| 529 | target |
| 530 | .addIllegalOp<scf::ForOp, scf::IfOp, scf::IndexSwitchOp, scf::WhileOp>(); |
| 531 | target.markUnknownOpDynamicallyLegal(fn: [](Operation *) { return true; }); |
| 532 | if (failed( |
| 533 | Result: applyPartialConversion(op: getOperation(), target, patterns: std::move(patterns)))) |
| 534 | signalPassFailure(); |
| 535 | } |
| 536 | |