[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-07-03 Thread via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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

vfdff wrote:

Ok, thanks

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-07-03 Thread via cfe-commits


@@ -0,0 +1,43 @@
+// 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,NoNewStructPathTBAA
+// RUN:  %clang -S -O3 -Xclang -new-struct-path-tbaa -emit-llvm -o - -x c++ %s 
| FileCheck %s -check-prefixes=CHECK,NewStructPathTBAA
+

vfdff wrote:

* I add a head file  in the test, and it could not find the head file 
with clang_cc1
* I'll try to relax the restriction, add that for windows because it run 
**timeout** with my first commit.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-07-03 Thread Zahira Ammarguellat via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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

zahiraam wrote:

Maybe it should be mentioned in the title of the PR which math function you are 
adding tbaa info for.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-07-03 Thread Zahira Ammarguellat via cfe-commits


@@ -0,0 +1,43 @@
+// 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,NoNewStructPathTBAA
+// RUN:  %clang -S -O3 -Xclang -new-struct-path-tbaa -emit-llvm -o - -x c++ %s 
| FileCheck %s -check-prefixes=CHECK,NewStructPathTBAA
+

zahiraam wrote:

Why are you using clang here instead of clang_cc1. I think in general CodeGen 
tests should use clang_cc1. I would think the same than what the other tbaa LIT 
tests are written. 

Why does the test fail on Windows? I don't see any reason to require Linux for 
this test.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-07-03 Thread via cfe-commits

vfdff wrote:

ping ?

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-29 Thread via cfe-commits

vfdff wrote:

hi, is there any new suggestion ?

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-26 Thread via cfe-commits

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 
Date: Tue, 18 Jun 2024 09:21:07 -0400
Subject: [PATCH 1/6] [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 , 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(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 0..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 
+
+
+// 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 
Date: Thu, 20 Jun 2024 02:29:11 -0400
Subject: [PATCH 2/6] 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 , 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 

[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-26 Thread via cfe-commits


@@ -707,7 +707,39 @@ static RValue emitLibraryCall(CodeGenFunction , 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;
+}();
+
+// Restrict to target with errno, for example, MacOS doesn't set errno.
+if (IntrinsicID && CGF.CGM.getLangOpts().MathErrno) {
+  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
+  ASTContext  = CGF.getContext();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  clang::QualType IntTy = Context.IntTy;
+  MDNode *AliasType = CGF.CGM.getTBAATypeInfo(IntTy);
+  MDNode *MDInt = MDHelper.createTBAAStructTagNode(AliasType, AliasType, 
0);

vfdff wrote:

Apply your comment, thanks very much 

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-26 Thread via cfe-commits

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 
Date: Tue, 18 Jun 2024 09:21:07 -0400
Subject: [PATCH 1/5] [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 , 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(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 0..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 
+
+
+// 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 
Date: Thu, 20 Jun 2024 02:29:11 -0400
Subject: [PATCH 2/5] 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 , 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 

[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-25 Thread Eli Friedman via cfe-commits


@@ -707,7 +707,39 @@ static RValue emitLibraryCall(CodeGenFunction , 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;
+}();
+
+// Restrict to target with errno, for example, MacOS doesn't set errno.
+if (IntrinsicID && CGF.CGM.getLangOpts().MathErrno) {
+  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
+  ASTContext  = CGF.getContext();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  clang::QualType IntTy = Context.IntTy;
+  MDNode *AliasType = CGF.CGM.getTBAATypeInfo(IntTy);
+  MDNode *MDInt = MDHelper.createTBAAStructTagNode(AliasType, AliasType, 
0);

efriedma-quic wrote:

I think you want CodeGenModule::getTBAAAccessTagInfo, not getTBAATypeInfo?  (My 
mistake.)

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-25 Thread via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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 = CGF.CGM.getTBAARoot();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  MDNode *AliasType = MDHelper.createTBAANode("int", RootMD);

vfdff wrote:

Yes, apply your comment, thanks

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-25 Thread via cfe-commits

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 
Date: Tue, 18 Jun 2024 09:21:07 -0400
Subject: [PATCH 1/4] [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 , 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(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 0..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 
+
+
+// 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 
Date: Thu, 20 Jun 2024 02:29:11 -0400
Subject: [PATCH 2/4] 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 , 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 

[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-25 Thread via cfe-commits

vfdff wrote:

> It report error **Access tag metadata must have either 4 or 5 operands** with 
> CGM.getTBAATypeInfo(Context.IntTy)
> 
> ```
> p MDHelper.createTBAAScalarTypeNode("int", RootMD)
>   <0xbe1f62a8> = !{!"int", <0xbddc68f8>, i64 0}  -- return a Type 
> descriptors
> p CGF.CGM.getTBAATypeInfo(CGF.getContext().IntTy)
>   <0xbe0c93b8> = !{<0xbddfd208>, i64 4, !"int"}  --  Is it a new 
> path-tbaa?
> ```

Oh, it works fine when I delete the option **-Xclang -new-struct-path-tbaa**

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-24 Thread via cfe-commits

vfdff wrote:

It report error **Access tag metadata must have either 4 or 5 operands** with 
CGM.getTBAATypeInfo(Context.IntTy)
```
p MDHelper.createTBAAScalarTypeNode("int", RootMD)
  <0xbe1f62a8> = !{!"int", <0xbddc68f8>, i64 0}  -- return a Type 
descriptors
p CGF.CGM.getTBAATypeInfo(CGF.getContext().IntTy)
  <0xbe0c93b8> = !{<0xbddfd208>, i64 4, !"int"}  --  I don't  sure what 
is it ?
```


https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-24 Thread Eli Friedman via cfe-commits


@@ -707,7 +707,36 @@ static RValue emitLibraryCall(CodeGenFunction , 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;
+}();
+
+const llvm::Triple  = CGF.getTarget().getTriple();
+// Restrict to Linux because not all targets set errno, such as MacOS.

efriedma-quic wrote:

LangOpts.MathErrno

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-24 Thread Eli Friedman via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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 = CGF.CGM.getTBAARoot();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  MDNode *AliasType = MDHelper.createTBAANode("int", RootMD);

efriedma-quic wrote:

Looked it up; should be `CGM.getTBAATypeInfo(Context.IntTy)`?

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-24 Thread via cfe-commits

https://github.com/vfdff deleted https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-24 Thread via cfe-commits


@@ -707,7 +707,38 @@ static RValue emitLibraryCall(CodeGenFunction , 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");

vfdff wrote:

Fixed, thanks 

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-23 Thread via cfe-commits

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 
Date: Tue, 18 Jun 2024 09:21:07 -0400
Subject: [PATCH 1/3] [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 , 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(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 0..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 
+
+
+// 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 
Date: Thu, 20 Jun 2024 02:29:11 -0400
Subject: [PATCH 2/3] 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 , 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 

[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-22 Thread via cfe-commits

https://github.com/vfdff edited https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-22 Thread via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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 = CGF.CGM.getTBAARoot();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  MDNode *AliasType = MDHelper.createTBAANode("int", RootMD);

vfdff wrote:

sorry, I don't find the metadata for "int". 

ps:I will update the **createTBAANode** into **createTBAAScalarTypeNode** 
according [switch from old TBAA format to the new struct-path aware TBAA 
format](https://discourse.llvm.org/t/dragonegg-switch-from-old-tbaa-format-to-the-new-struct-path-aware-tbaa-format/29448)

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-21 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

I can't think of any obvious reason this would cause timeouts. I mean, you're 
adding metadata to a bunch of functions, and if something handles that metadata 
inefficiently, things could easily explode.  But nothing specific comes to mind.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-21 Thread Eli Friedman via cfe-commits


@@ -707,7 +707,34 @@ static RValue emitLibraryCall(CodeGenFunction , 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 = CGF.CGM.getTBAARoot();
+  // Emit "int" TBAA metadata on FP math libcalls.
+  MDNode *AliasType = MDHelper.createTBAANode("int", RootMD);

efriedma-quic wrote:

We should have code somewhere for getting metadata for "int"?

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-21 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

> > Can we restrict this to targets where libm actually modifies errno?
> 
> Do you mean it is better to have a function list, which actually modifies 
> `errno` ? For example, we should restrict to `__exp10`, but but not a general 
> top call `__exp `?

I said targets, not functions.  For example, MacOS never set errno for math 
functions.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-20 Thread via cfe-commits

vfdff wrote:

hi @efriedma-quic, I surprise to find there are 4 new failed tests with new 
commit 9990877a2 because **timeout**, but the change seems tiny, need some time 
to figure out the reason. If you have any suggestions, it's also very welcome.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-20 Thread via cfe-commits

https://github.com/vfdff edited https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-20 Thread via cfe-commits


@@ -707,7 +707,38 @@ static RValue emitLibraryCall(CodeGenFunction , 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");

vfdff wrote:

I don't find the way to call CodeGenTBAA::getRoot, so add a public interface 
**getTBAARoot**, thanks.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-20 Thread via cfe-commits

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 
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 , 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(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 0..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 
+
+
+// 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 
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 , 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 

[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-19 Thread via cfe-commits

vfdff wrote:

> Can we restrict this to targets where libm actually modifies errno?

Do you mean it is better to have a function list, which actually modifies 
`errno` ? For example, we should restrict to `__exp10`, but but not a general 
top call `__exp `?
https://github.com/lattera/glibc/blob/895ef79e04a953cac1493863bcae29ad85657ee1/math/w_exp10_template.c#L35

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-19 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic edited 
https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-19 Thread Eli Friedman via cfe-commits


@@ -707,7 +707,38 @@ static RValue emitLibraryCall(CodeGenFunction , 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");

efriedma-quic wrote:

Please don't copy-paste code from CodeGenTBAA.

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-19 Thread Eli Friedman via cfe-commits

https://github.com/efriedma-quic commented:

Ideally we could identify errno more precisely somehow, but I guess this is 
better than nothing.

Can we restrict this to targets where libm actually modifies errno?

How does this interact with StrictFP?

https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-18 Thread via cfe-commits

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 
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 , 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(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 0..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 
+
+
+// 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


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-18 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Allen (vfdff)


Changes

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

---
Full diff: https://github.com/llvm/llvm-project/pull/96025.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+32-1) 
- (added) clang/test/CodeGen/math-libcalls-tbaa.cpp (+22) 


``diff
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 , 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(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 0..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 
+
+
+// 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;
+}

``




https://github.com/llvm/llvm-project/pull/96025
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [TBAA] Emit int TBAA metadata on FP math libcalls (PR #96025)

2024-06-18 Thread via cfe-commits

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 
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 , 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(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 0..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 
+
+
+// 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