From patchwork Wed Dec 6 15:48:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwenole Beauchesne X-Patchwork-Id: 174646 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4194718vqy; Wed, 6 Dec 2023 07:49:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGox9lamYi8xv0O4K/4s+8Ys+JGU09FU3xLJqt4Bvw3FDfjjOTENsXEimoFMRJKNrmR0F/h X-Received: by 2002:a54:4715:0:b0:3b8:42c9:dba0 with SMTP id k21-20020a544715000000b003b842c9dba0mr1134424oik.31.1701877745678; Wed, 06 Dec 2023 07:49:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701877745; cv=pass; d=google.com; s=arc-20160816; b=SfUDkHvVaY8ixvXOpIXGrgCtx6X0tGrXl+4yCd1IVbQyxYiRhu/JLL9hODvgkFWNH/ 17LWHSSRM9y5rigxcNLCcLPY98H+pnvuSb8nYCvedKZmK8VkK0nxzD8oGhjSl2a02WW4 18TWI1qXjEoFfgqbldiLId5YJ65jihacPmapHa8eqRe6HoiN6WehfHYzo4POPY6Yqhfx fboQI7zQQYtOe2/p/h/3FO7hsR/QmDU61zBuQjv2XrgQPogggchHnOpicLUI42IA1loH idT3nBa/+SfdJ9eikX8efHohD6+e34tgehD97bgtdaPU2WrzNL7II658Wxfw9PhKUE06 87zA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=s5g1YbYLpKT8PmiLXiBGCf0JCyYDcbSeRwEa6kSnJ0s=; fh=ZuhCKx8SjXRXOmXA+h3tadzh2RTUBpTUfPNEZ0Boa6Y=; b=wB3jLPPw2at+xNc2t83VFjJe1zZx0Rb0dtH1umbNJIKX4Cr9cS7RjpCbuPcUKWV3YA /r8UXUMmnywHCLHKbKII0ZCy9MUxEXIVNJMkFvIQ2xhfbXvqrZz6W6yGbH8r/nS4YjbG 1bbSZLKQwPz3cKq5sd8j8JJ31KQYFWozyWJCUehgN36Q0M0qqTGdpDr1wEKWQxQnK2Iq S6GN98XhNci7Fdc5x+R6tDJ9Dz/SRwMyRMQ5yZTI4E32wW81+YL2kiND5yAlcCuiYdQN OZwXNp9of5xFuPb7S8w8wkVHVJcUp8YXKAo6xxYYuoSnobGkDqgdYoZSwjrEcA2dfHos 2qiw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HOim7P5l; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h15-20020a05620a244f00b0077d7cbd1c38si119111qkn.251.2023.12.06.07.49.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 07:49:05 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HOim7P5l; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6609D3858417 for ; Wed, 6 Dec 2023 15:49:05 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 5B9FE3858D3C for ; Wed, 6 Dec 2023 15:48:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B9FE3858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5B9FE3858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701877722; cv=none; b=sm3N+gH9qO2JXhEM/e3GrLw6LKQl4Ok0Kl44q8JbPvkqj1GcgfrJWA7RNzVSxcHazr1PG4N/mUYnvwLoGaMuTSFV7sFV6iHyAf3TGfdJAiTR3CoSAF1zYPZHgurM1dpAGVKCvQsF+B1+ZWljazh0cp0S1+HxkVR7cmaDfaFnERg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701877722; c=relaxed/simple; bh=A/IQZJHmApxwag+lL/wVMNjZnBe1Mmo7YGc40XYK1kY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Ft0IQnHTEIMS9IKWyZomnUi9QDUGwp8mp686LEYrSdm/szZDKqexMhfGxilJEEynx6JGAP7WaZRi7NHnzJnGMXU9f3zc8kzjAQxuH6lwK4GT0Ym5Z4LK9Gjvqb8bk5S4PJWvcFQ1u06BAyJn1+iEa6WjSWW5ktiP33a/QofHDBs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40c0e7b8a9bso40396495e9.3 for ; Wed, 06 Dec 2023 07:48:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701877717; x=1702482517; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=s5g1YbYLpKT8PmiLXiBGCf0JCyYDcbSeRwEa6kSnJ0s=; b=HOim7P5lvmII48BQsgabutRLIpsC49NaS2kiHry2Egxo1ddsYY/6JzvorOpf69gwwr JbBVXlkNs2w6+K7VUCtPKVKnyBcEopPQEqW02TmLPf6bN0qGnGg1Ycudb/fQJvPyc2UN dLq4mOR342fn01OWPrc3Vjm95VnaKPPamsA7UJ0dDbwmWb0nywpu6eIaW7/u+TlXIqm5 yaS8IGpzOqmoZCjnPWJyvfAtIOyc5bx3Ei+LlX2pgIcfRnMk4quJjD/7XliT18EJNfh7 syJ8ks/HUKjkknOAPtNpLGB4o51x/bRk2WgPomudI5eIcVJzKSggFbWkmT1ghNrEG9u3 M85w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701877717; x=1702482517; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=s5g1YbYLpKT8PmiLXiBGCf0JCyYDcbSeRwEa6kSnJ0s=; b=mOcUwRqw8YSqk9RqcLe1BPWzugM3hROd3Dz7PlGNSQgYC9WXGb9bjGultd2GVxJBau SeVfL1siy6LRPbED+yoDQ1oj1lg0Euwg1SPxOmtoCr3GWQ8HfH0U8dUS/Oq2b/UilACB VV9Y5AEqgp6tkC+EVtZeooMEX7IrmWI5KHKh3rzMYjnJP7lEa4RkPWvxBPEZcZsWqd5u 0mzH7PBc0lCpQn6pkTKJ1Pds/aO1sXEemwBPFfWsIpRfmecL5z+sllM0lvpp7DGrjUWS VtxGjvojXfPyKCvjL1Fv5W7k3QOyJwiwvsN6jqwHG8xtMjuJg7UyF744zSndmxLvEHrN quQA== X-Gm-Message-State: AOJu0YxBinMvOZqF6BVu5ukWTt+GV7/bmc+RySFcGwMB19LEFVLa5INl YJss8pe62oQLsMEc7MNqpc0KrMFuFJVBhw== X-Received: by 2002:a05:600c:1385:b0:40b:5e21:dd3b with SMTP id u5-20020a05600c138500b0040b5e21dd3bmr866463wmf.105.1701877716656; Wed, 06 Dec 2023 07:48:36 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:bf3:71a0:e0cc:77ff:fe8c:d79a]) by smtp.gmail.com with ESMTPSA id jh3-20020a05600ca08300b0040836519dd9sm6492wmb.25.2023.12.06.07.48.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 07:48:36 -0800 (PST) From: Gwenole Beauchesne To: gcc-patches@gcc.gnu.org Cc: Gwenole Beauchesne Subject: [PATCH] c++: Handle '#pragma GCC target optimize' early [PR48026] Date: Wed, 6 Dec 2023 16:48:33 +0100 Message-Id: <20231206154833.2878478-1-gb.devel@gmail.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784548158760187497 X-GMAIL-MSGID: 1784548158760187497 Tested on x86_64-pc-linux-gnu with --enable-languages=c,c++ -- >8 -- Handle '#pragma GCC optimize' earlier as the __OPTIMIZE__ macro may need to be defined as well for certain usages. This is a follow-up to r14-4967-g8697d3a1. Also add more tests for the '#pragma GCC target' case with auto-vectorization enabled and multiple combinations of namespaces and/or class member functions. gcc/c-family/ChangeLog: PR c++/48026 * c-pragma.cc (init-pragma): Register `#pragma GCC optimize' in preprocess-only mode, and enable early handling. gcc/testsuite/ChangeLog: PR c++/48026 PR c++/41201 * g++.target/i386/vect-pragma-target-1.C: New test. * g++.target/i386/vect-pragma-target-2.C: New test. * gcc.target/i386/vect-pragma-target-1.c: New test. * gcc.target/i386/vect-pragma-target-1.c: New test. Signed-off-by: Gwenole Beauchesne --- gcc/c-family/c-pragma.cc | 4 +- .../g++.target/i386/vect-pragma-target-1.C | 6 + .../g++.target/i386/vect-pragma-target-2.C | 6 + .../gcc.target/i386/vect-pragma-target-1.c | 194 ++++++++++++++++++ .../gcc.target/i386/vect-pragma-target-2.c | 7 + 5 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.target/i386/vect-pragma-target-1.C create mode 100644 gcc/testsuite/g++.target/i386/vect-pragma-target-2.C create mode 100644 gcc/testsuite/gcc.target/i386/vect-pragma-target-1.c create mode 100644 gcc/testsuite/gcc.target/i386/vect-pragma-target-2.c diff --git a/gcc/c-family/c-pragma.cc b/gcc/c-family/c-pragma.cc index 849f8ac8c8b..26d4c0c71e0 100644 --- a/gcc/c-family/c-pragma.cc +++ b/gcc/c-family/c-pragma.cc @@ -1852,7 +1852,9 @@ init_pragma (void) c_register_pragma_with_early_handler ("GCC", "target", handle_pragma_target, handle_pragma_target); - c_register_pragma ("GCC", "optimize", handle_pragma_optimize); + c_register_pragma_with_early_handler ("GCC", "optimize", + handle_pragma_optimize, + handle_pragma_optimize); c_register_pragma_with_early_handler ("GCC", "push_options", handle_pragma_push_options, handle_pragma_push_options); diff --git a/gcc/testsuite/g++.target/i386/vect-pragma-target-1.C b/gcc/testsuite/g++.target/i386/vect-pragma-target-1.C new file mode 100644 index 00000000000..2f360cf50e1 --- /dev/null +++ b/gcc/testsuite/g++.target/i386/vect-pragma-target-1.C @@ -0,0 +1,6 @@ +/* { dg-do compile { target { i?86-*-* x86_64-*-* } } } */ +/* { dg-options "-O0" } */ +/* { dg-final { scan-assembler-times "paddd.+xmm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vfmadd132ps.+ymm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vpaddw.+zmm\[0-9]+" 1 } } */ +#include "../../gcc.target/i386/vect-pragma-target-1.c" diff --git a/gcc/testsuite/g++.target/i386/vect-pragma-target-2.C b/gcc/testsuite/g++.target/i386/vect-pragma-target-2.C new file mode 100644 index 00000000000..b85bc93d845 --- /dev/null +++ b/gcc/testsuite/g++.target/i386/vect-pragma-target-2.C @@ -0,0 +1,6 @@ +/* { dg-do compile { target { i?86-*-* x86_64-*-* } } } */ +/* { dg-options "-O0" } */ +/* { dg-final { scan-assembler-times "paddd.+xmm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vfmadd132ps.+ymm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vpaddw.+zmm\[0-9]+" 1 } } */ +#include "../../gcc.target/i386/vect-pragma-target-2.c" diff --git a/gcc/testsuite/gcc.target/i386/vect-pragma-target-1.c b/gcc/testsuite/gcc.target/i386/vect-pragma-target-1.c new file mode 100644 index 00000000000..f5e71e453ec --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vect-pragma-target-1.c @@ -0,0 +1,194 @@ +/* { dg-do compile { target { i?86-*-* x86_64-*-* } } } */ +/* { dg-options "-O0" } */ +/* { dg-final { scan-assembler-times "paddd.+xmm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vfmadd132ps.+ymm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vpaddw.+zmm\[0-9]+" 1 } } */ +#ifndef CHECK_DEFINES +#define CHECK_DEFINES 0 +#endif + +#define N 1024 + +/* Optimization flags and tree vectorizer shall be disabled at this point */ +#if CHECK_DEFINES && defined(__OPTIMIZE__) +#error "__OPTIMIZE__ is defined (not compiled with -O0?)" +#endif + +#pragma GCC push_options +#pragma GCC optimize ("O2", "tree-vectorize") + +/* Optimization flags and tree vectorizer shall be enabled at this point */ +#if CHECK_DEFINES && !defined(__OPTIMIZE__) +#error "__OPTIMIZE__ is not defined" +#endif + +#pragma GCC push_options +#pragma GCC target ("sse4.2") +#ifdef __cplusplus +namespace { +#endif + +/* Target flags up to including SSE4.2 shall be enabled at this point */ +#if CHECK_DEFINES && !defined(__SSE3__) +#error "Target flag (SSE3) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSSE3__) +#error "Target flag (SSSE3) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSE4_1__) +#error "Target flag (SSE4.1) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSE4_2__) +#error "Target flag (SSE4.2) is not defined" +#endif + +void +__attribute__((__noinline__, __used__)) +vec_saxpy_i32(int y[N], const int a[N], const int x[N]) +{ + int i; + for (i = 0; i < N; i++) + y[i] += a[i] * x[i]; +} + +#ifdef __cplusplus +} +#endif +#pragma GCC pop_options + +/* Target flags up to including SSE4.2 shall be disabled at this point */ +#if CHECK_DEFINES && defined(__SSE3__) +#error "Target flag (SSE3) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSSE3__) +#error "Target flag (SSSE3) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSE4_1__) +#error "Target flag (SSE4.1) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSE4_2__) +#error "Target flag (SSE4.2) is still defined" +#endif + +#pragma GCC push_options +#pragma GCC target ("avx2", "fma") +#ifdef __cplusplus +struct A { +#endif + +/* Target flags up to including AVX2+FMA shall be enabled at this point */ +#if CHECK_DEFINES && !defined(__SSE3__) +#error "Target flag (SSE3) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSSE3__) +#error "Target flag (SSSE3) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSE4_1__) +#error "Target flag (SSE4.1) is not defined" +#endif +#if CHECK_DEFINES && !defined(__SSE4_2__) +#error "Target flag (SSE4.2) is not defined" +#endif +#if CHECK_DEFINES && !defined(__AVX__) +#error "Target flag (AVX) is not defined" +#endif +#if CHECK_DEFINES && !defined(__AVX2__) +#error "Target flag (AVX2) is not defined" +#endif +#if CHECK_DEFINES && !defined(__FMA__) +#error "Target flag (FMA) is not defined" +#endif + +void +__attribute__((__noinline__, __used__)) +vec_saxpy_f32(float y[N], const float a[N], const float x[N]) +{ + int i; + for (i = 0; i < N; i++) + y[i] += a[i] * x[i]; +} + +#ifdef __cplusplus +}; +#endif +#pragma GCC pop_options + +/* Target flags up to including AVX2+FMA shall be disabled at this point */ +#if CHECK_DEFINES && defined(__SSE3__) +#error "Target flag (SSE3) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSSE3__) +#error "Target flag (SSSE3) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSE4_1__) +#error "Target flag (SSE4.1) is still defined" +#endif +#if CHECK_DEFINES && defined(__SSE4_2__) +#error "Target flag (SSE4.2) is still defined" +#endif +#if CHECK_DEFINES && defined(__AVX__) +#error "Target flag (AVX) is still defined" +#endif +#if CHECK_DEFINES && defined(__AVX2__) +#error "Target flag (AVX2) is still defined" +#endif +#if CHECK_DEFINES && defined(__FMA__) +#error "Target flag (FMA) is still defined" +#endif + +#pragma GCC push_options +#pragma GCC target ("arch=x86-64-v4") +#ifdef __cplusplus +namespace avx512 { +struct A { +#endif + +/* Essential AVX512 target flags shall be enabled at this point */ +#if CHECK_DEFINES && !defined(__AVX512F__) +#error "Target flag (AVX512F) is not defined" +#endif +#if CHECK_DEFINES && !defined(__AVX512VL__) +#error "Target flag (AVX512VL) is not defined" +#endif +#if CHECK_DEFINES && !defined(__AVX512DQ__) +#error "Target flag (AVX512DQ) is not defined" +#endif +#if CHECK_DEFINES && !defined(__AVX512BW__) +#error "Target flag (AVX512BW) is not defined" +#endif + +void +__attribute__((__noinline__, __used__)) +vec_saxpy_i16(short y[N], const short a[N], const short x[N]) +{ + int i; + for (i = 0; i < N; i++) + y[i] += a[i] * x[i]; +} + +#ifdef __cplusplus +}; +} +#endif +#pragma GCC pop_options + +/* Essential AVX512 target flags shall be disabled at this point */ +#if CHECK_DEFINES && defined(__AVX512F__) +#error "Target flag (AVX512F) is still defined" +#endif +#if CHECK_DEFINES && defined(__AVX512VL__) +#error "Target flag (AVX512VL) is still defined" +#endif +#if CHECK_DEFINES && defined(__AVX512DQ__) +#error "Target flag (AVX512DQ) is still defined" +#endif +#if CHECK_DEFINES && defined(__AVX512BW__) +#error "Target flag (AVX512BW) is still defined" +#endif + +#pragma GCC pop_options + +/* Optimization flags and tree vectorizer shall be disabled at this point */ +#if CHECK_DEFINES && defined(__OPTIMIZE__) +#error "__OPTIMIZE__ is still defined" +#endif diff --git a/gcc/testsuite/gcc.target/i386/vect-pragma-target-2.c b/gcc/testsuite/gcc.target/i386/vect-pragma-target-2.c new file mode 100644 index 00000000000..349680453a4 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/vect-pragma-target-2.c @@ -0,0 +1,7 @@ +/* { dg-do compile { target { i?86-*-* x86_64-*-* } } } */ +/* { dg-options "-O0" } */ +/* { dg-final { scan-assembler-times "paddd.+xmm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vfmadd132ps.+ymm\[0-9]+" 1 } } */ +/* { dg-final { scan-assembler-times "vpaddw.+zmm\[0-9]+" 1 } } */ +#define CHECK_DEFINES 1 +#include "vect-pragma-target-1.c"