Skip to content

Commit cd1d9a8

Browse files
authored
Reland "[clang] Lower modf builtin using llvm.modf intrinsic" (#129885)
Reverts #127987 Original description: This updates the existing modf[f|l] builtin to be lowered via the llvm.modf.* intrinsic (rather than directly to a library call). The legalization issues exposed by the original PR (#126750) should have been fixed in #128055 and #129264.
1 parent a685045 commit cd1d9a8

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,24 @@ static void emitSincosBuiltin(CodeGenFunction &CGF, const CallExpr *E,
859859
StoreCos->setMetadata(LLVMContext::MD_noalias, AliasScopeList);
860860
}
861861

862+
static llvm::Value *emitModfBuiltin(CodeGenFunction &CGF, const CallExpr *E,
863+
llvm::Intrinsic::ID IntrinsicID) {
864+
llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(0));
865+
llvm::Value *IntPartDest = CGF.EmitScalarExpr(E->getArg(1));
866+
867+
llvm::Value *Call =
868+
CGF.Builder.CreateIntrinsic(IntrinsicID, {Val->getType()}, Val);
869+
870+
llvm::Value *FractionalResult = CGF.Builder.CreateExtractValue(Call, 0);
871+
llvm::Value *IntegralResult = CGF.Builder.CreateExtractValue(Call, 1);
872+
873+
QualType DestPtrType = E->getArg(1)->getType()->getPointeeType();
874+
LValue IntegralLV = CGF.MakeNaturalAlignAddrLValue(IntPartDest, DestPtrType);
875+
CGF.EmitStoreOfScalar(IntegralResult, IntegralLV);
876+
877+
return FractionalResult;
878+
}
879+
862880
/// EmitFAbs - Emit a call to @llvm.fabs().
863881
static Value *EmitFAbs(CodeGenFunction &CGF, Value *V) {
864882
Function *F = CGF.CGM.getIntrinsic(Intrinsic::fabs, V->getType());
@@ -4120,6 +4138,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
41204138
case Builtin::BI__builtin_frexpf128:
41214139
case Builtin::BI__builtin_frexpf16:
41224140
return RValue::get(emitFrexpBuiltin(*this, E, Intrinsic::frexp));
4141+
case Builtin::BImodf:
4142+
case Builtin::BImodff:
4143+
case Builtin::BImodfl:
4144+
case Builtin::BI__builtin_modf:
4145+
case Builtin::BI__builtin_modff:
4146+
case Builtin::BI__builtin_modfl:
4147+
if (Builder.getIsFPConstrained())
4148+
break; // TODO: Emit constrained modf intrinsic once one exists.
4149+
return RValue::get(emitModfBuiltin(*this, E, Intrinsic::modf));
41234150
case Builtin::BI__builtin_isgreater:
41244151
case Builtin::BI__builtin_isgreaterequal:
41254152
case Builtin::BI__builtin_isless:

clang/test/CodeGen/X86/math-builtins.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
3838
// NO__ERRNO-NEXT: [[FREXP_F128_0:%.+]] = extractvalue { fp128, i32 } [[FREXP_F128]], 0
3939

4040

41+
// NO__ERRNO: [[MODF_F64:%.+]] = call { double, double } @llvm.modf.f64(double %{{.+}})
42+
// NO__ERRNO-NEXT: [[MODF_F64_FP:%.+]] = extractvalue { double, double } [[MODF_F64]], 0
43+
// NO__ERRNO-NEXT: [[MODF_F64_IP:%.+]] = extractvalue { double, double } [[MODF_F64]], 1
44+
// NO__ERRNO-NEXT: store double [[MODF_F64_IP]], ptr %{{.+}}, align 8
45+
46+
// NO__ERRNO: [[MODF_F32:%.+]] = call { float, float } @llvm.modf.f32(float %{{.+}})
47+
// NO__ERRNO-NEXT: [[MODF_F32_FP:%.+]] = extractvalue { float, float } [[MODF_F32]], 0
48+
// NO__ERRNO-NEXT: [[MODF_F32_IP:%.+]] = extractvalue { float, float } [[MODF_F32]], 1
49+
// NO__ERRNO-NEXT: store float [[MODF_F32_IP]], ptr %{{.+}}, align 4
50+
51+
// NO__ERRNO: [[MODF_F80:%.+]] = call { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80 %{{.+}})
52+
// NO__ERRNO-NEXT: [[MODF_F80_FP:%.+]] = extractvalue { x86_fp80, x86_fp80 } [[MODF_F80]], 0
53+
// NO__ERRNO-NEXT: [[MODF_F80_IP:%.+]] = extractvalue { x86_fp80, x86_fp80 } [[MODF_F80]], 1
54+
// NO__ERRNO-NEXT: store x86_fp80 [[MODF_F80_IP]], ptr %{{.+}}, align 16
55+
56+
// NO__ERRNO: call fp128 @modff128(fp128 noundef %{{.+}}, ptr noundef %{{.+}})
57+
58+
4159
// NO__ERRNO: [[SINCOS_F64:%.+]] = call { double, double } @llvm.sincos.f64(double %{{.+}})
4260
// NO__ERRNO-NEXT: [[SINCOS_F64_0:%.+]] = extractvalue { double, double } [[SINCOS_F64]], 0
4361
// NO__ERRNO-NEXT: [[SINCOS_F64_1:%.+]] = extractvalue { double, double } [[SINCOS_F64]], 1
@@ -158,13 +176,13 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
158176

159177
__builtin_modf(f,d); __builtin_modff(f,fp); __builtin_modfl(f,l); __builtin_modff128(f,l);
160178

161-
// NO__ERRNO: declare double @modf(double noundef, ptr noundef) [[NOT_READNONE:#[0-9]+]]
162-
// NO__ERRNO: declare float @modff(float noundef, ptr noundef) [[NOT_READNONE]]
163-
// NO__ERRNO: declare x86_fp80 @modfl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
164-
// NO__ERRNO: declare fp128 @modff128(fp128 noundef, ptr noundef) [[NOT_READNONE]]
165-
// HAS_ERRNO: declare double @modf(double noundef, ptr noundef) [[NOT_READNONE]]
166-
// HAS_ERRNO: declare float @modff(float noundef, ptr noundef) [[NOT_READNONE]]
167-
// HAS_ERRNO: declare x86_fp80 @modfl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
179+
// NO__ERRNO: declare { double, double } @llvm.modf.f64(double) [[READNONE_INTRINSIC]]
180+
// NO__ERRNO: declare { float, float } @llvm.modf.f32(float) [[READNONE_INTRINSIC]]
181+
// NO__ERRNO: declare { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80) [[READNONE_INTRINSIC]]
182+
// NO__ERRNO: declare fp128 @modff128(fp128 noundef, ptr noundef) [[NOT_READNONE:#[0-9]+]]
183+
// HAS_ERRNO: declare { double, double } @llvm.modf.f64(double) [[READNONE_INTRINSIC]]
184+
// HAS_ERRNO: declare { float, float } @llvm.modf.f32(float) [[READNONE_INTRINSIC]]
185+
// HAS_ERRNO: declare { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80) [[READNONE_INTRINSIC]]
168186
// HAS_ERRNO: declare fp128 @modff128(fp128 noundef, ptr noundef) [[NOT_READNONE]]
169187

170188
__builtin_nan(c); __builtin_nanf(c); __builtin_nanl(c); __builtin_nanf128(c);

clang/test/CodeGen/aix-builtin-mapping.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ int main()
1717
returnValue = __builtin_ldexpl(1.0L, 1);
1818
}
1919

