Intel SPMD Program Compiler  1.11.0
module.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2015, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file module.h
35  @brief %Declaration of the Module class, which is the ispc-side representation
36  of the results of compiling a source file.
37  */
38 
39 #ifndef ISPC_MODULE_H
40 #define ISPC_MODULE_H 1
41 
42 #include "ast.h"
43 #include "ispc.h"
44 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_4
45 #include <llvm/DebugInfo.h>
46 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_5
47 #include <llvm/IR/DebugInfo.h>
48 #endif
49 
50 namespace llvm {
51 class raw_string_ostream;
52 }
53 
54 struct DispatchHeaderInfo;
55 
56 class Module {
57  public:
58  /** The name of the source file being compiled should be passed as the
59  module name. */
60  Module(const char *filename);
61 
62  /** Compiles the source file passed to the Module constructor, adding
63  its global variables and functions to both the llvm::Module and
64  SymbolTable. Returns the number of errors during compilation. */
65  int CompileFile();
66 
67  /** Add a named type definition to the module. */
68  void AddTypeDef(const std::string &name, const Type *type, SourcePos pos);
69 
70  /** Add a new global variable corresponding to the given Symbol to the
71  module. If non-NULL, initExpr gives the initiailizer expression
72  for the global's inital value. */
73  void AddGlobalVariable(const std::string &name, const Type *type, Expr *initExpr, bool isConst,
74  StorageClass storageClass, SourcePos pos);
75 
76  /** Add a declaration of the function defined by the given function
77  symbol to the module. */
78  void AddFunctionDeclaration(const std::string &name, const FunctionType *ftype, StorageClass sc, bool isInline,
79  SourcePos pos);
80 
81  /** Adds the function described by the declaration information and the
82  provided statements to the module. */
83  void AddFunctionDefinition(const std::string &name, const FunctionType *ftype, Stmt *code);
84 
85  /** Adds the given type to the set of types that have their definitions
86  included in automatically generated header files. */
87  void AddExportedTypes(const std::vector<std::pair<const Type *, SourcePos>> &types);
88 
89  /** After a source file has been compiled, output can be generated in a
90  number of different formats. */
91  enum OutputType {
92  Asm, /** Generate text assembly language output */
93  Bitcode, /** Generate LLVM IR bitcode output */
94  Object, /** Generate a native object file */
95  CXX, /** Generate a C++ file */
96  Header, /** Generate a C/C++ header file with
97  declarations of 'export'ed functions, global
98  variables, and the types used by them. */
99  Deps, /** generate dependencies */
100  DevStub, /** generate device-side offload stubs */
101  HostStub /** generate host-side offload stubs */
102  };
103 
104  enum OutputFlags : int {
105  NoFlags = 0,
106  GeneratePIC = 0x1,
107  GenerateFlatDeps = 0x2, /** Dependencies will be output as a flat list. */
108  GenerateMakeRuleForDeps = 0x4, /** Dependencies will be output in a make rule format instead of a flat list. */
109  OutputDepsToStdout = 0x8, /** Dependency information will be output to stdout instead of file. */
110  };
111 
112  /** Compile the given source file, generating assembly, object file, or
113  LLVM bitcode output, as well as (optionally) a header file with
114  declarations of functions and types used in the ispc/application
115  interface.
116  @param srcFile Pathname to ispc source file to compile
117  @param arch %Target architecture (e.g. "x86-64")
118  @param cpu %Target CPU (e.g. "core-i7")
119  @param targets %Target ISAs; this parameter may give a single target
120  ISA, or may give a comma-separated list of them in
121  case we are compiling to multiple ISAs.
122  @param generatePIC Indicates whether position-independent code should
123  be generated.
124  @param outputType %Type of output to generate (object files, assembly,
125  LLVM bitcode.)
126  @param outFileName Base name of output filename for object files, etc.
127  If for example the multiple targets "sse2" and "avx"
128  are specified in the "targets" parameter and if this
129  parameter is "foo.o", then we'll generate multiple
130  output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
131  @param headerFileName If non-NULL, emit a header file suitable for
132  inclusion from C/C++ code with declarations of
133  types and functions exported from the given ispc
134  source file.
135  @param includeFileName If non-NULL, gives the filename for the C++
136  backend to emit in an #include statement to
137  get definitions of the builtins for the generic
138  target.
139  @return Number of errors encountered when compiling
140  srcFile.
141  */
142  static int CompileAndOutput(const char *srcFile, const char *arch, const char *cpu, const char *targets,
143  OutputFlags outputFlags, OutputType outputType, const char *outFileName,
144  const char *headerFileName, const char *includeFileName, const char *depsFileName,
145  const char *depsTargetName, const char *hostStubFileName, const char *devStubFileName);
146 
147  /** Total number of errors encountered during compilation. */
149 
150  /** Symbol table to hold symbols visible in the current scope during
151  compilation. */
153 
154  /** llvm Module object into which globals and functions are added. */
155  llvm::Module *module;
156 
157  /** The diBuilder manages generating debugging information */
158  llvm::DIBuilder *diBuilder;
159 
160 #if ISPC_LLVM_VERSION >= ISPC_LLVM_3_4 && ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
161  llvm::DICompileUnit diCompileUnit;
162 #elif ISPC_LLVM_VERSION >= ISPC_LLVM_3_7
163  llvm::DICompileUnit *diCompileUnit;
164 #endif // LLVM_3_4+
165 
166  private:
167  const char *filename;
169 
170  std::vector<std::pair<const Type *, SourcePos>> exportedTypes;
171 
172  /** Write the corresponding output type to the given file. Returns
173  true on success, false if there has been an error. The given
174  filename may be NULL, indicating that output should go to standard
175  output. */
176  bool writeOutput(OutputType ot, OutputFlags flags, const char *filename, const char *includeFileName = NULL,
177  const char *sourceFileName = NULL, DispatchHeaderInfo *DHI = 0);
178  bool writeHeader(const char *filename);
180  bool writeDeps(const char *filename, bool generateMakeRule, const char *targetName = NULL,
181  const char *srcFilename = NULL);
182  bool writeDevStub(const char *filename);
183  bool writeHostStub(const char *filename);
184  bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
185  static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine, llvm::Module *module,
186  OutputType outputType, const char *outFileName);
187  static bool writeBitcode(llvm::Module *module, const char *outFileName);
188 
189  void execPreprocessor(const char *infilename, llvm::raw_string_ostream *ostream) const;
190 };
191 
192 inline Module::OutputFlags &operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
193  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
194 }
195 inline Module::OutputFlags &operator&=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
196  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs & rhs);
197 }
199  return (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
200 }
201 
202 #endif // ISPC_MODULE_H
bool writeOutput(OutputType ot, OutputFlags flags, const char *filename, const char *includeFileName=NULL, const char *sourceFileName=NULL, DispatchHeaderInfo *DHI=0)
Definition: module.cpp:1155
Definition: ast.h:139
Definition: ispc.h:75
void AddTypeDef(const std::string &name, const Type *type, SourcePos pos)
Definition: module.cpp:532
std::vector< std::pair< const Type *, SourcePos > > exportedTypes
Definition: module.h:170
bool writeDevStub(const char *filename)
Definition: module.cpp:1907
static int CompileAndOutput(const char *srcFile, const char *arch, const char *cpu, const char *targets, OutputFlags outputFlags, OutputType outputType, const char *outFileName, const char *headerFileName, const char *includeFileName, const char *depsFileName, const char *depsTargetName, const char *hostStubFileName, const char *devStubFileName)
Definition: module.cpp:2962
Interface class for statements in the ispc language.
Definition: stmt.h:49
Symbol table that holds all known symbols during parsing and compilation.
Definition: sym.h:117
Module::OutputFlags & operator &=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:195
OutputType
Definition: module.h:91
bool writeHeader(const char *filename)
Definition: module.cpp:2160
bool writeObjectFileOrAssembly(OutputType outputType, const char *filename)
Definition: module.cpp:1407
bool writeHostStub(const char *filename)
Definition: module.cpp:2042
llvm::Module * module
Definition: module.h:155
void execPreprocessor(const char *infilename, llvm::raw_string_ostream *ostream) const
Definition: module.cpp:2393
static bool writeBitcode(llvm::Module *module, const char *outFileName)
Definition: module.cpp:1339
void AddExportedTypes(const std::vector< std::pair< const Type *, SourcePos >> &types)
Definition: module.cpp:1142
void AddFunctionDeclaration(const std::string &name, const FunctionType *ftype, StorageClass sc, bool isInline, SourcePos pos)
Definition: module.cpp:865
Definition: module.h:56
int CompileFile()
Definition: module.cpp:467
Module(const char *filename)
Definition: module.cpp:376
Representation of a range of positions in a source file.
Definition: ispc.h:129
Module::OutputFlags & operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:192
StorageClass
Definition: ispc.h:120
void AddFunctionDefinition(const std::string &name, const FunctionType *ftype, Stmt *code)
Definition: module.cpp:1123
OutputFlags
Definition: module.h:104
Type representing a function (return type + argument types)
Definition: type.h:858
Interface class that defines the type abstraction.
Definition: type.h:95
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
Module::OutputFlags operator|(const Module::OutputFlags lhs, const Module::OutputFlags rhs)
Definition: module.h:198
bool writeDeps(const char *filename, bool generateMakeRule, const char *targetName=NULL, const char *srcFilename=NULL)
Definition: module.cpp:1842
void AddGlobalVariable(const std::string &name, const Type *type, Expr *initExpr, bool isConst, StorageClass storageClass, SourcePos pos)
Definition: module.cpp:538
int errorCount
Definition: module.h:148
const char * filename
Definition: module.h:167
llvm::DIBuilder * diBuilder
Definition: module.h:158
Main ispc.header file. Defines Target, Globals and Opt classes.
SymbolTable * symbolTable
Definition: module.h:152
AST * ast
Definition: module.h:168
bool writeDispatchHeader(DispatchHeaderInfo *DHI)
Definition: module.cpp:2276