[mlir] Migrate away from std::nullopt (NFC) (#145523)

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.
This commit is contained in:
Kazu Hirata
2025-06-25 11:49:22 -07:00
committed by GitHub
parent d760f97387
commit 28f6f87061
5 changed files with 16 additions and 14 deletions

View File

@@ -991,7 +991,7 @@ void PDLToPDLInterpPass::runOnOperation() {
module.getLoc(), pdl_interp::PDLInterpDialect::getMatcherFunctionName(),
builder.getFunctionType(builder.getType<pdl::OperationType>(),
/*results=*/{}),
/*attrs=*/std::nullopt);
/*attrs=*/ArrayRef<NamedAttribute>());
// Create a nested module to hold the functions invoked for rewriting the IR
// after a successful match.

View File

@@ -310,8 +310,8 @@ static Value createLinalgBodyCalculationForElementwiseOp(
auto shifted =
rewriter.create<arith::ShRSIOp>(loc, resultTypes, args[0], subtract)
->getResults();
auto truncated =
rewriter.create<arith::TruncIOp>(loc, i1Ty, shifted, std::nullopt);
auto truncated = rewriter.create<arith::TruncIOp>(
loc, i1Ty, shifted, ArrayRef<NamedAttribute>());
auto isInputOdd =
rewriter.create<arith::AndIOp>(loc, i1Ty, truncated, i1one);
@@ -552,20 +552,20 @@ static Value createLinalgBodyCalculationForElementwiseOp(
if (isa<FloatType>(srcTy) && isa<FloatType>(dstTy) && bitExtend)
return rewriter.create<arith::ExtFOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
if (isa<FloatType>(srcTy) && isa<FloatType>(dstTy) && !bitExtend)
return rewriter.create<arith::TruncFOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
// 1-bit integers need to be treated as signless.
if (srcTy.isInteger(1) && arith::UIToFPOp::areCastCompatible(srcTy, dstTy))
return rewriter.create<arith::UIToFPOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
if (srcTy.isInteger(1) && isa<IntegerType>(dstTy) && bitExtend)
return rewriter.create<arith::ExtUIOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
// Unsigned integers need an unrealized cast so that they can be passed
// to UIToFP.
@@ -583,7 +583,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
// All other si-to-fp conversions should be handled by SIToFP.
if (arith::SIToFPOp::areCastCompatible(srcTy, dstTy))
return rewriter.create<arith::SIToFPOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
// Casting to boolean, floats need to only be checked as not-equal to zero.
if (isa<FloatType>(srcTy) && dstTy.isInteger(1)) {
@@ -690,7 +690,7 @@ static Value createLinalgBodyCalculationForElementwiseOp(
if (isa<IntegerType>(srcTy) && isa<IntegerType>(dstTy) && bitExtend)
return rewriter.create<arith::ExtSIOp>(loc, resultTypes, args,
std::nullopt);
ArrayRef<NamedAttribute>());
if (isa<IntegerType>(srcTy) && isa<IntegerType>(dstTy) && !bitExtend) {
return rewriter.create<arith::TruncIOp>(loc, dstTy, args[0]);

View File

@@ -111,7 +111,8 @@ static Value getFlatMemref(OpBuilder &rewriter, Location loc, Value source,
getFlatOffsetAndStrides(rewriter, loc, source, offsetsTemp);
MemRefType retType = inferCastResultType(base, offset);
return rewriter.create<memref::ReinterpretCastOp>(loc, retType, base, offset,
std::nullopt, std::nullopt);
ArrayRef<OpFoldResult>(),
ArrayRef<OpFoldResult>());
}
static bool needFlatten(Value val) {

View File

@@ -350,8 +350,9 @@ Value CodeGen::genNonInitializerVar(const ast::VariableDecl *varDecl,
Value results = builder.create<pdl::TypesOp>(
loc, pdl::RangeType::get(builder.getType<pdl::TypeType>()),
/*types=*/ArrayAttr());
return builder.create<pdl::OperationOp>(
loc, opType.getName(), operands, std::nullopt, ValueRange(), results);
return builder.create<pdl::OperationOp>(loc, opType.getName(), operands,
ArrayRef<StringRef>(), ValueRange(),
results);
}
if (ast::RangeType rangeTy = dyn_cast<ast::RangeType>(type)) {

View File

@@ -1014,7 +1014,7 @@ struct TestPassthroughInvalidOp : public ConversionPattern {
.getResult());
}
rewriter.replaceOpWithNewOp<TestValidOp>(op, TypeRange(), flattened,
std::nullopt);
ArrayRef<NamedAttribute>());
return success();
}
};
@@ -1030,7 +1030,7 @@ struct TestDropAndReplaceInvalidOp : public ConversionPattern {
matchAndRewrite(Operation *op, ArrayRef<Value> operands,
ConversionPatternRewriter &rewriter) const final {
rewriter.replaceOpWithNewOp<TestValidOp>(op, TypeRange(), ValueRange(),
std::nullopt);
ArrayRef<NamedAttribute>());
return success();
}
};