Talvos  0.1
SPIR-V interpreter and dynamic analysis framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Module.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_MODULE_H
10 #define TALVOS_MODULE_H
11 
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "talvos/Dim3.h"
18 #include "talvos/Object.h"
19 
20 namespace talvos
21 {
22 
23 class EntryPoint;
24 class Function;
25 class Instruction;
26 class Type;
27 class Variable;
28 
30 typedef std::vector<const Variable *> VariableList;
31 
37 class Module
38 {
39 public:
41  Module(uint32_t IdBound);
42 
43  // Destroy the module and all its types, functions, blocks, and instructions.
44  ~Module();
45 
46  // Do not allow Module objects to be copied.
48  Module(const Module &) = delete;
49  Module &operator=(const Module &) = delete;
51 
55  void addEntryPoint(EntryPoint *EP);
56 
58  void addFunction(std::unique_ptr<Function> Func);
59 
61  void addLocalSize(uint32_t Entry, Dim3 LocalSize);
62 
64  void addObject(uint32_t Id, const Object &Obj);
65 
67  void addSpecConstant(uint32_t SpecId, uint32_t ResultId);
68 
72 
74  void addType(uint32_t Id, std::unique_ptr<Type> Ty);
75 
77  void addVariable(Variable *Var) { Variables.push_back(Var); }
78 
82  const EntryPoint *getEntryPoint(const std::string &Name,
83  uint32_t ExecutionModel) const;
84 
86  const Function *getFunction(uint32_t Id) const;
87 
89  uint32_t getIdBound() const { return IdBound; }
90 
93  Dim3 getLocalSize(uint32_t Entry) const;
94 
97  const Object &getObject(uint32_t Id) const;
98 
100  const std::vector<Object> &getObjects() const;
101 
104  uint32_t getSpecConstant(uint32_t SpecId) const;
105 
107  const std::vector<Instruction *> &getSpecConstantOps() const;
108 
110  const Type *getType(uint32_t Id) const;
111 
113  const VariableList &getVariables() const { return Variables; }
114 
117  uint32_t getWorkgroupSizeId() const { return WorkgroupSizeId; }
118 
120  void setWorkgroupSizeId(uint32_t Id) { WorkgroupSizeId = Id; }
121 
124  static std::shared_ptr<Module> load(const uint32_t *Words, size_t NumWords);
125 
128  static std::shared_ptr<Module> load(const std::string &FileName);
129 
130 private:
132  typedef std::map<uint32_t, std::unique_ptr<Type>> TypeMap;
133 
135  typedef std::map<uint32_t, std::unique_ptr<Function>> FunctionMap;
136 
137  uint32_t IdBound;
138  std::vector<Object> Objects;
140 
142  std::vector<EntryPoint *> EntryPoints;
143  std::map<uint32_t, Dim3> LocalSizes;
144 
146  std::map<uint32_t, uint32_t> SpecConstants;
147 
149  std::vector<Instruction *> SpecConstantOps;
150 
152  uint32_t WorkgroupSizeId;
153 
156 };
157 
158 } // namespace talvos
159 
160 #endif
This class represents a module-scope variable declaration.
Definition: Variable.h:21
Module(uint32_t IdBound)
Create an empty module.
Definition: Module.cpp:695
const Type * getType(uint32_t Id) const
Returns the type with the specified ID.
Definition: Module.cpp:798
std::vector< const Variable * > VariableList
A list of module scope variables.
Definition: EntryPoint.h:19
std::vector< Instruction * > SpecConstantOps
List of specialization constant operation instructions.
Definition: Module.h:149
const std::vector< Instruction * > & getSpecConstantOps() const
Returns the list of specialization constant operation instructions.
Definition: Module.cpp:793
This class represents a SPIR-V module.
Definition: Module.h:37
std::map< uint32_t, std::unique_ptr< Type > > TypeMap
Map from SPIR-V result ID to talvos::Type.
Definition: Module.h:132
void addLocalSize(uint32_t Entry, Dim3 LocalSize)
Add a local size execution mode to an entry point.
Definition: Module.cpp:726
void setWorkgroupSizeId(uint32_t Id)
Set the ID of the object decorated with WorkgroupSize.
Definition: Module.h:120
const std::vector< Object > & getObjects() const
Returns a list of all result objects in this module.
Definition: Module.cpp:784
This class represents a function in a SPIR-V Module.
Definition: Function.h:23
std::vector< EntryPoint * > EntryPoints
List of entry points.
Definition: Module.h:142
std::map< uint32_t, std::unique_ptr< Function > > FunctionMap
Map from SPIR-V result ID to talvos::Function.
Definition: Module.h:135
std::vector< Object > Objects
Constant instruction results.
Definition: Module.h:138
void addSpecConstant(uint32_t SpecId, uint32_t ResultId)
Add a specialization constant ID mapping.
Definition: Module.cpp:738
const Function * getFunction(uint32_t Id) const
Returns the function with the specified ID.
Definition: Module.cpp:767
uint32_t WorkgroupSizeId
The ID of the object decorated with WorkgroupSize.
Definition: Module.h:152
VariableList Variables
Module scope variables.
Definition: Module.h:155
uint32_t getWorkgroupSizeId() const
Returns the ID of the object decorated with WorkgroupSize.
Definition: Module.h:117
void addVariable(Variable *Var)
Add a variable to this module, transferring ownership to the module.
Definition: Module.h:77
Class representing a 3-dimensional size or ID.
Definition: Dim3.h:22
void addEntryPoint(EntryPoint *EP)
Add an entry point to the module.
Definition: Module.cpp:714
void addFunction(std::unique_ptr< Function > Func)
Add a function to this module.
Definition: Module.cpp:720
const EntryPoint * getEntryPoint(const std::string &Name, uint32_t ExecutionModel) const
Get the entry point with the specified name and SPIR-V execution model.
Definition: Module.cpp:756
This file declares the Dim3 class.
std::map< uint32_t, uint32_t > SpecConstants
Map specialization constant IDs to result IDs.
Definition: Module.h:146
std::map< uint32_t, Dim3 > LocalSizes
LocalSize execution modes.
Definition: Module.h:143
This file declares the Object class.
TypeMap Types
Type mapping.
Definition: Module.h:139
uint32_t getIdBound() const
Returns the ID bound of the results in this module.
Definition: Module.h:89
void addObject(uint32_t Id, const Object &Obj)
Add an object to this module.
Definition: Module.cpp:732
uint32_t IdBound
The ID bound of the module.
Definition: Module.h:137
Dim3 getLocalSize(uint32_t Entry) const
Returns the LocalSize execution mode for an entry point.
Definition: Module.cpp:774
This class represents a SPIR-V type.
Definition: Type.h:33
static std::shared_ptr< Module > load(const uint32_t *Words, size_t NumWords)
Create a new module from the supplied SPIR-V binary data.
Definition: Module.cpp:805
Module & operator=(const Module &)=delete
This class represents an instruction result.
Definition: Object.h:51
This class represents a SPIR-V instruction.
Definition: Instruction.h:27
void addType(uint32_t Id, std::unique_ptr< Type > Ty)
Add a type to this module.
Definition: Module.cpp:750
const Object & getObject(uint32_t Id) const
Returns the object with the specified ID.
Definition: Module.cpp:782
void addSpecConstantOp(Instruction *Op)
Add a specialization constant operation instruction to this module.
Definition: Module.cpp:745
uint32_t getSpecConstant(uint32_t SpecId) const
Returns the result ID for the given specialization constant ID.
Definition: Module.cpp:786
FunctionMap Functions
Function mapping.
Definition: Module.h:141
This class represents a shader entry point.
Definition: EntryPoint.h:25
const VariableList & getVariables() const
Returns the list of module scope variables.
Definition: Module.h:113