https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/133408
None >From d792bbb3cc68c459f254b4ab1f54c46800c750c6 Mon Sep 17 00:00:00 2001 From: Matt Arsenault <matthew.arsena...@amd.com> Date: Fri, 28 Mar 2025 16:26:47 +0700 Subject: [PATCH] llvm-reduce: Fix losing fast math flags when removing arguments --- .../remove-arguments-preserve-fmf.ll | 26 +++++++++++++++++++ .../llvm-reduce/deltas/ReduceArguments.cpp | 7 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll diff --git a/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll new file mode 100644 index 0000000000000..2ceb3e717200b --- /dev/null +++ b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll @@ -0,0 +1,26 @@ +; Check that when removing arguments, existing fast math flags are preserved + +; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=arguments --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t +; RUN: FileCheck --check-prefixes=RESULT %s < %t + +; INTERESTING-LABEL: @math_callee( +define float @math_callee(float %a, float %b) { + %add = fadd float %a, %b + ret float %add +} + +; INTERESTING-LABEL: @math_callee_decl( +declare float @math_callee_decl(float %a, float %b) + +; INTERESTING-LABEL: @math_caller( +; INTERESTING: call +; INTERESTING: call + +; RESULT: %call0 = call nnan nsz float @math_callee() +; RESULT: %call1 = call ninf float @math_callee_decl() +define float @math_caller(float %x) { + %call0 = call nnan nsz float @math_callee(float %x, float 2.0) + %call1 = call ninf float @math_callee_decl(float %x, float 2.0) + %result = fadd float %call0, %call1 + ret float %result +} diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp index ec2699a990311..aea4d1dc186c4 100644 --- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp @@ -16,6 +16,8 @@ #include "Utils.h" #include "llvm/ADT/SmallVector.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/FMF.h" +#include "llvm/IR/Operator.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" #include <set> @@ -59,7 +61,7 @@ static void replaceFunctionCalls(Function &OldF, Function &NewF, } } - // FIXME: Losing bundles, fast math flags and metadata + // FIXME: Losing bundles and metadata CallInst *NewCI = CallInst::Create(&NewF, Args); NewCI->setCallingConv(NewF.getCallingConv()); @@ -73,6 +75,9 @@ static void replaceFunctionCalls(Function &OldF, Function &NewF, ++ArgI, ++AttrIdx) NewCI->addParamAttrs(AttrIdx, ArgAttrs[AttrIdx]); + if (auto *FPOp = dyn_cast<FPMathOperator>(NewCI)) + cast<Instruction>(FPOp)->setFastMathFlags(CI->getFastMathFlags()); + if (!CI->use_empty()) CI->replaceAllUsesWith(NewCI); ReplaceInstWithInst(CI, NewCI); _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits