10b57cec5SDimitry Andric //===--- ExpandReductions.cpp - Expand experimental reduction intrinsics --===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass implements IR expansion for reduction intrinsics, allowing targets 100b57cec5SDimitry Andric // to enable the experimental intrinsics until just before codegen. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/CodeGen/ExpandReductions.h" 150b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 170b57cec5SDimitry Andric #include "llvm/IR/Function.h" 180b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 190b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h" 200b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 210b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 220b57cec5SDimitry Andric #include "llvm/IR/Module.h" 23480093f4SDimitry Andric #include "llvm/InitializePasses.h" 240b57cec5SDimitry Andric #include "llvm/Pass.h" 250b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h" 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric using namespace llvm; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric namespace { 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric unsigned getOpcode(Intrinsic::ID ID) { 320b57cec5SDimitry Andric switch (ID) { 330b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fadd: 340b57cec5SDimitry Andric return Instruction::FAdd; 350b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fmul: 360b57cec5SDimitry Andric return Instruction::FMul; 370b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_add: 380b57cec5SDimitry Andric return Instruction::Add; 390b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_mul: 400b57cec5SDimitry Andric return Instruction::Mul; 410b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_and: 420b57cec5SDimitry Andric return Instruction::And; 430b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_or: 440b57cec5SDimitry Andric return Instruction::Or; 450b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_xor: 460b57cec5SDimitry Andric return Instruction::Xor; 470b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 480b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 490b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 500b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 510b57cec5SDimitry Andric return Instruction::ICmp; 520b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 530b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: 540b57cec5SDimitry Andric return Instruction::FCmp; 550b57cec5SDimitry Andric default: 560b57cec5SDimitry Andric llvm_unreachable("Unexpected ID"); 570b57cec5SDimitry Andric } 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric RecurrenceDescriptor::MinMaxRecurrenceKind getMRK(Intrinsic::ID ID) { 610b57cec5SDimitry Andric switch (ID) { 620b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 630b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_SIntMax; 640b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 650b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_SIntMin; 660b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 670b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_UIntMax; 680b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 690b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_UIntMin; 700b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 710b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_FloatMax; 720b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: 730b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_FloatMin; 740b57cec5SDimitry Andric default: 750b57cec5SDimitry Andric return RecurrenceDescriptor::MRK_Invalid; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric bool expandReductions(Function &F, const TargetTransformInfo *TTI) { 800b57cec5SDimitry Andric bool Changed = false; 810b57cec5SDimitry Andric SmallVector<IntrinsicInst *, 4> Worklist; 82480093f4SDimitry Andric for (auto &I : instructions(F)) { 83480093f4SDimitry Andric if (auto *II = dyn_cast<IntrinsicInst>(&I)) { 84480093f4SDimitry Andric switch (II->getIntrinsicID()) { 85480093f4SDimitry Andric default: break; 86480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fadd: 87480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fmul: 88480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_add: 89480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_mul: 90480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_and: 91480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_or: 92480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_xor: 93480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 94480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 95480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 96480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 97480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 98480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: 99480093f4SDimitry Andric if (TTI->shouldExpandReduction(II)) 1000b57cec5SDimitry Andric Worklist.push_back(II); 1010b57cec5SDimitry Andric 102480093f4SDimitry Andric break; 103480093f4SDimitry Andric } 104480093f4SDimitry Andric } 105480093f4SDimitry Andric } 1060b57cec5SDimitry Andric 107480093f4SDimitry Andric for (auto *II : Worklist) { 1080b57cec5SDimitry Andric FastMathFlags FMF = 1090b57cec5SDimitry Andric isa<FPMathOperator>(II) ? II->getFastMathFlags() : FastMathFlags{}; 1100b57cec5SDimitry Andric Intrinsic::ID ID = II->getIntrinsicID(); 1110b57cec5SDimitry Andric RecurrenceDescriptor::MinMaxRecurrenceKind MRK = getMRK(ID); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric Value *Rdx = nullptr; 1140b57cec5SDimitry Andric IRBuilder<> Builder(II); 1150b57cec5SDimitry Andric IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); 1160b57cec5SDimitry Andric Builder.setFastMathFlags(FMF); 1170b57cec5SDimitry Andric switch (ID) { 118480093f4SDimitry Andric default: llvm_unreachable("Unexpected intrinsic!"); 1190b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fadd: 1200b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fmul: { 1210b57cec5SDimitry Andric // FMFs must be attached to the call, otherwise it's an ordered reduction 1220b57cec5SDimitry Andric // and it can't be handled by generating a shuffle sequence. 1230b57cec5SDimitry Andric Value *Acc = II->getArgOperand(0); 1240b57cec5SDimitry Andric Value *Vec = II->getArgOperand(1); 1250b57cec5SDimitry Andric if (!FMF.allowReassoc()) 1260b57cec5SDimitry Andric Rdx = getOrderedReduction(Builder, Acc, Vec, getOpcode(ID), MRK); 1270b57cec5SDimitry Andric else { 128*5ffd83dbSDimitry Andric if (!isPowerOf2_32( 129*5ffd83dbSDimitry Andric cast<FixedVectorType>(Vec->getType())->getNumElements())) 130480093f4SDimitry Andric continue; 131480093f4SDimitry Andric 1320b57cec5SDimitry Andric Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), MRK); 1330b57cec5SDimitry Andric Rdx = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(ID), 1340b57cec5SDimitry Andric Acc, Rdx, "bin.rdx"); 1350b57cec5SDimitry Andric } 136480093f4SDimitry Andric break; 137480093f4SDimitry Andric } 1380b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_add: 1390b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_mul: 1400b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_and: 1410b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_or: 1420b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_xor: 1430b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 1440b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 1450b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 1460b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 1470b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 1480b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: { 1490b57cec5SDimitry Andric Value *Vec = II->getArgOperand(0); 150*5ffd83dbSDimitry Andric if (!isPowerOf2_32( 151*5ffd83dbSDimitry Andric cast<FixedVectorType>(Vec->getType())->getNumElements())) 1520b57cec5SDimitry Andric continue; 153480093f4SDimitry Andric 154480093f4SDimitry Andric Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), MRK); 155480093f4SDimitry Andric break; 156480093f4SDimitry Andric } 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric II->replaceAllUsesWith(Rdx); 1590b57cec5SDimitry Andric II->eraseFromParent(); 1600b57cec5SDimitry Andric Changed = true; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric return Changed; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric class ExpandReductions : public FunctionPass { 1660b57cec5SDimitry Andric public: 1670b57cec5SDimitry Andric static char ID; 1680b57cec5SDimitry Andric ExpandReductions() : FunctionPass(ID) { 1690b57cec5SDimitry Andric initializeExpandReductionsPass(*PassRegistry::getPassRegistry()); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric bool runOnFunction(Function &F) override { 1730b57cec5SDimitry Andric const auto *TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 1740b57cec5SDimitry Andric return expandReductions(F, TTI); 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1780b57cec5SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 1790b57cec5SDimitry Andric AU.setPreservesCFG(); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric }; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric char ExpandReductions::ID; 1850b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions", 1860b57cec5SDimitry Andric "Expand reduction intrinsics", false, false) 1870b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 1880b57cec5SDimitry Andric INITIALIZE_PASS_END(ExpandReductions, "expand-reductions", 1890b57cec5SDimitry Andric "Expand reduction intrinsics", false, false) 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric FunctionPass *llvm::createExpandReductionsPass() { 1920b57cec5SDimitry Andric return new ExpandReductions(); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric PreservedAnalyses ExpandReductionsPass::run(Function &F, 1960b57cec5SDimitry Andric FunctionAnalysisManager &AM) { 1970b57cec5SDimitry Andric const auto &TTI = AM.getResult<TargetIRAnalysis>(F); 1980b57cec5SDimitry Andric if (!expandReductions(F, &TTI)) 1990b57cec5SDimitry Andric return PreservedAnalyses::all(); 2000b57cec5SDimitry Andric PreservedAnalyses PA; 2010b57cec5SDimitry Andric PA.preserveSet<CFGAnalyses>(); 2020b57cec5SDimitry Andric return PA; 2030b57cec5SDimitry Andric } 204