Skip to content

Commit 061a719

Browse files
kazutakahirataAnthony Tran
authored andcommitted
[mlir] Migrate away from std::nullopt (NFC) (llvm#145842)
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 replaces {} with std::nullopt.
1 parent c4661f8 commit 061a719

File tree

18 files changed

+41
-45
lines changed

18 files changed

+41
-45
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ class LLVM_MemOpPatterns {
235235
}];
236236
code setInvariantGroupCode = [{
237237
if ($invariantGroup) {
238-
llvm::MDNode *metadata = llvm::MDNode::get(inst->getContext(),
239-
std::nullopt);
238+
llvm::MDNode *metadata = llvm::MDNode::get(inst->getContext(), {});
240239
inst->setMetadata(llvm::LLVMContext::MD_invariant_group, metadata);
241240
}
242241
}];

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def LLVM_LoadOp : LLVM_MemAccessOpBase<"load",
425425
auto *inst = builder.CreateLoad($_resultType, $addr, $volatile_);
426426
$res = inst;
427427
if ($invariant) {
428-
llvm::MDNode *metadata = llvm::MDNode::get(inst->getContext(), std::nullopt);
428+
llvm::MDNode *metadata = llvm::MDNode::get(inst->getContext(), {});
429429
inst->setMetadata(llvm::LLVMContext::MD_invariant_load, metadata);
430430
}
431431
if ($dereferenceable)

