From 1bea0d0aa5936cb36b6f86f721ca03c1a1bb601d Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Fri, 20 Nov 2020 12:28:34 +0100
Subject: c++: Add __builtin_clear_padding builtin - C++20 P0528R3 compiler
 side [PR88101]

The following patch implements __builtin_clear_padding builtin that clears
the padding bits in object representation (but preserves value
representation).  Inside of unions it clears only those padding bits that
are padding for all the union members (so that it never alters value
representation).

It handles trailing padding, padding in the middle of structs including
bitfields (PDP11 unhandled, I've never figured out how those bitfields
work), VLAs (doesn't handle variable length structures, but I think almost
nobody uses them and it isn't worth the extra complexity).  For VLAs and
sufficiently large arrays it uses runtime clearing loop instead of emitting
straight-line code (unless arrays are inside of a union).

The way I think this can be used for atomics is e.g. if the structures
are power of two sized and small enough that we use the hw atomics
for say compare_exchange __builtin_clear_padding could be called first on
the address of expected and desired arguments (for desired only if we want
to ensure that most of the time the atomic memory will have padding bits
cleared), then perform the weak cmpxchg and if that fails, we got the
value from the atomic memory; we can call __builtin_clear_padding on a copy
of that and then compare it with expected, and if it is the same with the
padding bits masked off, we can use the original with whatever random
padding bits in it as the new expected for next cmpxchg.
__builtin_clear_padding itself is not atomic and therefore it shouldn't
be called on the atomic memory itself, but compare_exchange*'s expected
argument is a reference and normally the implementation may store there
the current value from memory, so padding bits can be cleared in that,
and desired is passed by value rather than reference, so clearing is fine
too.
When using libatomic, we can use it either that way, or add new libatomic
APIs that accept another argument, pointer to the padding bit bitmask,
and construct that in the template as
  alignas (_T) unsigned char _mask[sizeof (_T)];
  std::memset (_mask, ~0, sizeof (_mask));
  __builtin_clear_padding ((_T *) _mask);
which will have bits cleared for padding bits and set for bits taking part
in the value representation.  Then libatomic could internally instead
of using memcmp compare
for (i = 0; i < N; i++) if ((val1[i] & mask[i]) != (val2[i] & mask[i]))

2020-11-20  Jakub Jelinek  <jakub@redhat.com>

	PR libstdc++/88101
gcc/
	* builtins.def (BUILT_IN_CLEAR_PADDING): New built-in function.
	* gimplify.c (gimplify_call_expr): Rewrite single argument
	BUILT_IN_CLEAR_PADDING into two-argument variant.
	* gimple-fold.c (clear_padding_unit, clear_padding_buf_size): New
	const variables.
	(struct clear_padding_struct): New type.
	(clear_padding_flush, clear_padding_add_padding,
	clear_padding_emit_loop, clear_padding_type,
	clear_padding_union, clear_padding_real_needs_padding_p,
	clear_padding_type_may_have_padding_p,
	gimple_fold_builtin_clear_padding): New functions.
	(gimple_fold_builtin): Handle BUILT_IN_CLEAR_PADDING.
	* doc/extend.texi (__builtin_clear_padding): Document.
gcc/c-family/
	* c-common.c (check_builtin_function_arguments): Handle
	BUILT_IN_CLEAR_PADDING.
gcc/testsuite/
	* c-c++-common/builtin-clear-padding-1.c: New test.
	* c-c++-common/torture/builtin-clear-padding-1.c: New test.
	* c-c++-common/torture/builtin-clear-padding-2.c: New test.
	* c-c++-common/torture/builtin-clear-padding-3.c: New test.
	* c-c++-common/torture/builtin-clear-padding-4.c: New test.
	* c-c++-common/torture/builtin-clear-padding-5.c: New test.
	* g++.dg/torture/builtin-clear-padding-1.C: New test.
	* g++.dg/torture/builtin-clear-padding-2.C: New test.
	* gcc.dg/builtin-clear-padding-1.c: New test.
---
 gcc/builtins.def | 1 +
 1 file changed, 1 insertion(+)

(limited to 'gcc/builtins.def')

diff --git a/gcc/builtins.def b/gcc/builtins.def
index b4494c7..04dbe79 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -839,6 +839,7 @@ DEF_EXT_LIB_BUILTIN    (BUILT_IN_CLEAR_CACHE, "__clear_cache", BT_FN_VOID_PTR_PT
 /* [trans-mem]: Adjust BUILT_IN_TM_CALLOC if BUILT_IN_CALLOC is changed.  */
 DEF_LIB_BUILTIN        (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_WARN_UNUSED_RESULT_SIZE_1_2_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLASSIFY_TYPE, "classify_type", BT_FN_INT_VAR, ATTR_LEAF_LIST)
+DEF_GCC_BUILTIN        (BUILT_IN_CLEAR_PADDING, "clear_padding", BT_FN_VOID_VAR, ATTR_NOTHROW_NONNULL_TYPEGENERIC_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZ, "clz", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZIMAX, "clzimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_GCC_BUILTIN        (BUILT_IN_CLZL, "clzl", BT_FN_INT_ULONG, ATTR_CONST_NOTHROW_LEAF_LIST)
-- 
cgit v1.1

