OpenVDB  12.1.0
Public Types | Public Member Functions | Static Protected Member Functions | List of all members
CFunction< SignatureT > Struct Template Reference

Represents a concrete C function binding. More...

#include <openvdb_ax/codegen/FunctionTypes.h>

Inherits CFunctionBase.

Inherited by SRetFunction< SignatureT, CFunction< SignatureT > >.

Public Types

using CFunctionT = CFunction< SignatureT >
 
using Ptr = std::shared_ptr< CFunctionT >
 
using Traits = FunctionTraits< SignatureT >
 
enum  SignatureMatch {
  None = 0, Size, Implicit, Ambiguous,
  Explicit
}
 The result type from calls to Function::match. More...
 

Public Member Functions

 CFunction (const std::string &symbol, SignatureT *function)
 
 ~CFunction () override=default
 
ArgInfo types (ArgInfoVector &types, llvm::LLVMContext &C) const override
 Populate a vector of ArgInfos which describe this function signature. This method is used by Function::create, Function::print and Function::match. More...
 
uint64_t address () const override final
 Returns the global address of this function. More...
 
llvm::Value * call (const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B) const override
 
Value call (const Arguments &args, llvm::IRBuilder<> &B) const override
 
llvm::Constant * fold (const std::vector< llvm::Value * > &args, llvm::LLVMContext &C) const override final
 
llvm::Type * types (std::vector< llvm::Type * > &types, llvm::LLVMContext &C) const override
 
llvm::Value * call (const std::vector< llvm::Value * > &args, llvm::IRBuilder<> &B, const bool cast) const override
 
void setConstantFold (bool on)
 
bool hasConstantFold () const
 
virtual llvm::Function * create (llvm::LLVMContext &C, llvm::Module *M=nullptr) const
 Converts and creates this AX function into a llvm Function. More...
 
llvm::Function * create (llvm::Module &M) const
 Convenience method which always uses the provided module to find the function or insert it if necessary. More...
 
llvm::Function * get (const llvm::Module &M) const
 Convenience method for calling M.getFunction(symbol). Returns a nullptr if the function has not yet been created or if it is embedded IR. More...
 
Value call (const NativeArguments &args, llvm::IRBuilder<> &B) const
 
virtual SignatureMatch match (const ArgInfoVector &inputs, llvm::LLVMContext &C) const
 The base implementation for determining how a vector of llvm arguments translates to this functions signature. Returns an enum which represents the available mapping. More...
 
virtual SignatureMatch match (const std::vector< llvm::Type * > &inputs, llvm::LLVMContext &C) const
 
size_t size () const
 The number of arguments that this function has. More...
 
const char * symbol () const
 The function symbol name. More...
 
const char * argName (const size_t idx) const
 Returns the descriptive name of the given argument index. More...
 
virtual void print (llvm::LLVMContext &C, std::ostream &os, const char *name=nullptr, const bool axTypes=true) const
 Print this function's signature to the provided ostream. More...
 
const SmallArgumentVector< const char * > & dependencies () const
 
bool hasParamAttribute (const size_t i, const llvm::Attribute::AttrKind &kind) const
 Deprecated builder methods, no longer public. More...
 
void setArgumentNames (std::vector< const char * > names)
 
void setDependencies (std::vector< const char * > deps)
 
void setFnAttributes (const std::vector< llvm::Attribute::AttrKind > &in)
 
void setRetAttributes (const std::vector< llvm::Attribute::AttrKind > &in)
 
void setParamAttributes (const size_t i, const std::vector< llvm::Attribute::AttrKind > &in)
 

Static Protected Member Functions

static void cast (std::vector< llvm::Value * > &args, const std::vector< llvm::Type * > &types, llvm::IRBuilder<> &B)
 Cast the provided arguments to the given type as supported by implicit casting of function types. If the types already match OR if a cast cannot be performed, nothing is done to the argument. More...
 

Detailed Description

template<typename SignatureT>
struct openvdb::v12_1::ax::codegen::CFunction< SignatureT >

Represents a concrete C function binding.

Note
This struct is templated on the signature to allow for evaluation of the arguments to llvm types from any llvm context.

Member Typedef Documentation

using CFunctionT = CFunction<SignatureT>
using Ptr = std::shared_ptr<CFunctionT>
using Traits = FunctionTraits<SignatureT>

Member Enumeration Documentation

enum SignatureMatch
inherited

The result type from calls to Function::match.

Note
Function::match cannot return Ambiguous - this is only returned by the FunctionGroup API.
Enumerator
None 
Size 
Implicit 
Ambiguous 
Explicit 

Constructor & Destructor Documentation

CFunction ( const std::string &  symbol,
SignatureT *  function 
)
inline
~CFunction ( )
overridedefault

Member Function Documentation

uint64_t address ( ) const
inlinefinaloverridevirtual

Returns the global address of this function.

Note
This is only required for C bindings.

Implements CFunctionBase.

const char* argName ( const size_t  idx) const
inlineinherited

Returns the descriptive name of the given argument index.

If the index is greater than the number of arguments, an empty string is returned.

Parameters
idxThe index of the argument
Value call ( const NativeArguments args,
llvm::IRBuilder<> &  B 
) const
inlineinherited
llvm::Value* call ( const std::vector< llvm::Value * > &  args,
llvm::IRBuilder<> &  B 
) const
inlineoverridevirtual

From LLVM 16 onwards, this version of call does not support argument casting. This must be performed using the NativeArguments struct

Reimplemented from Function.

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >.

Value call ( const Arguments args,
llvm::IRBuilder<> &  B 
) const
inlineoverridevirtual

Reimplemented from Function.

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >.

llvm::Value* call ( const std::vector< llvm::Value * > &  args,
llvm::IRBuilder<> &  B,
const bool  cast 
) const
inlineoverridevirtual
Note
This is deprecated! Omitting the warning as it invokes the parent function which is also deprecated. Use NativeArguments to perform argument casting

Reimplemented from Function.

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >.

static void cast ( std::vector< llvm::Value * > &  args,
const std::vector< llvm::Type * > &  types,
llvm::IRBuilder<> &  B 
)
staticprotectedinherited

Cast the provided arguments to the given type as supported by implicit casting of function types. If the types already match OR if a cast cannot be performed, nothing is done to the argument.

virtual llvm::Function* create ( llvm::LLVMContext &  C,
llvm::Module *  M = nullptr 
) const
virtualinherited

Converts and creates this AX function into a llvm Function.

This method uses the result from Function::types() to construct a llvm::FunctionType and a subsequent a llvm::Function. Any parameter, return or function attributes are also added to the function. If a module is provided, the module if first checked to see if the function already exists. If it does, it is immediately returned. If the function doesn't exist in the module, its prototype is created and also inserted into the end of the modules function list. If no module is provided, the function is left detached and must be added to a valid Module to be callable.

Warning
If a module is not provided, the caller takes ownership of the returned function and is responsible for deallocating it.
Note
The body of the function is left to derived classes to implement. As you need a Module to generate the prototype/body, this function serves two purposes. The first is to return the detached function signature if only a context is provided. The second is to ensure the function prototype and body (if required) is inserted into the module prior to returning.
It is possible to end up with function symbol collisions if you do not have unique function symbols in your module
Parameters
CThe LLVM Context
MThe Module to write the function to

Reimplemented in IRFunctionBase.

llvm::Function* create ( llvm::Module &  M) const
inlineinherited

Convenience method which always uses the provided module to find the function or insert it if necessary.

Parameters
MThe llvm::Module to use
const SmallArgumentVector<const char*>& dependencies ( ) const
inlineinherited
llvm::Constant* fold ( const std::vector< llvm::Value * > &  ,
llvm::LLVMContext &   
) const
inlinefinaloverridevirtual
Deprecated:

Reimplemented from CFunctionBase.

llvm::Function* get ( const llvm::Module &  M) const
inherited

Convenience method for calling M.getFunction(symbol). Returns a nullptr if the function has not yet been created or if it is embedded IR.

