From 7a70a2f04d6463013921e9ff1bdad0b02b26b880 Mon Sep 17 00:00:00 2001 From: Jessica Paquette Date: Wed, 11 Nov 2020 13:21:35 -0800 Subject: [PATCH] [AArch64][GlobalISel] Mark G_FCONSTANT as legal when there is full fp16 support When there is full fp16 support, there is no reason to widen 16-bit G_FCONSTANTs to 32 bits. Mark them as legal in this case. Also, we currently import a pattern for materializing a 16-bit 0.0. Add a testcase showing we select it. (All other 16-bit G_FCONSTANTS are not yet selected.) Differential Revision: https://reviews.llvm.org/D89164 --- .../Target/AArch64/GISel/AArch64LegalizerInfo.cpp | 15 ++++++++++++--- .../AArch64/GlobalISel/legalize-fp16-fconstant.mir | 22 ++++++++++++++++++++++ .../GlobalISel/legalizer-info-validation.mir | 4 ++-- .../AArch64/GlobalISel/select-fp16-fconstant.mir | 17 +++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/legalize-fp16-fconstant.mir create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/select-fp16-fconstant.mir diff --git a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp index 8e7186d..efc82ff 100644 --- a/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp +++ b/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp @@ -71,6 +71,11 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST) return; } + // Some instructions only support s16 if the subtarget has full 16-bit FP + // support. + const bool HasFP16 = ST.hasFullFP16(); + const LLT &MinFPScalar = HasFP16 ? s16 : s32; + getActionDefinitionsBuilder({G_IMPLICIT_DEF, G_FREEZE}) .legalFor({p0, s1, s8, s16, s32, s64}) .legalFor(PackedVectorAllTypeList) @@ -323,9 +328,13 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST) .clampScalar(0, s8, s64) .widenScalarToNextPow2(0); getActionDefinitionsBuilder(G_FCONSTANT) - .legalFor({s32, s64}) - .clampScalar(0, s32, s64); - + .legalIf([=](const LegalityQuery &Query) { + const auto &Ty = Query.Types[0]; + if (HasFP16 && Ty == s16) + return true; + return Ty == s32 || Ty == s64; + }) + .clampScalar(0, MinFPScalar, s64); getActionDefinitionsBuilder({G_ICMP, G_FCMP}) .legalFor({{s32, s32}, diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fp16-fconstant.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fp16-fconstant.mir new file mode 100644 index 0000000..685035a --- /dev/null +++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalize-fp16-fconstant.mir @@ -0,0 +1,22 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -mtriple aarch64-unknown-unknown -verify-machineinstrs -run-pass=legalizer %s -o - | FileCheck %s --check-prefix=NO-FP16 +# RUN: llc -mtriple aarch64-unknown-unknown -verify-machineinstrs -mattr=+fullfp16 -run-pass=legalizer %s -o - | FileCheck %s --check-prefix=FP16 + +--- +name: fp16 +tracksRegLiveness: true +body: | + bb.0: + ; NO-FP16-LABEL: name: fp16 + ; NO-FP16: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float 0.000000e+00 + ; NO-FP16: %cst:_(s16) = G_FPTRUNC [[C]](s32) + ; NO-FP16: $h0 = COPY %cst(s16) + ; NO-FP16: RET_ReallyLR implicit $h0 + ; FP16-LABEL: name: fp16 + ; FP16: %cst:_(s16) = G_FCONSTANT half 0xH0000 + ; FP16: $h0 = COPY %cst(s16) + ; FP16: RET_ReallyLR implicit $h0 + %cst:_(s16) = G_FCONSTANT half 0.0 + $h0 = COPY %cst + RET_ReallyLR implicit $h0 + diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir b/llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir index 8ca63a1..a937e33 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir +++ b/llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir @@ -247,8 +247,8 @@ # DEBUG-NEXT: .. the first uncovered type index: 1, OK # DEBUG-NEXT: .. the first uncovered imm index: 0, OK # DEBUG-NEXT: G_FCONSTANT (opcode {{[0-9]+}}): 1 type index, 0 imm indices -# DEBUG-NEXT: .. the first uncovered type index: 1, OK -# DEBUG-NEXT: .. the first uncovered imm index: 0, OK +# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected +# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected # DEBUG-NEXT: G_VASTART (opcode {{[0-9]+}}): 1 type index, 0 imm indices # DEBUG-NEXT: .. the first uncovered type index: 1, OK # DEBUG-NEXT: .. the first uncovered imm index: 0, OK diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/select-fp16-fconstant.mir b/llvm/test/CodeGen/AArch64/GlobalISel/select-fp16-fconstant.mir new file mode 100644 index 0000000..59ee5bb --- /dev/null +++ b/llvm/test/CodeGen/AArch64/GlobalISel/select-fp16-fconstant.mir @@ -0,0 +1,17 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -mtriple=aarch64-unknown-unknown -mattr=+fullfp16 -run-pass=instruction-select -verify-machineinstrs %s -o - | FileCheck %s + +--- +name: positive_zero +legalized: true +regBankSelected: true +tracksRegLiveness: true +body: | + bb.0: + ; CHECK-LABEL: name: positive_zero + ; CHECK: [[FMOVH0_:%[0-9]+]]:fpr16 = FMOVH0 + ; CHECK: $h0 = COPY [[FMOVH0_]] + ; CHECK: RET_ReallyLR implicit $h0 + %0:fpr(s16) = G_FCONSTANT half 0.0 + $h0 = COPY %0(s16) + RET_ReallyLR implicit $h0 -- 2.7.4