Skip to content

Commit 34881c8

Browse files
[mlir] Migrate away from std::nullopt (NFC)
ArrayRef has a constructor that accepts std::nullopt. This constructor dates back to the days when we still had llvm::Optional. Since the use of std::nullopt outside the context of std::optional is kind of abuse and not intuitive to new comers, I would like to move away from the constructor and eventually remove it. This patch migrates away from std::nullopt in favor of ArrayRef<T>() where we use perfect forwarding. Note that {} would be ambiguous for perfect forwarding to work.
1 parent 4847bd5 commit 34881c8

File tree

8 files changed

+34
-32
lines changed

8 files changed

+34
-32
lines changed

clang/lib/ASTMatchers/Dynamic/Marshallers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ makeMatcherAutoMarshall(ReturnType (*Func)(), StringRef MatcherName) {
10601060
BuildReturnTypeVector<ReturnType>::build(RetTypes);
10611061
return std::make_unique<FixedArgCountMatcherDescriptor>(
10621062
matcherMarshall0<ReturnType>, reinterpret_cast<void (*)()>(Func),
1063-
MatcherName, RetTypes, std::nullopt);
1063+
MatcherName, RetTypes, ArrayRef<ArgKind>());
10641064
}
10651065

10661066
/// 1-arg overload

mlir/lib/Conversion/PDLToPDLInterp/PDLToPDLInterp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ void PDLToPDLInterpPass::runOnOperation() {
991991
module.getLoc(), pdl_interp::PDLInterpDialect::getMatcherFunctionName(),
992992
builder.getFunctionType(builder.getType<pdl::OperationType>(),
993993
/*results=*/{}),
994-
/*attrs=*/std::nullopt);
994+
/*attrs=*/ArrayRef<NamedAttribute>());
995995

996996
// Create a nested module to hold the functions invoked for rewriting the IR
997997
// after a successful match.

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ static Value createLinalgBodyCalculationForElementwiseOp(
310310
auto shifted =
311311
rewriter.create<arith::ShRSIOp>(loc, resultTypes, args[0], subtract)
312312
->getResults();
313-
auto truncated =
314-
rewriter.create<arith::TruncIOp>(loc, i1Ty, shifted, std::nullopt);
313+
auto truncated = rewriter.create<arith::TruncIOp>(
314+
loc, i1Ty, shifted, ArrayRef<NamedAttribute>());
315315
auto isInputOdd =
316316
rewriter.create<arith::AndIOp>(loc, i1Ty, truncated, i1one);
317317

@@ -552,20 +552,20 @@ static Value createLinalgBodyCalculationForElementwiseOp(
552552

553553
if (isa<FloatType>(srcTy) && isa<FloatType>(dstTy) && bitExtend)
554554
return rewriter.create<arith::ExtFOp>(loc, resultTypes, args,
555-
std::nullopt);
555+
ArrayRef<NamedAttribute>());
556556

557557
if (isa<FloatType>(srcTy) && isa<FloatType>(dstTy) && !bitExtend)
558558
return rewriter.create<arith::TruncFOp>(loc, resultTypes, args,
559-
std::nullopt);
559+
ArrayRef<NamedAttribute>());
560560

561561
// 1-bit integers need to be treated as signless.
562562
if (srcTy.isInteger(1) && arith::UIToFPOp::areCastCompatible(srcTy, dstTy))
563563
return rewriter.create<arith::UIToFPOp>(loc, resultTypes, args,
564-
std::nullopt);
564+
ArrayRef<NamedAttribute>());
565565

566566
if (srcTy.isInteger(1) && isa<IntegerType>(dstTy) && bitExtend)
567567
return rewriter.create<arith::ExtUIOp>(loc, resultTypes, args,
568-
std::nullopt);
568+
ArrayRef<NamedAttribute>());
569569

570570
// Unsigned integers need an unrealized cast so that they can be passed
571571
// to UIToFP.
@@ -583,7 +583,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
583583
// All other si-to-fp conversions should be handled by SIToFP.
584584
if (arith::SIToFPOp::areCastCompatible(srcTy, dstTy))
585585
return rewriter.create<arith::SIToFPOp>(loc, resultTypes, args,
586-
std::nullopt);
586+
ArrayRef<NamedAttribute>());
587587

588588
// Casting to boolean, floats need to only be checked as not-equal to zero.
589589
if (isa<FloatType>(srcTy) && dstTy.isInteger(1)) {
@@ -690,7 +690,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
690690

691691
if (isa<IntegerType>(srcTy) && isa<IntegerType>(dstTy) && bitExtend)
692692
return rewriter.create<arith::ExtSIOp>(loc, resultTypes, args,
693-
std::nullopt);
693+
ArrayRef<NamedAttribute>());
694694

695695
if (isa<IntegerType>(srcTy) && isa<IntegerType>(dstTy) && !bitExtend) {
696696
return rewriter.create<arith::TruncIOp>(loc, dstTy, args[0]);

mlir/lib/Dialect/GPU/Transforms/DecomposeMemRefs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ static Value getFlatMemref(OpBuilder &rewriter, Location loc, Value source,
111111
getFlatOffsetAndStrides(rewriter, loc, source, offsetsTemp);
112112
MemRefType retType = inferCastResultType(base, offset);
113113
return rewriter.create<memref::ReinterpretCastOp>(loc, retType, base, offset,
114-
std::nullopt, std::nullopt);
114+
ArrayRef<OpFoldResult>(),
115+
ArrayRef<OpFoldResult>());
115116
}
116117

117118
static bool needFlatten(Value val) {

mlir/lib/Tools/PDLL/CodeGen/MLIRGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ Value CodeGen::genNonInitializerVar(const ast::VariableDecl *varDecl,
350350
Value results = builder.create<pdl::TypesOp>(
351351
loc, pdl::RangeType::get(builder.getType<pdl::TypeType>()),
352352
/*types=*/ArrayAttr());
353-
return builder.create<pdl::OperationOp>(
354-
loc, opType.getName(), operands, std::nullopt, ValueRange(), results);
353+
return builder.create<pdl::OperationOp>(loc, opType.getName(), operands,
354+
ArrayRef<StringRef>(), ValueRange(),
355+
results);
355356
}
356357

357358
if (ast::RangeType rangeTy = dyn_cast<ast::RangeType>(type)) {

mlir/test/lib/Dialect/Test/TestPatterns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ struct TestPassthroughInvalidOp : public ConversionPattern {
10141014
.getResult());
10151015
}
10161016
rewriter.replaceOpWithNewOp<TestValidOp>(op, TypeRange(), flattened,
1017-
std::nullopt);
1017+
ArrayRef<NamedAttribute>());
10181018
return success();
10191019
}
10201020
};
@@ -1030,7 +1030,7 @@ struct TestDropAndReplaceInvalidOp : public ConversionPattern {
10301030
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
10311031
ConversionPatternRewriter &rewriter) const final {
10321032
rewriter.replaceOpWithNewOp<TestValidOp>(op, TypeRange(), ValueRange(),
1033-
std::nullopt);
1033+
ArrayRef<NamedAttribute>());
10341034
return success();
10351035
}
10361036
};

