https://github.com/yandalur updated https://github.com/llvm/llvm-project/pull/243182
>From c59c1638b34c730455a2ddc5ee663311b015bfbd Mon Sep 17 00:00:00 2001 From: Yashas Andaluri <[email protected]> Date: Fri, 13 Nov 2026 19:11:28 +0530 Subject: [PATCH 1/3] [Hexagon] Add the hlxcc and preserve_hlx calling conventions Add two calling conventions for calls between HLX and non-HLX code: - hexagon_hlxcc (Clang: __attribute__((hlxcc))) also preserves the HLX callee-saved registers L12-L15, so a function without HLX types in its signature can be called while HLX values are live in those registers. - hexagon_preserve_hlx (Clang: __attribute__((preserve_hlx))) preserves all HLX registers L0-L15. It cannot be used on functions with HLX types in their signature, since L0-L11 pass HLX arguments and return values. Without HLX, both behave like the default calling convention. They are also mapped to DWARF calling convention codes and to libclang. Assisted by AI Co-authored-by: pavani karveti <[email protected]> --- clang/include/clang-c/Index.h | 2 + clang/include/clang/Basic/Attr.td | 12 +++ clang/include/clang/Basic/AttrDocs.td | 35 ++++++++ .../clang/Basic/DiagnosticSemaKinds.td | 2 + clang/include/clang/Basic/Specifiers.h | 2 + clang/include/clang/Sema/SemaHexagon.h | 4 + clang/lib/AST/ItaniumMangle.cpp | 2 + clang/lib/AST/Type.cpp | 6 ++ clang/lib/AST/TypePrinter.cpp | 12 +++ clang/lib/Basic/Targets/Hexagon.cpp | 12 +++ clang/lib/Basic/Targets/Hexagon.h | 2 + clang/lib/CodeGen/CGCall.cpp | 10 +++ clang/lib/CodeGen/CGDebugInfo.cpp | 4 + clang/lib/Sema/SemaDeclAttr.cpp | 14 +++ clang/lib/Sema/SemaHexagon.cpp | 8 ++ clang/lib/Sema/SemaType.cpp | 15 ++++ clang/test/CodeGen/hexagon-hlxcc.c | 19 ++++ clang/test/CodeGen/hexagon-preserve-hlx.c | 19 ++++ clang/test/DebugInfo/Generic/cc.c | 17 ++++ ...a-attribute-supported-attributes-list.test | 2 + clang/test/Sema/hexagon-hlx-cc-other-target.c | 5 ++ clang/test/Sema/hexagon-hlxcc.c | 23 +++++ clang/test/Sema/hexagon-preserve-hlx.c | 40 +++++++++ clang/tools/libclang/CXType.cpp | 2 + llvm/include/llvm/AsmParser/LLToken.h | 2 + llvm/include/llvm/BinaryFormat/Dwarf.def | 2 + llvm/include/llvm/IR/CallingConv.h | 10 +++ llvm/lib/AsmParser/LLLexer.cpp | 2 + llvm/lib/AsmParser/LLParser.cpp | 6 ++ llvm/lib/IR/AsmWriter.cpp | 6 ++ llvm/lib/IR/Function.cpp | 2 + .../Target/Hexagon/HexagonRegisterInfo.cpp | 17 +++- .../lib/Target/Hexagon/HexagonRegisterInfo.td | 7 ++ llvm/test/Bitcode/compatibility.ll | 8 ++ llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll | 32 +++++++ llvm/test/CodeGen/Hexagon/hlxcc-callconv.ll | 42 +++++++++ .../CodeGen/Hexagon/preserve-hlx-callconv.ll | 40 +++++++++ .../CodeGen/Hexagon/preserve-hlx-spill.ll | 86 +++++++++++++++++++ 38 files changed, 528 insertions(+), 3 deletions(-) create mode 100644 clang/test/CodeGen/hexagon-hlxcc.c create mode 100644 clang/test/CodeGen/hexagon-preserve-hlx.c create mode 100644 clang/test/Sema/hexagon-hlx-cc-other-target.c create mode 100644 clang/test/Sema/hexagon-hlxcc.c create mode 100644 clang/test/Sema/hexagon-preserve-hlx.c create mode 100644 llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll create mode 100644 llvm/test/CodeGen/Hexagon/hlxcc-callconv.ll create mode 100644 llvm/test/CodeGen/Hexagon/preserve-hlx-callconv.ll create mode 100644 llvm/test/CodeGen/Hexagon/preserve-hlx-spill.ll diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index 6f9928535ffc65..6114ead2ccbacb 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -3100,6 +3100,8 @@ enum CXCallingConv { CXCallingConv_RISCVVLSCall_16384 = 31, CXCallingConv_RISCVVLSCall_32768 = 32, CXCallingConv_RISCVVLSCall_65536 = 33, + CXCallingConv_HexagonHLXCall = 34, + CXCallingConv_HexagonPreserveHLX = 35, CXCallingConv_Invalid = 100, CXCallingConv_Unexposed = 200 diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index d68e3253628d8d..0849fa6c2fb431 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -3695,6 +3695,18 @@ def RISCVVLSCC: DeclOrTypeAttr, TargetSpecificAttr<TargetRISCV> { let Documentation = [RISCVVLSCCDocs]; } +def HexagonHLXCall : DeclOrTypeAttr, TargetSpecificAttr<TargetHexagon> { + let Spellings = [Clang<"hlxcc">]; + let Subjects = SubjectList<[FunctionLike]>; + let Documentation = [HexagonHLXCallDocs]; +} + +def HexagonPreserveHLX : DeclOrTypeAttr, TargetSpecificAttr<TargetHexagon> { + let Spellings = [Clang<"preserve_hlx">]; + let Subjects = SubjectList<[FunctionLike]>; + let Documentation = [HexagonPreserveHLXDocs]; +} + def HexagonHLXVectorStackLimit : InheritableAttr, TargetSpecificAttr<TargetHexagon> { let Spellings = [Clang<"hlx_vector_stack_limit">]; diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 060908a212f008..eadbbbb20cd2e8 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -6979,6 +6979,41 @@ the ABI. This variant aims to pass fixed-length vectors via vector registers, if possible, rather than through general-purpose registers.}]; } +def HexagonHLXCallDocs : Documentation { + let Category = DocCatCallingConvs; + let Heading = "hlxcc"; + let Content = [{ +On Hexagon with HLX enabled, the ``hlxcc`` calling convention makes a function +preserve the HLX vector registers ``L12``-``L15``, as functions with HLX vector +arguments or return values already do. This lets callers keep HLX values in +these registers across calls to functions without HLX types in their signature. +It maps to the LLVM ``hexagon_hlxcc`` calling convention. + +.. code-block:: c + + void f(int *) __attribute__((hlxcc)); + }]; +} + +def HexagonPreserveHLXDocs : Documentation { + let Category = DocCatCallingConvs; + let Heading = "preserve_hlx"; + let Content = [{ +On Hexagon with HLX enabled, the ``preserve_hlx`` calling convention makes a +function preserve all HLX vector registers (``L0``-``L15``), so callers do not +need to save any HLX values around calls to it. It maps to the LLVM +``hexagon_preserve_hlx`` calling convention. + +Because HLX vector arguments and return values are passed in HLX registers, it +is an error to use ``preserve_hlx`` on a function whose return type or any +parameter type is an HLX vector type. + +.. code-block:: c + + void g(int *) __attribute__((preserve_hlx)); + }]; +} + def HexagonHLXVectorStackLimitDocs : Documentation { let Category = DocCatFunction; let Content = [{ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 5bf172b8f0b573..05ed6a63061e08 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3455,6 +3455,8 @@ def err_hlx_invalid_field : Error< "aggregate objects with HLX types are not allowed">; def err_hlx_invalid_object : Error< "global, static or variable sized objects with HLX types are not allowed">; +def err_hlx_invalid_attribute : Error< + "%0 attribute cannot be used on functions with HLX types in their signature">; def err_sve_vector_in_non_sve_target : Error< "SVE vector type %0 cannot be used in a target without sve">; def err_sve_vector_in_non_streaming_function : Error< diff --git a/clang/include/clang/Basic/Specifiers.h b/clang/include/clang/Basic/Specifiers.h index 67742038bb6a8b..3637cb14a5ef29 100644 --- a/clang/include/clang/Basic/Specifiers.h +++ b/clang/include/clang/Basic/Specifiers.h @@ -315,6 +315,8 @@ namespace clang { CC_RISCVVLSCall_16384, // __attribute__((riscv_vls_cc(16384))) CC_RISCVVLSCall_32768, // __attribute__((riscv_vls_cc(32768))) CC_RISCVVLSCall_65536, // __attribute__((riscv_vls_cc(65536))) + CC_HexagonHLXCall, // __attribute__((hlxcc)) + CC_HexagonPreserveHLX, // __attribute__((preserve_hlx)) }; /// Checks whether the given calling convention supports variadic diff --git a/clang/include/clang/Sema/SemaHexagon.h b/clang/include/clang/Sema/SemaHexagon.h index dc3976b469ca8a..4459f1b34d1588 100644 --- a/clang/include/clang/Sema/SemaHexagon.h +++ b/clang/include/clang/Sema/SemaHexagon.h @@ -31,6 +31,10 @@ class SemaHexagon : public SemaBase { /// Returns true if \p T is an HLX vector or vector pair type, or an array /// of them, and HLX is enabled. bool isHLXVectorType(QualType T) const; + + /// Returns true if the return type or a parameter type of \p FT is an HLX + /// vector type. + bool hasHLXVectorInSignature(const FunctionType *FT) const; }; } // namespace clang diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp index 6ebbdbd5369245..7fcc8c7bd85cec 100644 --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -3659,6 +3659,8 @@ StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) { case CC_PreserveAll: case CC_M68kRTD: case CC_PreserveNone: + case CC_HexagonHLXCall: + case CC_HexagonPreserveHLX: case CC_RISCVVectorCall: #define CC_VLS_CASE(ABI_VLEN) case CC_RISCVVLSCall_##ABI_VLEN: CC_VLS_CASE(32) diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index 7eafa13962e793..222ff4002f5a27 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -3890,6 +3890,10 @@ StringRef FunctionType::getNameForCallConv(CallingConv CC) { return "m68k_rtd"; case CC_PreserveNone: return "preserve_none"; + case CC_HexagonHLXCall: + return "hlxcc"; + case CC_HexagonPreserveHLX: + return "preserve_hlx"; // clang-format off case CC_RISCVVectorCall: return "riscv_vector_cc"; #define CC_VLS_CASE(ABI_VLEN) \ @@ -4761,6 +4765,8 @@ bool AttributedType::isCallingConv() const { case attr::PreserveAll: case attr::M68kRTD: case attr::PreserveNone: + case attr::HexagonHLXCall: + case attr::HexagonPreserveHLX: case attr::RISCVVectorCC: case attr::RISCVVLSCC: return true; diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index a90b2d2c0cb94d..6bbb0cb064de96 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1174,6 +1174,12 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info, case CC_PreserveNone: OS << " __attribute__((preserve_none))"; break; + case CC_HexagonHLXCall: + OS << " __attribute__((hlxcc))"; + break; + case CC_HexagonPreserveHLX: + OS << " __attribute__((preserve_hlx))"; + break; case CC_RISCVVectorCall: OS << "__attribute__((riscv_vector_cc))"; break; @@ -2117,6 +2123,12 @@ void TypePrinter::printAttributedAfter(const AttributedType *T, case attr::M68kRTD: OS << "m68k_rtd"; break; + case attr::HexagonHLXCall: + OS << "hlxcc"; + break; + case attr::HexagonPreserveHLX: + OS << "preserve_hlx"; + break; case attr::RISCVVectorCC: OS << "riscv_vector_cc"; break; diff --git a/clang/lib/Basic/Targets/Hexagon.cpp b/clang/lib/Basic/Targets/Hexagon.cpp index 3355c96fa83efd..ebd913e58b402d 100644 --- a/clang/lib/Basic/Targets/Hexagon.cpp +++ b/clang/lib/Basic/Targets/Hexagon.cpp @@ -136,6 +136,18 @@ bool HexagonTargetInfo::handleTargetFeatures(std::vector<std::string> &Features, bool HexagonTargetInfo::hasBFloat16Type() const { return HasBFloat16; } +TargetInfo::CallingConvCheckResult +HexagonTargetInfo::checkCallingConvention(CallingConv CC) const { + switch (CC) { + case CC_C: + case CC_HexagonHLXCall: + case CC_HexagonPreserveHLX: + return CCCR_OK; + default: + return CCCR_Warning; + } +} + const char *const HexagonTargetInfo::GCCRegNames[] = { // Scalar registers: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", diff --git a/clang/lib/Basic/Targets/Hexagon.h b/clang/lib/Basic/Targets/Hexagon.h index facbddb81ddc92..81ca1586198f94 100644 --- a/clang/lib/Basic/Targets/Hexagon.h +++ b/clang/lib/Basic/Targets/Hexagon.h @@ -95,6 +95,8 @@ class LLVM_LIBRARY_VISIBILITY HexagonTargetInfo : public TargetInfo { bool hasBFloat16Type() const override; + CallingConvCheckResult checkCallingConvention(CallingConv CC) const override; + bool initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index ae73b16a42782d..12abc23ac4a857 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -110,6 +110,10 @@ unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) { return llvm::CallingConv::M68k_RTD; case CC_PreserveNone: return llvm::CallingConv::PreserveNone; + case CC_HexagonHLXCall: + return llvm::CallingConv::Hexagon_HLXCall; + case CC_HexagonPreserveHLX: + return llvm::CallingConv::Hexagon_PreserveHLX; // clang-format off case CC_RISCVVectorCall: return llvm::CallingConv::RISCV_VectorCall; // clang-format on @@ -314,6 +318,12 @@ static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D, if (D->hasAttr<PreserveNoneAttr>()) return CC_PreserveNone; + if (D->hasAttr<HexagonHLXCallAttr>()) + return CC_HexagonHLXCall; + + if (D->hasAttr<HexagonPreserveHLXAttr>()) + return CC_HexagonPreserveHLX; + if (D->hasAttr<RISCVVectorCCAttr>()) return CC_RISCVVectorCall; diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 279a7beceeba1f..b5a0aacb4fdd3d 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -1905,6 +1905,10 @@ static unsigned getDwarfCC(CallingConv CC, const llvm::Triple &T) { return llvm::dwarf::DW_CC_LLVM_M68kRTD; case CC_PreserveNone: return llvm::dwarf::DW_CC_LLVM_PreserveNone; + case CC_HexagonHLXCall: + return llvm::dwarf::DW_CC_LLVM_HexagonHLXCall; + case CC_HexagonPreserveHLX: + return llvm::dwarf::DW_CC_LLVM_HexagonPreserveHLX; case CC_RISCVVectorCall: return llvm::dwarf::DW_CC_LLVM_RISCVVectorCall; #define CC_VLS_CASE(ABI_VLEN) case CC_RISCVVLSCall_##ABI_VLEN: diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index b878a1f8734fb7..cf630aa0889aaf 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5651,6 +5651,12 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { case ParsedAttr::AT_PreserveNone: D->addAttr(::new (S.Context) PreserveNoneAttr(S.Context, AL)); return; + case ParsedAttr::AT_HexagonHLXCall: + D->addAttr(::new (S.Context) HexagonHLXCallAttr(S.Context, AL)); + return; + case ParsedAttr::AT_HexagonPreserveHLX: + D->addAttr(::new (S.Context) HexagonPreserveHLXAttr(S.Context, AL)); + return; case ParsedAttr::AT_RISCVVectorCC: D->addAttr(::new (S.Context) RISCVVectorCCAttr(S.Context, AL)); return; @@ -5922,6 +5928,12 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC, case ParsedAttr::AT_PreserveNone: CC = CC_PreserveNone; break; + case ParsedAttr::AT_HexagonHLXCall: + CC = CC_HexagonHLXCall; + break; + case ParsedAttr::AT_HexagonPreserveHLX: + CC = CC_HexagonPreserveHLX; + break; case ParsedAttr::AT_RISCVVectorCC: CC = CC_RISCVVectorCall; break; @@ -8205,6 +8217,8 @@ ProcessDeclAttribute(Sema &S, Decl *D, const ParsedAttr &AL, case ParsedAttr::AT_AArch64SVEPcs: case ParsedAttr::AT_M68kRTD: case ParsedAttr::AT_PreserveNone: + case ParsedAttr::AT_HexagonHLXCall: + case ParsedAttr::AT_HexagonPreserveHLX: case ParsedAttr::AT_RISCVVectorCC: case ParsedAttr::AT_RISCVVLSCC: handleCallConvAttr(S, D, AL); diff --git a/clang/lib/Sema/SemaHexagon.cpp b/clang/lib/Sema/SemaHexagon.cpp index f18c6e7719b83c..e3bc1c3ba6e781 100644 --- a/clang/lib/Sema/SemaHexagon.cpp +++ b/clang/lib/Sema/SemaHexagon.cpp @@ -314,4 +314,12 @@ bool SemaHexagon::isHLXVectorType(QualType T) const { return Size == 2048 || Size == 4096; } +bool SemaHexagon::hasHLXVectorInSignature(const FunctionType *FT) const { + if (isHLXVectorType(FT->getReturnType())) + return true; + const auto *FPT = dyn_cast<FunctionProtoType>(FT); + return FPT && llvm::any_of(FPT->getParamTypes(), + [&](QualType T) { return isHLXVectorType(T); }); +} + } // namespace clang diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 0e49975e4bed04..1ca4e0766d79a6 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -38,6 +38,7 @@ #include "clang/Sema/ScopeInfo.h" #include "clang/Sema/SemaCUDA.h" #include "clang/Sema/SemaHLSL.h" +#include "clang/Sema/SemaHexagon.h" #include "clang/Sema/SemaObjC.h" #include "clang/Sema/SemaOpenMP.h" #include "clang/Sema/Template.h" @@ -142,6 +143,8 @@ static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr, case ParsedAttr::AT_PreserveAll: \ case ParsedAttr::AT_M68kRTD: \ case ParsedAttr::AT_PreserveNone: \ + case ParsedAttr::AT_HexagonHLXCall: \ + case ParsedAttr::AT_HexagonPreserveHLX: \ case ParsedAttr::AT_RISCVVectorCC: \ case ParsedAttr::AT_RISCVVLSCC @@ -7900,6 +7903,10 @@ static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) { return createSimpleAttr<M68kRTDAttr>(Ctx, Attr); case ParsedAttr::AT_PreserveNone: return createSimpleAttr<PreserveNoneAttr>(Ctx, Attr); + case ParsedAttr::AT_HexagonHLXCall: + return createSimpleAttr<HexagonHLXCallAttr>(Ctx, Attr); + case ParsedAttr::AT_HexagonPreserveHLX: + return createSimpleAttr<HexagonPreserveHLXAttr>(Ctx, Attr); case ParsedAttr::AT_RISCVVectorCC: return createSimpleAttr<RISCVVectorCCAttr>(Ctx, Attr); case ParsedAttr::AT_RISCVVLSCC: { @@ -8451,6 +8458,14 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, const FunctionType *fn = unwrapped.get(); CallingConv CCOld = fn->getCallConv(); + + // preserve_hlx functions cannot pass or return HLX vectors, as HLX vectors + // are passed in the registers they preserve. + if (CC == CC_HexagonPreserveHLX && S.Hexagon().hasHLXVectorInSignature(fn)) { + S.Diag(attr.getLoc(), diag::err_hlx_invalid_attribute) << attr; + attr.setInvalid(); + return true; + } Attr *CCAttr = getCCTypeAttr(S.Context, attr); if (CCOld != CC) { diff --git a/clang/test/CodeGen/hexagon-hlxcc.c b/clang/test/CodeGen/hexagon-hlxcc.c new file mode 100644 index 00000000000000..5c0b773e945336 --- /dev/null +++ b/clang/test/CodeGen/hexagon-hlxcc.c @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -emit-llvm -o - %s | FileCheck %s --check-prefix=C +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -emit-llvm -x c++ -o - %s \ +// RUN: | FileCheck %s --check-prefix=CXX + +void __attribute__((hlxcc)) f(int *); + +// C-LABEL: define dso_local void @g( +// C: call hexagon_hlxcc void @f( +// CXX-LABEL: define dso_local void @_Z1gPi( +// CXX: call hexagon_hlxcc void @_Z1fPi( +void g(int *a) { f(a); } + +// C: declare hexagon_hlxcc void @f( +// CXX: declare hexagon_hlxcc void @_Z1fPi( +// C: define dso_local hexagon_hlxcc void @h( +// CXX: define dso_local hexagon_hlxcc void @_Z1hPi( +void __attribute__((hlxcc)) h(int *a) { f(a); } diff --git a/clang/test/CodeGen/hexagon-preserve-hlx.c b/clang/test/CodeGen/hexagon-preserve-hlx.c new file mode 100644 index 00000000000000..a725b8ecc6c456 --- /dev/null +++ b/clang/test/CodeGen/hexagon-preserve-hlx.c @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -emit-llvm -o - %s | FileCheck %s --check-prefix=C +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -emit-llvm -x c++ -o - %s \ +// RUN: | FileCheck %s --check-prefix=CXX + +void __attribute__((preserve_hlx)) f(int *); + +// C-LABEL: define dso_local void @g( +// C: call hexagon_preserve_hlx void @f( +// CXX-LABEL: define dso_local void @_Z1gPi( +// CXX: call hexagon_preserve_hlx void @_Z1fPi( +void g(int *a) { f(a); } + +// C: declare hexagon_preserve_hlx void @f( +// CXX: declare hexagon_preserve_hlx void @_Z1fPi( +// C: define dso_local hexagon_preserve_hlx void @h( +// CXX: define dso_local hexagon_preserve_hlx void @_Z1hPi( +void __attribute__((preserve_hlx)) h(int *a) { f(a); } diff --git a/clang/test/DebugInfo/Generic/cc.c b/clang/test/DebugInfo/Generic/cc.c index 38d6a635a07fa0..968c6585359024 100644 --- a/clang/test/DebugInfo/Generic/cc.c +++ b/clang/test/DebugInfo/Generic/cc.c @@ -5,6 +5,7 @@ // RUN: %clang_cc1 -triple i386-pc-linux-gnu -o - -emit-llvm -debug-info-kind=limited %s | FileCheck %s --check-prefix=LINUX32 // RUN: %clang_cc1 -triple armv7--linux-gnueabihf -o - -emit-llvm -debug-info-kind=limited %s | FileCheck %s --check-prefix=ARM // RUN: %clang_cc1 -triple spirv64-unknown-unknown -o - -emit-llvm -debug-info-kind=limited %s | FileCheck %s --check-prefix=SPIRV +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 -target-feature +hlx -o - -emit-llvm -debug-info-kind=limited %s | FileCheck %s --check-prefix=HEXAGON // enum CallingConv { // CC_C, // __attribute__((cdecl)) @@ -25,6 +26,8 @@ // CC_PreserveMost, // __attribute__((preserve_most)) // CC_PreserveAll, // __attribute__((preserve_all)) // CC_PreserveNone, // __attribute__((preserve_none)) +// CC_HexagonHLXCall, // __attribute__((hlxcc)) +// CC_HexagonPreserveHLX, // __attribute__((preserve_hlx)) // }; #ifdef __x86_64__ @@ -142,3 +145,17 @@ int add_spir(int a, int b) { return a+b; } #endif + +#ifdef __hexagon__ +// HEXAGON: !DISubprogram({{.*}}"add_hlxcc", {{.*}}type: ![[FTY:[0-9]+]] +// HEXAGON: ![[FTY]] = !DISubroutineType({{.*}}cc: DW_CC_LLVM_HexagonHLXCall, +__attribute__((hlxcc)) int add_hlxcc(int a, int b) { + return a+b; +} + +// HEXAGON: !DISubprogram({{.*}}"add_preserve_hlx", {{.*}}type: ![[FTY:[0-9]+]] +// HEXAGON: ![[FTY]] = !DISubroutineType({{.*}}cc: DW_CC_LLVM_HexagonPreserveHLX, +__attribute__((preserve_hlx)) int add_preserve_hlx(int a, int b) { + return a+b; +} +#endif diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test index 2b5c4741dc2166..c78dba4dee8747 100644 --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -93,7 +93,9 @@ // CHECK-NEXT: HLSLVkBuiltIn (SubjectMatchRule_variable_is_parameter, SubjectMatchRule_field, SubjectMatchRule_function) // CHECK-NEXT: HLSLVkLocation (SubjectMatchRule_variable_is_parameter, SubjectMatchRule_field, SubjectMatchRule_function) // CHECK-NEXT: HLSLVkPushConstant (SubjectMatchRule_variable_is_global) +// CHECK-NEXT: HexagonHLXCall (SubjectMatchRule_hasType_functionType) // CHECK-NEXT: HexagonHLXVectorStackLimit (SubjectMatchRule_function) +// CHECK-NEXT: HexagonPreserveHLX (SubjectMatchRule_hasType_functionType) // CHECK-NEXT: Hot (SubjectMatchRule_function) // CHECK-NEXT: HybridPatchable (SubjectMatchRule_function) // CHECK-NEXT: IBAction (SubjectMatchRule_objc_method_is_instance) diff --git a/clang/test/Sema/hexagon-hlx-cc-other-target.c b/clang/test/Sema/hexagon-hlx-cc-other-target.c new file mode 100644 index 00000000000000..4736326fb55acf --- /dev/null +++ b/clang/test/Sema/hexagon-hlx-cc-other-target.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -verify %s + +// hlxcc and preserve_hlx are Hexagon-only attributes. +void __attribute__((hlxcc)) f(void); // expected-warning {{unknown attribute 'hlxcc' ignored}} +void __attribute__((preserve_hlx)) g(void); // expected-warning {{unknown attribute 'preserve_hlx' ignored}} diff --git a/clang/test/Sema/hexagon-hlxcc.c b/clang/test/Sema/hexagon-hlxcc.c new file mode 100644 index 00000000000000..db135ad277e3e2 --- /dev/null +++ b/clang/test/Sema/hexagon-hlxcc.c @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -fsyntax-only -verify %s + +typedef __attribute__((hlxcc)) int invalid_typedef; // expected-warning {{'hlxcc' attribute only applies to functions}} + // expected-warning@-1 {{'hlxcc' only applies to function types; type here is 'int'}} + +void __attribute__((hlxcc(0))) foo0(void); // expected-error {{'hlxcc' attribute takes no arguments}} + +void __attribute__((hlxcc, cdecl)) foo1(void); // expected-error {{not compatible}} +void __attribute__((hlxcc, preserve_hlx)) foo1(void); // expected-error {{not compatible}} + +void __attribute__((cdecl)) foo2(void); // expected-note {{previous declaration is here}} +void __attribute__((hlxcc)) foo2(void) {} // expected-error {{function declared 'hlxcc' here was previously declared 'cdecl'}} + +void foo3(void); // expected-note {{previous declaration is here}} +void __attribute__((hlxcc)) foo3(void) {} // expected-error {{function declared 'hlxcc' here was previously declared without calling convention}} + +void __attribute__((hlxcc)) foo4(void); +int bad = foo4; // expected-error {{incompatible pointer to integer conversion initializing 'int' with an expression of type 'void (void) __attribute__((hlxcc))'}} + +// Function pointer and typedef uses are fine. +void (*fp)(int) __attribute__((hlxcc)); +typedef void __attribute__((hlxcc)) ft(int); diff --git a/clang/test/Sema/hexagon-preserve-hlx.c b/clang/test/Sema/hexagon-preserve-hlx.c new file mode 100644 index 00000000000000..fc56faadfbefa4 --- /dev/null +++ b/clang/test/Sema/hexagon-preserve-hlx.c @@ -0,0 +1,40 @@ +// RUN: %clang_cc1 -triple hexagon-unknown-elf -target-cpu hexagonv85 \ +// RUN: -target-feature +hlx -fsyntax-only -verify %s + +typedef short HLX_Vector __attribute__((__vector_size__(2048))); +typedef short HLX_VectorPair __attribute__((__vector_size__(4096))); + +typedef __attribute__((preserve_hlx)) int invalid_typedef; // expected-warning {{'preserve_hlx' attribute only applies to functions}} + // expected-warning@-1 {{'preserve_hlx' only applies to function types; type here is 'int'}} + +void __attribute__((preserve_hlx(0))) foo0(void); // expected-error {{'preserve_hlx' attribute takes no arguments}} + +void __attribute__((preserve_hlx, cdecl)) foo1(void); // expected-error {{not compatible}} + +void __attribute__((cdecl)) foo2(void); // expected-note {{previous declaration is here}} +void __attribute__((preserve_hlx)) foo2(void) {} // expected-error {{function declared 'preserve_hlx' here was previously declared 'cdecl'}} + +void foo3(void); // expected-note {{previous declaration is here}} +void __attribute__((preserve_hlx)) foo3(void) {} // expected-error {{function declared 'preserve_hlx' here was previously declared without calling convention}} + +void __attribute__((preserve_hlx)) foo4(void); +int bad = foo4; // expected-error {{incompatible pointer to integer conversion initializing 'int' with an expression of type 'void (void) __attribute__((preserve_hlx))'}} + +void __attribute__((preserve_hlx)) ok(int *, HLX_Vector *); + +HLX_Vector __attribute__((preserve_hlx)) bar0(void); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} +HLX_Vector __attribute__((preserve_hlx)) bar0(void) {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} + +HLX_Vector __attribute__((preserve_hlx)) bar1(); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} +HLX_Vector __attribute__((preserve_hlx)) bar1() {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} + +void __attribute__((preserve_hlx)) bar2(HLX_Vector a); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} +void __attribute__((preserve_hlx)) bar2(HLX_Vector a) {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} + +void __attribute__((preserve_hlx)) bar3(int, HLX_VectorPair); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} + +void (*__attribute__((preserve_hlx)) bar4)(HLX_Vector); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} + +// Function pointer and typedef uses are fine. +void (*fp)(int) __attribute__((preserve_hlx)); +typedef void __attribute__((preserve_hlx)) ft(int); diff --git a/clang/tools/libclang/CXType.cpp b/clang/tools/libclang/CXType.cpp index 912ce10afc251f..cc447bdf37e48b 100644 --- a/clang/tools/libclang/CXType.cpp +++ b/clang/tools/libclang/CXType.cpp @@ -730,6 +730,8 @@ CXCallingConv clang_getFunctionTypeCallingConv(CXType X) { TCALLINGCONV(RISCVVLSCall_16384); TCALLINGCONV(RISCVVLSCall_32768); TCALLINGCONV(RISCVVLSCall_65536); + TCALLINGCONV(HexagonHLXCall); + TCALLINGCONV(HexagonPreserveHLX); case CC_DeviceKernel: return CXCallingConv_Unexposed; break; diff --git a/llvm/include/llvm/AsmParser/LLToken.h b/llvm/include/llvm/AsmParser/LLToken.h index 1adb04d13ab8cd..9a843b5190a883 100644 --- a/llvm/include/llvm/AsmParser/LLToken.h +++ b/llvm/include/llvm/AsmParser/LLToken.h @@ -193,6 +193,8 @@ enum Kind { kw_cheriot_compartmentcallcc, kw_cheriot_compartmentcalleecc, kw_cheriot_librarycallcc, + kw_hexagon_hlxcc, + kw_hexagon_preserve_hlx, // Attributes: kw_attributes, diff --git a/llvm/include/llvm/BinaryFormat/Dwarf.def b/llvm/include/llvm/BinaryFormat/Dwarf.def index f83f86c9bf9e43..aa0663bcec72b5 100644 --- a/llvm/include/llvm/BinaryFormat/Dwarf.def +++ b/llvm/include/llvm/BinaryFormat/Dwarf.def @@ -1229,6 +1229,8 @@ HANDLE_DW_CC(0xcd, LLVM_PreserveNone) HANDLE_DW_CC(0xce, LLVM_RISCVVectorCall) HANDLE_DW_CC(0xcf, LLVM_SwiftTail) HANDLE_DW_CC(0xd0, LLVM_RISCVVLSCall) +HANDLE_DW_CC(0xd1, LLVM_HexagonHLXCall) +HANDLE_DW_CC(0xd2, LLVM_HexagonPreserveHLX) // From GCC source code (include/dwarf2.h): This DW_CC_ value is not currently // generated by any toolchain. It is used internally to GDB to indicate OpenCL // C functions that have been compiled with the IBM XL C for OpenCL compiler and diff --git a/llvm/include/llvm/IR/CallingConv.h b/llvm/include/llvm/IR/CallingConv.h index 249c512dda5324..71fb60a28255f2 100644 --- a/llvm/include/llvm/IR/CallingConv.h +++ b/llvm/include/llvm/IR/CallingConv.h @@ -297,6 +297,16 @@ namespace CallingConv { /// stateless compartment. CHERIoT_LibraryCall = 127, + /// Used for Hexagon functions and call sites selected by Clang's + /// __attribute__((hlxcc)). If HLX is enabled, L12-L15 are callee-saved + /// even when the signature has no HLX vector types. + Hexagon_HLXCall = 128, + + /// Used for Hexagon functions and call sites selected by Clang's + /// __attribute__((preserve_hlx)). If HLX is enabled, all HLX vector + /// registers (L0-L15) are callee-saved. + Hexagon_PreserveHLX = 129, + /// The highest possible ID. Must be some 2^k - 1. MaxID = 1023 }; diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index 6678269c8793ca..7bc69d2bfddff3 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -706,6 +706,8 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(cheriot_compartmentcallcc); KEYWORD(cheriot_compartmentcalleecc); KEYWORD(cheriot_librarycallcc); + KEYWORD(hexagon_hlxcc); + KEYWORD(hexagon_preserve_hlx); KEYWORD(cc); KEYWORD(c); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index 3a21ad056083f0..2e9347ee3d3d36 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -2511,6 +2511,12 @@ bool LLParser::parseOptionalCallingConv(unsigned &CC) { case lltok::kw_cheriot_librarycallcc: CC = CallingConv::CHERIoT_LibraryCall; break; + case lltok::kw_hexagon_hlxcc: + CC = CallingConv::Hexagon_HLXCall; + break; + case lltok::kw_hexagon_preserve_hlx: + CC = CallingConv::Hexagon_PreserveHLX; + break; case lltok::kw_cc: { Lex.Lex(); return parseUInt32(CC); diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index d3f1bbfc4c3c1e..c801e5cfc49e81 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -430,6 +430,12 @@ static void printCallingConv(unsigned cc, raw_ostream &Out) { case CallingConv::CHERIoT_LibraryCall: Out << "cheriot_librarycallcc"; break; + case CallingConv::Hexagon_HLXCall: + Out << "hexagon_hlxcc"; + break; + case CallingConv::Hexagon_PreserveHLX: + Out << "hexagon_preserve_hlx"; + break; } } diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index c11e5f2ae12209..cac054308e7572 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -1167,6 +1167,8 @@ bool llvm::CallingConv::supportsNonVoidReturnType(CallingConv::ID CC) { case CallingConv::CFGuard_Check: case CallingConv::SwiftTail: case CallingConv::PreserveNone: + case CallingConv::Hexagon_HLXCall: + case CallingConv::Hexagon_PreserveHLX: case CallingConv::X86_StdCall: case CallingConv::X86_FastCall: case CallingConv::ARM_APCS: diff --git a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp index dc2f2a2dfc298a..60b861c6cafb73 100644 --- a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp +++ b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.cpp @@ -157,14 +157,25 @@ HexagonRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { }; bool HasEHReturn = MF->getInfo<HexagonMachineFunctionInfo>()->hasEHReturn(); + bool UseHLX = MF->getSubtarget<HexagonSubtarget>().useHLXOps(); + CallingConv::ID CC = MF->getFunction().getCallingConv(); - if (hasHLXSignature(*MF)) + if (UseHLX && CC == CallingConv::Hexagon_PreserveHLX) + return HasEHReturn ? HexagonCSR_PreserveHLX_EH_SaveList + : HexagonCSR_PreserveHLX_SaveList; + if (hasHLXSignature(*MF) || (UseHLX && CC == CallingConv::Hexagon_HLXCall)) return HasEHReturn ? HexagonCSR_HLX_EH_SaveList : HexagonCSR_HLX_SaveList; return HasEHReturn ? CalleeSavedRegsV3EHReturn : CalleeSavedRegsV3; } -const uint32_t *HexagonRegisterInfo::getCallPreservedMask( - const MachineFunction &MF, CallingConv::ID) const { +const uint32_t * +HexagonRegisterInfo::getCallPreservedMask(const MachineFunction &MF, + CallingConv::ID CC) const { + if (CC == CallingConv::Hexagon_PreserveHLX) + return HexagonCSR_PreserveHLX_RegMask; + if (CC == CallingConv::Hexagon_HLXCall && + MF.getSubtarget<HexagonSubtarget>().useHLXOps()) + return HexagonCSR_HLX_RegMask; return HexagonCSR_RegMask; } diff --git a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td index 52a176eb285b5d..d9da6f71ab9814 100644 --- a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td +++ b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td @@ -707,3 +707,10 @@ def HexagonCSR_HLX : CalleeSavedRegs<(add HexagonCSR, L12, L13, L14, L15)>; // Functions that contain a call to __builtin_eh_return also save R0-R3. def HexagonCSR_HLX_EH : CalleeSavedRegs<(add R0, R1, R2, R3, HexagonCSR_HLX)>; + +// Functions using the hexagon_preserve_hlx calling convention preserve all HLX +// vector registers. +def HexagonCSR_PreserveHLX + : CalleeSavedRegs<(add HexagonCSR, (sequence "L%u", 0, 15))>; +def HexagonCSR_PreserveHLX_EH + : CalleeSavedRegs<(add R0, R1, R2, R3, HexagonCSR_PreserveHLX)>; diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll index 61e7b25787bbd9..c91c3771024c57 100644 --- a/llvm/test/Bitcode/compatibility.ll +++ b/llvm/test/Bitcode/compatibility.ll @@ -577,6 +577,14 @@ declare cc124 void @f.cc124(i1) ; CHECK: declare amdgpu_gfx_whole_wave void @f.cc124(i1) declare amdgpu_gfx_whole_wave void @f.amdgpu_gfx_whole_wave(i1) ; CHECK: declare amdgpu_gfx_whole_wave void @f.amdgpu_gfx_whole_wave(i1) +declare cc128 void @f.cc128() +; CHECK: declare hexagon_hlxcc void @f.cc128() +declare hexagon_hlxcc void @f.hexagon_hlxcc() +; CHECK: declare hexagon_hlxcc void @f.hexagon_hlxcc() +declare cc129 void @f.cc129() +; CHECK: declare hexagon_preserve_hlx void @f.cc129() +declare hexagon_preserve_hlx void @f.hexagon_preserve_hlx() +; CHECK: declare hexagon_preserve_hlx void @f.hexagon_preserve_hlx() declare cc1023 void @f.cc1023() ; CHECK: declare cc1023 void @f.cc1023() diff --git a/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll b/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll new file mode 100644 index 00000000000000..fe9ef6d60c1088 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll @@ -0,0 +1,32 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -stop-after=finalize-isel < %s | FileCheck %s --check-prefixes=CHECK,HLX +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -stop-after=finalize-isel < %s | FileCheck %s --check-prefixes=CHECK,NOHLX + +; Calls without HLX arguments or return values use the call-preserved mask of +; the callee's calling convention. With HLX disabled, hexagon_hlxcc preserves +; the same registers as the C calling convention. + +; CHECK-LABEL: name: call_c +; CHECK: J2_call @c_callee, hexagoncsr, +define void @call_c() { + call void @c_callee() + ret void +} + +; CHECK-LABEL: name: call_hlxcc +; HLX: J2_call @hlxcc_callee, hexagoncsr_hlx, +; NOHLX: J2_call @hlxcc_callee, hexagoncsr, +define void @call_hlxcc() { + call hexagon_hlxcc void @hlxcc_callee() + ret void +} + +; CHECK-LABEL: name: call_preserve_hlx +; CHECK: J2_call @preserve_hlx_callee, hexagoncsr_preservehlx, +define void @call_preserve_hlx() { + call hexagon_preserve_hlx void @preserve_hlx_callee() + ret void +} + +declare void @c_callee() +declare hexagon_hlxcc void @hlxcc_callee() +declare hexagon_preserve_hlx void @preserve_hlx_callee() diff --git a/llvm/test/CodeGen/Hexagon/hlxcc-callconv.ll b/llvm/test/CodeGen/Hexagon/hlxcc-callconv.ll new file mode 100644 index 00000000000000..8cd01e3f2add10 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/hlxcc-callconv.ll @@ -0,0 +1,42 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -stop-after=prolog-epilog < %s | FileCheck %s + +; Check that when HLX caller calls a scalar callee with hlxcc attribute, +; caller uses L12 register for a live value across the call. +; CHECK-LABEL: name: foo0 +; CHECK: $l12 = LX_L16b_pseudo +; CHECK-NOT: PS_storerl +; CHECK: J2_call @bar0 +define inreg <1024 x i16> @foo0(ptr readonly %ptr) { +entry: + %0 = load <1024 x i16>, ptr %ptr, align 2048 + %call = tail call hexagon_hlxcc ptr @bar0(ptr nonnull %ptr) + %1 = load <1024 x i16>, ptr %call, align 2048 + %add = add <1024 x i16> %1, %0 + ret <1024 x i16> %add +} + +; Check that HLX spills are inserted when scalar caller with hlxcc attribute +; calls a scalar callee +; CHECK-LABEL: name: foo1 +; CHECK: PS_storerl {{.*}}$l12 +; CHECK: PS_storerl {{.*}}$l13 +; CHECK: PS_storerl {{.*}}$l14 +; CHECK: PS_storerl {{.*}}$l15 +; CHECK: J2_call @bar1, hexagoncsr, +; CHECK: $l12 = PS_loadrl +; CHECK: $l13 = PS_loadrl +; CHECK: $l14 = PS_loadrl +; CHECK: $l15 = PS_loadrl +define hexagon_hlxcc void @foo1(ptr readonly %ptr, ptr writeonly %dst) { +entry: + %0 = load <1024 x i16>, ptr %ptr, align 2048 + %call = tail call ptr @bar1(ptr nonnull %ptr) + %1 = load <1024 x i16>, ptr %call, align 2048 + %add = add <1024 x i16> %1, %0 + store <1024 x i16> %add, ptr %dst, align 2048 + ret void +} + +declare hexagon_hlxcc ptr @bar0(ptr) + +declare ptr @bar1(ptr) diff --git a/llvm/test/CodeGen/Hexagon/preserve-hlx-callconv.ll b/llvm/test/CodeGen/Hexagon/preserve-hlx-callconv.ll new file mode 100644 index 00000000000000..e2de785691fcd4 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/preserve-hlx-callconv.ll @@ -0,0 +1,40 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -stop-after=prolog-epilog < %s | FileCheck %s + +; Check that HLX spills are not inserted when a HLX caller calls a scalar callee +; with preserve_hlx attribute. +; CHECK-LABEL: name: foo0 +; CHECK-NOT: PS_storerll +; CHECK-NOT: PS_storerl +define inreg <1024 x i16> @foo0(ptr readonly %ptr) { +entry: + %0 = load <1024 x i16>, ptr %ptr, align 2048 + %call = tail call hexagon_preserve_hlx ptr @bar0(ptr nonnull %ptr) + %1 = load <1024 x i16>, ptr %call, align 2048 + %add = add <1024 x i16> %1, %0 + ret <1024 x i16> %add +} + +; Check that HLX spills are inserted when scalar callee does not have preserve_hlx +; attribute. +; CHECK-LABEL: name: foo1 +; CHECK: PS_storerl {{.*}}$l12 +; CHECK: PS_storerl {{.*}}$l13 +; CHECK: PS_storerl {{.*}}$l14 +; CHECK: PS_storerl {{.*}}$l15 +; CHECK: J2_call @bar1, hexagoncsr, +; CHECK: $l12 = PS_loadrl +; CHECK: $l13 = PS_loadrl +; CHECK: $l14 = PS_loadrl +; CHECK: $l15 = PS_loadrl +define inreg <1024 x i16> @foo1(ptr readonly %ptr) { +entry: + %0 = load <1024 x i16>, ptr %ptr, align 2048 + %call = tail call ptr @bar1(ptr nonnull %ptr) + %1 = load <1024 x i16>, ptr %call, align 2048 + %add = add <1024 x i16> %1, %0 + ret <1024 x i16> %add +} + +declare hexagon_preserve_hlx ptr @bar0(ptr) + +declare ptr @bar1(ptr) diff --git a/llvm/test/CodeGen/Hexagon/preserve-hlx-spill.ll b/llvm/test/CodeGen/Hexagon/preserve-hlx-spill.ll new file mode 100644 index 00000000000000..7c2c01ba82582c --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/preserve-hlx-spill.ll @@ -0,0 +1,86 @@ +; Check that a preserve_hlx function saves and restores every HLX register it +; clobbers, including the argument registers L0-L11. + +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -stop-after=prolog-epilog \ +; RUN: < %s | FileCheck %s + +; CHECK-LABEL: name: hlx_use_all_L16_regs +; CHECK: frame-setup V2_vallocframe_imm 16, +; CHECK-DAG: PS_storerl {{.*}}, killed $l0 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l1 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l2 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l3 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l4 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l5 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l6 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l7 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l8 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l9 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l10 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l11 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l12 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l13 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l14 :: +; CHECK-DAG: PS_storerl {{.*}}, killed $l15 :: +; CHECK: LX_S16b_pseudo +; CHECK-DAG: $l0 = PS_loadrl +; CHECK-DAG: $l1 = PS_loadrl +; CHECK-DAG: $l2 = PS_loadrl +; CHECK-DAG: $l3 = PS_loadrl +; CHECK-DAG: $l4 = PS_loadrl +; CHECK-DAG: $l5 = PS_loadrl +; CHECK-DAG: $l6 = PS_loadrl +; CHECK-DAG: $l7 = PS_loadrl +; CHECK-DAG: $l8 = PS_loadrl +; CHECK-DAG: $l9 = PS_loadrl +; CHECK-DAG: $l10 = PS_loadrl +; CHECK-DAG: $l11 = PS_loadrl +; CHECK-DAG: $l12 = PS_loadrl +; CHECK-DAG: $l13 = PS_loadrl +; CHECK-DAG: $l14 = PS_loadrl +; CHECK-DAG: $l15 = PS_loadrl +; CHECK: V2_vdeallocframe_imm 16, + +define hexagon_preserve_hlx void @hlx_use_all_L16_regs(ptr writeonly %dst) #0 { +entry: + %0 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 1) + %1 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 2) + %2 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 3) + %3 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 4) + %4 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 5) + %5 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 6) + %6 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 7) + %7 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 8) + %8 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 9) + %9 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 10) + %10 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 11) + %11 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 12) + %12 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 13) + %13 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 14) + %14 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 15) + %15 = tail call <1024 x i16> @llvm.hexagon.LX.assign.u(i32 16) + %16 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %0, <1024 x i16> %1) + %17 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %1, <1024 x i16> %16) + %18 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %2, <1024 x i16> %17) + %19 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %3, <1024 x i16> %18) + %20 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %4, <1024 x i16> %19) + %21 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %5, <1024 x i16> %20) + %22 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %6, <1024 x i16> %21) + %23 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %7, <1024 x i16> %22) + %24 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %8, <1024 x i16> %23) + %25 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %9, <1024 x i16> %24) + %26 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %10, <1024 x i16> %25) + %27 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %11, <1024 x i16> %26) + %28 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %12, <1024 x i16> %27) + %29 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %13, <1024 x i16> %28) + %30 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %14, <1024 x i16> %29) + %31 = tail call <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16> %15, <1024 x i16> %30) + store <1024 x i16> %31, ptr %dst, align 2048 + ret void +} + +declare <1024 x i16> @llvm.hexagon.LX.assign.u(i32 immarg) + +declare <1024 x i16> @llvm.hexagon.LX.add.h(<1024 x i16>, <1024 x i16>) + +attributes #0 = { optnone noinline } >From 6f5dad3b233f4871ebcde21f79643139cfb96b18 Mon Sep 17 00:00:00 2001 From: Yashas Andaluri <[email protected]> Date: Thu, 19 Nov 2026 15:42:42 +0530 Subject: [PATCH 2/3] [Hexagon] Allow HLX types on preserve_hlx and fix related call lowering - Allow HLX arguments and return values on hexagon_preserve_hlx (drop the Sema error). HLX argument registers stay callee-saved; for a call that returns an HLX value, LowerCallResult clears the return registers from a per-call copy of the register mask, and LowerReturn does not save them in the callee. - A preserve_hlx callee that returns an HLX value is only tail called by a caller that returns the same type. Tail calls keep the static mask. - Do not mark a callee-saved register killed when it is spilled and also live into the function (e.g. an HLX argument register). - Update the attribute docs, and add tests for HLX signatures, function pointers, tail calls, live-in spills and the DWARF calling convention. Assisted by AI --- clang/include/clang/Basic/AttrDocs.td | 5 +- .../clang/Basic/DiagnosticSemaKinds.td | 2 - clang/include/clang/Sema/SemaHexagon.h | 4 - clang/lib/Sema/SemaHexagon.cpp | 8 - clang/lib/Sema/SemaType.cpp | 9 - clang/test/CodeGen/hexagon-hlxcc.c | 7 + clang/test/CodeGen/hexagon-preserve-hlx.c | 7 + clang/test/Sema/hexagon-preserve-hlx.c | 18 +- .../Target/Hexagon/HexagonFrameLowering.cpp | 7 +- .../Target/Hexagon/HexagonISelLowering.cpp | 77 ++++++-- llvm/lib/Target/Hexagon/HexagonISelLowering.h | 2 +- llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll | 16 ++ .../Hexagon/preserve-hlx-hlx-signature.ll | 184 ++++++++++++++++++ .../Hexagon/hlx-calling-convention.ll | 38 ++++ 14 files changed, 329 insertions(+), 55 deletions(-) create mode 100644 llvm/test/CodeGen/Hexagon/preserve-hlx-hlx-signature.ll create mode 100644 llvm/test/DebugInfo/Hexagon/hlx-calling-convention.ll diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index eadbbbb20cd2e8..65926daabb4401 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7004,9 +7004,8 @@ function preserve all HLX vector registers (``L0``-``L15``), so callers do not need to save any HLX values around calls to it. It maps to the LLVM ``hexagon_preserve_hlx`` calling convention. -Because HLX vector arguments and return values are passed in HLX registers, it -is an error to use ``preserve_hlx`` on a function whose return type or any -parameter type is an HLX vector type. +HLX vector arguments are passed as with the default calling convention and are +also preserved. Registers holding an HLX vector return value are not preserved. .. code-block:: c diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 05ed6a63061e08..5bf172b8f0b573 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3455,8 +3455,6 @@ def err_hlx_invalid_field : Error< "aggregate objects with HLX types are not allowed">; def err_hlx_invalid_object : Error< "global, static or variable sized objects with HLX types are not allowed">; -def err_hlx_invalid_attribute : Error< - "%0 attribute cannot be used on functions with HLX types in their signature">; def err_sve_vector_in_non_sve_target : Error< "SVE vector type %0 cannot be used in a target without sve">; def err_sve_vector_in_non_streaming_function : Error< diff --git a/clang/include/clang/Sema/SemaHexagon.h b/clang/include/clang/Sema/SemaHexagon.h index 4459f1b34d1588..dc3976b469ca8a 100644 --- a/clang/include/clang/Sema/SemaHexagon.h +++ b/clang/include/clang/Sema/SemaHexagon.h @@ -31,10 +31,6 @@ class SemaHexagon : public SemaBase { /// Returns true if \p T is an HLX vector or vector pair type, or an array /// of them, and HLX is enabled. bool isHLXVectorType(QualType T) const; - - /// Returns true if the return type or a parameter type of \p FT is an HLX - /// vector type. - bool hasHLXVectorInSignature(const FunctionType *FT) const; }; } // namespace clang diff --git a/clang/lib/Sema/SemaHexagon.cpp b/clang/lib/Sema/SemaHexagon.cpp index e3bc1c3ba6e781..f18c6e7719b83c 100644 --- a/clang/lib/Sema/SemaHexagon.cpp +++ b/clang/lib/Sema/SemaHexagon.cpp @@ -314,12 +314,4 @@ bool SemaHexagon::isHLXVectorType(QualType T) const { return Size == 2048 || Size == 4096; } -bool SemaHexagon::hasHLXVectorInSignature(const FunctionType *FT) const { - if (isHLXVectorType(FT->getReturnType())) - return true; - const auto *FPT = dyn_cast<FunctionProtoType>(FT); - return FPT && llvm::any_of(FPT->getParamTypes(), - [&](QualType T) { return isHLXVectorType(T); }); -} - } // namespace clang diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 1ca4e0766d79a6..3b30a9881e0c23 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -38,7 +38,6 @@ #include "clang/Sema/ScopeInfo.h" #include "clang/Sema/SemaCUDA.h" #include "clang/Sema/SemaHLSL.h" -#include "clang/Sema/SemaHexagon.h" #include "clang/Sema/SemaObjC.h" #include "clang/Sema/SemaOpenMP.h" #include "clang/Sema/Template.h" @@ -8458,14 +8457,6 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr, const FunctionType *fn = unwrapped.get(); CallingConv CCOld = fn->getCallConv(); - - // preserve_hlx functions cannot pass or return HLX vectors, as HLX vectors - // are passed in the registers they preserve. - if (CC == CC_HexagonPreserveHLX && S.Hexagon().hasHLXVectorInSignature(fn)) { - S.Diag(attr.getLoc(), diag::err_hlx_invalid_attribute) << attr; - attr.setInvalid(); - return true; - } Attr *CCAttr = getCCTypeAttr(S.Context, attr); if (CCOld != CC) { diff --git a/clang/test/CodeGen/hexagon-hlxcc.c b/clang/test/CodeGen/hexagon-hlxcc.c index 5c0b773e945336..0f0bd7673b60c4 100644 --- a/clang/test/CodeGen/hexagon-hlxcc.c +++ b/clang/test/CodeGen/hexagon-hlxcc.c @@ -17,3 +17,10 @@ void g(int *a) { f(a); } // C: define dso_local hexagon_hlxcc void @h( // CXX: define dso_local hexagon_hlxcc void @_Z1hPi( void __attribute__((hlxcc)) h(int *a) { f(a); } + +// Calls through a function pointer use the calling convention of its type. +// C-LABEL: define dso_local void @call_fp( +// C: call hexagon_hlxcc void %{{.*}}( +// CXX-LABEL: define dso_local void @_Z7call_fp +// CXX: call hexagon_hlxcc void %{{.*}}( +void call_fp(void (*fp)(int *) __attribute__((hlxcc)), int *a) { fp(a); } diff --git a/clang/test/CodeGen/hexagon-preserve-hlx.c b/clang/test/CodeGen/hexagon-preserve-hlx.c index a725b8ecc6c456..ce9554547837ec 100644 --- a/clang/test/CodeGen/hexagon-preserve-hlx.c +++ b/clang/test/CodeGen/hexagon-preserve-hlx.c @@ -17,3 +17,10 @@ void g(int *a) { f(a); } // C: define dso_local hexagon_preserve_hlx void @h( // CXX: define dso_local hexagon_preserve_hlx void @_Z1hPi( void __attribute__((preserve_hlx)) h(int *a) { f(a); } + +// Calls through a function pointer use the calling convention of its type. +// C-LABEL: define dso_local void @call_fp( +// C: call hexagon_preserve_hlx void %{{.*}}( +// CXX-LABEL: define dso_local void @_Z7call_fp +// CXX: call hexagon_preserve_hlx void %{{.*}}( +void call_fp(void (*fp)(int *) __attribute__((preserve_hlx)), int *a) { fp(a); } diff --git a/clang/test/Sema/hexagon-preserve-hlx.c b/clang/test/Sema/hexagon-preserve-hlx.c index fc56faadfbefa4..1d30c3c3c51242 100644 --- a/clang/test/Sema/hexagon-preserve-hlx.c +++ b/clang/test/Sema/hexagon-preserve-hlx.c @@ -22,18 +22,12 @@ int bad = foo4; // expected-error {{incompatible pointer to integer conversion i void __attribute__((preserve_hlx)) ok(int *, HLX_Vector *); -HLX_Vector __attribute__((preserve_hlx)) bar0(void); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} -HLX_Vector __attribute__((preserve_hlx)) bar0(void) {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} - -HLX_Vector __attribute__((preserve_hlx)) bar1(); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} -HLX_Vector __attribute__((preserve_hlx)) bar1() {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} - -void __attribute__((preserve_hlx)) bar2(HLX_Vector a); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} -void __attribute__((preserve_hlx)) bar2(HLX_Vector a) {} // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} - -void __attribute__((preserve_hlx)) bar3(int, HLX_VectorPair); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} - -void (*__attribute__((preserve_hlx)) bar4)(HLX_Vector); // expected-error {{'preserve_hlx' attribute cannot be used on functions with HLX types in their signature}} +// HLX types are allowed in the signature. +HLX_Vector __attribute__((preserve_hlx)) bar0(void); +HLX_Vector __attribute__((preserve_hlx)) bar1(); +void __attribute__((preserve_hlx)) bar2(HLX_Vector a); +void __attribute__((preserve_hlx)) bar3(int, HLX_VectorPair); +void (*__attribute__((preserve_hlx)) bar4)(HLX_Vector); // Function pointer and typedef uses are fine. void (*fp)(int) __attribute__((preserve_hlx)); diff --git a/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp b/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp index 55189e2fd5a60f..74601ad9e8b720 100644 --- a/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp +++ b/llvm/lib/Target/Hexagon/HexagonFrameLowering.cpp @@ -1825,7 +1825,12 @@ MachineBasicBlock::iterator HexagonFrameLowering::insertCSRSpillsInBlock( // Add live in registers. We treat eh_return callee saved register r0 - r3 // specially. They are not really callee saved registers as they are not // supposed to be killed. - bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg); + // A register that is live into the function (e.g. an HLX argument of a + // preserve_hlx function) is still read after the spill. + bool IsLiveIn = any_of(MF.getRegInfo().liveins(), [&](const auto &LI) { + return HRI.regsOverlap(LI.first, Reg); + }); + bool IsKill = !HRI.isEHReturnCalleeSaveReg(Reg) && !IsLiveIn; int FI = I.getFrameIdx(); const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(Reg); HII.storeRegToStackSlot(MBB, MI, Reg, IsKill, FI, RC, Register()); diff --git a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp index 297497605b01fe..694182a4e1f1b3 100644 --- a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp +++ b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp @@ -139,6 +139,11 @@ static unsigned assignVTCMStackOffsets(SmallVectorImpl<CCValAssign> &ArgLocs) { #define GET_CALLING_CONV_IMPL #include "HexagonGenCallingConv.inc" +static bool isHLXRegLoc(const CCValAssign &VA) { + return VA.isRegLoc() && (Hexagon::HlxLRRegClass.contains(VA.getLocReg()) || + Hexagon::HlxLLRRegClass.contains(VA.getLocReg())); +} + unsigned HexagonTargetLowering::getVectorTypeBreakdownForCallingConv( LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const { @@ -311,6 +316,13 @@ HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); } + // preserve_hlx functions do not preserve their HLX return registers. + if (CallConv == CallingConv::Hexagon_PreserveHLX) + for (const CCValAssign &VA : RVLocs) + if (isHLXRegLoc(VA)) + DAG.getMachineFunction().getRegInfo().disableCalleeSavedRegister( + VA.getLocReg()); + RetOps[0] = Chain; // Update chain. // Add the glue if we have it. @@ -407,7 +419,10 @@ SDValue HexagonTargetLowering::LowerCallResult( SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg, const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, - const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const { + const SmallVectorImpl<SDValue> &OutVals, SDValue Callee, + uint32_t *RegMask) const { + const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo(); + // Assign locations to each value returned by this call. SmallVector<CCValAssign, 16> RVLocs; @@ -418,6 +433,13 @@ SDValue HexagonTargetLowering::LowerCallResult( // Copy all of the result registers out of their specified physreg. for (unsigned i = 0; i != RVLocs.size(); ++i) { + // A returned HLX register is not preserved by the callee. Clear its + // super-registers as well: a pair such as LL0 is only preserved if both + // L0 and L1 are. + if (RegMask && isHLXRegLoc(RVLocs[i])) + for (MCPhysReg R : HRI.sub_and_superregs_inclusive(RVLocs[i].getLocReg())) + RegMask[R / 32] &= ~(1u << (R % 32)); + SDValue RetVal; if (RVLocs[i].getValVT() == MVT::i1) { // Return values of type MVT::i1 require special handling. The reason @@ -653,20 +675,32 @@ HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, for (const auto &R : RegsToPass) Ops.push_back(DAG.getRegister(R.first, R.second.getValueType())); - // Callees that take or return HLX values preserve L12-L15. - bool HasHLXLocs = - any_of(ArgLocs, - [](const CCValAssign &VA) { - return VA.isRegLoc() && - (Hexagon::HlxLRRegClass.contains(VA.getLocReg()) || - Hexagon::HlxLLRRegClass.contains(VA.getLocReg())); - }) || + // Callees that take or return HLX values preserve L12-L15, unless they use + // preserve_hlx. + bool IsPreserveHLX = CallConv == CallingConv::Hexagon_PreserveHLX; + bool HasHLXLocs = any_of(ArgLocs, isHLXRegLoc) || + any_of(Ins, [this](const ISD::InputArg &In) { + return Subtarget.isHLXVectorType(In.VT); + }); + const uint32_t *Mask = HasHLXLocs && !IsPreserveHLX + ? HRI.getHLXCallPreservedMask() + : HRI.getCallPreservedMask(MF, CallConv); + assert(Mask && "Missing call preserved mask for calling convention"); + + // preserve_hlx calls clobber their HLX return registers: LowerCallResult + // clears them from a copy of the mask. A tail call keeps the static mask, as + // on X86: it returns to our caller, so nothing is live across it. + uint32_t *RetMask = nullptr; + if (IsPreserveHLX && !CLI.IsTailCall && any_of(Ins, [this](const ISD::InputArg &In) { return Subtarget.isHLXVectorType(In.VT); - }); - const uint32_t *Mask = HasHLXLocs ? HRI.getHLXCallPreservedMask() - : HRI.getCallPreservedMask(MF, CallConv); - assert(Mask && "Missing call preserved mask for calling convention"); + })) { + RetMask = MF.allocateRegMask(); + memcpy(RetMask, Mask, + sizeof(RetMask[0]) * + MachineOperand::getRegMaskSize(HRI.getNumRegs())); + Mask = RetMask; + } Ops.push_back(DAG.getRegisterMask(Mask)); if (Glue.getNode()) @@ -694,8 +728,8 @@ HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, // Handle result values, copying them out of physregs into vregs that we // return. - return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG, - InVals, OutVals, Callee); + return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG, InVals, + OutVals, Callee, RetMask); } /// Returns true by value, base pointer and offset pointer and addressing @@ -3913,6 +3947,19 @@ bool HexagonTargetLowering::IsEligibleForTailCallOptimization( return false; } + // A preserve_hlx callee clobbers its HLX return registers. Its caller (also + // preserve_hlx, as the conventions match) preserves them unless it returns + // the same type, e.g. 'void f() { return_hlx(); }' must not tail call. + if (CalleeCC == CallingConv::Hexagon_PreserveHLX && + any_of(Ins, [this](const ISD::InputArg &In) { + return Subtarget.isHLXVectorType(In.VT); + })) { + EVT CallerRetVT = + getValueType(DAG.getDataLayout(), CallerF.getReturnType()); + if (Ins.size() != 1 || EVT(Ins[0].VT) != CallerRetVT) + return false; + } + // Do not tail call optimize vararg calls. if (IsVarArg) return false; diff --git a/llvm/lib/Target/Hexagon/HexagonISelLowering.h b/llvm/lib/Target/Hexagon/HexagonISelLowering.h index b6f3adc3c44708..2a0714db56acbc 100644 --- a/llvm/lib/Target/Hexagon/HexagonISelLowering.h +++ b/llvm/lib/Target/Hexagon/HexagonISelLowering.h @@ -146,7 +146,7 @@ class HexagonTargetLowering : public TargetLowering { const SDLoc &dl, SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, const SmallVectorImpl<SDValue> &OutVals, - SDValue Callee) const; + SDValue Callee, uint32_t *RegMask) const; SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const; SDValue LowerVSELECT(SDValue Op, SelectionDAG &DAG) const; diff --git a/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll b/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll index fe9ef6d60c1088..f16ea0d1c23ab2 100644 --- a/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll +++ b/llvm/test/CodeGen/Hexagon/hlx-cc-call-mask.ll @@ -27,6 +27,22 @@ define void @call_preserve_hlx() { ret void } +; Indirect calls use the mask of the call's calling convention. +; CHECK-LABEL: name: call_fp_hlxcc +; HLX: J2_callr {{.*}}, hexagoncsr_hlx, +; NOHLX: J2_callr {{.*}}, hexagoncsr, +define void @call_fp_hlxcc(ptr %fp) { + call hexagon_hlxcc void %fp() + ret void +} + +; CHECK-LABEL: name: call_fp_preserve_hlx +; CHECK: J2_callr {{.*}}, hexagoncsr_preservehlx, +define void @call_fp_preserve_hlx(ptr %fp) { + call hexagon_preserve_hlx void %fp() + ret void +} + declare void @c_callee() declare hexagon_hlxcc void @hlxcc_callee() declare hexagon_preserve_hlx void @preserve_hlx_callee() diff --git a/llvm/test/CodeGen/Hexagon/preserve-hlx-hlx-signature.ll b/llvm/test/CodeGen/Hexagon/preserve-hlx-hlx-signature.ll new file mode 100644 index 00000000000000..a874750ecad790 --- /dev/null +++ b/llvm/test/CodeGen/Hexagon/preserve-hlx-hlx-signature.ll @@ -0,0 +1,184 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -stop-after=finalize-isel < %s | FileCheck %s --check-prefix=ISEL +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -verify-machineinstrs -stop-after=prolog-epilog < %s | FileCheck %s --check-prefix=PEI + +; preserve_hlx functions may have HLX types in their signature. HLX arguments +; stay callee-saved; the HLX return registers are not. + +;; Caller side: call-preserved masks. + +; HLX arguments without an HLX return value keep the full preserve_hlx mask. +; ISEL-LABEL: name: call_args_void +; ISEL: J2_call @callee_args_void, hexagoncsr_preservehlx, +define void @call_args_void(<1024 x i16> %a, <1024 x i16> %b) { + call hexagon_preserve_hlx void @callee_args_void(<1024 x i16> %a, <1024 x i16> %b) + ret void +} + +; ISEL-LABEL: name: call_args_scalar_ret +; ISEL: J2_call @callee_args_scalar_ret, hexagoncsr_preservehlx, +define i32 @call_args_scalar_ret(<1024 x i16> %a) { + %r = call hexagon_preserve_hlx i32 @callee_args_scalar_ret(<1024 x i16> %a) + ret i32 %r +} + +; An HLX return value in L0 removes L0 and its super-register LL0. +; ISEL-LABEL: name: call_ret +; ISEL: J2_call @callee_ret, CustomRegMask($d8,$d9,$d10,$d11,$d12,$d13,$l1,{{.*}},$l15,$ll1, +define <1024 x i16> @call_ret(<1024 x i16> %a, <1024 x i16> %b) { + %r = call hexagon_preserve_hlx <1024 x i16> @callee_ret(<1024 x i16> %a) + %s = add <1024 x i16> %r, %b + ret <1024 x i16> %s +} + +; An HLX pair return value in LL0 removes LL0 and its sub-registers L0 and L1. +; ISEL-LABEL: name: call_ret_pair +; ISEL: J2_call @callee_ret_pair, CustomRegMask($d8,$d9,$d10,$d11,$d12,$d13,$l2,{{.*}},$l15,$ll1, +define <2048 x i16> @call_ret_pair(<2048 x i16> %a) { + %r = call hexagon_preserve_hlx <2048 x i16> @callee_ret_pair(<2048 x i16> %a) + ret <2048 x i16> %r +} + +; ISEL-LABEL: name: call_ret_indirect +; ISEL: J2_callr {{.*}}, CustomRegMask($d8,$d9,$d10,$d11,$d12,$d13,$l1,{{.*}},$l15,$ll1, +define <1024 x i16> @call_ret_indirect(ptr %fp, <1024 x i16> %a) { + %r = call hexagon_preserve_hlx <1024 x i16> %fp(<1024 x i16> %a) + ret <1024 x i16> %r +} + +; A discarded HLX return value is still clobbered. +; ISEL-LABEL: name: call_discard_ret +; ISEL: J2_call @callee_ret, CustomRegMask($d8,$d9,$d10,$d11,$d12,$d13,$l1,{{.*}},$l15,$ll1, +define void @call_discard_ret(<1024 x i16> %a) { + call hexagon_preserve_hlx <1024 x i16> @callee_ret(<1024 x i16> %a) + ret void +} + +;; Tail calls. Only a preserve_hlx caller can tail call a preserve_hlx callee. + +; The caller returns the same type, so it clobbers the same register. Nothing +; is live across the tail call, so it keeps the static mask (as on X86). +; ISEL-LABEL: name: tail_ret +; ISEL: PS_tailcall_i @callee_ret, hexagoncsr_preservehlx, +define hexagon_preserve_hlx <1024 x i16> @tail_ret(<1024 x i16> %a) { + %r = tail call hexagon_preserve_hlx <1024 x i16> @callee_ret(<1024 x i16> %a) + ret <1024 x i16> %r +} + +; ISEL-LABEL: name: tail_args_void +; ISEL: PS_tailcall_i @callee_args_void, hexagoncsr_preservehlx, +define hexagon_preserve_hlx void @tail_args_void(<1024 x i16> %a) { + tail call hexagon_preserve_hlx void @callee_args_void(<1024 x i16> %a) + ret void +} + +; The caller returns void, so it preserves L0, but the callee clobbers L0 as +; its return register. This must not be a tail call. +; ISEL-LABEL: name: tail_discard_ret +; ISEL-NOT: PS_tailcall_i +; ISEL: J2_call @callee_ret, CustomRegMask($d8,$d9,$d10,$d11,$d12,$d13,$l1,{{.*}},$l15,$ll1, +define hexagon_preserve_hlx void @tail_discard_ret(<1024 x i16> %a) { + %r = tail call hexagon_preserve_hlx <1024 x i16> @callee_ret(<1024 x i16> %a) + ret void +} + +; The incoming argument L0 is saved but still read by the call, so the spill +; must not kill it. +; PEI-LABEL: name: tail_discard_ret +; PEI: PS_storerl {{.*}}$l0 :: +; PEI: J2_call @callee_ret +; PEI: $l0 = PS_loadrl + +; The incoming pair LL0 is saved as L0 and L1 but still read by the call. +; PEI-LABEL: name: live_arg_pair +; PEI: PS_storerl {{.*}}$l0 :: +; PEI: PS_storerl {{.*}}$l1 :: +; PEI: J2_call @callee_ret_pair +; PEI: $l0 = PS_loadrl +; PEI: $l1 = PS_loadrl +define hexagon_preserve_hlx void @live_arg_pair(<2048 x i16> %a) { + call hexagon_preserve_hlx <2048 x i16> @callee_ret_pair(<2048 x i16> %a) + ret void +} + +; The calling conventions differ, so this is not a tail call. +; ISEL-LABEL: name: tail_from_c +; ISEL-NOT: PS_tailcall_i +; ISEL: J2_call @callee_args_void, hexagoncsr_preservehlx, +define void @tail_from_c(<1024 x i16> %a) { + tail call hexagon_preserve_hlx void @callee_args_void(<1024 x i16> %a) + ret void +} + +; HLX argument registers are preserved, so the caller keeps %a and %b in +; registers across the call. +; PEI-LABEL: name: call_args_void_live +; PEI-NOT: PS_storer +; PEI: J2_call @callee_args_void +; PEI-NOT: PS_loadr +; PEI: LX_add_h +define <1024 x i16> @call_args_void_live(<1024 x i16> %a, <1024 x i16> %b) { + call hexagon_preserve_hlx void @callee_args_void(<1024 x i16> %a, <1024 x i16> %b) + %s = add <1024 x i16> %a, %b + ret <1024 x i16> %s +} + +;; Callee side: saved registers. + +; Clobbered HLX argument registers are saved and restored. +; PEI-LABEL: name: callee_args_void +; PEI: PS_storerl {{.*}}$l0 :: +; PEI: PS_storerl {{.*}}$l1 :: +; PEI: $l0 = PS_loadrl +; PEI: $l1 = PS_loadrl +define hexagon_preserve_hlx void @callee_args_void(<1024 x i16> %a, <1024 x i16> %b) { + call void asm sideeffect "", "~{l0},~{l1}"() + ret void +} + +; The return register L0 is not saved; the clobbered L1 is. +; PEI-LABEL: name: callee_ret +; PEI-NOT: {{\$(l0|ll0) ::|\$(l0|ll0) = PS_loadr}} +; PEI: PS_storerl {{.*}}$l1 :: +; PEI-NOT: {{\$(l0|ll0) ::|\$(l0|ll0) = PS_loadr}} +; PEI: $l1 = PS_loadrl +; PEI-NOT: {{\$(l0|ll0) ::|\$(l0|ll0) = PS_loadr}} +; PEI: L4_return +define hexagon_preserve_hlx <1024 x i16> @callee_ret(<1024 x i16> %a) { + call void asm sideeffect "", "~{l0},~{l1}"() + %r = add <1024 x i16> %a, %a + ret <1024 x i16> %r +} + +; The return register LL0 (L0, L1) is not saved; the clobbered L2 is. +; PEI-LABEL: name: callee_ret_pair +; PEI-NOT: {{\$(l0|l1|ll0) ::|\$(l0|l1|ll0) = PS_loadr}} +; PEI: PS_storerl {{.*}}$l2 :: +; PEI-NOT: {{\$(l0|l1|ll0) ::|\$(l0|l1|ll0) = PS_loadr}} +; PEI: $l2 = PS_loadrl +; PEI-NOT: {{\$(l0|l1|ll0) ::|\$(l0|l1|ll0) = PS_loadr}} +; PEI: L4_return +define hexagon_preserve_hlx <2048 x i16> @callee_ret_pair(<2048 x i16> %a) { + call void asm sideeffect "", "~{l0},~{l1},~{l2}"() + ret <2048 x i16> %a +} + +; __builtin_eh_return saves all callee-saved registers, but still not the +; return register L0. +; PEI-LABEL: name: callee_ret_eh +; PEI-NOT: {{\$(l0|ll0) ::|\$(l0|ll0) = PS_loadr}} +; PEI: EH_RETURN_JMPR +; PEI-NOT: {{\$(l0|ll0) ::|\$(l0|ll0) = PS_loadr}} +; PEI: L4_return +define hexagon_preserve_hlx <1024 x i16> @callee_ret_eh(<1024 x i16> %a, i1 %c, i32 %off, ptr %h) { + call void asm sideeffect "", "~{l0},~{l1}"() + br i1 %c, label %eh, label %ret +eh: + call void @llvm.eh.return.i32(i32 %off, ptr %h) + unreachable +ret: + %r = add <1024 x i16> %a, %a + ret <1024 x i16> %r +} + +declare hexagon_preserve_hlx i32 @callee_args_scalar_ret(<1024 x i16>) +declare void @llvm.eh.return.i32(i32, ptr) diff --git a/llvm/test/DebugInfo/Hexagon/hlx-calling-convention.ll b/llvm/test/DebugInfo/Hexagon/hlx-calling-convention.ll new file mode 100644 index 00000000000000..60250d03234c02 --- /dev/null +++ b/llvm/test/DebugInfo/Hexagon/hlx-calling-convention.ll @@ -0,0 +1,38 @@ +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx < %s | FileCheck %s --check-prefix=ASM +; RUN: llc -mtriple=hexagon -mcpu=hexagonv85 -mattr=+hlx -filetype=obj < %s \ +; RUN: | llvm-dwarfdump --debug-info - | FileCheck %s --check-prefix=DWARF + +; The DW_CC values of the Hexagon HLX calling conventions are emitted in +; objects, so they must not change: DW_CC_LLVM_HexagonHLXCall = 0xd1 and +; DW_CC_LLVM_HexagonPreserveHLX = 0xd2. + +; ASM: .byte 209 // DW_AT_calling_convention +; ASM: .byte 210 // DW_AT_calling_convention + +; DWARF: DW_AT_name ("f_hlxcc") +; DWARF: DW_AT_calling_convention (DW_CC_LLVM_HexagonHLXCall) +; DWARF: DW_AT_name ("f_preserve_hlx") +; DWARF: DW_AT_calling_convention (DW_CC_LLVM_HexagonPreserveHLX) + +define hexagon_hlxcc void @f_hlxcc() !dbg !6 { + ret void, !dbg !12 +} + +define hexagon_preserve_hlx void @f_preserve_hlx() !dbg !9 { + ret void, !dbg !13 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!2, !3} + +!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, emissionKind: FullDebug) +!1 = !DIFile(filename: "cc.c", directory: "/") +!2 = !{i32 7, !"Dwarf Version", i32 5} +!3 = !{i32 2, !"Debug Info Version", i32 3} +!6 = distinct !DISubprogram(name: "f_hlxcc", scope: !1, file: !1, line: 1, type: !7, spFlags: DISPFlagDefinition, unit: !0) +!7 = !DISubroutineType(cc: DW_CC_LLVM_HexagonHLXCall, types: !8) +!8 = !{null} +!9 = distinct !DISubprogram(name: "f_preserve_hlx", scope: !1, file: !1, line: 2, type: !10, spFlags: DISPFlagDefinition, unit: !0) +!10 = !DISubroutineType(cc: DW_CC_LLVM_HexagonPreserveHLX, types: !8) +!12 = !DILocation(line: 1, scope: !6) +!13 = !DILocation(line: 2, scope: !9) >From c80d2aae827ec599490939da527f4f89322649ef Mon Sep 17 00:00:00 2001 From: Yashas Andaluri <[email protected]> Date: Thu, 19 Nov 2026 19:36:48 +0530 Subject: [PATCH 3/3] [Hexagon] Document HLX registers and the return register exception for preserve_hlx Name the HLX argument and return registers in the hlxcc and preserve_hlx attribute docs, and note in the CallingConv.h and HexagonRegisterInfo.td comments that preserve_hlx does not preserve the registers holding an HLX return value. Comments and documentation only. Assisted by AI --- clang/include/clang/Basic/AttrDocs.td | 16 ++++++++++------ llvm/include/llvm/IR/CallingConv.h | 5 +++-- llvm/lib/Target/Hexagon/HexagonRegisterInfo.td | 3 ++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index 65926daabb4401..9afbb07e2a0668 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -7000,12 +7000,16 @@ def HexagonPreserveHLXDocs : Documentation { let Heading = "preserve_hlx"; let Content = [{ On Hexagon with HLX enabled, the ``preserve_hlx`` calling convention makes a -function preserve all HLX vector registers (``L0``-``L15``), so callers do not -need to save any HLX values around calls to it. It maps to the LLVM -``hexagon_preserve_hlx`` calling convention. - -HLX vector arguments are passed as with the default calling convention and are -also preserved. Registers holding an HLX vector return value are not preserved. +function preserve the HLX vector registers (``L0``-``L15``), so callers do not +need to save HLX values around calls to it. The exception is the registers that +hold an HLX vector return value. It maps to the LLVM ``hexagon_preserve_hlx`` +calling convention. + +A ``preserve_hlx`` function may have HLX vector types in its signature. They use +the same registers as in any function with HLX types: arguments are passed in +``L0``-``L11`` (vector pairs in ``LL0``-``LL5``) and a return value in ``L0`` (a +vector pair in ``LL0``). The callee preserves the argument registers, but not +the return value registers. .. code-block:: c diff --git a/llvm/include/llvm/IR/CallingConv.h b/llvm/include/llvm/IR/CallingConv.h index 71fb60a28255f2..ecde941f388de1 100644 --- a/llvm/include/llvm/IR/CallingConv.h +++ b/llvm/include/llvm/IR/CallingConv.h @@ -303,8 +303,9 @@ namespace CallingConv { Hexagon_HLXCall = 128, /// Used for Hexagon functions and call sites selected by Clang's - /// __attribute__((preserve_hlx)). If HLX is enabled, all HLX vector - /// registers (L0-L15) are callee-saved. + /// __attribute__((preserve_hlx)). If HLX is enabled, the HLX vector + /// registers (L0-L15) are callee-saved, except the registers that hold an + /// HLX vector return value. Hexagon_PreserveHLX = 129, /// The highest possible ID. Must be some 2^k - 1. diff --git a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td index d9da6f71ab9814..e5af463ae9894a 100644 --- a/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td +++ b/llvm/lib/Target/Hexagon/HexagonRegisterInfo.td @@ -709,7 +709,8 @@ def HexagonCSR_HLX : CalleeSavedRegs<(add HexagonCSR, L12, L13, L14, L15)>; def HexagonCSR_HLX_EH : CalleeSavedRegs<(add R0, R1, R2, R3, HexagonCSR_HLX)>; // Functions using the hexagon_preserve_hlx calling convention preserve all HLX -// vector registers. +// vector registers except the ones holding an HLX return value, which lowering +// removes from the function's callee-saved list. def HexagonCSR_PreserveHLX : CalleeSavedRegs<(add HexagonCSR, (sequence "L%u", 0, 15))>; def HexagonCSR_PreserveHLX_EH _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
