-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Simplify functions with "constexpr if" (NFC) #137551
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_001_constexpr_if_mlir
Apr 27, 2025
Merged
[mlir] Simplify functions with "constexpr if" (NFC) #137551
kazutakahirata
merged 1 commit into
llvm:main
from
kazutakahirata:cleanup_001_constexpr_if_mlir
Apr 27, 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
We can use "constexpr if" to combine the two variants of functions.
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Kazu Hirata (kazutakahirata) ChangesWe can use "constexpr if" to combine the two variants of functions. Full diff: https://github.com/llvm/llvm-project/pull/137551.diff 3 Files Affected:
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 237d48756c749..31f54413a5ff0 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -1631,14 +1631,11 @@ using detect_has_verify_region_trait =
/// Verify the given trait if it provides a verifier.
template <typename T>
-std::enable_if_t<detect_has_verify_trait<T>::value, LogicalResult>
-verifyTrait(Operation *op) {
- return T::verifyTrait(op);
-}
-template <typename T>
-inline std::enable_if_t<!detect_has_verify_trait<T>::value, LogicalResult>
-verifyTrait(Operation *) {
- return success();
+LogicalResult verifyTrait(Operation *op) {
+ if constexpr (detect_has_verify_trait<T>::value)
+ return T::verifyTrait(op);
+ else
+ return success();
}
/// Given a set of traits, return the result of verifying the given operation.
@@ -1649,15 +1646,11 @@ LogicalResult verifyTraits(Operation *op) {
/// Verify the given trait if it provides a region verifier.
template <typename T>
-std::enable_if_t<detect_has_verify_region_trait<T>::value, LogicalResult>
-verifyRegionTrait(Operation *op) {
- return T::verifyRegionTrait(op);
-}
-template <typename T>
-inline std::enable_if_t<!detect_has_verify_region_trait<T>::value,
- LogicalResult>
-verifyRegionTrait(Operation *) {
- return success();
+LogicalResult verifyRegionTrait(Operation *op) {
+ if constexpr (detect_has_verify_region_trait<T>::value)
+ return T::verifyRegionTrait(op);
+ else
+ return success();
}
/// Given a set of traits, return the result of verifying the regions of the
diff --git a/mlir/include/mlir/IR/PatternMatch.h b/mlir/include/mlir/IR/PatternMatch.h
index fc6ae8fb55fec..10cfe851765dc 100644
--- a/mlir/include/mlir/IR/PatternMatch.h
+++ b/mlir/include/mlir/IR/PatternMatch.h
@@ -273,17 +273,13 @@ class RewritePattern : public Pattern {
template <typename T>
using detect_has_initialize = llvm::is_detected<has_initialize, T>;
- /// Initialize the derived pattern by calling its `initialize` method.
+ /// Initialize the derived pattern by calling its `initialize` method if
+ /// available.
template <typename T>
- static std::enable_if_t<detect_has_initialize<T>::value>
- initializePattern(T &pattern) {
- pattern.initialize();
+ static void initializePattern(T &pattern) {
+ if constexpr (detect_has_initialize<T>::value)
+ pattern.initialize();
}
- /// Empty derived pattern initializer for patterns that do not have an
- /// initialize method.
- template <typename T>
- static std::enable_if_t<!detect_has_initialize<T>::value>
- initializePattern(T &) {}
/// An anchor for the virtual table.
virtual void anchor();
diff --git a/mlir/include/mlir/Pass/PassOptions.h b/mlir/include/mlir/Pass/PassOptions.h
index 68588279e2f5a..e1f16c6158ad5 100644
--- a/mlir/include/mlir/Pass/PassOptions.h
+++ b/mlir/include/mlir/Pass/PassOptions.h
@@ -75,16 +75,13 @@ static void printOptionValue(raw_ostream &os, const std::string &str) {
os << "}";
}
template <typename ParserT, typename DataT>
-static std::enable_if_t<has_stream_operator<DataT>::value>
-printOptionValue(raw_ostream &os, const DataT &value) {
- os << value;
-}
-template <typename ParserT, typename DataT>
-static std::enable_if_t<!has_stream_operator<DataT>::value>
-printOptionValue(raw_ostream &os, const DataT &value) {
- // If the value can't be streamed, fallback to checking for a print in the
- // parser.
- ParserT::print(os, value);
+static void printOptionValue(raw_ostream &os, const DataT &value) {
+ if constexpr (has_stream_operator<DataT>::value)
+ os << value;
+ else
+ // If the value can't be streamed, fallback to checking for a print in the
+ // parser.
+ ParserT::print(os, value);
}
} // namespace pass_options
|
kuhar
approved these changes
Apr 27, 2025
jyli0116
pushed a commit
to jyli0116/llvm-project
that referenced
this pull request
Apr 28, 2025
We can use "constexpr if" to combine the two variants of functions.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We can use "constexpr if" to combine the two variants of functions.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We can use "constexpr if" to combine the two variants of functions.
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
We can use "constexpr if" to combine the two variants of functions.
Ankur-0429
pushed a commit
to Ankur-0429/llvm-project
that referenced
this pull request
May 9, 2025
We can use "constexpr if" to combine the two variants of functions.
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.
We can use "constexpr if" to combine the two variants of functions.