LLVM:我必须设置目标布局还是设置目标三元组更好?

LLVM: Do I have to set a target layout or is it better to set a target triple?

我正在学习 LLVM 框架的基础知识。我阅读了描述数据类型的目标布局以及描述机器和 OS 的目标三元组。 然而,我无法找到关于什么是真正必须设置的以及为什么要设置的信息。 我找到了设置其中 none 或其中一个或另一个设置的示例。显然,对于一个工作的 IR 文件,没有必要设置任何一个,但我的印象是开发人员希望我在那里设置一些东西,否则为什么会暴露这个接口?

我的理解是,根据目标三元组,LLVM应该可以自己推导出正确的数据布局,但如果是这样,为什么可以手动设置数据布局。

那么正确的做法是什么?我应该选择哪种方式?设置目标三元组或手动定义完整布局?

DataLayout 将成为强制性的,因此我强烈建议您使用它。能够在没有 Triple 的情况下放入 DataLayout 的原因是您可以拥有需要 DataLayout 才能工作的目标独立转换。当你到达那个点时,三元组将影响代码生成。

将来我们希望有一个 API 前端编写者可以 use/modify 这将使他们能够从后端获得默认的三元组。

LLVM 人员建议同时设置目标三元组和数据布局规范。

Make sure that your Modules contain both a data layout specification and target triple. Without these pieces, non of the target specific optimization will be enabled. This can have a major effect on the generated code quality.

来自http://llvm.org/docs/Frontend/PerformanceTips.html#the-basics

有一个LLVMGetDefaultTargetTriple函数可以获取本地机器的目标规范。

你说:

My understanding is, that based on the target triple, LLVM should be able to derive the correct datalayout by itself, but if that is so, why is it possible to set the data layout by hand.

我也是这么想的。但实际上,优化器需要明确设置数据布局。否则它会采用默认布局,这可能会破坏您的代码。参见示例: https://bugs.llvm.org/show_bug.cgi?id=45188