mlir/unittests/IR/OperationSupportTest.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ TEST(OperandStorageTest, NonResizable) {
3232
MLIRContext context;
3333
Builder builder(&context);
3434

35-
Operation *useOp =
36-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
35+
Operation *useOp = createOp(&context, /*operands=*/ArrayRef<Value>(),
36+
builder.getIntegerType(16));
3737
Value operand = useOp->getResult(0);
3838

3939
// Create a non-resizable operation with one operand.
@@ -56,8 +56,8 @@ TEST(OperandStorageTest, Resizable) {
5656
MLIRContext context;
5757
Builder builder(&context);
5858

59-
Operation *useOp =
60-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
59+
Operation *useOp = createOp(&context, /*operands=*/ArrayRef<Value>(),
60+
builder.getIntegerType(16));
6161
Value operand = useOp->getResult(0);
6262

6363
// Create a resizable operation with one operand.
@@ -84,8 +84,8 @@ TEST(OperandStorageTest, RangeReplace) {
8484
MLIRContext context;
8585
Builder builder(&context);
8686

87-
Operation *useOp =
88-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
87+
Operation *useOp = createOp(&context, /*operands=*/ArrayRef<Value>(),
88+
builder.getIntegerType(16));
8989
Value operand = useOp->getResult(0);
9090

9191
// Create a resizable operation with one operand.
@@ -120,8 +120,8 @@ TEST(OperandStorageTest, MutableRange) {
120120
MLIRContext context;
121121
Builder builder(&context);
122122

123-
Operation *useOp =
124-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
123+
Operation *useOp = createOp(&context, /*operands=*/ArrayRef<Value>(),
124+
builder.getIntegerType(16));
125125
Value operand = useOp->getResult(0);
126126

127127
// Create a resizable operation with one operand.
@@ -159,7 +159,7 @@ TEST(OperandStorageTest, RangeErase) {
159159

160160
Type type = builder.getNoneType();
161161
Operation *useOp =
162-
createOp(&context, /*operands=*/std::nullopt, {type, type});
162+
createOp(&context, /*operands=*/ArrayRef<Value>(), {type, type});
163163
Value operand1 = useOp->getResult(0);
164164
Value operand2 = useOp->getResult(1);
165165

@@ -189,8 +189,8 @@ TEST(OperationOrderTest, OrderIsAlwaysValid) {
189189
MLIRContext context;
190190
Builder builder(&context);
191191

192-
Operation *containerOp = createOp(&context, /*operands=*/std::nullopt,
193-
/*resultTypes=*/std::nullopt,
192+
Operation *containerOp = createOp(&context, /*operands=*/ArrayRef<Value>(),
193+
/*resultTypes=*/ArrayRef<Type>(),
194194
/*numRegions=*/1);
195195
Region &region = containerOp->getRegion(0);
196196
Block *block = new Block();

mlir/unittests/IR/ValueTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ TEST(ValueTest, getNumUses) {
3131
MLIRContext context;
3232
Builder builder(&context);
3333

34-
Operation *op0 =
35-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
34+
Operation *op0 = createOp(&context, /*operands=*/ArrayRef<Value>(),
35+
builder.getIntegerType(16));
3636

3737
Value v0 = op0->getResult(0);
3838
EXPECT_EQ(v0.getNumUses(), (unsigned)0);
@@ -52,8 +52,8 @@ TEST(ValueTest, hasNUses) {
5252
MLIRContext context;
5353
Builder builder(&context);
5454

55-
Operation *op0 =
56-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
55+
Operation *op0 = createOp(&context, /*operands=*/ArrayRef<Value>(),
56+
builder.getIntegerType(16));
5757
Value v0 = op0->getResult(0);
5858
EXPECT_TRUE(v0.hasNUses(0));
5959
EXPECT_FALSE(v0.hasNUses(1));
@@ -76,8 +76,8 @@ TEST(ValueTest, hasNUsesOrMore) {
7676
MLIRContext context;
7777
Builder builder(&context);
7878

79-
Operation *op0 =
80-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
79+
Operation *op0 = createOp(&context, /*operands=*/ArrayRef<Value>(),
80+
builder.getIntegerType(16));
8181
Value v0 = op0->getResult(0);
8282
EXPECT_TRUE(v0.hasNUsesOrMore(0));
8383
EXPECT_FALSE(v0.hasNUsesOrMore(1));

0 commit comments

Comments
 (0)