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