From patchwork Wed Sep 2 09:00:47 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 142691 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0FC714BA2E27 for ; Wed, 2 Sep 2026 09:02:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0FC714BA2E27 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZbbVr9ws 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 ESMTP id 4FEF44BA2E0C for ; Wed, 2 Sep 2026 09:01:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4FEF44BA2E0C Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4FEF44BA2E0C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788339660; cv=none; b=PSueTrjojAwNDzrN+Sxp2fxWekOy+J0H+b1RX8y2QnP2bITFL/zjXuIaPBLi6ISuPLTtopkTTBJYnez6evh78zjCFDLmip8MEywT32m9abnlOsYcicrTnJA4duTYy5HtaCQndUK+7yOC48vof9ufFLhfPy2nRd7i0XE4Szg8tPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788339660; c=relaxed/simple; bh=/vWdulQLD58UkENTMXrW6UmeYoy8HzjX8a2A4Gx4J6k=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=g+0nRySEhGbJoCe1NmHF7tjYA7zird+tkUsalR6tgmAQl5aHOa2hLROIH8E6kpGKFCvFTe7DKDNGXDIC3TSTZ3x+suHxHjTex5EAEMXq3VFSpDfD2Y26l0IXoWsYgU0azpzK4cdaRvcQBZAGyzWB+OEbE4+deob1rkptH13S64w= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ZbbVr9ws DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4FEF44BA2E0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788339659; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=O2qXSHmoi20y/LV/3P5lrACcVQrTe5M3vrjZarXHSmY=; b=ZbbVr9wsW+YBfXHzmo4hTNXnGiJwmhF2T2TBXUCjMQw9DivKwgHWKXQHHsWHEUJ41nd0Lz ACvfcgDYl305ZR0oxZEjXK1sGDqkcn2jyfOhrihYrJx0TqNFsekU7wBWZNP21lzL2QbyUK EyPNl4+wNs7GXGlGasDMD+WOsMY1RAY= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-639-Ors9hxkqPpmNhm7hCn0i7g-1; Wed, 02 Sept 2026 05:00:58 -0400 X-MC-Unique: Ors9hxkqPpmNhm7hCn0i7g-1 X-Mimecast-MFC-AGG-ID: Ors9hxkqPpmNhm7hCn0i7g_1788339657 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3CBEA184B3BF; Wed, 2 Sep 2026 09:00:57 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.44.33.31]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9C2031800577; Wed, 2 Sep 2026 09:00:56 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.18.1/8.18.1) with ESMTPS id 68290pZn4170889 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 2 Sep 2026 11:00:51 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.18.1/8.18.1/Submit) id 68290mk34170888; Wed, 2 Sep 2026 11:00:48 +0200 Date: Wed, 2 Sep 2026 11:00:47 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] vect-patterns: Fix up vect_recog_popcount_clz_ctz_ffs_pattern for non-mode precision types [PR127149] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: lC6nxDM-ODpsH1CB0hdzMCnurffQKiur4LFGqGh_E7g_1788339657 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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_H2, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! The following testcase is miscompiled on aarch64-linux. The problem is that vect_recog_popcount_clz_ctz_ffs_pattern for builtins other than clz doesn't require type_has_mode_precision_p (lhs_type) and matches to an ifn with an argument with say _BitInt(7) type (as well as result) and we then happily vectorize it and match something that doesn't properly extend the padding bits. One possibility is to punt in this case (i.e. if (!type_has_mode_precision_p (lhs_type)) return NULL; ), the following patch instead pattern matches it with a cast to/from the TREE_TYPE (vec_type), i.e. the actual mode precision type we'll use for the vectorization. Bootstrapped/regtested on aarch64-linux, x86_64-linux and i686-linux, ok for trunk? 2026-09-02 Jakub Jelinek PR tree-optimization/127149 * tree-vect-patterns.cc (vect_recog_popcount_clz_ctz_ffs_pattern): Handle lhs_type without mode precision by adding casts. * gcc.dg/bitint-142.c: New test. Jakub --- a/gcc/tree-vect-patterns.cc 2026-08-31 17:12:13.972850769 +0200 +++ b/gcc/tree-vect-patterns.cc 2026-09-01 12:42:48.214480599 +0200 @@ -2318,6 +2318,17 @@ vect_recog_popcount_clz_ctz_ffs_pattern vect_pattern_detected ("vec_recog_popcount_clz_ctz_ffs_pattern", call_stmt); + tree orig_lhs_type = lhs_type; + gimple *cast_stmt = NULL; + if (!type_has_mode_precision_p (lhs_type)) + { + lhs_type = TREE_TYPE (vec_type); + cast_stmt + = gimple_build_assign (vect_recog_temp_ssa_var (lhs_type, NULL), + NOP_EXPR, unprom_diff.op); + unprom_diff.op = gimple_assign_lhs (cast_stmt); + } + /* Create B = .POPCOUNT (A). */ new_var = vect_recog_temp_ssa_var (lhs_type, NULL); tree arg2 = NULL_TREE; @@ -2358,12 +2369,24 @@ vect_recog_popcount_clz_ctz_ffs_pattern = vect_recog_ctz_ffs_pattern (vinfo, new_stmt_info, type_out); if (pattern_stmt == NULL) return NULL; + if (cast_stmt) + append_pattern_def_seq (vinfo, stmt_vinfo, cast_stmt, vec_type); if (gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (new_stmt_info)) { gimple_seq *pseq = &STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo); gimple_seq_add_seq_without_update (pseq, seq); } } + else if (cast_stmt) + append_pattern_def_seq (vinfo, stmt_vinfo, cast_stmt, vec_type); + + if (cast_stmt) + { + append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type); + tree ret_var = vect_recog_temp_ssa_var (orig_lhs_type, NULL); + pattern_stmt = gimple_build_assign (ret_var, NOP_EXPR, + gimple_get_lhs (pattern_stmt)); + } return pattern_stmt; } --- a/gcc/testsuite/gcc.dg/bitint-142.c 2026-09-01 12:51:40.839450937 +0200 +++ b/gcc/testsuite/gcc.dg/bitint-142.c 2026-09-01 12:51:21.346708207 +0200 @@ -0,0 +1,38 @@ +/* PR tree-optimization/127149 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-O3" } */ + +typedef unsigned _BitInt(7) B; + +[[gnu::noipa]] void +foo (B *restrict y, const B *restrict x) +{ + for (unsigned i = 0; i < 16; ++i) + y[i] = __builtin_popcountg (x[i]); +} + +[[gnu::noipa]] void +bar (B *restrict y, const B *restrict x) +{ + for (unsigned i = 0; i < 16; ++i) + y[i] = __builtin_ctzg (x[i], 8); +} + +int +main () +{ + B x[16], y[16], z, w[16]; + B e[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; + B f[16] = { 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; + for (unsigned i = 0; i < 16; ++i) + x[i] = i; + z = ~0; + __builtin_clear_padding (&z); + for (unsigned i = 0; i < 16; ++i) + ((unsigned char *) &x[0])[i] |= ~*(unsigned char *) &z; + foo (y, x); + bar (w, x); + for (unsigned i = 0; i < 16; ++i) + if (y[i] != e[i] || w[i] != f[i]) + __builtin_abort (); +}