Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
Functions
builtins.cpp File Reference

Definitions of functions related to setting up the standard library and other builtins. More...

#include "builtins.h"
#include "ctx.h"
#include "expr.h"
#include "llvmutil.h"
#include "module.h"
#include "sym.h"
#include "type.h"
#include "util.h"
#include <math.h>
#include <stdlib.h>
#include <llvm/ADT/Triple.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Target/TargetMachine.h>
Include dependency graph for builtins.cpp:

Go to the source code of this file.

Functions

int yyparse ()
 
yy_buffer_state * yy_scan_string (const char *)
 
static const TypelLLVMTypeToISPCType (const llvm::Type *t, bool intAsUnsigned)
 
static void lCreateSymbol (const std::string &name, const Type *returnType, llvm::SmallVector< const Type *, 8 > &argTypes, const llvm::FunctionType *ftype, llvm::Function *func, SymbolTable *symbolTable)
 
static bool lCreateISPCSymbol (llvm::Function *func, SymbolTable *symbolTable)
 
static void lAddModuleSymbols (llvm::Module *module, SymbolTable *symbolTable)
 
static void lCheckModuleIntrinsics (llvm::Module *module)
 
static void lSetInternalFunctions (llvm::Module *module)
 
void AddBitcodeToModule (const BitcodeLib *lib, llvm::Module *module, SymbolTable *symbolTable, bool warn)
 
static void lDefineConstantInt (const char *name, int val, llvm::Module *module, SymbolTable *symbolTable, std::vector< llvm::Constant *> &dbg_sym)
 
static void lDefineConstantIntFunc (const char *name, int val, llvm::Module *module, SymbolTable *symbolTable, std::vector< llvm::Constant *> &dbg_sym)
 
static void lDefineProgramIndex (llvm::Module *module, SymbolTable *symbolTable, std::vector< llvm::Constant *> &dbg_sym)
 
static void emitLLVMUsed (llvm::Module &module, std::vector< llvm::Constant *> &list)
 
void DefineStdlib (SymbolTable *symbolTable, llvm::LLVMContext *ctx, llvm::Module *module, bool includeStdlibISPC)
 

Detailed Description

Definitions of functions related to setting up the standard library and other builtins.

Definition in file builtins.cpp.

Function Documentation

◆ AddBitcodeToModule()

void AddBitcodeToModule ( const BitcodeLib lib,
llvm::Module *  module,
SymbolTable symbolTable,
bool  warn 
)

This utility function takes serialized binary LLVM bitcode and adds its definitions to the given module. Functions in the bitcode that can be mapped to ispc functions are also added to the symbol table.

Parameters
libPointer to BitcodeLib class representing LLVM bitcode (e.g. the contents of a *.bc file)
moduleModule to link the bitcode into
symbolTableSymbol table to add definitions to

Definition at line 746 of file builtins.cpp.

References Assert, Globals::ctx, Debug(), Error(), g, BitcodeLib::getLib(), BitcodeLib::getSize(), lAddModuleSymbols(), lCheckModuleIntrinsics(), lSetInternalFunctions(), m, Module::module, VerifyDataLayoutCompatibility(), and Warning().

Referenced by DefineStdlib(), and lInitDispatchModule().

◆ DefineStdlib()

void DefineStdlib ( SymbolTable symbolTable,
llvm::LLVMContext *  ctx,
llvm::Module *  module,
bool  includeStdlib 
)

Adds declarations and definitions of ispc standard library functions and types to the given module.

Parameters
symbolTableSymbolTable in which to add symbol definitions for stdlib stuff
ctxllvm::LLVMContext to use for getting types and the like for standard library definitions
moduleModule in which to add the declarations/definitions
includeStdlibIndicates whether the definitions from the stdlib.ispc file should be added to the module.

Definition at line 920 of file builtins.cpp.

References AddBitcodeToModule(), Assert, emitLLVMUsed(), Opt::fastMaskedVload, FATAL, Globals::forceAlignment, g, Globals::generateDebuggingSymbols, Target::GENERIC, Target::getArch(), TargetLibRegistry::getBuiltinsCLib(), Target::getISA(), Target::getISPCTarget(), TargetLibRegistry::getISPCTargetLib(), Target::getMaskBitCount(), Target::getVectorWidth(), Target::hasHalf(), Target::hasRand(), Target::hasRcpd(), Target::hasRsqrtd(), Target::hasTranscendentals(), Target::hasTrigonometry(), lDefineConstantInt(), lDefineConstantIntFunc(), lDefineProgramIndex(), LLVMInt32(), Globals::Math_ISPC, Globals::Math_ISPCFast, Globals::Math_SVML, Globals::Math_System, Globals::mathLib, Globals::opt, Globals::target, Globals::target_os, Globals::target_registry, yy_scan_string(), and yyparse().

Referenced by Module::CompileFile().

◆ emitLLVMUsed()

static void emitLLVMUsed ( llvm::Module &  module,
std::vector< llvm::Constant *> &  list 
)
static

Definition at line 903 of file builtins.cpp.

References LLVMTypes::Int8PointerType.

Referenced by DefineStdlib().

◆ lAddModuleSymbols()

static void lAddModuleSymbols ( llvm::Module *  module,
SymbolTable symbolTable 
)
static

Given an LLVM module, create ispc symbols for the functions in the module.

Definition at line 243 of file builtins.cpp.

References Assert, and lCreateISPCSymbol().

Referenced by AddBitcodeToModule().

◆ lCheckModuleIntrinsics()

static void lCheckModuleIntrinsics ( llvm::Module *  module)
static

In many of the builtins-*.ll files, we have declarations of various LLVM intrinsics that are then used in the implementation of various target- specific functions. This function loops over all of the intrinsic declarations and makes sure that the signature we have in our .ll file matches the signature of the actual intrinsic.

