Skip to content

Commit 25b05e0

Browse files
[mlir] Simplify functions with "constexpr if" (NFC) (#137551)
We can use "constexpr if" to combine the two variants of functions.
1 parent 992e928 commit 25b05e0

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,14 +1631,11 @@ using detect_has_verify_region_trait =
16311631

16321632
/// Verify the given trait if it provides a verifier.
16331633
template <typename T>
1634-
std::enable_if_t<detect_has_verify_trait<T>::value, LogicalResult>
1635-
verifyTrait(Operation *op) {
1636-
return T::verifyTrait(op);
1637-
}
1638-
template <typename T>
1639-
inline std::enable_if_t<!detect_has_verify_trait<T>::value, LogicalResult>
1640-
verifyTrait(Operation *) {
1641-
return success();
1634+
LogicalResult verifyTrait(Operation *op) {
1635+
if constexpr (detect_has_verify_trait<T>::value)
1636+
return T::verifyTrait(op);
1637+
else
1638+
return success();
16421639
}
16431640

16441641
/// Given a set of traits, return the result of verifying the given operation.
@@ -1649,15 +1646,11 @@ LogicalResult verifyTraits(Operation *op) {
16491646

16501647
/// Verify the given trait if it provides a region verifier.
16511648
template <typename T>
1652-
std::enable_if_t<detect_has_verify_region_trait<T>::value, LogicalResult>
1653-
verifyRegionTrait(Operation *op) {
1654-
return T::verifyRegionTrait(op);
1655-
}
1656-
template <typename T>
1657-
inline std::enable_if_t<!detect_has_verify_region_trait<T>::value,
1658-
LogicalResult>
1659-
verifyRegionTrait(Operation *) {
1660-
return success();
1649+
LogicalResult verifyRegionTrait(Operation *op) {
1650+
if constexpr (detect_has_verify_region_trait<T>::value)
1651+
return T::verifyRegionTrait(op);
1652+
else
1653+
return success();
16611654
}
16621655

16631656
/// Given a set of traits, return the result of verifying the regions of the

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,13 @@ class RewritePattern : public Pattern {
273273
template <typename T>
274274
using detect_has_initialize = llvm::is_detected<has_initialize, T>;
275275

276-
/// Initialize the derived pattern by calling its `initialize` method.
276+
/// Initialize the derived pattern by calling its `initialize` method if
277+
/// available.
277278
template <typename T>
278-
static std::enable_if_t<detect_has_initialize<T>::value>
279-
initializePattern(T &pattern) {
280-
pattern.initialize();
279+
static void initializePattern(T &pattern) {
280+
if constexpr (detect_has_initialize<T>::value)
281+
pattern.initialize();
281282
}
282-
/// Empty derived pattern initializer for patterns that do not have an
283-
/// initialize method.
284-
template <typename T>
285-
static std::enable_if_t<!detect_has_initialize<T>::value>
286-
initializePattern(T &) {}
287283

288284
/// An anchor for the virtual table.
289285
virtual void anchor();

mlir/include/mlir/Pass/PassOptions.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,13 @@ static void printOptionValue(raw_ostream &os, const std::string &str) {
7575
os << "}";
7676
}
7777
template <typename ParserT, typename DataT>
78-
static std::enable_if_t<has_stream_operator<DataT>::value>
79-
printOptionValue(raw_ostream &os, const DataT &value) {
80-
os << value;
81-
}
82-
template <typename ParserT, typename DataT>
83-
static std::enable_if_t<!has_stream_operator<DataT>::value>
84-
printOptionValue(raw_ostream &os, const DataT &value) {
85-
// If the value can't be streamed, fallback to checking for a print in the
86-
// parser.
87-
ParserT::print(os, value);
78+
static void printOptionValue(raw_ostream &os, const DataT &value) {
79+
if constexpr (has_stream_operator<DataT>::value)
80+
os << value;
81+
else
82+
// If the value can't be streamed, fallback to checking for a print in the
83+
// parser.
84+
ParserT::print(os, value);
8885
}
8986
} // namespace pass_options
9087

0 commit comments

Comments
 (0)