| 
 | 1 | +//===-- FixABIMuxBuiltinsSYCLNativeCPU.cpp - Fixup mux ABI issues       ---===//  | 
 | 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 | +// Creates calls to shuffle up/down/xor mux builtins taking into account ABI of  | 
 | 10 | +// the SYCL functions. For now this only is used for vector variants.  | 
 | 11 | +//  | 
 | 12 | +//===----------------------------------------------------------------------===//  | 
 | 13 | + | 
 | 14 | +#include <llvm/IR/IRBuilder.h>  | 
 | 15 | +#include <llvm/IR/Module.h>  | 
 | 16 | +#include <llvm/IR/Type.h>  | 
 | 17 | +#include <llvm/SYCLLowerIR/FixABIMuxBuiltinsSYCLNativeCPU.h>  | 
 | 18 | + | 
 | 19 | +#define DEBUG_TYPE "fix-abi-mux-builtins"  | 
 | 20 | + | 
 | 21 | +using namespace llvm;  | 
 | 22 | + | 
 | 23 | +PreservedAnalyses FixABIMuxBuiltinsPass::run(Module &M,  | 
 | 24 | +                                             ModuleAnalysisManager &AM) {  | 
 | 25 | +  bool Changed = false;  | 
 | 26 | + | 
 | 27 | +  // Decide if a function needs updated and if so what parameters need changing,  | 
 | 28 | +  // as well as the return value  | 
 | 29 | +  auto FunctionNeedsFixing =  | 
 | 30 | +      [](Function &F,  | 
 | 31 | +         llvm::SmallVectorImpl<std::pair<unsigned int, llvm::Type *>> &Updates,  | 
 | 32 | +         llvm::Type *&RetVal, std::string &MuxFuncNameToCall) {  | 
 | 33 | +        if (!F.isDeclaration()) {  | 
 | 34 | +          return false;  | 
 | 35 | +        }  | 
 | 36 | +        if (!F.getName().contains("__spirv_SubgroupShuffle")) {  | 
 | 37 | +          return false;  | 
 | 38 | +        }  | 
 | 39 | +        Updates.clear();  | 
 | 40 | +        auto LIDvPos = F.getName().find("ELIDv");  | 
 | 41 | +        llvm::StringRef NameToMatch;  | 
 | 42 | +        if (LIDvPos != llvm::StringRef::npos) {  | 
 | 43 | +          // Add sizeof ELIDv to get num characters to match against  | 
 | 44 | +          NameToMatch = F.getName().take_front(LIDvPos + 5);  | 
 | 45 | +        } else {  | 
 | 46 | +          return false;  | 
 | 47 | +        }  | 
 | 48 | + | 
 | 49 | +        unsigned int StartIdx = 0;  | 
 | 50 | +        unsigned int EndIdx = 1;  | 
 | 51 | +        if (NameToMatch == "_Z32__spirv_SubgroupShuffleDownINTELIDv") {  | 
 | 52 | +          MuxFuncNameToCall = "__mux_sub_group_shuffle_down_";  | 
 | 53 | +        } else if (NameToMatch == "_Z30__spirv_SubgroupShuffleUpINTELIDv") {  | 
 | 54 | +          MuxFuncNameToCall = "__mux_sub_group_shuffle_up_";  | 
 | 55 | +        } else if (NameToMatch == "_Z28__spirv_SubgroupShuffleINTELIDv") {  | 
 | 56 | +          MuxFuncNameToCall = "__mux_sub_group_shuffle_";  | 
 | 57 | +          EndIdx = 0;  | 
 | 58 | +        } else if (NameToMatch == "_Z31__spirv_SubgroupShuffleXorINTELIDv") {  | 
 | 59 | +          MuxFuncNameToCall = "__mux_sub_group_shuffle_xor_";  | 
 | 60 | +          EndIdx = 0;  | 
 | 61 | +        } else {  | 
 | 62 | +          return false;  | 
 | 63 | +        }  | 
 | 64 | + | 
 | 65 | +        // We need to create the body for this. First we need to find out what  | 
 | 66 | +        // the first arguments should be  | 
 | 67 | +        llvm::StringRef RemainingName =  | 
 | 68 | +            F.getName().drop_front(NameToMatch.size());  | 
 | 69 | +        std::string MuxFuncTypeStr = "UNKNOWN";  | 
 | 70 | + | 
 | 71 | +        unsigned int VecWidth = 0;  | 
 | 72 | +        if (RemainingName.consumeInteger(10, VecWidth)) {  | 
 | 73 | +          return false;  | 
 | 74 | +        }  | 
 | 75 | +        if (!RemainingName.consume_front("_")) {  | 
 | 76 | +          return false;  | 
 | 77 | +        }  | 
 | 78 | + | 
 | 79 | +        char TypeCh = RemainingName[0];  | 
 | 80 | +        Type *BaseType = nullptr;  | 
 | 81 | +        switch (TypeCh) {  | 
 | 82 | +        case 'a':  | 
 | 83 | +        case 'h':  | 
 | 84 | +          BaseType = llvm::Type::getInt8Ty(F.getContext());  | 
 | 85 | +          MuxFuncTypeStr = "i8";  | 
 | 86 | +          break;  | 
 | 87 | +        case 's':  | 
 | 88 | +        case 't':  | 
 | 89 | +          BaseType = llvm::Type::getInt16Ty(F.getContext());  | 
 | 90 | +          MuxFuncTypeStr = "i16";  | 
 | 91 | +          break;  | 
 | 92 | + | 
 | 93 | +        case 'i':  | 
 | 94 | +        case 'j':  | 
 | 95 | +          BaseType = llvm::Type::getInt32Ty(F.getContext());  | 
 | 96 | +          MuxFuncTypeStr = "i32";  | 
 | 97 | +          break;  | 
 | 98 | +        case 'l':  | 
 | 99 | +        case 'm':  | 
 | 100 | +          BaseType = llvm::Type::getInt64Ty(F.getContext());  | 
 | 101 | +          MuxFuncTypeStr = "i64";  | 
 | 102 | +          break;  | 
 | 103 | +        case 'f':  | 
 | 104 | +          BaseType = llvm::Type::getFloatTy(F.getContext());  | 
 | 105 | +          MuxFuncTypeStr = "f32";  | 
 | 106 | +          break;  | 
 | 107 | +        case 'd':  | 
 | 108 | +          BaseType = llvm::Type::getDoubleTy(F.getContext());  | 
 | 109 | +          MuxFuncTypeStr = "f64";  | 
 | 110 | +          break;  | 
 | 111 | +        default:  | 
 | 112 | +          return false;  | 
 | 113 | +        }  | 
 | 114 | +        auto *VecType = llvm::FixedVectorType::get(BaseType, VecWidth);  | 
 | 115 | +        RetVal = VecType;  | 
 | 116 | + | 
 | 117 | +        // Work out the mux function to call's type extension based on v##N##Sfx  | 
 | 118 | +        MuxFuncNameToCall += "v";  | 
 | 119 | +        MuxFuncNameToCall += std::to_string(VecWidth);  | 
 | 120 | +        MuxFuncNameToCall += MuxFuncTypeStr;  | 
 | 121 | + | 
 | 122 | +        unsigned int CurrentIndex = 0;  | 
 | 123 | +        for (auto &Arg : F.args()) {  | 
 | 124 | +          if (Arg.hasStructRetAttr()) {  | 
 | 125 | +            StartIdx++;  | 
 | 126 | +            EndIdx++;  | 
 | 127 | +          } else {  | 
 | 128 | +            if (CurrentIndex >= StartIdx && CurrentIndex <= EndIdx) {  | 
 | 129 | +              if (Arg.getType() != VecType) {  | 
 | 130 | +                Updates.push_back(std::pair<unsigned int, llvm::Type *>(  | 
 | 131 | +                    CurrentIndex, VecType));  | 
 | 132 | +              }  | 
 | 133 | +            }  | 
 | 134 | +          }  | 
 | 135 | +          CurrentIndex++;  | 
 | 136 | +        }  | 
 | 137 | +        return true;  | 
 | 138 | +      };  | 
 | 139 | + | 
 | 140 | +  llvm::SmallVector<Function *, 4> FuncsToProcess;  | 
 | 141 | +  for (auto &F : M.functions()) {  | 
 | 142 | +    FuncsToProcess.push_back(&F);  | 
 | 143 | +  }  | 
 | 144 | + | 
 | 145 | +  for (auto *F : FuncsToProcess) {  | 
 | 146 | +    llvm::SmallVector<std::pair<unsigned int, llvm::Type *>, 4> ArgUpdates;  | 
 | 147 | +    llvm::Type *RetType = nullptr;  | 
 | 148 | +    std::string MuxFuncNameToCall;  | 
 | 149 | +    if (!FunctionNeedsFixing(*F, ArgUpdates, RetType, MuxFuncNameToCall)) {  | 
 | 150 | +      continue;  | 
 | 151 | +    }  | 
 | 152 | +    if (!F->isDeclaration()) {  | 
 | 153 | +      continue;  | 
 | 154 | +    }  | 
 | 155 | +    Changed = true;  | 
 | 156 | +    IRBuilder<> IR(BasicBlock::Create(F->getContext(), "", F));  | 
 | 157 | + | 
 | 158 | +    llvm::SmallVector<Type *, 8> Args;  | 
 | 159 | +    unsigned int ArgIndex = 0;  | 
 | 160 | +    unsigned int UpdateIndex = 0;  | 
 | 161 | + | 
 | 162 | +    for (auto &Arg : F->args()) {  | 
 | 163 | +      if (!Arg.hasStructRetAttr()) {  | 
 | 164 | +        if (UpdateIndex < ArgUpdates.size() &&  | 
 | 165 | +            std::get<0>(ArgUpdates[UpdateIndex]) == ArgIndex) {  | 
 | 166 | +          Args.push_back(std::get<1>(ArgUpdates[UpdateIndex]));  | 
 | 167 | +          UpdateIndex++;  | 
 | 168 | +        } else {  | 
 | 169 | +          Args.push_back(Arg.getType());  | 
 | 170 | +        }  | 
 | 171 | +      }  | 
 | 172 | +      ArgIndex++;  | 
 | 173 | +    }  | 
 | 174 | + | 
 | 175 | +    FunctionType *FT = FunctionType::get(RetType, Args, false);  | 
 | 176 | +    Function *NewFunc =  | 
 | 177 | +        Function::Create(FT, F->getLinkage(), MuxFuncNameToCall, M);  | 
 | 178 | +    llvm::SmallVector<Value *, 8> CallArgs;  | 
 | 179 | +    auto NewFuncArgItr = NewFunc->args().begin();  | 
 | 180 | +    Argument *SretPtr = nullptr;  | 
 | 181 | +    for (auto &Arg : F->args()) {  | 
 | 182 | +      if (Arg.hasStructRetAttr()) {  | 
 | 183 | +        SretPtr = &Arg;  | 
 | 184 | +      } else {  | 
 | 185 | +        if (Arg.getType() != (*NewFuncArgItr).getType()) {  | 
 | 186 | +          if (Arg.getType()->isPointerTy()) {  | 
 | 187 | +            Value *ArgLoad = IR.CreateLoad((*NewFuncArgItr).getType(), &Arg);  | 
 | 188 | +            CallArgs.push_back(ArgLoad);  | 
 | 189 | +          } else {  | 
 | 190 | +            Value *ArgCast = IR.CreateBitCast(&Arg, (*NewFuncArgItr).getType());  | 
 | 191 | +            CallArgs.push_back(ArgCast);  | 
 | 192 | +          }  | 
 | 193 | +        } else {  | 
 | 194 | +          CallArgs.push_back(&Arg);  | 
 | 195 | +        }  | 
 | 196 | +        NewFuncArgItr++;  | 
 | 197 | +      }  | 
 | 198 | +    }  | 
 | 199 | + | 
 | 200 | +    Value *Res = IR.CreateCall(NewFunc, CallArgs);  | 
 | 201 | +    // If the return type is different to the initial function, then bitcast it  | 
 | 202 | +    // unless it's void in which case we'd expect an StructRet parameter which  | 
 | 203 | +    // needs stored to.  | 
 | 204 | +    if (F->getReturnType() != RetType) {  | 
 | 205 | +      if (F->getReturnType()->isVoidTy()) {  | 
 | 206 | +        // If we don't have an StructRet parameter then something is wrong with  | 
 | 207 | +        // the initial function  | 
 | 208 | +        if (!SretPtr) {  | 
 | 209 | +          llvm_unreachable(  | 
 | 210 | +              "No struct ret pointer for Sub group shuffle function");  | 
 | 211 | +        }  | 
 | 212 | + | 
 | 213 | +        IR.CreateStore(Res, SretPtr);  | 
 | 214 | +      } else {  | 
 | 215 | +        Res = IR.CreateBitCast(Res, F->getReturnType());  | 
 | 216 | +      }  | 
 | 217 | +    }  | 
 | 218 | +    if (F->getReturnType()->isVoidTy()) {  | 
 | 219 | +      IR.CreateRetVoid();  | 
 | 220 | +    } else {  | 
 | 221 | +      IR.CreateRet(Res);  | 
 | 222 | +    }  | 
 | 223 | +  }  | 
 | 224 | + | 
 | 225 | +  return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();  | 
 | 226 | +}  | 
0 commit comments