MLIR  19.0.0git
ControlFlowToSCF.h
Go to the documentation of this file.
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 #ifndef MLIR_CONVERSION_CONTROLFLOWTOSCF_CONTROLFLOWTOSCF_H
14 #define MLIR_CONVERSION_CONTROLFLOWTOSCF_CONTROLFLOWTOSCF_H
15 
16 #include <memory>
17 
19 
20 namespace mlir {
21 class Pass;
22 
23 /// Implementation of `CFGToSCFInterface` used to lift Control Flow Dialect
24 /// operations to SCF Dialect operations.
26 public:
27  /// Creates an `scf.if` op if `controlFlowCondOp` is a `cf.cond_br` op or
28  /// an `scf.index_switch` if `controlFlowCondOp` is a `cf.switch`.
29  /// Returns failure otherwise.
31  OpBuilder &builder, Operation *controlFlowCondOp, TypeRange resultTypes,
32  MutableArrayRef<Region> regions) override;
33 
34  /// Creates an `scf.yield` op returning the given results.
36  Location loc, OpBuilder &builder, Operation *branchRegionOp,
37  Operation *replacedControlFlowOp, ValueRange results) override;
38 
39  /// Creates an `scf.while` op. The loop body is made the before-region of the
40  /// while op and terminated with an `scf.condition` op. The after-region does
41  /// nothing but forward the iteration variables.
44  ValueRange loopVariablesInit, Value condition,
45  ValueRange loopVariablesNextIter,
46  Region &&loopBody) override;
47 
48  /// Creates an `arith.constant` with an i32 attribute of the given value.
50  unsigned value) override;
51 
52  /// Creates a `cf.switch` op with the given cases and flag.
53  void createCFGSwitchOp(Location loc, OpBuilder &builder, Value flag,
54  ArrayRef<unsigned> caseValues,
55  BlockRange caseDestinations,
56  ArrayRef<ValueRange> caseArguments, Block *defaultDest,
57  ValueRange defaultArgs) override;
58 
59  /// Creates a `ub.poison` op of the given type.
60  Value getUndefValue(Location loc, OpBuilder &builder, Type type) override;
61 
62  /// Creates a `func.return` op with poison for each of the return values of
63  /// the function. It is guaranteed to be directly within the function body.
64  /// TODO: This can be made independent of the `func` dialect once the UB
65  /// dialect has a `ub.unreachable` op.
67  OpBuilder &builder,
68  Region &region) override;
69 };
70 
71 #define GEN_PASS_DECL_LIFTCONTROLFLOWTOSCFPASS
72 #include "mlir/Conversion/Passes.h.inc"
73 
74 } // namespace mlir
75 
76 #endif // MLIR_CONVERSION_CONTROLFLOWTOSCF_CONTROLFLOWTOSCF_H
This class provides an abstraction over the different types of ranges over Blocks.
Definition: BlockSupport.h:106
Block represents an ordered list of Operations.
Definition: Block.h:30
Interface that should be implemented by any caller of transformCFGToSCF.
Definition: CFGToSCF.h:28
Implementation of CFGToSCFInterface used to lift Control Flow Dialect operations to SCF Dialect opera...
FailureOr< Operation * > createUnreachableTerminator(Location loc, OpBuilder &builder, Region &region) override
Creates a func.return op with poison for each of the return values of the function.
LogicalResult createStructuredBranchRegionTerminatorOp(Location loc, OpBuilder &builder, Operation *branchRegionOp, Operation *replacedControlFlowOp, ValueRange results) override
Creates an scf.yield op returning the given results.
void createCFGSwitchOp(Location loc, OpBuilder &builder, Value flag, ArrayRef< unsigned > caseValues, BlockRange caseDestinations, ArrayRef< ValueRange > caseArguments, Block *defaultDest, ValueRange defaultArgs) override
Creates a cf.switch op with the given cases and flag.
FailureOr< Operation * > createStructuredDoWhileLoopOp(OpBuilder &builder, Operation *replacedOp, ValueRange loopVariablesInit, Value condition, ValueRange loopVariablesNextIter, Region &&loopBody) override
Creates an scf.while op.
Value getCFGSwitchValue(Location loc, OpBuilder &builder, unsigned value) override
Creates an arith.constant with an i32 attribute of the given value.
Value getUndefValue(Location loc, OpBuilder &builder, Type type) override
Creates a ub.poison op of the given type.
FailureOr< Operation * > createStructuredBranchRegionOp(OpBuilder &builder, Operation *controlFlowCondOp, TypeRange resultTypes, MutableArrayRef< Region > regions) override
Creates an scf.if op if controlFlowCondOp is a cf.cond_br op or an scf.index_switch if controlFlowCon...
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:209
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26