From patchwork Tue Mar 21 17:01:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Matthias Kretz X-Patchwork-Id: 72942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1895149wrt; Tue, 21 Mar 2023 10:02:44 -0700 (PDT) X-Google-Smtp-Source: AK7set/gOJmvFhnQyt9mLt9amrUQLXZQ80rJoJZtesUwOZ2NVnPDfr4Daz2c/sRHXvcKSnbeytKM X-Received: by 2002:a17:906:7cd2:b0:92b:69cd:34c7 with SMTP id h18-20020a1709067cd200b0092b69cd34c7mr3765065ejp.40.1679418164736; Tue, 21 Mar 2023 10:02:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679418164; cv=none; d=google.com; s=arc-20160816; b=VXLIXMOWnxw0+yBq23U6Tpm20cdOesCN5995HiUQFH3/mlEhjvqaQFk2bxyJjNZ4aa kFTbKLi1nmb2Dt8VU74e4m7eEfedwv1BXlN1RsLkivNtJvcHgwKczXUj909SjRUtZ1YA s/+AByHPG4kbnpDWtL3wdSgQf03V+FaAdHNN/lItLLIs5tn0JTzLZw2bN4TitM2xkJ1r jxfa+bCVe9tRxlbEcfQaSGh4Ud5XsZPngHHzTMDGOsuXtXPw8lGsj/tU2UWT0+2DUwwM tUMEwWiqwUh+i3aRtI8YKNwFjUU9KJ2cKkTFu5gaPtJr2qEjkdVGQr3T4PSJoqlL23ek pYug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :organization:message-id:date:subject:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=6miSIw+o6mIroceCaK3IjtVkbNZEysq7tmCuFUxA4MA=; b=xZ7lkDQoMr4ZBXGVAOhv4TgPNN66rRkVJXoCfcUOcfJ/Qd+umrAZJQ/Ml9b2uiXtky VS85JC2GhcgnGdMmGk4EodepAA8t6en68fK0pX4a0pFWJ0O4pnVucQu+/P16sly9k+Cm M4hCPI0UlD5ogavE2U1qNE4qx33y5fnYtCqucgVPjo0cu/ik+Nv2kYAAc/Q/m1lBOdmG PmcbVo05+o0VUCyluv9R5YSmTqlYuDm3aijX5vfplJB0lFsxoWDiVT9/Xqqvj9WH8+WV 3Ns8uw+d9bCLKkod5M0rPzgvbPDfNxSggqMpPfFEeCDp9PBXVpOeDgF9K/yHZbKAqiot GBGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=xsO79F0J; 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=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bj13-20020a170906b04d00b0092606831159si12413997ejb.413.2023.03.21.10.02.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Mar 2023 10:02:44 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=xsO79F0J; 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=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 78B34385558F for ; Tue, 21 Mar 2023 17:02:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 78B34385558F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679418132; bh=6miSIw+o6mIroceCaK3IjtVkbNZEysq7tmCuFUxA4MA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=xsO79F0Jk+EVON0t6Du8ILKu81MiJTcfp6leHF0u9ZtdTM6hLJARLInMotApmCOqV E15+H+2FNZoHXqZq8HTPgyhqLDR03N0NJt2T7VYzx1GyAkY7uWfL5hboGz4GxFayMH 2i6lIBvvzjdaRtq7L20iqlD26ymxWtlpXKYC5w6w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from lxmtout1.gsi.de (lxmtout1.gsi.de [140.181.3.111]) by sourceware.org (Postfix) with ESMTPS id DFFBF3857007; Tue, 21 Mar 2023 17:01:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFFBF3857007 Received: from localhost (localhost [127.0.0.1]) by lxmtout1.gsi.de (Postfix) with ESMTP id BB5EA2051042; Tue, 21 Mar 2023 18:01:23 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lxmtout1.gsi.de Received: from lxmtout1.gsi.de ([127.0.0.1]) by localhost (lxmtout1.gsi.de [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ICwNkixSbgJQ; Tue, 21 Mar 2023 18:01:23 +0100 (CET) Received: from srvEX6.campus.gsi.de (unknown [10.10.4.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lxmtout1.gsi.de (Postfix) with ESMTPS id 9E20B2051040; Tue, 21 Mar 2023 18:01:23 +0100 (CET) Received: from minbar.localnet (140.181.3.12) by srvEX6.campus.gsi.de (10.10.4.96) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Tue, 21 Mar 2023 18:01:23 +0100 To: , Subject: [committed] libstdc++: Fix simd compilation with Clang Date: Tue, 21 Mar 2023 18:01:22 +0100 Message-ID: <27030948.6Emhk5qWAg@minbar> Organization: GSI Helmholtz Centre for Heavy Ion Research In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [140.181.3.12] X-ClientProxiedBy: srvex5.Campus.gsi.de (10.10.4.95) To srvEX6.campus.gsi.de (10.10.4.96) X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, BODY_8BITS, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Matthias Kretz via Gcc-patches From: Matthias Kretz Reply-To: Matthias Kretz Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760968765894033249?= X-GMAIL-MSGID: =?utf-8?q?1760997581471156527?= Slightly modified patch. I had to fix floating-point AVX512 blending on Clang by removing a cast. While at it I cleaned up the -Wundef noise. ----- 8< ------ Clang fails to compile some constant expressions involving simd. Therefore, just disable this non-conforming extension for clang. Fix AVX512 blend implementation for Clang. It was converting the bitmask to bool before, which is obviously wrong. Instead use a Clang builtin to convert the bitmask to vector-mask before using a vector blend ?:. A similar change is required for the masked unary implementation, because the GCC builtins do not exist on Clang. Signed-off-by: Matthias Kretz libstdc++-v3/ChangeLog: * include/experimental/bits/simd_detail.h: Don't declare the simd API as constexpr with Clang. * include/experimental/bits/simd_x86.h (__movm): New. (_S_blend_avx512): Resolve FIXME. Implement blend using __movm and ?:. (_SimdImplX86::_S_masked_unary): Clang does not implement the same builtins. Implement the function using __movm, ?:, and - operators on vector_size types instead. --- .../include/experimental/bits/simd_detail.h | 2 +- .../include/experimental/bits/simd_x86.h | 58 +++++++++++++++++-- 2 files changed, 55 insertions(+), 5 deletions(-) -- ────────────────────────────────────────────────────────────────────────── Dr. Matthias Kretz https://mattkretz.github.io GSI Helmholtz Centre for Heavy Ion Research https://gsi.de stdₓ::simd ────────────────────────────────────────────────────────────────────────── diff --git a/libstdc++-v3/include/experimental/bits/simd_detail.h b/libstdc++-v3/include/experimental/bits/simd_detail.h index 30cc1ef0eef..49b94decf0a 100644 --- a/libstdc++-v3/include/experimental/bits/simd_detail.h +++ b/libstdc++-v3/include/experimental/bits/simd_detail.h @@ -267,7 +267,7 @@ namespace experimental #define _GLIBCXX_SIMD_IS_UNLIKELY(__x) __builtin_expect(__x, 0) #define _GLIBCXX_SIMD_IS_LIKELY(__x) __builtin_expect(__x, 1) -#if defined __STRICT_ANSI__ && __STRICT_ANSI__ +#if __STRICT_ANSI__ || defined __clang__ #define _GLIBCXX_SIMD_CONSTEXPR #define _GLIBCXX_SIMD_USE_CONSTEXPR_API const #else diff --git a/libstdc++-v3/include/experimental/bits/simd_x86.h b/libstdc++-v3/include/experimental/bits/simd_x86.h index 608918542c6..7b8f1c664b3 100644 --- a/libstdc++-v3/include/experimental/bits/simd_x86.h +++ b/libstdc++-v3/include/experimental/bits/simd_x86.h @@ -363,6 +363,53 @@ __maskload_pd(const double* __ptr, _Tp __k) // }}} +#ifdef __clang__ +template + _GLIBCXX_SIMD_INTRINSIC constexpr auto + __movm(_Kp __k) noexcept + { + static_assert(is_unsigned_v<_Kp>); + if constexpr (sizeof(_Tp) == 1 && __have_avx512bw) + { + if constexpr (_Np <= 16 && __have_avx512vl) + return __builtin_ia32_cvtmask2b128(__k); + else if constexpr (_Np <= 32 && __have_avx512vl) + return __builtin_ia32_cvtmask2b256(__k); + else + return __builtin_ia32_cvtmask2b512(__k); + } + else if constexpr (sizeof(_Tp) == 2 && __have_avx512bw) + { + if constexpr (_Np <= 8 && __have_avx512vl) + return __builtin_ia32_cvtmask2w128(__k); + else if constexpr (_Np <= 16 && __have_avx512vl) + return __builtin_ia32_cvtmask2w256(__k); + else + return __builtin_ia32_cvtmask2w512(__k); + } + else if constexpr (sizeof(_Tp) == 4 && __have_avx512dq) + { + if constexpr (_Np <= 4 && __have_avx512vl) + return __builtin_ia32_cvtmask2d128(__k); + else if constexpr (_Np <= 8 && __have_avx512vl) + return __builtin_ia32_cvtmask2d256(__k); + else + return __builtin_ia32_cvtmask2d512(__k); + } + else if constexpr (sizeof(_Tp) == 8 && __have_avx512dq) + { + if constexpr (_Np <= 2 && __have_avx512vl) + return __builtin_ia32_cvtmask2q128(__k); + else if constexpr (_Np <= 4 && __have_avx512vl) + return __builtin_ia32_cvtmask2q256(__k); + else + return __builtin_ia32_cvtmask2q512(__k); + } + else + __assert_unreachable<_Tp>(); + } +#endif // __clang__ + #ifdef _GLIBCXX_SIMD_WORKAROUND_PR85048 #include "simd_x86_conversions.h" #endif @@ -619,14 +666,13 @@ _pdep_u32( _GLIBCXX_SIMD_INTRINSIC static _TV _S_blend_avx512(const _Kp __k, const _TV __a, const _TV __b) noexcept { -#ifdef __clang__ - // FIXME: this does a boolean choice, not a blend - return __k ? __a : __b; -#else static_assert(__is_vector_type_v<_TV>); using _Tp = typename _VectorTraits<_TV>::value_type; static_assert(sizeof(_TV) >= 16); static_assert(sizeof(_Tp) <= 8); +#ifdef __clang__ + return __movm<_VectorTraits<_TV>::_S_full_size, _Tp>(__k) ? __b : __a; +#else using _IntT = conditional_t<(sizeof(_Tp) > 2), conditional_t, @@ -3483,6 +3529,9 @@ _S_masked_unary(const _SimdWrapper<_K, _Np> __k, const _SimdWrapper<_Tp, _Np> __ // optimize masked unary increment and decrement as masked sub +/-1 constexpr int __pm_one = is_same_v<_Op, __increment> ? -1 : 1; +#ifdef __clang__ + return __movm<_Np, _Tp>(__k._M_data) ? __v._M_data - __pm_one : __v._M_data; +#else // __clang__ if constexpr (is_integral_v<_Tp>) { constexpr bool __lp64 = sizeof(long) == sizeof(long long); @@ -3526,6 +3575,7 @@ _S_masked_unary(const _SimdWrapper<_K, _Np> __k, const _SimdWrapper<_Tp, _Np> __ _GLIBCXX_SIMD_MASK_SUB(8, 16, subpd128); #undef _GLIBCXX_SIMD_MASK_SUB } +#endif // __clang__ } else return _Base::template _S_masked_unary<_Op>(__k, __v);