1 //===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // This test program creates two LLVM functions then calls them from three
12 // separate threads. It requires the pthreads library.
13 // The three threads are created and then block waiting on a condition variable.
14 // Once all threads are blocked on the conditional variable, the main thread
15 // wakes them up. This complicated work is performed so that all three threads
16 // call into the JIT at the same time (or the best possible approximation of the
17 // same time). This test had assertion errors until I got the locking right.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
25 #include "llvm/ExecutionEngine/MCJIT.h"
26 #include "llvm/IR/Argument.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/TargetSelect.h"
50 static Function
* createAdd1(Module
*M
) {
51 LLVMContext
&Context
= M
->getContext();
52 // Create the add1 function entry and insert this entry into module M. The
53 // function will have a return type of "int" and take an argument of "int".
55 Function::Create(FunctionType::get(Type::getInt32Ty(Context
),
56 {Type::getInt32Ty(Context
)}, false),
57 Function::ExternalLinkage
, "add1", M
);
59 // Add a basic block to the function. As before, it automatically inserts
60 // because of the last argument.
61 BasicBlock
*BB
= BasicBlock::Create(Context
, "EntryBlock", Add1F
);
63 // Get pointers to the constant `1'.
64 Value
*One
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
66 // Get pointers to the integer argument of the add1 function...
67 assert(Add1F
->arg_begin() != Add1F
->arg_end()); // Make sure there's an arg
68 Argument
*ArgX
= &*Add1F
->arg_begin(); // Get the arg
69 ArgX
->setName("AnArg"); // Give it a nice symbolic name for fun.
71 // Create the add instruction, inserting it into the end of BB.
72 Instruction
*Add
= BinaryOperator::CreateAdd(One
, ArgX
, "addresult", BB
);
74 // Create the return instruction and add it to the basic block
75 ReturnInst::Create(Context
, Add
, BB
);
77 // Now, function add1 is ready.
81 static Function
*CreateFibFunction(Module
*M
) {
82 LLVMContext
&Context
= M
->getContext();
83 // Create the fib function and insert it into module M. This function is said
84 // to return an int and take an int parameter.
85 FunctionType
*FibFTy
= FunctionType::get(Type::getInt32Ty(Context
),
86 {Type::getInt32Ty(Context
)}, false);
88 Function::Create(FibFTy
, Function::ExternalLinkage
, "fib", M
);
90 // Add a basic block to the function.
91 BasicBlock
*BB
= BasicBlock::Create(Context
, "EntryBlock", FibF
);
93 // Get pointers to the constants.
94 Value
*One
= ConstantInt::get(Type::getInt32Ty(Context
), 1);
95 Value
*Two
= ConstantInt::get(Type::getInt32Ty(Context
), 2);
97 // Get pointer to the integer argument of the add1 function...
98 Argument
*ArgX
= &*FibF
->arg_begin(); // Get the arg.
99 ArgX
->setName("AnArg"); // Give it a nice symbolic name for fun.
101 // Create the true_block.
102 BasicBlock
*RetBB
= BasicBlock::Create(Context
, "return", FibF
);
103 // Create an exit block.
104 BasicBlock
*RecurseBB
= BasicBlock::Create(Context
, "recurse", FibF
);
106 // Create the "if (arg < 2) goto exitbb"
107 Value
*CondInst
= new ICmpInst(BB
, ICmpInst::ICMP_SLE
, ArgX
, Two
, "cond");
108 BranchInst::Create(RetBB
, RecurseBB
, CondInst
, BB
);
111 ReturnInst::Create(Context
, One
, RetBB
);
114 Value
*Sub
= BinaryOperator::CreateSub(ArgX
, One
, "arg", RecurseBB
);
115 Value
*CallFibX1
= CallInst::Create(FibF
, Sub
, "fibx1", RecurseBB
);
118 Sub
= BinaryOperator::CreateSub(ArgX
, Two
, "arg", RecurseBB
);
119 Value
*CallFibX2
= CallInst::Create(FibF
, Sub
, "fibx2", RecurseBB
);
123 BinaryOperator::CreateAdd(CallFibX1
, CallFibX2
, "addresult", RecurseBB
);
125 // Create the return instruction and add it to the basic block
126 ReturnInst::Create(Context
, Sum
, RecurseBB
);
131 struct threadParams
{
137 // We block the subthreads just before they begin to execute:
138 // we want all of them to call into the JIT at the same time,
139 // to verify that the locking is working correctly.
148 int result
= pthread_cond_init( &condition
, nullptr );
150 assert( result
== 0 );
152 result
= pthread_mutex_init( &mutex
, nullptr );
153 assert( result
== 0 );
158 int result
= pthread_cond_destroy( &condition
);
160 assert( result
== 0 );
162 result
= pthread_mutex_destroy( &mutex
);
163 assert( result
== 0 );
166 // All threads will stop here until another thread calls releaseThreads
169 int result
= pthread_mutex_lock( &mutex
);
171 assert( result
== 0 );
173 //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
175 assert( waitFor
== 0 || n
<= waitFor
);
176 if ( waitFor
> 0 && n
== waitFor
)
178 // There are enough threads blocked that we can release all of them
179 std::cout
<< "Unblocking threads from block()" << std::endl
;
184 // We just need to wait until someone unblocks us
185 result
= pthread_cond_wait( &condition
, &mutex
);
186 assert( result
== 0 );
189 // unlock the mutex before returning
190 result
= pthread_mutex_unlock( &mutex
);
191 assert( result
== 0 );
194 // If there are num or more threads blocked, it will signal them all
195 // Otherwise, this thread blocks until there are enough OTHER threads
197 void releaseThreads( size_t num
)
199 int result
= pthread_mutex_lock( &mutex
);
201 assert( result
== 0 );
204 std::cout
<< "Unblocking threads from releaseThreads()" << std::endl
;
210 pthread_cond_wait( &condition
, &mutex
);
213 // unlock the mutex before returning
214 result
= pthread_mutex_unlock( &mutex
);
215 assert( result
== 0 );
219 void unblockThreads()
221 // Reset the counters to zero: this way, if any new threads
222 // enter while threads are exiting, they will block instead
223 // of triggering a new release of threads
226 // Reset waitFor to zero: this way, if waitFor threads enter
227 // while threads are exiting, they will block instead of
228 // triggering a new release of threads
231 int result
= pthread_cond_broadcast( &condition
);
238 pthread_cond_t condition
;
239 pthread_mutex_t mutex
;
242 static WaitForThreads synchronize
;
244 void* callFunc( void* param
)
246 struct threadParams
* p
= (struct threadParams
*) param
;
248 // Call the `foo' function with no arguments:
249 std::vector
<GenericValue
> Args(1);
250 Args
[0].IntVal
= APInt(32, p
->value
);
252 synchronize
.block(); // wait until other threads are at this point
253 GenericValue gv
= p
->EE
->runFunction(p
->F
, Args
);
255 return (void*)(intptr_t)gv
.IntVal
.getZExtValue();
259 InitializeNativeTarget();
260 LLVMInitializeNativeAsmPrinter();
263 // Create some module to put our function into it.
264 std::unique_ptr
<Module
> Owner
= std::make_unique
<Module
>("test", Context
);
265 Module
*M
= Owner
.get();
267 Function
* add1F
= createAdd1( M
);
268 Function
* fibF
= CreateFibFunction( M
);
270 // Now we create the JIT.
271 ExecutionEngine
* EE
= EngineBuilder(std::move(Owner
)).create();
273 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
274 //~ std::cout << "\n\nRunning foo: " << std::flush;
276 // Create one thread for add1 and two threads for fib
277 struct threadParams add1
= { EE
, add1F
, 1000 };
278 struct threadParams fib1
= { EE
, fibF
, 39 };
279 struct threadParams fib2
= { EE
, fibF
, 42 };
281 pthread_t add1Thread
;
282 int result
= pthread_create( &add1Thread
, nullptr, callFunc
, &add1
);
284 std::cerr
<< "Could not create thread" << std::endl
;
288 pthread_t fibThread1
;
289 result
= pthread_create( &fibThread1
, nullptr, callFunc
, &fib1
);
291 std::cerr
<< "Could not create thread" << std::endl
;
295 pthread_t fibThread2
;
296 result
= pthread_create( &fibThread2
, nullptr, callFunc
, &fib2
);
298 std::cerr
<< "Could not create thread" << std::endl
;
302 synchronize
.releaseThreads(3); // wait until other threads are at this point
305 result
= pthread_join( add1Thread
, &returnValue
);
307 std::cerr
<< "Could not join thread" << std::endl
;
310 std::cout
<< "Add1 returned " << intptr_t(returnValue
) << std::endl
;
312 result
= pthread_join( fibThread1
, &returnValue
);
314 std::cerr
<< "Could not join thread" << std::endl
;
317 std::cout
<< "Fib1 returned " << intptr_t(returnValue
) << std::endl
;
319 result
= pthread_join( fibThread2
, &returnValue
);
321 std::cerr
<< "Could not join thread" << std::endl
;
324 std::cout
<< "Fib2 returned " << intptr_t(returnValue
) << std::endl
;