|
| 1 | +//===------ NVPTXFoldFMA.cpp - Fold FMA --------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements FMA folding for float/double type for NVPTX. It folds |
| 10 | +// following patterns: |
| 11 | +// 1. fadd(fmul(a, b), c) => fma(a, b, c) |
| 12 | +// 2. fadd(c, fmul(a, b)) => fma(a, b, c) |
| 13 | +// 3. fadd(fmul(a, b), fmul(c, d)) => fma(a, b, fmul(c, d)) |
| 14 | +// 4. fsub(fmul(a, b), c) => fma(a, b, fneg(c)) |
| 15 | +// 5. fsub(a, fmul(b, c)) => fma(fneg(b), c, a) |
| 16 | +// 6. fsub(fmul(a, b), fmul(c, d)) => fma(a, b, fneg(fmul(c, d))) |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#include "NVPTXUtilities.h" |
| 20 | +#include "llvm/IR/IRBuilder.h" |
| 21 | +#include "llvm/IR/InstIterator.h" |
| 22 | +#include "llvm/IR/Instructions.h" |
| 23 | +#include "llvm/IR/Intrinsics.h" |
| 24 | + |
| 25 | +#define DEBUG_TYPE "nvptx-fold-fma" |
| 26 | + |
| 27 | +using namespace llvm; |
| 28 | + |
| 29 | +static bool foldFMA(Function &F) { |
| 30 | + bool Changed = false; |
| 31 | + SmallVector<BinaryOperator *, 16> FAddFSubInsts; |
| 32 | + |
| 33 | + // Collect all float/double FAdd/FSub instructions with allow-contract |
| 34 | + for (auto &I : instructions(F)) { |
| 35 | + if (auto *BI = dyn_cast<BinaryOperator>(&I)) { |
| 36 | + // Only FAdd and FSub are supported. |
| 37 | + if (BI->getOpcode() != Instruction::FAdd && |
| 38 | + BI->getOpcode() != Instruction::FSub) |
| 39 | + continue; |
| 40 | + |
| 41 | + // At minimum, the instruction should have allow-contract. |
| 42 | + if (!BI->hasAllowContract()) |
| 43 | + continue; |
| 44 | + |
| 45 | + // Only float and double are supported. |
| 46 | + if (!BI->getType()->isFloatTy() && !BI->getType()->isDoubleTy()) |
| 47 | + continue; |
| 48 | + |
| 49 | + FAddFSubInsts.push_back(BI); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + auto tryFoldBinaryFMul = [](BinaryOperator *BI, Value *MulOperand, |
| 54 | + Value *OtherOperand, bool IsFirstOperand, |
| 55 | + bool IsFSub) -> bool { |
| 56 | + auto *FMul = dyn_cast<BinaryOperator>(MulOperand); |
| 57 | + if (!FMul || FMul->getOpcode() != Instruction::FMul || !FMul->hasOneUse() || |
| 58 | + !FMul->hasAllowContract()) |
| 59 | + return false; |
| 60 | + |
| 61 | + LLVM_DEBUG({ |
| 62 | + const char *OpName = IsFSub ? "FSub" : "FAdd"; |
| 63 | + dbgs() << "Found " << OpName << " with FMul (single use) as " |
| 64 | + << (IsFirstOperand ? "first" : "second") << " operand: " << *BI |
| 65 | + << "\n"; |
| 66 | + }); |
| 67 | + |
| 68 | + Value *MulOp0 = FMul->getOperand(0); |
| 69 | + Value *MulOp1 = FMul->getOperand(1); |
| 70 | + IRBuilder<> Builder(BI); |
| 71 | + Value *FMA = nullptr; |
| 72 | + |
| 73 | + if (!IsFSub) { |
| 74 | + // fadd(fmul(a, b), c) => fma(a, b, c) |
| 75 | + // fadd(c, fmul(a, b)) => fma(a, b, c) |
| 76 | + FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()}, |
| 77 | + {MulOp0, MulOp1, OtherOperand}); |
| 78 | + } else { |
| 79 | + if (IsFirstOperand) { |
| 80 | + // fsub(fmul(a, b), c) => fma(a, b, fneg(c)) |
| 81 | + Value *NegOtherOp = Builder.CreateFNeg(OtherOperand); |
| 82 | + cast<Instruction>(NegOtherOp)->setFastMathFlags(BI->getFastMathFlags()); |
| 83 | + FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()}, |
| 84 | + {MulOp0, MulOp1, NegOtherOp}); |
| 85 | + } else { |
| 86 | + // fsub(a, fmul(b, c)) => fma(fneg(b), c, a) |
| 87 | + Value *NegMulOp0 = Builder.CreateFNeg(MulOp0); |
| 88 | + cast<Instruction>(NegMulOp0)->setFastMathFlags( |
| 89 | + FMul->getFastMathFlags()); |
| 90 | + FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()}, |
| 91 | + {NegMulOp0, MulOp1, OtherOperand}); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Combine fast-math flags from the original instructions |
| 96 | + auto *FMAInst = cast<Instruction>(FMA); |
| 97 | + FastMathFlags BinaryFMF = BI->getFastMathFlags(); |
| 98 | + FastMathFlags FMulFMF = FMul->getFastMathFlags(); |
| 99 | + FastMathFlags NewFMF = FastMathFlags::intersectRewrite(BinaryFMF, FMulFMF) | |
| 100 | + FastMathFlags::unionValue(BinaryFMF, FMulFMF); |
| 101 | + FMAInst->setFastMathFlags(NewFMF); |
| 102 | + |
| 103 | + LLVM_DEBUG({ |
| 104 | + const char *OpName = IsFSub ? "FSub" : "FAdd"; |
| 105 | + dbgs() << "Replacing " << OpName << " with FMA: " << *FMA << "\n"; |
| 106 | + }); |
| 107 | + BI->replaceAllUsesWith(FMA); |
| 108 | + BI->eraseFromParent(); |
| 109 | + FMul->eraseFromParent(); |
| 110 | + return true; |
| 111 | + }; |
| 112 | + |
| 113 | + for (auto *BI : FAddFSubInsts) { |
| 114 | + Value *Op0 = BI->getOperand(0); |
| 115 | + Value *Op1 = BI->getOperand(1); |
| 116 | + bool IsFSub = BI->getOpcode() == Instruction::FSub; |
| 117 | + |
| 118 | + if (tryFoldBinaryFMul(BI, Op0, Op1, true /*IsFirstOperand*/, IsFSub) || |
| 119 | + tryFoldBinaryFMul(BI, Op1, Op0, false /*IsFirstOperand*/, IsFSub)) |
| 120 | + Changed = true; |
| 121 | + } |
| 122 | + |
| 123 | + return Changed; |
| 124 | +} |
| 125 | + |
| 126 | +namespace { |
| 127 | + |
| 128 | +struct NVPTXFoldFMA : public FunctionPass { |
| 129 | + static char ID; |
| 130 | + NVPTXFoldFMA() : FunctionPass(ID) {} |
| 131 | + bool runOnFunction(Function &F) override; |
| 132 | +}; |
| 133 | + |
| 134 | +} // namespace |
| 135 | + |
| 136 | +char NVPTXFoldFMA::ID = 0; |
| 137 | +INITIALIZE_PASS(NVPTXFoldFMA, "nvptx-fold-fma", "NVPTX Fold FMA", false, false) |
| 138 | + |
| 139 | +bool NVPTXFoldFMA::runOnFunction(Function &F) { return foldFMA(F); } |
| 140 | + |
| 141 | +FunctionPass *llvm::createNVPTXFoldFMAPass() { return new NVPTXFoldFMA(); } |
| 142 | + |
| 143 | +PreservedAnalyses NVPTXFoldFMAPass::run(Function &F, |
| 144 | + FunctionAnalysisManager &) { |
| 145 | + return foldFMA(F) ? PreservedAnalyses::none() : PreservedAnalyses::all(); |
| 146 | +} |
0 commit comments