https://github.com/vfdff updated https://github.com/llvm/llvm-project/pull/96025
>From ed6292fd0e9119322c39e5f37e2225c76e324101 Mon Sep 17 00:00:00 2001 From: zhongyunde 00443407 <zhongyu...@huawei.com> Date: Tue, 18 Jun 2024 09:21:07 -0400 Subject: [PATCH 1/2] [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..d5b0741a476cb --- /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; +} >From 9990877a2b9736c684c8cabeb03c6d98a3d078ce Mon Sep 17 00:00:00 2001 From: zhongyunde 00443407 <zhongyu...@huawei.com> Date: Thu, 20 Jun 2024 02:29:11 -0400 Subject: [PATCH 2/2] Address comment, reuse CodeGenTBAA::getRoot --- clang/lib/CodeGen/CGBuiltin.cpp | 6 +----- clang/lib/CodeGen/CodeGenModule.h | 2 ++ clang/lib/CodeGen/CodeGenTBAA.h | 8 ++++---- clang/test/CodeGen/math-libcalls-tbaa.cpp | 20 +++++++++++++++----- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index dc4af109cdbdb..3f448e11bca1a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -725,11 +725,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD, 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"); + MDNode *RootMD = CGF.CGM.getTBAARoot(); // Emit "int" TBAA metadata on FP math libcalls. MDNode *AliasType = MDHelper.createTBAANode("int", RootMD); MDNode *MDInt = MDHelper.createTBAAStructTagNode(AliasType, AliasType, 0); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 9b63f47ef42cb..ea77007fdc48d 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -783,6 +783,8 @@ class CodeGenModule : public CodeGenTypeCache { CtorList &getGlobalCtors() { return GlobalCtors; } CtorList &getGlobalDtors() { return GlobalDtors; } + llvm::MDNode *getTBAARoot() { return TBAA->getRoot(); }; + /// getTBAATypeInfo - Get metadata used to describe accesses to objects of /// the given type. llvm::MDNode *getTBAATypeInfo(QualType QTy); diff --git a/clang/lib/CodeGen/CodeGenTBAA.h b/clang/lib/CodeGen/CodeGenTBAA.h index 5d9ecec3ff0fe..2a3a73bd3fd34 100644 --- a/clang/lib/CodeGen/CodeGenTBAA.h +++ b/clang/lib/CodeGen/CodeGenTBAA.h @@ -140,10 +140,6 @@ class CodeGenTBAA { llvm::MDNode *Root; llvm::MDNode *Char; - /// getRoot - This is the mdnode for the root of the metadata type graph - /// for this translation unit. - llvm::MDNode *getRoot(); - /// getChar - This is the mdnode for "char", which is special, and any types /// considered to be equivalent to it. llvm::MDNode *getChar(); @@ -178,6 +174,10 @@ class CodeGenTBAA { MangleContext &MContext); ~CodeGenTBAA(); + /// getRoot - This is the mdnode for the root of the metadata type graph + /// for this translation unit. + llvm::MDNode *getRoot(); + /// getTypeInfo - Get metadata used to describe accesses to objects of the /// given type. llvm::MDNode *getTypeInfo(QualType QTy); diff --git a/clang/test/CodeGen/math-libcalls-tbaa.cpp b/clang/test/CodeGen/math-libcalls-tbaa.cpp index d5b0741a476cb..b0a748b86e423 100644 --- a/clang/test/CodeGen/math-libcalls-tbaa.cpp +++ b/clang/test/CodeGen/math-libcalls-tbaa.cpp @@ -1,14 +1,16 @@ -// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2 +// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 +// The test may fail as time out on windows +// REQUIRES: system-linux + // 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-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]+]] @@ -20,3 +22,11 @@ float foo (float num[], float r2inv, int n) { float tmp = expm2 * num[10]; return tmp; } +//. +// CHECK: [[TBAA6]] = !{[[META7:![0-9]+]], [[META7]], i64 0} +// CHECK: [[META7]] = !{!"float", [[META8:![0-9]+]], i64 0} +// CHECK: [[META8]] = !{!"omnipotent char", [[META9:![0-9]+]], i64 0} +// CHECK: [[META9]] = !{!"Simple C++ TBAA"} +// CHECK: [[TBAA10]] = !{[[META11:![0-9]+]], [[META11]], i64 0} +// CHECK: [[META11]] = !{!"int", [[META9]]} +//. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits