Skip to content

Commit b7423af

Browse files
authored
RuntimeLibcalls: Add entries for vector sincospi functions (#166981)
Add libcall entries for sleef and armpl sincospi implementations. This is the start of adding the vector library functions; eventually they should all be tracked here. I'm starting with this case because this is a prerequisite to fix reporting sincospi calls which do not exist on any common targets without regressing vector codegen when these libraries are available.
1 parent e9d3340 commit b7423af

File tree

6 files changed

+168
-10
lines changed

6 files changed

+168
-10
lines changed

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,7 @@ struct RuntimeLibcallsInfo {
8383
const Triple &TT,
8484
ExceptionHandling ExceptionModel = ExceptionHandling::None,
8585
FloatABI::ABIType FloatABI = FloatABI::Default,
86-
EABI EABIVersion = EABI::Default, StringRef ABIName = "") {
87-
// FIXME: The ExceptionModel parameter is to handle the field in
88-
// TargetOptions. This interface fails to distinguish the forced disable
89-
// case for targets which support exceptions by default. This should
90-
// probably be a module flag and removed from TargetOptions.
91-
if (ExceptionModel == ExceptionHandling::None)
92-
ExceptionModel = TT.getDefaultExceptionHandling();
93-
94-
initLibcalls(TT, ExceptionModel, FloatABI, EABIVersion, ABIName);
95-
}
86+
EABI EABIVersion = EABI::Default, StringRef ABIName = "");
9687

9788
explicit RuntimeLibcallsInfo(const Module &M);
9889

@@ -170,6 +161,10 @@ struct RuntimeLibcallsInfo {
170161
getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
171162
RTLIB::LibcallImpl LibcallImpl) const;
172163

164+
/// Returns true if the function has a vector mask argument, which is assumed
165+
/// to be the last argument.
166+
static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl);
167+
173168
private:
174169
LLVM_ABI static iota_range<RTLIB::LibcallImpl>
175170
lookupLibcallImplNameImpl(StringRef Name);

llvm/include/llvm/IR/RuntimeLibcalls.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ foreach FPTy = ["F32", "F64", "F80", "F128", "PPCF128"] in {
182182
def MODF_#FPTy : RuntimeLibcall;
183183
}
184184

185+
foreach VecTy = ["V4F32", "V2F64", "NXV4F32", "NXV2F64"] in {
186+
def SINCOSPI_#VecTy : RuntimeLibcall;
187+
}
188+
185189
def FEGETENV : RuntimeLibcall;
186190
def FESETENV : RuntimeLibcall;
187191

@@ -1078,6 +1082,28 @@ def __security_check_cookie : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE>;
10781082
def __security_check_cookie_arm64ec : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE,
10791083
"#__security_check_cookie_arm64ec">;
10801084

1085+
//===----------------------------------------------------------------------===//
1086+
// sleef calls
1087+
//===----------------------------------------------------------------------===//
1088+
1089+
defset list<RuntimeLibcallImpl> SleefLibcalls = {
1090+
def _ZGVnN4vl4l4_sincospif : RuntimeLibcallImpl<SINCOSPI_V4F32>;
1091+
def _ZGVnN2vl8l8_sincospi : RuntimeLibcallImpl<SINCOSPI_V2F64>;
1092+
def _ZGVsNxvl4l4_sincospif : RuntimeLibcallImpl<SINCOSPI_NXV4F32>;
1093+
def _ZGVsNxvl8l8_sincospi : RuntimeLibcallImpl<SINCOSPI_NXV2F64>;
1094+
}
1095+
1096+
//===----------------------------------------------------------------------===//
1097+
// ARMPL calls
1098+
//===----------------------------------------------------------------------===//
1099+
1100+
defset list<RuntimeLibcallImpl> ARMPLLibcalls = {
1101+
def armpl_vsincospiq_f32 : RuntimeLibcallImpl<SINCOSPI_V4F32>;
1102+
def armpl_vsincospiq_f64 : RuntimeLibcallImpl<SINCOSPI_V2F64>;
1103+
def armpl_svsincospi_f32_x : RuntimeLibcallImpl<SINCOSPI_NXV4F32>;
1104+
def armpl_svsincospi_f64_x : RuntimeLibcallImpl<SINCOSPI_NXV2F64>;
1105+
}
1106+
10811107
//===----------------------------------------------------------------------===//
10821108
// F128 libm Runtime Libcalls
10831109
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,24 @@ RTLIB::Libcall RTLIB::getSINCOS(EVT RetVT) {
430430
}
431431

