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

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 18 22:32:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Allen (vfdff)

<details>
<summary>Changes</summary>

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 &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;
+}

``````````

</details>


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


More information about the cfe-commits mailing list