Skip to content

[clang codegen] Emit int TBAA metadata on FP math libcall expf #96025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,34 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
const CallExpr *E, llvm::Constant *calleeValue) {
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
RValue Call =
CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());

// Check the supported intrinsic.
if (unsigned BuiltinID = FD->getBuiltinID()) {
auto IsErrnoIntrinsic = [&]() -> unsigned {
switch (BuiltinID) {
case Builtin::BIexpf:
case Builtin::BI__builtin_expf:
case Builtin::BI__builtin_expf128:
return true;
}
// TODO: support more FP math libcalls
return false;
}();

// Restrict to target with errno, for example, MacOS doesn't set errno.
if (IsErrnoIntrinsic && CGF.CGM.getLangOpts().MathErrno &&
!CGF.Builder.getIsFPConstrained()) {
ASTContext &Context = CGF.getContext();
// Emit "int" TBAA metadata on FP math libcalls.
clang::QualType IntTy = Context.IntTy;
TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
CGF.CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
}
}
return Call;
}

/// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
Expand Down
9 changes: 6 additions & 3 deletions clang/test/CodeGen/math-libcalls-tbaa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ extern "C" float expf(float);
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[NUM]], i64 40
// CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2:![0-9]+]]
// CHECK-NEXT: [[CALL:%.*]] = tail call float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]]
// CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA2]]
// CHECK-NEXT: [[MUL:%.*]] = fmul float [[CALL]], [[TMP1]]
// CHECK-NEXT: [[CALL:%.*]] = tail call float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]], !tbaa [[TBAA6:![0-9]+]]
// CHECK-NEXT: [[MUL:%.*]] = fmul float [[TMP0]], [[CALL]]
// CHECK-NEXT: ret float [[MUL]]
//
extern "C" float foo (float num[], float r2inv, int n) {
Expand All @@ -27,11 +26,15 @@ extern "C" float foo (float num[], float r2inv, int n) {
// NoNewStructPathTBAA: [[META3]] = !{!"float", [[META4:![0-9]+]], i64 0}
// NoNewStructPathTBAA: [[META4]] = !{!"omnipotent char", [[META5:![0-9]+]], i64 0}
// NoNewStructPathTBAA: [[META5]] = !{!"Simple C++ TBAA"}
// NoNewStructPathTBAA: [[TBAA6]] = !{[[META7:![0-9]+]], [[META7]], i64 0}
// NoNewStructPathTBAA: [[META7]] = !{!"int", [[META4]], i64 0}
//.
// NewStructPathTBAA: [[TBAA2]] = !{[[META3:![0-9]+]], [[META3]], i64 0, i64 4}
// NewStructPathTBAA: [[META3]] = !{[[META4:![0-9]+]], i64 4, !"float"}
// NewStructPathTBAA: [[META4]] = !{[[META5:![0-9]+]], i64 1, !"omnipotent char"}
// NewStructPathTBAA: [[META5]] = !{!"Simple C++ TBAA"}
// NewStructPathTBAA: [[TBAA6]] = !{[[META7:![0-9]+]], [[META7]], i64 0, i64 4}
// NewStructPathTBAA: [[META7]] = !{[[META4]], i64 4, !"int"}
//.
//// NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
// NewStructPathTBAA: {{.*}}
Expand Down
Loading