为 LLVM 创建 VHDL 后端?

发布于 2024-09-17 19:06:40 字数 250 浏览 6 评论 0 原文

LLVM 非常模块化,允许您相当轻松地定义新的后端。然而,大多数有关创建 LLVM 后端的文档/教程都侧重于添加新的处理器指令集和寄存器。我想知道为 LLVM 创建 VHDL 后端需要什么?是否有使用 LLVM 从一种高级语言过渡到另一种高级语言的示例?

只是为了澄清:是否有将 LLVM IR 翻译为高级语言而不是汇编语言的示例?例如:您可以使用 Clang 读取 C 语言,使用 LLVM 进行一些优化,然后用另一种语言(如 Java 或 Fortran)编写代码。

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 higher 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

白龙吟 2024-09-24 19:06:40

是的 !

有许多针对 VHDL/Verilog 的 LLVM 后端:(

我知道还有很多其他的...

关于 LLVM 或 GIMPLE(顺便说一下,也称为 RTL)等低级表示的有趣之处在于,它们公开了静态 -单分配(SSA)形式:这可以直接转换为硬件,因为 SSA 可以被视为多路复用器树......

Yes !

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

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...

北方的巷 2024-09-24 19:06:40

LLVM IR 没有什么特别之处。它是一个具有可变数量的标准 DAG。反编译 LLVM IR 很像反编译机器语言。

您也许可以利用一些前端优化,例如不断折叠,但这与整个任务相比听起来相当小。

我使用 LLVM 的唯一经验是为一个类项目编写一个二进制翻译器,从玩具 CISC 到自定义 RISC。

我想说,因为它是最接近标准 IR 的东西(嗯, GCC GIMPLE 是紧随其后),看看它是否适合您的算法和风格,并将其作为一种替代方案进行评估。

请注意,GCC 也一开始就将可移植性放在首位,并且也取得了很多成就。

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.

假扮的天使 2024-09-24 19:06:40

我不确定我是否理解您问题的部分内容如何相互关联。

将 LLVM 瞄准 C 等高级语言是很有可能的,而且您似乎已经找到了一个参考点。

然而,VHDL 完全是另一回事。您认为 VHDL 是一种高级语言吗?它可能是,但只是描述硬件/逻辑。当然,VHDL 具有一些您可以用来实际编程的结构,但这并不是一个富有成果的努力。 VHDL 描述硬件,因此将 LLVM IR 翻译成硬件是一个非常困难的问题,当然,除非您使用 VHDL 中的自定义指令集设计 CPU 并将 LLVM IR 翻译成您的指令。

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.

北恋 2024-09-24 19:06:40

该线程是我在寻找相同内容时首先发现的内容之一。

我发现了一个相当长的项目,可以在 llvm 3.5 下/使用 llvm 3.5 干净地构建。这真是太酷了。它吐出 HDL 并执行各种其他与 FPGA 相关的很酷的事情。虽然它被设计为与 TTA 一起工作并为 FPGA 生成图像(或模拟它们),但它也可能可以从 C 函数生成一些简单的 HDL。

它非常适合我的目的,因为我想上传到 Altera FPGA,fpga_stdout 示例甚至可以输出 Quartus 构建脚本和项目文件。

基于 TTA 的协同设计环境

我还尝试了已接受的答案中列出的内容以及其他一些内容,发现它们不适合我或者质量不是很高(通常两者都是)。 TCE是专业的感觉,但我认为纯粹是学术性的。到处都非常好。

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.

彼岸花ソ最美的依靠 2024-09-24 19:06:40

看来这个问题得到了部分回答,所以我想尝试一下:

  • 为 LLVM 创建 VHDL 后端需要什么?

  • 如何将 LLVM IR 翻译为高级语言(大概是为了在高级语言之间进行转换)?

我将为您提供有关 2 的一些背景知识。并在稍后扩展 1。

如果您想将 LLVM IR 转换为高级语言(例如 C 或 Java):

您必须采用 LLVM 指令,并将其抽象出来转换成其等效的 C 代码。然后,您需要采用 LLVM 没有等效项的其余功能(例如 C++ 的类和抽象)并编写一个例程,在 LLVM 中查找这些模式(例如重用的块)并编写 C。对于基本内容,它非常简单。但是,只要顺着思路走,你很快就会发现自己意识到问题的真正困难,毕竟不是每个人都会编写简单的 C。更复杂的是,在编译生成的 C 时,你可能不会得到相同的 LLVM IR! (考虑由此产生的反馈循环)

对于 Java,您将面临一场直接从 LLVM IR 进行的更加艰苦的战斗,并且在任何一种情况下仍然存在问题,您可能不会将相同的代码编译为 LLVM IR,即使是可以做到这一点。相反,您可以将 LLVM IR 转换为 JVM 字节码。然后你可以使用反向编译器来获取你的Java。

一群中国学生显然能够做到这一点,但他们想知道为什么人们对他们的研究如此不感兴趣。我想说的是,他们并没有完全理解 LLVM 的人做了什么,以及它如何比 JVM 更好。 (事实上​​,LLVM 可以说使 JVM 过时了;)

尽管这看起来很有用,因为人们可以使用 LLVM 作为 C 和 Java 之间的中介来进行双向转换,但这种解决方案实际上没什么用,因为我们问了错误的问题。你看,出于实际目的,您希望这样做的全部原因是拥有一个通用的代码库并提高性能。

但真正的问题是,我们需要一种抽象了现代语言的共同特征的语言,并且为您提供了一种可以用来构建的中心语言。 http://julialang.org/ 已回答了问题

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 ????

心不设防 2024-09-24 19:06:40

看起来最好的起点是 LLVM 源中的 CBackend:

llvm/lib/Target/CBackend/CBackend.cpp

Looks like the best place to start is with the CBackend in the LLVM source:

llvm/lib/Target/CBackend/CBackend.cpp

八巷 2024-09-24 19:06:40

tl,dr:我认为 LLVM 不是正确的工具,

您正在寻找的是将 LLVM 代码转换为更高语言的方法,这就是 emscripten 用于 Javascript。

但看起来您有点错过了 LLVM 的要点,因为它旨在生成静态代码,以实现它们为此目的使用特定的中间语言构建。

正如你所看到的,emscripten 的工作方式是通过实现一个堆栈,但如果不使用 javascript,人类就可以做到这一点。

它们是几个试图实现您最初问题的项目,例如 MyHDL 将 python 转换为 VHDL或 Verilog。

tl,dr: I don't think LLVM is the right tool

What your are looking for is way to translate LLVM code to a higher language that's what emscripten do for Javascript.

But it looks like you miss a bit the point of LLVM as it's meant to generate static code in order to achieve that they use a specific intermediate language build for that purpose.

As you can see the way emscripten works is by implementing a stack, but without using javascript as a human would have done it.

They are several project that try to achieve what you original question was, like MyHDL that turns python to VHDL or Verilog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文