Intel SPMD Program Compiler  1.9.1
Classes | 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 "util.h"
#include "ctx.h"
#include "func.h"
#include "builtins.h"
#include "type.h"
#include "expr.h"
#include "sym.h"
#include "stmt.h"
#include "opt.h"
#include "llvmutil.h"
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <algorithm>
#include <set>
#include <sstream>
#include <iostream>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/DerivedTypes.h>
#include "llvm/PassManager.h"
#include <llvm/PassRegistry.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/FileUtilities.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/Support/InstIterator.h>
#include <llvm/Support/CFG.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/Utils.h>
#include <clang/Basic/TargetInfo.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Bitcode/ReaderWriter.h>
Include dependency graph for module.cpp:

Go to the source code of this file.

Classes

struct  DispatchHeaderInfo
 
struct  FunctionTargetVariants
 

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)
 
std::string emitOffloadParamStruct (const std::string &paramStructName, const Symbol *sym, const FunctionType *fct)
 
static std::string lGetTargetFileName (const char *outFileName, const char *isaString, bool forceCXX)
 
static std::vector< std::string > lExtractTargets (const char *target)
 
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.

Typedef Documentation

typedef struct yy_buffer_state* YY_BUFFER_STATE

Definition at line 448 of file module.cpp.

Function Documentation

std::string emitOffloadParamStruct ( const std::string &  paramStructName,
const Symbol sym,
const FunctionType fct 
)
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 1755 of file module.cpp.

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

Referenced by lGetExportedTypes().

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 816 of file module.cpp.

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

Referenced by Module::AddFunctionDeclaration().

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 856 of file module.cpp.

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

Referenced by Module::AddFunctionDeclaration().

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

Definition at line 2939 of file module.cpp.

Referenced by lExtractOrCheckGlobals().

static bool lContainsPtrToVarying ( const StructType st)
static
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 2753 of file module.cpp.

References Assert, Globals::ctx, Error(), FunctionTargetVariants::func, g, Target::GENERIC, Target::getTreatGenericAsSmth(), Target::KNL_AVX512, lGetVaryingDispatchType(), LLVMInt32(), Module::module, Target::NUM_ISAS, and Globals::target.

Referenced by lEmitDispatchModule().

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

Definition at line 2911 of file module.cpp.

References Assert, and lCreateDispatchFunction().

Referenced by Module::CompileAndOutput().

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 1675 of file module.cpp.

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

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

static void lEmitStructDecl ( const StructType st,
std::vector< const StructType * > *  emittedStructs,
FILE *  file,
bool  emitUnifs = true 
)
static
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 1651 of file module.cpp.

References lEmitStructDecl().

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

static void lEmitVectorTypedefs ( const std::vector< const VectorType * > &  types,
FILE *  file 
)
static
static void lExtractOrCheckGlobals ( llvm::Module *  msrc,
llvm::Module *  mdst,
bool  check 
)
static
static std::vector<std::string> lExtractTargets ( const char *  target)
static

Definition at line 2634 of file module.cpp.

Referenced by Module::CompileAndOutput().

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 1536 of file module.cpp.

References ArrayType::GetElementType().

Referenced by lEmitStructDecl().

static void lGetExportedFunctions ( SymbolTable symbolTable,
std::map< std::string, FunctionTargetVariants > &  functions 
)
static
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 1824 of file module.cpp.

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

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

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 1775 of file module.cpp.

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

Referenced by lGetExportedParamTypes().

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

Definition at line 2597 of file module.cpp.

Referenced by Module::CompileAndOutput().

static llvm::FunctionType* lGetVaryingDispatchType ( FunctionTargetVariants funcs)
static
static llvm::Module* lInitDispatchModule ( )
static
static bool lIsExported ( const Symbol sym)
static
static bool lIsExternC ( const Symbol sym)
static

Definition at line 1880 of file module.cpp.

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

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

static void lPrintFunctionDeclarations ( FILE *  file,
const std::vector< Symbol * > &  funcs,
bool  useExternC = 1,
bool  rewriteForDispatch = false 
)
static
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 773 of file module.cpp.

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

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

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.

for every instruction in the function

Definition at line 169 of file module.cpp.

References Assert, Globals::ctx, Debug(), Module::diBuilder, g, Globals::generateDebuggingSymbols, and m.

Referenced by Module::writeOutput().

static bool lSymbolIsExported ( const Symbol s)
static

Definition at line 2652 of file module.cpp.

References Symbol::exportedFunction.

Referenced by lGetExportedFunctions().

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 138 of file module.cpp.

References registeredDependencies.

YY_BUFFER_STATE yy_create_buffer ( FILE *  ,
int   
)

Referenced by Module::CompileFile().

void yy_delete_buffer ( YY_BUFFER_STATE  )

Referenced by Module::CompileFile().

YY_BUFFER_STATE yy_scan_string ( const char *  )

Referenced by Module::CompileFile().

void yy_switch_to_buffer ( YY_BUFFER_STATE  )

Referenced by Module::CompileFile().

int yyparse ( )

Referenced by Module::CompileFile().

Variable Documentation

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 134 of file module.cpp.

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

FILE* yyin