Skip to content

Conversation

kazutakahirata
Copy link
Contributor

@kazutakahirata kazutakahirata commented Sep 4, 2025

This patch makes shouldReverseIterate constexpr, allowing compile-time
evaluation at call sites.

This patch simplifies shouldReverseIterate with "if constexpr" while
switching to "constexpr bool", allowing compile-time evaluation at
call sites.
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

This patch simplifies shouldReverseIterate with "if constexpr" while
switching to "constexpr bool", allowing compile-time evaluation at
call sites.


Full diff: https://github.com/llvm/llvm-project/pull/156812.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/ReverseIteration.h (+5-7)
diff --git a/llvm/include/llvm/Support/ReverseIteration.h b/llvm/include/llvm/Support/ReverseIteration.h
index 9e9411856369e..456d423600d6e 100644
--- a/llvm/include/llvm/Support/ReverseIteration.h
+++ b/llvm/include/llvm/Support/ReverseIteration.h
@@ -6,13 +6,11 @@
 
 namespace llvm {
 
-template<class T = void *>
-bool shouldReverseIterate() {
-#if LLVM_ENABLE_REVERSE_ITERATION
-  return detail::IsPointerLike<T>::value;
-#else
-  return false;
-#endif
+template <class T = void *> constexpr bool shouldReverseIterate() {
+  if constexpr (LLVM_ENABLE_REVERSE_ITERATION)
+    return detail::IsPointerLike<T>::value;
+  else
+    return false;
 }
 
 } // namespace llvm

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a simplification? This is a macro, so I'm not sure what the benefit is. Is it for better IDE/lsp support?

@kazutakahirata
Copy link
Contributor Author

Is this really a simplification? This is a macro, so I'm not sure what the benefit is. Is it for better IDE/lsp support?

@kuhar Maybe simplification is a wrong word here. Making this function constexpr can help simplify its users. It can be used as in std::conditional_t for example. I'm planning to use the constexpr version of this function to further simplify DenseMapIterator.

@kuhar
Copy link
Member

kuhar commented Sep 4, 2025

You can make it contexpr while still keeping the macro. I'm not saying the new implementation with if constexpr is worse but that I don't know if it's better.

@kazutakahirata kazutakahirata changed the title [Support] Use "if constexpr" in shouldReverseIterate [Support] Make shouldReverseIterate constexpr Sep 4, 2025
@kazutakahirata
Copy link
Contributor Author

You can make it contexpr while still keeping the macro. I'm not saying the new implementation with if constexpr is worse but that I don't know if it's better.

@kuhar I now retain the macro and just make the return type constexpr.

@kazutakahirata kazutakahirata merged commit 3285251 into llvm:main Sep 4, 2025
9 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250903_ReverseIteration branch September 4, 2025 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants