Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
module.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2019, 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 #pragma once
40 
41 #include "ast.h"
42 #include "ispc.h"
43 #include <llvm/IR/DebugInfo.h>
44 
45 namespace llvm {
46 class raw_string_ostream;
47 }
48 
49 struct DispatchHeaderInfo;
50 
51 class Module {
52  public:
53  /** The name of the source file being compiled should be passed as the
54  module name. */
55  Module(const char *filename);
56 
57  /** Compiles the source file passed to the Module constructor, adding
58  its global variables and functions to both the llvm::Module and
59  SymbolTable. Returns the number of errors during compilation. */
60  int CompileFile();
61 
62  /** Add a named type definition to the module. */
63  void AddTypeDef(const std::string &name, const Type *type, SourcePos pos);
64 
65  /** Add a new global variable corresponding to the given Symbol to the
66  module. If non-NULL, initExpr gives the initiailizer expression
67  for the global's inital value. */
68  void AddGlobalVariable(const std::string &name, const Type *type, Expr *initExpr, bool isConst,
69  StorageClass storageClass, SourcePos pos);
70 
71  /** Add a declaration of the function defined by the given function
72  symbol to the module. */
73  void AddFunctionDeclaration(const std::string &name, const FunctionType *ftype, StorageClass sc, bool isInline,
74  bool isNoInline, SourcePos pos);
75 
76  /** Adds the function described by the declaration information and the
77  provided statements to the module. */
78  void AddFunctionDefinition(const std::string &name, const FunctionType *ftype, Stmt *code);
79 
80  /** Adds the given type to the set of types that have their definitions
81  included in automatically generated header files. */
82  void AddExportedTypes(const std::vector<std::pair<const Type *, SourcePos>> &types);
83 
84  /** After a source file has been compiled, output can be generated in a
85  number of different formats. */
86  enum OutputType {
87  Asm, /** Generate text assembly language output */
88  Bitcode, /** Generate LLVM IR bitcode output */
89  BitcodeText, /** Generate LLVM IR Text output */
90  Object, /** Generate a native object file */
91  CXX, /** Generate a C++ file */
92  Header, /** Generate a C/C++ header file with
93  declarations of 'export'ed functions, global
94  variables, and the types used by them. */
95  Deps, /** generate dependencies */
96  DevStub, /** generate device-side offload stubs */
97  HostStub /** generate host-side offload stubs */
98  };
99 
100  enum OutputFlags : int {
101  NoFlags = 0,
102  GeneratePIC = 0x1,
103  GenerateFlatDeps = 0x2, /** Dependencies will be output as a flat list. */
104  GenerateMakeRuleForDeps = 0x4, /** Dependencies will be output in a make rule format instead of a flat list. */
105  OutputDepsToStdout = 0x8, /** Dependency information will be output to stdout instead of file. */
106  };
107 
108  /** Compile the given source file, generating assembly, object file, or
109  LLVM bitcode output, as well as (optionally) a header file with
110  declarations of functions and types used in the ispc/application
111  interface.
112  @param srcFile Pathname to ispc source file to compile
113  @param arch %Target architecture (e.g. "x86-64")
114  @param cpu %Target CPU (e.g. "core-i7")
115  @param targets %Target ISAs; this parameter may give a single target
116  ISA, or may give a comma-separated list of them in
117  case we are compiling to multiple ISAs.
118  @param generatePIC Indicates whether position-independent code should
119  be generated.
120  @param outputType %Type of output to generate (object files, assembly,
121  LLVM bitcode.)
122  @param outFileName Base name of output filename for object files, etc.
123  If for example the multiple targets "sse2" and "avx"
124  are specified in the "targets" parameter and if this
125  parameter is "foo.o", then we'll generate multiple
126  output files, like "foo.o", "foo_sse2.o", "foo_avx.o".
127  @param headerFileName If non-NULL, emit a header file suitable for
128  inclusion from C/C++ code with declarations of
129  types and functions exported from the given ispc
130  source file.
131  @param includeFileName If non-NULL, gives the filename for the C++
132  backend to emit in an #include statement to
133  get definitions of the builtins for the generic
134  target.
135  @return Number of errors encountered when compiling
136  srcFile.
137  */
138  static int CompileAndOutput(const char *srcFile, Arch arch, const char *cpu, std::vector<ISPCTarget> targets,
139  OutputFlags outputFlags, OutputType outputType, const char *outFileName,
140  const char *headerFileName, const char *includeFileName, const char *depsFileName,
141  const char *depsTargetName, const char *hostStubFileName, const char *devStubFileName);
142 
143  /** Total number of errors encountered during compilation. */
145 
146  /** Symbol table to hold symbols visible in the current scope during
147  compilation. */
149 
150  /** llvm Module object into which globals and functions are added. */
151  llvm::Module *module;
152 
153  /** The diBuilder manages generating debugging information */
154  llvm::DIBuilder *diBuilder;
155 
156  llvm::DICompileUnit *diCompileUnit;
157 
158  private:
159  const char *filename;
161 
162  std::vector<std::pair<const Type *, SourcePos>> exportedTypes;
163 
164  /** Write the corresponding output type to the given file. Returns
165  true on success, false if there has been an error. The given
166  filename may be NULL, indicating that output should go to standard
167  output. */
168  bool writeOutput(OutputType ot, OutputFlags flags, const char *filename, const char *includeFileName = NULL,
169  const char *sourceFileName = NULL, DispatchHeaderInfo *DHI = 0);
170  bool writeHeader(const char *filename);
171  bool writeDispatchHeader(DispatchHeaderInfo *DHI);
172  bool writeDeps(const char *filename, bool generateMakeRule, const char *targetName = NULL,
173  const char *srcFilename = NULL);
174  bool writeDevStub(const char *filename);
175  bool writeHostStub(const char *filename);
176  bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
177  static bool writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine, llvm::Module *module,
178  OutputType outputType, const char *outFileName);
179  static bool writeBitcode(llvm::Module *module, const char *outFileName, OutputType outputType);
180 
181  void execPreprocessor(const char *infilename, llvm::raw_string_ostream *ostream) const;
182 };
183 
184 inline Module::OutputFlags &operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
185  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
186 }
187 inline Module::OutputFlags &operator&=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs) {
188  return lhs = (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs & rhs);
189 }
191  return (Module::OutputFlags)((__underlying_type(Module::OutputFlags))lhs | rhs);
192 }
Definition: ast.h:138
Definition: ispc.h:75
std::vector< std::pair< const Type *, SourcePos > > exportedTypes
Definition: module.h:162
Interface class for statements in the ispc language.
Definition: stmt.h:48
Symbol table that holds all known symbols during parsing and compilation.
Definition: sym.h:116
Module::OutputFlags & operator &=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:187
OutputType
Definition: module.h:86
llvm::Module * module
Definition: module.h:151
Definition: module.h:51
Representation of a range of positions in a source file.
Definition: ispc.h:123
Module::OutputFlags & operator|=(Module::OutputFlags &lhs, const __underlying_type(Module::OutputFlags) rhs)
Definition: module.h:184
StorageClass
Definition: ispc.h:114
OutputFlags
Definition: module.h:100
Type representing a function (return type + argument types)
Definition: type.h:829
Interface class that defines the type abstraction.
Definition: type.h:90
llvm::DICompileUnit * diCompileUnit
Definition: module.h:156
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:47
Module::OutputFlags operator|(const Module::OutputFlags lhs, const Module::OutputFlags rhs)
Definition: module.h:190
int errorCount
Definition: module.h:144
const char * filename
Definition: module.h:159
llvm::DIBuilder * diBuilder
Definition: module.h:154
Main ispc.header file. Defines Target, Globals and Opt classes.
Arch
Definition: target_enums.h:50
SymbolTable * symbolTable
Definition: module.h:148
AST * ast
Definition: module.h:160