• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef rr_Nucleus_hpp
16 #define rr_Nucleus_hpp
17 
18 #include <atomic>
19 #include <cassert>
20 #include <cstdarg>
21 #include <cstdint>
22 #include <functional>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #ifdef None
28 #	undef None  // TODO(b/127920555)
29 #endif
30 
31 static_assert(sizeof(short) == 2, "Reactor's 'Short' type is 16-bit, and requires the C++ 'short' to match that.");
32 static_assert(sizeof(int) == 4, "Reactor's 'Int' type is 32-bit, and requires the C++ 'int' to match that.");
33 
34 namespace rr {
35 
36 class Type;
37 class Value;
38 class SwitchCases;
39 class BasicBlock;
40 class Routine;
41 
42 // Optimization holds the optimization settings for code generation.
43 class Optimization
44 {
45 public:
46 	enum class Level
47 	{
48 		None,
49 		Less,
50 		Default,
51 		Aggressive,
52 	};
53 
54 	enum class Pass
55 	{
56 		Disabled,
57 		InstructionCombining,
58 		CFGSimplification,
59 		LICM,
60 		AggressiveDCE,
61 		GVN,
62 		Reassociate,
63 		DeadStoreElimination,
64 		SCCP,
65 		ScalarReplAggregates,
66 		EarlyCSEPass,
67 
68 		Count,
69 	};
70 
71 	using Passes = std::vector<Pass>;
72 
Optimization(Level level=Level::Default,const Passes & passes={})73 	Optimization(Level level = Level::Default, const Passes &passes = {})
74 	    : level(level)
75 	    , passes(passes)
76 	{
77 #if defined(REACTOR_DEFAULT_OPT_LEVEL)
78 		{
79 			this->level = Level::REACTOR_DEFAULT_OPT_LEVEL;
80 		}
81 #endif
82 	}
83 
getLevel() const84 	Level getLevel() const { return level; }
getPasses() const85 	const Passes &getPasses() const { return passes; }
86 
87 private:
88 	Level level = Level::Default;
89 	Passes passes;
90 };
91 
92 // Config holds the Reactor configuration settings.
93 class Config
94 {
95 public:
96 	// Edit holds a number of modifications to a config, that can be applied
97 	// on an existing Config to produce a new Config with the specified
98 	// changes.
99 	class Edit
100 	{
101 	public:
set(Optimization::Level level)102 		Edit &set(Optimization::Level level)
103 		{
104 			optLevel = level;
105 			optLevelChanged = true;
106 			return *this;
107 		}
add(Optimization::Pass pass)108 		Edit &add(Optimization::Pass pass)
109 		{
110 			optPassEdits.push_back({ ListEdit::Add, pass });
111 			return *this;
112 		}
remove(Optimization::Pass pass)113 		Edit &remove(Optimization::Pass pass)
114 		{
115 			optPassEdits.push_back({ ListEdit::Remove, pass });
116 			return *this;
117 		}
clearOptimizationPasses()118 		Edit &clearOptimizationPasses()
119 		{
120 			optPassEdits.push_back({ ListEdit::Clear, Optimization::Pass::Disabled });
121 			return *this;
122 		}
123 
124 		Config apply(const Config &cfg) const;
125 
126 	private:
127 		enum class ListEdit
128 		{
129 			Add,
130 			Remove,
131 			Clear
132 		};
133 		using OptPassesEdit = std::pair<ListEdit, Optimization::Pass>;
134 
135 		template<typename T>
136 		void apply(const std::vector<std::pair<ListEdit, T>> &edits, std::vector<T> &list) const;
137 
138 		Optimization::Level optLevel;
139 		bool optLevelChanged = false;
140 		std::vector<OptPassesEdit> optPassEdits;
141 	};
142 
143 	Config() = default;
Config(const Optimization & optimization)144 	Config(const Optimization &optimization)
145 	    : optimization(optimization)
146 	{}
147 
getOptimization() const148 	const Optimization &getOptimization() const { return optimization; }
149 
150 private:
151 	Optimization optimization;
152 };
153 
154 class Nucleus
155 {
156 public:
157 	Nucleus();
158 
159 	virtual ~Nucleus();
160 
161 	// Default configuration to use when no other configuration is specified.
162 	// The new configuration will be applied to subsequent reactor calls.
163 	static void setDefaultConfig(const Config &cfg);
164 	static void adjustDefaultConfig(const Config::Edit &cfgEdit);
165 	static Config getDefaultConfig();
166 
167 	std::shared_ptr<Routine> acquireRoutine(const char *name, const Config::Edit *cfgEdit = nullptr);
168 
169 	static Value *allocateStackVariable(Type *type, int arraySize = 0);
170 	static BasicBlock *createBasicBlock();
171 	static BasicBlock *getInsertBlock();
172 	static void setInsertBlock(BasicBlock *basicBlock);
173 
174 	static void createFunction(Type *returnType, const std::vector<Type *> &paramTypes);
175 	static Value *getArgument(unsigned int index);
176 
177 	// Coroutines
178 	using CoroutineHandle = void *;
179 
180 	template<typename... ARGS>
181 	using CoroutineBegin = CoroutineHandle(ARGS...);
182 	using CoroutineAwait = bool(CoroutineHandle, void *yieldValue);
183 	using CoroutineDestroy = void(CoroutineHandle);
184 
185 	enum CoroutineEntries
186 	{
187 		CoroutineEntryBegin = 0,
188 		CoroutineEntryAwait,
189 		CoroutineEntryDestroy,
190 		CoroutineEntryCount
191 	};
192 
193 	// Begins the generation of the three coroutine functions: CoroutineBegin, CoroutineAwait, and CoroutineDestroy,
194 	// which will be returned by Routine::getEntry() with arg CoroutineEntryBegin, CoroutineEntryAwait, and CoroutineEntryDestroy
195 	// respectively. Called by Coroutine constructor.
196 	// Params are used to generate the params to CoroutineBegin, while ReturnType is used as the YieldType for the coroutine,
197 	// returned via CoroutineAwait..
198 	static void createCoroutine(Type *returnType, const std::vector<Type *> &params);
199 	// Generates code to store the passed in value, and to suspend execution of the coroutine, such that the next call to
200 	// CoroutineAwait can set the output yieldValue and resume execution of the coroutine.
201 	static void yield(Value *val);
202 	// Called to finalize coroutine creation. After this call, Routine::getEntry can be called to retrieve the entry point to any
203 	// of the three coroutine functions. Called by Coroutine::finalize.
204 	std::shared_ptr<Routine> acquireCoroutine(const char *name, const Config::Edit *cfg = nullptr);
205 	// Called by Coroutine::operator() to execute CoroutineEntryBegin wrapped up in func. This is needed in case
206 	// the call must be run on a separate thread of execution (e.g. on a fiber).
207 	static CoroutineHandle invokeCoroutineBegin(Routine &routine, std::function<CoroutineHandle()> func);
208 
209 	// Terminators
210 	static void createRetVoid();
211 	static void createRet(Value *V);
212 	static void createBr(BasicBlock *dest);
213 	static void createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse);
214 
215 	// Binary operators
216 	static Value *createAdd(Value *lhs, Value *rhs);
217 	static Value *createSub(Value *lhs, Value *rhs);
218 	static Value *createMul(Value *lhs, Value *rhs);
219 	static Value *createUDiv(Value *lhs, Value *rhs);
220 	static Value *createSDiv(Value *lhs, Value *rhs);
221 	static Value *createFAdd(Value *lhs, Value *rhs);
222 	static Value *createFSub(Value *lhs, Value *rhs);
223 	static Value *createFMul(Value *lhs, Value *rhs);
224 	static Value *createFDiv(Value *lhs, Value *rhs);
225 	static Value *createURem(Value *lhs, Value *rhs);
226 	static Value *createSRem(Value *lhs, Value *rhs);
227 	static Value *createFRem(Value *lhs, Value *rhs);
228 	static Value *createShl(Value *lhs, Value *rhs);
229 	static Value *createLShr(Value *lhs, Value *rhs);
230 	static Value *createAShr(Value *lhs, Value *rhs);
231 	static Value *createAnd(Value *lhs, Value *rhs);
232 	static Value *createOr(Value *lhs, Value *rhs);
233 	static Value *createXor(Value *lhs, Value *rhs);
234 
235 	// Unary operators
236 	static Value *createNeg(Value *V);
237 	static Value *createFNeg(Value *V);
238 	static Value *createNot(Value *V);
239 
240 	// Memory instructions
241 	static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int alignment = 0, bool atomic = false, std::memory_order memoryOrder = std::memory_order_relaxed);
242 	static Value *createStore(Value *value, Value *ptr, Type *type, bool isVolatile = false, unsigned int aligment = 0, bool atomic = false, std::memory_order memoryOrder = std::memory_order_relaxed);
243 	static Value *createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex);
244 
245 	// Masked Load / Store instructions
246 	static Value *createMaskedLoad(Value *base, Type *elementType, Value *mask, unsigned int alignment, bool zeroMaskedLanes);
247 	static void createMaskedStore(Value *base, Value *value, Value *mask, unsigned int alignment);
248 
249 	// Barrier instructions
250 	static void createFence(std::memory_order memoryOrder);
251 
252 	// Atomic instructions
253 	static Value *createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
254 	static Value *createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
255 	static Value *createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
256 	static Value *createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
257 	static Value *createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
258 	static Value *createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
259 	static Value *createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
260 	static Value *createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
261 	static Value *createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
262 	static Value *createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
263 	static Value *createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal);
264 
265 	// Cast/Conversion Operators
266 	static Value *createTrunc(Value *V, Type *destType);
267 	static Value *createZExt(Value *V, Type *destType);
268 	static Value *createSExt(Value *V, Type *destType);
269 	static Value *createFPToUI(Value *V, Type *destType);
270 	static Value *createFPToSI(Value *V, Type *destType);
271 	static Value *createSIToFP(Value *V, Type *destType);
272 	static Value *createFPTrunc(Value *V, Type *destType);
273 	static Value *createFPExt(Value *V, Type *destType);
274 	static Value *createBitCast(Value *V, Type *destType);
275 
276 	// Compare instructions
277 	static Value *createICmpEQ(Value *lhs, Value *rhs);
278 	static Value *createICmpNE(Value *lhs, Value *rhs);
279 	static Value *createICmpUGT(Value *lhs, Value *rhs);
280 	static Value *createICmpUGE(Value *lhs, Value *rhs);
281 	static Value *createICmpULT(Value *lhs, Value *rhs);
282 	static Value *createICmpULE(Value *lhs, Value *rhs);
283 	static Value *createICmpSGT(Value *lhs, Value *rhs);
284 	static Value *createICmpSGE(Value *lhs, Value *rhs);
285 	static Value *createICmpSLT(Value *lhs, Value *rhs);
286 	static Value *createICmpSLE(Value *lhs, Value *rhs);
287 	static Value *createFCmpOEQ(Value *lhs, Value *rhs);
288 	static Value *createFCmpOGT(Value *lhs, Value *rhs);
289 	static Value *createFCmpOGE(Value *lhs, Value *rhs);
290 	static Value *createFCmpOLT(Value *lhs, Value *rhs);
291 	static Value *createFCmpOLE(Value *lhs, Value *rhs);
292 	static Value *createFCmpONE(Value *lhs, Value *rhs);
293 	static Value *createFCmpORD(Value *lhs, Value *rhs);
294 	static Value *createFCmpUNO(Value *lhs, Value *rhs);
295 	static Value *createFCmpUEQ(Value *lhs, Value *rhs);
296 	static Value *createFCmpUGT(Value *lhs, Value *rhs);
297 	static Value *createFCmpUGE(Value *lhs, Value *rhs);
298 	static Value *createFCmpULT(Value *lhs, Value *rhs);
299 	static Value *createFCmpULE(Value *lhs, Value *rhs);
300 	static Value *createFCmpUNE(Value *lhs, Value *rhs);
301 
302 	// Vector instructions
303 	static Value *createExtractElement(Value *vector, Type *type, int index);
304 	static Value *createInsertElement(Value *vector, Value *element, int index);
305 	static Value *createShuffleVector(Value *V1, Value *V2, const int *select);
306 
307 	// Other instructions
308 	static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse);
309 	static SwitchCases *createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases);
310 	static void addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch);
311 	static void createUnreachable();
312 
313 	// Constant values
314 	static Value *createNullValue(Type *type);
315 	static Value *createConstantLong(int64_t i);
316 	static Value *createConstantInt(int i);
317 	static Value *createConstantInt(unsigned int i);
318 	static Value *createConstantBool(bool b);
319 	static Value *createConstantByte(signed char i);
320 	static Value *createConstantByte(unsigned char i);
321 	static Value *createConstantShort(short i);
322 	static Value *createConstantShort(unsigned short i);
323 	static Value *createConstantFloat(float x);
324 	static Value *createNullPointer(Type *type);
325 	static Value *createConstantVector(const int64_t *constants, Type *type);
326 	static Value *createConstantVector(const double *constants, Type *type);
327 	static Value *createConstantString(const char *v);
createConstantString(const std::string & v)328 	static Value *createConstantString(const std::string &v) { return createConstantString(v.c_str()); }
329 
330 	static Type *getType(Value *value);
331 	static Type *getContainedType(Type *vectorType);
332 	static Type *getPointerType(Type *elementType);
333 	static Type *getPrintfStorageType(Type *valueType);
334 
335 	// Diagnostic utilities
336 	struct OptimizerReport
337 	{
338 		int allocas = 0;
339 		int loads = 0;
340 		int stores = 0;
341 	};
342 
343 	using OptimizerCallback = void(const OptimizerReport *report);
344 
345 	// Sets the callback to be used by the next optimizer invocation (during acquireRoutine),
346 	// for reporting stats about the resulting IR code. For testing only.
347 	static void setOptimizerCallback(OptimizerCallback *callback);
348 };
349 
350 }  // namespace rr
351 
352 #endif  // rr_Nucleus_hpp
353