Skip to content

[llvm] replace static_assert with std::enable_if_t in ilist_node_impl #127722

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
merged 4 commits into from
Mar 11, 2025

Conversation

andrurogerz
Copy link
Contributor

Purpose

Remove static_assert in ilist_node_impl::isSentinel and conditionally include the functino using std::enable_if_t instead.

Background

This fix is necessary to support building LLVM as a Windows DLL, tracked by #109483.

The static_assert in ilist_node_impl::isSentinel fails when compiling LLVM as a Windows DLL with the options -D LLVM_BUILD_LLVM_DYLIB=ON -D LLVM_BUILD_LLVM_DYLIB_VIS=ON -D LLVM_LINK_LLVM_DYLIB=ON:

S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(151): error C2338: static_assert failed: 'Use ilist_sentinel_tracking<true> to enable isSentinel()'
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(151): note: the template instantiation context (the oldest one first) is
S:\llvm\llvm-project\llvm\include\llvm/IR/SymbolTableListTraits.h(113): note: see reference to class template instantiation 'llvm::SymbolTableListTraits<llvm::Function>' being compiled
S:\llvm\llvm-project\llvm\include\llvm/IR/SymbolTableListTraits.h(69): note: see reference to class template instantiation 'llvm::simple_ilist<ValueSubClass>' being compiled
        with
        [
            ValueSubClass=llvm::Function
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/simple_ilist.h(87): note: see reference to class template instantiation 'llvm::ilist_sentinel<llvm::ilist_detail::node_options<T,true,false,llvm::ilist_detail::extract_tag<>::type,false,llvm::ilist_detail::extract_parent<>::type>>' being compiled
        with
        [
            T=llvm::Function
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(301): note: see reference to class template instantiation 'llvm::ilist_node_impl<OptionsT>' being compiled
        with
        [
            OptionsT=llvm::ilist_detail::node_options<llvm::Function,true,false,llvm::ilist_detail::extract_tag<>::type,false,llvm::ilist_detail::extract_parent<>::type>
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(150): note: while compiling class template member function 'bool llvm::ilist_node_impl<OptionsT>::isSentinel(void) const'
        with
        [
            OptionsT=llvm::ilist_detail::node_options<llvm::Function,true,false,llvm::ilist_detail::extract_tag<>::type,false,llvm::ilist_detail::extract_parent<>::type>
        ]

Conditionally including the function using std::enable_if_t has the same effect of preventing the function's use when is_sentinel_tracking_explicit=false, but avoids the issue when DLL-exporting downstream classes.

Validation

Verified I no longer fail compilation due to the static_assert when building LLVM on Windows 11 with the options -D LLVM_BUILD_LLVM_DYLIB=ON -D LLVM_BUILD_LLVM_DYLIB_VIS=ON -D LLVM_LINK_LLVM_DYLIB=ON.

@andrurogerz
Copy link
Contributor Author

+@compnerd, who helped me figure out this solution

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-llvm-adt

Author: Andrew Rogers (andrurogerz)

Changes

Purpose

Remove static_assert in ilist_node_impl::isSentinel and conditionally include the functino using std::enable_if_t instead.

Background

This fix is necessary to support building LLVM as a Windows DLL, tracked by #109483.

The static_assert in ilist_node_impl::isSentinel fails when compiling LLVM as a Windows DLL with the options -D LLVM_BUILD_LLVM_DYLIB=ON -D LLVM_BUILD_LLVM_DYLIB_VIS=ON -D LLVM_LINK_LLVM_DYLIB=ON:

S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(151): error C2338: static_assert failed: 'Use ilist_sentinel_tracking&lt;true&gt; to enable isSentinel()'
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(151): note: the template instantiation context (the oldest one first) is
S:\llvm\llvm-project\llvm\include\llvm/IR/SymbolTableListTraits.h(113): note: see reference to class template instantiation 'llvm::SymbolTableListTraits&lt;llvm::Function&gt;' being compiled
S:\llvm\llvm-project\llvm\include\llvm/IR/SymbolTableListTraits.h(69): note: see reference to class template instantiation 'llvm::simple_ilist&lt;ValueSubClass&gt;' being compiled
        with
        [
            ValueSubClass=llvm::Function
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/simple_ilist.h(87): note: see reference to class template instantiation 'llvm::ilist_sentinel&lt;llvm::ilist_detail::node_options&lt;T,true,false,llvm::ilist_detail::extract_tag&lt;&gt;::type,false,llvm::ilist_detail::extract_parent&lt;&gt;::type&gt;&gt;' being compiled
        with
        [
            T=llvm::Function
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(301): note: see reference to class template instantiation 'llvm::ilist_node_impl&lt;OptionsT&gt;' being compiled
        with
        [
            OptionsT=llvm::ilist_detail::node_options&lt;llvm::Function,true,false,llvm::ilist_detail::extract_tag&lt;&gt;::type,false,llvm::ilist_detail::extract_parent&lt;&gt;::type&gt;
        ]
S:\llvm\llvm-project\llvm\include\llvm/ADT/ilist_node.h(150): note: while compiling class template member function 'bool llvm::ilist_node_impl&lt;OptionsT&gt;::isSentinel(void) const'
        with
        [
            OptionsT=llvm::ilist_detail::node_options&lt;llvm::Function,true,false,llvm::ilist_detail::extract_tag&lt;&gt;::type,false,llvm::ilist_detail::extract_parent&lt;&gt;::type&gt;
        ]

Conditionally including the function using std::enable_if_t has the same effect of preventing the function's use when is_sentinel_tracking_explicit=false, but avoids the issue when DLL-exporting downstream classes.

Validation

Verified I no longer fail compilation due to the static_assert when building LLVM on Windows 11 with the options -D LLVM_BUILD_LLVM_DYLIB=ON -D LLVM_BUILD_LLVM_DYLIB_VIS=ON -D LLVM_LINK_LLVM_DYLIB=ON.


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

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/ilist_node.h (+3-2)
diff --git a/llvm/include/llvm/ADT/ilist_node.h b/llvm/include/llvm/ADT/ilist_node.h
index bfd50f8b3fb71..2148d6fde9be0 100644
--- a/llvm/include/llvm/ADT/ilist_node.h
+++ b/llvm/include/llvm/ADT/ilist_node.h
@@ -18,6 +18,8 @@
 #include "llvm/ADT/ilist_node_base.h"
 #include "llvm/ADT/ilist_node_options.h"
 
+#include <type_traits>
+
 namespace llvm {
 
 namespace ilist_detail {
@@ -147,9 +149,8 @@ class ilist_node_impl
   ///
   /// This requires sentinel tracking to be explicitly enabled.  Use the
   /// ilist_sentinel_tracking<true> option to get this API.
+  template <typename = std::enable_if_t<OptionsT::is_sentinel_tracking_explicit>>
   bool isSentinel() const {
-    static_assert(OptionsT::is_sentinel_tracking_explicit,
-                  "Use ilist_sentinel_tracking<true> to enable isSentinel()");
     return node_base_type::isSentinel();
   }
 };

@andrurogerz andrurogerz changed the title replace static_assert with std::enable_if_t [llvm] replace static_assert with std::enable_if_t Feb 18, 2025
@andrurogerz andrurogerz changed the title [llvm] replace static_assert with std::enable_if_t [llvm] replace static_assert with std::enable_if_t in ilist_node_impl Feb 18, 2025
Copy link

github-actions bot commented Feb 18, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@kuhar kuhar requested review from MaskRay and dwblaikie February 19, 2025 01:35
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.

@andrurogerz
Copy link
Contributor Author

Fixed by using std::is_enabled_t directly on the isSentinel return type.
Verified it compiles OK with clang on Fedora as well as msvc on Windows.

@andrurogerz
Copy link
Contributor Author

@kuhar any further requests/objections on this patch?

@kuhar
Copy link
Member

kuhar commented Feb 26, 2025

I think we should wait on #127850 (comment) given that the code works fine in all the configurations supported at the moment.

@compnerd
Copy link
Member

I think we should wait on #127850 (comment) given that the code works fine in all the configurations supported at the moment.

I'm not sure I see the reason for that. This is correcting a rather subtle design point in the code which has nothing to do other windows support. Can you help me understand why this is something that we should hold up?

@kuhar
Copy link
Member

kuhar commented Feb 26, 2025

This is correcting a rather subtle design point in the code

Can we write a test for this that exercises this without adding LLM_ABI annotations? I'm warry of introducing changes without tests that are supposed to fix something that seems to be working fine.

@compnerd
Copy link
Member

This is correcting a rather subtle design point in the code

Can we write a test for this that exercises this without adding LLM_ABI annotations? I'm warry of introducing changes without tests that are supposed to fix something that seems to be working fine.

I don't think that really makes sense. This is a bug that the compiler lets you get away with. We generally do not have tests for miscompilation from a host compiler.

@compnerd
Copy link
Member

Merging this on behalf of @andrurogerz

@compnerd compnerd merged commit 4f60f45 into llvm:main Mar 11, 2025
8 checks passed
@andrurogerz andrurogerz deleted the ilist_node_impl-static_assert branch March 12, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants