xref: /llvm-project/llvm/lib/Target/SPIRV/SPIRVBuiltins.h (revision 3ed2a81358e11a582eb5cc3edf711447767036e6)
1f61eb416SIlia Diachkov //===-- SPIRVBuiltins.h - SPIR-V Built-in Functions -------------*- C++ -*-===//
2f61eb416SIlia Diachkov //
3f61eb416SIlia Diachkov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f61eb416SIlia Diachkov // See https://llvm.org/LICENSE.txt for license information.
5f61eb416SIlia Diachkov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f61eb416SIlia Diachkov //
7f61eb416SIlia Diachkov //===----------------------------------------------------------------------===//
8f61eb416SIlia Diachkov //
9f61eb416SIlia Diachkov // Lowering builtin function calls and types using their demangled names.
10f61eb416SIlia Diachkov //
11f61eb416SIlia Diachkov //===----------------------------------------------------------------------===//
12f61eb416SIlia Diachkov 
13f61eb416SIlia Diachkov #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
14f61eb416SIlia Diachkov #define LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
15f61eb416SIlia Diachkov 
16f61eb416SIlia Diachkov #include "SPIRVGlobalRegistry.h"
17f61eb416SIlia Diachkov #include "llvm/CodeGen/GlobalISel/CallLowering.h"
18f61eb416SIlia Diachkov #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
19f61eb416SIlia Diachkov 
20f61eb416SIlia Diachkov namespace llvm {
21f61eb416SIlia Diachkov namespace SPIRV {
22dbd00a59SVyacheslav Levytskyy /// Parses the name part of the demangled builtin call.
23978de2d6SVyacheslav Levytskyy std::string lookupBuiltinNameHelper(StringRef DemangledCall,
24*3ed2a813SVyacheslav Levytskyy                                     FPDecorationId *DecorationId = nullptr);
25bf9e9e5eSVyacheslav Levytskyy /// Lowers a builtin function call using the provided \p DemangledCall skeleton
26f61eb416SIlia Diachkov /// and external instruction \p Set.
27f61eb416SIlia Diachkov ///
281fbc6b26SAleksandr Bezzubikov /// \return the lowering success status if the called function is a recognized
298a7cbea5SKazu Hirata /// builtin, std::nullopt otherwise.
30f61eb416SIlia Diachkov ///
31f61eb416SIlia Diachkov /// \p DemangledCall is the skeleton of the lowered builtin function call.
32f61eb416SIlia Diachkov /// \p Set is the external instruction set containing the given builtin.
33f61eb416SIlia Diachkov /// \p OrigRet is the single original virtual return register if defined,
34698c8001SIlia Diachkov /// Register(0) otherwise.
35698c8001SIlia Diachkov /// \p OrigRetTy is the type of the \p OrigRet.
36698c8001SIlia Diachkov /// \p Args are the arguments of the lowered builtin call.
37b0df7040SFangrui Song std::optional<bool> lowerBuiltin(const StringRef DemangledCall,
381fbc6b26SAleksandr Bezzubikov                                  InstructionSet::InstructionSet Set,
391fbc6b26SAleksandr Bezzubikov                                  MachineIRBuilder &MIRBuilder,
401fbc6b26SAleksandr Bezzubikov                                  const Register OrigRet, const Type *OrigRetTy,
411fbc6b26SAleksandr Bezzubikov                                  const SmallVectorImpl<Register> &Args,
42f61eb416SIlia Diachkov                                  SPIRVGlobalRegistry *GR);
4381751905SMichal Paszkowski 
44bf9e9e5eSVyacheslav Levytskyy /// Helper function for finding a builtin function attributes
45bf9e9e5eSVyacheslav Levytskyy /// by a demangled function name. Defined in SPIRVBuiltins.cpp.
46bf9e9e5eSVyacheslav Levytskyy std::tuple<int, unsigned, unsigned>
47bf9e9e5eSVyacheslav Levytskyy mapBuiltinToOpcode(const StringRef DemangledCall,
48bf9e9e5eSVyacheslav Levytskyy                    SPIRV::InstructionSet::InstructionSet Set);
49bf9e9e5eSVyacheslav Levytskyy 
5043222bd3SMichal Paszkowski /// Parses the provided \p ArgIdx argument base type in the \p DemangledCall
5143222bd3SMichal Paszkowski /// skeleton. A base type is either a basic type (e.g. i32 for int), pointer
5243222bd3SMichal Paszkowski /// element type (e.g. i8 for char*), or builtin type (TargetExtType).
5343222bd3SMichal Paszkowski ///
5443222bd3SMichal Paszkowski /// \return LLVM Type or nullptr if unrecognized
5543222bd3SMichal Paszkowski ///
5643222bd3SMichal Paszkowski /// \p DemangledCall is the skeleton of the lowered builtin function call.
5743222bd3SMichal Paszkowski /// \p ArgIdx is the index of the argument to parse.
5843222bd3SMichal Paszkowski Type *parseBuiltinCallArgumentBaseType(const StringRef DemangledCall,
5943222bd3SMichal Paszkowski                                        unsigned ArgIdx, LLVMContext &Ctx);
60489db653SVyacheslav Levytskyy bool parseBuiltinTypeStr(SmallVector<StringRef, 10> &BuiltinArgsTypeStrs,
61489db653SVyacheslav Levytskyy                          const StringRef DemangledCall, LLVMContext &Ctx);
62489db653SVyacheslav Levytskyy Type *parseBuiltinCallArgumentType(StringRef TypeStr, LLVMContext &Ctx);
6343222bd3SMichal Paszkowski 
6481751905SMichal Paszkowski /// Translates a string representing a SPIR-V or OpenCL builtin type to a
6581751905SMichal Paszkowski /// TargetExtType that can be further lowered with lowerBuiltinType().
6681751905SMichal Paszkowski ///
6781751905SMichal Paszkowski /// \return A TargetExtType representing the builtin SPIR-V type.
6881751905SMichal Paszkowski ///
6981751905SMichal Paszkowski /// \p TypeName is the full string representation of the SPIR-V or OpenCL
7081751905SMichal Paszkowski /// builtin type.
7143222bd3SMichal Paszkowski TargetExtType *parseBuiltinTypeNameToTargetExtType(std::string TypeName,
7243222bd3SMichal Paszkowski                                                    LLVMContext &Context);
7381751905SMichal Paszkowski 
74698c8001SIlia Diachkov /// Handles the translation of the provided special opaque/builtin type \p Type
75698c8001SIlia Diachkov /// to SPIR-V type. Generates the corresponding machine instructions for the
76698c8001SIlia Diachkov /// target type or gets the already existing OpType<...> register from the
77698c8001SIlia Diachkov /// global registry \p GR.
78698c8001SIlia Diachkov ///
79698c8001SIlia Diachkov /// \return A machine instruction representing the OpType<...> SPIR-V type.
80698c8001SIlia Diachkov ///
81698c8001SIlia Diachkov /// \p Type is the special opaque/builtin type to be lowered.
825ac69674SMichal Paszkowski SPIRVType *lowerBuiltinType(const Type *Type,
83698c8001SIlia Diachkov                             AccessQualifier::AccessQualifier AccessQual,
84698c8001SIlia Diachkov                             MachineIRBuilder &MIRBuilder,
85698c8001SIlia Diachkov                             SPIRVGlobalRegistry *GR);
86f61eb416SIlia Diachkov } // namespace SPIRV
87f61eb416SIlia Diachkov } // namespace llvm
88f61eb416SIlia Diachkov #endif // LLVM_LIB_TARGET_SPIRV_SPIRVBUILTINS_H
89