Skip to content

Commit aafed97

Browse files
committed
Add hook to limit intrinsic use
1 parent 35de429 commit aafed97

File tree

4 files changed

+52
-11
lines changed

4 files changed

+52
-11
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,12 +2937,21 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
29372937
&getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
29382938
BuiltinID = mutateLongDoubleBuiltin(BuiltinID);
29392939

2940-
// If the builtin has been declared explicitly with an assembler label,
2941-
// disable the specialized emitting below. Ideally we should communicate the
2942-
// rename in IR, or at least avoid generating the intrinsic calls that are
2943-
// likely to get lowered to the renamed library functions.
2944-
const unsigned BuiltinIDIfNoAsmLabel =
2945-
FD->hasAttr<AsmLabelAttr>() ? 0 : BuiltinID;
2940+
const unsigned BuiltinIDIfEmitIntrinsics = [&] {
2941+
// If the builtin has been declared explicitly with an assembler label,
2942+
// disable the specialized emitting below. Ideally we should communicate the
2943+
// rename in IR, or at least avoid generating the intrinsic calls that are
2944+
// likely to get lowered to the renamed library functions.
2945+
if (FD->hasAttr<AsmLabelAttr>())
2946+
return 0U;
2947+
2948+
// If the target requests not to use intrinsics for a builtin, disable the
2949+
// specialized emitting below.
2950+
if (!getTargetHooks().shouldUseIntrinsicsForBuiltin(BuiltinID))
2951+
return 0U;
2952+
2953+
return BuiltinID;
2954+
}();
29462955

29472956
std::optional<bool> ErrnoOverriden;
29482957
// ErrnoOverriden is true if math-errno is overriden via the
@@ -3035,7 +3044,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
30353044
ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
30363045
}
30373046
if (GenerateIntrinsics) {
3038-
switch (BuiltinIDIfNoAsmLabel) {
3047+
switch (BuiltinIDIfEmitIntrinsics) {
30393048
case Builtin::BIacos:
30403049
case Builtin::BIacosf:
30413050
case Builtin::BIacosl:
@@ -3515,7 +3524,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
35153524
}
35163525
};
35173526

3518-
switch (BuiltinIDIfNoAsmLabel) {
3527+
switch (BuiltinIDIfEmitIntrinsics) {
35193528
default: break;
35203529
case Builtin::BI__builtin___CFStringMakeConstantString:
35213530
case Builtin::BI__builtin___NSStringMakeConstantString:
@@ -3645,7 +3654,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
36453654
case Builtin::BI__builtin_ctzl:
36463655
case Builtin::BI__builtin_ctzll:
36473656
case Builtin::BI__builtin_ctzg: {
3648-
bool HasFallback = BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_ctzg &&
3657+
bool HasFallback = BuiltinIDIfEmitIntrinsics == Builtin::BI__builtin_ctzg &&
36493658
E->getNumArgs() > 1;
36503659

36513660
Value *ArgValue =
@@ -3677,7 +3686,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
36773686
case Builtin::BI__builtin_clzl:
36783687
case Builtin::BI__builtin_clzll:
36793688
case Builtin::BI__builtin_clzg: {
3680-
bool HasFallback = BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_clzg &&
3689+
bool HasFallback = BuiltinIDIfEmitIntrinsics == Builtin::BI__builtin_clzg &&
36813690
E->getNumArgs() > 1;
36823691

36833692
Value *ArgValue =
@@ -4347,7 +4356,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
43474356
Ty = VecTy->getElementType();
43484357
bool IsSigned = Ty->isSignedIntegerType();
43494358
unsigned Opc;
4350-
if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_add_sat)
4359+
if (BuiltinIDIfEmitIntrinsics == Builtin::BI__builtin_elementwise_add_sat)
43514360
Opc = IsSigned ? llvm::Intrinsic::sadd_sat : llvm::Intrinsic::uadd_sat;
43524361
else
43534362
Opc = IsSigned ? llvm::Intrinsic::ssub_sat : llvm::Intrinsic::usub_sat;

clang/lib/CodeGen/TargetInfo.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,23 @@ class TargetCodeGenInfo {
454454
initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
455455
llvm::AttrBuilder &FuncAttrs);
456456

457+
/// Returns true if an intrinsic should be emitted for a specific builtin. By
458+
/// default, this disables emitting intrinsics for (non-prefixed) sincos
459+
/// builtins, as on targets that do not set llvm::TargetSubtargetInfo::useAA()
460+
/// the intrinsics can worsen codegen.
461+
/// TODO: Remove once the intrinsic lowering works well for all targets.
462+
virtual bool shouldUseIntrinsicsForBuiltin(unsigned BuiltinID) const {
463+
switch (BuiltinID) {
464+
case Builtin::BIsincos:
465+
case Builtin::BIsincosf:
466+
case Builtin::BIsincosl:
467+
return false;
468+
default:
469+
break;
470+
}
471+
return true;
472+
}
473+
457474
protected:
458475
static std::string qualifyWindowsLibrary(StringRef Lib);
459476

clang/lib/CodeGen/Targets/AArch64.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
181181
bool wouldInliningViolateFunctionCallABI(
182182
const FunctionDecl *Caller, const FunctionDecl *Callee) const override;
183183

184+
bool shouldUseIntrinsicsForBuiltin(unsigned BuiltinID) const override {
185+
return true;
186+
}
187+
184188
private:
185189
// Diagnose calls between functions with incompatible Streaming SVE
186190
// attributes.

clang/test/CodeGen/math-libcalls.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,17 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
660660
// HAS_MAYTRAP: declare float @llvm.experimental.constrained.sinh.f32(
661661
// HAS_MAYTRAP: declare x86_fp80 @llvm.experimental.constrained.sinh.f80(
662662

663+
sincos(f, d, d); sincosf(f, fp, fp); sincosl(f, l, l);
664+
665+
// NO__ERRNO: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
666+
// NO__ERRNO: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
667+
// NO__ERRNO: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
668+
// HAS__ERRNO: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
669+
// HAS__ERRNO: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
670+
// HAS__ERRNO: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
671+
// HAS_MAYTRAP: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
672+
// HAS_MAYTRAP: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
673+
// HAS_MAYTRAP: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
663674

664675
sqrt(f); sqrtf(f); sqrtl(f);
665676

0 commit comments

Comments
 (0)