From patchwork Wed Jul 3 10:49:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1956160 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; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=MQsBvDte; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 4WDc4Q3Sfbz1xqb for ; Wed, 3 Jul 2024 20:50:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C6C4D386100C for ; Wed, 3 Jul 2024 10:50:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id F348A3860C37 for ; Wed, 3 Jul 2024 10:49:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F348A3860C37 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F348A3860C37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::32f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720003772; cv=none; b=cT9nrQXqM4e5d5Y9sq9vyoRjI8SEGOIu4gxE9kEwwXcg4aAL0vWDnQRaqcx29z9pkSBH7nSfzg92Y7fz8Q3dNxyf8XwygqjmvQd/SdZuSZ3WZUCkoeFotdaWWKBXleF0gHUU1faq1TBKL5KgJ+fPJ68PuK0JNGPKdeP/K3l+vzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720003772; c=relaxed/simple; bh=yy3c/4Q8m6+bU4tBH1zm5lz6jcYgCknQGPlKRerndZg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=teSYHVragIwpgjYzwogAxRmkgjHdr3r34koHtplW4Tp8umKuk8E9l/zs84Qu7el1QslK/D8XSxyp4KmIoGhdWhIjilLPkhXO3xAzHZAVe/UuyObS73YBmiwzhuioU77Mfdr067Z/MrvoemGDlULf8qQdcUIyHSOyFGvrBxyk+fA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-700cc366e0dso2570859a34.2 for ; Wed, 03 Jul 2024 03:49:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1720003767; x=1720608567; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=YsLZhs32QMh1hEBcTJhLrngClKEX41m3C22zOyv4tME=; b=MQsBvDte7ifzVmXLWs5htku+zk2yByyuykp2rlQPD4dFDnBHMo7NEcpEn4FsWAywcO OSV1wEEIwgdid5XnPSyjAd32+rGUW/Glr4EHaDGOJlhsCjcd3kNwL5k8zirFNjCp1s8P 5ogPHZmYTp3TBqmlxpz92Z5nVBvpSL/SKciFcXQmTcTGO8+hyEdpiPy5z6cR6+xg+bee xf5lEBorQXUnMn11xyjpvThOkuv4eOEMIzypJSRW3NTOR/3JhRd1tCZMujPG4jzRqGHR 29gmAqlUOV4XHVsjZxL4DEnPya1qwP4Hrzh0wBEiEEClSS+QflLWMDCqgObxWjHSVL5Q Cw8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720003767; x=1720608567; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YsLZhs32QMh1hEBcTJhLrngClKEX41m3C22zOyv4tME=; b=a2g24zVvvwCXKFmRHx8GmwbmrpqPfVVC/gfkdfduMabiy9ddh+Xaw96Ct320OpHcQh 5vV56Gr8sRkIh3wDxqQrDN5ku2VtMl391GXcBFyp98baoN89lEKWDyoeinyew23akXLg aTb/zXcaggV8AHBpG3vvUo6U940Txg5836Yq9xWvWvhoyOQExHm5/ibK/MSNlHpA+1b2 E7vtuKiTQCgn4dB8ztMLoQ+B52y7K2bcJY7QYqLzkP/a5KSnnteTrjFy2pJPMwyXYkHp wjwnh2cpAsGtPLiyx+mOwmNUMs3v9Exeii7xSok3pu1zO6ZyQrbJ11SfpROmtNrJm2An VFhQ== X-Gm-Message-State: AOJu0YwBJp2uzxRcqm4cxNR1x4gDEygsi2vhsGf0w8Q1Ek/bJszhm5fb eARrIZtOli5EcjkDCJqfNP64uUsUqzXUtN9R7LI+A0um9QeugsBH7DQqjMvVq37ymcC8MvFUGGj 6aVLD X-Google-Smtp-Source: AGHT+IHUEZt0JuBfLYySpiQEH5UdifbwwoKmumq70bh5/oPeAz2CclA0kKxmQ+KW3kmZu4CxoayhYg== X-Received: by 2002:a05:6870:d38e:b0:254:930c:fa08 with SMTP id 586e51a60fabf-25db33aaccamr9895747fac.12.1720003767002; Wed, 03 Jul 2024 03:49:27 -0700 (PDT) Received: from free.home ([2804:7f1:218a:d53f:1f12:68c4:3c4:d64a]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70801e53f83sm10137079b3a.26.2024.07.03.03.49.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 03:49:26 -0700 (PDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 463AnEn5660404 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 3 Jul 2024 07:49:14 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Cc: ebotcazou@adacore.com Subject: [PATCH, FYI] [debug] Avoid dropping bits from num/den in fixed-point types Organization: Free thinker, does not speak for AdaCore Date: Wed, 03 Jul 2024 07:49:14 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_QUOTING 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~incoming=patchwork.ozlabs.org@gcc.gnu.org We used to use an unsigned 128-bit type to hold the numerator and denominator used to represent the delta of a fixed-point type in debug information, but there are cases in which that was not enough, and more significant bits silently overflowed and got omitted from debug information. Introduce a mode in which UI_to_gnu selects a wide-enough unsigned type, and use that to convert numerator and denominator. While at that, avoid exceeding the maximum precision for wide ints, and for available int modes, when selecting a type to represent very wide constants, falling back to 0/0 for unrepresentable fractions. Regstrapped on x86_64-linux-gnu. Pre-approved by Eric. I'm checking it in. for gcc/ada/ChangeLog * gcc-interface/cuintp.cc (UI_To_gnu): Add mode that selects a wide enough unsigned type. Fail if the constant exceeds the representable numbers. * gcc-interface/decl.cc (gnat_to_gnu_entity): Use it for numerator and denominator of fixed-point types. In case of failure, fall back to an indeterminate fraction. --- gcc/ada/gcc-interface/cuintp.cc | 66 +++++++++++++++++++++++++++++---------- gcc/ada/gcc-interface/decl.cc | 19 +++++++++-- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/gcc/ada/gcc-interface/cuintp.cc b/gcc/ada/gcc-interface/cuintp.cc index cdf6c0197500d..1903c5af0f19a 100644 --- a/gcc/ada/gcc-interface/cuintp.cc +++ b/gcc/ada/gcc-interface/cuintp.cc @@ -35,6 +35,7 @@ #include "tree.h" #include "inchash.h" #include "fold-const.h" +#include "stor-layout.h" #include "ada.h" #include "types.h" @@ -67,7 +68,9 @@ build_cst_from_int (tree type, HOST_WIDE_INT low) /* Similar to UI_To_Int, but return a GCC INTEGER_CST or REAL_CST node, depending on whether TYPE is an integral or real type. Overflow is tested by the constant-folding used to build the node. TYPE is the GCC type of - the resulting node. */ + the resulting node. If TYPE is NULL, an unsigned integer type wide enough + to hold the entire constant is selected, and if no such type exists, + return NULL_TREE. */ tree UI_To_gnu (Uint Input, tree type) @@ -77,8 +80,10 @@ UI_To_gnu (Uint Input, tree type) any such possible value for intermediate computations and then rely on a conversion back to TYPE to perform the bias adjustment when need be. */ tree comp_type - = TREE_CODE (type) == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type) - ? get_base_type (type) : type; + = (!type ? gnat_type_for_size (32, 1) + : (TREE_CODE (type) == INTEGER_TYPE + && TYPE_BIASED_REPRESENTATION_P (type)) + ? get_base_type (type) : type); tree gnu_ret; if (Input <= Uint_Direct_Last) @@ -88,9 +93,14 @@ UI_To_gnu (Uint Input, tree type) Int Idx = (*Uints_Ptr)[Input - Uint_Table_Start].Loc; Pos Length = (*Uints_Ptr)[Input - Uint_Table_Start].Length; Int First = (*Udigits_Ptr)[Idx]; + tree_code code = First < 0 ? MINUS_EXPR : PLUS_EXPR; tree gnu_base; gcc_assert (Length > 0); + /* The extension of unsigned types we use to try to fit the + constant only works if we're dealing with nonnegative + constants, but that's what we expect when !TYPE. */ + gcc_assert (type || First >= 0); /* The computations we perform below always require a type at least as large as an integer not to overflow. FP types are always fine, but @@ -103,22 +113,44 @@ UI_To_gnu (Uint Input, tree type) gnu_base = build_cst_from_int (comp_type, Base); gnu_ret = build_cst_from_int (comp_type, First); - if (First < 0) - for (Idx++, Length--; Length; Idx++, Length--) - gnu_ret = fold_build2 (MINUS_EXPR, comp_type, - fold_build2 (MULT_EXPR, comp_type, - gnu_ret, gnu_base), - build_cst_from_int (comp_type, - (*Udigits_Ptr)[Idx])); - else - for (Idx++, Length--; Length; Idx++, Length--) - gnu_ret = fold_build2 (PLUS_EXPR, comp_type, - fold_build2 (MULT_EXPR, comp_type, - gnu_ret, gnu_base), - build_cst_from_int (comp_type, - (*Udigits_Ptr)[Idx])); + for (Idx++, Length--; Length; Idx++, Length--) + for (;;) + { + tree elt, scaled, next_ret; + elt = build_cst_from_int (comp_type, (*Udigits_Ptr)[Idx]); + /* We want to detect overflows with an unsigned type when + TYPE is not given, but int_const_binop doesn't work for + e.g. floating-point TYPEs. */ + if (!type) + { + scaled = int_const_binop (MULT_EXPR, gnu_ret, gnu_base, -1); + next_ret = int_const_binop (code, scaled, elt, -1); + } + else + { + scaled = fold_build2 (MULT_EXPR, comp_type, gnu_ret, gnu_base); + next_ret = fold_build2 (code, comp_type, scaled, elt); + } + if (!TREE_OVERFLOW (next_ret) || type) + { + gnu_ret = next_ret; + break; + } + opt_scalar_int_mode wider_mode + = GET_MODE_WIDER_MODE (SCALAR_INT_TYPE_MODE + (comp_type)).require (); + if (!wider_mode.exists ()) + /* Signal that we couldn't represent the value. */ + return NULL_TREE; + comp_type = make_unsigned_type (GET_MODE_BITSIZE + (wider_mode.require ())); + gnu_base = convert (comp_type, gnu_base); + gnu_ret = convert (comp_type, gnu_ret); + } } + if (!type) + type = comp_type; gnu_ret = convert (type, gnu_ret); /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET. */ diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc index 5b3a3b4961b42..d7c17238bbc05 100644 --- a/gcc/ada/gcc-interface/decl.cc +++ b/gcc/ada/gcc-interface/decl.cc @@ -1767,14 +1767,25 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition) /* Use the arbitrary scale factor description. Note that we support a Small_Value whose magnitude is larger than 64-bit even on 32-bit - platforms, so we unconditionally use a (dummy) 128-bit type. */ + platforms. UI_To_gnu chooses a wide-enough integral type. */ else { const Uint gnat_num = Norm_Num (gnat_small_value); const Uint gnat_den = Norm_Den (gnat_small_value); - tree gnu_small_type = make_unsigned_type (128); - tree gnu_num = UI_To_gnu (gnat_num, gnu_small_type); - tree gnu_den = UI_To_gnu (gnat_den, gnu_small_type); + tree gnu_num = UI_To_gnu (gnat_num, NULL_TREE); + tree gnu_den = UI_To_gnu (gnat_den, NULL_TREE); + + if (!gnu_num || !gnu_den) + gnu_num = gnu_den = integer_zero_node; + + tree gnu_num_type = TREE_TYPE (gnu_num); + tree gnu_den_type = TREE_TYPE (gnu_den); + tree gnu_small_type = (TYPE_PRECISION (gnu_num_type) + >= TYPE_PRECISION (gnu_den_type) + ? gnu_num_type : gnu_den_type); + + gnu_num = convert (gnu_small_type, gnu_num); + gnu_den = convert (gnu_small_type, gnu_den); scale_factor = build2 (RDIV_EXPR, gnu_small_type, gnu_num, gnu_den);