432432
RTLIB::Libcall RTLIB::getSINCOSPI(EVT RetVT) {
433+
// TODO: Tablegen should generate this function
434+
if (RetVT.isVector()) {
435+
if (!RetVT.isSimple())
436+
return RTLIB::UNKNOWN_LIBCALL;
437+
switch (RetVT.getSimpleVT().SimpleTy) {
438+
case MVT::v4f32:
439+
return RTLIB::SINCOSPI_V4F32;
440+
case MVT::v2f64:
441+
return RTLIB::SINCOSPI_V2F64;
442+
case MVT::nxv4f32:
443+
return RTLIB::SINCOSPI_NXV4F32;
444+
case MVT::nxv2f64:
445+
return RTLIB::SINCOSPI_NXV2F64;
446+
default:
447+
return RTLIB::UNKNOWN_LIBCALL;
448+
}
449+
}
450+
433451
return getFPLibCall(RetVT, SINCOSPI_F32, SINCOSPI_F64, SINCOSPI_F80,
434452
SINCOSPI_F128, SINCOSPI_PPCF128);
435453
}

llvm/lib/IR/RuntimeLibcalls.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/ADT/FloatingPointMode.h"
1111
#include "llvm/ADT/StringTable.h"
1212
#include "llvm/IR/Module.h"
13+
#include "llvm/IR/SystemLibraries.h"
1314
#include "llvm/Support/Debug.h"
1415
#include "llvm/Support/xxhash.h"
1516
#include "llvm/TargetParser/ARMTargetParser.h"
@@ -25,6 +26,40 @@ using namespace RTLIB;
2526
#define DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
2627
#include "llvm/IR/RuntimeLibcalls.inc"
2728

29+
RuntimeLibcallsInfo::RuntimeLibcallsInfo(const Triple &TT,
30+
ExceptionHandling ExceptionModel,
31+
FloatABI::ABIType FloatABI,
32+
EABI EABIVersion, StringRef ABIName) {
33+
// FIXME: The ExceptionModel parameter is to handle the field in
34+
// TargetOptions. This interface fails to distinguish the forced disable
35+
// case for targets which support exceptions by default. This should
36+
// probably be a module flag and removed from TargetOptions.
37+
if (ExceptionModel == ExceptionHandling::None)
38+
ExceptionModel = TT.getDefaultExceptionHandling();
39+
40+
initLibcalls(TT, ExceptionModel, FloatABI, EABIVersion, ABIName);
41+
42+
// TODO: Tablegen should generate these sets
43+
switch (ClVectorLibrary) {
44+
case VectorLibrary::SLEEFGNUABI:
45+
for (RTLIB::LibcallImpl Impl :
46+
{RTLIB::impl__ZGVnN4vl4l4_sincospif, RTLIB::impl__ZGVnN2vl8l8_sincospi,
47+
RTLIB::impl__ZGVsNxvl4l4_sincospif,
48+
RTLIB::impl__ZGVsNxvl8l8_sincospi})
49+
setAvailable(Impl);
50+
break;
51+
case VectorLibrary::ArmPL:
52+
for (RTLIB::LibcallImpl Impl :
53+
{RTLIB::impl_armpl_vsincospiq_f32, RTLIB::impl_armpl_vsincospiq_f64,
54+
RTLIB::impl_armpl_svsincospi_f32_x,
55+
RTLIB::impl_armpl_svsincospi_f64_x})
56+
setAvailable(Impl);
57+
break;
58+
default:
59+
break;
60+
}
61+
}
62+
2863
RuntimeLibcallsInfo::RuntimeLibcallsInfo(const Module &M)
2964
: RuntimeLibcallsInfo(M.getTargetTriple()) {
3065
// TODO: Consider module flags
@@ -88,6 +123,8 @@ RuntimeLibcallsInfo::getFunctionTy(LLVMContext &Ctx, const Triple &TT,
88123
static constexpr Attribute::AttrKind CommonFnAttrs[] = {
89124
Attribute::NoCallback, Attribute::NoFree, Attribute::NoSync,
90125
Attribute::NoUnwind, Attribute::WillReturn};
126+
static constexpr Attribute::AttrKind CommonPtrArgAttrs[] = {
127+
Attribute::NoAlias, Attribute::WriteOnly, Attribute::NonNull};
91128

92129
switch (LibcallImpl) {
93130
case RTLIB::impl___sincos_stret:
@@ -151,9 +188,67 @@ RuntimeLibcallsInfo::getFunctionTy(LLVMContext &Ctx, const Triple &TT,
151188
fcNegNormal));
152189
return {FuncTy, Attrs};
153190
}
191+
case RTLIB::impl__ZGVnN4vl4l4_sincospif:
192+
case RTLIB::impl__ZGVnN2vl8l8_sincospi:
193+
case RTLIB::impl__ZGVsNxvl4l4_sincospif:
194+
case RTLIB::impl__ZGVsNxvl8l8_sincospi:
195+
case RTLIB::impl_armpl_vsincospiq_f32:
196+
case RTLIB::impl_armpl_vsincospiq_f64:
197+
case RTLIB::impl_armpl_svsincospi_f32_x:
198+
case RTLIB::impl_armpl_svsincospi_f64_x: {
199+
AttrBuilder FuncAttrBuilder(Ctx);
200+
201+
bool IsF32 = LibcallImpl == RTLIB::impl__ZGVnN4vl4l4_sincospif ||
202+
LibcallImpl == RTLIB::impl__ZGVsNxvl4l4_sincospif ||
203+
LibcallImpl == RTLIB::impl_armpl_vsincospiq_f32 ||
204+
LibcallImpl == RTLIB::impl_armpl_svsincospi_f32_x;
205+
Type *ScalarTy = IsF32 ? Type::getFloatTy(Ctx) : Type::getDoubleTy(Ctx);
206+
unsigned EC = IsF32 ? 4 : 2;
207+
208+
bool IsScalable = LibcallImpl == RTLIB::impl__ZGVsNxvl4l4_sincospif ||
209+
LibcallImpl == RTLIB::impl__ZGVsNxvl8l8_sincospi ||
210+
LibcallImpl == RTLIB::impl_armpl_svsincospi_f32_x ||
211+
LibcallImpl == RTLIB::impl_armpl_svsincospi_f64_x;
212+
VectorType *VecTy = VectorType::get(ScalarTy, EC, IsScalable);
213+
214+
for (Attribute::AttrKind Attr : CommonFnAttrs)
215+
FuncAttrBuilder.addAttribute(Attr);
216+
FuncAttrBuilder.addMemoryAttr(MemoryEffects::argMemOnly(ModRefInfo::Mod));
217+
218+
AttributeList Attrs;
219+
Attrs = Attrs.addFnAttributes(Ctx, FuncAttrBuilder);
220+
221+
{
222+
AttrBuilder ArgAttrBuilder(Ctx);
223+
for (Attribute::AttrKind AK : CommonPtrArgAttrs)
224+
ArgAttrBuilder.addAttribute(AK);
225+
ArgAttrBuilder.addAlignmentAttr(DL.getABITypeAlign(VecTy));
226+
Attrs = Attrs.addParamAttributes(Ctx, 1, ArgAttrBuilder);
227+
Attrs = Attrs.addParamAttributes(Ctx, 2, ArgAttrBuilder);
228+
}
229+
230+
PointerType *PtrTy = PointerType::get(Ctx, 0);
231+
SmallVector<Type *, 4> ArgTys = {VecTy, PtrTy, PtrTy};
232+
if (hasVectorMaskArgument(LibcallImpl))
233+
ArgTys.push_back(VectorType::get(Type::getInt1Ty(Ctx), EC, IsScalable));
234+
235+
return {FunctionType::get(Type::getVoidTy(Ctx), ArgTys, false), Attrs};
236+
}
154237
default:
155238
return {};
156239
}
157240