20-
// CHECK: %call = call double @modf(double noundef 1.000000e+00, ptr noundef %DummyLongDouble) #3
20+
// CHECK: %{{.+}} = call { double, double } @llvm.modf.f64(double 1.000000e+00)
2121
// CHECK: %{{.+}} = call { double, i32 } @llvm.frexp.f64.i32(double 0.000000e+00)
2222
// CHECK: %{{.+}} = call double @llvm.ldexp.f64.i32(double 1.000000e+00, i32 1)

clang/test/CodeGen/builtin-attributes.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ char* f2(char* a, char* b) {
2424
return __builtin_strstr(a, b);
2525
}
2626

27+
// Note: Use asm label to disable intrinsic lowering of modf.
28+
double modf(double x, double*) asm("modf");
29+
float modff(float x, float*) asm("modff");
30+
long double modfl(long double x, long double*) asm("modfl");
31+
2732
// frexp is NOT readnone. It writes to its pointer argument.
2833
//
2934
// CHECK: f3
@@ -55,9 +60,9 @@ int f3(double x) {
5560
frexp(x, &e);
5661
frexpf(x, &e);
5762
frexpl(x, &e);
58-
__builtin_modf(x, &e);
59-
__builtin_modff(x, &e);
60-
__builtin_modfl(x, &e);
63+
modf(x, &e);
64+
modff(x, &e);
65+
modfl(x, &e);
6166
__builtin_remquo(x, x, &e);
6267
__builtin_remquof(x, x, &e);
6368
__builtin_remquol(x, x, &e);

clang/test/CodeGen/math-builtins-long.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ void foo(long double f, long double *l, int *i, const char *c) {
5858
// PPCF128: call fp128 @ldexpf128(fp128 noundef %{{.+}}, {{(signext)?.+}})
5959
__builtin_ldexpl(f,f);
6060

61-
// F80: call x86_fp80 @modfl(x86_fp80 noundef %{{.+}}, ptr noundef %{{.+}})
62-
// PPC: call ppc_fp128 @modfl(ppc_fp128 noundef %{{.+}}, ptr noundef %{{.+}})
63-
// X86F128: call fp128 @modfl(fp128 noundef %{{.+}}, ptr noundef %{{.+}})
61+
// F80: call { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80 %{{.+}})
62+
// PPC: call { ppc_fp128, ppc_fp128 } @llvm.modf.ppcf128(ppc_fp128 %{{.+}})
63+
// X86F128: call { fp128, fp128 } @llvm.modf.f128(fp128 %{{.+}})
6464
// PPCF128: call fp128 @modff128(fp128 noundef %{{.+}}, ptr noundef %{{.+}})
6565
__builtin_modfl(f,l);
6666

clang/test/CodeGen/math-libcalls.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
8383

8484
modf(f,d); modff(f,fp); modfl(f,l);
8585

86-
// NO__ERRNO: declare double @modf(double noundef, ptr noundef) [[NOT_READNONE]]
87-
// NO__ERRNO: declare float @modff(float noundef, ptr noundef) [[NOT_READNONE]]
88-
// NO__ERRNO: declare x86_fp80 @modfl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
89-
// HAS_ERRNO: declare double @modf(double noundef, ptr noundef) [[NOT_READNONE]]
90-
// HAS_ERRNO: declare float @modff(float noundef, ptr noundef) [[NOT_READNONE]]
91-
// HAS_ERRNO: declare x86_fp80 @modfl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
86+
// NO__ERRNO: declare { double, double } @llvm.modf.f64(double) [[READNONE_INTRINSIC]]
87+
// NO__ERRNO: declare { float, float } @llvm.modf.f32(float) [[READNONE_INTRINSIC]]
88+
// NO__ERRNO: declare { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80) [[READNONE_INTRINSIC]]
89+
// HAS_ERRNO: declare { double, double } @llvm.modf.f64(double) [[READNONE_INTRINSIC]]
90+
// HAS_ERRNO: declare { float, float } @llvm.modf.f32(float) [[READNONE_INTRINSIC]]
91+
// HAS_ERRNO: declare { x86_fp80, x86_fp80 } @llvm.modf.f80(x86_fp80) [[READNONE_INTRINSIC]]
9292
// HAS_MAYTRAP: declare double @modf(double noundef, ptr noundef) [[NOT_READNONE]]
9393
// HAS_MAYTRAP: declare float @modff(float noundef, ptr noundef) [[NOT_READNONE]]
9494
// HAS_MAYTRAP: declare x86_fp80 @modfl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]

0 commit comments

Comments
 (0)