Author: Allen Date: 2024-07-19T11:19:21+08:00 New Revision: 1df2e0c344f0ddf7e09a9c89eba6bbee52142344
URL: https://github.com/llvm/llvm-project/commit/1df2e0c344f0ddf7e09a9c89eba6bbee52142344 DIFF: https://github.com/llvm/llvm-project/commit/1df2e0c344f0ddf7e09a9c89eba6bbee52142344.diff LOG: [clang codegen] Emit int TBAA metadata on FP math libcall expf (#96025) 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 https://github.com/llvm/llvm-project/issues/86635 Added: Modified: clang/lib/CodeGen/CGBuiltin.cpp clang/test/CodeGen/math-libcalls-tbaa.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index f426570b17e15..2d32a8582cfdf 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -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.* diff --git a/clang/test/CodeGen/math-libcalls-tbaa.cpp b/clang/test/CodeGen/math-libcalls-tbaa.cpp index 0b231d474df77..f15938dee0272 100644 --- a/clang/test/CodeGen/math-libcalls-tbaa.cpp +++ b/clang/test/CodeGen/math-libcalls-tbaa.cpp @@ -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) { @@ -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: {{.*}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits