[llvm] [PassSupport] Simplify callDefaultCtor (NFC) (PR #137504)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 27 00:40:09 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/137504

We can use "constexpr if" to combine the two variants of functions.


>From c81e749084775342c8f48cc842173de8cf83f44d Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 27 Apr 2025 00:27:49 -0700
Subject: [PATCH] [PassSupport] Simplify callDefaultCtor (NFC)

We can use "constexpr if" to combine the two variants of functions.
---
 llvm/include/llvm/PassSupport.h | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/PassSupport.h b/llvm/include/llvm/PassSupport.h
index 2806d9b52b0b9..b0897a6be37d1 100644
--- a/llvm/include/llvm/PassSupport.h
+++ b/llvm/include/llvm/PassSupport.h
@@ -64,20 +64,13 @@ class Pass;
   INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis)       \
   INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
 
-template <
-    class PassName,
-    std::enable_if_t<std::is_default_constructible<PassName>{}, bool> = true>
-Pass *callDefaultCtor() {
-  return new PassName();
-}
-
-template <
-    class PassName,
-    std::enable_if_t<!std::is_default_constructible<PassName>{}, bool> = true>
-Pass *callDefaultCtor() {
-  // Some codegen passes should only be testable via
-  // `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`.
-  report_fatal_error("target-specific codegen-only pass");
+template <class PassName> Pass *callDefaultCtor() {
+  if constexpr (std::is_default_constructible_v<PassName>)
+    return new PassName();
+  else
+    // Some codegen passes should only be testable via
+    // `llc -{start|stop}-{before|after}=<passname>`, not via `opt -<passname>`.
+    report_fatal_error("target-specific codegen-only pass");
 }
 
 //===---------------------------------------------------------------------------



More information about the llvm-commits mailing list