[llvm-branch-commits] [llvm] RuntimeLibcalls: Add signatures for the Darwin sincos stret functions (PR #239426)
Matt Arsenault via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 5 16:01:47 PST 2026
https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/239426
__sincos_stret and __sincosf_stret return a struct holding the 2
results. Describe the C struct return type in the signature table and
lower for the target ABI. This is a vector on x86-64 for float,
through memory on x86-32 and APCS ARM, and as a struct elsewhere.
This replaces the special case that built the types in C++. Ideally we
would be using the ABI library, but it's incomplete for most targets
and it requires some relayering to use here.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
>From 2cbc63c59044b6ba9b3a06b05bc4191724b21647 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 3 Nov 2026 18:20:28 -0800
Subject: [PATCH] RuntimeLibcalls: Add signatures for the Darwin sincos stret
functions
__sincos_stret and __sincosf_stret return a struct holding the 2
results. Describe the C struct return type in the signature table and
lower for the target ABI. This is a vector on x86-64 for float,
through memory on x86-32 and APCS ARM, and as a struct elsewhere.
This replaces the special case that built the types in C++. Ideally we
would be using the ABI library, but it's incomplete for most targets
and it requires some relayering to use here.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
llvm/include/llvm/IR/RuntimeLibcalls.h | 10 ++
llvm/include/llvm/IR/RuntimeLibcalls.td | 6 +-
llvm/include/llvm/IR/RuntimeLibcallsImpl.td | 3 +
llvm/lib/IR/RuntimeLibcalls.cpp | 102 +++++++++++---------
llvm/test/TableGen/RuntimeLibcallEmitter.td | 2 +
5 files changed, 75 insertions(+), 48 deletions(-)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.h b/llvm/include/llvm/IR/RuntimeLibcalls.h
index 112d645bb6c2c..f00b9dadee399 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.h
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.h
@@ -29,6 +29,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/TargetParser/Triple.h"
#include <optional>
+#include <string>
/// TableGen will produce 2 enums, RTLIB::Libcall and
/// RTLIB::LibcallImpl. RTLIB::Libcall describes abstract functionality the
@@ -83,6 +84,15 @@ struct RuntimeLibcallsInfo {
LongDoubleFormat LongDoubleFmt;
+ std::string ABIName;
+
+ /// Lower a returned C struct of two \p EltTy values for the target ABI.
+ /// \returns the IR return type, and the struct type if the struct is
+ /// returned through an sret argument (in which case the IR return type is
+ /// void), or nullptr if it is returned in registers.
+ std::pair<Type *, Type *> lowerCPairReturn(const Triple &TT,
+ Type *EltTy) const;
+
public:
friend class llvm::LibcallLoweringInfo;
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 0c921d86cd2b6..43dc5bbabe396 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -348,9 +348,8 @@ foreach FPTy = [F32, F64, F80, F128, PPCF128] in {
def CABS_#FPTy.Suffix : RuntimeLibcall<NoneType, []>;
}
-foreach FPTy = [ "F32", "F64" ] in {
- def SINCOS_STRET_#FPTy : RuntimeLibcall;
-}
+def SINCOS_STRET_F32 : RuntimeLibcall<CFltPair, [Flt]>;
+def SINCOS_STRET_F64 : RuntimeLibcall<CDblPair, [Dbl]>;
foreach FPTy = [F32, F64, F80, F128, PPCF128] in {
def POW_#FPTy.Suffix : RuntimeLibcall<FPTy.Type, [FPTy.Type, FPTy.Type]>;
@@ -2000,6 +1999,7 @@ def __exp10 : RuntimeLibcallImpl<EXP10_F64>;
def __exp10f : RuntimeLibcallImpl<EXP10_F32>;
def __sincos_stret : RuntimeLibcallImpl<SINCOS_STRET_F64>;
def __sincosf_stret : RuntimeLibcallImpl<SINCOS_STRET_F32>;
+
def __sincospi : RuntimeLibcallImpl<SINCOSPI_F64>;
def __sincospif : RuntimeLibcallImpl<SINCOSPI_F32>;
diff --git a/llvm/include/llvm/IR/RuntimeLibcallsImpl.td b/llvm/include/llvm/IR/RuntimeLibcallsImpl.td
index 025d1d4e78095..1c8708bc19061 100644
--- a/llvm/include/llvm/IR/RuntimeLibcallsImpl.td
+++ b/llvm/include/llvm/IR/RuntimeLibcallsImpl.td
@@ -98,6 +98,9 @@ def FP128 : FuncArgType;
def PPCFP128 : FuncArgType;
def Floating : FuncArgType; // Any floating type.
def Ptr : FuncArgType; // Any pointer type.
+// C structs, lowered to IR types according to the target's C ABI.
+def CFltPair : FuncArgType; // struct { float, float }
+def CDblPair : FuncArgType; // struct { double, double }
def AllocaPtr : FuncArgType; // Pointer in the alloca address space.
def Struct : FuncArgType; // Any struct type.
def Ellip : FuncArgType; // The ellipsis (...).
diff --git a/llvm/lib/IR/RuntimeLibcalls.cpp b/llvm/lib/IR/RuntimeLibcalls.cpp
index 8a0f221a8c548..ab49f96fdc585 100644
--- a/llvm/lib/IR/RuntimeLibcalls.cpp
+++ b/llvm/lib/IR/RuntimeLibcalls.cpp
@@ -58,7 +58,8 @@ RuntimeLibcallsInfo::RuntimeLibcallsInfo(
const Triple &TT, ExceptionHandling ExceptionModel,
FloatABI::ABIType FloatABI, std::optional<LongDoubleFormat> LDblFormat,
StringRef ABIName, VectorLibrary VecLib)
- : LongDoubleFmt(LDblFormat.value_or(TT.getDefaultLongDoubleFormat())) {
+ : LongDoubleFmt(LDblFormat.value_or(TT.getDefaultLongDoubleFormat())),
+ ABIName(ABIName) {
// Only an unspecified model resolves to the triple default; None is left as
// an explicit disable.
if (ExceptionModel == ExceptionHandling::Default)
@@ -409,6 +410,30 @@ static Attribute::AttrKind getIntExtAttr(FuncArgTypeID ID, Type *Ty,
return Attribute::None;
}
+static FuncArgTypeID getCPairElementType(FuncArgTypeID ID) {
+ switch (ID) {
+ case CFltPair:
+ return Flt;
+ case CDblPair:
+ return Dbl;
+ default:
+ return NoFuncArgType;
+ }
+}
+
+std::pair<Type *, Type *>
+RuntimeLibcallsInfo::lowerCPairReturn(const Triple &TT, Type *EltTy) const {
+ Type *PairTy = StructType::get(EltTy, EltTy);
+ // These return small structs in memory.
+ if (TT.isX86_32() ||
+ ((TT.isARM() || TT.isThumb()) && !isAAPCS_ABI(TT, ABIName)))
+ return {Type::getVoidTy(EltTy->getContext()), PairTy};
+ // x86-64 SysV returns a pair of floats packed in one SSE register.
+ if (TT.isX86_64() && !TT.isOSWindows() && EltTy->isFloatTy())
+ return {FixedVectorType::get(EltTy, 2), nullptr};
+ return {PairTy, nullptr};
+}
+
std::pair<FunctionType *, AttributeList>
RuntimeLibcallsInfo::getDefaultFunctionTy(
LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
@@ -421,16 +446,32 @@ RuntimeLibcallsInfo::getDefaultFunctionTy(
return {};
FuncArgTypeID LastID = ProtoTypes[0];
- Type *RetTy = convertToIRType(LastID, Ctx, TT, DL, LongDoubleFmt);
- AttributeSet RetAttrs;
- if (Attribute::AttrKind AK =
- getIntExtAttr(LastID, RetTy, TT, /*IsReturn=*/true))
- RetAttrs = AttributeSet::get(Ctx, Attribute::get(Ctx, AK));
- if (isInRegStruct(LastID))
- RetAttrs = RetAttrs.addAttribute(Ctx, Attribute::InReg);
-
SmallVector<Type *, 4> ArgTys;
SmallVector<AttributeSet, 4> ArgAttrs;
+ Type *RetTy;
+ AttributeSet RetAttrs;
+ if (FuncArgTypeID EltID = getCPairElementType(LastID);
+ EltID != NoFuncArgType) {
+ Type *EltTy = convertToIRType(EltID, Ctx, TT, DL, LongDoubleFmt);
+ Type *SRetTy;
+ std::tie(RetTy, SRetTy) = lowerCPairReturn(TT, EltTy);
+ if (SRetTy) {
+ AttrBuilder SRetAttrs(Ctx);
+ SRetAttrs.addStructRetAttr(SRetTy);
+ SRetAttrs.addAlignmentAttr(DL.getABITypeAlign(SRetTy));
+ ArgTys.push_back(DL.getAllocaPtrType(Ctx));
+ ArgAttrs.push_back(AttributeSet::get(Ctx, SRetAttrs));
+ }
+ } else {
+ RetTy = convertToIRType(LastID, Ctx, TT, DL, LongDoubleFmt);
+ if (Attribute::AttrKind AK =
+ getIntExtAttr(LastID, RetTy, TT, /*IsReturn=*/true))
+ RetAttrs = AttributeSet::get(Ctx, Attribute::get(Ctx, AK));
+ if (isInRegStruct(LastID))
+ RetAttrs = RetAttrs.addAttribute(Ctx, Attribute::InReg);
+ }
+
+ unsigned FirstArgNo = ArgTys.size();
bool IsVarArg = false;
unsigned Idx = 1;
for (FuncArgTypeID TyID = ProtoTypes[Idx]; TyID != NoFuncArgType;
@@ -460,7 +501,7 @@ RuntimeLibcallsInfo::getDefaultFunctionTy(
for (const LibcallArgAttrs &Static :
getLibcallImplArgAttributes(LibcallImpl)) {
- assert(Static.ArgNo < ArgAttrs.size() &&
+ assert(FirstArgNo + Static.ArgNo < ArgAttrs.size() &&
"attributes for nonexistent argument");
AttributeSet StaticAttrs = getLibcallArgAttributeSet(Ctx, Static.AttrSetID);
if (Static.PointeeType != NoFuncArgType) {
@@ -479,7 +520,7 @@ RuntimeLibcallsInfo::getDefaultFunctionTy(
StaticAttrs = StaticAttrs.addSortedAttributes(Ctx, PointeeAttrs);
}
- AttributeSet &ArgAttrSet = ArgAttrs[Static.ArgNo];
+ AttributeSet &ArgAttrSet = ArgAttrs[FirstArgNo + Static.ArgNo];
ArgAttrSet = ArgAttrSet.addAttributes(Ctx, StaticAttrs);
}
@@ -512,44 +553,15 @@ RuntimeLibcallsInfo::getFunctionTy(LLVMContext &Ctx, const Triple &TT,
// FIXME: Should also include __sincospi[f]_stret
case RTLIB::impl___sincos_stret:
case RTLIB::impl___sincosf_stret: {
- if (!darwinHasSinCosStret(TT)) // Non-darwin currently unexpected
- return {};
-
- Type *ScalarTy = LibcallImpl == RTLIB::impl___sincosf_stret
- ? Type::getFloatTy(Ctx)
- : Type::getDoubleTy(Ctx);
-
+ auto [FuncTy, Attrs] = getDefaultFunctionTy(Ctx, TT, DL, LibcallImpl);
+ bool UseSRet = FuncTy->getNumParams() != 0 &&
+ Attrs.hasParamAttr(0, Attribute::StructRet);
AttrBuilder FuncAttrBuilder(Ctx);
for (Attribute::AttrKind Attr : CommonFnAttrs)
FuncAttrBuilder.addAttribute(Attr);
-
- const bool UseSret =
- TT.isX86_32() || ((TT.isARM() || TT.isThumb()) &&
- ARM::computeTargetABI(TT) == ARM::ARM_ABI_APCS);
-
FuncAttrBuilder.addMemoryAttr(MemoryEffects::argMemOnly(
- UseSret ? ModRefInfo::Mod : ModRefInfo::NoModRef));
-
- AttributeList Attrs;
- Attrs = Attrs.addFnAttributes(Ctx, FuncAttrBuilder);
-
- if (UseSret) {
- AttrBuilder AttrBuilder(Ctx);
- StructType *StructTy = StructType::get(ScalarTy, ScalarTy);
- AttrBuilder.addStructRetAttr(StructTy);
- AttrBuilder.addAlignmentAttr(DL.getABITypeAlign(StructTy));
- FunctionType *FuncTy = FunctionType::get(
- Type::getVoidTy(Ctx), {DL.getAllocaPtrType(Ctx), ScalarTy}, false);
-
- return {FuncTy, Attrs.addParamAttributes(Ctx, 0, AttrBuilder)};
- }
-
- Type *RetTy =
- LibcallImpl == RTLIB::impl___sincosf_stret && TT.isX86_64()
- ? static_cast<Type *>(FixedVectorType::get(ScalarTy, 2))
- : static_cast<Type *>(StructType::get(ScalarTy, ScalarTy));
-
- return {FunctionType::get(RetTy, {ScalarTy}, false), Attrs};
+ UseSRet ? ModRefInfo::Mod : ModRefInfo::NoModRef));
+ return {FuncTy, Attrs.addFnAttributes(Ctx, FuncAttrBuilder)};
}
case RTLIB::impl_malloc:
case RTLIB::impl_calloc: {
diff --git a/llvm/test/TableGen/RuntimeLibcallEmitter.td b/llvm/test/TableGen/RuntimeLibcallEmitter.td
index a6ebe2bfe073d..f6407fb65ba02 100644
--- a/llvm/test/TableGen/RuntimeLibcallEmitter.td
+++ b/llvm/test/TableGen/RuntimeLibcallEmitter.td
@@ -122,6 +122,8 @@ def BlahLibrary : SystemRuntimeLibrary<isBlahArch, ["libc"]>;
// CHECK-NEXT: AllocaPtr,
// CHECK-NEXT: BFloat,
// CHECK-NEXT: Bool,
+// CHECK-NEXT: CDblPair,
+// CHECK-NEXT: CFltPair,
// CHECK-NEXT: CmpResult,
// CHECK-NEXT: Dbl,
// CHECK-NEXT: Ellip,
More information about the llvm-branch-commits
mailing list