comparison examples/ParallelJIT/ParallelJIT.cpp @ 95:afa8332a0e37 LLVM3.8

LLVM 3.8
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 13 Oct 2015 17:48:58 +0900
parents 54457678186b
children 7d135dc70f03
comparison
equal deleted inserted replaced
84:f3e34b893a5f 95:afa8332a0e37
15 // Once all threads are blocked on the conditional variable, the main thread 15 // Once all threads are blocked on the conditional variable, the main thread
16 // wakes them up. This complicated work is performed so that all three threads 16 // wakes them up. This complicated work is performed so that all three threads
17 // call into the JIT at the same time (or the best possible approximation of the 17 // call into the JIT at the same time (or the best possible approximation of the
18 // same time). This test had assertion errors until I got the locking right. 18 // same time). This test had assertion errors until I got the locking right.
19 19
20 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ExecutionEngine/GenericValue.h" 21 #include "llvm/ExecutionEngine/GenericValue.h"
21 #include "llvm/ExecutionEngine/Interpreter.h" 22 #include "llvm/ExecutionEngine/Interpreter.h"
22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/LLVMContext.h" 26 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h" 27 #include "llvm/IR/Module.h"
27 #include "llvm/Support/TargetSelect.h" 28 #include "llvm/Support/TargetSelect.h"
28 #include <iostream> 29 #include <iostream>
29 #include <pthread.h> 30 #include <pthread.h>
31
30 using namespace llvm; 32 using namespace llvm;
31 33
32 static Function* createAdd1(Module *M) { 34 static Function* createAdd1(Module *M) {
33 // Create the add1 function entry and insert this entry into module M. The 35 // Create the add1 function entry and insert this entry into module M. The
34 // function will have a return type of "int" and take an argument of "int". 36 // function will have a return type of "int" and take an argument of "int".
35 // The '0' terminates the list of argument types. 37 // The '0' terminates the list of argument types.
36 Function *Add1F = 38 Function *Add1F =
37 cast<Function>(M->getOrInsertFunction("add1", 39 cast<Function>(M->getOrInsertFunction("add1",
38 Type::getInt32Ty(M->getContext()), 40 Type::getInt32Ty(M->getContext()),
39 Type::getInt32Ty(M->getContext()), 41 Type::getInt32Ty(M->getContext()),
40 (Type *)0)); 42 nullptr));
41 43
42 // Add a basic block to the function. As before, it automatically inserts 44 // Add a basic block to the function. As before, it automatically inserts
43 // because of the last argument. 45 // because of the last argument.
44 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F); 46 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
45 47
66 // to return an int and take an int parameter. 68 // to return an int and take an int parameter.
67 Function *FibF = 69 Function *FibF =
68 cast<Function>(M->getOrInsertFunction("fib", 70 cast<Function>(M->getOrInsertFunction("fib",
69 Type::getInt32Ty(M->getContext()), 71 Type::getInt32Ty(M->getContext()),
70 Type::getInt32Ty(M->getContext()), 72 Type::getInt32Ty(M->getContext()),
71 (Type *)0)); 73 nullptr));
72 74
73 // Add a basic block to the function. 75 // Add a basic block to the function.
74 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF); 76 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
75 77
76 // Get pointers to the constants. 78 // Get pointers to the constants.
126 WaitForThreads() 128 WaitForThreads()
127 { 129 {
128 n = 0; 130 n = 0;
129 waitFor = 0; 131 waitFor = 0;
130 132
131 int result = pthread_cond_init( &condition, NULL ); 133 int result = pthread_cond_init( &condition, nullptr );
132 assert( result == 0 ); 134 assert( result == 0 );
133 135
134 result = pthread_mutex_init( &mutex, NULL ); 136 result = pthread_mutex_init( &mutex, nullptr );
135 assert( result == 0 ); 137 assert( result == 0 );
136 } 138 }
137 139
138 ~WaitForThreads() 140 ~WaitForThreads()
139 { 141 {
258 struct threadParams add1 = { EE, add1F, 1000 }; 260 struct threadParams add1 = { EE, add1F, 1000 };
259 struct threadParams fib1 = { EE, fibF, 39 }; 261 struct threadParams fib1 = { EE, fibF, 39 };
260 struct threadParams fib2 = { EE, fibF, 42 }; 262 struct threadParams fib2 = { EE, fibF, 42 };
261 263
262 pthread_t add1Thread; 264 pthread_t add1Thread;
263 int result = pthread_create( &add1Thread, NULL, callFunc, &add1 ); 265 int result = pthread_create( &add1Thread, nullptr, callFunc, &add1 );
264 if ( result != 0 ) { 266 if ( result != 0 ) {
265 std::cerr << "Could not create thread" << std::endl; 267 std::cerr << "Could not create thread" << std::endl;
266 return 1; 268 return 1;
267 } 269 }
268 270
269 pthread_t fibThread1; 271 pthread_t fibThread1;
270 result = pthread_create( &fibThread1, NULL, callFunc, &fib1 ); 272 result = pthread_create( &fibThread1, nullptr, callFunc, &fib1 );
271 if ( result != 0 ) { 273 if ( result != 0 ) {
272 std::cerr << "Could not create thread" << std::endl; 274 std::cerr << "Could not create thread" << std::endl;
273 return 1; 275 return 1;
274 } 276 }
275 277
276 pthread_t fibThread2; 278 pthread_t fibThread2;
277 result = pthread_create( &fibThread2, NULL, callFunc, &fib2 ); 279 result = pthread_create( &fibThread2, nullptr, callFunc, &fib2 );
278 if ( result != 0 ) { 280 if ( result != 0 ) {
279 std::cerr << "Could not create thread" << std::endl; 281 std::cerr << "Could not create thread" << std::endl;
280 return 1; 282 return 1;
281 } 283 }
282 284