Skip to content

Commit 296e416

Browse files
authored
Use SSA form for memref liveness tracking (#5)
* Initial changes * Go back to a single consistent terminator, with yield semantics * Rename terminator to yield * fix parser * Fixed from code review
1 parent 9f3ab92 commit 296e416

File tree

12 files changed

+140
-75
lines changed

12 files changed

+140
-75
lines changed

mlir/examples/toy/Ch5/mlir/LowerToAffineLoops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
7878
// Terminate the loop body and update the rewriter insertion point to the
7979
// beginning of the loop.
8080
rewriter.setInsertionPointToStart(loop.getBody());
81-
rewriter.create<AffineTerminatorOp>(loc);
81+
rewriter.create<AffineYieldOp>(loc);
8282
rewriter.setInsertionPointToStart(loop.getBody());
8383
}
8484

mlir/examples/toy/Ch6/mlir/LowerToAffineLoops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
7878
// Terminate the loop body and update the rewriter insertion point to the
7979
// beginning of the loop.
8080
rewriter.setInsertionPointToStart(loop.getBody());
81-
rewriter.create<AffineTerminatorOp>(loc);
81+
rewriter.create<AffineYieldOp>(loc);
8282
rewriter.setInsertionPointToStart(loop.getBody());
8383
}
8484

mlir/examples/toy/Ch7/mlir/LowerToAffineLoops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void lowerOpToLoops(Operation *op, ArrayRef<Value> operands,
7878
// Terminate the loop body and update the rewriter insertion point to the
7979
// beginning of the loop.
8080
rewriter.setInsertionPointToStart(loop.getBody());
81-
rewriter.create<AffineTerminatorOp>(loc);
81+
rewriter.create<AffineYieldOp>(loc);
8282
rewriter.setInsertionPointToStart(loop.getBody());
8383
}
8484

mlir/include/mlir/Dialect/Affine/IR/AffineOps.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AffineApplyOp;
2727
class AffineBound;
2828
class AffineDimExpr;
2929
class AffineValueMap;
30-
class AffineTerminatorOp;
30+
class AffineYieldOp;
3131
class FlatAffineConstraints;
3232
class OpBuilder;
3333

mlir/include/mlir/Dialect/Affine/IR/AffineOps.td

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class Affine_Op<string mnemonic, list<OpTrait> traits = []> :
3737
let parser = [{ return ::parse$cppClass(parser, result); }];
3838
}
3939

40-
// Require regions to have affine terminator.
40+
// Require regions to have affine.yield.
4141
def ImplicitAffineTerminator
42-
: SingleBlockImplicitTerminator<"AffineTerminatorOp">;
42+
: SingleBlockImplicitTerminator<"AffineYieldOp">;
4343

