Skip to content

[z/OS][libc++] Remove align_val_t dependency in small_buffer.h #114396

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 7 commits into from
Nov 7, 2024

Conversation

zibi2
Copy link
Contributor

@zibi2 zibi2 commented Oct 31, 2024

We need to guard 2 functions to avoid errors when small_buffer.h is included in the modules LIT tests. For example:

test-suite-install/include/c++/v1/__utility/small_buffer.h:69:81: error: use of undeclared identifier 'align_val_t'
# |    69 |       byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
# |       |                                                                                 ^

@zibi2 zibi2 self-assigned this Oct 31, 2024
@zibi2 zibi2 requested a review from a team as a code owner October 31, 2024 12:10
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2024

@llvm/pr-subscribers-libcxx

Author: Zibi Sarbinowski (zibi2)

Changes

We need to guard 2 functions to avoid errors when small_buffer.h is included in the modules LIT tests. For example:

test-suite-install/include/c++/v1/__utility/small_buffer.h:69:81: error: use of undeclared identifier 'align_val_t'
# |    69 |       byte* __allocation = static_cast&lt;byte*&gt;(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
# |       |                                                                                 ^

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

1 Files Affected:

  • (modified) libcxx/include/__utility/small_buffer.h (+4)
diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index 70e068f89f62ed..6071bda4513657 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -61,6 +61,7 @@ class __small_buffer {
       return *std::launder(reinterpret_cast<_Stored**>(__buffer_));
   }
 
+#  ifdef _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI _Stored* __alloc() {
     if constexpr (__fits_in_buffer<_Stored>) {
@@ -72,11 +73,14 @@ class __small_buffer {
     }
   }
 
+#  endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#  ifdef _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
   template <class _Stored>
   _LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
     if constexpr (!__fits_in_buffer<_Stored>)
       ::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
   }
+#  endif // _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
 
   template <class _Stored, class... _Args>
   _LIBCPP_HIDE_FROM_ABI void __construct(_Args&&... __args) {

Copy link
Contributor

@abhina-sree abhina-sree left a comment

Choose a reason for hiding this comment

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

LGTM

@zibi2 zibi2 changed the title [z/OS][libc++] Guard to be used only when they are available [z/OS][libc++] Guard to be used only when align_val_t is available Oct 31, 2024
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a comment

Choose a reason for hiding this comment

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

Currently this approach just disables dynamic allocation when aligned allocation is not available in such a case. I guess we should manually align the buffer ptrs instead.

@zibi2
Copy link
Contributor Author

zibi2 commented Oct 31, 2024

Thank you for the comments, Louis please check if I addressed your suggestions correctly.

Copy link

github-actions bot commented Oct 31, 2024

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

@@ -75,7 +75,7 @@ class __small_buffer {
template <class _Stored>
_LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
if constexpr (!__fits_in_buffer<_Stored>)
::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
std::__libcpp_deallocate((void*)__buffer_, _BufferSize * sizeof(byte), alignof(_Stored));
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect, since you're deallocating the buffer itself (which is a member variable), not the pointer contained in the buffer. The previous reinterpret_cast must be kept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for caching it.

@zibi2 zibi2 requested review from philnik777 and ldionne November 6, 2024 20:10
@@ -75,7 +75,7 @@ class __small_buffer {
template <class _Stored>
_LIBCPP_HIDE_FROM_ABI void __dealloc() noexcept {
if constexpr (!__fits_in_buffer<_Stored>)
::operator delete[](*reinterpret_cast<void**>(__buffer_), sizeof(_Stored), align_val_t{alignof(_Stored)});
std::__libcpp_deallocate(*reinterpret_cast<void**>(__buffer_), _BufferSize * sizeof(byte), alignof(_Stored));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you change the size calculation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing out this. The good part is that this issue was also caught by stage3 sanitizer failure.

@ldionne
Copy link
Member

ldionne commented Nov 7, 2024

Let's merge once CI is green.

@zibi2 zibi2 changed the title [z/OS][libc++] Guard to be used only when align_val_t is available [z/OS][libc++] Remove align_val_t dependency in small_buffer.h Nov 7, 2024
@zibi2 zibi2 merged commit ef73533 into llvm:main Nov 7, 2024
36 of 37 checks passed
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…m#114396)

Rewriting `__alloc()` and `__dealloc()` template functions to avoid errors when `small_buffer.h` is
included in the modules LIT tests. For example:

```
test-suite-install/include/c++/v1/__utility/small_buffer.h:69:81: error: use of undeclared identifier 'align_val_t'
# |    69 |       byte* __allocation = static_cast<byte*>(::operator new[](sizeof(_Stored), align_val_t{alignof(_Stored)}));
# |       |                                                                                 ^
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants