https://github.com/vfdff created https://github.com/llvm/llvm-project/pull/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. Fix https://github.com/llvm/llvm-project/issues/86635 >From 741904a869b41d423346082150a0be76e34b2812 Mon Sep 17 00:00:00 2001 From: zhongyunde 00443407 <zhongyu...@huawei.com> Date: Tue, 18 Jun 2024 09:21:07 -0400 Subject: [PATCH] [TBAA] Emit int TBAA metadata on FP math libcalls 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. Fix https://github.com/llvm/llvm-project/issues/86635 --- clang/lib/CodeGen/CGBuiltin.cpp | 33 ++++++++++++++++++++++- clang/test/CodeGen/math-libcalls-tbaa.cpp | 22 +++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGen/math-libcalls-tbaa.cpp diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 08a89bd123d03..dc4af109cdbdb 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -707,7 +707,38 @@ 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 IntrinsicID = [&]() -> 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; + }(); + + if (IntrinsicID) { + llvm::MDBuilder MDHelper(CGF.getLLVMContext()); + MDNode *RootMD; + if (CGF.getLangOpts().CPlusPlus) + RootMD = MDHelper.createTBAARoot("Simple C++ TBAA"); + else + RootMD = MDHelper.createTBAARoot("Simple C/C++ TBAA"); + // Emit "int" TBAA metadata on FP math libcalls. + MDNode *AliasType = MDHelper.createTBAANode("int", RootMD); + MDNode *MDInt = MDHelper.createTBAAStructTagNode(AliasType, AliasType, 0); + + Value *Val = Call.getScalarVal(); + cast<llvm::Instruction>(Val)->setMetadata(LLVMContext::MD_tbaa, MDInt); + } + } + 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 new file mode 100644 index 0000000000000..b3241c97da71f --- /dev/null +++ b/clang/test/CodeGen/math-libcalls-tbaa.cpp @@ -0,0 +1,22 @@ +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2 +// RUN: %clang -S -O3 -emit-llvm -o - -x c++ %s | FileCheck %s -check-prefixes=CHECK + +#include <math.h> + + +// Emit int TBAA metadata on FP math libcalls, which is useful for alias analysis + +// CHECK-LABEL: define dso_local noundef float @_Z3fooPffi +// CHECK-SAME: (ptr nocapture noundef readonly [[NUM:%.*]], float noundef [[R2INV:%.*]], i32 noundef [[N:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { +// CHECK-NEXT: entry: +// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[NUM]], i64 40 +// CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA6:![0-9]+]] +// CHECK-NEXT: [[CALL_I:%.*]] = tail call noundef float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]], !tbaa [[TBAA10:![0-9]+]] +// CHECK-NEXT: [[MUL:%.*]] = fmul float [[TMP0]], [[CALL_I]] +// CHECK-NEXT: ret float [[MUL]] +// +float foo (float num[], float r2inv, int n) { + const float expm2 = std::exp(num[10]); # Emit TBAA metadata on @expf + float tmp = expm2 * num[10]; + return tmp; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits