From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0BF493858D1E for ; Mon, 11 Apr 2022 16:44:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0BF493858D1E Received: from mimecast-mx02.redhat.com (mx3-rdu2.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-468-k2Rl-FpdPCyUzYRkHYE0uQ-1; Mon, 11 Apr 2022 12:44:18 -0400 X-MC-Unique: k2Rl-FpdPCyUzYRkHYE0uQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 210EE3803901; Mon, 11 Apr 2022 16:44:18 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.172]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D73F9401E1B; Mon, 11 Apr 2022 16:44:17 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 23BGiFgo2641444 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 11 Apr 2022 18:44:15 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 23BGiES22641443; Mon, 11 Apr 2022 18:44:14 +0200 Date: Mon, 11 Apr 2022 18:44:14 +0200 From: Jakub Jelinek To: Richard Biener , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] builtins: Fix up expand_builtin_int_roundingfn_2 [PR105211] Message-ID: Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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, 11 Apr 2022 16:44:23 -0000 Hi! The expansion of __builtin_iround{,f,l} etc. builtins in some cases emits calls to a different fallback builtin. To locate the right builtin it uses mathfn_built_in_1 with the type of the first argument. If its TYPE_MAIN_VARIANT is {float,double,long_double}_type_node, all is fine, but on the following testcase, because GIMPLE considers scalar float conversions between types with the same mode as useless, TYPE_MAIN_VARIANT of the arg's type is float32_type_node and because there isn't __builtin_lroundf32 returns NULL and we ICE. This patch will first try the type of the argument and as fallback the type of the first argument of the builtin (which can't be 100% trusted either if user incorrectly prototypes it), and if neither works, doesn't fallback. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Though, perhaps it would be better to prefer the TREE_VALUE (TYPE_ARG_TYPES) type and only use TREE_TYPE (arg) as fallback, so that say on a TYPE_MODE (double_type_mode) == TYPE_MODE (long_double_type_mode) target we decide based on what builtin the user actually called rather than whether the argument has been converted from the other type earlier. 2022-04-11 Jakub Jelinek PR rtl-optimization/105211 * builtins.cc (expand_builtin_int_roundingfn_2): If mathfn_built_in_1 fails for TREE_TYPE (arg), retry it with TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) and if even that fails, emit call normally. * gcc.dg/pr105211.c: New test. --- gcc/builtins.cc.jj 2022-03-14 10:34:34.122924399 +0100 +++ gcc/builtins.cc 2022-04-11 11:57:31.178251743 +0200 @@ -2968,15 +2968,25 @@ expand_builtin_int_roundingfn_2 (tree ex a call to lround in the hope that the target provides at least some C99 functions. This should result in the best user experience for not full C99 targets. */ - tree fallback_fndecl = mathfn_built_in_1 - (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); + tree fallback_fndecl + = mathfn_built_in_1 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); + /* As scalar float conversions with same mode are useless in GIMPLE, + we can end up e.g. with _Float32 argument passed to float builtin, + try to get the type from the builtin prototype instead. */ + if (fallback_fndecl == NULL_TREE) + if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl))) + fallback_fndecl + = mathfn_built_in_1 (TREE_VALUE (argtypes), + as_combined_fn (fallback_fn), 0); + if (fallback_fndecl) + { + exp = build_call_nofold_loc (EXPR_LOCATION (exp), + fallback_fndecl, 1, arg); - exp = build_call_nofold_loc (EXPR_LOCATION (exp), - fallback_fndecl, 1, arg); - - target = expand_call (exp, NULL_RTX, target == const0_rtx); - target = maybe_emit_group_store (target, TREE_TYPE (exp)); - return convert_to_mode (mode, target, 0); + target = expand_call (exp, NULL_RTX, target == const0_rtx); + target = maybe_emit_group_store (target, TREE_TYPE (exp)); + return convert_to_mode (mode, target, 0); + } } return expand_call (exp, target, target == const0_rtx); --- gcc/testsuite/gcc.dg/pr105211.c.jj 2022-04-11 11:48:17.369946248 +0200 +++ gcc/testsuite/gcc.dg/pr105211.c 2022-04-11 11:48:09.924049700 +0200 @@ -0,0 +1,11 @@ +/* PR rtl-optimization/105211 */ +/* { dg-do compile } */ +/* { dg-options "-Os -ffast-math" } */ +/* { dg-add-options float32 } */ +/* { dg-require-effective-target float32 } */ + +short +foo (_Float32 f) +{ + return __builtin_roundf (f); +} Jakub