From patchwork Fri Sep 8 12:16:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 137726 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp501530vqo; Fri, 8 Sep 2023 05:17:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmoIGOZs0fqm2+1WwpqAPSjTlr9e1kVyIFrzHvRI4Y4Ewupg/SXRtssDxtrqqdOCCqCYFi X-Received: by 2002:a05:6402:795:b0:527:251e:1be8 with SMTP id d21-20020a056402079500b00527251e1be8mr1864920edy.13.1694175422819; Fri, 08 Sep 2023 05:17:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694175422; cv=none; d=google.com; s=arc-20160816; b=tv+CWJISI4VHo7I4REZJv3gCm40LJ5aSBfjDO1Tb1fAUXVZI64WUx1c5q59oDWItAO jnAzWyuwoYmOQiNYkli81nQ8uwA1Zs7e9B9ssC6mEC7qydeD8R92Ce4xrqvZPkd14lN7 xlpMFDj/AgCdXMYIhPkzvoO89IVE90Vf+0HFSS6wE9lsqoP7vz7xHSFedRQMk/wdWruo HnTOzpwRUTjh4TFeyjpkenxd0JdrXXSFQGGtTTGoYMzRXXoJj1Ckw/mIy86tzsBaQmZ9 67B268ZwGtSDBrYKUSXOsb7q5bOLvNPKF9NiASQxq9NTrbBIehpMjX70um7ywTyytKvE f+Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=v+bdWqgSHCxirHLnaG+e5OGx6D3F1AM6Uvk7l73so5w=; fh=kRVE1+m5qInUjPXomUwtbLegXKZevp8DH8O4fydPang=; b=Hq6wxU9oE61V+fmZnf3gb3nw+0JrFWWIhA1JoFhZPQMHnWYiy496o1owmqOSWcBbW7 bwnQgbb8ppwk4Nsd3qfdsGlJG649a4ZwgEUXklzHBQ2ybIN55fgtVlvmkj9miad2jl7I WO4OiV2kxXdytROfM1C8OyLKMq5pQfHzjr7ToLqnicpBqcWOCPbjFb8BI+npsThfJAnO A1n/7q5p8g9mYaCgmfVZ1km7i4QsGK2blYXFXRs4d0bs4ozZBlLtfdLf2ReDfqxaV548 UV7TokjAn1m8ZmuWmLHG/csMiJBjO6OZsUoqoqTD7agxjIFQoC9fLJsCoJZ8BCI7Ad6Z qikg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tmMKWWFA; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u21-20020a056402065500b0052e9912f5f2si1379509edx.495.2023.09.08.05.17.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 05:17:02 -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=@gcc.gnu.org header.s=default header.b=tmMKWWFA; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92ADC38582A1 for ; Fri, 8 Sep 2023 12:17:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 92ADC38582A1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694175421; bh=v+bdWqgSHCxirHLnaG+e5OGx6D3F1AM6Uvk7l73so5w=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=tmMKWWFAtHmU4+NbZw2CuVc8PuH4p3EViXYcOrUUkaO2YXR9aTe55Bw1ds5K3pPTH j1xxrpgfpd859LTTTBB1C3VgF9EnNd332PvF95fPXqLHrJKPoTJUhXQLBb6iM4jYBu 5Xto96QTNeZibko+qmDVDgj2CkMRRDAQQXN4yIR0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id DA9FA38582BD for ; Fri, 8 Sep 2023 12:16:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA9FA38582BD Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-361-IHCU_cNMOlucH5VLuqTzAA-1; Fri, 08 Sep 2023 08:16:13 -0400 X-MC-Unique: IHCU_cNMOlucH5VLuqTzAA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0A8AD381AE43; Fri, 8 Sep 2023 12:16:13 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C16AA40C2070; Fri, 8 Sep 2023 12:16:12 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 388CGBFE1033381 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 8 Sep 2023 14:16:11 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 388CGAMT1033380; Fri, 8 Sep 2023 14:16:10 +0200 Date: Fri, 8 Sep 2023 14:16:10 +0200 To: Richard Sandiford , David Malcolm Cc: gcc-patches@gcc.gnu.org, Richard Biener Subject: [PATCH] pretty-print: Fix up pp_wide_int [PR111329] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776471688286887034 X-GMAIL-MSGID: 1776471688286887034 Hi! The recent pp_wide_int changes for _BitInt support (because not all wide_ints fit into the small fixed size digit_buffer anymore) apparently broke +FAIL: gcc.dg/analyzer/out-of-bounds-diagram-1-debug.c (test for excess errors) +FAIL: gcc.dg/analyzer/out-of-bounds-diagram-1-debug.c 2 blank line(s) in output +FAIL: gcc.dg/analyzer/out-of-bounds-diagram-1-debug.c expected multiline pattern lines 17-39 (and I couldn't reproduce that in bisect seed (which is -O0 compiled) and thought it would be some analyzer diagnostic bug). The problem is that analyzer uses pp_wide_int with a function call in the second argument. Previously, when pp_wide_int macro just did print_dec (W, pp_buffer (PP)->digit_buffer, SGN); pp_string (PP, pp_buffer (PP)->digit_buffer); it worked, because the const wide_int_ref & first argument to print_dec bound to a temporary, which was only destructed at the end of the full statement after print_dec was called. But with my changes where I need to first compare the precision of the const wide_int_ref & to decide whether to use digit_buffer or XALLOCAVEC something larger, this means that pp_wide_int_ref binds to a temporary which is destroyed at the end of full statement which is the const wide_int_ref &pp_wide_int_ref = (W); declaration, so then invokes UB accessing a destructed temporary. The following patch fixes it by rewriting pp_wide_int into an inline function, so that the end of the full statement is the end of the inline function call. As functions using alloca aren't normally inlined, I've also split that part into a separate out of line function. Putting that into pretty-print.cc didn't work, e.g. the gm2 binary doesn't link, because pretty-print.o is in libcommon.a, but wide-print-print.o which defines print_dec is not. So I've put that out of line function into wide-int-print.cc instead. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-09-08 Jakub Jelinek PR middle-end/111329 * pretty-print.h (pp_wide_int): Rewrite from macro into inline function. For printing values which don't fit into digit_buffer use out-of-line function. * wide-int-print.h (pp_wide_int_large): Declare. * wide-int-print.cc: Include pretty-print.h. (pp_wide_int_large): Define. Jakub --- gcc/pretty-print.h.jj 2023-09-06 14:36:53.485246347 +0200 +++ gcc/pretty-print.h 2023-09-08 11:11:21.173649942 +0200 @@ -333,28 +333,6 @@ pp_get_prefix (const pretty_printer *pp) #define pp_decimal_int(PP, I) pp_scalar (PP, "%d", I) #define pp_unsigned_wide_integer(PP, I) \ pp_scalar (PP, HOST_WIDE_INT_PRINT_UNSIGNED, (unsigned HOST_WIDE_INT) I) -#define pp_wide_int(PP, W, SGN) \ - do \ - { \ - const wide_int_ref &pp_wide_int_ref = (W); \ - unsigned int pp_wide_int_prec \ - = pp_wide_int_ref.get_precision (); \ - if ((pp_wide_int_prec + 3) / 4 \ - > sizeof (pp_buffer (PP)->digit_buffer) - 3) \ - { \ - char *pp_wide_int_buf \ - = XALLOCAVEC (char, (pp_wide_int_prec + 3) / 4 + 3);\ - print_dec (pp_wide_int_ref, pp_wide_int_buf, SGN); \ - pp_string (PP, pp_wide_int_buf); \ - } \ - else \ - { \ - print_dec (pp_wide_int_ref, \ - pp_buffer (PP)->digit_buffer, SGN); \ - pp_string (PP, pp_buffer (PP)->digit_buffer); \ - } \ - } \ - while (0) #define pp_vrange(PP, R) \ do \ { \ @@ -453,6 +431,19 @@ pp_wide_integer (pretty_printer *pp, HOS pp_scalar (pp, HOST_WIDE_INT_PRINT_DEC, i); } +inline void +pp_wide_int (pretty_printer *pp, const wide_int_ref &w, signop sgn) +{ + unsigned int prec = w.get_precision (); + if (UNLIKELY ((prec + 3) / 4 > sizeof (pp_buffer (pp)->digit_buffer) - 3)) + pp_wide_int_large (pp, w, sgn); + else + { + print_dec (w, pp_buffer (pp)->digit_buffer, sgn); + pp_string (pp, pp_buffer (pp)->digit_buffer); + } +} + template void pp_wide_integer (pretty_printer *pp, const poly_int_pod &); --- gcc/wide-int-print.h.jj 2023-09-08 11:03:48.320944156 +0200 +++ gcc/wide-int-print.h 2023-09-08 11:11:46.982292282 +0200 @@ -34,5 +34,6 @@ extern void print_decu (const wide_int_r extern void print_decu (const wide_int_ref &wi, FILE *file); extern void print_hex (const wide_int_ref &wi, char *buf); extern void print_hex (const wide_int_ref &wi, FILE *file); +extern void pp_wide_int_large (pretty_printer *, const wide_int_ref &, signop); #endif /* WIDE_INT_PRINT_H */ --- gcc/wide-int-print.cc.jj 2023-09-08 11:03:51.543898946 +0200 +++ gcc/wide-int-print.cc 2023-09-08 11:12:11.481952762 +0200 @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. #include "config.h" #include "system.h" #include "coretypes.h" +#include "pretty-print.h" /* * public printing routines. @@ -138,3 +139,14 @@ print_hex (const wide_int_ref &wi, FILE fputs (buf, file); } +/* Print larger precision wide_int. Not defined as inline in a header + together with pp_wide_int because XALLOCAVEC will make it uninlinable. */ + +void +pp_wide_int_large (pretty_printer *pp, const wide_int_ref &w, signop sgn) +{ + unsigned int prec = w.get_precision (); + char *buf = XALLOCAVEC (char, (prec + 3) / 4 + 3); + print_dec (w, buf, sgn); + pp_string (pp, buf); +}