开发者

Creating a VHDL backend for LLVM?

开发者 https://www.devze.com 2023-01-15 00:43 出处:网络
LLVM is very modular and allows you to fairly easily define new backends.However most of the documentation/tutorials on creating an LLVM backend focus on adding a new processor instruction set and reg

LLVM is very modular and allows you to fairly easily define new backends. However most of the documentation/tutorials on creating an LLVM backend focus on adding a new processor instruction set and registers. I'm wondering what it would take to create a VHDL backend for LLVM? Are there examples of using LLVM to go from one higher level language to another?

Just to clarify: are there examples of translating LLVM IR to a hi开发者_如何学Gogher level language instead of to an assembly language? For example: you could read in C with Clang, use LLVM to do some optimization and then write out code in another language like Java or maybe Fortran.


Yes !

There are many LLVM back-end targeting VHDL/Verilog around :

  • (open source) Legup paper
  • (commercial) Xilinx HLS
  • (online) C-to-verilog

And I know there are many others...

The interesting thing about such low-level representations as LLVM or GIMPLE (also called RTL by the the way) is that they expose static-single assignments (SSA) forms : this can be translated to hardware quite directly, as SSA can be seen as a tree of multiplexers...


There's nothing really special about the LLVM IR. It's a standard DAG with variable arity. Decompiling LLVM IR is a lot like decompiling machine language.

You might be able to leverage some frontend optimizations such as constant folding, but that sounds pretty minor compared to the whole task.

My only experience with LLVM was writing a binary translator for a class project, from a toy CISC to a custom RISC.

I'd say, since it's the closest thing to a standard IR (well, GCC GIMPLE is a close second), see if it fits with your algorithms and style and evaluate it as one alternative.

Note that GCC also started out prioritizing portability above all, and has also accomplished a lot.


I'm not sure I follow how parts of your question relate one to another.

To target LLVM into a high-level language like C is very possible and you seem to have found one reference point.

VHDL is a whole other business however. Do you consider VHDL a high-level language? It may be, but but describing hardware/logic. Sure VHDL has some constructs that you can employ to actually program in it, but it's hardly a fruitful endeavor. VHDL describes hardware and thus makes translating LLVM IR into it a very hard problem, unless of course you design a CPU with a custom instruction set in VHDL and translate LLVM IR into your instructions.


This thread was one of the first things I found while looking for the same thing.

I found a project that's rather far along that cleanly builds under/with llvm 3.5. It's pretty darn cool. It spits out HDL and does various other cool FPGA related things. While it's designed to work with TTAs and generate images for FPGA (or simulate them), it can probably also be made to do some trivial HDL generation from c functions.

It was perfect for my purposes because I wanted to upload to an Altera FPGA, and the fpga_stdout example even spits out Quartus build scripts and project files.

TTA-Based Co-design Environment

I also tried the things listed in the accepted answer and a couple others and found that they weren't going to work for me or weren't very high quality (usually both). TCE is professional feeling, but purely academic I believe. Very nice all the way around.


It seems the question was partially answered, so I’d like to give it a shot:

  • What it would take to create a VHDL backend for LLVM?

  • What it would take to translate LLVM IR to a higher level language (presumably with the intention of converting between high-level langs)?

I will give you some background on 2. And expand at a later date on 1.

If you want to convert LLVM IR to a high-level language such as C or Java:

You would have to take the LLVM instructions, and abstract that out into its equivalent C code. Then you need to take the remaining features that LLVM does not have an equivalent for (like classes and abstractions for C++) and write a routine that would find those patterns in the LLVM (like reused blocks) and write C. For the basic stuff, its pretty straightforward. But, just follow the train of thought and you quickly find yourself realizing the true difficultly of the problem, after all not everyone writes simple C. To compound the difficulty further, you may not get the same LLVM IR when compiling the generated C! (Consider the resulting feedback loop)

As for Java, you are in for an even harder battle going direct from LLVM IR, and in either case still have the problem you likely won't get the same code compiling to LLVM IR, if one even can do that. Rather, you would translate LLVM IR to JVM Bytecode. Then you could use a reverse compiler to get your Java.

A group of Chinese students was apparently able to do this, but they wondered why such little interest in their research. I would say its bc they don't fully understand just what the LLVM guys have done, and how it is better than the JVM. (In fact, LLVM arguably makes the JVM obsolete ;)

Even though this seems useful in that one can use LLVM as an intermediary between C and Java to convert bidirectionally, this solution is actually of little use because we are asking the wrong question. See, the entire reason you would want that for practical purposes is to have a common code base and increase performance.

But the real problem is that we need a language that has abstracted the common features of modern languages, and that gives you a central language that you can build from. http://julialang.org/ has answered the question

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号