Talvos  0.1
SPIR-V interpreter and dynamic analysis framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Invocation.h
Go to the documentation of this file.
1 // Copyright (c) 2018 the Talvos developers. All rights reserved.
2 //
3 // This file is distributed under a three-clause BSD license. For full license
4 // terms please see the LICENSE file distributed with this source code.
5 
8 
9 #ifndef TALVOS_INVOCATION_H
10 #define TALVOS_INVOCATION_H
11 
12 #include <vector>
13 
14 #include "talvos/Dim3.h"
15 #include "talvos/Object.h"
16 
17 namespace talvos
18 {
19 
20 class Device;
21 class Function;
22 class Instruction;
23 class Memory;
24 class Module;
25 class PipelineStage;
26 class Workgroup;
27 
34 {
35 public:
38  enum State
39  {
43  };
44 
45 public:
48  Invocation(Device &Dev, const std::vector<Object> &InitialObjects);
49 
53  Invocation(Device &Dev, const PipelineStage &Stage,
54  const std::vector<Object> &InitialObjects,
55  std::shared_ptr<Memory> PipelineMemory, Workgroup *Group,
56  Dim3 GlobalId);
57 
59  ~Invocation();
60 
61  // Do not allow Invocation objects to be copied.
63  Invocation(const Invocation &) = delete;
64  Invocation &operator=(const Invocation &) = delete;
66 
68  void clearBarrier() { AtBarrier = false; }
69 
71  void execute(const Instruction *Inst);
72 
75  {
76  return CurrentInstruction;
77  }
78 
80  Dim3 getGlobalId() const { return GlobalId; }
81 
84  Object getObject(uint32_t Id) const;
85 
87  State getState() const;
88 
90  void step();
91 
93  bool wasDiscarded() const { return Discarded; }
94 
97  void executeAccessChain(const Instruction *Inst);
98  void executeAll(const Instruction *Inst);
99  void executeAny(const Instruction *Inst);
100  void executeAtomicCompareExchange(const Instruction *Inst);
101  template <typename T> void executeAtomicOp(const Instruction *Inst);
102  void executeBitcast(const Instruction *Inst);
103  void executeBitwiseAnd(const Instruction *Inst);
104  void executeBitwiseOr(const Instruction *Inst);
105  void executeBitwiseXor(const Instruction *Inst);
106  void executeBranch(const Instruction *Inst);
107  void executeBranchConditional(const Instruction *Inst);
108  void executeCompositeConstruct(const Instruction *Inst);
109  void executeCompositeExtract(const Instruction *Inst);
110  void executeCompositeInsert(const Instruction *Inst);
111  void executeControlBarrier(const Instruction *Inst);
112  void executeConvertFToS(const Instruction *Inst);
113  void executeConvertFToU(const Instruction *Inst);
114  void executeConvertSToF(const Instruction *Inst);
115  void executeConvertUToF(const Instruction *Inst);
116  void executeCopyMemory(const Instruction *Inst);
117  void executeCopyObject(const Instruction *Inst);
118  void executeDot(const Instruction *Inst);
119  void executeExtInst(const Instruction *Inst);
120  void executeFAdd(const Instruction *Inst);
121  void executeFConvert(const Instruction *Inst);
122  void executeFDiv(const Instruction *Inst);
123  void executeFMod(const Instruction *Inst);
124  void executeFMul(const Instruction *Inst);
125  void executeFNegate(const Instruction *Inst);
126  void executeFOrdEqual(const Instruction *Inst);
127  void executeFOrdGreaterThan(const Instruction *Inst);
128  void executeFOrdGreaterThanEqual(const Instruction *Inst);
129  void executeFOrdLessThan(const Instruction *Inst);
130  void executeFOrdLessThanEqual(const Instruction *Inst);
131  void executeFOrdNotEqual(const Instruction *Inst);
132  void executeFRem(const Instruction *Inst);
133  void executeFSub(const Instruction *Inst);
134  void executeFunctionCall(const Instruction *Inst);
135  void executeFUnordEqual(const Instruction *Inst);
136  void executeFUnordGreaterThan(const Instruction *Inst);
137  void executeFUnordGreaterThanEqual(const Instruction *Inst);
138  void executeFUnordLessThan(const Instruction *Inst);
139  void executeFUnordLessThanEqual(const Instruction *Inst);
140  void executeFUnordNotEqual(const Instruction *Inst);
141  void executeIAdd(const Instruction *Inst);
142  void executeIEqual(const Instruction *Inst);
143  void executeImage(const Instruction *Inst);
144  void executeImageQuerySize(const Instruction *Inst);
145  void executeImageRead(const Instruction *Inst);
146  void executeImageSampleExplicitLod(const Instruction *Inst);
147  void executeImageWrite(const Instruction *Inst);
148  void executeIMul(const Instruction *Inst);
149  void executeINotEqual(const Instruction *Inst);
150  void executeIsInf(const Instruction *Inst);
151  void executeIsNan(const Instruction *Inst);
152  void executeISub(const Instruction *Inst);
153  void executeKill(const Instruction *Inst);
154  void executeLoad(const Instruction *Inst);
155  void executeLogicalAnd(const Instruction *Inst);
156  void executeLogicalEqual(const Instruction *Inst);
157  void executeLogicalNot(const Instruction *Inst);
158  void executeLogicalNotEqual(const Instruction *Inst);
159  void executeLogicalOr(const Instruction *Inst);
160  void executeMatrixTimesScalar(const Instruction *Inst);
161  void executeMatrixTimesVector(const Instruction *Inst);
162  void executeNot(const Instruction *Inst);
163  void executePhi(const Instruction *Inst);
164  void executeReturn(const Instruction *Inst);
165  void executeReturnValue(const Instruction *Inst);
166  void executeSampledImage(const Instruction *Inst);
167  void executeSConvert(const Instruction *Inst);
168  void executeSDiv(const Instruction *Inst);
169  void executeSelect(const Instruction *Inst);
170  void executeSGreaterThan(const Instruction *Inst);
171  void executeSGreaterThanEqual(const Instruction *Inst);
172  void executeShiftLeftLogical(const Instruction *Inst);
173  void executeShiftRightArithmetic(const Instruction *Inst);
174  void executeShiftRightLogical(const Instruction *Inst);
175  void executeSLessThan(const Instruction *Inst);
176  void executeSLessThanEqual(const Instruction *Inst);
177  void executeSMod(const Instruction *Inst);
178  void executeSNegate(const Instruction *Inst);
179  void executeSRem(const Instruction *Inst);
180  void executeStore(const Instruction *Inst);
181  void executeSwitch(const Instruction *Inst);
182  void executeUConvert(const Instruction *Inst);
183  void executeUDiv(const Instruction *Inst);
184  void executeUGreaterThan(const Instruction *Inst);
185  void executeUGreaterThanEqual(const Instruction *Inst);
186  void executeULessThan(const Instruction *Inst);
187  void executeULessThanEqual(const Instruction *Inst);
188  void executeUMod(const Instruction *Inst);
189  void executeUndef(const Instruction *Inst);
190  void executeUnreachable(const Instruction *Inst);
191  void executeVariable(const Instruction *Inst);
192  void executeVectorExtractDynamic(const Instruction *Inst);
193  void executeVectorInsertDynamic(const Instruction *Inst);
194  void executeVectorShuffle(const Instruction *Inst);
195  void executeVectorTimesMatrix(const Instruction *Inst);
196  void executeVectorTimesScalar(const Instruction *Inst);
198 
199 private:
201  std::shared_ptr<const Module> CurrentModule;
202 
205  uint32_t CurrentBlock;
206  uint32_t PreviousBlock;
207  bool AtBarrier;
208  bool Discarded;
209 
211  struct StackEntry
212  {
213  // The instruction, block, and function to return to.
216  uint32_t CallBlock;
217 
219  std::vector<uint64_t> Allocations;
220  };
221 
222  std::vector<StackEntry> CallStack;
223 
224  std::vector<Object> Objects;
225 
230 
232  std::shared_ptr<Memory> PipelineMemory;
233 
235  std::vector<std::pair<uint32_t, Object>> PhiTemps;
236 
244  template <typename OpTy, unsigned N, unsigned Offset = 2, typename F>
245  void executeOp(const Instruction *Inst, const F &Op);
246  template <unsigned N, unsigned Offset = 2, typename F>
247  void executeOpFP(const Instruction *Inst, const F &&Op);
248  template <unsigned N, unsigned Offset = 2, typename F>
249  void executeOpSInt(const Instruction *Inst, const F &&Op);
250  template <unsigned N, unsigned Offset = 2, typename F>
251  void executeOpUInt(const Instruction *Inst, const F &&Op);
253 
255  Memory &getMemory(uint32_t StorageClass);
256 
258  void moveToBlock(uint32_t Id);
259 };
260 
261 } // namespace talvos
262 
263 #endif
void executeOpUInt(const Instruction *Inst, const F &&Op)
void executeSLessThanEqual(const Instruction *Inst)
void executeImageWrite(const Instruction *Inst)
std::vector< std::pair< uint32_t, Object > > PhiTemps
Temporary OpPhi results to be applied when we reach first non-OpPhi.
Definition: Invocation.h:235
void executeCompositeConstruct(const Instruction *Inst)
Definition: Invocation.cpp:503
void executeUndef(const Instruction *Inst)
void executeFRem(const Instruction *Inst)
Definition: Invocation.cpp:832
void executePhi(const Instruction *Inst)
void executeIsInf(const Instruction *Inst)
uint32_t PreviousBlock
The previous block (for OpPhi).
Definition: Invocation.h:206
std::vector< StackEntry > CallStack
The function call stack.
Definition: Invocation.h:222
std::vector< uint64_t > Allocations
Function scope allocations within this stack frame.
Definition: Invocation.h:219
void executeExtInst(const Instruction *Inst)
Definition: Invocation.cpp:663
void executeFAdd(const Instruction *Inst)
Definition: Invocation.cpp:748
void executeOpSInt(const Instruction *Inst, const F &&Op)
A data structure holding information for a function call.
Definition: Invocation.h:211
void executeVariable(const Instruction *Inst)
void executeULessThan(const Instruction *Inst)
State getState() const
Returns the state of this invocation.
bool Discarded
True when fragment was discarded.
Definition: Invocation.h:208
void step()
Step this invocation by executing the next instruction.
void executeCopyObject(const Instruction *Inst)
Definition: Invocation.cpp:631
void executeCompositeInsert(const Instruction *Inst)
Definition: Invocation.cpp:528
void executeULessThanEqual(const Instruction *Inst)
void executeImageQuerySize(const Instruction *Inst)
Definition: Invocation.cpp:926
void executeUMod(const Instruction *Inst)
void executeConvertUToF(const Instruction *Inst)
Definition: Invocation.cpp:598
void executeShiftRightArithmetic(const Instruction *Inst)
void executeINotEqual(const Instruction *Inst)
Memory * PrivateMemory
The private memory instance.
Definition: Invocation.h:229
Device & Dev
The device this invocation is executing on.
Definition: Invocation.h:226
void executeAtomicOp(const Instruction *Inst)
Definition: Invocation.cpp:428
void executeIsNan(const Instruction *Inst)
void executeFUnordGreaterThan(const Instruction *Inst)
Definition: Invocation.cpp:871
void executeAll(const Instruction *Inst)
Definition: Invocation.cpp:396
void executeFOrdLessThanEqual(const Instruction *Inst)
Definition: Invocation.cpp:818
This class represents a function in a SPIR-V Module.
Definition: Function.h:23
void executeKill(const Instruction *Inst)
void executeUDiv(const Instruction *Inst)
void executeAccessChain(const Instruction *Inst)
Definition: Invocation.cpp:238
void executeMatrixTimesScalar(const Instruction *Inst)
void executeFDiv(const Instruction *Inst)
Definition: Invocation.cpp:768
~Invocation()
Destroy this invocation.
Definition: Invocation.cpp:89
void executeReturn(const Instruction *Inst)
void executeFSub(const Instruction *Inst)
Definition: Invocation.cpp:838
const Instruction * CallInst
The calling instruction.
Definition: Invocation.h:214
Dim3 GlobalId
The GlobalInvocationID.
Definition: Invocation.h:228
void executeFOrdNotEqual(const Instruction *Inst)
Definition: Invocation.cpp:825
void executeSMod(const Instruction *Inst)
void executeSelect(const Instruction *Inst)
Invocation(Device &Dev, const std::vector< Object > &InitialObjects)
Create a standalone invocation for a device, with an initial set of result objects.
Definition: Invocation.cpp:38
void executeBitwiseXor(const Instruction *Inst)
Definition: Invocation.cpp:487
std::shared_ptr< const Module > CurrentModule
The current module.
Definition: Invocation.h:201
void executeUConvert(const Instruction *Inst)
void executeIAdd(const Instruction *Inst)
Definition: Invocation.cpp:906
void executeUGreaterThanEqual(const Instruction *Inst)
Invocation & operator=(const Invocation &)=delete
void executeShiftLeftLogical(const Instruction *Inst)
This class represents a single execution of a SPIR-V entry point.
Definition: Invocation.h:33
void executeAtomicCompareExchange(const Instruction *Inst)
Definition: Invocation.cpp:454
void executeImageSampleExplicitLod(const Instruction *Inst)
void executeNot(const Instruction *Inst)
void executeBitcast(const Instruction *Inst)
Definition: Invocation.cpp:470
void executeOp(const Instruction *Inst, const F &Op)
Helper functions to execute simple instructions that can either operate on scalars or component-wise ...
void executeFOrdLessThan(const Instruction *Inst)
Definition: Invocation.cpp:811
void executeFUnordNotEqual(const Instruction *Inst)
Definition: Invocation.cpp:899
void executeSampledImage(const Instruction *Inst)
const Function * CallFunc
The function containing CallInst.
Definition: Invocation.h:215
Class representing a 3-dimensional size or ID.
Definition: Dim3.h:22
State
Used to indicate whether an invocation is ready to execute, waiting at a barrier, or complete...
Definition: Invocation.h:38
void executeFMul(const Instruction *Inst)
Definition: Invocation.cpp:780
void execute(const Instruction *Inst)
Execute Inst in this invocation.
Definition: Invocation.cpp:91
void executeConvertSToF(const Instruction *Inst)
Definition: Invocation.cpp:583
void executeIEqual(const Instruction *Inst)
Definition: Invocation.cpp:911
void executeFUnordLessThan(const Instruction *Inst)
Definition: Invocation.cpp:885
void executeCompositeExtract(const Instruction *Inst)
Definition: Invocation.cpp:519
void executeStore(const Instruction *Inst)
This file declares the Dim3 class.
This class represents an address space in the virtual device.
Definition: Memory.h:37
void executeSDiv(const Instruction *Inst)
void executeVectorTimesMatrix(const Instruction *Inst)
void executeISub(const Instruction *Inst)
void executeSGreaterThanEqual(const Instruction *Inst)
void executeOpFP(const Instruction *Inst, const F &&Op)
void executeLogicalEqual(const Instruction *Inst)
bool wasDiscarded() const
Returns true if this invocation has been discarded with OpKill.
Definition: Invocation.h:93
const Instruction * CurrentInstruction
The current instruction.
Definition: Invocation.h:204
A Device instance encapsulates properties and state for the virtual device.
Definition: Device.h:29
void executeVectorShuffle(const Instruction *Inst)
This file declares the Object class.
void executeLogicalNotEqual(const Instruction *Inst)
void executeLogicalAnd(const Instruction *Inst)
void executeAny(const Instruction *Inst)
Definition: Invocation.cpp:412
uint32_t CallBlock
The block containing CallInst.
Definition: Invocation.h:216
This class represents a workgroup executing a compute command.
Definition: Workgroup.h:27
void executeMatrixTimesVector(const Instruction *Inst)
void executeCopyMemory(const Instruction *Inst)
Definition: Invocation.cpp:613
void executeVectorExtractDynamic(const Instruction *Inst)
void executeSRem(const Instruction *Inst)
void executeConvertFToU(const Instruction *Inst)
Definition: Invocation.cpp:565
void executeLogicalNot(const Instruction *Inst)
void executeFOrdGreaterThan(const Instruction *Inst)
Definition: Invocation.cpp:797
void executeVectorTimesScalar(const Instruction *Inst)
This class encapsulates information about a pipeline stage.
Definition: PipelineStage.h:30
void executeSLessThan(const Instruction *Inst)
void executeVectorInsertDynamic(const Instruction *Inst)
void executeBitwiseAnd(const Instruction *Inst)
Definition: Invocation.cpp:477
void executeSNegate(const Instruction *Inst)
bool AtBarrier
True when at a barrier.
Definition: Invocation.h:207
void executeShiftRightLogical(const Instruction *Inst)
void executeFUnordLessThanEqual(const Instruction *Inst)
Definition: Invocation.cpp:892
void executeBitwiseOr(const Instruction *Inst)
Definition: Invocation.cpp:482
void executeDot(const Instruction *Inst)
Definition: Invocation.cpp:636
const Instruction * getCurrentInstruction() const
Returns the instruction that this invocation is executing.
Definition: Invocation.h:74
void executeUnreachable(const Instruction *Inst)
void executeFNegate(const Instruction *Inst)
Definition: Invocation.cpp:785
void executeFUnordGreaterThanEqual(const Instruction *Inst)
Definition: Invocation.cpp:878
void executeImageRead(const Instruction *Inst)
Definition: Invocation.cpp:979
void executeLoad(const Instruction *Inst)
void executeFConvert(const Instruction *Inst)
Definition: Invocation.cpp:753
Dim3 getGlobalId() const
Returns the global invocation ID.
Definition: Invocation.h:80
Object getObject(uint32_t Id) const
Returns the object with the specified ID.
Workgroup * Group
The workgroup this invocation belongs to.
Definition: Invocation.h:227
void executeConvertFToS(const Instruction *Inst)
Definition: Invocation.cpp:547
std::vector< Object > Objects
Set of result objects.
Definition: Invocation.h:224
void executeControlBarrier(const Instruction *Inst)
Definition: Invocation.cpp:540
void executeImage(const Instruction *Inst)
Definition: Invocation.cpp:916
This class represents an instruction result.
Definition: Object.h:51
void moveToBlock(uint32_t Id)
Move this invocation to the block with ID Id.
void executeReturnValue(const Instruction *Inst)
void executeBranch(const Instruction *Inst)
Definition: Invocation.cpp:492
void executeIMul(const Instruction *Inst)
uint32_t CurrentBlock
The current block.
Definition: Invocation.h:205
void executeBranchConditional(const Instruction *Inst)
Definition: Invocation.cpp:497
void executeFUnordEqual(const Instruction *Inst)
Definition: Invocation.cpp:864
This class represents a SPIR-V instruction.
Definition: Instruction.h:27
void executeSConvert(const Instruction *Inst)
void executeLogicalOr(const Instruction *Inst)
void executeSwitch(const Instruction *Inst)
void executeFOrdEqual(const Instruction *Inst)
Definition: Invocation.cpp:790
void executeFunctionCall(const Instruction *Inst)
Definition: Invocation.cpp:843
void clearBarrier()
Clear the barrier state, allowing the invocation to continue.
Definition: Invocation.h:68
Memory & getMemory(uint32_t StorageClass)
Returns the memory instance associated with StorageClass.
const Function * CurrentFunction
The current function.
Definition: Invocation.h:203
void executeSGreaterThan(const Instruction *Inst)
void executeFOrdGreaterThanEqual(const Instruction *Inst)
Definition: Invocation.cpp:804
std::shared_ptr< Memory > PipelineMemory
Memory used for input and output storage classes.
Definition: Invocation.h:232
void executeFMod(const Instruction *Inst)
Definition: Invocation.cpp:773
void executeUGreaterThan(const Instruction *Inst)