Parameters
MThe llvm::Module to use
bool hasConstantFold ( ) const
inlineinherited
bool hasParamAttribute ( const size_t  i,
const llvm::Attribute::AttrKind &  kind 
) const
inlineinherited

Deprecated builder methods, no longer public.

Deprecated:
"This method incorrectly returns the attributes " "of the function set by the FunctionBuilder, not by the codegen. To " "inspect function attributes, retrieve the created function from the " "llvm::Module."
virtual SignatureMatch match ( const ArgInfoVector inputs,
llvm::LLVMContext &  C 
) const
virtualinherited

The base implementation for determining how a vector of llvm arguments translates to this functions signature. Returns an enum which represents the available mapping.

This method calls types() to figure out the function signature, then compares each argument type to the type in the input vector. If the types match exactly, an Explicit match is found. If the sizes of the inputs and signature differ, no match is found and None is returned. If however, the sizes match and there exists a valid implicit cast from the input type to the signature type for every input, an Implicit match is returned. Finally, if the sizes match but there is no implicit cast mapping, Size is returned. i8 -> i32 : Implicit i32 -> i32 : Explicit str -> i32 : Size (i32,i32) -> i32 : None Never returns Ambiguous (this state is used by FunctionGroup)

Note
Due to the way CFunctionSRet is implemented, the LLVM Context must be provided in case we have a zero arg function signature with a SRET.
Parameters
inputsThe input types
CThe LLVM Context

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >, and SRetFunction< SignatureT, IRFunction< SignatureT > >.

virtual SignatureMatch match ( const std::vector< llvm::Type * > &  inputs,
llvm::LLVMContext &  C 
) const
virtualinherited
Deprecated:
"Switch to AX's internal ArgInfo types for LLVM 16 onwards"

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >, and SRetFunction< SignatureT, IRFunction< SignatureT > >.

virtual void print ( llvm::LLVMContext &  C,
std::ostream &  os,
const char *  name = nullptr,
const bool  axTypes = true 
) const
virtualinherited

Print this function's signature to the provided ostream.

This is intended to return a descriptive front end user string rather than the function's IR representation. This function is virtual so that derived classes can customize how they present frontend information.

See also
printSignature
Parameters
CThe llvm context
osThe ostream to print to
nameThe name to insert into the description.
axTypesWhether to print llvm IR or AX Types.

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >, and SRetFunction< SignatureT, IRFunction< SignatureT > >.

void setArgumentNames ( std::vector< const char * >  names)
inlineinherited
Deprecated:
"Use the FunctionBuilder to construct Functions"
void setConstantFold ( bool  on)
inlineinherited
void setDependencies ( std::vector< const char * >  deps)
inlineinherited
Deprecated:
"Use the FunctionBuilder to construct Functions"
void setFnAttributes ( const std::vector< llvm::Attribute::AttrKind > &  in)
inlineinherited
Deprecated:
"Use the FunctionBuilder to construct Functions"
void setParamAttributes ( const size_t  i,
const std::vector< llvm::Attribute::AttrKind > &  in 
)
inlineinherited
Deprecated:
"Use the FunctionBuilder to construct Functions"
void setRetAttributes ( const std::vector< llvm::Attribute::AttrKind > &  in)
inlineinherited
Deprecated:
"Use the FunctionBuilder to construct Functions"
size_t size ( ) const
inlineinherited

The number of arguments that this function has.

const char* symbol ( ) const
inlineinherited

The function symbol name.

This will be used as its identifier in IR and must be unique.

ArgInfo types ( ArgInfoVector ,
llvm::LLVMContext &   
) const
inlineoverridevirtual

Populate a vector of ArgInfos which describe this function signature. This method is used by Function::create, Function::print and Function::match.

Note
The variant that takes a vector of ArgInfos is optional with LLVM 15 for compatibility but must be implemented with newer versions.

Reimplemented from Function.

Reimplemented in SRetFunction< SignatureT, CFunction< SignatureT > >.

llvm::Type* types ( std::vector< llvm::Type * > &  ,
llvm::LLVMContext &   
) const
inlineoverridevirtual
Deprecated:
"Switch to AX's internal ArgInfo types for LLVM 16 onwards"

Implements Function.