158241
return {};
159242
}
243+
244+
bool RuntimeLibcallsInfo::hasVectorMaskArgument(RTLIB::LibcallImpl Impl) {
245+
/// FIXME: This should be generated by tablegen and support the argument at an
246+
/// arbitrary position
247+
switch (Impl) {
248+
case RTLIB::impl_armpl_svsincospi_f32_x:
249+
case RTLIB::impl_armpl_svsincospi_f64_x:
250+
return true;
251+
default:
252+
return false;
253+
}
254+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; REQUIRES: aarch64-registered-target
2+
; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=aarch64-unknown-linux -mattr=+neon,+sve -vector-library=ArmPL < %s | FileCheck %s
3+
4+
; CHECK: declare void @armpl_svsincospi_f32_x(<vscale x 4 x float>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16, <vscale x 4 x i1>) [[ATTRS:#[0-9]+]]
5+
6+
; CHECK: declare void @armpl_svsincospi_f64_x(<vscale x 2 x double>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16, <vscale x 2 x i1>) [[ATTRS:#[0-9]+]]
7+
8+
; CHECK: declare void @armpl_vsincospiq_f32(<4 x float>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS]]
9+
10+
; CHECK: declare void @armpl_vsincospiq_f64(<2 x double>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS]]
11+
12+
; CHECK: attributes [[ATTRS]] = { nocallback nofree nosync nounwind willreturn memory(argmem: write) }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; REQUIRES: aarch64-registered-target
2+
; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=aarch64-unknown-linux -mattr=+neon,+sve -vector-library=sleefgnuabi < %s | FileCheck %s
3+
4+
; CHECK: declare void @_ZGVnN2vl8l8_sincospi(<2 x double>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS:#[0-9]+]]
5+
6+
; CHECK: declare void @_ZGVnN4vl4l4_sincospif(<4 x float>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS]]
7+
8+
; CHECK: declare void @_ZGVsNxvl4l4_sincospif(<vscale x 4 x float>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS:#[0-9]+]]
9+
10+
; CHECK: declare void @_ZGVsNxvl8l8_sincospi(<vscale x 2 x double>, ptr noalias nonnull writeonly align 16, ptr noalias nonnull writeonly align 16) [[ATTRS:#[0-9]+]]
11+
12+
; CHECK: attributes [[ATTRS]] = { nocallback nofree nosync nounwind willreturn memory(argmem: write) }

0 commit comments

Comments
 (0)