1bdd1243dSDimitry Andric //===-- SPIRVBuiltins.h - SPIR-V Built-in Functions -------------*- C++ -*-===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric // 9bdd1243dSDimitry Andric // Lowering builtin function calls and types using their demangled names. 10bdd1243dSDimitry Andric // 11bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 12bdd1243dSDimitry Andric 13bdd1243dSDimitry Andric #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H 14bdd1243dSDimitry Andric #define LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H 15bdd1243dSDimitry Andric 16bdd1243dSDimitry Andric #include "SPIRVGlobalRegistry.h" 17bdd1243dSDimitry Andric #include "llvm/CodeGen/GlobalISel/CallLowering.h" 18bdd1243dSDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" 19bdd1243dSDimitry Andric 20bdd1243dSDimitry Andric namespace llvm { 21bdd1243dSDimitry Andric namespace SPIRV { 22*0fca6ea1SDimitry Andric /// Parses the name part of the demangled builtin call. 23*0fca6ea1SDimitry Andric std::string lookupBuiltinNameHelper(StringRef DemangledCall); 24*0fca6ea1SDimitry Andric /// Lowers a builtin function call using the provided \p DemangledCall skeleton 25bdd1243dSDimitry Andric /// and external instruction \p Set. 26bdd1243dSDimitry Andric /// 27bdd1243dSDimitry Andric /// \return the lowering success status if the called function is a recognized 28bdd1243dSDimitry Andric /// builtin, std::nullopt otherwise. 29bdd1243dSDimitry Andric /// 30bdd1243dSDimitry Andric /// \p DemangledCall is the skeleton of the lowered builtin function call. 31bdd1243dSDimitry Andric /// \p Set is the external instruction set containing the given builtin. 32bdd1243dSDimitry Andric /// \p OrigRet is the single original virtual return register if defined, 33bdd1243dSDimitry Andric /// Register(0) otherwise. 34bdd1243dSDimitry Andric /// \p OrigRetTy is the type of the \p OrigRet. 35bdd1243dSDimitry Andric /// \p Args are the arguments of the lowered builtin call. 36bdd1243dSDimitry Andric std::optional<bool> lowerBuiltin(const StringRef DemangledCall, 37bdd1243dSDimitry Andric InstructionSet::InstructionSet Set, 38bdd1243dSDimitry Andric MachineIRBuilder &MIRBuilder, 39bdd1243dSDimitry Andric const Register OrigRet, const Type *OrigRetTy, 40bdd1243dSDimitry Andric const SmallVectorImpl<Register> &Args, 41bdd1243dSDimitry Andric SPIRVGlobalRegistry *GR); 425f757f3fSDimitry Andric 43*0fca6ea1SDimitry Andric /// Helper function for finding a builtin function attributes 44*0fca6ea1SDimitry Andric /// by a demangled function name. Defined in SPIRVBuiltins.cpp. 45*0fca6ea1SDimitry Andric std::tuple<int, unsigned, unsigned> 46*0fca6ea1SDimitry Andric mapBuiltinToOpcode(const StringRef DemangledCall, 47*0fca6ea1SDimitry Andric SPIRV::InstructionSet::InstructionSet Set); 48*0fca6ea1SDimitry Andric 49*0fca6ea1SDimitry Andric /// Parses the provided \p ArgIdx argument base type in the \p DemangledCall 50*0fca6ea1SDimitry Andric /// skeleton. A base type is either a basic type (e.g. i32 for int), pointer 51*0fca6ea1SDimitry Andric /// element type (e.g. i8 for char*), or builtin type (TargetExtType). 52*0fca6ea1SDimitry Andric /// 53*0fca6ea1SDimitry Andric /// \return LLVM Type or nullptr if unrecognized 54*0fca6ea1SDimitry Andric /// 55*0fca6ea1SDimitry Andric /// \p DemangledCall is the skeleton of the lowered builtin function call. 56*0fca6ea1SDimitry Andric /// \p ArgIdx is the index of the argument to parse. 57*0fca6ea1SDimitry Andric Type *parseBuiltinCallArgumentBaseType(const StringRef DemangledCall, 58*0fca6ea1SDimitry Andric unsigned ArgIdx, LLVMContext &Ctx); 59*0fca6ea1SDimitry Andric 605f757f3fSDimitry Andric /// Translates a string representing a SPIR-V or OpenCL builtin type to a 615f757f3fSDimitry Andric /// TargetExtType that can be further lowered with lowerBuiltinType(). 625f757f3fSDimitry Andric /// 635f757f3fSDimitry Andric /// \return A TargetExtType representing the builtin SPIR-V type. 645f757f3fSDimitry Andric /// 655f757f3fSDimitry Andric /// \p TypeName is the full string representation of the SPIR-V or OpenCL 665f757f3fSDimitry Andric /// builtin type. 67*0fca6ea1SDimitry Andric TargetExtType *parseBuiltinTypeNameToTargetExtType(std::string TypeName, 68*0fca6ea1SDimitry Andric LLVMContext &Context); 695f757f3fSDimitry Andric 70bdd1243dSDimitry Andric /// Handles the translation of the provided special opaque/builtin type \p Type 71bdd1243dSDimitry Andric /// to SPIR-V type. Generates the corresponding machine instructions for the 72bdd1243dSDimitry Andric /// target type or gets the already existing OpType<...> register from the 73bdd1243dSDimitry Andric /// global registry \p GR. 74bdd1243dSDimitry Andric /// 75bdd1243dSDimitry Andric /// \return A machine instruction representing the OpType<...> SPIR-V type. 76bdd1243dSDimitry Andric /// 77bdd1243dSDimitry Andric /// \p Type is the special opaque/builtin type to be lowered. 7806c3fb27SDimitry Andric SPIRVType *lowerBuiltinType(const Type *Type, 79bdd1243dSDimitry Andric AccessQualifier::AccessQualifier AccessQual, 80bdd1243dSDimitry Andric MachineIRBuilder &MIRBuilder, 81bdd1243dSDimitry Andric SPIRVGlobalRegistry *GR); 82bdd1243dSDimitry Andric } // namespace SPIRV 83bdd1243dSDimitry Andric } // namespace llvm 84bdd1243dSDimitry Andric #endif // LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H 85