4444
def AffineApplyOp : Affine_Op<"apply", [NoSideEffect]> {
4545
let summary = "affine apply operation";
@@ -114,7 +114,7 @@ def AffineForOp : Affine_Op<"for",
114114

115115
The `affine.for` operation represents an affine loop nest. It has one region
116116
containing its body. This region must contain one block that terminates with
117-
[`affine.terminator`](#affineterminator-operation). *Note:* when
117+
[`affine.yield`](#affinyield-operation). *Note:* when
118118
`affine.for` is printed in custom format, the terminator is omitted. The
119119
block has one argument of [`index`](../LangRef.md#index-type) type that
120120
represents the induction variable of the loop.
@@ -292,7 +292,7 @@ def AffineIfOp : Affine_Op<"if",
292292
clauses. The latter may be empty (i.e. contain no blocks), meaning the
293293
absence of the else clause. When non-empty, both regions must contain
294294
exactly one block terminating with
295-
[`affine.terminator`](#affineterminator-operation). *Note:* when `affine.if`
295+
[`affine.yield`](#affineyield-operation). *Note:* when `affine.if`
296296
is printed in custom format, the terminator is omitted. These blocks must
297297
not have any arguments.
298298

@@ -316,14 +316,17 @@ def AffineIfOp : Affine_Op<"if",
316316
}
317317
```
318318
}];
319+
let results = (outs Variadic<AnyMemRef>:$results);
319320
let arguments = (ins Variadic<AnyType>);
320321
let regions = (region SizedRegion<1>:$thenRegion, AnyRegion:$elseRegion);
321322

322323
let skipDefaultBuilders = 1;
323324

324325
let builders = [
325326
OpBuilder<"Builder *builder, OperationState &result, "
326-
"IntegerSet set, ValueRange args, bool withElseRegion">
327+
"IntegerSet set, ValueRange args, bool withElseRegion">,
328+
OpBuilder<"Builder *builder, OperationState &result, TypeRange resultTypes, "
329+
"IntegerSet set, ValueRange args, bool withElseRegion">,
327330
];
328331

329332
let extraClassDeclaration = [{
@@ -440,7 +443,7 @@ def AffineParallelOp : Affine_Op<"parallel", [ImplicitAffineTerminator]> {
440443
steps, are positive constant integers which defaults to "1" if not present.
441444
The lower and upper bounds specify a half-open range: the range includes the
442445
lower bound but does not include the upper bound. The body region must
443-
contain exactly one block that terminates with "affine.terminator".
446+
contain exactly one block that terminates with "affine.yield".
444447

445448
The lower and upper bounds of a parallel operation are represented as an
446449
application of an affine mapping to a list of SSA values passed to the map.
@@ -466,14 +469,15 @@ def AffineParallelOp : Affine_Op<"parallel", [ImplicitAffineTerminator]> {
466469
AffineMapAttr:$upperBoundsMap,
467470
I64ArrayAttr:$steps,
468471
Variadic<Index>:$mapOperands);
472+
let results = (outs Variadic<AnyMemRef>:$results);
469473
let regions = (region SizedRegion<1>:$region);
470474

471475
let builders = [
472-
OpBuilder<"Builder* builder, OperationState& result,"
476+
OpBuilder<"Builder* builder, OperationState& result, ArrayRef<Type> resultTypes,"
473477
"ArrayRef<int64_t> ranges">,
474-
OpBuilder<"Builder* builder, OperationState& result, AffineMap lbMap,"
478+
OpBuilder<"Builder* builder, OperationState& result, ArrayRef<Type> resultTypes, AffineMap lbMap,"
475479
"ValueRange lbArgs, AffineMap ubMap, ValueRange ubArgs">,
476-
OpBuilder<"Builder* builder, OperationState& result, AffineMap lbMap,"
480+
OpBuilder<"Builder* builder, OperationState& result, ArrayRef<Type> resultTypes, AffineMap lbMap,"
477481
"ValueRange lbArgs, AffineMap ubMap, ValueRange ubArgs,"
478482
"ArrayRef<int64_t> steps">
479483
];
@@ -581,35 +585,26 @@ def AffinePrefetchOp : Affine_Op<"prefetch"> {
581585
let hasFolder = 1;
582586
}
583587

584-
def AffineTerminatorOp :
585-
Affine_Op<"terminator", [NoSideEffect, Terminator]> {
586-
let summary = "affine terminator operation";
588+
def AffineYieldOp : Affine_Op<"yield", [NoSideEffect, Terminator]>,
589+
Arguments<(ins Variadic<AnyMemRef>:$values)> {
590+
let summary = "Yield values to parent operation";
587591
let description = [{
588-
Syntax:
589-
590-
```
591-
operation ::= `"affine.terminator"() : () -> ()`
592+
"affine.yield" yields zero or more SSA values from an affine op region and
593+
terminates the region. The semantics of how the values yielded are used
594+
is defined by the parent operation.
595+
If "affine.yield" has any operands, the operands must match the parent
596+
operation's results.
597+
If the parent operation defines no values, then the "affine.yield" may be
598+
left out in the custom syntax and the builders will insert one implicitly.
599+
Otherwise, it has to be present in the syntax to indicate which values are
600+
yielded.
592601
```
593-
594-
Affine terminator is a special terminator operation for blocks inside affine
595-
loops ([`affine.for`](#affinefor-operation)) and branches
596-
([`affine.if`](#affineif-operation)). It unconditionally transmits the
597-
control flow to the successor of the operation enclosing the region.
598-
599-
*Rationale*: bodies of affine operations are [blocks](../LangRef.md#blocks)
600-
that must have terminators. Loops and branches represent structured control
601-
flow and should not accept arbitrary branches as terminators.
602-
603-
This operation does _not_ have a custom syntax. However, affine control
604-
operations omit the terminator in their custom syntax for brevity.
605602
}];
603+
let arguments = (ins Variadic<AnyMemRef>:$results);
604+
let builders = [
605+
OpBuilder<"Builder *builder, OperationState &result",
606+
[{ /* nothing to do */ }]>
607+
];
606608

607-
// No custom parsing/printing form.
608-
let parser = ?;
609-
let printer = ?;
610-
611-
// Fully specified by traits.
612-
let verifier = ?;
613609
}
614-
615610
#endif // AFFINE_OPS

mlir/lib/Analysis/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ bool mlir::isLoopParallel(AffineForOp forOp) {
10101010
auto walkResult = forOp.walk([&](Operation *opInst) -> WalkResult {
10111011
if (isa<AffineLoadOp>(opInst) || isa<AffineStoreOp>(opInst))
10121012
loadAndStoreOpInsts.push_back(opInst);
1013-
else if (!isa<AffineForOp>(opInst) && !isa<AffineTerminatorOp>(opInst) &&
1013+
else if (!isa<AffineForOp>(opInst) && !isa<AffineYieldOp>(opInst) &&
10141014
!isa<AffineIfOp>(opInst) &&
10151015
!MemoryEffectOpInterface::hasNoEffect(opInst))
10161016
return WalkResult::interrupt();

mlir/lib/Conversion/AffineToStandard/AffineToStandard.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ class AffineMaxLowering : public OpRewritePattern<AffineMaxOp> {
326326
};
327327

328328
/// Affine terminators are removed.
329-
class AffineTerminatorLowering : public OpRewritePattern<AffineTerminatorOp> {
329+
class AffineTerminatorLowering : public OpRewritePattern<AffineYieldOp> {
330330
public:
331-
using OpRewritePattern<AffineTerminatorOp>::OpRewritePattern;
331+
using OpRewritePattern<AffineYieldOp>::OpRewritePattern;
332332

333-
LogicalResult matchAndRewrite(AffineTerminatorOp op,
333+
LogicalResult matchAndRewrite(AffineYieldOp op,
334334
PatternRewriter &rewriter) const override {
335335
rewriter.replaceOpWithNewOp<loop::YieldOp>(op);
336336
return success();

0 commit comments

Comments
 (0)