aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Fibonacci/Makefile17
-rw-r--r--examples/Fibonacci/fibonacci.cpp121
-rw-r--r--examples/HowToUseJIT/HowToUseJIT.cpp112
-rw-r--r--examples/HowToUseJIT/Makefile15
-rw-r--r--examples/Makefile19
-rw-r--r--examples/ModuleMaker/Makefile14
-rw-r--r--examples/ModuleMaker/ModuleMaker.cpp61
-rw-r--r--examples/ModuleMaker/README.txt8
-rw-r--r--examples/ParallelJIT/Makefile17
-rw-r--r--examples/ParallelJIT/ParallelJIT.cpp298
10 files changed, 682 insertions, 0 deletions
diff --git a/examples/Fibonacci/Makefile b/examples/Fibonacci/Makefile
new file mode 100644
index 0000000..5010cb4
--- /dev/null
+++ b/examples/Fibonacci/Makefile
@@ -0,0 +1,17 @@
+##===- examples/Fibonacci/Makefile -------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file was developed by Valery A. Khamenya and is distributed under
+# the University of Illinois Open Source License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TOOLNAME = Fibonacci
+EXAMPLE_TOOL = 1
+
+# Link in JIT support
+LINK_COMPONENTS := jit interpreter native
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/Fibonacci/fibonacci.cpp b/examples/Fibonacci/fibonacci.cpp
new file mode 100644
index 0000000..cd35417
--- /dev/null
+++ b/examples/Fibonacci/fibonacci.cpp
@@ -0,0 +1,121 @@
+//===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Valery A. Khamenya and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This small program provides an example of how to build quickly a small module
+// with function Fibonacci and execute it with the JIT.
+//
+// The goal of this snippet is to create in the memory the LLVM module
+// consisting of one function as follow:
+//
+// int fib(int x) {
+// if(x<=2) return 1;
+// return fib(x-1)+fib(x-2);
+// }
+//
+// Once we have this, we compile the module via JIT, then execute the `fib'
+// function and return result to a driver, i.e. to a "host program".
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include <iostream>
+using namespace llvm;
+
+static Function *CreateFibFunction(Module *M) {
+ // Create the fib function and insert it into module M. This function is said
+ // to return an int and take an int parameter.
+ Function *FibF =
+ cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function.
+ BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
+
+ // Get pointers to the constants.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+
+ // Get pointer to the integer argument of the add1 function...
+ Argument *ArgX = FibF->arg_begin(); // Get the arg.
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the true_block.
+ BasicBlock *RetBB = new BasicBlock("return", FibF);
+ // Create an exit block.
+ BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
+
+ // Create the "if (arg < 2) goto exitbb"
+ Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
+ new BranchInst(RetBB, RecurseBB, CondInst, BB);
+
+ // Create: ret int 1
+ new ReturnInst(One, RetBB);
+
+ // create fib(x-1)
+ Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
+ CallInst *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
+ CallFibX1->setTailCall();
+
+ // create fib(x-2)
+ Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
+ CallInst *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
+ CallFibX2->setTailCall();
+
+
+ // fib(x-1)+fib(x-2)
+ Value *Sum = BinaryOperator::createAdd(CallFibX1, CallFibX2,
+ "addresult", RecurseBB);
+
+ // Create the return instruction and add it to the basic block
+ new ReturnInst(Sum, RecurseBB);
+
+ return FibF;
+}
+
+
+int main(int argc, char **argv) {
+ int n = argc > 1 ? atol(argv[1]) : 24;
+
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ // We are about to create the "fib" function:
+ Function *FibF = CreateFibFunction(M);
+
+ // Now we going to create JIT
+ ExistingModuleProvider *MP = new ExistingModuleProvider(M);
+ ExecutionEngine *EE = ExecutionEngine::create(MP, false);
+
+ std::cerr << "verifying... ";
+ if (verifyModule(*M)) {
+ std::cerr << argv[0] << ": Error constructing function!\n";
+ return 1;
+ }
+
+ std::cerr << "OK\n";
+ std::cerr << "We just constructed this LLVM module:\n\n---------\n" << *M;
+ std::cerr << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
+
+ // Call the Fibonacci function with argument n:
+ std::vector<GenericValue> Args(1);
+ Args[0].IntVal = APInt(32, n);
+ GenericValue GV = EE->runFunction(FibF, Args);
+
+ // import result of execution
+ std::cout << "Result: " << GV.IntVal.toString(10) << "\n";
+ return 0;
+}
diff --git a/examples/HowToUseJIT/HowToUseJIT.cpp b/examples/HowToUseJIT/HowToUseJIT.cpp
new file mode 100644
index 0000000..621621d
--- /dev/null
+++ b/examples/HowToUseJIT/HowToUseJIT.cpp
@@ -0,0 +1,112 @@
+//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Valery A. Khamenya and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This small program provides an example of how to quickly build a small
+// module with two functions and execute it with the JIT.
+//
+// Goal:
+// The goal of this snippet is to create in the memory
+// the LLVM module consisting of two functions as follow:
+//
+// int add1(int x) {
+// return x+1;
+// }
+//
+// int foo() {
+// return add1(10);
+// }
+//
+// then compile the module via JIT, then execute the `foo'
+// function and return result to a driver, i.e. to a "host program".
+//
+// Some remarks and questions:
+//
+// - could we invoke some code using noname functions too?
+// e.g. evaluate "foo()+foo()" without fears to introduce
+// conflict of temporary function name with some real
+// existing function name?
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include <iostream>
+using namespace llvm;
+
+int main() {
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ // Create the add1 function entry and insert this entry into module M. The
+ // function will have a return type of "int" and take an argument of "int".
+ // The '0' terminates the list of argument types.
+ Function *Add1F =
+ cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function. As before, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
+
+ // Get pointers to the constant `1'.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+
+ // Get pointers to the integer argument of the add1 function...
+ assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
+ Argument *ArgX = Add1F->arg_begin(); // Get the arg
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the add instruction, inserting it into the end of BB.
+ Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
+
+ // Create the return instruction and add it to the basic block
+ new ReturnInst(Add, BB);
+
+ // Now, function add1 is ready.
+
+
+ // Now we going to create function `foo', which returns an int and takes no
+ // arguments.
+ Function *FooF =
+ cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
+
+ // Add a basic block to the FooF function.
+ BB = new BasicBlock("EntryBlock", FooF);
+
+ // Get pointers to the constant `10'.
+ Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
+
+ // Pass Ten to the call call:
+ CallInst *Add1CallRes = new CallInst(Add1F, Ten, "add1", BB);
+ Add1CallRes->setTailCall(true);
+
+ // Create the return instruction and add it to the basic block.
+ new ReturnInst(Add1CallRes, BB);
+
+ // Now we create the JIT.
+ ExistingModuleProvider* MP = new ExistingModuleProvider(M);
+ ExecutionEngine* EE = ExecutionEngine::create(MP, false);
+
+ std::cout << "We just constructed this LLVM module:\n\n" << *M;
+ std::cout << "\n\nRunning foo: " << std::flush;
+
+ // Call the `foo' function with no arguments:
+ std::vector<GenericValue> noargs;
+ GenericValue gv = EE->runFunction(FooF, noargs);
+
+ // Import result of execution:
+ std::cout << "Result: " << gv.IntVal.toString(10) << "\n";
+ return 0;
+}
diff --git a/examples/HowToUseJIT/Makefile b/examples/HowToUseJIT/Makefile
new file mode 100644
index 0000000..fbacc2d
--- /dev/null
+++ b/examples/HowToUseJIT/Makefile
@@ -0,0 +1,15 @@
+##===- examples/HowToUseJIT/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file was developed by Valery A. Khamenya and is distributed under
+# the University of Illinois Open Source License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = HowToUseJIT
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := jit interpreter native
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..d96f66c
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,19 @@
+##===- examples/Makefile -----------------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file was developed by the LLVM research group and is distributed under
+# the University of Illinois Open Source License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL=..
+
+include $(LEVEL)/Makefile.config
+
+PARALLEL_DIRS:= Fibonacci HowToUseJIT ModuleMaker
+
+ifeq ($(HAVE_PTHREAD),1)
+PARALLEL_DIRS += ParallelJIT
+endif
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/ModuleMaker/Makefile b/examples/ModuleMaker/Makefile
new file mode 100644
index 0000000..8bb934f
--- /dev/null
+++ b/examples/ModuleMaker/Makefile
@@ -0,0 +1,14 @@
+##===- examples/ModuleMaker/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file was developed by the LLVM research group and is distributed under
+# the University of Illinois Open Source License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL=../..
+TOOLNAME=ModuleMaker
+EXAMPLE_TOOL = 1
+LINK_COMPONENTS := bitwriter
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/ModuleMaker/ModuleMaker.cpp b/examples/ModuleMaker/ModuleMaker.cpp
new file mode 100644
index 0000000..ed56625
--- /dev/null
+++ b/examples/ModuleMaker/ModuleMaker.cpp
@@ -0,0 +1,61 @@
+//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This programs is a simple example that creates an LLVM module "from scratch",
+// emitting it as a bitcode file to standard out. This is just to show how
+// LLVM projects work and to demonstrate some of the LLVM APIs.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include <iostream>
+using namespace llvm;
+
+int main() {
+ // Create the "module" or "program" or "translation unit" to hold the
+ // function
+ Module *M = new Module("test");
+
+ // Create the main function: first create the type 'int ()'
+ FunctionType *FT = FunctionType::get(Type::Int32Ty, std::vector<const Type*>(),
+ /*not vararg*/false);
+
+ // By passing a module as the last parameter to the Function constructor,
+ // it automatically gets appended to the Module.
+ Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
+
+ // Add a basic block to the function... again, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = new BasicBlock("EntryBlock", F);
+
+ // Get pointers to the constant integers...
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+ Value *Three = ConstantInt::get(Type::Int32Ty, 3);
+
+ // Create the add instruction... does not insert...
+ Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
+ "addresult");
+
+ // explicitly insert it into the basic block...
+ BB->getInstList().push_back(Add);
+
+ // Create the return instruction and add it to the basic block
+ BB->getInstList().push_back(new ReturnInst(Add));
+
+ // Output the bitcode file to stdout
+ WriteBitcodeToFile(M, std::cout);
+
+ // Delete the module and all of its contents.
+ delete M;
+ return 0;
+}
diff --git a/examples/ModuleMaker/README.txt b/examples/ModuleMaker/README.txt
new file mode 100644
index 0000000..ecbe30e
--- /dev/null
+++ b/examples/ModuleMaker/README.txt
@@ -0,0 +1,8 @@
+//===----------------------------------------------------------------------===//
+// ModuleMaker Sample project
+//===----------------------------------------------------------------------===//
+
+This project is an extremely simple example of using some simple pieces of the
+LLVM API. The actual executable generated by this project simply emits an
+LLVM bytecode file to standard output. It is designed to show some basic
+usage of LLVM APIs, and how to link to LLVM libraries.
diff --git a/examples/ParallelJIT/Makefile b/examples/ParallelJIT/Makefile
new file mode 100644
index 0000000..f183da2
--- /dev/null
+++ b/examples/ParallelJIT/Makefile
@@ -0,0 +1,17 @@
+##===- examples/ParallelJIT/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file was developed by Reid A. Spencer and is distributed under
+# the University of Illinois Open Source License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = ParallelJIT
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := jit interpreter native
+
+include $(LEVEL)/Makefile.common
+
+LIBS += -lpthread
diff --git a/examples/ParallelJIT/ParallelJIT.cpp b/examples/ParallelJIT/ParallelJIT.cpp
new file mode 100644
index 0000000..31cadc3
--- /dev/null
+++ b/examples/ParallelJIT/ParallelJIT.cpp
@@ -0,0 +1,298 @@
+//===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Evan Jones and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Parallel JIT
+//
+// This test program creates two LLVM functions then calls them from three
+// separate threads. It requires the pthreads library.
+// The three threads are created and then block waiting on a condition variable.
+// Once all threads are blocked on the conditional variable, the main thread
+// wakes them up. This complicated work is performed so that all three threads
+// call into the JIT at the same time (or the best possible approximation of the
+// same time). This test had assertion errors until I got the locking right.
+
+#include <pthread.h>
+#include "llvm/Module.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include <iostream>
+using namespace llvm;
+
+static Function* createAdd1(Module *M) {
+ // Create the add1 function entry and insert this entry into module M. The
+ // function will have a return type of "int" and take an argument of "int".
+ // The '0' terminates the list of argument types.
+ Function *Add1F =
+ cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function. As before, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
+
+ // Get pointers to the constant `1'.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+
+ // Get pointers to the integer argument of the add1 function...
+ assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
+ Argument *ArgX = Add1F->arg_begin(); // Get the arg
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the add instruction, inserting it into the end of BB.
+ Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
+
+ // Create the return instruction and add it to the basic block
+ new ReturnInst(Add, BB);
+
+ // Now, function add1 is ready.
+ return Add1F;
+}
+
+static Function *CreateFibFunction(Module *M) {
+ // Create the fib function and insert it into module M. This function is said
+ // to return an int and take an int parameter.
+ Function *FibF =
+ cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function.
+ BasicBlock *BB = new BasicBlock("EntryBlock", FibF);
+
+ // Get pointers to the constants.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+
+ // Get pointer to the integer argument of the add1 function...
+ Argument *ArgX = FibF->arg_begin(); // Get the arg.
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the true_block.
+ BasicBlock *RetBB = new BasicBlock("return", FibF);
+ // Create an exit block.
+ BasicBlock* RecurseBB = new BasicBlock("recurse", FibF);
+
+ // Create the "if (arg < 2) goto exitbb"
+ Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
+ new BranchInst(RetBB, RecurseBB, CondInst, BB);
+
+ // Create: ret int 1
+ new ReturnInst(One, RetBB);
+
+ // create fib(x-1)
+ Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
+ Value *CallFibX1 = new CallInst(FibF, Sub, "fibx1", RecurseBB);
+
+ // create fib(x-2)
+ Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
+ Value *CallFibX2 = new CallInst(FibF, Sub, "fibx2", RecurseBB);
+
+ // fib(x-1)+fib(x-2)
+ Value *Sum =
+ BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
+
+ // Create the return instruction and add it to the basic block
+ new ReturnInst(Sum, RecurseBB);
+
+ return FibF;
+}
+
+struct threadParams {
+ ExecutionEngine* EE;
+ Function* F;
+ int value;
+};
+
+// We block the subthreads just before they begin to execute:
+// we want all of them to call into the JIT at the same time,
+// to verify that the locking is working correctly.
+class WaitForThreads
+{
+public:
+ WaitForThreads()
+ {
+ n = 0;
+ waitFor = 0;
+
+ int result = pthread_cond_init( &condition, NULL );
+ assert( result == 0 );
+
+ result = pthread_mutex_init( &mutex, NULL );
+ assert( result == 0 );
+ }
+
+ ~WaitForThreads()
+ {
+ int result = pthread_cond_destroy( &condition );
+ assert( result == 0 );
+
+ result = pthread_mutex_destroy( &mutex );
+ assert( result == 0 );
+ }
+
+ // All threads will stop here until another thread calls releaseThreads
+ void block()
+ {
+ int result = pthread_mutex_lock( &mutex );
+ assert( result == 0 );
+ n ++;
+ //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
+
+ assert( waitFor == 0 || n <= waitFor );
+ if ( waitFor > 0 && n == waitFor )
+ {
+ // There are enough threads blocked that we can release all of them
+ std::cout << "Unblocking threads from block()" << std::endl;
+ unblockThreads();
+ }
+ else
+ {
+ // We just need to wait until someone unblocks us
+ result = pthread_cond_wait( &condition, &mutex );
+ assert( result == 0 );
+ }
+
+ // unlock the mutex before returning
+ result = pthread_mutex_unlock( &mutex );
+ assert( result == 0 );
+ }
+
+ // If there are num or more threads blocked, it will signal them all
+ // Otherwise, this thread blocks until there are enough OTHER threads
+ // blocked
+ void releaseThreads( size_t num )
+ {
+ int result = pthread_mutex_lock( &mutex );
+ assert( result == 0 );
+
+ if ( n >= num ) {
+ std::cout << "Unblocking threads from releaseThreads()" << std::endl;
+ unblockThreads();
+ }
+ else
+ {
+ waitFor = num;
+ pthread_cond_wait( &condition, &mutex );
+ }
+
+ // unlock the mutex before returning
+ result = pthread_mutex_unlock( &mutex );
+ assert( result == 0 );
+ }
+
+private:
+ void unblockThreads()
+ {
+ // Reset the counters to zero: this way, if any new threads
+ // enter while threads are exiting, they will block instead
+ // of triggering a new release of threads
+ n = 0;
+
+ // Reset waitFor to zero: this way, if waitFor threads enter
+ // while threads are exiting, they will block instead of
+ // triggering a new release of threads
+ waitFor = 0;
+
+ int result = pthread_cond_broadcast( &condition );
+ assert( result == 0 );
+ }
+
+ size_t n;
+ size_t waitFor;
+ pthread_cond_t condition;
+ pthread_mutex_t mutex;
+};
+
+static WaitForThreads synchronize;
+
+void* callFunc( void* param )
+{
+ struct threadParams* p = (struct threadParams*) param;
+
+ // Call the `foo' function with no arguments:
+ std::vector<GenericValue> Args(1);
+ Args[0].IntVal = APInt(32, p->value);
+
+ synchronize.block(); // wait until other threads are at this point
+ GenericValue gv = p->EE->runFunction(p->F, Args);
+
+ return (void*)(intptr_t)gv.IntVal.getZExtValue();
+}
+
+int main()
+{
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ Function* add1F = createAdd1( M );
+ Function* fibF = CreateFibFunction( M );
+
+ // Now we create the JIT.
+ ExistingModuleProvider* MP = new ExistingModuleProvider(M);
+ ExecutionEngine* EE = ExecutionEngine::create(MP, false);
+
+ //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
+ //~ std::cout << "\n\nRunning foo: " << std::flush;
+
+ // Create one thread for add1 and two threads for fib
+ struct threadParams add1 = { EE, add1F, 1000 };
+ struct threadParams fib1 = { EE, fibF, 39 };
+ struct threadParams fib2 = { EE, fibF, 42 };
+
+ pthread_t add1Thread;
+ int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ pthread_t fibThread1;
+ result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ pthread_t fibThread2;
+ result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ synchronize.releaseThreads(3); // wait until other threads are at this point
+
+ void* returnValue;
+ result = pthread_join( add1Thread, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
+
+ result = pthread_join( fibThread1, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
+
+ result = pthread_join( fibThread2, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
+
+ return 0;
+}