From patchwork Thu May 2 19:44:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1930822 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=iWYYv5La; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VVktH1T7hz20fb for ; Fri, 3 May 2024 05:45:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 68526384AB71 for ; Thu, 2 May 2024 19:45:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 91E673858D20 for ; Thu, 2 May 2024 19:44:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 91E673858D20 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 91E673858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714679088; cv=none; b=v2AFKF0VSXza7nW4koe/uokzxvYRUz9OnsE5Qgi0koau4H/r3rDyV5owh1397lkiIE0AtxxohXzykcCA/2jNOsXyEtjEvUgTBE/wrCqAs702lVdKV72cd/0I2A+CFb8PgRMZwfhs9xhdpuR7GwgtfmISAuJ3Dl3q2VYlgMDiz4Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714679088; c=relaxed/simple; bh=tmitKIOkK5S8r2rA5joY6RnWbPA9HMa9eNmqVemGIpE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KB34eiMHVTFy9mYYtWguAqK+r2PfTxFJpsoj9yC8urQ6uw3A5Ptz3kMWs/w5llrpzUftnJOqZcCbfrNy0ynXDJlxps25zWpmLoqtnFDZiJLJezAOET2FNt0DQWcnWy4fhbBvp9IxPOKPK/BrRtKbxcFDbCqaK+HIqbPnXqe2nxA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6f4472561f1so492719b3a.0 for ; Thu, 02 May 2024 12:44:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714679085; x=1715283885; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=9YrUC77mmcFOGUafadzlE+tfp4oHvL043vAAIbiEbZA=; b=iWYYv5LadWSP1j/IVUw8nBGikL+RjfLFYJATeRTO7+BstrNu3URBZ6OzXXfj38K7b3 y8bqLXV9qGfBZZ38CdJqFwI40L9oE5+A8/ai4g854V5/oz3qBRXI1OzrfOg1HGTQ36R2 6uiK3/W6RoSL8ZkTF9DUnWasg3B+6cCMM2nuU9Fzo3nRAFgHXeTRur+kfKA42tHH7YPI oDXRMjsPZwhwmQ1OfuS5mSbv4we2f/jd0hnuz5sdwsNimE7mTGOUEYTcyd7dSlAedCTo qIsYy2hp68SfDaul8f7cRqrssnXxxjc6fGdLRrhLfzDTun/JWwUic6dPM09GoVgvHD+F KHYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714679085; x=1715283885; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9YrUC77mmcFOGUafadzlE+tfp4oHvL043vAAIbiEbZA=; b=O33RzcS4iKNkT1UJXzvykhlFHo8gzcdlmyFW0BduRKpMOasWJ3o5r4GHw+FvLoZqFY 6pNDnTngTd/Fm7QySIoV4BJ821bu19VoiqXIAV6T5cqcbeJSD9je/pkbh9lQpCNMViH5 ROxqJwFdv+Ulg4qQZCzs+7cwwGKIGAQWfhgNe2gMseFb4C86q19WCnL2jk22mYNW7fyc ic5mKGhxgKAQIzD6pY6m4np/DW/UkcFqrqE/q+y2XcjgRWfihAXhMn97GqiRCY8oMnhU 4E03uVkzRDigweI2B4si8DULRSS+mQdZUhUpufvHBAh/3kWKMwEyMyFbaauVmPBL7d2I Psug== X-Gm-Message-State: AOJu0Yx44//Ga1qA0aDPOOaIj8EepbCoT7kJ5mV3DjhjMJ64lPZ0J6Oj DD4ROz1AYBVWQDSy6RSVPCJ6miWUz55hPP8bnpJkuN5utEmPetfZPvmjEA== X-Google-Smtp-Source: AGHT+IEKFd3xqbaIU6Q6rRHicPGXObgYenvpI7hStO3XE+54Uh27erd/HdjVdfQhVTi7oedLyR9wDA== X-Received: by 2002:a05:6a20:948b:b0:1a7:aa08:4b6c with SMTP id hs11-20020a056a20948b00b001a7aa084b6cmr760720pzb.17.1714679085338; Thu, 02 May 2024 12:44:45 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.56.168.158]) by smtp.gmail.com with ESMTPSA id nv16-20020a17090b1b5000b002a67079c3absm1614204pjb.42.2024.05.02.12.44.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 12:44:44 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id C84FD740151 for ; Thu, 2 May 2024 12:44:43 -0700 (PDT) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Subject: [PATCH] Implement _Float16 to bfloat16 conversion with float32 Date: Thu, 2 May 2024 12:44:43 -0700 Message-ID: <20240502194443.651680-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3022.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Since bfloat16 isn't a subset nor superset of _Float16, implement _Float16 to bfloat16 conversion with _Float16 -> float32 -> bfloat16. gcc/ PR middle-end/114907 * expr.cc (convert_mode_scalar): Implement _Float16 to bfloat16 conversion with float32 conversions. gcc/testsuite/ PR middle-end/114907 * gcc.dg/pr114907.c: New test. * gcc.target/i386/avx512fp16-hf2bf.c: Likewise. --- gcc/expr.cc | 11 +++++++++-- gcc/testsuite/gcc.dg/pr114907.c | 14 ++++++++++++++ .../gcc.target/i386/avx512fp16-hf2bf.c | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr114907.c create mode 100644 gcc/testsuite/gcc.target/i386/avx512fp16-hf2bf.c diff --git a/gcc/expr.cc b/gcc/expr.cc index d4414e242cb..3a599637e34 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -373,10 +373,17 @@ convert_mode_scalar (rtx to, rtx from, int unsignedp) } #ifdef HAVE_SFmode - if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format + /* Since BFmode isn't a subset nor superset of BFmode, implement + HFmode -> BFmode conversion by HFmode -> SFmode -> BFmode + conversions. */ + if ((REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format + || (REAL_MODE_FORMAT (from_mode) == &ieee_half_format + && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format)) && REAL_MODE_FORMAT (SFmode) == &ieee_single_format) { - if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode)) + if ((REAL_MODE_FORMAT (from_mode) == &ieee_half_format + && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format) + || GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode)) { /* To cut down on libgcc size, implement BFmode -> {DF,XF,TF}mode conversions by diff --git a/gcc/testsuite/gcc.dg/pr114907.c b/gcc/testsuite/gcc.dg/pr114907.c new file mode 100644 index 00000000000..2f49fc0bdac --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr114907.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ +/* { dg-options "-O0" } */ +/* { dg-add-options bfloat16 } */ +/* { dg-require-effective-target bfloat16_runtime } */ + +__bf16 bf; +_Float16 f16; + +int +main (void) +{ + bf = f16; + return 0; +} diff --git a/gcc/testsuite/gcc.target/i386/avx512fp16-hf2bf.c b/gcc/testsuite/gcc.target/i386/avx512fp16-hf2bf.c new file mode 100644 index 00000000000..91f387f5af8 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512fp16-hf2bf.c @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-options "-mavx512fp16 -O2 -save-temps" } */ + +__bf16 bf; +_Float16 f16; + +int +main (void) +{ + if (!__builtin_cpu_supports ("avx512fp16")) + return 0; + + bf = f16; + return 0; +} + +/* { dg-final { scan-assembler-times "vcvtsh2ss\[ \\t\]" 1 } } */ +/* { dg-final { scan-assembler-not "__extendhfsf2\[ \\t\]" } } */