Index: llvm/lib/Transforms/IPO/OpenMPOpt.cpp =================================================================== --- llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -385,33 +385,36 @@ return nullptr; } - /// Helper to initialize all runtime function information for those defined in - /// OpenMPKinds.def. - void initializeRuntimeFunctions() { - // Helper to check the types in the declaration against the expected types. - auto CheckTypes = [&](RuntimeFunctionInfo &RFI) { - if (!RFI.Declaration) - return true; - - Function *F = RFI.Declaration; - if (F->getReturnType()->getTypeID() != RFI.ReturnType->getTypeID()) - return false; + /// Returns true if the function declaration \p F matches the runtime + /// function types, that is, return type \p RTFRetType, and arguments types + /// \p RTFArgTypes. If the runtime function is variadic, \p IsVarArg, the + /// comparison is made until the runtime function number of arguments. + static bool declMatchesRTFTypes(Function *F, Type *RTFRetType, bool IsVarArg, + SmallVector &RTFArgTypes) { + if (!F) + return true; + if (F->getReturnType() != RTFRetType) + return false; + if (F->arg_size() < RTFArgTypes.size()) + return false; + if (!IsVarArg && F->arg_size() > RTFArgTypes.size()) + return false; - if (F->arg_size() != RFI.getNumArgs()) + auto RTFTyIt = RTFArgTypes.begin(); + for (Argument &Arg : F->args()) { + if (Arg.getType() != *RTFTyIt) return false; - SmallVector::iterator RTFTyIt; - RTFTyIt = RFI.ArgumentTypes.begin(); - for (Argument &Arg : F->args()) { - if (Arg.getType()->getTypeID() != (*RTFTyIt)->getTypeID()) - return false; - - ++RTFTyIt; - } + if (IsVarArg && ++RTFTyIt == RTFArgTypes.end()) + break; + } - return true; - }; + return true; + } + /// Helper to initialize all runtime function information for those defined in + /// OpenMPKinds.def. + void initializeRuntimeFunctions() { // Helper to collect all uses of the decleration in the UsesMap. auto CollectUses = [&](RuntimeFunctionInfo &RFI) { unsigned NumUses = 0; @@ -439,24 +442,26 @@ #define OMP_RTL(_Enum, _Name, _IsVarArg, _ReturnType, ...) \ { \ - auto &RFI = RFIs[_Enum]; \ - RFI.Kind = _Enum; \ - RFI.Name = _Name; \ - RFI.IsVarArg = _IsVarArg; \ - RFI.ReturnType = _ReturnType; \ - RFI.ArgumentTypes = SmallVector({__VA_ARGS__}); \ - RFI.Declaration = M.getFunction(_Name); \ - bool TypesMatch = CheckTypes(RFI); \ - (void)TypesMatch; \ - unsigned NumUses = CollectUses(RFI); \ - (void)NumUses; \ - LLVM_DEBUG({ \ - dbgs() << TAG << RFI.Name << (RFI.Declaration ? "" : " not") \ - << " found\n"; \ - if (RFI.Declaration) \ - dbgs() << TAG << "-> got " << NumUses << " uses in " \ - << RFI.UsesMap.size() << " different functions.\n"; \ - }); \ + SmallVector ArgsTypes({__VA_ARGS__}); \ + Function *F = M.getFunction(_Name); \ + if (declMatchesRTFTypes(F, _ReturnType, _IsVarArg, ArgsTypes)) { \ + auto &RFI = RFIs[_Enum]; \ + RFI.Kind = _Enum; \ + RFI.Name = _Name; \ + RFI.IsVarArg = _IsVarArg; \ + RFI.ReturnType = _ReturnType; \ + RFI.ArgumentTypes = ArgsTypes; \ + RFI.Declaration = F; \ + unsigned NumUses = CollectUses(RFI); \ + (void)NumUses; \ + LLVM_DEBUG({ \ + dbgs() << TAG << RFI.Name << (RFI.Declaration ? "" : " not") \ + << " found\n"; \ + if (RFI.Declaration) \ + dbgs() << TAG << "-> got " << NumUses << " uses in " \ + << RFI.UsesMap.size() << " different functions.\n"; \ + }); \ + } \ } #include "llvm/Frontend/OpenMP/OMPKinds.def"