From patchwork Mon Dec 11 18:03:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 81928 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 299C53858414 for ; Mon, 11 Dec 2023 18:04:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 008CB3858D28 for ; Mon, 11 Dec 2023 18:04:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 008CB3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 008CB3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702317853; cv=none; b=QEeOToHM3hiMq7tqza5XZekBOphKe7SNcoOjWepShqdlud4BHyE8Pg7s+upmIYZS6khioraPUJ7gU71iw1jSsihgbjN3HxoDAKehl5Gdkj0VPm59O30xklyuXF2U+F+Hw4NHCR2FJ9Ei3YERfWUyUwqMgHdWV1ngfao5wmkutKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702317853; c=relaxed/simple; bh=UdtSm6CRCrGx6IZoj4V1/L3CfCP6zBukVrs/xBlHvnA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KKTxlKxBucbnC9kO2vpA32F+qXu3dWitZ8CXzaETo6txsxwZQqcpCpwWlMFA/J4Fc5eED/FN3lRFZ/2ZyVzOBRoHWMcwJXEUdpMmSK68118KwvH2tjvNZ2iEwjO+BLqPvomWCudVAVMRWP3mr2aU2CjHZNW+vhK0c+MPUr9RYK4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d0897e99e0so27748575ad.3 for ; Mon, 11 Dec 2023 10:04:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702317850; x=1702922650; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ju0dsbye/1rOjkIWGUfyt8WEhgn50w5AOHYpFeU2nUk=; b=QCZO2DaRumPJhQf+CKIUKSpgVwNWypQNWWOt8HupGd8kPZRMSLnZWdfI8IhWJVNMJj InuZp3+u5CaslJ/T3BxQDAsPUwoN99VT5fqWri+R5t2BgsRk+AJOEJHl2gEmhLAvdHcn rxvkfd2wACEzepLta/tGa72HjAoE1xWnjCvMSMSvDstFQWjzBgRX1emBzGv7DLvJd1gn l8R9iw7aUjeZodh4hNKRaA7itK1rKT5kOBQ6eaS4JdMJLahFTcTEz1CsPOnzflb3VCb2 SozyYcjBYq0Ee1s83yissLN2DbOLvIeZ2ifcx/FMqZj8go1j2GErcVKzMHzhn7kzBWB2 fUJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702317850; x=1702922650; h=mime-version:user-agent:message-id:in-reply-to:date:references :organization:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ju0dsbye/1rOjkIWGUfyt8WEhgn50w5AOHYpFeU2nUk=; b=uVaG4YEfcflQUENIky7yIQUBDEWUJ7K8qq9i285+AQmoBThtrb2GDscqcVreozIRIr xSLYatgqGZhUjVzGH1Q5KJCgub8TTv7k/zyRE3OwIybndr3sRJti2uf65SE4iJQNYiEb YsbCwov7Ub1vTfbxP2AjWPqAgL6MrU+VvWY6AJgZWYP1MYLFw4N119YwX52MkyKrZcMI zJNGkVgdrAeR+6vnqX0cu2T0bIXZGvUMVtenq8AJF25pOfUphYLLtjHDv9r2H3tBLxoY YZYjHuq1XsKHZs1lquGeQ3bNzxg+2KGksjyo5DbkIrG/7mD4/SFUzyBaAFGPrCMe2JNi Hjqg== X-Gm-Message-State: AOJu0YxPaPvEVk0mbZvd39BkNRYTe3lOHzByEkTFCaYQ0OR3bbQqH7UK flwFlakCViA5Vel8CC8H1OtUzg== X-Google-Smtp-Source: AGHT+IGd1FaTv9I5dMleGZ0Wid77T/R4ue3MJjNrNcR6Dkuf5VGE/2WjHzyL3sACHYSYYFutx4CwBA== X-Received: by 2002:a17:903:482:b0:1d0:8686:b68b with SMTP id jj2-20020a170903048200b001d08686b68bmr1953898plb.26.1702317849853; Mon, 11 Dec 2023 10:04:09 -0800 (PST) Received: from free.home ([2804:7f1:2080:bc9e:aead:db9b:9f77:6e06]) by smtp.gmail.com with ESMTPSA id d1-20020a170902728100b001d087820175sm6926878pll.40.2023.12.11.10.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 10:04:08 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 3BBI3cDU387811 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 11 Dec 2023 15:03:38 -0300 From: Alexandre Oliva To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH v2 FYI] -finline-stringops: avoid too-wide smallest_int_mode_for_size [PR112784] Organization: Free thinker, does not speak for AdaCore References: Date: Mon, 11 Dec 2023 15:03:38 -0300 In-Reply-To: (Richard Biener's message of "Mon, 11 Dec 2023 08:35:45 +0100") 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=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, 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+patchwork=sourceware.org@gcc.gnu.org On Dec 11, 2023, Richard Biener wrote: > you can use .exists (&move_mode) here to ... Aah, yeah, and that should help avoid the noisy as_a conversions too, that I could replace with require(), or drop altogether. >> + || (GET_MODE_BITSIZE (as_a (int_move_mode)) >> + != incr * BITS_PER_UNIT)) Unfortunately, here it can't quite be dropped, GET_MODE_SIZE on a machine_mode isn't suitable for the !=, but with .require() we know it's a scalar_int_mode and thus != on its bitsize is fine. > I'll note that int_mode_for_size and smallest_int_mode_for_size > are not semantically equivalent in what they can return. In > particular it seems you are incrementing by iter_incr even when > formerly smallest_int_mode_for_size would have returned a > larger than necessary mode, resulting in at least inefficient > code, copying/comparing pieces multiple times. If we get a mode that isn't exactly the expected size, we go for BLKmode, so it should be fine and efficient. Unless machine modes are not powers of two multiples of BITS_PER_UNIT, then things may get a little weird, not so much because of repeated copying/comparing, but because of inefficiencies in the block copy/compare operations with block sizes that are not a good fit for such hypothetical (?) GCC targets. I guess we can cross that bridge if we ever get to it. > So int_mode_for_size looks more correct. *nod*. IIRC I had it at first (very long ago), and went for the smallest_ when transitioning to the machine_mode type hierarchy revamp. > OK with the above change. Here's what I've regstrapped on x86_64-linux-gnu, and will install shortly. Thanks! smallest_int_mode_for_size may abort when the requested mode is not available. Call int_mode_for_size instead, that signals the unsatisfiable request in a more graceful way. for gcc/ChangeLog PR middle-end/112784 * expr.cc (emit_block_move_via_loop): Call int_mode_for_size for maybe-too-wide sizes. (emit_block_cmp_via_loop): Likewise. for gcc/testsuite/ChangeLog PR middle-end/112784 * gcc.target/i386/avx512cd-inline-stringops-pr112784.c: New. --- gcc/expr.cc | 20 +++++++++----------- .../i386/avx512cd-inline-stringops-pr112784.c | 12 ++++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/avx512cd-inline-stringops-pr112784.c diff --git a/gcc/expr.cc b/gcc/expr.cc index 6da51f2aca296..076ba706537aa 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -2449,15 +2449,14 @@ emit_block_move_via_loop (rtx x, rtx y, rtx size, } emit_move_insn (iter, iter_init); - scalar_int_mode int_move_mode - = smallest_int_mode_for_size (incr * BITS_PER_UNIT); - if (GET_MODE_BITSIZE (int_move_mode) != incr * BITS_PER_UNIT) + opt_scalar_int_mode int_move_mode + = int_mode_for_size (incr * BITS_PER_UNIT, 1); + if (!int_move_mode.exists (&move_mode) + || GET_MODE_BITSIZE (int_move_mode.require ()) != incr * BITS_PER_UNIT) { move_mode = BLKmode; gcc_checking_assert (can_move_by_pieces (incr, align)); } - else - move_mode = int_move_mode; x_addr = force_operand (XEXP (x, 0), NULL_RTX); y_addr = force_operand (XEXP (y, 0), NULL_RTX); @@ -2701,16 +2700,15 @@ emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target, iter = gen_reg_rtx (iter_mode); emit_move_insn (iter, iter_init); - scalar_int_mode int_cmp_mode - = smallest_int_mode_for_size (incr * BITS_PER_UNIT); - if (GET_MODE_BITSIZE (int_cmp_mode) != incr * BITS_PER_UNIT - || !can_compare_p (NE, int_cmp_mode, ccp_jump)) + opt_scalar_int_mode int_cmp_mode + = int_mode_for_size (incr * BITS_PER_UNIT, 1); + if (!int_cmp_mode.exists (&cmp_mode) + || GET_MODE_BITSIZE (int_cmp_mode.require ()) != incr * BITS_PER_UNIT + || !can_compare_p (NE, cmp_mode, ccp_jump)) { cmp_mode = BLKmode; gcc_checking_assert (incr != 1); } - else - cmp_mode = int_cmp_mode; /* Save the base addresses. */ x_addr = force_operand (XEXP (x, 0), NULL_RTX); diff --git a/gcc/testsuite/gcc.target/i386/avx512cd-inline-stringops-pr112784.c b/gcc/testsuite/gcc.target/i386/avx512cd-inline-stringops-pr112784.c new file mode 100644 index 0000000000000..c81f99c693c24 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512cd-inline-stringops-pr112784.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-mavx512cd -finline-stringops" } */ + +struct S { + int e; +} __attribute__((aligned(128))); + +int main() { + struct S s1; + struct S s2; + int v = __builtin_memcmp(&s1, &s2, sizeof(s1)); +}