-
Notifications
You must be signed in to change notification settings - Fork 0
/
ispc.h
322 lines (260 loc) · 11.1 KB
/
ispc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file ispc.h
@brief Main ispc.header file
*/
#ifndef ISPC_H
#define ISPC_H
#if defined(_WIN32) || defined(_WIN64)
#define ISPC_IS_WINDOWS
#elif defined(__linux__)
#define ISPC_IS_LINUX
#elif defined(__APPLE__)
#define ISPC_IS_APPLE
#endif
#include <assert.h>
#include <stdint.h>
#include <vector>
#include <string>
/** @def ISPC_MAX_NVEC maximum vector size of any of the compliation
targets.
*/
#define ISPC_MAX_NVEC 16
// Forward declarations of a number of widely-used LLVM types
namespace llvm {
class BasicBlock;
class Constant;
class ConstantValue;
class DIBuilder;
class DIDescriptor;
class DIFile;
class DIType;
class Function;
class FunctionType;
class LLVMContext;
class Module;
class Type;
class Value;
}
// llvm::Type *s are no longer const in llvm 3.0
#if defined(LLVM_3_0) || defined(LLVM_3_0svn)
#define LLVM_TYPE_CONST
#else
#define LLVM_TYPE_CONST const
#endif
class ArrayType;
class AtomicType;
class DeclSpecs;
class Declaration;
class Declarator;
class FunctionEmitContext;
class Expr;
class ExprList;
class FunctionType;
class GatherBuffer;
class Module;
class Stmt;
class Symbol;
class SymbolTable;
class Type;
/** @brief Representation of a range of positions in a source file.
This class represents a range of characters in a source file
(e.g. those that span a token's definition), from starting line and
column to ending line and column. (These values are tracked by the
lexing code). Both lines and columns are counted starting from one.
*/
struct SourcePos {
SourcePos(const char *n = NULL, int l = 0, int c = 0);
const char *name;
int first_line;
int first_column;
int last_line;
int last_column;
/** Prints the filename and line/column range to standard output. */
void Print() const;
/** Returns a LLVM DIFile object that represents the SourcePos's file */
llvm::DIFile GetDIFile() const;
bool operator==(const SourcePos &p2) const;
};
/** @brief Abstract base class for nodes in the abstract syntax tree (AST).
This class defines a basic interface that all abstract syntax tree
(AST) nodes must implement. The base classes for both expressions
(Expr) and statements (Stmt) inherit from this class.
*/
class ASTNode {
public:
ASTNode(SourcePos p) : pos(p) { }
virtual ~ASTNode();
/** The Optimize() method should perform any appropriate early-stage
optimizations on the node (e.g. constant folding). The caller
should use the returned ASTNode * in place of the original node.
This method may return NULL if an error is encountered during
optimization. */
virtual ASTNode *Optimize() = 0;
/** Type checking should be performed by the node when this method is
called. In the event of an error, a NULL value may be returned.
As with ASTNode::Optimize(), the caller should store the returned
pointer in place of the original ASTNode *. */
virtual ASTNode *TypeCheck() = 0;
/** All AST nodes must track the file position where they are
defined. */
const SourcePos pos;
};
/** @brief Structure that defines a compilation target
This structure defines a compilation target for the ispc compiler.
*/
struct Target {
Target();
/** Enumerator giving the instruction sets that the compiler can
target. */
enum ISA { SSE2, SSE4, AVX };
/** Instruction set being compiled to. */
ISA isa;
/** Target system architecture. (e.g. "x86-64", "x86"). */
std::string arch;
/** Target CPU. (e.g. "corei7", "corei7-avx", ..) */
std::string cpu;
/** Native vector width of the vector instruction set. Note that this
value is directly derived from the ISA Being used (e.g. it's 4 for
SSE, 8 for AVX, etc.) */
int nativeVectorWidth;
/** Actual vector width currently being compiled to. This may be an
integer multiple of the native vector width, for example if we're
"doubling up" and compiling 8-wide on a 4-wide SSE system. */
int vectorWidth;
};
/** @brief Structure that collects optimization options
This structure collects all of the options related to optimization of
generated code.
*/
struct Opt {
Opt();
/** Optimization level. Currently, the only valid values are 0,
indicating essentially no optimization, and 1, indicating as much
optimization as possible. */
int level;
/** Indicates whether "fast and loose" numerically unsafe optimizations
should be performed. This is false by default. */
bool fastMath;
/** On targets that don't have a masked store instruction but do have a
blending instruction, by default, we simulate masked stores by
loading the old value, blending, and storing the result. This can
potentially be unsafe in multi-threaded code, in that it writes to
locations that aren't supposed to be written to. Setting this
value to true disables this work-around, and instead implements
masked stores by 'scalarizing' them, so that we iterate over the
ISIMD lanes and do a scalar write for the ones that are running. */
bool disableBlendedMaskedStores;
/** Disables the 'coherent control flow' constructs in the
language. (e.g. this causes "cif" statements to be demoted to "if"
statements.) This is likely only useful for measuring the impact
of coherent control flow. */
bool disableCoherentControlFlow;
/** Disables uniform control flow optimizations (e.g. this changes an
"if" statement with a uniform condition to have a varying
condition). This is likely only useful for measuring the impact of
uniform control flow. */
bool disableUniformControlFlow;
/** Disables the backend optimizations related to gather/scatter
(e.g. transforming gather from sequential locations to an unaligned
load, etc.) This is likely only useful for measuring the impact of
these optimizations. */
bool disableGatherScatterOptimizations;
/** Disables the optimization that demotes masked stores to regular
stores when the store is happening at the same control flow level
where the variable was declared. This is likely only useful for
measuring the impact of this optimization. */
bool disableMaskedStoreToStore;
/** Disables the optimization that detects when the execution mask is
all on and emits code for gathers and scatters that doesn't loop
over the SIMD lanes but just does the scalar loads and stores
directly. */
bool disableGatherScatterFlattening;
/** Disables the optimizations that detect when arrays are being
indexed with 'uniform' values and issue scalar loads/stores rather
than gathers/scatters. This is likely only useful for measuring
the impact of this optimization. */
bool disableUniformMemoryOptimizations;
/** Disables optimizations for masked stores: masked stores with the
mask all on are transformed to regular stores, and masked stores
with the mask are all off are removed (which in turn can allow
eliminating additional dead code related to computing the value
stored). This is likely only useful for measuring the impact of
this optimization. */
bool disableMaskedStoreOptimizations;
};
/** @brief This structure collects together a number of global variables.
This structure collects a number of global variables that mostly
represent parameter settings for this compilation run. In particular,
none of these values should change after compilation befins; their
values are all set during command-line argument processing or very
early during the compiler's execution, before any files are parsed.
*/
struct Globals {
Globals();
/** Optimization option settings */
Opt opt;
/** Compilation target information */
Target target;
/** There are a number of math libraries that can be used for
transcendentals and the like during program compilation. */
enum MathLib { Math_ISPC, Math_ISPCFast, Math_SVML, Math_System };
MathLib mathLib;
/** Records whether the ispc standard library should be made available
to the program during compilations. (Default is true.) */
bool includeStdlib;
/** Indicates whether the C pre-processor should be run over the
program source before compiling it. (Default is true.) */
bool runCPP;
/** When \c true, voluminous debugging output will be printed during
ispc's execution. */
bool debugPrint;
/** Indicates whether all warning messages should be surpressed. */
bool disableWarnings;
/** Indicates whether additional warnings should be issued about
possible performance pitfalls. */
bool emitPerfWarnings;
/** Indicates whether calls should be emitted in the program to an
externally-defined program instrumentation function. (See the
"Instrumenting your ispc programs" section in the user's
manual.) */
bool emitInstrumentation;
/** Indicates whether ispc should generate debugging symbols for the
program in its output. */
bool generateDebuggingSymbols;
/** Global LLVMContext object */
llvm::LLVMContext *ctx;
/** Current working directory when the ispc compiler starts
execution. */
char currentDirectory[1024];
/** Arguments to pass along to the C pre-processor, if it is run on the
program before compilation. */
std::vector<std::string> cppArgs;
};
extern Globals *g;
extern Module *m;
#endif // ISPC_H