• 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:
102 		static const Edit None;
103 
set(Optimization::Level level)104 		Edit &set(Optimization::Level level)
105 		{
106 			optLevel = level;
107 			optLevelChanged = true;
108 			return *this;
109 		}
add(Optimization::Pass pass)110 		Edit &add(Optimization::Pass pass)
111 		{
112 			optPassEdits.push_back({ ListEdit::Add, pass });
113 			return *this;
114 		}
remove(Optimization::Pass pass)115 		Edit &remove(Optimization::Pass pass)
116 		{
117 			optPassEdits.push_back({ ListEdit::Remove, pass });
118 			return *this;
119 		}
clearOptimizationPasses()120 		Edit &clearOptimizationPasses()
121 		{
122 			optPassEdits.push_back({ ListEdit::Clear, Optimization::Pass::Disabled });
123 			return *this;
124 		}
125 
126 		Config apply(const Config &cfg) const;
127 
128 	private:
129 		enum class ListEdit
130 		{
131 			Add,
132 			Remove,
133 			Clear
134 		};
135 		using OptPassesEdit = std::pair<ListEdit, Optimization::Pass>;
136 
137 		template<typename T>
138 		void apply(const std::vector<std::pair<ListEdit, T>> &edits, std::vector<T> &list) const;
139 
140 		Optimization::Level optLevel;
141 		bool optLevelChanged = false;
142 		std::vector<OptPassesEdit> optPassEdits;
143 	};
144 
145 	Config() = default;
Config(const Optimization & optimization)146 	Config(const Optimization &optimization)
147 	    : optimization(optimization)
148 	{}
149 
getOptimization() const150 	const Optimization &getOptimization() const { return optimization; }
151 
152 private:
153 	Optimization optimization;
154 };
155 
156 class Nucleus
157 {
158 public:
159 	Nucleus();
160 
161 	virtual ~Nucleus();
162 
163 	// Default configuration to use when no other configuration is specified.
164 	// The new configuration will be applied to subsequent reactor calls.
165 	static void setDefaultConfig(const Config &cfg);
166 	static void adjustDefaultConfig(const Config::Edit &cfgEdit);
167 	static Config getDefaultConfig();
168 
169 	std::shared_ptr<Routine> acquireRoutine(const char *name, const Config::Edit &cfgEdit = Config::Edit::None);
170 
171 	static Value *allocateStackVariable(Type *type, int arraySize = 0);
172 	static BasicBlock *createBasicBlock();
173 	static BasicBlock *getInsertBlock();
174 	static void setInsertBlock(BasicBlock *basicBlock);
175 
176 	static void createFunction(Type *returnType, const std::vector<Type *> &paramTypes);
177 	static Value *getArgument(unsigned int index);
178 
179 	// Coroutines
180 	using CoroutineHandle = void *;
181 
182 	template<typename... ARGS>
183 	using CoroutineBegin = CoroutineHandle(ARGS...);
184 	using CoroutineAwait = bool(CoroutineHandle, void *yieldValue);
185 	using CoroutineDestroy = void(CoroutineHandle);
186 
187 	enum CoroutineEntries
188 	{
189 		CoroutineEntryBegin = 0,
190 		CoroutineEntryAwait,
191 		CoroutineEntryDestroy,
192 		CoroutineEntryCount
193 	};
194 
195 	// Begins the generation of the three coroutine functions: CoroutineBegin, CoroutineAwait, and CoroutineDestroy,
196 	// which will be returned by Routine::getEntry() with arg CoroutineEntryBegin, CoroutineEntryAwait, and CoroutineEntryDestroy
197 	// respectively. Called by Coroutine constructor.
198 	// Params are used to generate the params to CoroutineBegin, while ReturnType is used as the YieldType for the coroutine,
199 	// returned via CoroutineAwait..
200 	static void createCoroutine(Type *returnType, const std::vector<Type *> &params);
201 	// Generates code to store the passed in value, and to suspend execution of the coroutine, such that the next call to
202 	// CoroutineAwait can set the output yieldValue and resume execution of the coroutine.
203 	static void yield(Value *val);
204 	// Called to finalize coroutine creation. After this call, Routine::getEntry can be called to retrieve the entry point to any
205 	// of the three coroutine functions. Called by Coroutine::finalize.
206 	std::shared_ptr<Routine> acquireCoroutine(const char *name, const Config::Edit &cfg = Config::Edit::None);
207 	// Called by Coroutine::operator() to execute CoroutineEntryBegin wrapped up in func. This is needed in case
208 	// the call must be run on a separate thread of execution (e.g. on a fiber).
209 	static CoroutineHandle invokeCoroutineBegin(Routine &routine, std::function<CoroutineHandle()> func);
210 
211 	// Terminators
212 	static void createRetVoid();
213 	static void createRet(Value *V);
214 	static void createBr(BasicBlock *dest);
215 	static void createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse);
216 
217 	// Binary operators
218 	static Value *createAdd(Value *lhs, Value *rhs);
219 	static Value *createSub(Value *lhs, Value *rhs);
220 	static Value *createMul(Value *lhs, Value *rhs);
221 	static Value *createUDiv(Value *lhs, Value *rhs);
222 	static Value *createSDiv(Value *lhs, Value *rhs);
223 	static Value *createFAdd(Value *lhs, Value *rhs);
224 	static Value *createFSub(Value *lhs, Value *rhs);
225 	static Value *createFMul(Value *lhs, Value *rhs);
226 	static Value *createFDiv(Value *lhs, Value *rhs);
227 	static Value *createURem(Value *lhs, Value *rhs);
228 	static Value *createSRem(Value *lhs, Value *rhs);
229 	static Value *createFRem(Value *lhs, Value *rhs);
230 	static Value *createShl(Value *lhs, Value *rhs);
231 	static Value *createLShr(Value *lhs, Value *rhs);
232 	static Value *createAShr(Value *lhs, Value *rhs);
233 	static Value *createAnd(Value *lhs, Value *rhs);
234 	static Value *createOr(Value *lhs, Value *rhs);
235 	static Value *createXor(Value *lhs, Value *rhs);
236 
237 	// Unary operators
238 	static Value *createNeg(Value *V);
239 	static Value *createFNeg(Value *V);
240 	static Value *createNot(Value *V);
241 
242 	// Memory instructions
243 	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);
244 	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);
245 	static Value *createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex);
246 
247 	// Masked Load / Store instructions
248 	static Value *createMaskedLoad(Value *base, Type *elementType, Value *mask, unsigned int alignment, bool zeroMaskedLanes);
249 	static void createMaskedStore(Value *base, Value *value, Value *mask, unsigned int alignment);
250 
251 	// Barrier instructions
252 	static void createFence(std::memory_order memoryOrder);
253 
254 	// Atomic instructions
255 	static Value *createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
256 	static Value *createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
257 	static Value *createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
258 	static Value *createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
259 	static Value *createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
260 	static Value *createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
261 	static Value *createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
262 	static Value *createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
263 	static Value *createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
264 	static Value *createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder = std::memory_order_relaxed);
265 	static Value *createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal);
266 
267 	// Cast/Conversion Operators
268 	static Value *createTrunc(Value *V, Type *destType);
269 	static Value *createZExt(Value *V, Type *destType);
270 	static Value *createSExt(Value *V, Type *destType);
271 	static Value *createFPToUI(Value *V, Type *destType);
272 	static Value *createFPToSI(Value *V, Type *destType);
273 	static Value *createSIToFP(Value *V, Type *destType);
274 	static Value *createFPTrunc(Value *V, Type *destType);
275 	static Value *createFPExt(Value *V, Type *destType);
276 	static Value *createBitCast(Value *V, Type *destType);
277 
278 	// Compare instructions
279 	static Value *createICmpEQ(Value *lhs, Value *rhs);
280 	static Value *createICmpNE(Value *lhs, Value *rhs);
281 	static Value *createICmpUGT(Value *lhs, Value *rhs);
282 	static Value *createICmpUGE(Value *lhs, Value *rhs);
283 	static Value *createICmpULT(Value *lhs, Value *rhs);
284 	static Value *createICmpULE(Value *lhs, Value *rhs);
285 	static Value *createICmpSGT(Value *lhs, Value *rhs);
286 	static Value *createICmpSGE(Value *lhs, Value *rhs);
287 	static Value *createICmpSLT(Value *lhs, Value *rhs);
288 	static Value *createICmpSLE(Value *lhs, Value *rhs);
289 	static Value *createFCmpOEQ(Value *lhs, Value *rhs);
290 	static Value *createFCmpOGT(Value *lhs, Value *rhs);
291 	static Value *createFCmpOGE(Value *lhs, Value *rhs);
292 	static Value *createFCmpOLT(Value *lhs, Value *rhs);
293 	static Value *createFCmpOLE(Value *lhs, Value *rhs);
294 	static Value *createFCmpONE(Value *lhs, Value *rhs);
295 	static Value *createFCmpORD(Value *lhs, Value *rhs);
296 	static Value *createFCmpUNO(Value *lhs, Value *rhs);
297 	static Value *createFCmpUEQ(Value *lhs, Value *rhs);
298 	static Value *createFCmpUGT(Value *lhs, Value *rhs);
299 	static Value *createFCmpUGE(Value *lhs, Value *rhs);
300 	static Value *createFCmpULT(Value *lhs, Value *rhs);
301 	static Value *createFCmpULE(Value *lhs, Value *rhs);
302 	static Value *createFCmpUNE(Value *lhs, Value *rhs);
303 
304 	// Vector instructions
305 	static Value *createExtractElement(Value *vector, Type *type, int index);
306 	static Value *createInsertElement(Value *vector, Value *element, int index);
307 	static Value *createShuffleVector(Value *V1, Value *V2, const int *select);
308 
309 	// Other instructions
310 	static Value *createSelect(Value *C, Value *ifTrue, Value *ifFalse);
311 	static SwitchCases *createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases);
312 	static void addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch);
313 	static void createUnreachable();
314 
315 	// Constant values
316 	static Value *createNullValue(Type *type);
317 	static Value *createConstantLong(int64_t i);
318 	static Value *createConstantInt(int i);
319 	static Value *createConstantInt(unsigned int i);
320 	static Value *createConstantBool(bool b);
321 	static Value *createConstantByte(signed char i);
322 	static Value *createConstantByte(unsigned char i);
323 	static Value *createConstantShort(short i);
324 	static Value *createConstantShort(unsigned short i);
325 	static Value *createConstantFloat(float x);
326 	static Value *createNullPointer(Type *type);
327 	static Value *createConstantVector(const int64_t *constants, Type *type);
328 	static Value *createConstantVector(const double *constants, Type *type);
329 	static Value *createConstantString(const char *v);
createConstantString(const std::string & v)330 	static Value *createConstantString(const std::string &v) { return createConstantString(v.c_str()); }
331 
332 	static Type *getType(Value *value);
333 	static Type *getContainedType(Type *vectorType);
334 	static Type *getPointerType(Type *elementType);
335 	static Type *getPrintfStorageType(Type *valueType);
336 
337 	// Diagnostic utilities
338 	struct OptimizerReport
339 	{
340 		int allocas = 0;
341 		int loads = 0;
342 		int stores = 0;
343 	};
344 
345 	using OptimizerCallback = void(const OptimizerReport *report);
346 
347 	// Sets the callback to be used by the next optimizer invocation (during acquireRoutine),
348 	// for reporting stats about the resulting IR code. For testing only.
349 	static void setOptimizerCallback(OptimizerCallback *callback);
350 };
351 
352 }  // namespace rr
353 
354 #endif  // rr_Nucleus_hpp
355