Intel SPMD Program Compiler  1.11.0
ctx.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 ctx.h
35  @brief %Declaration of the FunctionEmitContext class
36 */
37 
38 #ifndef ISPC_CTX_H
39 #define ISPC_CTX_H 1
40 
41 #include "ispc.h"
42 #include <map>
43 #if ISPC_LLVM_VERSION == ISPC_LLVM_3_2
44 #include <llvm/InstrTypes.h>
45 #include <llvm/Instructions.h>
46 #else // 3.3+
47 #include <llvm/IR/InstrTypes.h>
48 #include <llvm/IR/Instructions.h>
49 #endif
50 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_4
51 #include <llvm/DIBuilder.h>
52 #include <llvm/DebugInfo.h>
53 #else // 3.5+
54 #include <llvm/IR/DIBuilder.h>
55 #include <llvm/IR/DebugInfo.h>
56 #endif
57 
58 struct CFInfo;
59 
60 /** FunctionEmitContext is one of the key classes in ispc; it is used to
61  help with emitting the intermediate representation of a function during
62  compilation. It carries information the current program context during
63  IR emission (e.g. the basic block into which instructions should be
64  added; or, the current source file and line number, so debugging
65  symbols can be correctly generated). This class also provides a number
66  of helper routines that are useful for code that emits IR.
67  */
69  public:
70  /** Create a new FunctionEmitContext.
71  @param function The Function object representing the function
72  @param funSym Symbol that corresponds to the function
73  @param llvmFunction LLVM function in the current module that corresponds
74  to the function
75  @param firstStmtPos Source file position of the first statement in the
76  function
77  */
78  FunctionEmitContext(Function *function, Symbol *funSym, llvm::Function *llvmFunction, SourcePos firstStmtPos);
80 
81  /** Returns the Function * corresponding to the function that we're
82  currently generating code for. */
83  const Function *GetFunction() const;
84 
85  /** @name Current basic block management
86  @{
87  */
88  /** Returns the current basic block pointer */
89  llvm::BasicBlock *GetCurrentBasicBlock();
90 
91  /** Set the given llvm::BasicBlock to be the basic block to emit
92  forthcoming instructions into. */
93  void SetCurrentBasicBlock(llvm::BasicBlock *bblock);
94 
95  /** @name Mask management
96  @{
97  */
98  /** Returns the mask value at entry to the current function. */
99  llvm::Value *GetFunctionMask();
100 
101  /** Returns the mask value corresponding to "varying" control flow
102  within the current function. (i.e. this doesn't include the effect
103  of the mask at function entry. */
104  llvm::Value *GetInternalMask();
105 
106  /** Returns the complete current mask value--i.e. the logical AND of
107  the function entry mask and the internal mask. */
108  llvm::Value *GetFullMask();
109 
110  /** Returns a pointer to storage in memory that stores the current full
111  mask. */
112  llvm::Value *GetFullMaskPointer();
113 
114  /** Provides the value of the mask at function entry */
115  void SetFunctionMask(llvm::Value *val);
116 
117  /** Sets the internal mask to a new value */
118  void SetInternalMask(llvm::Value *val);
119 
120  /** Sets the internal mask to (oldMask & val) */
121  void SetInternalMaskAnd(llvm::Value *oldMask, llvm::Value *val);
122 
123  /** Sets the internal mask to (oldMask & ~val) */
124  void SetInternalMaskAndNot(llvm::Value *oldMask, llvm::Value *test);
125 
126  /** Emits a branch instruction to the basic block btrue if any of the
127  lanes of current mask are on and bfalse if none are on. */
128  void BranchIfMaskAny(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse);
129 
130  /** Emits a branch instruction to the basic block btrue if all of the
131  lanes of current mask are on and bfalse if none are on. */
132  void BranchIfMaskAll(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse);
133 
134  /** Emits a branch instruction to the basic block btrue if none of the
135  lanes of current mask are on and bfalse if none are on. */
136  void BranchIfMaskNone(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse);
137  /** @} */
138 
139  /** @name Control flow management
140  @{
141  */
142  /** Notifies the FunctionEmitContext that we're starting emission of an
143  'if' statement with a uniform test. */
144  void StartUniformIf();
145 
146  /** Notifies the FunctionEmitContext that we're starting emission of an
147  'if' statement with a varying test. The value of the mask going
148  into the 'if' statement is provided in the oldMask parameter. */
149  void StartVaryingIf(llvm::Value *oldMask);
150 
151  /** Notifies the FunctionEmitConitext that we're done emitting the IR
152  for an 'if' statement. */
153  void EndIf();
154 
155  /** Notifies the FunctionEmitContext that we're starting to emit IR
156  for a loop. Basic blocks are provides for where 'break' and
157  'continue' statements should jump to (if all running lanes want to
158  break or continue), uniformControlFlow indicates whether the loop
159  condition is 'uniform'. */
160  void StartLoop(llvm::BasicBlock *breakTarget, llvm::BasicBlock *continueTarget, bool uniformControlFlow);
161 
162  /** Informs FunctionEmitContext of the value of the mask at the start
163  of a loop body or switch statement. */
164  void SetBlockEntryMask(llvm::Value *mask);
165 
166  /** Informs FunctionEmitContext that code generation for a loop is
167  finished. */
168  void EndLoop();
169 
170  /** Indicates that code generation for a 'foreach', 'foreach_tiled',
171  'foreach_active', or 'foreach_unique' loop is about to start. */
173  void StartForeach(ForeachType ft);
174  void EndForeach();
175 
176  /** Emit code for a 'break' statement in a loop. If doCoherenceCheck
177  is true, then if we're in a 'varying' loop, code will be emitted to
178  see if all of the lanes want to break, in which case a jump to the
179  break target will be taken. (For 'uniform' loops, the jump is
180  always done). */
181  void Break(bool doCoherenceCheck);
182 
183  /** Emit code for a 'continue' statement in a loop. If
184  doCoherenceCheck is true, then if we're in a 'varying' loop, code
185  will be emitted to see if all of the lanes want to continue, in
186  which case a jump to the continue target will be taken. (For
187  'uniform' loops, the jump is always done). */
188  void Continue(bool doCoherenceCheck);
189 
190  /** This method is called by code emitting IR for a loop at the end of
191  the loop body; it restores the lanes of the mask that executed a
192  'continue' statement when going through the loop body in the
193  previous iteration. */
194  void RestoreContinuedLanes();
195 
196  /** This method is called by code emitting IR for a loop. It clears
197  any lanes that contained a break since the mask has been updated to take
198  them into account. This is necessary as all the bail out checks for
199  breaks are meant to only deal with lanes breaking on the current iteration.
200  */
201  void ClearBreakLanes();
202 
203  /** Indicates that code generation for a "switch" statement is about to
204  start. isUniform indicates whether the "switch" value is uniform,
205  and bbAfterSwitch gives the basic block immediately following the
206  "switch" statement. (For example, if the switch condition is
207  uniform, we jump here upon executing a "break" statement.) */
208  void StartSwitch(bool isUniform, llvm::BasicBlock *bbAfterSwitch);
209  /** Indicates the end of code generation for a "switch" statement. */
210  void EndSwitch();
211 
212  /** Emits code for a "switch" statement in the program.
213  @param expr Gives the value of the expression after the "switch"
214  @param defaultBlock Basic block to execute for the "default" case. This
215  should be NULL if there is no "default" label inside
216  the switch.
217  @param caseBlocks vector that stores the mapping from label values
218  after "case" statements to basic blocks corresponding
219  to the "case" labels.
220  @param nextBlocks For each basic block for a "case" or "default"
221  label, this gives the basic block for the
222  immediately-following "case" or "default" label (or
223  the basic block after the "switch" statement for the
224  last label.)
225  */
226  void SwitchInst(llvm::Value *expr, llvm::BasicBlock *defaultBlock,
227  const std::vector<std::pair<int, llvm::BasicBlock *>> &caseBlocks,
228  const std::map<llvm::BasicBlock *, llvm::BasicBlock *> &nextBlocks);
229 
230  /** Generates code for a "default" label after a "switch" statement.
231  The checkMask parameter indicates whether additional code should be
232  generated to check to see if the execution mask is all off after
233  the default label (in which case a jump to the following label will
234  be issued. */
235  void EmitDefaultLabel(bool checkMask, SourcePos pos);
236 
237  /** Generates code for a "case" label after a "switch" statement. See
238  the documentation for EmitDefaultLabel() for discussion of the
239  checkMask parameter. */
240  void EmitCaseLabel(int value, bool checkMask, SourcePos pos);
241 
242  /** Returns the current number of nested levels of 'varying' control
243  flow */
244  int VaryingCFDepth() const;
245 
246  bool InForeachLoop() const;
247 
248  /** Temporarily disables emission of performance warnings from gathers
249  and scatters from subsequent code. */
251 
252  /** Reenables emission of gather/scatter performance warnings. */
254 
255  void SetContinueTarget(llvm::BasicBlock *bb) { continueTarget = bb; }
256 
257  /** Step through the code and find label statements; create a basic
258  block for each one, so that subsequent calls to
259  GetLabeledBasicBlock() return the corresponding basic block. */
260  void InitializeLabelMap(Stmt *code);
261 
262  /** If there is a label in the function with the given name, return the
263  new basic block that it starts. */
264  llvm::BasicBlock *GetLabeledBasicBlock(const std::string &label);
265 
266  /** Returns a vector of all labels in the context. This is
267  simply the key set of the labelMap */
268  std::vector<std::string> GetLabels();
269 
270  /** Called to generate code for 'return' statement; value is the
271  expression in the return statement (if non-NULL), and
272  doCoherenceCheck indicates whether instructions should be generated
273  to see if all of the currently-running lanes have returned (if
274  we're under varying control flow). */
275  void CurrentLanesReturned(Expr *value, bool doCoherenceCheck);
276  /** @} */
277 
278  /** @name Small helper/utility routines
279  @{
280  */
281  /** Given a boolean mask value of type LLVMTypes::MaskType, return an
282  i1 value that indicates if any of the mask lanes are on. */
283  llvm::Value *Any(llvm::Value *mask);
284 
285  /** Given a boolean mask value of type LLVMTypes::MaskType, return an
286  i1 value that indicates if all of the mask lanes are on. */
287  llvm::Value *All(llvm::Value *mask);
288 
289  /** Given a boolean mask value of type LLVMTypes::MaskType, return an
290  i1 value that indicates if all of the mask lanes are off. */
291  llvm::Value *None(llvm::Value *mask);
292 
293  /** Given a boolean mask value of type LLVMTypes::MaskType, return an
294  i64 value wherein the i'th bit is on if and only if the i'th lane
295  of the mask is on. */
296  llvm::Value *LaneMask(llvm::Value *mask);
297 
298  /** Given two masks of type LLVMTypes::MaskType, return an i1 value
299  that indicates whether the two masks are equal. */
300  llvm::Value *MasksAllEqual(llvm::Value *mask1, llvm::Value *mask2);
301 
302  /** generate constantvector, which contains programindex, i.e.
303  < i32 0, i32 1, i32 2, i32 3> */
304  llvm::Value *ProgramIndexVector(bool is32bits = true);
305 #ifdef ISPC_NVPTX_ENABLED
306  llvm::Value *ProgramIndexVectorPTX(bool is32bits = true);
307 
308  /** Issues a call to __insert_int8/int16/int32/int64/float/double */
309  llvm::Value *Insert(llvm::Value *vector, llvm::Value *lane, llvm::Value *scalar);
310  /** Issues a call to __extract_int8/int16/int32/int64/float/double */
311  llvm::Value *Extract(llvm::Value *vector, llvm::Value *lane);
312 #endif
313 
314  /** Given a string, create an anonymous global variable to hold its
315  value and return the pointer to the string. */
316  llvm::Value *GetStringPtr(const std::string &str);
317 
318  /** Create a new basic block with given name */
319  llvm::BasicBlock *CreateBasicBlock(const char *name);
320 
321  /** Given a vector with element type i1, return a vector of type
322  LLVMTypes::BoolVectorType. This method handles the conversion for
323  the targets where the bool vector element type is, for example,
324  i32. */
325  llvm::Value *I1VecToBoolVec(llvm::Value *b);
326 
327  /** If the user has asked to compile the program with instrumentation,
328  this inserts a callback to the user-supplied instrumentation
329  function at the current point in the code. */
330  void AddInstrumentationPoint(const char *note);
331  /** @} */
332 
333  /** @name Debugging support
334  @{
335  */
336  /** Set the current source file position; subsequent emitted
337  instructions will have this position associated with them if
338  debugging information is being generated. */
339  void SetDebugPos(SourcePos pos);
340 
341  SourcePos GetDebugPos() const;
342 
343  /** Adds debugging metadata to the given instruction. If pos == NULL,
344  use FunctionEmitContext::currentPos as the source file position for
345  the instruction. Similarly, if a DIScope is provided, it's used
346  and otherwise the scope is found from a GetDIScope() call. This
347  takes a llvm::Value for the instruction rather than an
348  llvm::Instruction for convenience; in calling code we often have
349  Instructions stored using Value pointers; the code here returns
350  silently if it's not actually given an instruction. */
351  void AddDebugPos(llvm::Value *instruction, const SourcePos *pos = NULL,
352 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
353  llvm::DIScope *scope = NULL);
354 #else /* LLVM 3.7+ */
355  llvm::DIScope *scope = NULL);
356  // llvm::MDScope *scope = NULL );
357 #endif
358 
359  /** Inform the debugging information generation code that a new scope
360  is starting in the source program. */
361  void StartScope();
362 
363  /** Inform the debugging information generation code that the current
364  scope is ending in the source program. */
365  void EndScope();
366 
367  /** Returns the llvm::DIScope corresponding to the current program
368  scope. */
369 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
370  llvm::DIScope GetDIScope() const;
371 #else // LLVM 3.7++
372  llvm::DIScope *GetDIScope() const;
373 #endif
374 
375  /** Emits debugging information for the variable represented by
376  sym. */
377  void EmitVariableDebugInfo(Symbol *sym);
378 
379  /** Emits debugging information for the function parameter represented
380  by sym. */
381  void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum);
382  /** @} */
383 
384  /** @name IR instruction emission
385  @brief These methods generally closely correspond to LLVM IR
386  instructions. See the LLVM assembly language reference manual
387  (http://llvm.org/docs/LangRef.html) and the LLVM doxygen documentaion
388  (http://llvm.org/doxygen) for more information. Here we will only
389  document significant generalizations to the functionality of the
390  corresponding basic LLVM instructions.
391 
392  Beyond actually emitting the instruction, the implementations of
393  these methods in FunctionEmitContext also handle adding debugging
394  metadata if debugging symbols are enabled, adding the instructions
395  to the current basic block, and handling generalizations like
396  'varying' lvalues, arithmetic operations with VectorType operands,
397  etc.
398  @{
399  */
400  /** Emit the binary operator given by the inst parameter. If
401  llvm::Values corresponding to VectorTypes are given as operands,
402  this also handles applying the given operation to the vector
403  elements. */
404  llvm::Value *BinaryOperator(llvm::Instruction::BinaryOps inst, llvm::Value *v0, llvm::Value *v1,
405  const char *name = NULL);
406 
407  /** Emit the "not" operator. Like BinaryOperator(), this also handles
408  a VectorType-based operand. */
409  llvm::Value *NotOperator(llvm::Value *v, const char *name = NULL);
410 
411  /** Emit a comparison instruction. If the operands are VectorTypes,
412  then a value for the corresponding boolean VectorType is
413  returned. */
414  llvm::Value *CmpInst(llvm::Instruction::OtherOps inst, llvm::CmpInst::Predicate pred, llvm::Value *v0,
415  llvm::Value *v1, const char *name = NULL);
416 
417  /** Given a scalar value, return a vector of the same type (or an
418  array, for pointer types). */
419  llvm::Value *SmearUniform(llvm::Value *value, const char *name = NULL);
420 
421  llvm::Value *BitCastInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
422  llvm::Value *PtrToIntInst(llvm::Value *value, const char *name = NULL);
423  llvm::Value *PtrToIntInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
424  llvm::Value *IntToPtrInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
425 
426  llvm::Instruction *TruncInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
427  llvm::Instruction *CastInst(llvm::Instruction::CastOps op, llvm::Value *value, llvm::Type *type,
428  const char *name = NULL);
429  llvm::Instruction *FPCastInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
430  llvm::Instruction *SExtInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
431  llvm::Instruction *ZExtInst(llvm::Value *value, llvm::Type *type, const char *name = NULL);
432 
433  /** Given two integer-typed values (but possibly one vector and the
434  other not, and or of possibly-different bit-widths), update their
435  values as needed so that the two have the same (more general)
436  type. */
437  void MatchIntegerTypes(llvm::Value **v0, llvm::Value **v1);
438 
439  /** Create a new slice pointer out of the given pointer to an soa type
440  and an integer offset to a slice within that type. */
441  llvm::Value *MakeSlicePointer(llvm::Value *ptr, llvm::Value *offset);
442 
443  /** These GEP methods are generalizations of the standard ones in LLVM;
444  they support both uniform and varying basePtr values as well as
445  uniform and varying index values (arrays of indices). Varying base
446  pointers are expected to come in as vectors of i32/i64 (depending
447  on the target), since LLVM doesn't currently support vectors of
448  pointers. The underlying type of the base pointer must be provided
449  via the ptrType parameter */
450  llvm::Value *GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index, const Type *ptrType,
451  const char *name = NULL);
452  llvm::Value *GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index0, llvm::Value *index1, const Type *ptrType,
453  const char *name = NULL);
454 
455  /** This method returns a new pointer that represents offsetting the
456  given base pointer to point at the given element number of the
457  structure type that the base pointer points to. (The provided
458  pointer must be a pointer to a structure type. The ptrType gives
459  the type of the pointer, though it may be NULL if the base pointer
460  is uniform. */
461  llvm::Value *AddElementOffset(llvm::Value *basePtr, int elementNum, const Type *ptrType, const char *name = NULL,
462  const PointerType **resultPtrType = NULL);
463 
464  /** Load from the memory location(s) given by lvalue, using the given
465  mask. The lvalue may be varying, in which case this corresponds to
466  a gather from the multiple memory locations given by the array of
467  pointer values given by the lvalue. If the lvalue is not varying,
468  then both the mask pointer and the type pointer may be NULL. */
469  llvm::Value *LoadInst(llvm::Value *ptr, llvm::Value *mask, const Type *ptrType, const char *name = NULL,
470  bool one_elem = false);
471 
472  llvm::Value *LoadInst(llvm::Value *ptr, const char *name = NULL);
473 
474  /** Emits an alloca instruction to allocate stack storage for the given
475  type. If a non-zero alignment is specified, the object is also
476  allocated at the given alignment. By default, the alloca
477  instruction is added at the start of the function in the entry
478  basic block; if it should be added to the current basic block, then
479  the atEntryBlock parameter should be false. */
480  llvm::Value *AllocaInst(llvm::Type *llvmType, const char *name = NULL, int align = 0, bool atEntryBlock = true);
481 
482  /** Standard store instruction; for this variant, the lvalue must be a
483  single pointer, not a varying lvalue. */
484  void StoreInst(llvm::Value *value, llvm::Value *ptr);
485 
486  /** In this variant of StoreInst(), the lvalue may be varying. If so,
487  this corresponds to a scatter. Whether the lvalue is uniform of
488  varying, the given storeMask is used to mask the stores so that
489  they only execute for the active program instances. */
490  void StoreInst(llvm::Value *value, llvm::Value *ptr, llvm::Value *storeMask, const Type *valueType,
491  const Type *ptrType);
492 
493  /** Copy count bytes of memory from the location pointed to by src to
494  the location pointed to by dest. (src and dest must not be
495  overlapping.) */
496  void MemcpyInst(llvm::Value *dest, llvm::Value *src, llvm::Value *count, llvm::Value *align = NULL);
497 
498  void BranchInst(llvm::BasicBlock *block);
499  void BranchInst(llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock, llvm::Value *test);
500 
501  /** This convenience method maps to an llvm::ExtractElementInst if the
502  given value is a llvm::VectorType, and to an llvm::ExtractValueInst
503  otherwise. */
504  llvm::Value *ExtractInst(llvm::Value *v, int elt, const char *name = NULL);
505 
506  /** This convenience method maps to an llvm::InsertElementInst if the
507  given value is a llvm::VectorType, and to an llvm::InsertValueInst
508  otherwise. */
509  llvm::Value *InsertInst(llvm::Value *v, llvm::Value *eltVal, int elt, const char *name = NULL);
510 
511  /** This convenience method maps to an llvm::ShuffleVectorInst. */
512  llvm::Value *ShuffleInst(llvm::Value *v1, llvm::Value *v2, llvm::Value *mask, const char *name = NULL);
513 
514  /** This convenience method to generate broadcast pattern. It takes a value
515  and a vector type. Type of the value must match element type of the
516  vector. */
517  llvm::Value *BroadcastValue(llvm::Value *v, llvm::Type *vecType, const char *name = NULL);
518 
519  llvm::PHINode *PhiNode(llvm::Type *type, int count, const char *name = NULL);
520  llvm::Instruction *SelectInst(llvm::Value *test, llvm::Value *val0, llvm::Value *val1, const char *name = NULL);
521 
522  /** Emits IR to do a function call with the given arguments. If the
523  function type is a varying function pointer type, its full type
524  must be provided in funcType. funcType can be NULL if func is a
525  uniform function pointer. */
526  llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, const std::vector<llvm::Value *> &args,
527  const char *name = NULL);
528 
529  /** This is a convenience method that issues a call instruction to a
530  function that takes just a single argument. */
531  llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, llvm::Value *arg, const char *name = NULL);
532 
533  /** This is a convenience method that issues a call instruction to a
534  function that takes two arguments. */
535  llvm::Value *CallInst(llvm::Value *func, const FunctionType *funcType, llvm::Value *arg0, llvm::Value *arg1,
536  const char *name = NULL);
537 
538  /** Launch an asynchronous task to run the given function, passing it
539  he given argument values. */
540  llvm::Value *LaunchInst(llvm::Value *callee, std::vector<llvm::Value *> &argVals, llvm::Value *launchCount[3]);
541 
542  void SyncInst();
543 
544  llvm::Instruction *ReturnInst();
545  /** @} */
546 
547  private:
548  /** Pointer to the Function for which we're currently generating code. */
549  Function *function;
550 
551  /** LLVM function representation for the current function. */
552  llvm::Function *llvmFunction;
553 
554  /** The basic block into which we add any alloca instructions that need
555  to go at the very start of the function. */
556  llvm::BasicBlock *allocaBlock;
557 
558  /** The current basic block into which we're emitting new
559  instructions */
560  llvm::BasicBlock *bblock;
561 
562  /** Pointer to stack-allocated memory that stores the current value of
563  the full program mask. */
564  llvm::Value *fullMaskPointer;
565 
566  /** Pointer to stack-allocated memory that stores the current value of
567  the program mask representing varying control flow within the
568  function. */
569  llvm::Value *internalMaskPointer;
570 
571  /** Value of the program mask when the function starts execution. */
572  llvm::Value *functionMaskValue;
573 
574  /** Current source file position; if debugging information is being
575  generated, this position is used to set file/line information for
576  instructions. */
578 
579  /** Source file position where the function definition started. Used
580  for error messages and debugging symbols. */
582 
583  /** If currently in a loop body or switch statement, the value of the
584  mask at the start of it. */
585  llvm::Value *blockEntryMask;
586 
587  /** If currently in a loop body or switch statement, this is a pointer
588  to memory to store a mask value that represents which of the lanes
589  have executed a 'break' statement. If we're not in a loop body or
590  switch, this should be NULL. */
591  llvm::Value *breakLanesPtr;
592 
593  /** Similar to breakLanesPtr, if we're inside a loop, this is a pointer
594  to memory to record which of the program instances have executed a
595  'continue' statement. */
596  llvm::Value *continueLanesPtr;
597 
598  /** If we're inside a loop or switch statement, this gives the basic
599  block immediately after the current loop or switch, which we will
600  jump to if all of the lanes have executed a break statement or are
601  otherwise done with it. */
602  llvm::BasicBlock *breakTarget;
603 
604  /** If we're inside a loop, this gives the block to jump to if all of
605  the running lanes have executed a 'continue' statement. */
606  llvm::BasicBlock *continueTarget;
607 
608  /** @name Switch statement state
609 
610  These variables store various state that's active when we're
611  generating code for a switch statement. They should all be NULL
612  outside of a switch.
613  @{
614  */
615 
616  /** The value of the expression used to determine which case in the
617  statements after the switch to execute. */
618  llvm::Value *switchExpr;
619 
620  /** Map from case label numbers to the basic block that will hold code
621  for that case. */
622  const std::vector<std::pair<int, llvm::BasicBlock *>> *caseBlocks;
623 
624  /** The basic block of code to run for the "default" label in the
625  switch statement. */
626  llvm::BasicBlock *defaultBlock;
627 
628  /** For each basic block for the code for cases (and the default label,
629  if present), this map gives the basic block for the immediately
630  following case/default label. */
631  const std::map<llvm::BasicBlock *, llvm::BasicBlock *> *nextBlocks;
632 
633  /** Records whether the switch condition was uniform; this is a
634  distinct notion from whether the switch represents uniform or
635  varying control flow; we may have varying control flow from a
636  uniform switch condition if there is a 'break' inside the switch
637  that's under varying control flow. */
639  /** @} */
640 
641  /** A pointer to memory that records which of the program instances
642  have executed a 'return' statement (and are thus really truly done
643  running any more instructions in this functions. */
644  llvm::Value *returnedLanesPtr;
645 
646  /** A pointer to memory to store the return value for the function.
647  Since difference program instances may execute 'return' statements
648  at different times, we need to accumulate the return values as they
649  come in until we return for real. */
650  llvm::Value *returnValuePtr;
651 
652  /** The CFInfo structure records information about a nesting level of
653  control flow. This vector lets us see what control flow is going
654  around outside the current position in the function being
655  emitted. */
656  std::vector<CFInfo *> controlFlowInfo;
657 
658 #if ISPC_LLVM_VERSION <= ISPC_LLVM_3_6
659  /** DIFile object corresponding to the source file where the current
660  function was defined (used for debugging info). */
661  llvm::DIFile diFile;
662 
663  /** DISubprogram corresponding to this function (used for debugging
664  info). */
665  llvm::DISubprogram diSubprogram;
666 
667  /** These correspond to the current set of nested scopes in the
668  function. */
669  std::vector<llvm::DILexicalBlock> debugScopes;
670 #else // LLVM 3.7++
671  /** DIFile object corresponding to the source file where the current
672  function was defined (used for debugging info). */
673  llvm::DIFile *diFile;
674 
675  /** DISubprogram corresponding to this function (used for debugging
676  info). */
677  llvm::DISubprogram *diSubprogram;
678 
679  /** These correspond to the current set of nested scopes in the
680  function. */
681  std::vector<llvm::DIScope *> debugScopes;
682 #endif
683 
684  /** True if a 'launch' statement has been encountered in the function. */
686 
687  /** This is a pointer to a void * that is passed to the ISPCLaunch(),
688  ISPCAlloc(), and ISPCSync() routines as a handle to the group ot
689  tasks launched from the current function. */
690  llvm::Value *launchGroupHandlePtr;
691 
692  /** Nesting count of the number of times calling code has disabled (and
693  not yet reenabled) gather/scatter performance warnings. */
695 
696  std::map<std::string, llvm::BasicBlock *> labelMap;
697 
698  static bool initLabelBBlocks(ASTNode *node, void *data);
699 
700  llvm::Value *pointerVectorToVoidPointers(llvm::Value *value);
701  static void addGSMetadata(llvm::Value *inst, SourcePos pos);
702  bool ifsInCFAllUniform(int cfType) const;
703  void jumpIfAllLoopLanesAreDone(llvm::BasicBlock *target);
704  llvm::Value *emitGatherCallback(llvm::Value *lvalue, llvm::Value *retPtr);
705 
706  llvm::Value *applyVaryingGEP(llvm::Value *basePtr, llvm::Value *index, const Type *ptrType);
707 
708  void restoreMaskGivenReturns(llvm::Value *oldMask);
709  void addSwitchMaskCheck(llvm::Value *mask);
710  bool inSwitchStatement() const;
711  llvm::Value *getMaskAtSwitchEntry();
712 
713  CFInfo *popCFState();
714 
715  void scatter(llvm::Value *value, llvm::Value *ptr, const Type *valueType, const Type *ptrType, llvm::Value *mask);
716  void maskedStore(llvm::Value *value, llvm::Value *ptr, const Type *ptrType, llvm::Value *mask);
717  void storeUniformToSOA(llvm::Value *value, llvm::Value *ptr, llvm::Value *mask, const Type *valueType,
718  const PointerType *ptrType);
719  llvm::Value *loadUniformFromSOA(llvm::Value *ptr, llvm::Value *mask, const PointerType *ptrType, const char *name);
720 
721  llvm::Value *gather(llvm::Value *ptr, const PointerType *ptrType, llvm::Value *mask, const char *name);
722 
723  llvm::Value *addVaryingOffsetsIfNeeded(llvm::Value *ptr, const Type *ptrType);
724 };
725 
726 #endif // ISPC_CTX_H
const Function * GetFunction() const
Definition: ctx.cpp:421
llvm::Value * Any(llvm::Value *mask)
Definition: ctx.cpp:1187
llvm::Value * pointerVectorToVoidPointers(llvm::Value *value)
void InitializeLabelMap(Stmt *code)
Definition: ctx.cpp:1095
llvm::Value * PtrToIntInst(llvm::Value *value, const char *name=NULL)
Definition: ctx.cpp:1752
void jumpIfAllLoopLanesAreDone(llvm::BasicBlock *target)
Definition: ctx.cpp:783
Definition: func.h:44
std::vector< CFInfo * > controlFlowInfo
Definition: ctx.h:656
llvm::Value * AddElementOffset(llvm::Value *basePtr, int elementNum, const Type *ptrType, const char *name=NULL, const PointerType **resultPtrType=NULL)
Definition: ctx.cpp:2179
CFInfo * popCFState()
Definition: ctx.cpp:3523
void StartUniformIf()
Definition: ctx.cpp:489
void BranchIfMaskNone(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse)
Definition: ctx.cpp:480
SourcePos currentPos
Definition: ctx.h:577
bool InForeachLoop() const
Definition: ctx.cpp:1068
void StartSwitch(bool isUniform, llvm::BasicBlock *bbAfterSwitch)
Definition: ctx.cpp:848
llvm::Value * ShuffleInst(llvm::Value *v1, llvm::Value *v2, llvm::Value *mask, const char *name=NULL)
Definition: ctx.cpp:3018
llvm::Value * ProgramIndexVector(bool is32bits=true)
Definition: ctx.cpp:1333
void SetInternalMask(llvm::Value *val)
Definition: ctx.cpp:445
void StartLoop(llvm::BasicBlock *breakTarget, llvm::BasicBlock *continueTarget, bool uniformControlFlow)
Definition: ctx.cpp:551
llvm::Instruction * FPCastInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1859
void EmitVariableDebugInfo(Symbol *sym)
Definition: ctx.cpp:1506
void StartScope()
Definition: ctx.cpp:1455
void SetInternalMaskAnd(llvm::Value *oldMask, llvm::Value *val)
Definition: ctx.cpp:451
llvm::Value * functionMaskValue
Definition: ctx.h:572
void BranchInst(llvm::BasicBlock *block)
Definition: ctx.cpp:2961
void maskedStore(llvm::Value *value, llvm::Value *ptr, const Type *ptrType, llvm::Value *mask)
Definition: ctx.cpp:2629
llvm::Instruction * ZExtInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1891
Interface class for statements in the ispc language.
Definition: stmt.h:49
FunctionEmitContext(Function *function, Symbol *funSym, llvm::Function *llvmFunction, SourcePos firstStmtPos)
Definition: ctx.cpp:211
llvm::Value * NotOperator(llvm::Value *v, const char *name=NULL)
Definition: ctx.cpp:1637
llvm::Value * LoadInst(llvm::Value *ptr, llvm::Value *mask, const Type *ptrType, const char *name=NULL, bool one_elem=false)
Definition: ctx.cpp:2367
void BranchIfMaskAll(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse)
Definition: ctx.cpp:471
std::vector< std::string > GetLabels()
Definition: ctx.cpp:1107
llvm::Instruction * TruncInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1826
llvm::Value * AllocaInst(llvm::Type *llvmType, const char *name=NULL, int align=0, bool atEntryBlock=true)
Definition: ctx.cpp:2579
llvm::BasicBlock * allocaBlock
Definition: ctx.h:556
llvm::Value * CmpInst(llvm::Instruction::OtherOps inst, llvm::CmpInst::Predicate pred, llvm::Value *v0, llvm::Value *v1, const char *name=NULL)
Definition: ctx.cpp:1679
void EndSwitch()
Definition: ctx.cpp:869
void StartVaryingIf(llvm::Value *oldMask)
Definition: ctx.cpp:491
llvm::Value * LaunchInst(llvm::Value *callee, std::vector< llvm::Value *> &argVals, llvm::Value *launchCount[3])
Definition: ctx.cpp:3304
void SetContinueTarget(llvm::BasicBlock *bb)
Definition: ctx.h:255
llvm::Value * launchGroupHandlePtr
Definition: ctx.h:690
bool ifsInCFAllUniform(int cfType) const
Definition: ctx.cpp:767
void addSwitchMaskCheck(llvm::Value *mask)
Definition: ctx.cpp:880
void StartForeach(ForeachType ft)
Definition: ctx.cpp:589
llvm::Value * breakLanesPtr
Definition: ctx.h:591
llvm::Value * emitGatherCallback(llvm::Value *lvalue, llvm::Value *retPtr)
llvm::Value * switchExpr
Definition: ctx.h:618
void Continue(bool doCoherenceCheck)
Definition: ctx.cpp:724
llvm::Value * GetFullMask()
Definition: ctx.cpp:431
int VaryingCFDepth() const
Definition: ctx.cpp:1060
void AddInstrumentationPoint(const char *note)
Definition: ctx.cpp:1413
llvm::Value * MakeSlicePointer(llvm::Value *ptr, llvm::Value *offset)
Definition: ctx.cpp:2030
llvm::Value * gather(llvm::Value *ptr, const PointerType *ptrType, llvm::Value *mask, const char *name)
Definition: ctx.cpp:2450
bool launchedTasks
Definition: ctx.h:685
void restoreMaskGivenReturns(llvm::Value *oldMask)
Definition: ctx.cpp:625
Type implementation for pointers to other types.
Definition: type.h:430
void BranchIfMaskAny(llvm::BasicBlock *btrue, llvm::BasicBlock *bfalse)
Definition: ctx.cpp:462
void RestoreContinuedLanes()
Definition: ctx.cpp:826
llvm::Value * returnValuePtr
Definition: ctx.h:650
llvm::Value * loadUniformFromSOA(llvm::Value *ptr, llvm::Value *mask, const PointerType *ptrType, const char *name)
Definition: ctx.cpp:2338
llvm::BasicBlock * GetCurrentBasicBlock()
Definition: ctx.cpp:423
void Break(bool doCoherenceCheck)
Definition: ctx.cpp:653
SourcePos funcStartPos
Definition: ctx.h:581
llvm::Value * CallInst(llvm::Value *func, const FunctionType *funcType, const std::vector< llvm::Value *> &args, const char *name=NULL)
Definition: ctx.cpp:3112
llvm::BasicBlock * CreateBasicBlock(const char *name)
Definition: ctx.cpp:1368
const std::map< llvm::BasicBlock *, llvm::BasicBlock * > * nextBlocks
Definition: ctx.h:631
llvm::Value * BroadcastValue(llvm::Value *v, llvm::Type *vecType, const char *name=NULL)
Definition: ctx.cpp:3036
static void addGSMetadata(llvm::Value *inst, SourcePos pos)
Definition: ctx.cpp:2534
llvm::Value * internalMaskPointer
Definition: ctx.h:569
void StoreInst(llvm::Value *value, llvm::Value *ptr)
Definition: ctx.cpp:2827
llvm::BasicBlock * defaultBlock
Definition: ctx.h:626
void EmitCaseLabel(int value, bool checkMask, SourcePos pos)
Definition: ctx.cpp:966
void EndLoop()
Definition: ctx.cpp:576
bool switchConditionWasUniform
Definition: ctx.h:638
llvm::Value * GetFunctionMask()
Definition: ctx.cpp:427
void AddDebugPos(llvm::Value *instruction, const SourcePos *pos=NULL, llvm::DIScope *scope=NULL)
Definition: ctx.cpp:1436
SourcePos GetDebugPos() const
Definition: ctx.cpp:1434
Abstract base class for nodes in the abstract syntax tree (AST).
Definition: ast.h:50
llvm::Value * GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index, const Type *ptrType, const char *name=NULL)
Definition: ctx.cpp:2045
void CurrentLanesReturned(Expr *value, bool doCoherenceCheck)
Definition: ctx.cpp:1119
llvm::BasicBlock * continueTarget
Definition: ctx.h:606
llvm::Value * LaneMask(llvm::Value *mask)
Definition: ctx.cpp:1234
void MemcpyInst(llvm::Value *dest, llvm::Value *src, llvm::Value *count, llvm::Value *align=NULL)
Definition: ctx.cpp:2915
llvm::DIScope GetDIScope() const
Definition: ctx.cpp:1501
llvm::PHINode * PhiNode(llvm::Type *type, int count, const char *name=NULL)
Definition: ctx.cpp:3071
llvm::Value * addVaryingOffsetsIfNeeded(llvm::Value *ptr, const Type *ptrType)
Definition: ctx.cpp:3491
llvm::DISubprogram diSubprogram
Definition: ctx.h:665
llvm::Value * getMaskAtSwitchEntry()
Definition: ctx.cpp:898
void MatchIntegerTypes(llvm::Value **v0, llvm::Value **v1)
Definition: ctx.cpp:1966
void SetBlockEntryMask(llvm::Value *mask)
Definition: ctx.cpp:443
std::vector< llvm::DILexicalBlock > debugScopes
Definition: ctx.h:669
Representation of a range of positions in a source file.
Definition: ispc.h:129
void ClearBreakLanes()
Definition: ctx.cpp:840
llvm::Value * continueLanesPtr
Definition: ctx.h:596
llvm::Value * None(llvm::Value *mask)
Definition: ctx.cpp:1218
llvm::Instruction * SExtInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1875
int disableGSWarningCount
Definition: ctx.h:694
void SwitchInst(llvm::Value *expr, llvm::BasicBlock *defaultBlock, const std::vector< std::pair< int, llvm::BasicBlock *>> &caseBlocks, const std::map< llvm::BasicBlock *, llvm::BasicBlock *> &nextBlocks)
Definition: ctx.cpp:1013
llvm::Function * llvmFunction
Definition: ctx.h:552
llvm::Value * GetStringPtr(const std::string &str)
Definition: ctx.cpp:1360
void storeUniformToSOA(llvm::Value *value, llvm::Value *ptr, llvm::Value *mask, const Type *valueType, const PointerType *ptrType)
Definition: ctx.cpp:2892
llvm::Value * InsertInst(llvm::Value *v, llvm::Value *eltVal, int elt, const char *name=NULL)
Definition: ctx.cpp:2997
llvm::Value * fullMaskPointer
Definition: ctx.h:564
void DisableGatherScatterWarnings()
Definition: ctx.cpp:1075
llvm::Value * returnedLanesPtr
Definition: ctx.h:644
void SetDebugPos(SourcePos pos)
Definition: ctx.cpp:1432
Definition: ctx.cpp:73
llvm::Value * GetFullMaskPointer()
Definition: ctx.cpp:435
llvm::Value * GetInternalMask()
Definition: ctx.cpp:429
llvm::Value * BitCastInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1738
void SetInternalMaskAndNot(llvm::Value *oldMask, llvm::Value *test)
Definition: ctx.cpp:456
Type representing a function (return type + argument types)
Definition: type.h:858
Representation of a program symbol.
Definition: sym.h:63
llvm::Value * ExtractInst(llvm::Value *v, int elt, const char *name=NULL)
Definition: ctx.cpp:2976
void EndForeach()
Definition: ctx.cpp:620
void EnableGatherScatterWarnings()
Definition: ctx.cpp:1077
Interface class that defines the type abstraction.
Definition: type.h:95
static bool initLabelBBlocks(ASTNode *node, void *data)
Definition: ctx.cpp:1079
const std::vector< std::pair< int, llvm::BasicBlock * > > * caseBlocks
Definition: ctx.h:622
void SetCurrentBasicBlock(llvm::BasicBlock *bblock)
Definition: ctx.cpp:425
void EmitDefaultLabel(bool checkMask, SourcePos pos)
Definition: ctx.cpp:907
llvm::Instruction * SelectInst(llvm::Value *test, llvm::Value *val0, llvm::Value *val1, const char *name=NULL)
Definition: ctx.cpp:3077
void EmitFunctionParameterDebugInfo(Symbol *sym, int parameterNum)
Definition: ctx.cpp:1545
llvm::Value * blockEntryMask
Definition: ctx.h:585
Expr is the abstract base class that defines the interface that all expression types must implement...
Definition: expr.h:48
llvm::Value * IntToPtrInst(llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1798
llvm::Value * MasksAllEqual(llvm::Value *mask1, llvm::Value *mask2)
Definition: ctx.cpp:1309
llvm::Value * All(llvm::Value *mask)
Definition: ctx.cpp:1202
bool inSwitchStatement() const
Definition: ctx.cpp:641
llvm::Value * SmearUniform(llvm::Value *value, const char *name=NULL)
Definition: ctx.cpp:1706
llvm::DIFile diFile
Definition: ctx.h:661
Main ispc.header file. Defines Target, Globals and Opt classes.
llvm::BasicBlock * breakTarget
Definition: ctx.h:602
void scatter(llvm::Value *value, llvm::Value *ptr, const Type *valueType, const Type *ptrType, llvm::Value *mask)
Definition: ctx.cpp:2728
llvm::Instruction * CastInst(llvm::Instruction::CastOps op, llvm::Value *value, llvm::Type *type, const char *name=NULL)
Definition: ctx.cpp:1842
llvm::Instruction * ReturnInst()
Definition: ctx.cpp:3283
llvm::Value * applyVaryingGEP(llvm::Value *basePtr, llvm::Value *index, const Type *ptrType)
Definition: ctx.cpp:1913
llvm::Value * BinaryOperator(llvm::Instruction::BinaryOps inst, llvm::Value *v0, llvm::Value *v1, const char *name=NULL)
Definition: ctx.cpp:1608
llvm::BasicBlock * GetLabeledBasicBlock(const std::string &label)
Definition: ctx.cpp:1100
void SetFunctionMask(llvm::Value *val)
Definition: ctx.cpp:437
std::map< std::string, llvm::BasicBlock * > labelMap
Definition: ctx.h:696
llvm::BasicBlock * bblock
Definition: ctx.h:560
llvm::Value * I1VecToBoolVec(llvm::Value *b)
Definition: ctx.cpp:1372