Skip to content

Commit a5fa3b3

Browse files
committed
Make return types optional and change names
1 parent 7d99581 commit a5fa3b3

File tree

10 files changed

+131
-89
lines changed

10 files changed

+131
-89
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def AffineForOp : Affine_Op<"for",
118118
[AttrSizedOperandSegments, AutomaticAllocationScope,
119119
ImplicitAffineTerminator, ConditionallySpeculatable,
120120
RecursiveMemoryEffects, DeclareOpInterfaceMethods<LoopLikeOpInterface,
121-
["getInductionVars", "getMixedLowerBound", "getMixedStep",
122-
"getMixedUpperBound", "getYieldedValuesMutable",
121+
["getInductionVars", "getLowerBounds", "getSteps",
122+
"getUpperBounds", "getYieldedValuesMutable",
123123
"replaceWithAdditionalYields"]>,
124124
DeclareOpInterfaceMethods<RegionBranchOpInterface,
125125
["getEntrySuccessorOperands"]>]> {
@@ -671,7 +671,7 @@ def AffineParallelOp : Affine_Op<"parallel",
671671
I32ElementsAttr:$lowerBoundsGroups,
672672
AffineMapAttr:$upperBoundsMap,
673673
I32ElementsAttr:$upperBoundsGroups,
674-
I64SmallVectorArrayAttr:$steps,
674+
I64SmallVectorArrayAttr:$step,
675675
Variadic<Index>:$mapOperands);
676676
let results = (outs Variadic<AnyType>:$results);
677677
let regions = (region SizedRegion<1>:$region);
@@ -682,7 +682,7 @@ def AffineParallelOp : Affine_Op<"parallel",
682682
OpBuilder<(ins "TypeRange":$resultTypes,
683683
"ArrayRef<arith::AtomicRMWKind>":$reductions, "ArrayRef<AffineMap>":$lbMaps,
684684
"ValueRange":$lbArgs, "ArrayRef<AffineMap>":$ubMaps, "ValueRange":$ubArgs,
685-
"ArrayRef<int64_t>":$steps)>
685+
"ArrayRef<int64_t>":$step)>
686686
];
687687

