27 #define GEN_PASS_DEF_SCFTOEMITC
28 #include "mlir/Conversion/Passes.h.inc"
36 struct SCFToEmitCPass :
public impl::SCFToEmitCBase<SCFToEmitCPass> {
37 void runOnOperation()
override;
56 return resultVariables;
65 Type resultType = result.getType();
67 emitc::VariableOp var =
68 rewriter.
create<emitc::VariableOp>(loc, resultType, noInit);
69 resultVariables.push_back(var);
72 return resultVariables;
79 for (
auto [value, var] : llvm::zip(values, variables))
80 rewriter.
create<emitc::AssignOp>(loc, var, value);
91 assignValues(operands, resultVariables, rewriter, loc);
93 rewriter.
create<emitc::YieldOp>(loc);
104 createVariablesForResults(forOp, rewriter);
106 createVariablesForResults(forOp, rewriter);
108 assignValues(forOp.getInits(), iterArgsVariables, rewriter, loc);
110 emitc::ForOp loweredFor = rewriter.
create<emitc::ForOp>(
111 loc, forOp.getLowerBound(), forOp.getUpperBound(), forOp.getStep());
113 Block *loweredBody = loweredFor.getBody();
116 rewriter.
eraseOp(loweredBody->getTerminator());
119 replacingValues.push_back(loweredFor.getInductionVar());
120 replacingValues.append(iterArgsVariables.begin(), iterArgsVariables.end());
122 rewriter.
mergeBlocks(forOp.getBody(), loweredBody, replacingValues);
123 lowerYield(iterArgsVariables, rewriter,
124 cast<scf::YieldOp>(loweredBody->getTerminator()));
127 assignValues(iterArgsVariables, resultVariables, rewriter, loc);
129 rewriter.
replaceOp(forOp, resultVariables);
151 createVariablesForResults(ifOp, rewriter);
158 auto lowerRegion = [&resultVariables, &rewriter](
Region ®ion,
161 Operation *terminator = loweredRegion.back().getTerminator();
162 lowerYield(resultVariables, rewriter, cast<scf::YieldOp>(terminator));
165 Region &thenRegion = ifOp.getThenRegion();
166 Region &elseRegion = ifOp.getElseRegion();
168 bool hasElseBlock = !elseRegion.
empty();
171 rewriter.
create<emitc::IfOp>(loc, ifOp.getCondition(),
false,
false);
173 Region &loweredThenRegion = loweredIf.getThenRegion();
174 lowerRegion(thenRegion, loweredThenRegion);
177 Region &loweredElseRegion = loweredIf.getElseRegion();
178 lowerRegion(elseRegion, loweredElseRegion);
181 rewriter.
replaceOp(ifOp, resultVariables);
190 void SCFToEmitCPass::runOnOperation() {
196 target.addIllegalOp<scf::ForOp, scf::IfOp>();
197 target.markUnknownOpDynamicallyLegal([](
Operation *) {
return true; });
static MLIRContext * getContext(OpFoldResult val)
Block represents an ordered list of Operations.
This class describes a specific conversion target.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
RAII guard to reset the insertion point of the builder when destroyed.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This is a value defined by a result of an operation.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
Location getLoc()
The source location the operation was defined or derived from.
result_range getResults()
unsigned getNumResults()
Return the number of results held by this operation.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
void mergeBlocks(Block *source, Block *dest, ValueRange argValues=std::nullopt)
Inline the operations of block 'source' into the end of block 'dest'.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
Include the generated interface declarations.
void populateSCFToEmitCConversionPatterns(RewritePatternSet &patterns)
Collect a set of patterns to convert SCF operations to the EmitC dialect.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...