From patchwork Mon Oct 17 14:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "J.W. Jagersma" X-Patchwork-Id: 3484 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1489285wrs; Mon, 17 Oct 2022 07:46:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6kQiCzx/wCYSP2AnurjYuZ3YBngyGY/b68lXIKJb+VxFyVYc0Dxdteuo1uuhH1obzR16Ta X-Received: by 2002:a50:ec8f:0:b0:459:b0e0:e030 with SMTP id e15-20020a50ec8f000000b00459b0e0e030mr10688101edr.303.1666017990623; Mon, 17 Oct 2022 07:46:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666017990; cv=none; d=google.com; s=arc-20160816; b=e1vDrciSjQjHImWp7o5rvIuohsOeaAAYS5dXIndovIiK9wvH9xrkCxmrq2AxEdEvf/ 3C2IS3iKIMRr6PyzS5aLFGoY5+LITbJkgSlBW1jTgsDtZnn0N66mPZ1IXVZqOh7swlSI lKhZ844vSmuLDpoxx83Pl0QrX5H9HwtDcxX6hsGIG/O+IV1TWlhst4YM5n88Me0s6LgC IujsTaKDTYI+hR18R8KaJ0BJ/z07f7kZJhOc6wHTuF7P+9HOgf2HTFt6vOL8dxNswOuo tSjQ0JsYC88I6UegaQ0ph2w815VB8nGTaB8vi21yFdA72cPg38Hj1TeG08YKhNegJfqx imvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=16+w4MlFzJOQeFqNBCXxhuVPRNqlvbN8CGLDNpwm0oI=; b=xvKITxi+/hTzrmZEa1okq2REX/pi7TWbC6h6FqbJiksRuj5UBQblgkO8kpcNQBJFYb U1XiHuzi5h50gyrHZqY4O3bY57yNu/CgYuqhmFlWW4CjSJtT4BY2myIQQx+ERMglCmgY MH+oC8aZbvasCVSG2ol4HNBfRlAq/UR2Hm+eAhk2yJFxaBJO8H4e4ouO7YmLWtRg9Gdi SX4/MBIP/EkDCgY359R3NHm/NJfIasVbrlypNe/xAL6hEyBPD74JW2IjpSikhTDkEn6V dwesrgWIK67b8wtLueu56suo52TD6xZeQxVPdgx9RTGWnBqqU90bkq7+aLAIJgZFyBPG 4jrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qnWBs8HN; 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 x18-20020aa7d392000000b00458e0f23902si8361327edq.169.2022.10.17.07.46.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 07:46:30 -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=qnWBs8HN; 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 96EFF3858C83 for ; Mon, 17 Oct 2022 14:46:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 96EFF3858C83 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666017989; bh=16+w4MlFzJOQeFqNBCXxhuVPRNqlvbN8CGLDNpwm0oI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=qnWBs8HNfYh0rlst7fWEXTwAJjBr0EvJs0MjhWxGXrLeEKJGAq4kkNYkon/PoqMfV 6II35/VJb260g3w2e+6AulVzo9LSVSJPWY7zUknVx1n295hRQZUhZBjnYRHYjMDX0H yIf0ncpT7E+qVEyedNcNP1pfPwihEpH1ROo9H3qA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 9807F3858D28 for ; Mon, 17 Oct 2022 14:45:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9807F3858D28 Received: by mail-ej1-x630.google.com with SMTP id d26so25423201eje.10 for ; Mon, 17 Oct 2022 07:45:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=16+w4MlFzJOQeFqNBCXxhuVPRNqlvbN8CGLDNpwm0oI=; b=6Sda+zIGJo5nv4f12H/a9mitUrsjTiarPzq7cd+JV9GMSJSNF6bwdttlfG7BvG/iFi 28K6PdZmuA+tY2pXaxX3uRpz2U/RqiljSZlYghvGyAK6wX2l0dsvuZt45dYHGSwu+CFd IDSoIgWlCH/7ikLETtSydTEgTzn51rsDkdk+n/r22ZcaJsJnzsdsCHOgr5l71jpve9Pz XvNtkq+59La3/WDdEOm+qW5kqi4Yqi6HJammk5amfSO7NBIQIz1xkCjpsvtewXLZtCEi qI23y9ppTX7mxFKPVG8PAVvCk2V1r6ODtvm5egE/4apfWHPfdaJSd1DluWmhg/bfPNFy IBaA== X-Gm-Message-State: ACrzQf28XqbT0pSkxeLToh6KsR4jSLNdNnJvJUFGSzZL45oa7W/StU1K bDK4yHZAsx218IEjGuLS5SyKMVqJGKo= X-Received: by 2002:a17:906:8447:b0:78d:776f:c544 with SMTP id e7-20020a170906844700b0078d776fc544mr9161031ejy.405.1666017938148; Mon, 17 Oct 2022 07:45:38 -0700 (PDT) Received: from server.. (138.sub226.ddfr.nl. [217.27.226.138]) by smtp.gmail.com with ESMTPSA id f19-20020a056402195300b00459cd13fd34sm7507723edz.85.2022.10.17.07.45.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 07:45:37 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] i386: Allow setting target attribute from conditional expression Date: Mon, 17 Oct 2022 16:44:37 +0200 Message-Id: <20221017144437.157424-1-jwjagersma@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-17.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: "J.W. Jagersma via Gcc-patches" From: "J.W. Jagersma" Reply-To: "J.W. Jagersma" Cc: "J.W. Jagersma" 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?1746946480199738018?= X-GMAIL-MSGID: =?utf-8?q?1746946480199738018?= Recently I tried to set a function's target attribute conditionally based on template parameters, eg.: template [[gnu::target (enable_sse ? "sse" : "")]] void func () { /* ... */ } I then discovered that this is currently not possible. This small patch resolves that. A possible alternative solution is to do this globally, eg. in decl_attributes. But doing so would trigger empty-string warnings from handle_target_attribute, and I don't know how safe it is to remove that. There likely isn't much use for this with other attributes, anyway. 2022-10-17 Jan W. Jagersma gcc/ChangeLog: * config/i386/i386-options.cc (ix86_valid_target_attribute_inner_p): Dereference args string from ADDR_EXPR. gcc/testsuite/ChangeLog: * g++.target/i386/target-attr-conditional.C: New test. --- gcc/config/i386/i386-options.cc | 9 ++++ .../g++.target/i386/target-attr-conditional.C | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 gcc/testsuite/g++.target/i386/target-attr-conditional.C diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc index acb2291e70f..915f3b0c1f0 100644 --- a/gcc/config/i386/i386-options.cc +++ b/gcc/config/i386/i386-options.cc @@ -1123,6 +1123,15 @@ ix86_valid_target_attribute_inner_p (tree fndecl, tree args, char *p_strings[], = fndecl == NULL ? UNKNOWN_LOCATION : DECL_SOURCE_LOCATION (fndecl); const char *attr_name = target_clone_attr ? "target_clone" : "target"; + args = tree_strip_nop_conversions (args); + + if (TREE_CODE (args) == ADDR_EXPR) + { + /* Attribute string is given by a constexpr function or conditional + expression. Dereference ADDR_EXPR, operand should be a STRING_CST. */ + args = TREE_OPERAND (args, 0); + } + /* If this is a list, recurse to get the options. */ if (TREE_CODE (args) == TREE_LIST) { diff --git a/gcc/testsuite/g++.target/i386/target-attr-conditional.C b/gcc/testsuite/g++.target/i386/target-attr-conditional.C new file mode 100644 index 00000000000..2d418ed90bf --- /dev/null +++ b/gcc/testsuite/g++.target/i386/target-attr-conditional.C @@ -0,0 +1,53 @@ +/* { dg-do compile } */ +/* { dg-options "-Wno-psabi -m32 -march=i386 -std=c++20" } */ + +#pragma GCC push_options +#pragma GCC target("sse") + +typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__)); +typedef short __v4hi __attribute__ ((__vector_size__ (8))); + +extern __inline int __attribute__((__gnu_inline__, __always_inline__, __artificial__)) +_mm_extract_pi16 (__m64 const __A, int const __N) +{ + return (unsigned short) __builtin_ia32_vec_ext_v4hi ((__v4hi)__A, __N); +} + +#pragma GCC pop_options + +consteval const char* +target_string (bool enable_sse) +{ + return enable_sse ? "sse" : ""; +} + +// Via consteval function +template +[[gnu::target (target_string (enable_sse))]] +int +extract1 (__m64 const src) +{ + if constexpr (enable_sse) + return _mm_extract_pi16 (src, 0); + else + return reinterpret_cast<__v4hi>(src)[1]; +} + +// Via ternary operator +template +[[gnu::target (enable_sse ? "sse" : "")]] +int +extract2 (__m64 const src) +{ + if constexpr (enable_sse) + return _mm_extract_pi16 (src, 2); + else + return reinterpret_cast<__v4hi>(src)[3]; +} + +int +test (__m64 const src) +{ + return extract1(src) + extract1(src) + + extract2(src) + extract2(src); +}