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" 23*480093f4SDimitry 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; 82*480093f4SDimitry Andric for (auto &I : instructions(F)) { 83*480093f4SDimitry Andric if (auto *II = dyn_cast<IntrinsicInst>(&I)) { 84*480093f4SDimitry Andric switch (II->getIntrinsicID()) { 85*480093f4SDimitry Andric default: break; 86*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fadd: 87*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_v2_fmul: 88*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_add: 89*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_mul: 90*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_and: 91*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_or: 92*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_xor: 93*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 94*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 95*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 96*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 97*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 98*480093f4SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: 99*480093f4SDimitry Andric if (TTI->shouldExpandReduction(II)) 1000b57cec5SDimitry Andric Worklist.push_back(II); 1010b57cec5SDimitry Andric 102*480093f4SDimitry Andric break; 103*480093f4SDimitry Andric } 104*480093f4SDimitry Andric } 105*480093f4SDimitry Andric } 1060b57cec5SDimitry Andric 107*480093f4SDimitry 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) { 118*480093f4SDimitry 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*480093f4SDimitry Andric if (!isPowerOf2_32(Vec->getType()->getVectorNumElements())) 129*480093f4SDimitry Andric continue; 130*480093f4SDimitry Andric 1310b57cec5SDimitry Andric Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), MRK); 1320b57cec5SDimitry Andric Rdx = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(ID), 1330b57cec5SDimitry Andric Acc, Rdx, "bin.rdx"); 1340b57cec5SDimitry Andric } 135*480093f4SDimitry Andric break; 136*480093f4SDimitry Andric } 1370b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_add: 1380b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_mul: 1390b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_and: 1400b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_or: 1410b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_xor: 1420b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smax: 1430b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_smin: 1440b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umax: 1450b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_umin: 1460b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmax: 1470b57cec5SDimitry Andric case Intrinsic::experimental_vector_reduce_fmin: { 1480b57cec5SDimitry Andric Value *Vec = II->getArgOperand(0); 149*480093f4SDimitry Andric if (!isPowerOf2_32(Vec->getType()->getVectorNumElements())) 1500b57cec5SDimitry Andric continue; 151*480093f4SDimitry Andric 152*480093f4SDimitry Andric Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), MRK); 153*480093f4SDimitry Andric break; 154*480093f4SDimitry Andric } 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric II->replaceAllUsesWith(Rdx); 1570b57cec5SDimitry Andric II->eraseFromParent(); 1580b57cec5SDimitry Andric Changed = true; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric return Changed; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric class ExpandReductions : public FunctionPass { 1640b57cec5SDimitry Andric public: 1650b57cec5SDimitry Andric static char ID; 1660b57cec5SDimitry Andric ExpandReductions() : FunctionPass(ID) { 1670b57cec5SDimitry Andric initializeExpandReductionsPass(*PassRegistry::getPassRegistry()); 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric bool runOnFunction(Function &F) override { 1710b57cec5SDimitry Andric const auto *TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 1720b57cec5SDimitry Andric return expandReductions(F, TTI); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1760b57cec5SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>(); 1770b57cec5SDimitry Andric AU.setPreservesCFG(); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric }; 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric char ExpandReductions::ID; 1830b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions", 1840b57cec5SDimitry Andric "Expand reduction intrinsics", false, false) 1850b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 1860b57cec5SDimitry Andric INITIALIZE_PASS_END(ExpandReductions, "expand-reductions", 1870b57cec5SDimitry Andric "Expand reduction intrinsics", false, false) 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric FunctionPass *llvm::createExpandReductionsPass() { 1900b57cec5SDimitry Andric return new ExpandReductions(); 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric PreservedAnalyses ExpandReductionsPass::run(Function &F, 1940b57cec5SDimitry Andric FunctionAnalysisManager &AM) { 1950b57cec5SDimitry Andric const auto &TTI = AM.getResult<TargetIRAnalysis>(F); 1960b57cec5SDimitry Andric if (!expandReductions(F, &TTI)) 1970b57cec5SDimitry Andric return PreservedAnalyses::all(); 1980b57cec5SDimitry Andric PreservedAnalyses PA; 1990b57cec5SDimitry Andric PA.preserveSet<CFGAnalyses>(); 2000b57cec5SDimitry Andric return PA; 2010b57cec5SDimitry Andric } 202