Skip to content

Commit fe88052

Browse files
vfdffyuxuanchen1997
authored andcommitted
[clang codegen] Emit int TBAA metadata on FP math libcall expf (#96025)
Summary: Base on the discussion https://discourse.llvm.org/t/fp-can-we-add-pure-attribute-for-math-library-functions-default/79459, math libcalls set errno, so it should emit "int" TBAA metadata on FP libcalls to solve the alias issue. Note: Only add support for expf in this PR Fix #86635 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251692
1 parent ef83e63 commit fe88052

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,34 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
688688
const CallExpr *E, llvm::Constant *calleeValue) {
689689
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
690690
CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
691-
return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
691+
RValue Call =
692+
CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
693+
694+
// Check the supported intrinsic.
695+
if (unsigned BuiltinID = FD->getBuiltinID()) {
696+
auto IsErrnoIntrinsic = [&]() -> unsigned {
697+
switch (BuiltinID) {
698+
case Builtin::BIexpf:
699+
case Builtin::BI__builtin_expf:
700+
case Builtin::BI__builtin_expf128:
701+
return true;
702+
}
703+
// TODO: support more FP math libcalls
704+
return false;
705+
}();
706+
707+
// Restrict to target with errno, for example, MacOS doesn't set errno.
708+
if (IsErrnoIntrinsic && CGF.CGM.getLangOpts().MathErrno &&
709+
!CGF.Builder.getIsFPConstrained()) {
710+
ASTContext &Context = CGF.getContext();
711+
// Emit "int" TBAA metadata on FP math libcalls.
712+
clang::QualType IntTy = Context.IntTy;
713+
TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
714+
Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
715+
CGF.CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
716+
}
717+
}
718+
return Call;
692719
}
693720

694721
/// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*

clang/test/CodeGen/math-libcalls-tbaa.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ extern "C" float expf(float);
1212
// CHECK-NEXT: [[ENTRY:.*:]]
1313
// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[NUM]], i64 40
1414
// CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2:![0-9]+]]
15-
// CHECK-NEXT: [[CALL:%.*]] = tail call float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]]
16-
// CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
17-
// CHECK-NEXT: [[MUL:%.*]] = fmul float [[CALL]], [[TMP1]]
15+
// CHECK-NEXT: [[CALL:%.*]] = tail call float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]], !tbaa [[TBAA6:![0-9]+]]
16+
// CHECK-NEXT: [[MUL:%.*]] = fmul float [[TMP0]], [[CALL]]
1817
// CHECK-NEXT: ret float [[MUL]]
1918
//
2019
extern "C" float foo (float num[], float r2inv, int n) {
@@ -27,11 +26,15 @@ extern "C" float foo (float num[], float r2inv, int n) {
2726
// NoNewStructPathTBAA: [[META3]] = !{!"float", [[META4:![0-9]+]], i64 0}
2827
// NoNewStructPathTBAA: [[META4]] = !{!"omnipotent char", [[META5:![0-9]+]], i64 0}
2928
// NoNewStructPathTBAA: [[META5]] = !{!"Simple C++ TBAA"}
29+
// NoNewStructPathTBAA: [[TBAA6]] = !{[[META7:![0-9]+]], [[META7]], i64 0}
30+
// NoNewStructPathTBAA: [[META7]] = !{!"int", [[META4]], i64 0}
3031
//.
3132
// NewStructPathTBAA: [[TBAA2]] = !{[[META3:![0-9]+]], [[META3]], i64 0, i64 4}
3233
// NewStructPathTBAA: [[META3]] = !{[[META4:![0-9]+]], i64 4, !"float"}
3334
// NewStructPathTBAA: [[META4]] = !{[[META5:![0-9]+]], i64 1, !"omnipotent char"}
3435
// NewStructPathTBAA: [[META5]] = !{!"Simple C++ TBAA"}
36+
// NewStructPathTBAA: [[TBAA6]] = !{[[META7:![0-9]+]], [[META7]], i64 0, i64 4}
37+
// NewStructPathTBAA: [[META7]] = !{[[META4]], i64 4, !"int"}
3538
//.
3639
//// NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
3740
// NewStructPathTBAA: {{.*}}

0 commit comments

Comments
 (0)