From patchwork Mon Jul 22 17:43:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1963357 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=lIFPqYiD; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WSSLw5Qjdz1ybY for ; Tue, 23 Jul 2024 03:43:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 728CE385DDEE for ; Mon, 22 Jul 2024 17:43:50 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb2d.google.com (mail-yb1-xb2d.google.com [IPv6:2607:f8b0:4864:20::b2d]) by sourceware.org (Postfix) with ESMTPS id 56E0C3858C52 for ; Mon, 22 Jul 2024 17:43:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 56E0C3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 56E0C3858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b2d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721670215; cv=none; b=IT7bw5ICNUdZZy+Dy9Q2JcwIUW5gjNJ6cm1mMENmkDGUszOoSOGbfo3YFuQufCqhBqCaTaq3414/1rozkJT0WN2yRn9Kzz5LhrTPpmmuKmEX/G/uNcAK9Qk/Zoz7qD4JlNs08l61znhZtRjrZ+OVCJnn03EJLq7UodliB78cgZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721670215; c=relaxed/simple; bh=S+lTSe646hoA9/jlLdm0BE1yk7t7BN9MmyTZRjPkxk4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LcMd5PwHLp8Z4sPsxICY3eS5RfN4F0j8gRIlR53I2IW3IfXq9pjGtZNryMMV8bry6bqXcmiaD4u4A0hp283Uy70hCqbquFIYml2Xh+h/z7x1SOHXOvbEi0TqABEPgU5d7zCJv7qQfHUhAnDe9H9jgCQQjfcL12cNv5dYszkI/Ys= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb2d.google.com with SMTP id 3f1490d57ef6-e05f25fb96eso4186632276.1 for ; Mon, 22 Jul 2024 10:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721670212; x=1722275012; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uI/dEO+wMYu8WCH4J17C1beQDrTaxaqlebuWcgFLOJI=; b=lIFPqYiD5SbiqKSdUIUDnLRv6XZsfKB/2DNYyzuojo5k+iRF+VTvGtk4FdUPw3ozAi 0ibDZswEe5M+NeCyMY/JbTv/USeCL4VTtnBeT2gY5OF5zz/DJ6sQTyGtntK6jJSTZAwl xgatjxObbcFUphey4gSc0u3vhsV+D0oNMYhzX6vuKzBAU/z/gpUa6s1BKR08PuZbdYrU JTjfNJ3wrSKF2lhvFAZdSH/X4pBJOpMcxH4W4del4Ryb1QaL+OegcsqkeHPosLCI+znw cwvDN7xtX5gC7wisHVu080lO2ih+2392zOweECP1OlwnG9Cxt6gXxrkm0l4Z/NnkcVbw NLUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721670212; x=1722275012; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uI/dEO+wMYu8WCH4J17C1beQDrTaxaqlebuWcgFLOJI=; b=aXsJLfD6reH7aTfK6f1qyQ1issiUBlFSU7/gTsFHadhMIyQyD6oVKnJLOLDhjK5MJu 3/2XrAaAI9FCocO0Nt6oKSodQA1D3hh/vVih3JKa5NoW7frkl30CgNCGQ7eFxfkVH24U +0Iu9KPtxX4n1AphOaZPnU4l9wpelI0XF9KAZ0dSGiUJekupBoILeNc96ORc9pHsijjT 8SBj6PNTW+dU4M3IBOeAyZNZvhT46lTrFRVEVZPfdxRDrv+LAAskmJD9QKLJXB9Z1KXh Tv8zG1+t4SgXsuydpWtcG7S5MTzHMAMoKZ9M/HNufqE7Ds6qC4dln+DC0TL6opcRl8K2 l6/A== X-Gm-Message-State: AOJu0YyhVWs4LPYju0bLP2IQAUOn3OsyimfCnHdr3QtPohugFYq6hkPq uy9YimeIUDay47Sm6JOpjUyjlmO9dZk2mGVC58m1jte9+1vDJHHYJrd6ErTS0eU= X-Google-Smtp-Source: AGHT+IFjYRuNZozvauCRyTq8gkdo4H3nOK+O8fzsXEUPRLhOAj0H1kx0mTxJOhsnUWlaMVhIK72QWw== X-Received: by 2002:a05:6902:114b:b0:e02:b466:e59c with SMTP id 3f1490d57ef6-e086fcbcf88mr10061564276.0.1721670212196; Mon, 22 Jul 2024 10:43:32 -0700 (PDT) Received: from noahgold-desk.sh.intel.com ([192.55.46.44]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b967a72324sm20566426d6.140.2024.07.22.10.43.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 10:43:31 -0700 (PDT) From: Noah Goldstein To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com Subject: assert: Wrap __assert_fail in templated function Date: Tue, 23 Jul 2024 01:43:25 +0800 Message-Id: <20240722174325.1848039-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org The idea of this commit to essentially save code side in the "hot" `assert` passes case. By wrapping `__assert_fail` with a templated function and using the quasi unique `__LINE__` constant as the template parameter, we essentially ensure that each `assert` gets forwarded through a unique function. This then allows any reasonable optimizing compiler the clone the wrapper function and constant propagate the arguments passed (starts at -O2 for GCC/Clang). The result of the seperate, is that the codegen for setting up `__expression`, `__line`, `__file`, and `__functions` for the function call are moved to the presumed cold code on the assertion failure path. Having a seperate cold function allows the compiler/linker to move assertion setup code to a cold section ultimately saving space in potentially hot regions. See examples: https://godbolt.org/z/vMvrjnvaE --- assert/assert.h | 117 ++++++++++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 43 deletions(-) diff --git a/assert/assert.h b/assert/assert.h index 266a41df06..ea8dcdb8cd 100644 --- a/assert/assert.h +++ b/assert/assert.h @@ -82,66 +82,97 @@ extern void __assert (const char *__assertion, const char *__file, int __line) __END_DECLS +# if defined __cplusplus && __cplusplus >= 201103L \ + && (defined __GNUC__ || defined __clang__) +template +static __attribute__ ((__noreturn__)) __attribute_noinline__ __COLD void +__assert_fail_forwarder (const char *__assertion, const char *__file, + unsigned int __line, const char *__function) +{ + __assert_fail (__assertion, __file, __line, __function); +} +# define __ASSERT_FAIL(expr, file, line, func) \ + __assert_fail_forwarder (expr, file, line, func) + +# ifdef __USE_GNU +template +static __attribute__ ((__noreturn__)) __attribute_noinline__ __COLD void +__assert_perror_fail_forwarder (int __errnum, const char *__file, + unsigned int __line, const char *__function) +{ + __assert_perror_fail (__errnum, __file, __line, __function); +} +# define __ASSERT_PERROR_FAIL(errnum, file, line, func) \ + __assert_perror_fail_forwarder (errnum, file, line, func) +# endif +# else +# define __ASSERT_FAIL(expr, file, line, func) \ + __assert_fail (expr, file, line, func) +# ifdef __USE_GNU +# define __ASSERT_PERROR_FAIL(errnum, file, line, func) \ + __assert_perror_fail (errnum, file, line, func) +# endif +# endif + /* When possible, define assert so that it does not add extra parentheses around EXPR. Otherwise, those added parentheses would suppress warnings we'd expect to be detected by gcc's -Wparentheses. */ -# if defined __cplusplus -# if defined __has_builtin -# if __has_builtin (__builtin_FILE) -# define __ASSERT_FILE __builtin_FILE () -# define __ASSERT_LINE __builtin_LINE () -# endif -# endif -# if !defined __ASSERT_FILE -# define __ASSERT_FILE __FILE__ -# define __ASSERT_LINE __LINE__ -# endif -# define assert(expr) \ - (static_cast (expr) \ - ? void (0) \ - : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE, \ - __ASSERT_FUNCTION)) -# elif !defined __GNUC__ || defined __STRICT_ANSI__ -# define assert(expr) \ - ((expr) \ - ? __ASSERT_VOID_CAST (0) \ - : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) -# else +# if defined __cplusplus +# if defined __has_builtin +# if __has_builtin(__builtin_FILE) +# define __ASSERT_FILE __builtin_FILE () +# define __ASSERT_LINE __builtin_LINE () +# endif +# endif +# if !defined __ASSERT_FILE +# define __ASSERT_FILE __FILE__ +# define __ASSERT_LINE __LINE__ +# endif +# define assert(expr) \ + (static_cast (expr) \ + ? void (0) \ + : __ASSERT_FAIL (#expr, __ASSERT_FILE, __ASSERT_LINE, \ + __ASSERT_FUNCTION)) +# elif !defined __GNUC__ || defined __STRICT_ANSI__ +# define assert(expr) \ + ((expr) ? __ASSERT_VOID_CAST (0) \ + : __ASSERT_FAIL (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +# else /* The first occurrence of EXPR is not evaluated due to the sizeof, but will trigger any pedantic warnings masked by the __extension__ for the second occurrence. The ternary operator is required to support function pointers and bit fields in this context, and to suppress the evaluation of variable length arrays. */ -# define assert(expr) \ - ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \ - if (expr) \ - ; /* empty */ \ - else \ - __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \ - })) -# endif +# define assert(expr) \ + ((void) sizeof ((expr) ? 1 : 0), __extension__ ({ \ + if (expr) \ + ; /* empty */ \ + else \ + __ASSERT_FAIL (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \ + })) +# endif -# ifdef __USE_GNU -# define assert_perror(errnum) \ - (!(errnum) \ - ? __ASSERT_VOID_CAST (0) \ - : __assert_perror_fail ((errnum), __FILE__, __LINE__, __ASSERT_FUNCTION)) -# endif +# ifdef __USE_GNU +# define assert_perror(errnum) \ + (!(errnum) ? __ASSERT_VOID_CAST (0) \ + : __ASSERT_PERROR_FAIL ((errnum), __FILE__, __LINE__, \ + __ASSERT_FUNCTION)) +# endif /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__' which contains the name of the function currently being defined. This is broken in G++ before version 2.6. C9x has a similar variable called __func__, but prefer the GCC one since it demangles C++ function names. */ -# if defined __cplusplus ? __GNUC_PREREQ (2, 6) : __GNUC_PREREQ (2, 4) -# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__ -# else -# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L -# define __ASSERT_FUNCTION __func__ +# if defined __cplusplus ? __GNUC_PREREQ(2, 6) : __GNUC_PREREQ(2, 4) +# define __ASSERT_FUNCTION __extension__ __PRETTY_FUNCTION__ # else -# define __ASSERT_FUNCTION ((const char *) 0) +# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L +# define __ASSERT_FUNCTION __func__ +# else +# define __ASSERT_FUNCTION ((const char *) 0) +# endif # endif -# endif #endif /* NDEBUG. */