From patchwork Thu Oct 26 08:37:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 158447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp517767vqb; Thu, 26 Oct 2023 01:39:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5anKcm+iflNloXFqPl2QWTEK03nLZ3hT9lSQtp5Hq7jck+ST7EGXlyaavt63p86q7xMQo X-Received: by 2002:a05:6214:f06:b0:66f:af50:94bb with SMTP id gw6-20020a0562140f0600b0066faf5094bbmr494317qvb.6.1698309550716; Thu, 26 Oct 2023 01:39:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698309550; cv=pass; d=google.com; s=arc-20160816; b=fH4/kAPA+9EjKzDxsFPi9M2XctiimZq+ew5ThESeS6fvAr5pgMhSD2tVyuHdh6cgZz aebmRdl4zatJhnHMh7z8ydYAL99Nsiyuap8nghviJ5/anj9nVSS2/LbkXpHO1VV7WBlV CfflB2Ei46JEFy5tVg+G10w8Jmrkg7loutkjVRDJEWWOvXHkDpN2LvTAGBibYG6xcJy3 5nCuY40k05hpIPq0OXEm8ZttwkrqxZQmzwqUzKbGvyCPB/jIYkxZVAwyp0n/APUY68F5 V+a17CYHotX0NWml3xYsYwIkp/EcfmLmJNucRaO6ONZ9zw3v7h7P/eVlkyLI3M9pH2dy /oUQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :content-transfer-encoding:date:cc:to:from:subject:message-id :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to; bh=fOwyf5vIzvEPzctD2V10nHtm9XjfUMi2Ea+wO15z9o4=; fh=GS0oUCSo8Y9sw9ctcizPpdR0KsO7YG5ekA8o8n6MGOY=; b=A6icuYdZTEYoyIrET0MrBOw64zG8w+4p5P8VkwRsJjKtgvF/GFvFrTwTlPs4k+WDOP b7xIDQz6yyYsOfhlBxowFSReIQ/UUeRbLkbXfhaXKsmw6m+d0FHvPYrKogk4uOh9EDqW YePBCiCi3+eMgIgwZqhKD5Nj11sLN77ZvPSLLvKhYUrYi0XUSNWuZPorU9nio1JqyRYg hHAWep7ofvqcRMEr91bSZFmDSntiUe2U/n1Qk4NweSqJexdRkWo/OGdHArMGCsnnpTog 6PjGRU9niAPD6TknhMIEgfzzx7Iu8QFa/SOlZ7drXFHGbO7L2sn5xzgZx7wTQhYhXYoY 8X8g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=auWux0qa; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ee12-20020a0562140a4c00b0066d36b953f2si9034648qvb.407.2023.10.26.01.39.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 01:39:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=auWux0qa; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C17C385B51F for ; Thu, 26 Oct 2023 08:39:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 40FAA385B531 for ; Thu, 26 Oct 2023 08:38:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40FAA385B531 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 40FAA385B531 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698309487; cv=none; b=hQrKVfW+sWwvG1C5ultdSh7ojRe4oZvW+6bl4FYmE+2hHDavgX75lRBbPauREQGHbNRtfY8yfJCzwxa7yqwuLUrgeKFZqxbkRSqXBkgxecBKGy99Jj9OqEgq+baxU8PvMawBviTiXKr89x24Ild0JoLMu11/EBAYzD+iAfpI/c8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698309487; c=relaxed/simple; bh=jeR5bPtWwe4XtECMtTlWAmkAf3FmwOqQYEBp1ZBnDug=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=d9HZN3FqJj/aSRtm4VHYjZKJt+cNE+kMverrOOhPgwDLYtVwBqwTdTYNGNW77p1cOcPFF4Vgjg/Ipetz+vES3UEcytVehpwlAwEA41CjwsLSm0LDQgMYfpJTD4xQdIvs8eCRh4WKfxt++GUaDXG2zr+TECGYaCZ19Ms9wnpmkuY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-169-81.tugraz.at (vra-169-81.tugraz.at [129.27.169.81]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4SGK1h1Ymlz1LM0T; Thu, 26 Oct 2023 10:38:00 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4SGK1h1Ymlz1LM0T DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1698309481; bh=fOwyf5vIzvEPzctD2V10nHtm9XjfUMi2Ea+wO15z9o4=; h=Subject:From:To:Cc:Date:From; b=auWux0qa/8W4To6a0/8mjaP0BgYAz8pdTABaDzlDCIrSu9yH+Y/rJYhO17AoAcdO8 e0uxhZbuTM8G8UDOkflHleSrnjLhEkW+exDWCwqTc8uwrLERP2wS70r7UoZJRRJRLD 1JH/8zXcR1OHdbNnjwtecrPRhyF6pTO5W7a7Y7c4= Message-ID: <67644e18c4a0ba9febd1b0e82080f74e387b83f7.camel@tugraz.at> Subject: [PATCH] tree-optimization/109334: Improve computation for access attribute From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Siddhesh Poyarekar , Jakub Jelinek Date: Thu, 26 Oct 2023 10:37:59 +0200 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780806635639535396 X-GMAIL-MSGID: 1780806635639535396 Hi Sid and Jakub, here is the patch discussed in PR 109334. Martin tree-optimization/109334: Improve computation for access attribute The fix for PR104970 restricted size computations to the case where the access attribute was specified explicitly (no VLA). It also restricted it to void pointers or elements with constant sizes. The second restriction is enough to fix the original bug. Revert the first change to again allow size computations for VLA parameters and for VLA parameters together with an explicit access attribute. gcc/ChangeLog: PR tree-optimization/109334 * tree-object-size.cc (parm_object_size): Allow size computation for explicit access attributes. gcc/testsuite/ChangeLog: PR tree-optimization/109334 * gcc.dg/builtin-dynamic-object-size-20.c (test_parmsz_simple3): Supported again. (test_parmsz_external4): New test. * gcc.dg/builtin-dynamic-object-size-20.c: New test. * gcc.dg/pr104970.c: New test. diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c index 6da04202ffe..07e3da6f254 100644 --- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c +++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c @@ -455,7 +455,6 @@ test_parmsz_simple2 (size_t sz, char obj[]) return __builtin_dynamic_object_size (obj, 0); } -/* Implicitly constructed access attributes not supported yet. */ size_t __attribute__ ((noinline)) test_parmsz_simple3 (size_t sz, char obj[sz]) @@ -527,6 +526,13 @@ test_parmsz_internal3 (size_t sz1, size_t sz2, double obj[sz1][sz2]) return __builtin_dynamic_object_size (obj, 0); } +size_t +__attribute__ ((noinline)) +test_parmsz_internal4 (size_t sz1, size_t sz2, double obj[sz1 + 1][4]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + /* Loops. */ size_t @@ -721,8 +727,8 @@ main (int argc, char **argv) if (test_parmsz_simple2 (__builtin_strlen (argv[0]) + 1, argv[0]) != __builtin_strlen (argv[0]) + 1) FAIL (); - /* Only explicitly added access attributes are supported for now. */ - if (test_parmsz_simple3 (__builtin_strlen (argv[0]) + 1, argv[0]) != -1) + if (test_parmsz_simple3 (__builtin_strlen (argv[0]) + 1, argv[0]) + != __builtin_strlen (argv[0]) + 1) FAIL (); int arr[42]; if (test_parmsz_scaled (arr, 42) != sizeof (arr)) @@ -759,6 +765,8 @@ main (int argc, char **argv) FAIL (); if (test_parmsz_internal3 (4, 4, obj) != -1) FAIL (); + if (test_parmsz_internal4 (3, 4, obj) != -1) + FAIL (); if (test_loop (arr, 42, 0, 32, 1) != 10 * sizeof (int)) FAIL (); if (test_loop (arr, 42, 32, -1, -1) != 0) diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-20.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-20.c new file mode 100644 index 00000000000..2c8e07dd98d --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-20.c @@ -0,0 +1,49 @@ +/* PR 109334 + * { dg-do run } + * { dg-options "-O1" } */ + + +[[gnu::noinline,gnu::noipa]] +int f(int n, int buf[n]) + [[gnu::access(read_only, 2, 1)]] +{ + return __builtin_dynamic_object_size(buf, 0); +} + +[[gnu::noinline,gnu::noipa]] +int g(int n, int buf[]) + [[gnu::access(read_only, 2, 1)]] +{ + return __builtin_dynamic_object_size(buf, 0); +} + +[[gnu::noinline,gnu::noipa]] +int h(int n, int buf[n]) +{ + return __builtin_dynamic_object_size(buf, 0); +} + +int dummy(int x) { return x + 1; } + +[[gnu::noinline,gnu::noipa]] +int i(int n, int buf[dummy(n)]) +{ + return __builtin_dynamic_object_size(buf, 0); +} + +int main() +{ + int n = 10; + int buf[n]; + if (n * sizeof(int) != f(n, buf)) + __builtin_abort(); + if (n * sizeof(int) != g(n, buf)) + __builtin_abort(); + if (n * sizeof(int) != h(n, buf)) + __builtin_abort(); + + (void)i(n, buf); + + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/pr104970.c b/gcc/testsuite/gcc.dg/pr104970.c new file mode 100644 index 00000000000..e24a7f22dfb --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr104970.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -D_FORTIFY_SOURCE=2" } */ + +__inline void +memset2(void *__dest, int __ch, long __len) { + long __trans_tmp_1 = __builtin_dynamic_object_size(__dest, 0); + __builtin___memset_chk(__dest, __ch, __len, __trans_tmp_1); +} + +void +mleye(int l, double E[][l]) { memset2(E, 0, sizeof(double)); } + + diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index a62af050056..28f27adf9ca 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1575,8 +1575,8 @@ parm_object_size (struct object_size_info *osi, tree var) tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm))); tree sz = NULL_TREE; - /* If we have an explicit access attribute with a usable size argument... */ - if (access && access->sizarg != UINT_MAX && !access->internal_p + /* If we have an access attribute with a usable size argument... */ + if (access && access->sizarg != UINT_MAX /* ... and either PARM is void * or has a type that is complete and has a constant size... */ && ((typesize && poly_int_tree_p (typesize)) @@ -1587,10 +1587,14 @@ parm_object_size (struct object_size_info *osi, tree var) unsigned argpos = 0; /* ... then walk through the parameters to pick the size parameter and - safely scale it by the type size if needed. */ + safely scale it by the type size if needed. + + TODO: we could also compute the size of VLAs where the size is + given by a function parameter. */ for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos) - if (argpos == access->sizarg && INTEGRAL_TYPE_P (TREE_TYPE (arg))) + if (argpos == access->sizarg) { + gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg))); sz = get_or_create_ssa_default_def (cfun, arg); if (sz != NULL_TREE) {