[cfe-dev] using clangs rewrite capabilities

Toralf Niebuhr niebuhr at niebuhrt.de
Fri Nov 19 07:52:51 PST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi


I have got the following problem.
Out university is developing a process based simulation library. It's been working really well, but our last project using it, run into some problems because the library doesn't scale to well when simulating many processes (>1000).

First of all some implementation insights:

Every process has its own main function. Since we want reproducible simulation results, they are not run as threads, but rather as coroutines implemented as Windows fibers or with ucontext. Each process now, has the ability at given points to pause and activates the scheduler process.

A different approach without coroutines would be to do event based simulations. But those are not really intuitive to write.

A better idea would be to turn each processes main function into a state machine (by the compiler). All points, where the process would yield to the scheduler would be entry points of a huge switch statement. And later, when the process would be rescheduled, the process would just switch to the state, that is after its last scheduling operation.
This way there would be only one process and no threads/coroutines.


for example:

class Process1 {

	int main() {

		foo();
		yield();
		bar();
		return 0;
	}
};

would be translated to

class Process1 {
	int ret;
	bool running;

	void main() {
		static int state = START;
		running = true;

		switch (state) {
		case START:
			foo();
			state = __S1;
			return;
		case __S1 :
			bar();
			running = false;
			ret = 0;
			return;
		}
	}
};



Obviously START __S1 should be defined by the compiler as well and should be part of an enum instead of int.
Another thing that makes it not so trivial is, that the member functions foo() and bar could have scheduling operations as well.
As we are not really interested in the actual code of the state machine, a transformation of the llvm code would be sufficient as well.

Where should I start to look at, if I would want to implement this? Has something similar been done before with clang/llvm?


Toralf Niebuhr




-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.16 (Darwin)

iF4EAREIAAYFAkzmnVYACgkQ06zpjJY8ywWq0gD+JAQUM7S3E/0drFk/dyxK9Uqb
QbqnHfabLHYDGBBUTFQBAIXJ3xeH8ibBzYim07dzgg7hiMW8Re9MUIlO+znOoOYR
=rjz7
-----END PGP SIGNATURE-----




More information about the cfe-dev mailing list