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