[llvm] [NFC] Add useFPRegsForHalfType(). (PR #74147)
Harald van Dijk via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 11 17:25:15 PST 2024
https://github.com/hvdijk updated https://github.com/llvm/llvm-project/pull/74147
>From 07916dcf5325cc88891d2faf01038aa5b1541c2a Mon Sep 17 00:00:00 2001
From: Harald van Dijk <harald at gigawatt.nl>
Date: Fri, 12 Jan 2024 01:22:56 +0000
Subject: [PATCH] [NFC] Add useFPRegsForHalfType().
Currently, half operations can be promoted in one of two ways.
* If softPromoteHalfType() returns false, fp16 values are passed to and
returned from functions in fp32 registers, and whole chains of fp16
operations are promoted to fp32 in one go.
* If softPromoteHalfType() returns true, fp16 values are passed to and
returned from functions in i16 registers, and individual fp16
operations are promoted to fp32 and the result truncated to fp16 right
away.
The softPromoteHalfType behavior is necessary for correctness, but
changing this for an existing target breaks the ABI. Therefore, this
commit adds a third option:
* If softPromoteHalfType() returns true and useFPRegsForHalfType()
returns true as well, fp16 values are passed to and returned from
functions in fp32 registers, but individual fp16 operations are
promoted to fp32 and the result truncated to fp16 right away.
This change does not yet update any target to make use of it.
---
llvm/include/llvm/CodeGen/TargetLowering.h | 14 ++++++++++----
llvm/lib/CodeGen/TargetLoweringBase.cpp | 13 +++++++++----
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index ed2b513be96086..570f7886374e82 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -505,12 +505,18 @@ class TargetLoweringBase {
return TypePromoteInteger;
}
- // Return true if the half type should be passed around as i16, but promoted
- // to float around arithmetic. The default behavior is to pass around as
- // float and convert around loads/stores/bitcasts and other places where
- // the size matters.
+ // Return true if the half type should be promoted using soft promotion rules
+ // where each operation is promoted to f32 individually, then converted to
+ // fp16. The default behavior is to promote chains of operations, keeping
+ // intermediate results in f32 precision and range.
virtual bool softPromoteHalfType() const { return false; }
+ // Return true if, for soft-promoted half, the half type should be passed
+ // passed to and returned from functions as f32. The default behavior is to
+ // pass as i16. If soft-promoted half is not used, this function is ignored
+ // and values are always passed and returned as f32.
+ virtual bool useFPRegsForHalfType() const { return false; }
+
// There are two general methods for expanding a BUILD_VECTOR node:
// 1. Use SCALAR_TO_VECTOR on the defined scalar values and then shuffle
// them together.
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 7dffd8d4f2b28e..a6b0199f387572 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1430,15 +1430,20 @@ void TargetLoweringBase::computeRegisterProperties(
// conversions).
if (!isTypeLegal(MVT::f16)) {
// Allow targets to control how we legalize half.
- if (softPromoteHalfType()) {
+ bool SoftPromoteHalfType = softPromoteHalfType();
+ bool UseFPRegsForHalfType = !SoftPromoteHalfType || useFPRegsForHalfType();
+
+ if (!UseFPRegsForHalfType) {
NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
- TransformToType[MVT::f16] = MVT::f32;
- ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
} else {
NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
- TransformToType[MVT::f16] = MVT::f32;
+ }
+ TransformToType[MVT::f16] = MVT::f32;
+ if (SoftPromoteHalfType) {
+ ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
+ } else {
ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
}
}
More information about the llvm-commits
mailing list