Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
Classes | Macros | Typedefs | Functions | Variables
module.cpp File Reference

Impementation of the Module class, which collects the result of compiling a source file and then generates output (object files, etc.) More...

#include "module.h"
#include "builtins.h"
#include "ctx.h"
#include "expr.h"
#include "func.h"
#include "llvmutil.h"
#include "opt.h"
#include "stmt.h"
#include "sym.h"
#include "type.h"
#include "util.h"
#include <algorithm>
#include <ctype.h>
#include <fcntl.h>
#include <set>
#include <sstream>
#include <stdarg.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "llvm/IR/LegacyPassManager.h"
#include <clang/Basic/TargetInfo.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/Utils.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/CFG.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/IRPrintingPasses.h>
#include <llvm/IR/InstIterator.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/IR/Verifier.h>
#include <llvm/PassRegistry.h>
#include <llvm/Support/FileUtilities.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/Utils/ValueMapper.h>

Go to the source code of this file.

Classes

struct  DispatchHeaderInfo
 
struct  FunctionTargetVariants
 

Macros

#define UNESCAPE_SEQ(c, esc)
 

Typedefs

typedef struct yy_buffer_state * YY_BUFFER_STATE
 

Functions

void RegisterDependency (const std::string &fileName)
 
static void lDeclareSizeAndPtrIntTypes (SymbolTable *symbolTable)
 
static void lStripUnusedDebugInfo (llvm::Module *module)
 
int yyparse ()
 
void yy_switch_to_buffer (YY_BUFFER_STATE)
 
YY_BUFFER_STATE yy_scan_string (const char *)
 
YY_BUFFER_STATE yy_create_buffer (FILE *, int)
 
void yy_delete_buffer (YY_BUFFER_STATE)
 
static bool lRecursiveCheckValidParamType (const Type *t, bool vectorOk)
 
static void lCheckExportedParameterTypes (const Type *type, const std::string &name, SourcePos pos)
 
static void lCheckForStructParameters (const FunctionType *ftype, SourcePos pos)
 
static const StructTypelGetElementStructType (const Type *t)
 
static bool lContainsPtrToVarying (const StructType *st)
 
static void lEmitStructDecl (const StructType *st, std::vector< const StructType *> *emittedStructs, FILE *file, bool emitUnifs=true)
 
static void lEmitStructDecls (std::vector< const StructType *> &structTypes, FILE *file, bool emitUnifs=true)
 
static void lEmitEnumDecls (const std::vector< const EnumType *> &enumTypes, FILE *file)
 
static void lEmitVectorTypedefs (const std::vector< const VectorType *> &types, FILE *file)
 
template<typename T >
static void lAddTypeIfNew (const Type *type, std::vector< const T *> *exportedTypes)
 
static void lGetExportedTypes (const Type *type, std::vector< const StructType *> *exportedStructTypes, std::vector< const EnumType *> *exportedEnumTypes, std::vector< const VectorType *> *exportedVectorTypes)
 
static void lGetExportedParamTypes (const std::vector< Symbol *> &funcs, std::vector< const StructType *> *exportedStructTypes, std::vector< const EnumType *> *exportedEnumTypes, std::vector< const VectorType *> *exportedVectorTypes)
 
static void lPrintFunctionDeclarations (FILE *file, const std::vector< Symbol *> &funcs, bool useExternC=1, bool rewriteForDispatch=false)
 
static bool lIsExported (const Symbol *sym)
 
static bool lIsExternC (const Symbol *sym)
 
static void lUnescapeStringInPlace (std::string &str)
 
std::string emitOffloadParamStruct (const std::string &paramStructName, const Symbol *sym, const FunctionType *fct)
 
static std::string lGetTargetFileName (const char *outFileName, const char *isaString)
 
static bool lSymbolIsExported (const Symbol *s)
 
static void lGetExportedFunctions (SymbolTable *symbolTable, std::map< std::string, FunctionTargetVariants > &functions)
 
static llvm::FunctionType * lGetVaryingDispatchType (FunctionTargetVariants &funcs)
 
static void lCreateDispatchFunction (llvm::Module *module, llvm::Function *setISAFunc, llvm::Value *systemBestISAPtr, const std::string &name, FunctionTargetVariants &funcs)
 
static llvm::Module * lInitDispatchModule ()
 
static void lEmitDispatchModule (llvm::Module *module, std::map< std::string, FunctionTargetVariants > &functions)
 
static bool lCompatibleTypes (llvm::Type *Ty1, llvm::Type *Ty2)
 
static void lExtractOrCheckGlobals (llvm::Module *msrc, llvm::Module *mdst, bool check)
 

Variables

std::set< std::string > registeredDependencies
 
FILE * yyin
 

Detailed Description

Impementation of the Module class, which collects the result of compiling a source file and then generates output (object files, etc.)

Definition in file module.cpp.

Macro Definition Documentation

◆ UNESCAPE_SEQ

#define UNESCAPE_SEQ (   c,
  esc 
)
Value:
case c: \
*it = esc; \
str.erase(next); \
it = str.begin() + pos; \
break

Referenced by lUnescapeStringInPlace().

Typedef Documentation

◆ YY_BUFFER_STATE

typedef struct yy_buffer_state* YY_BUFFER_STATE

Definition at line 197 of file module.cpp.

Function Documentation

◆ emitOffloadParamStruct()

std::string emitOffloadParamStruct ( const std::string &  paramStructName,
const Symbol sym,
const FunctionType fct 
)

◆ lAddTypeIfNew()

template<typename T >
static void lAddTypeIfNew ( const Type type,
std::vector< const T *> *  exportedTypes 
)
static

Add the given type to the vector, if that type isn't already in there.

Definition at line 1208 of file module.cpp.

References Assert, Type::Equal(), and Type::GetAsNonConstType().

Referenced by lGetExportedTypes().

◆ lCheckExportedParameterTypes()

static void lCheckExportedParameterTypes ( const Type type,
const std::string &  name,
SourcePos  pos 
)
static

Given a Symbol representing a function parameter, see if it or any contained types are varying. If so, issue an error. (This function should only be called for parameters to 'export'ed functions, where varying parameters is illegal.

Definition at line 505 of file module.cpp.

References Error(), Type::GetBaseType(), and lRecursiveCheckValidParamType().

Referenced by Module::AddFunctionDeclaration().

◆ lCheckForStructParameters()

static void lCheckForStructParameters ( const FunctionType ftype,
SourcePos  pos 
)
static

Given a function type, loop through the function parameters and see if any are StructTypes. If so, issue an error; this is currently broken (https://github.com/ispc/ispc/issues/3).

Definition at line 531 of file module.cpp.

References Error(), FunctionType::GetNumParameters(), and FunctionType::GetParameterType().

Referenced by Module::AddFunctionDeclaration().

◆ lCompatibleTypes()

static bool lCompatibleTypes ( llvm::Type *  Ty1,
llvm::Type *  Ty2 
)
static

Definition at line 2311 of file module.cpp.

Referenced by lExtractOrCheckGlobals().

◆ lContainsPtrToVarying()

static bool lContainsPtrToVarying ( const StructType st)
static

◆ lCreateDispatchFunction()

static void lCreateDispatchFunction ( llvm::Module *  module,
llvm::Function *  setISAFunc,
llvm::Value *  systemBestISAPtr,
const std::string &  name,
FunctionTargetVariants funcs 
)
static

Create the dispatch function for an exported ispc function. This function checks to see which vector ISAs the system the code is running on supports and calls out to the best available variant that was generated at compile time.

Parameters
moduleModule in which to create the dispatch function.
setISAFuncPointer to the __set_system_isa() function defined in builtins-dispatch.ll (which is linked into the given module before we get here.)
systemBestISAPtrPointer to the module-local __system_best_isa variable, which holds a value of the Target::ISA enumerant giving the most capable ISA that the system supports.
nameName of the function for which we're generating a dispatch function
funcsTarget-specific variants of the exported function.

Definition at line 2157 of file module.cpp.

References Assert, Globals::ctx, FunctionTargetVariants::func, g, lGetVaryingDispatchType(), LLVMInt32(), Module::module, and Target::NUM_ISAS.

Referenced by lEmitDispatchModule().

◆ lDeclareSizeAndPtrIntTypes()

static void lDeclareSizeAndPtrIntTypes ( SymbolTable symbolTable)
static

◆ lEmitDispatchModule()

static void lEmitDispatchModule ( llvm::Module *  module,
std::map< std::string, FunctionTargetVariants > &  functions 
)
static

Definition at line 2290 of file module.cpp.

References Assert, and lCreateDispatchFunction().

Referenced by Module::CompileAndOutput().

◆ lEmitEnumDecls()

static void lEmitEnumDecls ( const std::vector< const EnumType *> &  enumTypes,
FILE *  file 
)
static

Emit C declarations of enumerator types to the generated header file.

Definition at line 1133 of file module.cpp.

References Assert, Symbol::constValue, ConstExpr::GetValues(), and Symbol::name.

Referenced by Module::writeDevStub(), Module::writeDispatchHeader(), Module::writeHeader(), and Module::writeHostStub().

◆ lEmitStructDecl()

static void lEmitStructDecl ( const StructType st,
std::vector< const StructType *> *  emittedStructs,
FILE *  file,
bool  emitUnifs = true 
)
static

◆ lEmitStructDecls()

static void lEmitStructDecls ( std::vector< const StructType *> &  structTypes,
FILE *  file,
bool  emitUnifs = true 
)
static

Given a set of structures that we want to print C declarations of in a header file, emit their declarations.

Definition at line 1112 of file module.cpp.

References lEmitStructDecl().

Referenced by Module::writeDevStub(), Module::writeDispatchHeader(), Module::writeHeader(), and Module::writeHostStub().

◆ lEmitVectorTypedefs()

static void lEmitVectorTypedefs ( const std::vector< const VectorType *> &  types,
FILE *  file 
)
static

◆ lExtractOrCheckGlobals()

static void lExtractOrCheckGlobals ( llvm::Module *  msrc,
llvm::Module *  mdst,
bool  check 
)
static

◆ lGetElementStructType()

static const StructType* lGetElementStructType ( const Type t)
static

Given a pointer to an element of a structure, see if it is a struct type or an array of a struct type. If so, return a pointer to the underlying struct type.

Definition at line 1007 of file module.cpp.

References ArrayType::GetElementType().

Referenced by lEmitStructDecl().

◆ lGetExportedFunctions()

static void lGetExportedFunctions ( SymbolTable symbolTable,
std::map< std::string, FunctionTargetVariants > &  functions 
)
static

◆ lGetExportedParamTypes()

static void lGetExportedParamTypes ( const std::vector< Symbol *> &  funcs,
std::vector< const StructType *> *  exportedStructTypes,
std::vector< const EnumType *> *  exportedEnumTypes,
std::vector< const VectorType *> *  exportedVectorTypes 
)
static

Given a set of functions, return the set of structure and vector types present in the parameters to them.

Definition at line 1265 of file module.cpp.

References Assert, FunctionType::GetNumParameters(), FunctionType::GetParameterType(), FunctionType::GetReturnType(), and lGetExportedTypes().

Referenced by Module::writeDevStub(), Module::writeDispatchHeader(), Module::writeHeader(), and Module::writeHostStub().

◆ lGetExportedTypes()

static void lGetExportedTypes ( const Type type,
std::vector< const StructType *> *  exportedStructTypes,
std::vector< const EnumType *> *  exportedEnumTypes,
std::vector< const VectorType *> *  exportedVectorTypes 
)
static

Given an arbitrary type that appears in the app/ispc interface, add it to an appropriate vector if it is a struct, enum, or short vector type. Then, if it's a struct, recursively process its members to do the same.

Definition at line 1226 of file module.cpp.

References Assert, Type::GetBaseType(), ArrayType::GetElementType(), Type::GetReferenceTarget(), and lAddTypeIfNew().

Referenced by lGetExportedParamTypes().

◆ lGetTargetFileName()

static std::string lGetTargetFileName ( const char *  outFileName,
const char *  isaString 
)
static

Definition at line 2039 of file module.cpp.

Referenced by Module::CompileAndOutput().

◆ lGetVaryingDispatchType()

static llvm::FunctionType* lGetVaryingDispatchType ( FunctionTargetVariants funcs)
static

◆ lInitDispatchModule()

static llvm::Module* lInitDispatchModule ( )
static

◆ lIsExported()

static bool lIsExported ( const Symbol sym)
static

◆ lIsExternC()

static bool lIsExternC ( const Symbol sym)
static

Definition at line 1313 of file module.cpp.

References Assert, FunctionType::isExternC, and Symbol::type.

Referenced by Module::writeDispatchHeader(), and Module::writeHeader().

◆ lPrintFunctionDeclarations()

static void lPrintFunctionDeclarations ( FILE *  file,
const std::vector< Symbol *> &  funcs,
bool  useExternC = 1,
bool  rewriteForDispatch = false 
)
static

◆ lRecursiveCheckValidParamType()

static bool lRecursiveCheckValidParamType ( const Type t,
bool  vectorOk 
)
static

Given an arbitrary type, see if it or any of the leaf types contained in it has a type that's illegal to have exported to C/C++ code.

(Note that it's fine for the original struct or a contained struct to be varying, so long as all of its members have bound 'uniform' variability.)

This functions returns true and issues an error if are any illegal types are found and returns false otherwise.

Definition at line 465 of file module.cpp.

References StructType::GetElementCount(), SequentialType::GetElementType(), StructType::GetElementType(), and Type::IsVaryingType().

Referenced by Module::AddFunctionDeclaration(), and lCheckExportedParameterTypes().

◆ lStripUnusedDebugInfo()

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

After compilation completes, there's often a lot of extra debugging metadata left around that isn't needed any more–for example, for static functions that weren't actually used, function information for functions that were inlined, etc. This function takes a llvm::Module and tries to strip out all of this extra stuff.

Definition at line 126 of file module.cpp.

Referenced by Module::writeOutput().

◆ lSymbolIsExported()

static bool lSymbolIsExported ( const Symbol s)
static

Definition at line 2065 of file module.cpp.

References Symbol::exportedFunction.

Referenced by lGetExportedFunctions().

◆ lUnescapeStringInPlace()

static void lUnescapeStringInPlace ( std::string &  str)
static

Definition at line 1319 of file module.cpp.

References UNESCAPE_SEQ.

Referenced by Module::writeDeps().

◆ RegisterDependency()

void RegisterDependency ( const std::string &  fileName)

this is where the parser tells us that it has seen the given file name in the CPP hash

Definition at line 101 of file module.cpp.

References registeredDependencies.

◆ yy_create_buffer()

YY_BUFFER_STATE yy_create_buffer ( FILE *  ,
int   
)

Referenced by Module::CompileFile().

◆ yy_delete_buffer()

void yy_delete_buffer ( YY_BUFFER_STATE  )

Referenced by Module::CompileFile().

◆ yy_scan_string()

YY_BUFFER_STATE yy_scan_string ( const char *  )

Referenced by Module::CompileFile().

◆ yy_switch_to_buffer()

void yy_switch_to_buffer ( YY_BUFFER_STATE  )

Referenced by Module::CompileFile().

◆ yyparse()

int yyparse ( )

Variable Documentation

◆ registeredDependencies

std::set<std::string> registeredDependencies

list of files encountered by the parser. this allows emitting of the module file's dependencies via the -MMM option

Definition at line 97 of file module.cpp.

Referenced by RegisterDependency(), and Module::writeDeps().

◆ yyin

FILE* yyin