[clang] LoongArch fp16,fp128 basic support (PR #68851)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 11 23:10:27 PDT 2023


https://github.com/Xinmudotmoe created https://github.com/llvm/llvm-project/pull/68851

Currently, there are no officially released half-precision and quad-precision floating-point instructions for the LoongArch architecture.
This request aims to ensure that when there are projects that require this functionality, such as ROCM, PyTorch, etc., compilation, testing, and execution produce correct results instead of the current compilation errors.

>From 2ef1ddfb977da35f31f1f351ac4daf0478fff166 Mon Sep 17 00:00:00 2001
From: xinmu <xinmu at xinmu.moe>
Date: Thu, 12 Oct 2023 13:36:34 +0800
Subject: [PATCH] LoongArch fp16,fp128 basic support

---
 clang/lib/Basic/Targets/LoongArch.h                 | 2 ++
 clang/test/CodeGen/fp16-ops.c                       | 1 +
 llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp | 7 +++++++
 3 files changed, 10 insertions(+)

diff --git a/clang/lib/Basic/Targets/LoongArch.h b/clang/lib/Basic/Targets/LoongArch.h
index ba7fb78ab94cd23..b25857207acdedd 100644
--- a/clang/lib/Basic/Targets/LoongArch.h
+++ b/clang/lib/Basic/Targets/LoongArch.h
@@ -40,6 +40,8 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
     SuitableAlign = 128;
     WCharType = SignedInt;
     WIntType = UnsignedInt;
+    HasFloat128 = true;
+    HasFloat16 = true;
   }
 
   bool setCPU(const std::string &Name) override {
diff --git a/clang/test/CodeGen/fp16-ops.c b/clang/test/CodeGen/fp16-ops.c
index 0626e0aaed9d0c0..e230011a59402d4 100644
--- a/clang/test/CodeGen/fp16-ops.c
+++ b/clang/test/CodeGen/fp16-ops.c
@@ -2,6 +2,7 @@
 // RUN: %clang_cc1 -emit-llvm -o - -triple arm-none-linux-gnueabi %s | FileCheck %s --check-prefix=NOTNATIVE --check-prefix=CHECK
 // RUN: %clang_cc1 -emit-llvm -o - -triple aarch64-none-linux-gnueabi %s | FileCheck %s --check-prefix=NOTNATIVE --check-prefix=CHECK
 // RUN: %clang_cc1 -emit-llvm -o - -triple x86_64-linux-gnu %s | FileCheck %s --check-prefix=NOTNATIVE --check-prefix=CHECK
+// RUN: %clang_cc1 -emit-llvm -o - -triple loongarch64-linux-gnu %s | FileCheck %s --check-prefix=NOTNATIVE --check-prefix=CHECK
 // RUN: %clang_cc1 -emit-llvm -o - -triple arm-none-linux-gnueabi -fnative-half-type %s \
 // RUN:   | FileCheck %s --check-prefix=NATIVE-HALF
 // RUN: %clang_cc1 -emit-llvm -o - -triple aarch64-none-linux-gnueabi -fnative-half-type %s \
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 7d01887f24c1597..0295389e1a92e4c 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -214,6 +214,13 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
       setOperationAction(ISD::FRINT, MVT::f64, Legal);
   }
 
+  setOperationAction(ISD::FP16_TO_FP,        MVT::f32,   Expand);
+  setOperationAction(ISD::FP_TO_FP16,        MVT::f32,   Expand);
+  setTruncStoreAction(MVT::f32, MVT::f16, Expand);
+  setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
+  setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
+  setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
+
   // Set operations for 'LSX' feature.
 
   if (Subtarget.hasExtLSX())



More information about the cfe-commits mailing list