Definition at line 262 of file builtins.cpp.

References Assert, Globals::ctx, FATAL, and g.

Referenced by AddBitcodeToModule().

◆ lCreateISPCSymbol()

static bool lCreateISPCSymbol ( llvm::Function *  func,
SymbolTable symbolTable 
)
static

Given an LLVM function declaration, synthesize the equivalent ispc symbol for the function (if possible). Returns true on success, false on failure.

Definition at line 166 of file builtins.cpp.

References SymbolTable::AddFunction(), Debug(), Type::Equal(), Symbol::function, g, Target::getMaskBitCount(), lCreateSymbol(), lLLVMTypeToISPCType(), SourcePos::name, Globals::target, AtomicType::VaryingBool, and AtomicType::VaryingInt32.

Referenced by lAddModuleSymbols().

◆ lCreateSymbol()

static void lCreateSymbol ( const std::string &  name,
const Type returnType,
llvm::SmallVector< const Type *, 8 > &  argTypes,
const llvm::FunctionType *  ftype,
llvm::Function *  func,
SymbolTable symbolTable 
)
static

◆ lDefineConstantInt()

static void lDefineConstantInt ( const char *  name,
int  val,
llvm::Module *  module,
SymbolTable symbolTable,
std::vector< llvm::Constant *> &  dbg_sym 
)
static

Utility routine that defines a constant int32 with given value, adding the symbol to both the ispc symbol table and the given LLVM module.

Definition at line 822 of file builtins.cpp.

References SymbolTable::AddVariable(), Symbol::constValue, Module::diBuilder, Module::diCompileUnit, Type::GetDIType(), LLVMTypes::Int32Type, LLVMInt32(), m, SC_STATIC, Symbol::storagePtr, Symbol::type, and AtomicType::UniformInt32.

Referenced by DefineStdlib().

◆ lDefineConstantIntFunc()

static void lDefineConstantIntFunc ( const char *  name,
int  val,
llvm::Module *  module,
SymbolTable symbolTable,
std::vector< llvm::Constant *> &  dbg_sym 
)
static

◆ lDefineProgramIndex()

static void lDefineProgramIndex ( llvm::Module *  module,
SymbolTable symbolTable,
std::vector< llvm::Constant *> &  dbg_sym 
)
static

◆ lLLVMTypeToISPCType()

static const Type* lLLVMTypeToISPCType ( const llvm::Type *  t,
bool  intAsUnsigned 
)
static

Given an LLVM type, try to find the equivalent ispc type. Note that this is an under-constrained problem due to LLVM's type representations carrying less information than ispc's. (For example, LLVM doesn't distinguish between signed and unsigned integers in its types.)

Because this function is only used for generating ispc declarations of functions defined in LLVM bitcode in the builtins-*.ll files, in practice we can get enough of what we need for the relevant cases to make things work, partially with the help of the intAsUnsigned parameter, which indicates whether LLVM integer types should be treated as being signed or unsigned.

Definition at line 81 of file builtins.cpp.

References LLVMTypes::BoolType, LLVMTypes::DoublePointerType, LLVMTypes::DoubleType, LLVMTypes::DoubleVectorPointerType, LLVMTypes::DoubleVectorType, LLVMTypes::FloatPointerType, LLVMTypes::FloatType, LLVMTypes::FloatVectorPointerType, LLVMTypes::FloatVectorType, PointerType::GetUniform(), LLVMTypes::Int16PointerType, LLVMTypes::Int16Type, LLVMTypes::Int16VectorPointerType, LLVMTypes::Int16VectorType, LLVMTypes::Int32PointerType, LLVMTypes::Int32Type, LLVMTypes::Int32VectorPointerType, LLVMTypes::Int32VectorType, LLVMTypes::Int64PointerType, LLVMTypes::Int64Type, LLVMTypes::Int64VectorPointerType, LLVMTypes::Int64VectorType, LLVMTypes::Int8PointerType, LLVMTypes::Int8Type, LLVMTypes::Int8VectorPointerType, LLVMTypes::Int8VectorType, LLVMTypes::MaskType, AtomicType::UniformBool, AtomicType::UniformDouble, AtomicType::UniformFloat, AtomicType::UniformInt16, AtomicType::UniformInt32, AtomicType::UniformInt64, AtomicType::UniformInt8, AtomicType::UniformUInt16, AtomicType::UniformUInt32, AtomicType::UniformUInt64, AtomicType::UniformUInt8, AtomicType::VaryingBool, AtomicType::VaryingDouble, AtomicType::VaryingFloat, AtomicType::VaryingInt16, AtomicType::VaryingInt32, AtomicType::VaryingInt64, AtomicType::VaryingInt8, AtomicType::VaryingUInt16, AtomicType::VaryingUInt32, AtomicType::VaryingUInt64, AtomicType::VaryingUInt8, AtomicType::Void, and LLVMTypes::VoidType.

Referenced by lCreateISPCSymbol().

◆ lSetInternalFunctions()

static void lSetInternalFunctions ( llvm::Module *  module)
static

We'd like to have all of these functions declared as 'internal' in their respective bitcode files so that if they aren't needed by the user's program they are elimiated from the final output. However, if we do so, then they aren't brought in by the LinkModules() call below since they aren't yet used by anything in the module they're being linked with (in LLVM 3.1, at least).

Therefore, we don't declare them as internal when we first define them, but instead mark them as internal after they've been linked in. This is admittedly a kludge.

Definition at line 297 of file builtins.cpp.

Referenced by AddBitcodeToModule().

◆ yy_scan_string()

yy_buffer_state* yy_scan_string ( const char *  )

Referenced by DefineStdlib().

◆ yyparse()

int yyparse ( )

Referenced by DefineStdlib().