-
Notifications
You must be signed in to change notification settings - Fork 14k
[mlir] Compare std::optional<T> to values directly (NFC) #144241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_20250614_opt_mlir
Jun 15, 2025
Merged
[mlir] Compare std::optional<T> to values directly (NFC) #144241
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_20250614_opt_mlir
Jun 15, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This patch transforms: X && *X == Y to: X == Y where X is of std::optional<T>, and Y is of T or similar.
@llvm/pr-subscribers-mlir-spirv @llvm/pr-subscribers-mlir-linalg Author: Kazu Hirata (kazutakahirata) ChangesThis patch transforms: X && *X == Y to: X == Y where X is of std::optional<T>, and Y is of T or similar. Full diff: https://github.com/llvm/llvm-project/pull/144241.diff 7 Files Affected:
diff --git a/mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.cpp b/mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.cpp
index dab15d23f6e0f..ac8ed4fdff7c3 100644
--- a/mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.cpp
+++ b/mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.cpp
@@ -173,7 +173,7 @@ getTreePredicates(std::vector<PositionalPredicate> &predList, Value val,
// Ignore the specified operand, usually because this position was
// visited in an upward traversal via an iterative choice.
- if (ignoreOperand && *ignoreOperand == operandIt.index())
+ if (ignoreOperand == operandIt.index())
continue;
Position *pos =
diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 8a708eb29210c..3d09c6a9b2c24 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -2367,7 +2367,7 @@ struct AffineForEmptyLoopFolder : public OpRewritePattern<AffineForOp> {
if (forOp.getNumResults() == 0)
return success();
std::optional<uint64_t> tripCount = getTrivialConstantTripCount(forOp);
- if (tripCount && *tripCount == 0) {
+ if (tripCount == 0) {
// The initial values of the iteration arguments would be the op's
// results.
rewriter.replaceOp(forOp, forOp.getInits());
@@ -2447,7 +2447,7 @@ void AffineForOp::getSuccessorRegions(
// From the loop body, if the trip count is one, we can only branch back to
// the parent.
- if (!point.isParent() && tripCount && *tripCount == 1) {
+ if (!point.isParent() && tripCount == 1) {
regions.push_back(RegionSuccessor(getResults()));
return;
}
@@ -2460,8 +2460,7 @@ void AffineForOp::getSuccessorRegions(
/// Returns true if the affine.for has zero iterations in trivial cases.
static bool hasTrivialZeroTripCount(AffineForOp op) {
- std::optional<uint64_t> tripCount = getTrivialConstantTripCount(op);
- return tripCount && *tripCount == 0;
+ return getTrivialConstantTripCount(op) == 0;
}
LogicalResult AffineForOp::fold(FoldAdaptor adaptor,
@@ -4789,7 +4788,7 @@ struct DropUnitExtentBasis
llvm::enumerate(delinearizeOp.getPaddedBasis())) {
std::optional<int64_t> basisVal =
basis ? getConstantIntValue(basis) : std::nullopt;
- if (basisVal && *basisVal == 1)
+ if (basisVal == 1)
replacements[index] = getZero();
else
newBasis.push_back(basis);
diff --git a/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp b/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
index 0d4ba3940c48e..4aa1fe318efa8 100644
--- a/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
+++ b/mlir/lib/Dialect/Affine/Utils/LoopUtils.cpp
@@ -1015,8 +1015,7 @@ LogicalResult mlir::affine::loopUnrollByFactor(
std::optional<uint64_t> mayBeConstantTripCount = getConstantTripCount(forOp);
if (unrollFactor == 1) {
- if (mayBeConstantTripCount && *mayBeConstantTripCount == 1 &&
- failed(promoteIfSingleIteration(forOp)))
+ if (mayBeConstantTripCount == 1 && failed(promoteIfSingleIteration(forOp)))
return failure();
return success();
}
@@ -1103,8 +1102,7 @@ LogicalResult mlir::affine::loopUnrollJamByFactor(AffineForOp forOp,
std::optional<uint64_t> mayBeConstantTripCount = getConstantTripCount(forOp);
if (unrollJamFactor == 1) {
- if (mayBeConstantTripCount && *mayBeConstantTripCount == 1 &&
- failed(promoteIfSingleIteration(forOp)))
+ if (mayBeConstantTripCount == 1 && failed(promoteIfSingleIteration(forOp)))
return failure();
return success();
}
diff --git a/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp b/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
index bd4ffabfbb929..5e6dde36d7f9f 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
@@ -606,8 +606,7 @@ struct DropPadUnitDims : public OpRewritePattern<tensor::PadOp> {
int64_t padRank = sourceShape.size();
auto isStaticZero = [](OpFoldResult f) {
- std::optional<int64_t> maybeInt = getConstantIntValue(f);
- return maybeInt && *maybeInt == 0;
+ return getConstantIntValue(f) == 0;
};
llvm::SmallDenseSet<unsigned> unitDimsFilter(allowedUnitDims.begin(),
diff --git a/mlir/lib/Dialect/Linalg/Utils/Utils.cpp b/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
index bae06c003fd97..2527d90cfa2e6 100644
--- a/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
@@ -688,7 +688,7 @@ computeSliceParameters(OpBuilder &builder, Location loc, Value valueToTile,
// tensors with "0" dimensions would never be constructed.
int64_t shapeSize = shape[r];
std::optional<int64_t> sizeCst = getConstantIntValue(size);
- auto hasTileSizeOne = sizeCst && *sizeCst == 1;
+ auto hasTileSizeOne = sizeCst == 1;
auto dividesEvenly = sizeCst && !ShapedType::isDynamic(shapeSize) &&
((shapeSize % *sizeCst) == 0);
if (!hasTileSizeOne && !dividesEvenly) {
diff --git a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
index f5a58c58e05df..1e7bb046d3752 100644
--- a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
+++ b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp
@@ -737,7 +737,7 @@ static spirv::GlobalVariableOp getBuiltinVariable(Block &body,
spirv::SPIRVDialect::getAttributeName(
spirv::Decoration::BuiltIn))) {
auto varBuiltIn = spirv::symbolizeBuiltIn(builtinAttr.getValue());
- if (varBuiltIn && *varBuiltIn == builtin) {
+ if (varBuiltIn == builtin) {
return varOp;
}
}
diff --git a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
index 29f7bd6857c27..8e3f796af54df 100644
--- a/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
+++ b/mlir/lib/Dialect/Utils/StaticValueUtils.cpp
@@ -142,8 +142,7 @@ getConstantIntValues(ArrayRef<OpFoldResult> ofrs) {
}
bool isConstantIntValue(OpFoldResult ofr, int64_t value) {
- auto val = getConstantIntValue(ofr);
- return val && *val == value;
+ return getConstantIntValue(ofr) == value;
}
bool areAllConstantIntValue(ArrayRef<OpFoldResult> ofrs, int64_t value) {
|
matthias-springer
approved these changes
Jun 15, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch transforms:
X && *X == Y
to:
X == Y
where X is of std::optional, and Y is of T or similar.