[clang] Don't emit int TBAA metadata on more complex FP math libcalls. (PR #107598)

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 11:46:16 PDT 2024


https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/107598

>From 0763f8d25194e18a040d4cd4cde7c88b6fccbb44 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Fri, 6 Sep 2024 08:01:25 -0700
Subject: [PATCH 1/5] Don't emit int TBAA metadata on more complex FP math
 libcalls.

---
 clang/lib/CodeGen/CGBuiltin.cpp               | 15 +++++++--
 .../test/CodeGen/complex-math-libcalls-tbaa.c | 31 +++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/complex-math-libcalls-tbaa.c

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index e4d169d2ad6030..a84c9ca4be8a72 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -699,9 +699,20 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
     bool ConstWithoutErrnoAndExceptions =
         Context.BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
     // Restrict to target with errno, for example, MacOS doesn't set errno.
-    // TODO: Support builtin function with complex type returned, eg: cacosh
+    bool CallWithPointerArgsOrPointerReturnType = false;
+    if (Call.isScalar()) {
+      if (CallBase *CB = dyn_cast<CallBase>(Call.getScalarVal())) {
+        for (Value *A : CB->args())
+          if (A->getType()->isPointerTy())
+            CallWithPointerArgsOrPointerReturnType = true;
+        CallWithPointerArgsOrPointerReturnType =
+          CallWithPointerArgsOrPointerReturnType ||
+          CB->getFunctionType()->getReturnType()->isPointerTy();
+      }
+    }
     if (ConstWithoutErrnoAndExceptions && CGF.CGM.getLangOpts().MathErrno &&
-        !CGF.Builder.getIsFPConstrained() && Call.isScalar()) {
+        !CGF.Builder.getIsFPConstrained() && Call.isScalar() &&
+        !CallWithPointerArgsOrPointerReturnType) {
       // Emit "int" TBAA metadata on FP math libcalls.
       clang::QualType IntTy = Context.IntTy;
       TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
diff --git a/clang/test/CodeGen/complex-math-libcalls-tbaa.c b/clang/test/CodeGen/complex-math-libcalls-tbaa.c
new file mode 100644
index 00000000000000..957dae6361b6f6
--- /dev/null
+++ b/clang/test/CodeGen/complex-math-libcalls-tbaa.c
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -O3  -fmath-errno -emit-llvm -triple x86_64-unknown-unknown -o - %s | FileCheck %s -check-prefixes=CHECK
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple x86_64-pc-win64 -o - %s | FileCheck %s -check-prefixes=CHECK
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple i686-unknown-unknown -o - %s | FileCheck %s -check-prefixes=CHECK
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple powerpc-unknown-unknown -o - %s | FileCheck %s -check-prefixes=CHECK-PPC
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple armv7-none-linux-gnueabi -o - %s | FileCheck %s -check-prefixes=CHECK-TBAA,TBAA
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple armv7-none-linux-gnueabihf -o - %s | FileCheck %s -check-prefixes=CHECK-ARM,TBAA
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple thumbv7k-apple-watchos2.0 -o - -target-abi aapcs16 %s | FileCheck %s -check-prefixes=CHECK-THUMB,TBAA
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple aarch64-unknown-unknown -o - %s | FileCheck %s -check-prefixes=CHECK-AARCH,TBAA
+// RUN: %clang_cc1 %s -O3 -fmath-errno -emit-llvm -triple spir -o - %s | FileCheck %s -check-prefixes=CHECK-SPIR
+
+_Complex long double foo() {
+  _Complex long double cld;
+  long double v2 = __builtin_cargl(cld);
+  _Complex long double tmp = v2 * cld;
+  return tmp;
+}
+// CHECK: tail call x86_fp80 @cargl(ptr noundef nonnull byval({ {{.*}}, {{.*}} })
+// CHECK-PPC: tail call ppc_fp128 @cargl(ptr noundef nonnull byval({ ppc_fp128, ppc_fp128 })
+// CHECK-TBAA: tail call double @cargl([2 x i64] noundef undef) #[[ATTR1:[0-9]+]], !tbaa [[TBAA3:![0-9]+]]
+
+// CHECK-ARM: tail call double @cargl({ double, double } noundef undef) #[[ATTR1:[0-9]+]], !tbaa [[TBAA3:![0-9]+]]
+
+// CHECK-THUMB: tail call double @cargl([2 x double] noundef undef) #[[ATTR1:[0-9]+]], !tbaa [[TBAA3:![0-9]+]]
+
+// CHECK-AARCH: tail call fp128 @cargl([2 x fp128] noundef alignstack(16) undef) #[[ATTR1:[0-9]+]], !tbaa [[TBAA3:![0-9]+]]
+
+// CHECK-SPIR: tail call spir_func double @cargl(ptr noundef nonnull byval({ {{.*}}, {{.*}} })
+
+// TBAA: [[TBAA3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
+// TBAA: [[META4]] = !{!"int", [[META5:![0-9]+]], i64 0}
+// TBAA: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}

>From 358407997bee2c218bfc3a7e11704fd3191b8abb Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Fri, 6 Sep 2024 08:19:17 -0700
Subject: [PATCH 2/5] Fix format.

---
 clang/lib/CodeGen/CGBuiltin.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index a84c9ca4be8a72..3b9d994041ecab 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -706,8 +706,8 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
           if (A->getType()->isPointerTy())
             CallWithPointerArgsOrPointerReturnType = true;
         CallWithPointerArgsOrPointerReturnType =
-          CallWithPointerArgsOrPointerReturnType ||
-          CB->getFunctionType()->getReturnType()->isPointerTy();
+            CallWithPointerArgsOrPointerReturnType ||
+            CB->getFunctionType()->getReturnType()->isPointerTy();
       }
     }
     if (ConstWithoutErrnoAndExceptions && CGF.CGM.getLangOpts().MathErrno &&

>From 72d939befeea47c609de5bfc3850c7376a6e5865 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Fri, 6 Sep 2024 11:55:30 -0700
Subject: [PATCH 3/5] Added fix to the failed LIT tests.

---
 clang/lib/CodeGen/CGBuiltin.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 3b9d994041ecab..ed8a977c8088ab 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -700,7 +700,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
         Context.BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
     // Restrict to target with errno, for example, MacOS doesn't set errno.
     bool CallWithPointerArgsOrPointerReturnType = false;
-    if (Call.isScalar()) {
+    if (Call.isScalar() && Call.getScalarVal()) {
       if (CallBase *CB = dyn_cast<CallBase>(Call.getScalarVal())) {
         for (Value *A : CB->args())
           if (A->getType()->isPointerTy())

>From 9ad690aa2406a8ee1e36bb20203e8d3ad165361f Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 12 Sep 2024 07:11:03 -0700
Subject: [PATCH 4/5] Addressed review comments.

---
 clang/lib/CodeGen/CGBuiltin.cpp | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index ed8a977c8088ab..24d53f32e72a1c 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -686,6 +686,20 @@ static Value *EmitSignBit(CodeGenFunction &CGF, Value *V) {
   return CGF.Builder.CreateICmpSLT(V, Zero);
 }
 
+static bool hasPointerArgsOrPointerReturnType(const Value *V) {
+  if (const CallBase *CB = dyn_cast<CallBase>(V)) {
+    for (const Value *A : CB->args()) {
+      if (A->getType()->isPointerTy()) {
+        return true;
+      }
+    }
+    if (CB->getFunctionType()->getReturnType()->isPointerTy()) {
+      return true;
+    }
+  }
+  return false;
+}
+
 static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
                               const CallExpr *E, llvm::Constant *calleeValue) {
   CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
@@ -699,17 +713,8 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
     bool ConstWithoutErrnoAndExceptions =
         Context.BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
     // Restrict to target with errno, for example, MacOS doesn't set errno.
-    bool CallWithPointerArgsOrPointerReturnType = false;
-    if (Call.isScalar() && Call.getScalarVal()) {
-      if (CallBase *CB = dyn_cast<CallBase>(Call.getScalarVal())) {
-        for (Value *A : CB->args())
-          if (A->getType()->isPointerTy())
-            CallWithPointerArgsOrPointerReturnType = true;
-        CallWithPointerArgsOrPointerReturnType =
-            CallWithPointerArgsOrPointerReturnType ||
-            CB->getFunctionType()->getReturnType()->isPointerTy();
-      }
-    }
+    bool CallWithPointerArgsOrPointerReturnType =
+        hasPointerArgsOrPointerReturnType(Call.getScalarVal());
     if (ConstWithoutErrnoAndExceptions && CGF.CGM.getLangOpts().MathErrno &&
         !CGF.Builder.getIsFPConstrained() && Call.isScalar() &&
         !CallWithPointerArgsOrPointerReturnType) {

>From 19bb4674def16828b02d762e1f5e603da0c9ffcb Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 12 Sep 2024 11:45:43 -0700
Subject: [PATCH 5/5] Added fix for the LIT failures.

---
 clang/lib/CodeGen/CGBuiltin.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 24d53f32e72a1c..59bb0208e67fe3 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -714,6 +714,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
         Context.BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
     // Restrict to target with errno, for example, MacOS doesn't set errno.
     bool CallWithPointerArgsOrPointerReturnType =
+        Call.isScalar() && Call.getScalarVal() &&
         hasPointerArgsOrPointerReturnType(Call.getScalarVal());
     if (ConstWithoutErrnoAndExceptions && CGF.CGM.getLangOpts().MathErrno &&
         !CGF.Builder.getIsFPConstrained() && Call.isScalar() &&



More information about the cfe-commits mailing list