From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id 02F843858C3A for ; Mon, 27 Sep 2021 20:34:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 02F843858C3A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com IronPort-SDR: ZziNE30IPVQRCub07xGjt2GL+ALD/VzSvXxCzyrJtuh3SwZaC4qplYlhIZftDwfipeYJjkGyy+ Pqxd54JC2FWqD1SLOlS+Q+pka+l8Q6waPvdht0sDaxVc/wmiAkhuTRpYl/LPs1DDTBAOLQ5RQW rarWqUcYf/hwZB9WHpaDOsextcJAYfseYw9F5u3pBKyrqtJqyvGRe8wShlbfoLOn4iRr/MCRtg K4/XHfikMm39S71XWc0bvPxqizw+U5uTuxivh5jH2BryeNnSS7j0mbfGoq/c7l60SukeUg+jWB xLT4qjHlgJD1M5Cc9+qZ8Cy9 X-IronPort-AV: E=Sophos;i="5.85,327,1624348800"; d="scan'208";a="66370356" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 27 Sep 2021 12:34:05 -0800 IronPort-SDR: gW2Gi6WSLYi8X+Va+942r1G+4bPuDUbL4ma0iVBfZ93jdRigYptFUkcVTk9qi/hTe51GeN8/lB +LTPiiI3LY4JHquvkHy94KvQWywQaANXGBqVihD/DVdtXjBzdpS9ZDLXDIrI7BALQ+99ZVKJLp L8gpirCZxhPJRLw959NKPfHoJnlgRVJNPC/JnbbtbRjslsTKY0Ok9Bebn8SaKq7G/u1b1oAKea q1n1wjVy/7hYqIUqnm2RCbspF4gMpuFbMjGdwH7Gi91RmckndDc4X8wbz0gIPl/+8wY+MVTavR qa8= Date: Mon, 27 Sep 2021 20:33:59 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Richard Biener CC: Roger Sayle , GCC Patches Subject: Re: [RFC] Experimental __attribute__((saturating)) on integer types. In-Reply-To: Message-ID: References: <008b01d7b316$63e54060$2bafc120$@nextmovesoftware.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-08.mgc.mentorg.com (139.181.222.8) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-3118.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Sep 2021 20:34:09 -0000 On Mon, 27 Sep 2021, Richard Biener via Gcc-patches wrote: > Now - ISTR that elsewhere Joseph suggested that taking on > saturating operations by type was eventually misguided and we should > have instead added saturating arithmetic tree codes that we could > expose via some builtin functions like the overflow ones. There are several issues there: * saturating (and other fixed-point) types at the C API level; * saturating (and other fixed-point) types in GIMPLE; * saturating (and other fixed-point) modes in RTL. As I said in , I think having special modes for these kinds of types is a bad idea, because operations should be lowered to ordinary integer arithmetic at some point in GIMPLE, or at the latest in expand. (Maybe a few cases would sensibly use libgcc functions rather than inline arithmetic, but those would be the exception. We handle inline expansion of the overflow-checking built-in functions in general, much of that code could be shared to expand saturating arithmetic in general on hardware lacking the operations.) At present, there are loads of fixed-point machine modes, and very many libgcc functions on the targets supporting fixed-point, and very little optimization done on these operations, when if the operations were lowered to normal arithmetic earlier, generic code in the compiler could optimize them. (Back ends would still need to know enough about the types in question to be able to implement any desired ABI differences from the underlying ordinary integer types.) My inclination is that GIMPLE should also use saturating operations rather than saturating types. At the C API level it's less clear. When you have saturating types in the front end - as in those we currently have implemented, from the Embedded C TR, for example - at some point they need lowering to saturating operations on normal types, if you follow my suggested model above. That could be at gimplification, or you could allow saturating types in GIMPLE but then have some early pass that replaces them by normal types using saturating operations. For some kinds of algorithm, saturating types may well be a convenient abstraction for the user. For others, saturating operations on normal types may make more sense (e.g. using saturating arithmetic on size_t to compute an allocation size, knowing that SIZE_MAX will result in allocation failure if passed to an allocation function). As for the specific patch: it looks like you create a new type every time the user uses the attribute. If you allow users to create such saturating types (distinct from the fixed-point ones) at all, I think that every time someone requests int __attribute__ ((saturating)) it should produce the same type (and likewise for each other underlying non-saturating integer type, and watch out for any interactions with types created for bit-fields). Then there would be API design questions to address such as the results of converting out-of-range integer or floating-point values - or, for that matter, wider pointers - to a saturating type. -- Joseph S. Myers joseph@codesourcery.com