I am wondering if it is possible to have a program that interprets programming code and is capable of turning that code into an optimized circuit. Is this possible, and if so, does it exist?
-
$\begingroup$ At present I think the question is too broad and thus not likely to yield an answer that is useful to you. Can you narrow down your question? Why do you ask? What problem do you face that has prompted this question? Is there a specific program you want to compile in this way? A specific programming language? Why do you want a circuit, and what do you plan to do with the circuit? $\endgroup$– D.W. ♦Commented Jul 31, 2014 at 23:48
-
1$\begingroup$ I was looking for a way to make specialized hardware for simple programs that do heavy calculations. A good example would be a raytracer or hash algorithm. $\endgroup$– Tyler ScottCommented Aug 1, 2014 at 4:46
-
1$\begingroup$ The Wikipedia article "C to HDL" might be of interest. $\endgroup$– user4577Commented Aug 1, 2014 at 13:11
-
$\begingroup$ Ahhh. Yes, Paul Clayton's suggestion is a good one. You can also find work on compiling systems code (e.g., C code) into a form that can be executed on a FGPA, which is in a very similar vein. However, you might find that you get better performance by implementing the algorithm by hand in AHDL/VHDL, than by compiling from C code -- you'd have to experiment to be sure, but that's my guess. $\endgroup$– D.W. ♦Commented Aug 1, 2014 at 17:20
-
$\begingroup$ A ray tracer is by no means a simple program ! I doubt that specialized, programmable hardware (FPGAs) would be a good solution. Better use a GPGPU. $\endgroup$– user16034Commented Aug 29, 2023 at 6:27
3 Answers
Yes, it is certainly possible.
There are compilers that will compile from code to circuits. Classic examples would be VHDL or AHDL compilers, but you can certainly do this for any other language, to the extent that you have an upper bound on the running time of the program or that you have an upper bound on the memory use of the program and are willing to have a circuit that contains stateful elements.
In fact, you can walk down to Fry's and buy something that basically has the form you asked for: it's called a computer. That's more or less what a computer does: it takes code written in a programming language and executes it efficiently, using an optimized circuit. The circuit is fixed (it is the gates in the processor) and part of its input is data that depends upon the program you are executing (which is stored in the RAM of the computer). However, you could consider this a larger circuit where part of it is hardcoded (i.e., the program part of the input is hardcoded); then you can view a computer running a program as a big circuit with part that is universal and identical for all programs (the gates of the processor) and part that depends on the program (the hardcoded input), and this immediately gives a mapping from programs to circuits. The mapping is implemented by a compiler.
-
$\begingroup$ Part of the circuit is data? Sorry but this answer does not make any sense to me. A circuit's inputs are certainly not a part of it. $\endgroup$ Commented Feb 27, 2021 at 14:45
-
1$\begingroup$ @ShashankVM, you're right, that wording was poor. I tried an edit; see if you think it is any better or not. Maybe you can help me express this more clearly. $\endgroup$– D.W. ♦Commented Feb 28, 2021 at 7:59
there are different ways to interpret your question but based on comments (incl yours) some of this may be helpful. there is a lot of research in converting higher level programming constructs into FPGAs, field programmable gate arrays, basically hardware where circuits are dynamically reconfigurable. (aka reconfigurable computing).
there is also a special language eg "C for FPGAs", a subset of full C, that is customized for this purpose.
C for FPGAs Williams/ Dr Dobbs
FpgaC / Wikipedia
as for getting efficient circuits for particular programs, that is another area of research/ optimization eg circuit optimization/ minimization/ compression. there are also optimization heuristics built as input preprocessors into most SAT solvers. more generally this is known as logic optimization and there are various techniques/ tools for that. also the general theory/ application of optimizing compilers has some overlap with circuit optimization. one simple, near "toy" example or model of a compiler optimization in some cases is short circuit evaluation. the general area you refer to is also an active area of research both theoretical/ applied & there are many related papers on the subject.
FPGA code is just another kind of code, and it's convertible.
I guess if you're making a real machine, it could be written in C, but I'm doing one now and I'm just using sub-procedures in place of the electric modules, and it seems to be like it could work, and you can even do it on the GPU to get that awesome speed out of it.
The variables are the same between gates and code.
The variables just mark where the gates are linking and it perfectly converts (you just do them as normal), FPGA code is probably the same and does this I bet!
Just using the C code directly to make a circuit I'm not doing but its close if it works or not, but I'm talking about handcrafting the code to make it easily convertible works, and makes the conversion process less of a headache.
If you use a virtual machine to run it, it goes slower, it's best if it's direct code what you're running 'cause then you can get more out of your computer, when you're debugging what's going to be the final circuit.