688688
let extraClassDeclaration = [{
@@ -727,7 +727,7 @@ def AffineParallelOp : Affine_Op<"parallel",
727727
static StringRef getUpperBoundsGroupsAttrStrName() {
728728
return "upperBoundsGroups";
729729
}
730-
static StringRef getStepsAttrStrName() { return "steps"; }
730+
static StringRef getStepsAttrStrName() { return "step"; }
731731

732732
/// Returns `true` if the loop bounds have min/max expressions.
733733
bool hasMinMaxBounds() {

mlir/include/mlir/Dialect/SCF/IR/SCFOps.td

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def ExecuteRegionOp : SCF_Op<"execute_region", [
136136
def ForOp : SCF_Op<"for",
137137
[AutomaticAllocationScope, DeclareOpInterfaceMethods<LoopLikeOpInterface,
138138
["getInitsMutable", "getLoopResults", "getRegionIterArgs",
139-
"getInductionVars", "getMixedLowerBound", "getMixedStep",
140-
"getMixedUpperBound", "getYieldedValuesMutable",
139+
"getInductionVars", "getLowerBounds", "getSteps",
140+
"getUpperBounds", "getYieldedValuesMutable",
141141
"promoteIfSingleIteration", "replaceWithAdditionalYields",
142142
"yieldTiledValuesAndReplace"]>,
143143
AllTypesMatch<["lowerBound", "upperBound", "step"]>,
@@ -302,7 +302,7 @@ def ForallOp : SCF_Op<"forall", [
302302
AutomaticAllocationScope,
303303
DeclareOpInterfaceMethods<LoopLikeOpInterface,
304304
["getInitsMutable", "getRegionIterArgs", "getInductionVars",
305-
"getMixedLowerBound", "getMixedUpperBound", "getMixedStep",
305+
"getLowerBounds", "getUpperBounds", "getSteps",
306306
"promoteIfSingleIteration", "yieldTiledValuesAndReplace"]>,
307307
RecursiveMemoryEffects,
308308
SingleBlockImplicitTerminator<"scf::InParallelOp">,
@@ -510,6 +510,27 @@ def ForallOp : SCF_Op<"forall", [
510510
];
511511

512512
let extraClassDeclaration = [{
513+
// Get lower bounds as OpFoldResult.
514+
SmallVector<OpFoldResult> getMixedLowerBound() {
515+
auto maybeLowerBounds = getLowerBounds();
516+
assert(maybeLowerBounds.has_value() && "expected values");
517+
return *maybeLowerBounds;
518+
}
519+
520+
// Get upper bounds as OpFoldResult.
521+
SmallVector<OpFoldResult> getMixedUpperBound() {
522+
auto maybeUpperBounds = getUpperBounds();
523+
assert(maybeUpperBounds.has_value() && "expected values");
524+
return *maybeUpperBounds;
525+
}
526+
527+
// Get steps as OpFoldResult.
528+
SmallVector<OpFoldResult> getMixedStep() {
529+
auto maybeSteps = getSteps();
530+
assert(maybeSteps.has_value() && "expected values");
531+
return *maybeSteps;
532+
}
533+
513534
/// Get lower bounds as values.
514535
SmallVector<Value> getLowerBound(OpBuilder &b) {
515536
return getValueOrCreateConstantIndexOp(b, getLoc(), getMixedLowerBound());
@@ -744,7 +765,7 @@ def ParallelOp : SCF_Op<"parallel",
744765
[AutomaticAllocationScope,
745766
AttrSizedOperandSegments,
746767
DeclareOpInterfaceMethods<LoopLikeOpInterface, ["getInductionVars",
747-
"getMixedLowerBound", "getMixedUpperBound", "getMixedStep"]>,
768+
"getLowerBounds", "getUpperBounds", "getSteps"]>,
748769
RecursiveMemoryEffects,
749770
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
750771
SingleBlockImplicitTerminator<"scf::ReduceOp">,

mlir/include/mlir/Interfaces/LoopLikeInterface.td

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,34 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
106106
InterfaceMethod<[{
107107
Return all lower bounds.
108108
}],
109-
/*retTy=*/"::llvm::SmallVector<::mlir::OpFoldResult>",
110-
/*methodName=*/"getMixedLowerBound",
109+
/*retTy=*/"::std::optional<::llvm::SmallVector<::mlir::OpFoldResult>>",
110+
/*methodName=*/"getLowerBounds",
111111
/*args=*/(ins),
112112
/*methodBody=*/"",
113113
/*defaultImplementation=*/[{
114-
return {};
114+
return std::nullopt;
115115
}]
116116
>,
117117
InterfaceMethod<[{
118118
Return all steps.
119119
}],
120-
/*retTy=*/"::llvm::SmallVector<::mlir::OpFoldResult>",
121-
/*methodName=*/"getMixedStep",
120+
/*retTy=*/"std::optional<::llvm::SmallVector<::mlir::OpFoldResult>>",
121+
/*methodName=*/"getSteps",
122122
/*args=*/(ins),
123123
/*methodBody=*/"",
124124
/*defaultImplementation=*/[{
125-
return {};
125+
return std::nullopt;
126126
}]
127127
>,
128128
InterfaceMethod<[{
129129
Return all upper bounds.
130130
}],
131-
/*retTy=*/"::llvm::SmallVector<::mlir::OpFoldResult>",
132-
/*methodName=*/"getMixedUpperBound",
131+
/*retTy=*/"::std::optional<::llvm::SmallVector<::mlir::OpFoldResult>>",
132+
/*methodName=*/"getUpperBounds",
133133
/*args=*/(ins),
134134
/*methodBody=*/"",
135135
/*defaultImplementation=*/[{
136-
return {};
136+
return std::nullopt;
137137
}]
138138
>,
139139
InterfaceMethod<[{
@@ -242,26 +242,26 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
242242
/// Return the single lower bound value or attribute if it exists, otherwise
243243
/// return std::nullopt.
244244
::std::optional<::mlir::OpFoldResult> getSingleLowerBound() {
245-
auto lowerBounds = this->getMixedLowerBound();
246-
if (lowerBounds.size() == 1)
247-
return lowerBounds[0];
248-
return std::nullopt;
245+
auto lowerBounds = this->getLowerBounds();
246+
if (lowerBounds.has_value() && (*lowerBounds).size() == 1)
247+
return (*lowerBounds)[0];
248+
return std::nullopt;
249249
}
250250
/// Return the single step value or attribute if it exists, otherwise
251251
/// return std::nullopt.
252252
::std::optional<::mlir::OpFoldResult> getSingleStep() {
253-
auto steps = this->getMixedStep();
254-
if (steps.size() == 1)
255-
return steps[0];
256-
return std::nullopt;
253+
auto steps = this->getSteps();
254+
if (steps.has_value() && (*steps).size() == 1)
255+
return (*steps)[0];
256+
return std::nullopt;
257257
}
258258
/// Return the single upper bound value or attribute if it exists, otherwise
259259
/// return std::nullopt.
260260
::std::optional<::mlir::OpFoldResult> getSingleUpperBound() {
261-
auto upperBounds = this->getMixedUpperBound();
262-
if (upperBounds.size() == 1)
263-
return upperBounds[0];
264-
return std::nullopt;
261+
auto upperBounds = this->getUpperBounds();
262+
if (upperBounds.has_value() && (*upperBounds).size() == 1)
263+
return (*upperBounds)[0];
264+
return std::nullopt;
265265
}
266266

267267
/// Append the specified additional "init" operands: replace this loop with

mlir/lib/Conversion/AffineToStandard/AffineToStandard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ class AffineParallelLowering : public OpRewritePattern<AffineParallelOp> {
196196
return rewriter.notifyMatchFailure(op, "couldn't convert upper bounds");
197197
upperBoundTuple.push_back(upper);
198198
}
199-
steps.reserve(op.getSteps().size());
200-
for (int64_t step : op.getSteps())
199+
steps.reserve(op.getStep().size());
200+
for (int64_t step : op.getStep())
201201
steps.push_back(rewriter.create<arith::ConstantIndexOp>(loc, step));
202202

203203
// Get the terminator op.

mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,9 @@ LogicalResult ForallLowering::matchAndRewrite(ForallOp forallOp,
695695
"only fully bufferized scf.forall ops can be lowered to scf.parallel");
696696

697697
// Convert mixed bounds and steps to SSA values.
698-
SmallVector<Value> lbs = getValueOrCreateConstantIndexOp(
699-
rewriter, loc, forallOp.getMixedLowerBound());
700-
SmallVector<Value> ubs = getValueOrCreateConstantIndexOp(
701-
rewriter, loc, forallOp.getMixedUpperBound());
702-
SmallVector<Value> steps =
703-
getValueOrCreateConstantIndexOp(rewriter, loc, forallOp.getMixedStep());
698+
SmallVector<Value> lbs = forallOp.getLowerBound(rewriter);
699+
SmallVector<Value> ubs = forallOp.getUpperBound(rewriter);
700+
SmallVector<Value> steps = forallOp.getStep(rewriter);
704701

705702
// Create empty scf.parallel op.
706703
auto parallelOp = rewriter.create<ParallelOp>(loc, lbs, ubs, steps);

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,23 +2456,26 @@ SmallVector<Region *> AffineForOp::getLoopRegions() { return {&getRegion()}; }
24562456

24572457
ValueRange AffineForOp::getInductionVars() { return {getInductionVar()}; }
24582458

2459-
SmallVector<OpFoldResult> AffineForOp::getMixedLowerBound() {
2459+
std::optional<SmallVector<OpFoldResult>> AffineForOp::getLowerBounds() {
24602460
if (!hasConstantLowerBound())
2461-
return {};
2461+
return std::nullopt;
24622462
OpBuilder b(getContext());
2463-
return {OpFoldResult(b.getI64IntegerAttr(getConstantLowerBound()))};
2463+
return SmallVector<OpFoldResult>{
2464+
OpFoldResult(b.getI64IntegerAttr(getConstantLowerBound()))};
24642465
}
24652466

2466-
SmallVector<OpFoldResult> AffineForOp::getMixedStep() {
2467+
std::optional<SmallVector<OpFoldResult>> AffineForOp::getSteps() {
24672468
OpBuilder b(getContext());
2468-
return {OpFoldResult(b.getI64IntegerAttr(getStepAsInt()))};
2469+
return SmallVector<OpFoldResult>{
2470+
OpFoldResult(b.getI64IntegerAttr(getStepAsInt()))};
24692471
}
24702472

2471-
SmallVector<OpFoldResult> AffineForOp::getMixedUpperBound() {
2473+
std::optional<SmallVector<OpFoldResult>> AffineForOp::getUpperBounds() {
24722474
if (!hasConstantUpperBound())
24732475
return {};
24742476
OpBuilder b(getContext());
2475-
return {OpFoldResult(b.getI64IntegerAttr(getConstantUpperBound()))};
2477+
return SmallVector<OpFoldResult>{
2478+
OpFoldResult(b.getI64IntegerAttr(getConstantUpperBound()))};
24762479
}
24772480

24782481
FailureOr<LoopLikeOpInterface> AffineForOp::replaceWithAdditionalYields(
@@ -3753,7 +3756,7 @@ SmallVector<Region *> AffineParallelOp::getLoopRegions() {
37533756
return {&getRegion()};
37543757
}
37553758

3756-
unsigned AffineParallelOp::getNumDims() { return getSteps().size(); }
3759+
unsigned AffineParallelOp::getNumDims() { return getStep().size(); }
37573760

37583761
AffineParallelOp::operand_range AffineParallelOp::getLowerBoundsOperands() {
37593762
return getOperands().take_front(getLowerBoundsMap().getNumInputs());
@@ -3838,7 +3841,7 @@ void AffineParallelOp::setUpperBounds(ValueRange ubOperands, AffineMap map) {
38383841
}
38393842

38403843
void AffineParallelOp::setSteps(ArrayRef<int64_t> newSteps) {
3841-
setStepsAttr(getBodyBuilder().getI64ArrayAttr(newSteps));
3844+
setStepAttr(getBodyBuilder().getI64ArrayAttr(newSteps));
38423845
}
38433846

38443847
// check whether resultType match op or not in affine.parallel
@@ -3888,14 +3891,14 @@ LogicalResult AffineParallelOp::verify() {
38883891
auto numDims = getNumDims();
38893892
if (getLowerBoundsGroups().getNumElements() != numDims ||
38903893
getUpperBoundsGroups().getNumElements() != numDims ||
3891-
getSteps().size() != numDims || getBody()->getNumArguments() != numDims) {
3894+
getStep().size() != numDims || getBody()->getNumArguments() != numDims) {
38923895
return emitOpError() << "the number of region arguments ("
38933896
<< getBody()->getNumArguments()
38943897
<< ") and the number of map groups for lower ("
38953898
<< getLowerBoundsGroups().getNumElements()
38963899
<< ") and upper bound ("
38973900
<< getUpperBoundsGroups().getNumElements()
3898-
<< "), and the number of steps (" << getSteps().size()
3901+
<< "), and the number of steps (" << getStep().size()
38993902
<< ") must all match";
39003903
}
39013904

@@ -4013,7 +4016,7 @@ void AffineParallelOp::print(OpAsmPrinter &p) {
40134016
printMinMaxBound(p, getUpperBoundsMapAttr(), getUpperBoundsGroupsAttr(),
40144017
getUpperBoundsOperands(), "min");
40154018
p << ')';
4016-
SmallVector<int64_t, 8> steps = getSteps();
4019+
SmallVector<int64_t, 8> steps = getStep();
40174020
bool elideSteps = llvm::all_of(steps, [](int64_t step) { return step == 1; });
40184021
if (!elideSteps) {
40194022
p << " step (";

mlir/lib/Dialect/Affine/Utils/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ void mlir::affine::normalizeAffineParallel(AffineParallelOp op) {
494494
return;
495495

496496
AffineMap lbMap = op.getLowerBoundsMap();
497-
SmallVector<int64_t, 8> steps = op.getSteps();
497+
SmallVector<int64_t, 8> steps = op.getStep();
498498
// No need to do any work if the parallel op is already normalized.
499499
bool isAlreadyNormalized =
500500
llvm::all_of(llvm::zip(steps, lbMap.getResults()), [](auto tuple) {

mlir/lib/Dialect/SCF/IR/SCF.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,16 @@ LogicalResult ForOp::verifyRegions() {
380380

381381
ValueRange ForOp::getInductionVars() { return {getInductionVar()}; }
382382

383-
SmallVector<OpFoldResult> ForOp::getMixedLowerBound() {
384-
return {OpFoldResult(getLowerBound())};
383+
std::optional<SmallVector<OpFoldResult>> ForOp::getLowerBounds() {
384+
return SmallVector<OpFoldResult, 1>{OpFoldResult(getLowerBound())};
385385
}
386386

387-
SmallVector<OpFoldResult> ForOp::getMixedStep() {
388-
return {OpFoldResult(getStep())};
387+
std::optional<SmallVector<OpFoldResult>> ForOp::getSteps() {
388+
return SmallVector<OpFoldResult, 1>{OpFoldResult(getStep())};
389389
}
390390

391-
SmallVector<OpFoldResult> ForOp::getMixedUpperBound() {
392-
return {OpFoldResult(getUpperBound())};
391+
std::optional<SmallVector<OpFoldResult>> ForOp::getUpperBounds() {
392+
return SmallVector<OpFoldResult, 1>{OpFoldResult(getUpperBound())};
393393
}
394394

395395
std::optional<ResultRange> ForOp::getLoopResults() { return getResults(); }
@@ -1431,19 +1431,19 @@ ValueRange ForallOp::getInductionVars() {
14311431
}
14321432

14331433
// Get lower bounds as OpFoldResult.
1434-
SmallVector<OpFoldResult> ForallOp::getMixedLowerBound() {
1434+
std::optional<SmallVector<OpFoldResult>> ForallOp::getLowerBounds() {
14351435
Builder b(getOperation()->getContext());
14361436
return getMixedValues(getStaticLowerBound(), getDynamicLowerBound(), b);
14371437
}
14381438

14391439
// Get upper bounds as OpFoldResult.
1440-
SmallVector<OpFoldResult> ForallOp::getMixedUpperBound() {
1440+
std::optional<SmallVector<OpFoldResult>> ForallOp::getUpperBounds() {
14411441
Builder b(getOperation()->getContext());
14421442
return getMixedValues(getStaticUpperBound(), getDynamicUpperBound(), b);
14431443
}
14441444

14451445
// Get steps as OpFoldResult.
1446-
SmallVector<OpFoldResult> ForallOp::getMixedStep() {
1446+
std::optional<SmallVector<OpFoldResult>> ForallOp::getSteps() {
14471447
Builder b(getOperation()->getContext());
14481448
return getMixedValues(getStaticStep(), getDynamicStep(), b);
14491449
}
@@ -3006,15 +3006,17 @@ SmallVector<Region *> ParallelOp::getLoopRegions() { return {&getRegion()}; }
30063006

30073007
ValueRange ParallelOp::getInductionVars() { return getBody()->getArguments(); }
30083008

3009-
SmallVector<OpFoldResult> ParallelOp::getMixedLowerBound() {
3009+
std::optional<SmallVector<OpFoldResult>> ParallelOp::getLowerBounds() {
30103010
return getLowerBound();
30113011
}
30123012

3013-
SmallVector<OpFoldResult> ParallelOp::getMixedUpperBound() {
3013+
std::optional<SmallVector<OpFoldResult>> ParallelOp::getUpperBounds() {
30143014
return getUpperBound();
30153015
}
30163016

3017-
SmallVector<OpFoldResult> ParallelOp::getMixedStep() { return getStep(); }
3017+
std::optional<SmallVector<OpFoldResult>> ParallelOp::getSteps() {
3018+
return getStep();
3019+
}
30183020

30193021
ParallelOp mlir::scf::getParallelForInductionVarOwner(Value val) {
30203022
auto ivArg = llvm::dyn_cast<BlockArgument>(val);

mlir/lib/Dialect/SCF/Transforms/ForallToFor.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ mlir::scf::forallToForLoop(RewriterBase &rewriter, scf::ForallOp forallOp,
3434
rewriter.setInsertionPoint(forallOp);
3535

3636
Location loc = forallOp.getLoc();
37-
SmallVector<Value> lbs = getValueOrCreateConstantIndexOp(
38-
rewriter, loc, forallOp.getMixedLowerBound());
39-
SmallVector<Value> ubs = getValueOrCreateConstantIndexOp(
40-
rewriter, loc, forallOp.getMixedUpperBound());
41-
SmallVector<Value> steps =
42-
getValueOrCreateConstantIndexOp(rewriter, loc, forallOp.getMixedStep());
37+
SmallVector<Value> lbs = forallOp.getLowerBound(rewriter);
38+
SmallVector<Value> ubs = forallOp.getUpperBound(rewriter);
39+
SmallVector<Value> steps = forallOp.getStep(rewriter);
4340
LoopNest loopNest = scf::buildLoopNest(rewriter, loc, lbs, ubs, steps);
4441

4542
SmallVector<Value> ivs = llvm::map_to_vector(

0 commit comments

Comments
 (0)