https://github.com/quic-akaryaki created https://github.com/llvm/llvm-project/pull/243572
Add HMX intrinsics, builtins, wrappers, and preprocessor defines for HMX. HMX activate and weight instructions collectively encode a matrix multiply operation, they must be packetized together. This change includes the following to implement this requirement: - HexagonMergeActivateWeight pass tries to move the activation and weight instructions together by moving one of them across any instructions between them. It can't move across calls, inline asm blocks, barriers, terminators, instructions with side effects or ordered-memory ones, as well as instructions with conflicting register defines. Currently, the pass does not use aliasing information, so it can't move across any stores either. If the pair cannot be placed together, a warning is emitted but the machine code is not changed. The pass runs twice: once after the machine scheduler and once just before the packetizer to handle the case when pairs are split later. - HMXActivateWeightMutation is added to the pre-RA, post-RA, swing modulo (pipeliner) and packetizer schedulers. It tries to keep activation and weight instructions together by setting the latency between them to 0 and giving them the same depth. - HexagonHazardRecognizer delays starting a new pair and holds the other half until after the first one is placed. - Packetizer inserts the pair in one packet. - Pipeliner keeps activate/weight instructions, and everything they depend on, out of software pipelining. The merge pass is anchored to the MachineScheduler pass with optimized register allocation and to PHIElimination pass with fast register allocation. Co-authored-by: Brendon Cahoon <[email protected]> Co-authored-by: Harsha Jagasia <[email protected]> Co-authored-by: Ikhlas Ajbar <[email protected]> Co-authored-by: Krzysztof Parzyszek <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Change-Id: Iddb4542194cc72242fb6b1cd336ff23fc1de35d1 >From f8a15fdfee3994f757cc6ff7c5d97c7a6a015b97 Mon Sep 17 00:00:00 2001 From: Alexey Karyakin <[email protected]> Date: Wed, 28 Oct 2026 14:59:30 -0700 Subject: [PATCH] [Hexagon] HMX intrinsics, builtins, and the activate/weight merging Add HMX intrinsics, builtins, wrappers, and preprocessor defines for HMX. HMX activate and weight instructions collectively encode a matrix multiply operation, they must be packetized together. This change includes the following to implement this requirement: - HexagonMergeActivateWeight pass tries to move the activation and weight instructions together by moving one of them across any instructions between them. It can't move across calls, inline asm blocks, barriers, terminators, instructions with side effects or ordered-memory ones, as well as instructions with conflicting register defines. Currently, the pass does not use aliasing information, so it can't move across any stores either. If the pair cannot be placed together, a warning is emitted but the machine code is not changed. The pass runs twice: once after the machine scheduler and once just before the packetizer to handle the case when pairs are split later. - HMXActivateWeightMutation is added to the pre-RA, post-RA, swing modulo (pipeliner) and packetizer schedulers. It tries to keep activation and weight instructions together by setting the latency between them to 0 and giving them the same depth. - HexagonHazardRecognizer delays starting a new pair and holds the other half until after the first one is placed. - Packetizer inserts the pair in one packet. - Pipeliner keeps activate/weight instructions, and everything they depend on, out of software pipelining. The merge pass is anchored to the MachineScheduler pass with optimized register allocation and to PHIElimination pass with fast register allocation. Co-authored-by: Brendon Cahoon <[email protected]> Co-authored-by: Harsha Jagasia <[email protected]> Co-authored-by: Ikhlas Ajbar <[email protected]> Co-authored-by: Krzysztof Parzyszek <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Change-Id: Iddb4542194cc72242fb6b1cd336ff23fc1de35d1 --- .../include/clang/Basic/BuiltinsHexagonDep.td | 16 ++ clang/lib/Basic/Targets/Hexagon.cpp | 10 + clang/lib/Basic/Targets/Hexagon.h | 1 + clang/lib/Headers/CMakeLists.txt | 1 + clang/lib/Headers/hmx_hexagon_protos.h | 111 +++++++++ clang/test/CodeGen/builtins-hexagon-hmx.c | 64 +++++ clang/test/Driver/hexagon-hmx.c | 12 +- clang/test/Headers/hexagon-hmx-headers.c | 65 +++++ clang/test/Preprocessor/hexagon-predefines.c | 25 ++ llvm/include/llvm/IR/IntrinsicsHexagonDep.td | 95 +++++-- llvm/lib/Target/Hexagon/CMakeLists.txt | 1 + llvm/lib/Target/Hexagon/Hexagon.h | 5 + .../Target/Hexagon/HexagonDepMapAsm2Intrin.td | 26 ++ .../Hexagon/HexagonHazardRecognizer.cpp | 67 ++++- .../Target/Hexagon/HexagonHazardRecognizer.h | 15 ++ llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp | 17 +- llvm/lib/Target/Hexagon/HexagonInstrInfo.h | 4 + .../Hexagon/HexagonMergeActivateWeight.cpp | 227 +++++++++++++++++ llvm/lib/Target/Hexagon/HexagonSubtarget.cpp | 89 +++++++ llvm/lib/Target/Hexagon/HexagonSubtarget.h | 3 + .../Target/Hexagon/HexagonTargetMachine.cpp | 14 ++ .../Target/Hexagon/HexagonVLIWPacketizer.cpp | 28 +++ .../CodeGen/Hexagon/hmx-act-wei-adjacent.ll | 41 +++ .../CodeGen/Hexagon/hmx-act-wei-attributes.ll | 45 ++++ .../Hexagon/hmx-act-wei-hazard-deadlock.mir | 100 ++++++++ .../Hexagon/hmx-act-wei-memory-effects.ll | 33 +++ .../Hexagon/hmx-act-wei-pairing-errors.ll | 83 +++++++ .../CodeGen/Hexagon/intrinsics/v68-hmx.ll | 68 +++++ .../CodeGen/Hexagon/intrinsics/v73-hmx.ll | 23 ++ .../CodeGen/Hexagon/isRestNoSlot1Store.ll | 36 +++ .../CodeGen/Hexagon/merge-activate-weight.ll | 25 ++ llvm/test/CodeGen/Hexagon/pureslot0.ll | 36 +++ .../test/CodeGen/Hexagon/update-reg-states.ll | 233 ++++++++++++++++++ 33 files changed, 1585 insertions(+), 34 deletions(-) create mode 100644 clang/lib/Headers/hmx_hexagon_protos.h create mode 100644 clang/test/CodeGen/builtins-hexagon-hmx.c create mode 100644 clang/test/Headers/hexagon-hmx-headers.c create mode 100644 llvm/lib/Target/Hexagon/HexagonMergeActivateWeight.cpp create mode 100644 llvm/test/CodeGen/Hexagon/hmx-act-wei-adjacent.ll create mode 100644 llvm/test/CodeGen/Hexagon/hmx-act-wei-attributes.ll create mode 100644 llvm/test/CodeGen/Hexagon/hmx-act-wei-hazard-deadlock.mir create mode 100644 llvm/test/CodeGen/Hexagon/hmx-act-wei-memory-effects.ll create mode 100644 llvm/test/CodeGen/Hexagon/hmx-act-wei-pairing-errors.ll create mode 100644 llvm/test/CodeGen/Hexagon/intrinsics/v68-hmx.ll create mode 100644 llvm/test/CodeGen/Hexagon/intrinsics/v73-hmx.ll create mode 100644 llvm/test/CodeGen/Hexagon/isRestNoSlot1Store.ll create mode 100644 llvm/test/CodeGen/Hexagon/merge-activate-weight.ll create mode 100644 llvm/test/CodeGen/Hexagon/pureslot0.ll create mode 100644 llvm/test/CodeGen/Hexagon/update-reg-states.ll diff --git a/clang/include/clang/Basic/BuiltinsHexagonDep.td b/clang/include/clang/Basic/BuiltinsHexagonDep.td index 9f73356d58e31f..5805cd125ab4e0 100644 --- a/clang/include/clang/Basic/BuiltinsHexagonDep.td +++ b/clang/include/clang/Basic/BuiltinsHexagonDep.td @@ -2314,3 +2314,19 @@ let Features = HVXV83.Features in { def V6_vsubuwwsat_dv : HexagonBuiltin<"_Vector<32, int>(_Vector<32, int>, _Vector<32, int>)">; def V6_vsubuwwsat_dv_128B : HexagonBuiltin<"_Vector<64, int>(_Vector<64, int>, _Vector<64, int>)">; } + +let Features = V68.Features in { + def M8_mxclracc_hf : HexagonBuiltin<"void()">; + def M8_mxmem2_bias : HexagonBuiltin<"void(void *)">; + def M8_mxmem2_st_bias : HexagonBuiltin<"void(void *)">; + def M8_mxmem_blk_sm_act_hf : HexagonBuiltin<"void(unsigned int, int)">; + def M8_mxmem_sm_act_hf : HexagonBuiltin<"void(unsigned int, int)">; + def M8_mxmem_wei_hf : HexagonBuiltin<"void(unsigned int, int)">; + def M8_mxmemdp_wei_hf : HexagonBuiltin<"void(unsigned int, int)">; + def M8_mxmems_blk_sm_act_hf : HexagonBuiltin<"void(unsigned int, int)">; +} + +let Features = V73.Features in { + def M8_cvt_rs_hf : HexagonBuiltin<"void(int)">; + def M8_mxmem : HexagonBuiltin<"void(void *, int)">; +} diff --git a/clang/lib/Basic/Targets/Hexagon.cpp b/clang/lib/Basic/Targets/Hexagon.cpp index 3355c96fa83efd..b4c6ac0be1ccf3 100644 --- a/clang/lib/Basic/Targets/Hexagon.cpp +++ b/clang/lib/Basic/Targets/Hexagon.cpp @@ -60,6 +60,11 @@ void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts, if (HasHVXIeeeFp) Builder.defineMacro("__HVX_IEEE_FP__"); + if (hasFeature("hmx")) { + Builder.defineMacro("__HMX__"); + Builder.defineMacro("__HMX_ARCH__", ArchVersion); + } + if (hasFeature("hlx")) { Builder.defineMacro("__HLX__"); Builder.defineMacro("__HLX_ARCH__", "85"); @@ -123,6 +128,10 @@ bool HexagonTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, HasHLX = true; else if (F == "-hlx") HasHLX = false; + else if (F == "+hmx") + HasHMX = true; + else if (F == "-hmx") + HasHMX = false; } if (CPU.compare("hexagonv68") >= 0) { HasFastHalfType = true; @@ -218,6 +227,7 @@ bool HexagonTargetInfo::hasFeature(StringRef Feature) const { .Case("hvx-ieee-fp", HasHVXIeeeFp) .Case("long-calls", UseLongCalls) .Case("audio", HasAudio) + .Case("hmx", HasHMX) .Case("hlx", HasHLX) .Default(false); } diff --git a/clang/lib/Basic/Targets/Hexagon.h b/clang/lib/Basic/Targets/Hexagon.h index facbddb81ddc92..635befa16f1b27 100644 --- a/clang/lib/Basic/Targets/Hexagon.h +++ b/clang/lib/Basic/Targets/Hexagon.h @@ -36,6 +36,7 @@ class LLVM_LIBRARY_VISIBILITY HexagonTargetInfo : public TargetInfo { bool HasAudio = false; bool HasHLX = false; bool UseLongCalls = false; + bool HasHMX = false; public: HexagonTargetInfo(const llvm::Triple &Triple, const TargetOptions &) diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt index 6ea15247597101..1c278db41bf010 100644 --- a/clang/lib/Headers/CMakeLists.txt +++ b/clang/lib/Headers/CMakeLists.txt @@ -80,6 +80,7 @@ set(hexagon_files hexagon_types.h hlx_hexagon_protos.h hvx_hexagon_protos.h + hmx_hexagon_protos.h ) set(hip_files diff --git a/clang/lib/Headers/hmx_hexagon_protos.h b/clang/lib/Headers/hmx_hexagon_protos.h new file mode 100644 index 00000000000000..6bc73c8cf8e387 --- /dev/null +++ b/clang/lib/Headers/hmx_hexagon_protos.h @@ -0,0 +1,111 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// Automatically generated file, do not edit! +//===----------------------------------------------------------------------===// + +#ifndef _HMX_HEXAGON_PROTOS_H_ +#define _HMX_HEXAGON_PROTOS_H_ 1 + +#ifdef __HMX__ +/* ========================================================================== + Assembly Syntax: mxclracc.hf + C Intrinsic Prototype: void Q6_mxclracc_hf() + Instruction Type: ST + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_mxclracc_hf __builtin_HEXAGON_M8_mxclracc_hf + +/* ========================================================================== + Assembly Syntax: bias=mxmem2(Rs32) + C Intrinsic Prototype: void Q6_bias_mxmem2_A(Address Rs) + Instruction Type: LD + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_bias_mxmem2_A __builtin_HEXAGON_M8_mxmem2_bias + +/* ========================================================================== + Assembly Syntax: mxmem2(Rs32)=bias + C Intrinsic Prototype: void Q6_mxmem2_bias_A(Address Rs) + Instruction Type: ST + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_mxmem2_bias_A __builtin_HEXAGON_M8_mxmem2_st_bias + +/* ========================================================================== + Assembly Syntax: activation.hf=mxmem(Rs32,Rt32) + C Intrinsic Prototype: void Q6_activation_hf_mxmem_RR(UWord32 Rs, Word32 Rt) + Instruction Type: LD + Execution Slots: SLOT01 + ========================================================================== */ + +#define Q6_activation_hf_mxmem_RR __builtin_HEXAGON_M8_mxmem_blk_sm_act_hf + +/* ========================================================================== + Assembly Syntax: activation.hf=mxmem(Rs32,Rt32):deep + C Intrinsic Prototype: void Q6_activation_hf_mxmem_RR_deep(UWord32 Rs, Word32 Rt) + Instruction Type: LD + Execution Slots: SLOT01 + ========================================================================== */ + +#define Q6_activation_hf_mxmem_RR_deep __builtin_HEXAGON_M8_mxmem_sm_act_hf + +/* ========================================================================== + Assembly Syntax: weight.hf=mxmem(Rs32,Rt32) + C Intrinsic Prototype: void Q6_weight_hf_mxmem_RR(UWord32 Rs, Word32 Rt) + Instruction Type: LD + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_weight_hf_mxmem_RR __builtin_HEXAGON_M8_mxmem_wei_hf + +/* ========================================================================== + Assembly Syntax: weight.hf=mxmem(Rs32,Rt32):deep + C Intrinsic Prototype: void Q6_weight_hf_mxmem_RR_deep(UWord32 Rs, Word32 Rt) + Instruction Type: LD + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_weight_hf_mxmem_RR_deep __builtin_HEXAGON_M8_mxmemdp_wei_hf + +/* ========================================================================== + Assembly Syntax: activation.hf=mxmem(Rs32,Rt32):single + C Intrinsic Prototype: void Q6_activation_hf_mxmem_RR_single(UWord32 Rs, Word32 Rt) + Instruction Type: LD + Execution Slots: SLOT01 + ========================================================================== */ + +#define Q6_activation_hf_mxmem_RR_single __builtin_HEXAGON_M8_mxmems_blk_sm_act_hf + +#if __HMX_ARCH__ >= 73 +/* ========================================================================== + Assembly Syntax: cvt.hf=acc(Rs32) + C Intrinsic Prototype: void Q6_cvt_hf_acc_R(Word32 Rs) + Instruction Type: ST + Execution Slots: SLOT02 + ========================================================================== */ + +#define Q6_cvt_hf_acc_R __builtin_HEXAGON_M8_cvt_rs_hf +#endif + +#if __HMX_ARCH__ >= 73 +/* ========================================================================== + Assembly Syntax: mxmem(Rs32,Rt32)=cvt + C Intrinsic Prototype: void Q6_mxmem_cvt_RR(Word32 Rs, Word32 Rt) + Instruction Type: ST + Execution Slots: SLOT0 + ========================================================================== */ + +#define Q6_mxmem_cvt_RR __builtin_HEXAGON_M8_mxmem +#endif + +#endif /* __HMX__ */ + +#endif diff --git a/clang/test/CodeGen/builtins-hexagon-hmx.c b/clang/test/CodeGen/builtins-hexagon-hmx.c new file mode 100644 index 00000000000000..5708b7af7901db --- /dev/null +++ b/clang/test/CodeGen/builtins-hexagon-hmx.c @@ -0,0 +1,64 @@ +// RUN: %clang_cc1 -triple hexagon -target-cpu hexagonv73 -target-feature +hmx \ +// RUN: -emit-llvm -o - %s | FileCheck %s + +// Test that each HMX builtin is lowered to the corresponding LLVM intrinsic. + +// CHECK-LABEL: @test_M8_mxclracc_hf +// CHECK: call void @llvm.hexagon.M8.mxclracc.hf() +void test_M8_mxclracc_hf(void) { + __builtin_HEXAGON_M8_mxclracc_hf(); +} + +// CHECK-LABEL: @test_M8_mxmem2_bias +// CHECK: call void @llvm.hexagon.M8.mxmem2.bias(ptr %{{[0-9]+}}) +void test_M8_mxmem2_bias(void *a) { + __builtin_HEXAGON_M8_mxmem2_bias(a); +} + +// CHECK-LABEL: @test_M8_mxmem2_st_bias +// CHECK: call void @llvm.hexagon.M8.mxmem2.st.bias(ptr %{{[0-9]+}}) +void test_M8_mxmem2_st_bias(void *a) { + __builtin_HEXAGON_M8_mxmem2_st_bias(a); +} + +// CHECK-LABEL: @test_M8_mxmem_blk_sm_act_hf +// CHECK: call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmem_blk_sm_act_hf(unsigned int a, int b) { + __builtin_HEXAGON_M8_mxmem_blk_sm_act_hf(a, b); +} + +// CHECK-LABEL: @test_M8_mxmem_sm_act_hf +// CHECK: call void @llvm.hexagon.M8.mxmem.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmem_sm_act_hf(unsigned int a, int b) { + __builtin_HEXAGON_M8_mxmem_sm_act_hf(a, b); +} + +// CHECK-LABEL: @test_M8_mxmem_wei_hf +// CHECK: call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmem_wei_hf(unsigned int a, int b) { + __builtin_HEXAGON_M8_mxmem_wei_hf(a, b); +} + +// CHECK-LABEL: @test_M8_mxmemdp_wei_hf +// CHECK: call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmemdp_wei_hf(unsigned int a, int b) { + __builtin_HEXAGON_M8_mxmemdp_wei_hf(a, b); +} + +// CHECK-LABEL: @test_M8_mxmems_blk_sm_act_hf +// CHECK: call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmems_blk_sm_act_hf(unsigned int a, int b) { + __builtin_HEXAGON_M8_mxmems_blk_sm_act_hf(a, b); +} + +// CHECK-LABEL: @test_M8_cvt_rs_hf +// CHECK: call void @llvm.hexagon.M8.cvt.rs.hf(i32 %{{[0-9]+}}) +void test_M8_cvt_rs_hf(int a) { + __builtin_HEXAGON_M8_cvt_rs_hf(a); +} + +// CHECK-LABEL: @test_M8_mxmem +// CHECK: call void @llvm.hexagon.M8.mxmem(ptr %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_M8_mxmem(void *a, int b) { + __builtin_HEXAGON_M8_mxmem(a, b); +} diff --git a/clang/test/Driver/hexagon-hmx.c b/clang/test/Driver/hexagon-hmx.c index 2422e64241ef11..4d73a94bc44ad4 100644 --- a/clang/test/Driver/hexagon-hmx.c +++ b/clang/test/Driver/hexagon-hmx.c @@ -1,23 +1,23 @@ // Test the hmx flag. -// RUN: %clang -target hexagon -### %s 2>&1 \ +// RUN: %clang --target hexagon -### %s 2>&1 \ // RUN: | FileCheck %s -check-prefix CHECK-DEFAULT -// RUN: %clang -target hexagon -### -mhmx %s 2>&1 \ +// RUN: %clang --target hexagon -### -mhmx %s 2>&1 \ // RUN: | FileCheck %s -check-prefix CHECK-HMX -// RUN: %clang -target hexagon -### -mhmx -mno-hmx %s 2>&1 \ +// RUN: %clang --target hexagon -### -mhmx -mno-hmx %s 2>&1 \ // RUN: | FileCheck %s -check-prefix CHECK-NO-HMX -// RUN: %clang -c %s -### -target hexagon -mhmx \ +// RUN: %clang -c %s -### --target hexagon -mhmx \ // RUN: -fno-integrated-as 2>&1 | FileCheck \ // RUN: -check-prefix=CHECK-NOIAS-HMX %s -// RUN: %clang -c %s -### -target hexagon -mno-hmx \ +// RUN: %clang -c %s -### --target hexagon -mno-hmx \ // RUN: -fno-integrated-as 2>&1 | FileCheck \ // RUN: -check-prefix=CHECK-NOIAS-NO-HMX %s // -// RUN: %clang -target hexagon --help 2>&1 \ +// RUN: %clang --target hexagon --help 2>&1 \ // RUN: | FileCheck %s -check-prefix CHECK-HELP // CHECK-DEFAULT-NOT: "-target-feature" "+hmx" diff --git a/clang/test/Headers/hexagon-hmx-headers.c b/clang/test/Headers/hexagon-hmx-headers.c new file mode 100644 index 00000000000000..6844f17abb2c19 --- /dev/null +++ b/clang/test/Headers/hexagon-hmx-headers.c @@ -0,0 +1,65 @@ +// RUN: %clang_cc1 -O0 -internal-isystem %S/../../lib/Headers/ \ +// RUN: -target-cpu hexagonv79 -triple hexagon-unknown-elf \ +// RUN: -target-feature +hmx -emit-llvm -o - %s | FileCheck %s + +#include <hmx_hexagon_protos.h> + +// CHECK-LABEL: @test_Q6_mxclracc_hf +// CHECK: call void @llvm.hexagon.M8.mxclracc.hf() +void test_Q6_mxclracc_hf(void) { + Q6_mxclracc_hf(); +} + +// CHECK-LABEL: @test_Q6_bias_mxmem2_A +// CHECK: call void @llvm.hexagon.M8.mxmem2.bias(ptr %{{[0-9]+}}) +void test_Q6_bias_mxmem2_A(void *rs) { + Q6_bias_mxmem2_A(rs); +} + +// CHECK-LABEL: @test_Q6_mxmem2_bias_A +// CHECK: call void @llvm.hexagon.M8.mxmem2.st.bias(ptr %{{[0-9]+}}) +void test_Q6_mxmem2_bias_A(void *rs) { + Q6_mxmem2_bias_A(rs); +} + +// CHECK-LABEL: @test_Q6_activation_hf_mxmem_RR( +// CHECK: call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_activation_hf_mxmem_RR(unsigned int rs, int rt) { + Q6_activation_hf_mxmem_RR(rs, rt); +} + +// CHECK-LABEL: @test_Q6_activation_hf_mxmem_RR_deep( +// CHECK: call void @llvm.hexagon.M8.mxmem.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_activation_hf_mxmem_RR_deep(unsigned int rs, int rt) { + Q6_activation_hf_mxmem_RR_deep(rs, rt); +} + +// CHECK-LABEL: @test_Q6_weight_hf_mxmem_RR( +// CHECK: call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_weight_hf_mxmem_RR(unsigned int rs, int rt) { + Q6_weight_hf_mxmem_RR(rs, rt); +} + +// CHECK-LABEL: @test_Q6_weight_hf_mxmem_RR_deep( +// CHECK: call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_weight_hf_mxmem_RR_deep(unsigned int rs, int rt) { + Q6_weight_hf_mxmem_RR_deep(rs, rt); +} + +// CHECK-LABEL: @test_Q6_activation_hf_mxmem_RR_single( +// CHECK: call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_activation_hf_mxmem_RR_single(unsigned int rs, int rt) { + Q6_activation_hf_mxmem_RR_single(rs, rt); +} + +// CHECK-LABEL: @test_Q6_cvt_hf_acc_R( +// CHECK: call void @llvm.hexagon.M8.cvt.rs.hf(i32 %{{[0-9]+}}) +void test_Q6_cvt_hf_acc_R(int rs) { + Q6_cvt_hf_acc_R(rs); +} + +// CHECK-LABEL: @test_Q6_mxmem_cvt_RR( +// CHECK: call void @llvm.hexagon.M8.mxmem(ptr %{{[0-9]+}}, i32 %{{[0-9]+}}) +void test_Q6_mxmem_cvt_RR(void *rs, int rt) { + Q6_mxmem_cvt_RR(rs, rt); +} diff --git a/clang/test/Preprocessor/hexagon-predefines.c b/clang/test/Preprocessor/hexagon-predefines.c index 29718cf38dc2b9..14e90ee4cec5d5 100644 --- a/clang/test/Preprocessor/hexagon-predefines.c +++ b/clang/test/Preprocessor/hexagon-predefines.c @@ -120,6 +120,11 @@ // CHECK-V68HVX-IEEE-FP: #define __HVX__ 1 // CHECK-V68HVX-IEEE-FP: #define __hexagon__ 1 +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv68 \ +// RUN: -target-feature +hmx %s | FileCheck %s -check-prefix CHECK-HMX +// CHECK-HMX: #define __HMX_ARCH__ 68 +// CHECK-HMX: #define __HMX__ 1 + // RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv69 \ // RUN: -target-feature +hvxv69 -target-feature +hvx-length128b %s | FileCheck \ // RUN: %s -check-prefix CHECK-V69HVX-128B @@ -150,6 +155,11 @@ // CHECK-V73HVX-128B: #define __HVX__ 1 // CHECK-V73HVX-128B: #define __hexagon__ 1 +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv73 \ +// RUN: -target-feature +hmx %s | FileCheck %s -check-prefix CHECK-HMXV73 +// CHECK-HMXV73: #define __HMX_ARCH__ 73 +// CHECK-HMXV73: #define __HMX__ 1 + // RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv75 %s\ // RUN: | FileCheck %s -check-prefix CHECK-V75 // CHECK-V75: #define __HEXAGON_ARCH__ 75 @@ -167,6 +177,11 @@ // CHECK-V75HVX-128B: #define __HVX__ 1 // CHECK-V75HVX-128B: #define __hexagon__ 1 +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv75 \ +// RUN: -target-feature +hmx %s | FileCheck %s -check-prefix CHECK-HMXV75 +// CHECK-HMXV75: #define __HMX_ARCH__ 75 +// CHECK-HMXV75: #define __HMX__ 1 + // RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv79 %s\ // RUN: | FileCheck %s -check-prefix CHECK-V79 // CHECK-V79: #define __HEXAGON_ARCH__ 79 @@ -184,6 +199,11 @@ // CHECK-V79HVX-128B: #define __HVX__ 1 // CHECK-V79HVX-128B: #define __hexagon__ 1 +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv79 \ +// RUN: -target-feature +hmx %s | FileCheck %s -check-prefix CHECK-HMXV79 +// CHECK-HMXV79: #define __HMX_ARCH__ 79 +// CHECK-HMXV79: #define __HMX__ 1 + // RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv81 %s\ // RUN: | FileCheck %s -check-prefix CHECK-V81 // CHECK-V81: #define __HEXAGON_ARCH__ 81 @@ -191,6 +211,11 @@ // CHECK-V81: #define __HEXAGON_V81__ 1 // CHECK-V81: #define __hexagon__ 1 +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv81 \ +// RUN: -target-feature +hmx %s | FileCheck %s -check-prefix CHECK-HMXV81 +// CHECK-HMXV81: #define __HMX_ARCH__ 81 +// CHECK-HMXV81: #define __HMX__ 1 + // RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -target-cpu hexagonv81 \ // RUN: -target-feature +hvxv81 -target-feature +hvx-length128b %s | FileCheck \ // RUN: %s -check-prefix CHECK-V81HVX-128B diff --git a/llvm/include/llvm/IR/IntrinsicsHexagonDep.td b/llvm/include/llvm/IR/IntrinsicsHexagonDep.td index c5596f68c670c0..6cb2ffd7a8d69f 100644 --- a/llvm/include/llvm/IR/IntrinsicsHexagonDep.td +++ b/llvm/include/llvm/IR/IntrinsicsHexagonDep.td @@ -515,6 +515,41 @@ class Hexagon_i64_i64i32i32_Intrinsic<string GCCIntSuffix, [llvm_i64_ty], [llvm_i64_ty,llvm_i32_ty,llvm_i32_ty], intr_properties>; +// tag : M8_cvt_rs_hf +class Hexagon__i32_Intrinsic<string GCCIntSuffix, + list<IntrinsicProperty> intr_properties = [IntrNoMem]> + : Hexagon_Intrinsic<GCCIntSuffix, + [], [llvm_i32_ty], + intr_properties>; + +// tag : M8_mxclracc_hf +class Hexagon___Intrinsic<string GCCIntSuffix, + list<IntrinsicProperty> intr_properties = [IntrNoMem]> + : Hexagon_Intrinsic<GCCIntSuffix, + [], [], + intr_properties>; + +// tag : M8_mxmem +class Hexagon__ptri32_Intrinsic<string GCCIntSuffix, + list<IntrinsicProperty> intr_properties = [IntrNoMem]> + : Hexagon_Intrinsic<GCCIntSuffix, + [], [llvm_ptr_ty,llvm_i32_ty], + intr_properties>; + +// tag : M8_mxmem2_bias +class Hexagon__ptr_Intrinsic<string GCCIntSuffix, + list<IntrinsicProperty> intr_properties = [IntrNoMem]> + : Hexagon_Intrinsic<GCCIntSuffix, + [], [llvm_ptr_ty], + intr_properties>; + +// tag : M8_mxmem_blk_sm_act_hf +class Hexagon__i32i32_Intrinsic<string GCCIntSuffix, + list<IntrinsicProperty> intr_properties = [IntrNoMem]> + : Hexagon_Intrinsic<GCCIntSuffix, + [], [llvm_i32_ty,llvm_i32_ty], + intr_properties>; + // tag : S2_insert class Hexagon_i32_i32i32i32i32_Intrinsic<string GCCIntSuffix, list<IntrinsicProperty> intr_properties = [IntrNoMem]> @@ -1271,20 +1306,6 @@ class Hexagon_v64i32_v64i32v32i32_Intrinsic<string GCCIntSuffix, [llvm_v64i32_ty], [llvm_v64i32_ty,llvm_v32i32_ty], intr_properties>; -// tag : Y2_dccleana -class Hexagon__ptr_Intrinsic<string GCCIntSuffix, - list<IntrinsicProperty> intr_properties = [IntrNoMem]> - : Hexagon_Intrinsic<GCCIntSuffix, - [], [llvm_ptr_ty], - intr_properties>; - -// tag : Y4_l2fetch -class Hexagon__ptri32_Intrinsic<string GCCIntSuffix, - list<IntrinsicProperty> intr_properties = [IntrNoMem]> - : Hexagon_Intrinsic<GCCIntSuffix, - [], [llvm_ptr_ty,llvm_i32_ty], - intr_properties>; - // tag : Y5_l2fetch class Hexagon__ptri64_Intrinsic<string GCCIntSuffix, list<IntrinsicProperty> intr_properties = [IntrNoMem]> @@ -1306,18 +1327,6 @@ class Hexagon_i32__Intrinsic<string GCCIntSuffix, [llvm_i32_ty], [], intr_properties>; -class Hexagon___Intrinsic<string GCCIntSuffix, - list<IntrinsicProperty> intr_properties = [IntrNoMem]> - : Hexagon_Intrinsic<GCCIntSuffix, - [], [], - intr_properties>; - -class Hexagon__i32_Intrinsic<string GCCIntSuffix, - list<IntrinsicProperty> intr_properties = [IntrNoMem]> - : Hexagon_Intrinsic<GCCIntSuffix, - [], [llvm_i32_ty], - intr_properties>; - // V5 Scalar Instructions. def int_hexagon_A2_abs : @@ -7483,6 +7492,40 @@ Hexagon_v32i32_v32i32v32i32_Intrinsic<"HEXAGON_V6_vsubuwwsat_dv">; def int_hexagon_V6_vsubuwwsat_dv_128B : Hexagon_v64i32_v64i32v64i32_Intrinsic<"HEXAGON_V6_vsubuwwsat_dv_128B">; +// V68 HMX Instructions. + +def int_hexagon_M8_mxclracc_hf : +Hexagon___Intrinsic<"HEXAGON_M8_mxclracc_hf", []>; + +def int_hexagon_M8_mxmem2_bias : +Hexagon__ptr_Intrinsic<"HEXAGON_M8_mxmem2_bias", [IntrWriteMem, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmem2_st_bias : +Hexagon__ptr_Intrinsic<"HEXAGON_M8_mxmem2_st_bias", [IntrWriteMem, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmem_blk_sm_act_hf : +Hexagon__i32i32_Intrinsic<"HEXAGON_M8_mxmem_blk_sm_act_hf", [IntrConvergent, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmem_sm_act_hf : +Hexagon__i32i32_Intrinsic<"HEXAGON_M8_mxmem_sm_act_hf", [IntrConvergent, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmem_wei_hf : +Hexagon__i32i32_Intrinsic<"HEXAGON_M8_mxmem_wei_hf", [IntrConvergent, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmemdp_wei_hf : +Hexagon__i32i32_Intrinsic<"HEXAGON_M8_mxmemdp_wei_hf", [IntrConvergent, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>; + +def int_hexagon_M8_mxmems_blk_sm_act_hf : +Hexagon__i32i32_Intrinsic<"HEXAGON_M8_mxmems_blk_sm_act_hf", [IntrConvergent, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>; + +// V73 HMX Instructions. + +def int_hexagon_M8_cvt_rs_hf : +Hexagon__i32_Intrinsic<"HEXAGON_M8_cvt_rs_hf", [IntrHasSideEffects]>; + +def int_hexagon_M8_mxmem : +Hexagon__ptri32_Intrinsic<"HEXAGON_M8_mxmem", [IntrWriteMem, IntrHasSideEffects]>; + // V85 HLX Instructions. def int_hexagon_LX_L16b_tmp : diff --git a/llvm/lib/Target/Hexagon/CMakeLists.txt b/llvm/lib/Target/Hexagon/CMakeLists.txt index 5b2f60b34f5366..6fc63798e3c874 100644 --- a/llvm/lib/Target/Hexagon/CMakeLists.txt +++ b/llvm/lib/Target/Hexagon/CMakeLists.txt @@ -59,6 +59,7 @@ add_llvm_target(HexagonCodeGen HexagonMachineScheduler.cpp HexagonMask.cpp HexagonMCInstLower.cpp + HexagonMergeActivateWeight.cpp HexagonNewValueJump.cpp HexagonOptAddrMode.cpp HexagonOptimizeSZextends.cpp diff --git a/llvm/lib/Target/Hexagon/Hexagon.h b/llvm/lib/Target/Hexagon/Hexagon.h index 64bc33164c7638..180fe7e32fe33b 100644 --- a/llvm/lib/Target/Hexagon/Hexagon.h +++ b/llvm/lib/Target/Hexagon/Hexagon.h @@ -26,6 +26,7 @@ class Pass; extern char &HexagonCopyHoistingID; extern char &HexagonExpandCondsetsID; +extern char &HexagonMergeActivateWeightID; extern char &HexagonTfrCleanupID; extern char &HexagonLiveVariablesID; void initializeHexagonAlignGlobalArraysPass(PassRegistry &); @@ -53,6 +54,8 @@ void initializeHexagonLoopAlignPass(PassRegistry &); void initializeHexagonLoopReschedulingPass(PassRegistry &); void initializeHexagonLowerHLXPairAllocasPass(PassRegistry &); void initializeHexagonMaskPass(PassRegistry &); +void initializeHexagonMergeActivateWeightPass(PassRegistry &); +void initializeHexagonMergeActivateWeight2Pass(PassRegistry &); void initializeHexagonNewValueJumpPass(PassRegistry &); void initializeHexagonOptAddrModePass(PassRegistry &); void initializeHexagonPacketizerPass(PassRegistry &); @@ -108,6 +111,8 @@ FunctionPass *createHexagonLoopAlign(); FunctionPass *createHexagonLoopRescheduling(); FunctionPass *createHexagonLowerHLXPairAllocas(); FunctionPass *createHexagonMask(); +FunctionPass *createHexagonMergeActivateWeight(); +FunctionPass *createHexagonMergeActivateWeight2(); FunctionPass *createHexagonNewValueJump(); FunctionPass *createHexagonOptAddrMode(); FunctionPass *createHexagonOptimizeSZextends(); diff --git a/llvm/lib/Target/Hexagon/HexagonDepMapAsm2Intrin.td b/llvm/lib/Target/Hexagon/HexagonDepMapAsm2Intrin.td index 91c14aeb0b1154..77b6db27989ebd 100644 --- a/llvm/lib/Target/Hexagon/HexagonDepMapAsm2Intrin.td +++ b/llvm/lib/Target/Hexagon/HexagonDepMapAsm2Intrin.td @@ -4092,6 +4092,32 @@ def: Pat<(int_hexagon_V6_vsubuwwsat_dv HvxWR:$src1, HvxWR:$src2), (V6_vsubuwwsat_dv HvxWR:$src1, HvxWR:$src2)>, Requires<[UseHVXV83, UseHVX64B]>; def: Pat<(int_hexagon_V6_vsubuwwsat_dv_128B HvxWR:$src1, HvxWR:$src2), (V6_vsubuwwsat_dv HvxWR:$src1, HvxWR:$src2)>, Requires<[UseHVXV83, UseHVX128B]>; + +// V68 HMX Instructions. + +def: Pat<(int_hexagon_M8_mxclracc_hf ), + (M8_mxclracc_hf )>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem2_bias IntRegs:$src1), + (M8_mxmem2_bias IntRegs:$src1)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem2_st_bias IntRegs:$src1), + (M8_mxmem2_st_bias IntRegs:$src1)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem_blk_sm_act_hf IntRegs:$src1, IntRegs:$src2), + (M8_mxmem_blk_sm_act_hf IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem_sm_act_hf IntRegs:$src1, IntRegs:$src2), + (M8_mxmem_sm_act_hf IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem_wei_hf IntRegs:$src1, IntRegs:$src2), + (M8_mxmem_wei_hf IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmemdp_wei_hf IntRegs:$src1, IntRegs:$src2), + (M8_mxmemdp_wei_hf IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV68, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmems_blk_sm_act_hf IntRegs:$src1, IntRegs:$src2), + (M8_mxmems_blk_sm_act_hf IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV68, UseHMX]>; + +// V73 HMX Instructions. + +def: Pat<(int_hexagon_M8_cvt_rs_hf IntRegs:$src1), + (M8_cvt_rs_hf IntRegs:$src1)>, Requires<[HasV73, UseHMX]>; +def: Pat<(int_hexagon_M8_mxmem IntRegs:$src1, IntRegs:$src2), + (M8_mxmem IntRegs:$src1, IntRegs:$src2)>, Requires<[HasV73, UseHMX]>; def: Pat<(int_hexagon_LX_abs_uh HlxLR:$src1), (LX_abs_uh HlxLR:$src1)>, Requires<[HasV85, UseHLX, UseHLX2048B]>; def: Pat<(int_hexagon_LX_abs_uh_sat HlxLR:$src1), diff --git a/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.cpp b/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.cpp index bae47eea9b398b..de088659942c90 100644 --- a/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.cpp +++ b/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.cpp @@ -32,6 +32,9 @@ void HexagonHazardRecognizer::Reset() { DotCurPNum = -1; UsesLoad = false; PrefVectorStoreNew = nullptr; + Weight = nullptr; + Activate = nullptr; + HMXPartner = nullptr; RegDefs.clear(); } @@ -66,9 +69,48 @@ HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) { return Hazard; } + if (mustWaitForHMXPartner(SU)) { + LLVM_DEBUG(dbgs() << "*** HMX pair hazard in cycle " << PacketNum << ", " + << *MI); + return Hazard; + } + return NoHazard; } +SUnit *HexagonHazardRecognizer::findHMXPartner(SUnit *SU) const { + const MachineInstr &MI = *SU->getInstr(); + bool IsActivate = TII->isHMXActivateInstr(MI); + SUnit *Partner = nullptr; + // The pair is formed by the nearest following instruction of the opposite + // kind (see HexagonSubtarget::HMXActivateWeightMutation). Both instructions + // have side effects, so they are always connected by a dependence edge. + for (const SDep &D : SU->Succs) { + SUnit *S = D.getSUnit(); + if (!S->isInstr()) + continue; + const MachineInstr &SI = *S->getInstr(); + bool Matches = + IsActivate ? TII->isHMXWeightInstr(SI) : TII->isHMXActivateInstr(SI); + if (Matches && (!Partner || S->NodeNum < Partner->NodeNum)) + Partner = S; + } + return Partner; +} + +bool HexagonHazardRecognizer::mustWaitForHMXPartner(SUnit *SU) const { + // Nothing to wait for unless exactly one half of a pair has been emitted. + if (!!Activate == !!Weight) + return false; + if (SU == HMXPartner) + return false; + // Delaying other instructions only helps if the partner can become ready + // without them. If the partner still depends on unscheduled instructions + // (e.g. a store or another side-effecting instruction between the two), + // or no partner was found, waiting would deadlock the scheduler. + return HMXPartner && HMXPartner->NumPredsLeft == 0; +} + void HexagonHazardRecognizer::AdvanceCycle() { LLVM_DEBUG(dbgs() << "Advance cycle, clear state\n"); Resources->clearResources(); @@ -78,6 +120,11 @@ void HexagonHazardRecognizer::AdvanceCycle() { } UsesLoad = false; PrefVectorStoreNew = nullptr; + if (Weight != nullptr && Activate != nullptr) { + Weight = nullptr; + Activate = nullptr; + HMXPartner = nullptr; + } PacketNum++; RegDefs.clear(); } @@ -91,8 +138,16 @@ void HexagonHazardRecognizer::AdvanceCycle() { /// to a .new store. The packetizer will not generate the .new store if the /// store doesn't have resources to fit in the packet (but the .new store may /// have resources). We attempt to schedule the store as soon as possible to -/// help packetize the two instructions together. +/// help packetize the two instructions together. Case 4 - when no HMX +/// activation/weight pair is in progress, we prefer other instructions over +/// starting a new pair. bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) const { + + if (Weight == nullptr && Activate == nullptr) + if (TII->isHMXWeightInstr(*SU->getInstr()) || + TII->isHMXActivateInstr(*SU->getInstr())) + return true; + if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU) return true; if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad()) @@ -167,4 +222,14 @@ void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) { PrefVectorStoreNew = S.getSUnit(); break; } + bool IsWeight = TII->isHMXWeightInstr(*MI); + bool IsActivate = TII->isHMXActivateInstr(*MI); + if (IsWeight || IsActivate) { + bool IsFirstHalf = !Weight && !Activate; + if (IsWeight) + Weight = SU; + if (IsActivate) + Activate = SU; + HMXPartner = IsFirstHalf ? findHMXPartner(SU) : nullptr; + } } diff --git a/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.h b/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.h index 4f8931a49f3875..0c2db38dadf949 100644 --- a/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.h +++ b/llvm/lib/Target/Hexagon/HexagonHazardRecognizer.h @@ -37,6 +37,13 @@ class HexagonHazardRecognizer : public ScheduleHazardRecognizer { // resources available (even if the .new version does). To help, the schedule // attempts to schedule the .new as soon as possible in the packet. SUnit *PrefVectorStoreNew = nullptr; + // The activation and weight instructions of the HMX pair being scheduled, + // used to prefer the second half once the first half is scheduled. + SUnit *Weight = nullptr; + SUnit *Activate = nullptr; + // The expected second half of the activation/weight pair after the first + // half has been emitted, or null if it is unknown. + SUnit *HMXPartner = nullptr; // The set of registers defined by instructions in the current packet. SmallSet<unsigned, 8> RegDefs; @@ -44,6 +51,14 @@ class HexagonHazardRecognizer : public ScheduleHazardRecognizer { // store because its value is defined in the same packet. bool isNewStore(MachineInstr &MI); + // Return the HMX instruction that pairs with the activation/weight + // instruction \p SU, or null if there is no such successor. + SUnit *findHMXPartner(SUnit *SU) const; + + // Return true if instructions other than the pending HMX partner must be + // delayed so that the partner is scheduled next to the first half. + bool mustWaitForHMXPartner(SUnit *SU) const; + public: HexagonHazardRecognizer(const InstrItineraryData *II, const HexagonInstrInfo *HII, diff --git a/llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp b/llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp index 97023f96a2f280..3815bd2978df73 100644 --- a/llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp +++ b/llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp @@ -753,7 +753,8 @@ class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo { bool shouldIgnoreForPipelining(const MachineInstr *MI) const override { // Only ignore the terminator. - return MI == EndLoop; + return MI == EndLoop || TII->isHMXActivateInstr(*MI) || + TII->isHMXWeightInstr(*MI); } std::optional<bool> createTripCountGreaterCondition( @@ -2389,6 +2390,20 @@ bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa, return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG)); } +bool HexagonInstrInfo::isHMXActivateInstr(const MachineInstr &MI) const { + return HexagonII::getTSFlag(MI.getDesc(), HexagonII::isHMXActivatePos); +} + +bool HexagonInstrInfo::isHMXWeightInstr(const MachineInstr &MI) const { + return HexagonII::getTSFlag(MI.getDesc(), HexagonII::isHMXWeightPos); +} + +bool HexagonInstrInfo::isActivateWeightPair(const MachineInstr &MIa, + const MachineInstr &MIb) const { + return (isHMXActivateInstr(MIa) && isHMXWeightInstr(MIb)) || + (isHMXWeightInstr(MIa) && isHMXActivateInstr(MIb)); +} + bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const { return (Opcode == Hexagon::ENDLOOP0 || Opcode == Hexagon::ENDLOOP1); diff --git a/llvm/lib/Target/Hexagon/HexagonInstrInfo.h b/llvm/lib/Target/Hexagon/HexagonInstrInfo.h index 929ea2f0fc7e5e..4a52ecda2e9cef 100644 --- a/llvm/lib/Target/Hexagon/HexagonInstrInfo.h +++ b/llvm/lib/Target/Hexagon/HexagonInstrInfo.h @@ -380,6 +380,10 @@ class HexagonInstrInfo : public HexagonGenInstrInfo { bool isDotCurInst(const MachineInstr &MI) const; bool isDotNewInst(const MachineInstr &MI) const; bool isDuplexPair(const MachineInstr &MIa, const MachineInstr &MIb) const; + bool isHMXActivateInstr(const MachineInstr &MI) const; + bool isHMXWeightInstr(const MachineInstr &MI) const; + bool isActivateWeightPair(const MachineInstr &MIa, + const MachineInstr &MIb) const; bool isEndLoopN(unsigned Opcode) const; bool isExpr(unsigned OpType) const; bool isExtendable(const MachineInstr &MI) const; diff --git a/llvm/lib/Target/Hexagon/HexagonMergeActivateWeight.cpp b/llvm/lib/Target/Hexagon/HexagonMergeActivateWeight.cpp new file mode 100644 index 00000000000000..17cddf5dff3a1a --- /dev/null +++ b/llvm/lib/Target/Hexagon/HexagonMergeActivateWeight.cpp @@ -0,0 +1,227 @@ +//===-- HexagonMergeActivateWeight.cpp - Pair HMX activate/weight ---------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "merge-hmx-activate-weight" + +#include "Hexagon.h" +#include "HexagonInstrInfo.h" +#include "HexagonSubtarget.h" +#include "llvm/CodeGen/LiveIntervals.h" +#include "llvm/CodeGen/MachineBasicBlock.h" +#include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstr.h" +#include "llvm/CodeGen/MachineOperand.h" +#include "llvm/CodeGen/SlotIndexes.h" +#include "llvm/CodeGen/TargetRegisterInfo.h" +#include "llvm/IR/DiagnosticInfo.h" +#include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/InitializePasses.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" +#include <iterator> + +using namespace llvm; + +namespace llvm { + +// A class to generate a warning message about weight and activate +class DiagnosticInfoWeightActivate : public DiagnosticInfoResourceLimit { + +public: + DiagnosticInfoWeightActivate(const Function &Fn, unsigned S, + DiagnosticSeverity Severity = DS_Warning) + : DiagnosticInfoResourceLimit(Fn, "activate weight pair", S, Severity) {} + + void print(DiagnosticPrinter &DP) const override { + DP << "In " << getFunction() << ", " + << "expected activate and weight pair"; + } + + static bool classof(const DiagnosticInfo *DI) { + return DI->getKind() == DK_ResourceLimit; + } +}; + +// Return true if \p Moved can be moved past every instruction in the range +// [Begin, End), in either direction. \p Moved is an HMX activation or weight +// instruction, which reads memory and updates the HMX state, so it must not be +// reordered with: +// - calls, inline asm, barriers or terminators, +// - side-effecting instructions (e.g. mxclracc, bias/cvt mxmem forms and any +// other HMX instruction), +// - volatile or atomic memory accesses, +// - stores, which may write the memory read by \p Moved, +// - instructions that redefine a register used by \p Moved, or that read or +// redefine a register defined by \p Moved. +static bool canMoveAcross(const MachineInstr &Moved, + MachineBasicBlock::const_iterator Begin, + MachineBasicBlock::const_iterator End, + const TargetRegisterInfo *TRI) { + for (const MachineInstr &MI : make_range(Begin, End)) { + if (MI.isDebugInstr() || &MI == &Moved) + continue; + + if (MI.isCall() || MI.isInlineAsm() || MI.isBarrier() || + MI.isTerminator() || MI.hasUnmodeledSideEffects() || + MI.hasOrderedMemoryRef() || MI.mayStore()) { + LLVM_DEBUG(dbgs() << "Cannot move " << Moved << " past " << MI); + return false; + } + + for (const MachineOperand &MO : Moved.operands()) { + if (!MO.isReg() || !MO.getReg()) + continue; + Register R = MO.getReg(); + if (MI.modifiesRegister(R, TRI) || + (MO.isDef() && MI.readsRegister(R, TRI))) { + LLVM_DEBUG(dbgs() << "Register " << printReg(R, TRI) + << " prevents moving " << Moved << " past " << MI); + return false; + } + } + } + return true; +} + +// Move each HMX activation instruction next to its matching weight instruction +// (or vice versa), so that the packetizer can put them in the same packet, as +// required by the hardware. Warn about activation or weight instructions that +// cannot be paired. +static bool runActivateWeight(MachineFunction &MF, LiveIntervals *LIS) { + auto &HST = MF.getSubtarget<HexagonSubtarget>(); + const HexagonInstrInfo *HII = HST.getInstrInfo(); + const TargetRegisterInfo *TRI = HST.getRegisterInfo(); + const Function &F = MF.getFunction(); + bool Changed = false; + + auto Warn = [&]() { + DiagnosticInfoWeightActivate WeightActivate(F, 1); + F.getContext().diagnose(WeightActivate); + }; + + for (MachineBasicBlock &MBB : MF) { + // The first instruction of a pair, waiting for its partner. + MachineInstr *Pending = nullptr; + + for (MachineInstr &MI : make_early_inc_range(MBB)) { + bool IsActivate = HII->isHMXActivateInstr(MI); + if (!IsActivate && !HII->isHMXWeightInstr(MI)) + continue; + if (!Pending) { + Pending = &MI; + continue; + } + if (IsActivate == HII->isHMXActivateInstr(*Pending)) { + // Two activations or two weights in a row. + Warn(); + Pending = &MI; + continue; + } + + // Pending and MI form a pair. Sink the first one down to the second, + // or, if that is not possible, hoist the second one up to the first. + MachineBasicBlock::iterator Between = + std::next(MachineBasicBlock::iterator(Pending)); + MachineBasicBlock::iterator Second(MI); + if (canMoveAcross(*Pending, Between, Second, TRI)) { + if (Between != Second) { + MBB.splice(Second, &MBB, Pending); + if (LIS) + LIS->handleMove(*Pending, /*UpdateFlags=*/true); + Changed = true; + } + } else if (canMoveAcross(MI, Between, Second, TRI)) { + MBB.splice(Between, &MBB, &MI); + if (LIS) + LIS->handleMove(MI, /*UpdateFlags=*/true); + Changed = true; + } else { + Warn(); + } + Pending = nullptr; + } + + if (Pending) + Warn(); + } + + return Changed; +} + +class HexagonMergeActivateWeight : public MachineFunctionPass { +public: + static char ID; + HexagonMergeActivateWeight() : MachineFunctionPass(ID) { + PassRegistry &Registry = *PassRegistry::getPassRegistry(); + initializeHexagonMergeActivateWeightPass(Registry); + } + + StringRef getPassName() const override { + return "Hexagon Merge Activate Weight Instructions After Machine Scheduler"; + } + bool runOnMachineFunction(MachineFunction &MF) override { + return runActivateWeight(MF, + &getAnalysis<LiveIntervalsWrapperPass>().getLIS()); + } + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesCFG(); + AU.addPreserved<SlotIndexesWrapperPass>(); + AU.addRequired<LiveIntervalsWrapperPass>(); + AU.addPreserved<LiveIntervalsWrapperPass>(); + MachineFunctionPass::getAnalysisUsage(AU); + } +}; + +char HexagonMergeActivateWeight::ID = 0; +char &HexagonMergeActivateWeightID = HexagonMergeActivateWeight::ID; + +class HexagonMergeActivateWeight2 : public MachineFunctionPass { +public: + static char ID; + HexagonMergeActivateWeight2() : MachineFunctionPass(ID) { + PassRegistry &Registry = *PassRegistry::getPassRegistry(); + initializeHexagonMergeActivateWeight2Pass(Registry); + } + + StringRef getPassName() const override { + return "Hexagon Merge Activate Weight Instructions Before Packetizer"; + } + bool runOnMachineFunction(MachineFunction &MF) override { + return runActivateWeight(MF, /*LIS=*/nullptr); + } +}; + +char HexagonMergeActivateWeight2::ID = 0; + +} // namespace llvm + +//===----------------------------------------------------------------------===// +// Public Constructor Functions +//===----------------------------------------------------------------------===// + +INITIALIZE_PASS_BEGIN(HexagonMergeActivateWeight, + "hexagon-merge-activate-weight", + "Hexagon merge activate weight after machine scheduler", + false, false) +INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) +INITIALIZE_PASS_END(HexagonMergeActivateWeight, "hexagon-merge-activate-weight", + "Hexagon merge activate weight after machine scheduler", + false, false) + +INITIALIZE_PASS(HexagonMergeActivateWeight2, "hexagon-merge-activate-weight-2", + "Hexagon merge activate weight before packetizer", false, false) + +FunctionPass *llvm::createHexagonMergeActivateWeight() { + return new HexagonMergeActivateWeight(); +} + +FunctionPass *llvm::createHexagonMergeActivateWeight2() { + return new HexagonMergeActivateWeight2(); +} diff --git a/llvm/lib/Target/Hexagon/HexagonSubtarget.cpp b/llvm/lib/Target/Hexagon/HexagonSubtarget.cpp index 0d68fceebd97ad..fe6e4fc0e533c6 100644 --- a/llvm/lib/Target/Hexagon/HexagonSubtarget.cpp +++ b/llvm/lib/Target/Hexagon/HexagonSubtarget.cpp @@ -394,6 +394,93 @@ void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) { } } +// Ensure that HMX activate and weight instructions are scheduled together. +// The main idea is to: +// 1. Reduce any latency between corresponding A/W to 0. +// 2. Make the depths of corresponding A/W instructions be the same. (The +// post-RA scheduler is a top-down list scheduler.) +void HexagonSubtarget::HMXActivateWeightMutation::apply( + ScheduleDAGInstrs *DAG) { + if (DAG->SUnits.empty()) + return; + + auto &HII = static_cast<const HexagonInstrInfo &>(*DAG->TII); + DenseMap<SUnit *, SUnit *> AWMap; // Map: Activation <-> Weight. + DenseSet<SUnit *> First; // Which of the two came first. + // The SUnits in the DAG follow the order of instructions. The code below + // depends on it to pair up activation and weight instructions. + SUnit *A = nullptr, *W = nullptr; + for (SUnit &SU : DAG->SUnits) { + MachineInstr *MI = SU.getInstr(); + if (HII.isHMXActivateInstr(*MI)) { + assert(A == nullptr && "Unexpected activation instruction"); + A = &SU; + if (!W) + First.insert(A); + } else if (HII.isHMXWeightInstr(*MI)) { + assert(W == nullptr && "Unexpected weight instruction"); + W = &SU; + if (!A) + First.insert(W); + } + if (A && W) { + AWMap.insert({A, W}); + AWMap.insert({W, A}); + A = W = nullptr; + } + } + assert(A == nullptr && "Unmatched activation instruction"); + assert(W == nullptr && "Unmatched weight instruction"); + + // Reset the latency between matching instructions to 0. + for (SUnit *FirstSU : First) { + SUnit *SecondSU = AWMap.lookup(FirstSU); + // Set the successor latency to 0. + for (SDep &D : FirstSU->Succs) { + if (D.getSUnit() == SecondSU) { + D.setLatency(0); + FirstSU->setHeightDirty(); + } + } + // Set the predecessor latency to 0. + for (SDep &D : SecondSU->Preds) { + if (D.getSUnit() == FirstSU) { + D.setLatency(0); + SecondSU->setDepthDirty(); + } + } + } + + SUnit *Entry = &DAG->SUnits[0]; + assert(Entry->getDepth() == 0); + + // For each pair, reset the depth (max distance from the entry) to be the + // max of the two. We can't just set the depth directly, because the post-RA + // scheduler recalculates it during scheduling. There are also assertions + // in it that can fire when the depth on the node is inconsistent with the + // calculations. + // To avoid this issue, make the depth "real" by adding a fake edge from + // the Entry node (which is assumed to have a depth of 0). + // Adding a new edge will mark the depths of successors (recursively) + // as dirty, potentially including subsequent pairs. The "getDepth" call + // will recompute a dirty depth, so it's important to iterate over the nodes + // in such a way that predecessors are visited first. + // + // Skip the first SUnit in case it's an A/W instruction, so that we don't + // try to create an edge from that node to itself. + for (SUnit &SU : MutableArrayRef<SUnit>(DAG->SUnits).drop_front()) { + if (!First.count(&SU)) + continue; + SUnit *FirstSU = &SU; + SUnit *SecondSU = AWMap.lookup(FirstSU); + unsigned MaxDepth = std::max(FirstSU->getDepth(), SecondSU->getDepth()); + SDep DepthEdge(Entry, SDep::Artificial); + DepthEdge.setLatency(MaxDepth); + FirstSU->addPred(DepthEdge); + SecondSU->addPred(DepthEdge); + } +} + // Check if a call and subsequent A2_tfrpi instructions should maintain // scheduling affinity. We are looking for the TFRI to be consumed in // the next instruction. This should help reduce the instances of @@ -654,6 +741,7 @@ void HexagonSubtarget::getPostRAMutations( std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { Mutations.push_back(std::make_unique<UsrOverflowMutation>()); Mutations.push_back(std::make_unique<HVXMemLatencyMutation>()); + Mutations.push_back(std::make_unique<HMXActivateWeightMutation>()); Mutations.push_back(std::make_unique<BankConflictMutation>()); Mutations.push_back(std::make_unique<HLXPseudoMutation>()); } @@ -662,6 +750,7 @@ void HexagonSubtarget::getSMSMutations( std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { Mutations.push_back(std::make_unique<UsrOverflowMutation>()); Mutations.push_back(std::make_unique<HVXMemLatencyMutation>()); + Mutations.push_back(std::make_unique<HMXActivateWeightMutation>()); } // Pin the vtable to this file. diff --git a/llvm/lib/Target/Hexagon/HexagonSubtarget.h b/llvm/lib/Target/Hexagon/HexagonSubtarget.h index dfd77863d27781..9974bd67e00b62 100644 --- a/llvm/lib/Target/Hexagon/HexagonSubtarget.h +++ b/llvm/lib/Target/Hexagon/HexagonSubtarget.h @@ -89,6 +89,9 @@ class HexagonSubtarget : public HexagonGenSubtargetInfo { struct HVXMemLatencyMutation : public ScheduleDAGMutation { void apply(ScheduleDAGInstrs *DAG) override; }; + struct HMXActivateWeightMutation : public ScheduleDAGMutation { + void apply(ScheduleDAGInstrs *DAG) override; + }; struct CallMutation : public ScheduleDAGMutation { void apply(ScheduleDAGInstrs *DAG) override; private: diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp index dd68b3c52f2c2c..6e7c35058e26ce 100644 --- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp +++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp @@ -187,6 +187,8 @@ static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) { C, std::make_unique<HexagonConvergingVLIWScheduler>()); DAG->addMutation(std::make_unique<HexagonSubtarget::UsrOverflowMutation>()); DAG->addMutation(std::make_unique<HexagonSubtarget::HVXMemLatencyMutation>()); + DAG->addMutation( + std::make_unique<HexagonSubtarget::HMXActivateWeightMutation>()); DAG->addMutation(std::make_unique<HexagonSubtarget::CallMutation>()); DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII)); DAG->addMutation(std::make_unique<HexagonSubtarget::HLXPseudoMutation>()); @@ -223,6 +225,8 @@ LLVMInitializeHexagonTarget() { initializeHexagonHVXSaveRemarkPass(PR); initializeHexagonLoopIdiomRecognizeLegacyPassPass(PR); initializeHexagonLowerHLXPairAllocasPass(PR); + initializeHexagonMergeActivateWeightPass(PR); + initializeHexagonMergeActivateWeight2Pass(PR); initializeHexagonNewValueJumpPass(PR); initializeHexagonOptAddrModePass(PR); initializeHexagonPacketizerPass(PR); @@ -515,6 +519,14 @@ void HexagonPassConfig::addMachineSSAOptimization() { } void HexagonPassConfig::addPreRegAlloc() { + // HMX activation/weight pairs must be adjacent before register allocation. + // With optimized register allocation, run after the machine scheduler, + // which may separate the pairs. + if (getOptimizeRegAlloc()) + insertPass(&MachineSchedulerID, &HexagonMergeActivateWeightID); + else + insertPass(&PHIEliminationID, &HexagonMergeActivateWeightID); + if (getOptLevel() != CodeGenOptLevel::None) { if (EnableExpandCondsets) insertPass(&RegisterCoalescerID, &HexagonExpandCondsetsID); @@ -575,6 +587,8 @@ void HexagonPassConfig::addPreEmitPass() { addPass(&HexagonLiveVariablesID); } + addPass(createHexagonMergeActivateWeight2()); + // Emit KCFI checks for indirect calls. Must run before packetization so // the check and call can be bundled together into a VLIW packet. addPass(createKCFIPass()); diff --git a/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp b/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp index 6baa770fa23d4c..ab0f3f5276cfde 100644 --- a/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp +++ b/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp @@ -129,6 +129,7 @@ HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF, addMutation(std::make_unique<HexagonSubtarget::UsrOverflowMutation>()); addMutation(std::make_unique<HexagonSubtarget::HVXMemLatencyMutation>()); + addMutation(std::make_unique<HexagonSubtarget::HMXActivateWeightMutation>()); addMutation(std::make_unique<HexagonSubtarget::BankConflictMutation>()); addMutation(std::make_unique<HexagonSubtarget::HLXPseudoMutation>()); } @@ -1753,6 +1754,33 @@ HexagonPacketizerList::addToPacket(MachineInstr &MI) { } assert(ResourceTracker->canReserveResources(MI)); + MachineBasicBlock::iterator nextMII = MII; + if (std::next(MII) != MI.getParent()->end()) { + MachineInstr &nextMI = *++nextMII; + if (HII->isActivateWeightPair(MI, nextMI)) { + for (auto J : CurrentPacketMIs) { + MachineInstr &JMI = *J; + if (HII->isDependent(JMI, MI) || HII->isDependent(JMI, nextMI)) { + endPacket(MBB, MI); + break; + } + } + ResourceTracker->reserveResources(MI); + if (ResourceTracker->canReserveResources(nextMI)) + ResourceTracker->reserveResources(nextMI); + else { + endPacket(MBB, MI); + assert(ResourceTracker->canReserveResources(MI)); + ResourceTracker->reserveResources(MI); + assert(ResourceTracker->canReserveResources(nextMI)); + ResourceTracker->reserveResources(nextMI); + } + CurrentPacketMIs.push_back(&MI); + CurrentPacketMIs.push_back(&nextMI); + return nextMII; + } + } + bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI); bool Good = true; diff --git a/llvm/test/CodeGen/Hexagon/hmx-act-wei-adjacent.ll b/llvm/test/CodeGen/Hexagon/hmx-act-wei-adjacent.ll new file mode 100644 index 00000000000000..a9763640f9068b --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hmx-act-wei-adjacent.ll @@ -0,0 +1,41 @@ +; RUN: llc -march=hexagon < %s | FileCheck %s + +; These two should be adjacent. +; CHECK: activation.hf = mxmem +; CHECK-NEXT: weight.hf = mxmem + +target datalayout = "e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048" +target triple = "hexagon" + +define dllexport void @f0() local_unnamed_addr #0 { +b0: + %v0 = add nsw i32 0, 1 + %v1 = sub nuw nsw i32 4, 0 + %v2 = select i1 undef, i32 4, i32 %v1 + br label %b1 + +b1: ; preds = %b1, %b0 + %v3 = phi i32 [ 0, %b0 ], [ %v13, %b1 ] + %v4 = mul i32 %v3, -8 + %v5 = add i32 %v4, undef + %v6 = icmp slt i32 %v5, 8 + %v7 = select i1 %v6, i32 %v5, i32 8 + %v8 = shl nsw i32 %v7, 2 + %v9 = mul i32 %v8, %v2 + %v10 = shl i32 %v9, 6 + %v11 = mul i32 %v10, %v0 + %v12 = add nsw i32 %v11, -1 + tail call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 undef, i32 undef) + tail call void @llvm.hexagon.M8.mxmem.wei.hf(i32 undef, i32 %v12) + %v13 = add nuw nsw i32 %v3, 1 + br label %b1 +} + +; Function Attrs: nounwind writeonly +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) #1 + +; Function Attrs: nounwind writeonly +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) #1 + +attributes #0 = { "target-features"="+hmx,-packets" } +attributes #1 = { nounwind writeonly } diff --git a/llvm/test/CodeGen/Hexagon/hmx-act-wei-attributes.ll b/llvm/test/CodeGen/Hexagon/hmx-act-wei-attributes.ll new file mode 100644 index 00000000000000..d27328fed87cc2 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hmx-act-wei-attributes.ll @@ -0,0 +1,45 @@ +; RUN: llc -march=hexagon < %s | FileCheck %s + +target datalayout = "e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048" +target triple = "hexagon" + +; Check that there are two pairs of adjacent activation/weight instructions. + +; CHECK-LABEL: f0: +; CHECK: activation.hf +; CHECK-NEXT: weight.hf +; CHECK-LABEL: LBB0_2: +; CHECK: activation.hf +; CHECK-NEXT: weight.hf +define dllexport void @f0(i32 %a0, i32 %a1) local_unnamed_addr #0 { +b0: + %v0 = icmp slt i32 %a0, %a1 + br i1 %v0, label %b1, label %b2 + +b1: ; preds = %b1, %b0 + %v1 = add i32 %a0, 1 + tail call void @f1() + tail call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %v1, i32 %a1) + tail call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %a0, i32 %a1) + br label %b3 + +b2: + %v2 = add i32 %a1, 4 + tail call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %a0, i32 %v2) + tail call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %a0, i32 %a1) + br label %b3 + +b3: + ret void +} + +declare void @f1() #0 + +; Function Attrs: nounwind writeonly +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) #1 + +; Function Attrs: nounwind writeonly +declare void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32, i32) #1 + +attributes #0 = { "target-features"="+hmx" } +attributes #1 = { nounwind writeonly convergent nomerge noduplicate } diff --git a/llvm/test/CodeGen/Hexagon/hmx-act-wei-hazard-deadlock.mir b/llvm/test/CodeGen/Hexagon/hmx-act-wei-hazard-deadlock.mir new file mode 100644 index 00000000000000..17bc1c9763edce --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hmx-act-wei-hazard-deadlock.mir @@ -0,0 +1,100 @@ +# RUN: llc -mtriple=hexagon -mcpu=hexagonv68 -mattr=+hmx -run-pass=post-RA-sched \ +# RUN: %s -o - | FileCheck %s + +# The Hexagon hazard recognizer delays other instructions after one half of +# an HMX activation/weight pair is scheduled, so that the other half is placed +# next to it. If the second half depends on an instruction that is not yet +# scheduled, delaying it deadlocks the post-RA scheduler. Check that the +# scheduler terminates and keeps the dependent instruction between the two. + +# A volatile store between the activation and weight instructions. +# CHECK-LABEL: name: split_by_store +# CHECK: M8_mxmem_blk_sm_act_hf +# CHECK-NEXT: S2_storeri_io +# CHECK-NEXT: M8_mxmem_wei_hf +--- +name: split_by_store +tracksRegLiveness: true +body: | + bb.0: + liveins: $r0, $r1, $r2, $r3, $r4, $r5 + M8_mxmem_blk_sm_act_hf $r0, $r4 + S2_storeri_io $r2, 0, $r3 :: (volatile store (s32)) + M8_mxmem_wei_hf $r1, $r5 + PS_jmpret $r31, implicit-def $pc +... + +# A register used by the activation is redefined and then used by the weight. +# The anti-dependence is broken by renaming, so the pair can be adjacent. +# CHECK-LABEL: name: split_by_redef +# CHECK: A2_addi +# CHECK-NEXT: M8_mxmem_blk_sm_act_hf +# CHECK-NEXT: M8_mxmem_wei_hf +--- +name: split_by_redef +tracksRegLiveness: true +body: | + bb.0: + liveins: $r0, $r4, $r5 + M8_mxmem_blk_sm_act_hf $r0, $r4 + $r0 = A2_addi $r0, 128 + M8_mxmem_wei_hf $r0, $r5 + PS_jmpret $r31, implicit-def $pc +... + +# Another HMX instruction between the activation and weight instructions. +# CHECK-LABEL: name: split_by_hmx +# CHECK: M8_mxmem_blk_sm_act_hf +# CHECK-NEXT: M8_mxclracc_hf +# CHECK-NEXT: M8_mxmem_wei_hf +--- +name: split_by_hmx +tracksRegLiveness: true +body: | + bb.0: + liveins: $r0, $r1, $r4, $r5 + M8_mxmem_blk_sm_act_hf $r0, $r4 + M8_mxclracc_hf + M8_mxmem_wei_hf $r1, $r5 + PS_jmpret $r31, implicit-def $pc +... + +# Independent instructions must not be scheduled between the two halves. +# CHECK-LABEL: name: adjacent +# CHECK: M8_mxmem_blk_sm_act_hf +# CHECK-NEXT: M8_mxmem_wei_hf +--- +name: adjacent +tracksRegLiveness: true +body: | + bb.0: + liveins: $r0, $r1, $r2, $r3, $r4, $r5 + M8_mxmem_blk_sm_act_hf $r0, $r4 + $r6 = A2_addi $r2, 1 + $r7 = A2_addi $r3, 1 + $r8 = A2_addi $r2, 2 + $r9 = A2_addi $r3, 2 + M8_mxmem_wei_hf $r1, $r5 + PS_jmpret $r31, implicit-def $pc, implicit $r6, implicit $r7, implicit $r8, implicit $r9 +... + +# The load and the activation use both memory slots of the first cycle, so the +# weight is scheduled in the next cycle. An independent instruction that +# becomes ready in that cycle must not be scheduled before the weight. +# CHECK-LABEL: name: adjacent_after_slot_conflict +# CHECK: M8_mxmem_blk_sm_act_hf +# CHECK-NEXT: M8_mxmem_wei_hf +--- +name: adjacent_after_slot_conflict +tracksRegLiveness: true +body: | + bb.0: + liveins: $r0, $r1, $r2, $r3, $r4, $r5 + $r6 = L2_loadri_io $r2, 0 + M8_mxmem_blk_sm_act_hf $r0, $r4 + M8_mxmem_wei_hf $r1, $r5 + $r7 = A2_addi $r3, 1 + $r8 = A2_addi $r3, 2 + $r9 = A2_addi $r6, 3 + PS_jmpret $r31, implicit-def $pc, implicit $r7, implicit $r8, implicit $r9 +... diff --git a/llvm/test/CodeGen/Hexagon/hmx-act-wei-memory-effects.ll b/llvm/test/CodeGen/Hexagon/hmx-act-wei-memory-effects.ll new file mode 100644 index 00000000000000..ad56fbb3d4994f --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hmx-act-wei-memory-effects.ll @@ -0,0 +1,33 @@ +; RUN: opt -mtriple=hexagon -S -passes=dse < %s | FileCheck %s + +; HMX activation and weight instructions read memory. Their intrinsics must +; not be marked as write-only, otherwise dead store elimination removes the +; stores that fill the activation and weight buffers, because the buffers are +; overwritten after the HMX instructions. + +; CHECK-LABEL: define void @act_wei_pair( +; CHECK: store i32 %v0, ptr %act +; CHECK-NEXT: store i32 %v1, ptr %wei +; CHECK-NEXT: ptrtoint +; CHECK-NEXT: ptrtoint +; CHECK-NEXT: call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf( +; CHECK-NEXT: call void @llvm.hexagon.M8.mxmem.wei.hf( +; CHECK-NEXT: store i32 0, ptr %act +; CHECK-NEXT: store i32 0, ptr %wei +define void @act_wei_pair(ptr %act, ptr %wei, i32 %v0, i32 %v1, i32 %n) { + store i32 %v0, ptr %act + store i32 %v1, ptr %wei + %a = ptrtoint ptr %act to i32 + %w = ptrtoint ptr %wei to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + store i32 0, ptr %act + store i32 0, ptr %wei + ret void +} + +; CHECK: declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) [[ATTRS:#[0-9]+]] +; CHECK: declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) [[ATTRS]] +; CHECK-NOT: attributes [[ATTRS]] = {{.*}}memory( +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) diff --git a/llvm/test/CodeGen/Hexagon/hmx-act-wei-pairing-errors.ll b/llvm/test/CodeGen/Hexagon/hmx-act-wei-pairing-errors.ll new file mode 100644 index 00000000000000..20081d83f96eed --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hmx-act-wei-pairing-errors.ll @@ -0,0 +1,83 @@ +; RUN: llc -O0 -verify-machineinstrs -mtriple=hexagon -mcpu=hexagonv68 \ +; RUN: -mattr=+hmx < %s -o /dev/null 2>&1 \ +; RUN: | FileCheck %s --implicit-check-not=error: + +; HMX activation and weight instructions must be in the same packet. Check that +; a warning is reported when a pair cannot be formed, because neither +; instruction can be moved past the instructions between them. + +@abuf = external global [64 x i32] +@wbuf = external global [64 x i32] +@other = external global i32 + +; The addresses are plain integers, so the store may alias both buffers. +; CHECK: warning: In may_alias, expected activate and weight pair +define void @may_alias(i32 %a, i32 %w, i32 %n) { + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + store i32 1, ptr @other + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +; A volatile store must not be reordered with the HMX instructions. +; CHECK: warning: In volatile_store, expected activate and weight pair +define void @volatile_store(i32 %n) { + %a = ptrtoint ptr @abuf to i32 + %w = ptrtoint ptr @wbuf to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + store volatile i32 1, ptr @other + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +; Another HMX instruction between the activation and the weight. +; CHECK: warning: In hmx_between, expected activate and weight pair +define void @hmx_between(i32 %n) { + %a = ptrtoint ptr @abuf to i32 + %w = ptrtoint ptr @wbuf to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + call void @llvm.hexagon.M8.mxclracc.hf() + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +; Alias analysis is not used, so the instructions are not moved past a store, +; even if it does not alias the memory read by either of them. +; CHECK: warning: In sink, expected activate and weight pair +define void @sink(i32 %n) { + %a = ptrtoint ptr @abuf to i32 + %w = ptrtoint ptr @wbuf to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + store i32 1, ptr @other + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +; The store writes the activation buffer, so the activation must stay before +; it. The weight is not moved up past the store either, although the store does +; not alias the weight buffer. +; CHECK: warning: In hoist, expected activate and weight pair +define void @hoist(i32 %n) { + %a = ptrtoint ptr @abuf to i32 + %w = ptrtoint ptr @wbuf to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + store i32 1, ptr @abuf + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +; A call between the activation and the weight. +; CHECK: warning: In call_between, expected activate and weight pair +define void @call_between(i32 %n) { + %a = ptrtoint ptr @abuf to i32 + %w = ptrtoint ptr @wbuf to i32 + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %n) + call void @g() + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %w, i32 %n) + ret void +} + +declare void @g() +declare void @llvm.hexagon.M8.mxclracc.hf() +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) diff --git a/llvm/test/CodeGen/Hexagon/intrinsics/v68-hmx.ll b/llvm/test/CodeGen/Hexagon/intrinsics/v68-hmx.ll new file mode 100644 index 00000000000000..7b9cb158932394 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/intrinsics/v68-hmx.ll @@ -0,0 +1,68 @@ +; RUN: llc -march=hexagon -mcpu=hexagonv68 -mattr=+hmx < %s | FileCheck %s + +; Test that the compiler generates code for the HMX intrinsics. + +; CHECK-LABEL: M8_mxclracc_hf: +; CHECK: mxclracc.hf +define void @M8_mxclracc_hf() #0 { +entry: + call void @llvm.hexagon.M8.mxclracc.hf() + ret void +} +declare void @llvm.hexagon.M8.mxclracc.hf() + +; CHECK-LABEL: M8_mxmem2_bias: +; CHECK: bias = mxmem2(r0) +define void @M8_mxmem2_bias(ptr %a) #0 { +entry: + call void @llvm.hexagon.M8.mxmem2.bias(ptr %a) + ret void +} +declare void @llvm.hexagon.M8.mxmem2.bias(ptr) + +; CHECK-LABEL: M8_mxmem2_st_bias: +; CHECK: mxmem2(r0) = bias +define void @M8_mxmem2_st_bias(ptr %a) #0 { +entry: + call void @llvm.hexagon.M8.mxmem2.st.bias(ptr %a) + ret void +} +declare void @llvm.hexagon.M8.mxmem2.st.bias(ptr) + +; CHECK-LABEL: M8_mxmem_blk_sm_act_hf_M8_mxmem_wei_hf: +; CHECK: activation.hf = mxmem(r0,r1) +; CHECK-NEXT: weight.hf = mxmem(r2,r3) +define void @M8_mxmem_blk_sm_act_hf_M8_mxmem_wei_hf(i32 %a, i32 %b, i32 %c, i32 %d) #0 { +entry: + call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %a, i32 %b) + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %c, i32 %d) + ret void +} + +; CHECK-LABEL: M8_mxmem_sm_act_hf_M8_mxmem_wei_hf: +; CHECK: activation.hf = mxmem(r0,r1):deep +; CHECK-NEXT: weight.hf = mxmem(r2,r3) +define void @M8_mxmem_sm_act_hf_M8_mxmem_wei_hf(i32 %a, i32 %b, i32 %c, i32 %d) #0 { +entry: + call void @llvm.hexagon.M8.mxmem.sm.act.hf(i32 %a, i32 %b) + call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %c, i32 %d) + ret void +} + +; CHECK-LABEL: M8_mxmems_blk_sm_act_hf_M8_mxmemdp_wei_hf: +; CHECK: activation.hf = mxmem(r0,r1):single +; CHECK-NEXT: weight.hf = mxmem(r2,r3):deep +define void @M8_mxmems_blk_sm_act_hf_M8_mxmemdp_wei_hf(i32 %a, i32 %b, i32 %c, i32 %d) #0 { +entry: + call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %a, i32 %b) + call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %c, i32 %d) + ret void +} + +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmem.sm.act.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) +declare void @llvm.hexagon.M8.mxmemdp.wei.hf(i32, i32) + +attributes #0 = { nounwind } diff --git a/llvm/test/CodeGen/Hexagon/intrinsics/v73-hmx.ll b/llvm/test/CodeGen/Hexagon/intrinsics/v73-hmx.ll new file mode 100644 index 00000000000000..f510a754481e42 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/intrinsics/v73-hmx.ll @@ -0,0 +1,23 @@ +; RUN: llc -march=hexagon -mcpu=hexagonv73 -mattr=+hmx < %s | FileCheck %s + +; Test that the compiler generates code for the HMX intrinsics. + +; CHECK-LABEL: M8_cvt_rs_hf: +; CHECK: cvt.hf = acc(r0) +define void @M8_cvt_rs_hf(i32 %a) #0 { +entry: + call void @llvm.hexagon.M8.cvt.rs.hf(i32 %a) + ret void +} +declare void @llvm.hexagon.M8.cvt.rs.hf(i32) + +; CHECK-LABEL: M8_mxmem: +; CHECK: mxmem(r0,r1) = cvt +define void @M8_mxmem(ptr %a, i32 %b) #0 { +entry: + call void @llvm.hexagon.M8.mxmem(ptr %a, i32 %b) + ret void +} +declare void @llvm.hexagon.M8.mxmem(ptr, i32) + +attributes #0 = { nounwind } diff --git a/llvm/test/CodeGen/Hexagon/isRestNoSlot1Store.ll b/llvm/test/CodeGen/Hexagon/isRestNoSlot1Store.ll new file mode 100644 index 00000000000000..d84d66a7e007d6 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/isRestNoSlot1Store.ll @@ -0,0 +1,36 @@ +; RUN: llc -march=hexagon -mv68 -mattr=+hmx < %s | FileCheck %s + +; Make sure that the M8_mxmem2_bias and store are not packetized together. +; M8_mxmem2_bias is a pure SLOT0 instruction and has a A_RESTRICT_NOSLOT1_STORE +; constraint. +; S2_storeri_io can go in either SLOT0 or SLOT1. +; This implies we can not packetize these two instructions together. + +; CHECK-LABEL: foo1 +; CHECK: mxmem2 +; CHECK-NEXT: } +; CHECK: memw + +target triple = "hexagon" +declare void @llvm.hexagon.M8.mxmem2.bias(ptr) #0 + +define void @foo1(ptr %a) local_unnamed_addr { +entry: + call void @llvm.hexagon.M8.mxmem2.bias(ptr undef) + store i32 0, ptr %a, align 4 + ret void +} + +; CHECK-LABEL: foo2 +; CHECK: memw +; CHECK-NEXT: } +; CHECK: mxmem + +define void @foo2(ptr %a) local_unnamed_addr { +entry: + store i32 0, ptr %a, align 4 + call void @llvm.hexagon.M8.mxmem2.bias(ptr undef) + ret void +} + +attributes #0 = { nocallback nofree nosync nounwind willreturn memory(write) } diff --git a/llvm/test/CodeGen/Hexagon/merge-activate-weight.ll b/llvm/test/CodeGen/Hexagon/merge-activate-weight.ll new file mode 100644 index 00000000000000..a140bf032351d2 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/merge-activate-weight.ll @@ -0,0 +1,25 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv68 -mattr=+hmx < %s | FileCheck %s + +; This test was checking for a copy of an operand that is defined between the +; activation and weight instructions when HexagonMergeActivateWeight was run +; only after register allocator. A copy is not created anymore because +; HexagonMergeActivateWeight now run before register allocator as well. + +; CHECK: [[REG1:r(([0-9]+))]] = or +; CHECK: activation.hf = mxmem([[REG1]],r{{[0-9]+}}) +; CHECK: weight.hf = mxmem + +define dllexport i32 @conv2d_hmx_kernel1(i8* noalias, i8* noalias) local_unnamed_addr #1 { +entry: + tail call void @llvm.hexagon.M8.mxclracc.hf() + %2 = ptrtoint i8* %0 to i32 + %3 = or i32 %2, 1152 + tail call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %3, i32 3960) + %4 = ptrtoint i8* %1 to i32 + tail call void @llvm.hexagon.M8.mxmem.wei.hf(i32 %4, i32 25600) + ret i32 0 +} + +declare void @llvm.hexagon.M8.mxclracc.hf() #2 +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) #2 +declare void @llvm.hexagon.M8.mxmem.wei.hf(i32, i32) #2 diff --git a/llvm/test/CodeGen/Hexagon/pureslot0.ll b/llvm/test/CodeGen/Hexagon/pureslot0.ll new file mode 100644 index 00000000000000..ea1f50748ba17d --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/pureslot0.ll @@ -0,0 +1,36 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv73 -mattr=+hmx < %s | FileCheck %s + +; Test that we packetize HMX instructions correctly. In this case, the +; mxmem2_bias must be in SLOT0, so it can't be packetized with another +; HMX instructions. + +; CHECK: mxclracc.hf +; CHECK: bias = mxmem2(r0) +; CHECK: { +; CHECK: mxmem(r1,r2) = cvt + + +source_filename = "conv2d_hmx_kernel0" +target datalayout = "e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048" +target triple = "hexagon" + +define dllexport i32 @conv2d_hmx_kernel0(ptr noalias %0, ptr noalias %1) local_unnamed_addr #0 { +entry: + tail call void @llvm.hexagon.M8.mxclracc.hf() + tail call void @llvm.hexagon.M8.mxmem2.bias(ptr %0) + %2 = ptrtoint ptr %1 to i32 + %3 = or i32 %2, 1152 + tail call void @llvm.hexagon.M8.mxmem(ptr %1, i32 %3) + ret i32 0 +} + +; Function Attrs: nounwind +declare void @llvm.hexagon.M8.mxclracc.hf() #0 + +; Function Attrs: nounwind +declare void @llvm.hexagon.M8.mxmem2.bias(ptr) #0 + +; Function Attrs: nounwind +declare void @llvm.hexagon.M8.mxmem(ptr, i32) #0 + +attributes #0 = { nounwind } diff --git a/llvm/test/CodeGen/Hexagon/update-reg-states.ll b/llvm/test/CodeGen/Hexagon/update-reg-states.ll new file mode 100644 index 00000000000000..1d7f12596e5a8a --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/update-reg-states.ll @@ -0,0 +1,233 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv68 -mattr=+hmx < %s | FileCheck %s + +; Check that a register is not overwritten so the last operand of the third +; activation instruction has the correct value. + +; CHECK: .LBB0_6 +; CHECK: sub +; CHECK: r[[R19:[0-9]+]] = sub +; CHECK: r[[R18:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; CHECK: r{{[0-9]+}} = or(r{{[0-9]+}},r[[R19]]) +; CHECK-NOT: r[[R19]] = r[[R18]] +; CHECK: activation.hf = mxmem(r[[R18]] +; CHECK: r[[R20:[0-9]+]] = or(r{{[0-9]+}},r[[R19]]) +; CHECK: activation.hf = mxmem(r{{[0-9]+}},r[[R20]]):single + +; RUN: llc -O0 -mtriple=hexagon -mcpu=hexagonv68 -mattr=+hmx < %s | FileCheck -check-prefix=PACKETO0 %s +; Check that weight and activation instructions are packetized together at O0. + +; PACKETO0: { +; PACKETO0: activation.hf = mxmem +; PACKETO0-NEXT: weight.hf = mxmem +; PACKETO0-NEXT: } + +; RUN: llc -mtriple=hexagon -mcpu=hexagonv68 -mattr=+hmx < %s | FileCheck -check-prefix=PACKET %s +; Check that weight and activation instructions are packetized together. +; Also check that none of the operands of activate and weight instructions is +; packetized with them. + +; PACKET: .LBB0_8 +; PACKET-DAG: r[[R1:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET-DAG: r[[R2:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET: } +; PACKET: { +; PACKET: activation.hf = mxmem(r[[R1]],r[[R2]]):single +; PACKET-NEXT: weight.hf = mxmem(r{{[0-9]+}},r{{[0-9]+}}):deep + +; PACKET-DAG: r{{[0-9]+}} = add +; PACKET-DAG: r[[R3:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET-DAG: r[[R4:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET: } +; PACKET: { +; PACKET: activation.hf = mxmem(r[[R3]],r[[R4]]){{$}} +; PACKET-NEXT: weight.hf = mxmem(r{{[0-9]+}},r{{[0-9]+}}):deep + +; PACKET-DAG: r{{[0-9]+}} = add +; PACKET-DAG: r[[R5:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET-DAG: r[[R6:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET: } +; PACKET: { +; PACKET: activation.hf = mxmem(r[[R5]],r[[R6]]):single +; PACKET-NEXT: weight.hf = mxmem(r{{[0-9]+}},r{{[0-9]+}}):deep + +; PACKET-DAG: r[[R7:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET-DAG: r[[R8:[0-9]+]] = or(r{{[0-9]+}},r{{[0-9]+}}) +; PACKET-DAG: r{{[0-9]+}} = add +; PACKET: } +; PACKET: { +; PACKET: activation.hf = mxmem(r[[R7]],r[[R8]]){{$}} +; PACKET-NEXT: weight.hf = mxmem(r{{[0-9]+}},r{{[0-9]+}}):deep + +target triple = "hexagon" + +%struct.tile_data = type { ptr, i32, i32, i32, i32, i32 } +%struct.hmx_params = type { %struct.registerPair, [2 x %struct.registerPair], [4 x i32], [2 x i32], i32, [4 x i8] } +%struct.registerPair = type { i32, i32 } + +define dso_local void @function(ptr nocapture readonly %outblock, ptr nocapture readonly %inblock, ptr %weight_in, ptr %bias, ptr nocapture readonly %mxparam) local_unnamed_addr #0 { +entry: + %offset_t_row = getelementptr inbounds %struct.tile_data, ptr %inblock, i32 0, i32 2 + %0 = load i32, ptr %offset_t_row, align 4 + %height = getelementptr inbounds %struct.tile_data, ptr %outblock, i32 0, i32 4 + %1 = load i32, ptr %height, align 4 + %depth = getelementptr inbounds %struct.tile_data, ptr %outblock, i32 0, i32 5 + %2 = load i32, ptr %depth, align 4 + %height2 = getelementptr inbounds %struct.tile_data, ptr %inblock, i32 0, i32 4 + %3 = load i32, ptr %height2, align 4 + %depth4 = getelementptr inbounds %struct.tile_data, ptr %inblock, i32 0, i32 5 + %4 = load i32, ptr %depth4, align 4 + %cmp6197 = icmp eq i32 %2, 0 + br i1 %cmp6197, label %for.cond.cleanup, label %for.body.lr.ph + +for.body.lr.ph: ; preds = %entry + %cmp8194 = icmp eq i32 %1, 0 + %addr = getelementptr inbounds %struct.tile_data, ptr %inblock, i32 0, i32 0 + %start = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 1, i32 0, i32 0 + %start24 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 1, i32 1, i32 0 + %cmp36190 = icmp eq i32 %4, 0 + %range = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 1, i32 1, i32 1 + %arrayidx47 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 2, i32 1 + %range61 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 1, i32 0, i32 1 + %arrayidx64 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 2, i32 0 + %arrayidx70 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 2, i32 3 + %arrayidx71 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 3, i32 1 + %arrayidx85 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 3, i32 0 + %arrayidx95 = getelementptr inbounds %struct.hmx_params, ptr %mxparam, i32 0, i32 2, i32 2 + br label %for.body + +for.cond.cleanup: ; preds = %for.cond.cleanup9, %entry + ret void + +for.body: ; preds = %for.cond.cleanup9, %for.body.lr.ph + %weight_in.addr.0200 = phi ptr [ %weight_in, %for.body.lr.ph ], [ %weight.1.lcssa, %for.cond.cleanup9 ] + %d.0199 = phi i32 [ 0, %for.body.lr.ph ], [ %add105, %for.cond.cleanup9 ] + %weight.0198 = phi ptr [ undef, %for.body.lr.ph ], [ %weight.1.lcssa, %for.cond.cleanup9 ] + %mul = shl i32 %d.0199, 1 + %add.ptr = getelementptr inbounds i32, ptr %bias, i32 %mul + %5 = bitcast ptr %add.ptr to ptr + tail call void @llvm.hexagon.M8.mxmem2.st.bias(ptr %5) + br i1 %cmp8194, label %for.cond.cleanup9, label %for.body10 + +for.cond7.loopexit: ; preds = %for.body38, %for.body10 + %weight.2.lcssa = phi ptr [ %weight_in.addr.0200, %for.body10 ], [ %add.ptr99, %for.body38 ] + %cmp8 = icmp ult i32 %add, %1 + br i1 %cmp8, label %for.body10, label %for.cond.cleanup9 + +for.cond.cleanup9: ; preds = %for.cond7.loopexit, %for.body + %weight.1.lcssa = phi ptr [ %weight.0198, %for.body ], [ %weight.2.lcssa, %for.cond7.loopexit ] + %add105 = add i32 %d.0199, 32 + %cmp6 = icmp ult i32 %add105, %2 + br i1 %cmp6, label %for.body, label %for.cond.cleanup + +for.body10: ; preds = %for.cond7.loopexit, %for.body + %h.0195 = phi i32 [ %add, %for.cond7.loopexit ], [ 0, %for.body ] + %6 = load ptr, ptr %addr, align 4 + %7 = load i32, ptr %start, align 8 + %and.i = and i32 %7, 1792 + %cmp19 = icmp eq i32 %and.i, 0 + %add = add i32 %h.0195, 8 + %cmp20 = icmp ult i32 %add, %3 + %spec.select = select i1 %cmp20, i32 %0, i32 0 + %cond21 = select i1 %cmp19, i32 0, i32 %spec.select + %8 = load i32, ptr %start24, align 8 + %and.i189 = and i32 %8, 1792 + %cmp26 = icmp ne i32 %and.i189, 0 + %add28 = add i32 %h.0195, 16 + %cmp29 = icmp ult i32 %add28, %3 + %not.or.cond = and i1 %cmp29, %cmp26 + %mul32 = zext i1 %not.or.cond to i32 + %cond34 = shl i32 %0, %mul32 + tail call void @llvm.hexagon.M8.mxclracc.hf() + br i1 %cmp36190, label %for.cond7.loopexit, label %for.body38.preheader + +for.body38.preheader: ; preds = %for.body10 + %shr = lshr exact i32 %h.0195, 3 + %mul11 = mul i32 %shr, %0 + %add.ptr12 = getelementptr inbounds ptr, ptr %6, i32 %mul11 + br label %for.body38 + +for.body38: ; preds = %for.body38, %for.body38.preheader + %dd.0193 = phi i32 [ %add100, %for.body38 ], [ 0, %for.body38.preheader ] + %weight.2192 = phi ptr [ %add.ptr99, %for.body38 ], [ %weight_in.addr.0200, %for.body38.preheader ] + %ppIn.0191 = phi ptr [ %incdec.ptr, %for.body38 ], [ %add.ptr12, %for.body38.preheader ] + %add.ptr39 = getelementptr inbounds ptr, ptr %ppIn.0191, i32 %cond34 + %9 = bitcast ptr %add.ptr39 to ptr + %10 = load i32, ptr %9, align 4 + %add.ptr40 = getelementptr inbounds ptr, ptr %ppIn.0191, i32 %cond21 + %11 = bitcast ptr %add.ptr40 to ptr + %12 = load i32, ptr %11, align 4 + %incdec.ptr = getelementptr inbounds ptr, ptr %ppIn.0191, i32 1 + %13 = bitcast ptr %ppIn.0191 to ptr + %14 = load i32, ptr %13, align 4 + %15 = load i32, ptr %start24, align 8 + %or = or i32 %15, %14 + %sub.ptr.sub = sub i32 %12, %14 + %16 = load i32, ptr %range, align 4 + %or46 = or i32 %16, %sub.ptr.sub + tail call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %or, i32 %or46) + %17 = ptrtoint ptr %weight.2192 to i32 + %18 = load i32, ptr %arrayidx47, align 4 + tail call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %17, i32 %18) + %19 = load i32, ptr %arrayidx47, align 4 + %add50 = add i32 %19, 1 + %add.ptr51 = getelementptr inbounds i8, ptr %weight.2192, i32 %add50 + %20 = load i32, ptr %start, align 8 + %or55 = or i32 %20, %12 + %sub.ptr.sub58 = sub i32 %10, %12 + %21 = load i32, ptr %range61, align 4 + %or62 = or i32 %21, %sub.ptr.sub58 + tail call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %or55, i32 %or62) + %22 = ptrtoint ptr %add.ptr51 to i32 + %23 = load i32, ptr %arrayidx64, align 8 + tail call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %22, i32 %23) + %24 = load i32, ptr %arrayidx64, align 8 + %add67 = add i32 %24, 1 + %add.ptr68 = getelementptr inbounds i8, ptr %add.ptr51, i32 %add67 + tail call void @llvm.hexagon.M8.mxclracc.hf() + %25 = ptrtoint ptr %add.ptr68 to i32 + %26 = load i32, ptr %arrayidx70, align 4 + tail call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %25, i32 %26) + %27 = load i32, ptr %arrayidx71, align 4 + %or72 = or i32 %27, %14 + %28 = load i32, ptr %range, align 4 + %or79 = or i32 %28, %sub.ptr.sub + tail call void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32 %or72, i32 %or79) + %29 = load i32, ptr %arrayidx70, align 4 + %add82 = add i32 %29, 1 + %add.ptr83 = getelementptr inbounds i8, ptr %add.ptr68, i32 %add82 + %30 = load i32, ptr %arrayidx85, align 8 + %or86 = or i32 %30, %12 + %31 = load i32, ptr %range61, align 4 + %or93 = or i32 %31, %sub.ptr.sub58 + tail call void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32 %or86, i32 %or93) + %32 = ptrtoint ptr %add.ptr83 to i32 + %33 = load i32, ptr %arrayidx95, align 8 + tail call void @llvm.hexagon.M8.mxmemdp.wei.hf(i32 %32, i32 %33) + %34 = load i32, ptr %arrayidx95, align 8 + %add98 = add i32 %34, 1 + %add.ptr99 = getelementptr inbounds i8, ptr %add.ptr83, i32 %add98 + tail call void @llvm.hexagon.M8.mxclracc.hf() + %add100 = add i32 %dd.0193, 32 + %cmp36 = icmp ult i32 %add100, %4 + br i1 %cmp36, label %for.body38, label %for.cond7.loopexit +} + +; Function Attrs: nounwind writeonly +declare void @llvm.hexagon.M8.mxmem2.st.bias(ptr) #1 + +; Function Attrs: nounwind +declare void @llvm.hexagon.M8.mxclracc.hf() #2 + +; Function Attrs: convergent noduplicate nomerge nounwind writeonly +declare void @llvm.hexagon.M8.mxmems.blk.sm.act.hf(i32, i32) #3 + +; Function Attrs: convergent noduplicate nomerge nounwind writeonly +declare void @llvm.hexagon.M8.mxmemdp.wei.hf(i32, i32) #3 + +; Function Attrs: convergent noduplicate nomerge nounwind writeonly +declare void @llvm.hexagon.M8.mxmem.blk.sm.act.hf(i32, i32) #3 + +attributes #0 = { "target-cpu"="hexagonv68" "target-features"="+hmx" } +attributes #1 = { nounwind writeonly "target-cpu"="hexagonv68" "target-features"="+hmx" } +attributes #2 = { nounwind "target-cpu"="hexagonv68" "target-features"="+hmx" } +attributes #3 = { convergent noduplicate nomerge nounwind writeonly "target-cpu"="hexagonv68" "target-features"="+hmx" } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
