[clang-tools-extra] [lldb] [libc] [llvm] [clang] [libcxxabi] [libunwind] [libcxx] [flang] [lld] [compiler-rt] Fix a bug in Smith's algorithm used in complex div/mul. (PR #78330)

2024-01-18 Thread Zahira Ammarguellat via cfe-commits

zahiraam wrote:

> It's bad that we don't have a IR test that breaks when you change the IR 
> output like this. Please add one.

Done.

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


[clang-tools-extra] [lldb] [libc] [llvm] [clang] [libcxxabi] [libunwind] [libcxx] [flang] [lld] [compiler-rt] Fix a bug in Smith's algorithm used in complex div/mul. (PR #78330)

2024-01-18 Thread Zahira Ammarguellat via cfe-commits


@@ -2780,24 +2781,24 @@ static void RenderFloatingPointOptions(const ToolChain 
, const Driver ,
 case options::OPT_fcx_limited_range: {
   EmitComplexRangeDiag(D, Range, 
LangOptions::ComplexRangeKind::CX_Limited);
   Range = LangOptions::ComplexRangeKind::CX_Limited;
-  std::string ComplexRangeStr = RenderComplexRangeOption("limited");
-  if (!ComplexRangeStr.empty())
-CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr));
+  ComplexRangeStr = RenderComplexRangeOption("limited");

zahiraam wrote:

Done.

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


[clang-tools-extra] [lldb] [libc] [llvm] [clang] [libcxxabi] [libunwind] [libcxx] [flang] [lld] [compiler-rt] Fix a bug in Smith's algorithm used in complex div/mul. (PR #78330)

2024-01-18 Thread Simon Pilgrim via cfe-commits

https://github.com/RKSimon updated 
https://github.com/llvm/llvm-project/pull/78330

>From 8f8917528e30d2ba67f669cfd1a893bc85c21121 Mon Sep 17 00:00:00 2001
From: Ammarguellat 
Date: Tue, 16 Jan 2024 11:24:03 -0800
Subject: [PATCH 1/4] Fixed a bug in Smith's algorithm and made sure last
 option in command line rules.

---
 clang/lib/CodeGen/CGExprComplex.cpp   |  8 ++--
 clang/lib/Driver/ToolChains/Clang.cpp | 19 ++-
 clang/test/CodeGen/cx-complex-range.c | 16 
 clang/test/Driver/range.c |  7 +++
 4 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index e532794b71bdb4a..6fbd8f19eeb50a4 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -936,7 +936,7 @@ ComplexPairTy 
ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr,
   llvm::Value *RC = Builder.CreateFMul(CdD, RHSr);  // rc
   llvm::Value *DpRC = Builder.CreateFAdd(RHSi, RC); // tmp=d+rc
 
-  llvm::Value *T7 = Builder.CreateFMul(LHSr, RC);// ar
+  llvm::Value *T7 = Builder.CreateFMul(LHSr, CdD);   // ar
   llvm::Value *T8 = Builder.CreateFAdd(T7, LHSi);// ar+b
   llvm::Value *DSTFr = Builder.CreateFDiv(T8, DpRC); // (ar+b)/tmp
 
@@ -978,7 +978,11 @@ ComplexPairTy ComplexExprEmitter::EmitBinDiv(const 
BinOpInfo ) {
   return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi);
 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited)
   return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
-else if (!CGF.getLangOpts().FastMath) {
+else if (!CGF.getLangOpts().FastMath ||
+ // '-ffast-math' is used in the command line but followed by an
+ // '-fno-cx-limited-range'.
+ (CGF.getLangOpts().FastMath &&
+  Op.FPFeatures.getComplexRange() == LangOptions::CX_Full)) {
   LHSi = OrigLHSi;
   // If we have a complex operand on the RHS and FastMath is not allowed, 
we
   // delegate to a libcall to handle all of the complexities and minimize
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 9edae3fec91a87f..0c05d1db3960718 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2753,6 +2753,7 @@ static void RenderFloatingPointOptions(const ToolChain 
, const Driver ,
   StringRef Float16ExcessPrecision = "";
   StringRef BFloat16ExcessPrecision = "";
   LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_Full;
+  std::string ComplexRangeStr = "";
 
   if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
 CmdArgs.push_back("-mlimit-float-precision");
@@ -2768,24 +2769,24 @@ static void RenderFloatingPointOptions(const ToolChain 
, const Driver ,
 case options::OPT_fcx_limited_range: {
   EmitComplexRangeDiag(D, Range, 
LangOptions::ComplexRangeKind::CX_Limited);
   Range = LangOptions::ComplexRangeKind::CX_Limited;
-  std::string ComplexRangeStr = RenderComplexRangeOption("limited");
-  if (!ComplexRangeStr.empty())
-CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr));
+  ComplexRangeStr = RenderComplexRangeOption("limited");
   break;
 }
 case options::OPT_fno_cx_limited_range:
+  EmitComplexRangeDiag(D, Range, LangOptions::ComplexRangeKind::CX_Full);
   Range = LangOptions::ComplexRangeKind::CX_Full;
+  ComplexRangeStr = RenderComplexRangeOption("full");
   break;
 case options::OPT_fcx_fortran_rules: {
   EmitComplexRangeDiag(D, Range, 
LangOptions::ComplexRangeKind::CX_Fortran);
   Range = LangOptions::ComplexRangeKind::CX_Fortran;
-  std::string ComplexRangeStr = RenderComplexRangeOption("fortran");
-  if (!ComplexRangeStr.empty())
-CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr));
+  ComplexRangeStr = RenderComplexRangeOption("fortran");
   break;
 }
 case options::OPT_fno_cx_fortran_rules:
+  EmitComplexRangeDiag(D, Range, LangOptions::ComplexRangeKind::CX_Full);
   Range = LangOptions::ComplexRangeKind::CX_Full;
+  ComplexRangeStr = RenderComplexRangeOption("full");
   break;
 case options::OPT_ffp_model_EQ: {
   // If -ffp-model= is seen, reset to fno-fast-math
@@ -3056,9 +3057,7 @@ static void RenderFloatingPointOptions(const ToolChain 
, const Driver ,
   SeenUnsafeMathModeOption = true;
   // ffast-math enables fortran rules for complex multiplication and
   // division.
-  std::string ComplexRangeStr = RenderComplexRangeOption("limited");
-  if (!ComplexRangeStr.empty())
-CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr));
+  ComplexRangeStr = RenderComplexRangeOption("limited");
   break;
 }
 case options::OPT_fno_fast_math:
@@ -3215,6 +3214,8 @@ static void RenderFloatingPointOptions(const ToolChain 
, const Driver ,