mlir/include/mlir/IR/Matchers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct constant_op_binder {
8888

8989
// Fold the constant to an attribute.
9090
SmallVector<OpFoldResult, 1> foldedOp;
91-
LogicalResult result = op->fold(/*operands=*/std::nullopt, foldedOp);
91+
LogicalResult result = op->fold(/*operands=*/{}, foldedOp);
9292
(void)result;
9393
assert(succeeded(result) && "expected ConstantLike op to be foldable");
9494

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,8 +810,7 @@ class RewritePatternSet {
810810
RewritePatternSet &add(ConstructorArg &&arg, ConstructorArgs &&...args) {
811811
// The following expands a call to emplace_back for each of the pattern
812812
// types 'Ts'.
813-
(addImpl<Ts>(/*debugLabels=*/std::nullopt,
814-
std::forward<ConstructorArg>(arg),
813+
(addImpl<Ts>(/*debugLabels=*/{}, std::forward<ConstructorArg>(arg),
815814
std::forward<ConstructorArgs>(args)...),
816815
...);
817816
return *this;
@@ -894,7 +893,7 @@ class RewritePatternSet {
894893
RewritePatternSet &insert(ConstructorArg &&arg, ConstructorArgs &&...args) {
895894
// The following expands a call to emplace_back for each of the pattern
896895
// types 'Ts'.
897-
(addImpl<Ts>(/*debugLabels=*/std::nullopt, arg, args...), ...);
896+
(addImpl<Ts>(/*debugLabels=*/{}, arg, args...), ...);
898897
return *this;
899898
}
900899

mlir/include/mlir/Tools/PDLL/AST/Nodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ class UserConstraintDecl final
903903
ArrayRef<VariableDecl *> results,
904904
const CompoundStmt *body,
905905
Type resultType) {
906-
return createImpl(ctx, name, inputs, /*nativeInputTypes=*/std::nullopt,
907-
results, /*codeBlock=*/std::nullopt, body, resultType);
906+
return createImpl(ctx, name, inputs, /*nativeInputTypes=*/{}, results,
907+
/*codeBlock=*/std::nullopt, body, resultType);
908908
}
909909

910910
/// Return the name of the constraint.

mlir/lib/Dialect/Async/IR/Async.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
309309
return;
310310
assert(type.getNumInputs() == argAttrs.size());
311311
call_interface_impl::addArgAndResultAttrs(
312-
builder, state, argAttrs, /*resultAttrs=*/std::nullopt,
312+
builder, state, argAttrs, /*resultAttrs=*/{},
313313
getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
314314
}
315315

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
601601
return;
602602
assert(type.getNumInputs() == argAttrs.size());
603603
call_interface_impl::addArgAndResultAttrs(
604-
builder, state, argAttrs, /*resultAttrs=*/std::nullopt,
604+
builder, state, argAttrs, /*resultAttrs=*/{},
605605
getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
606606
}
607607

mlir/lib/Dialect/Func/IR/FuncOps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
193193
return;
194194
assert(type.getNumInputs() == argAttrs.size());
195195
call_interface_impl::addArgAndResultAttrs(
196-
builder, state, argAttrs, /*resultAttrs=*/std::nullopt,
196+
builder, state, argAttrs, /*resultAttrs=*/{},
197197
getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
198198
}
199199

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2774,7 +2774,7 @@ void LLVMFuncOp::build(OpBuilder &builder, OperationState &result,
27742774
assert(llvm::cast<LLVMFunctionType>(type).getNumParams() == argAttrs.size() &&
27752775
"expected as many argument attribute lists as arguments");
27762776
call_interface_impl::addArgAndResultAttrs(
2777-
builder, result, argAttrs, /*resultAttrs=*/std::nullopt,
2777+
builder, result, argAttrs, /*resultAttrs=*/{},
27782778
getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
27792779
}
27802780

mlir/lib/Dialect/Shape/IR/Shape.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
13031303
return;
13041304
assert(type.getNumInputs() == argAttrs.size());
13051305
call_interface_impl::addArgAndResultAttrs(
1306-
builder, state, argAttrs, /*resultAttrs=*/std::nullopt,
1306+
builder, state, argAttrs, /*resultAttrs=*/{},
13071307
getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
13081308
}
13091309

mlir/lib/Reducer/Tester.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Tester::Interestingness Tester::isInteresting(StringRef testCase) const {
6868

6969
std::string errMsg;
7070
int result = llvm::sys::ExecuteAndWait(
71-
testScript, testerArgs, /*Env=*/std::nullopt, /*Redirects=*/std::nullopt,
71+
testScript, testerArgs, /*Env=*/std::nullopt, /*Redirects=*/{},
7272
/*SecondsToWait=*/0, /*MemoryLimit=*/0, &errMsg);
7373

7474
if (result < 0)

mlir/lib/Target/LLVMIR/LoopAnnotationTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void LoopAnnotationConversion::convertLocation(FusedLoc location) {
206206

207207
llvm::MDNode *LoopAnnotationConversion::convert() {
208208
// Reserve operand 0 for loop id self reference.
209-
auto dummy = llvm::MDNode::getTemporary(ctx, std::nullopt);
209+
auto dummy = llvm::MDNode::getTemporary(ctx, {});
210210
metadataNodes.push_back(dummy.get());
211211

212212
if (FusedLoc startLoc = attr.getStartLoc())

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ ModuleTranslation::getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr) {
19221922
if (!scopeInserted)
19231923
return scopeIt->second;
19241924
llvm::LLVMContext &ctx = llvmModule->getContext();
1925-
auto dummy = llvm::MDNode::getTemporary(ctx, std::nullopt);
1925+
auto dummy = llvm::MDNode::getTemporary(ctx, {});
19261926
// Convert the domain metadata node if necessary.
19271927
auto [domainIt, insertedDomain] = aliasDomainMetadataMapping.try_emplace(
19281928
aliasScopeAttr.getDomain(), nullptr);

mlir/lib/Tools/PDLL/Parser/Parser.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ ast::Decl *Parser::createODSNativePDLLConstraintDecl(
985985
// Build the native constraint.
986986
auto *constraintDecl = ast::UserConstraintDecl::createNative(
987987
ctx, ast::Name::create(ctx, name, loc), paramVar,
988-
/*results=*/std::nullopt, codeBlock, ast::TupleType::get(ctx),
989-
nativeType);
988+
/*results=*/{}, codeBlock, ast::TupleType::get(ctx), nativeType);
990989
constraintDecl->setDocComment(ctx, docString);
991990
curDeclScope->add(constraintDecl);
992991
return constraintDecl;
@@ -1782,7 +1781,7 @@ Parser::parseConstraint(std::optional<SMRange> &typeConstraint,
17821781

17831782
FailureOr<ast::ConstraintRef> Parser::parseArgOrResultConstraint() {
17841783
std::optional<SMRange> typeConstraint;
1785-
return parseConstraint(typeConstraint, /*existingConstraints=*/std::nullopt,
1784+
return parseConstraint(typeConstraint, /*existingConstraints=*/{},
17861785
/*allowInlineTypeConstraints=*/false);
17871786
}
17881787

@@ -2995,8 +2994,8 @@ LogicalResult Parser::validateOperationOperandsOrResults(
29952994
// Otherwise, create dummy values for each of the entries so that we
29962995
// adhere to the ODS signature.
29972996
for (unsigned i = 0, e = odsValues.size(); i < e; ++i) {
2998-
values.push_back(ast::RangeExpr::create(
2999-
ctx, loc, /*elements=*/std::nullopt, rangeTy));
2997+
values.push_back(
2998+
ast::RangeExpr::create(ctx, loc, /*elements=*/{}, rangeTy));
30002999
}
30013000
return success();
30023001
}

mlir/unittests/Debug/FileLineColLocBreakpointManagerTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ static Operation *createOp(MLIRContext *context, Location loc,
2424
unsigned int numRegions = 0) {
2525
context->allowUnregisteredDialects();
2626
return Operation::create(loc, OperationName(operationName, context), {}, {},
27-
std::nullopt, OpaqueProperties(nullptr),
28-
std::nullopt, numRegions);
27+
std::nullopt, OpaqueProperties(nullptr), {},
28+
numRegions);
2929
}
3030

3131
namespace {

mlir/unittests/IR/OperationSupportTest.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ static Operation *createOp(MLIRContext *context, ArrayRef<Value> operands = {},
2222
ArrayRef<Type> resultTypes = {},
2323
unsigned int numRegions = 0) {
2424
context->allowUnregisteredDialects();
25-
return Operation::create(
26-
UnknownLoc::get(context), OperationName("foo.bar", context), resultTypes,
27-
operands, std::nullopt, nullptr, std::nullopt, numRegions);
25+
return Operation::create(UnknownLoc::get(context),
26+
OperationName("foo.bar", context), resultTypes,
27+
operands, std::nullopt, nullptr, {}, numRegions);
2828
}
2929

3030
namespace {
@@ -33,7 +33,7 @@ TEST(OperandStorageTest, NonResizable) {
3333
Builder builder(&context);
3434

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

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

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

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

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

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

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

127127
// Create a resizable operation with one operand.
@@ -158,8 +158,7 @@ TEST(OperandStorageTest, RangeErase) {
158158
Builder builder(&context);
159159

160160
Type type = builder.getNoneType();
161-
Operation *useOp =
162-
createOp(&context, /*operands=*/std::nullopt, {type, type});
161+
Operation *useOp = createOp(&context, /*operands=*/{}, {type, type});
163162
Value operand1 = useOp->getResult(0);
164163
Value operand2 = useOp->getResult(1);
165164

@@ -189,8 +188,8 @@ TEST(OperationOrderTest, OrderIsAlwaysValid) {
189188
MLIRContext context;
190189
Builder builder(&context);
191190

192-
Operation *containerOp = createOp(&context, /*operands=*/std::nullopt,
193-
/*resultTypes=*/std::nullopt,
191+
Operation *containerOp = createOp(&context, /*operands=*/{},
192+
/*resultTypes=*/{},
194193
/*numRegions=*/1);
195194
Region &region = containerOp->getRegion(0);
196195
Block *block = new Block();
@@ -237,7 +236,7 @@ TEST(OperationFormatPrintTest, CanPrintNameAsPrefix) {
237236
Operation *op = Operation::create(
238237
NameLoc::get(StringAttr::get(&context, "my_named_loc")),
239238
OperationName("t.op", &context), builder.getIntegerType(16), {},
240-
std::nullopt, nullptr, std::nullopt, 0);
239+
std::nullopt, nullptr, {}, 0);
241240

242241
std::string str;
243242
OpPrintingFlags flags;

mlir/unittests/IR/ValueTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ static Operation *createOp(MLIRContext *context, ArrayRef<Value> operands = {},
2020
ArrayRef<Type> resultTypes = {},
2121
unsigned int numRegions = 0) {
2222
context->allowUnregisteredDialects();
23-
return Operation::create(
24-
UnknownLoc::get(context), OperationName("foo.bar", context), resultTypes,
25-
operands, std::nullopt, nullptr, std::nullopt, numRegions);
23+
return Operation::create(UnknownLoc::get(context),
24+
OperationName("foo.bar", context), resultTypes,
25+
operands, std::nullopt, nullptr, {}, numRegions);
2626
}
2727

2828
namespace {
@@ -32,7 +32,7 @@ TEST(ValueTest, getNumUses) {
3232
Builder builder(&context);
3333

3434
Operation *op0 =
35-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
35+
createOp(&context, /*operands=*/{}, builder.getIntegerType(16));
3636

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

5555
Operation *op0 =
56-
createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16));
56+
createOp(&context, /*operands=*/{}, builder.getIntegerType(16));
5757
Value v0 = op0->getResult(0);
5858
EXPECT_TRUE(v0.hasNUses(0));
5959
EXPECT_FALSE(v0.hasNUses(1));
@@ -77,7 +77,7 @@ TEST(ValueTest, hasNUsesOrMore) {
7777
Builder builder(&context);
7878

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

mlir/unittests/Transforms/DialectConversion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ using namespace mlir;
1313

1414
static Operation *createOp(MLIRContext *context) {
1515
context->allowUnregisteredDialects();
16-
return Operation::create(
17-
UnknownLoc::get(context), OperationName("foo.bar", context), {}, {},
18-
std::nullopt, /*properties=*/nullptr, std::nullopt, 0);
16+
return Operation::create(UnknownLoc::get(context),
17+
OperationName("foo.bar", context), {}, {},
18+
std::nullopt, /*properties=*/nullptr, {}, 0);
1919
}
2020

2121
namespace {

0 commit comments

Comments
 (0)