[论文解读] Challenges and Opportunities in C/C++ Source-To-Source Compilation (Invited Paper)
本文提出 Verificarlo,一种基于 LLVM 的编译器工具,可对 C/C++/Fortran 代码实现自动、透明的蒙特卡洛算术(MCA),以评估浮点数精度。通过在 LLVM 中间表示(IR)层面进行插桩,Verificarlo 能够捕捉编译器优化对数值稳定性的影响,在检测数值噪声和提供可靠置信区间方面优于 CADNA 和基于 CIL 的 MCA,且无需修改源代码。
The C/C++ compilation stack (Intermediate Representations (IR), compilation passes and backends) is encumbered by a steep learning curve, which we believe can be lowered by complementing it with approaches such as source-to-source compilation. Source-to-source compilation is a technology that is widely used and quite mature in certain programming environments, such as JavaScript, but that faces a low adoption rate in others. In the particular case of C and C++ some of the identified factors include the high complexity of the languages, increased difficulty in building and maintaining C/C++ parsers, or limitations on using source code as an intermediate representation. Additionally, new technologies such as Multi-Level Intermediate Representation (MLIR) have appeared as potential competitors to source-to-source compilers at this level. In this paper, we present what we have identified as current challenges of source-to-source compilation of C and C++, as well as what we consider to be opportunities and possible directions forward. We also present several examples, implemented on top of the Clava source-to-source compiler, that use some of these ideas and techniques to raise the abstraction level of compiler research on complex compiled languages such as C or C++. The examples include automatic parallelization of for loops, high-level synthesis optimisation, hardware/software partitioning with run-time decisions, and automatic insertion of inline assembly for fast prototyping of custom instructions.
研究动机与目标
- 解决科学计算中对自动化、可扩展的数值精度验证的迫切需求,特别是在百亿亿次计算和高度并行的工作负载中。
- 克服现有工具(如 CADNA 和 CIL)的局限性,这些工具需要手动修改源代码,或无法支持编译器优化影响的分析。
- 提供一种完全自动化的端到端解决方案,通过在中间表示层使用蒙特卡洛算术估算浮点结果的置信区间。
- 在传统方法因高估或缺乏透明性而失效的大规模科学代码中,实现对数值不稳定的准确检测。
提出的方法
- 在 LLVM 编译器基础设施的中间表示(IR)层级集成蒙特卡洛算术(MCA),实现无需修改源代码的透明插桩。
- 实现三种 MCA 模式:随机舍入(RR)、精度边界(PB)和完整 MCA,通过向浮点数运算注入随机扰动来模拟舍入误差。
- 使用函数 inexact(x) = x + 2ex−tξ 建模虚拟精度 t,其中 ξ 是 (−0.5, 0.5) 上的均匀随机变量,以实现无偏误差模拟。
- 在 MCA 执行期间使用 MPFR 进行高精度算术运算,以确保对结果均值和标准差的准确统计估计。
- 通过公式 s′ = −logβ(σ / |μ|) 计算有效数字,其中 μ 和 σ 分别为多次 MCA 试验下的样本均值和标准差。
- 通过 LLVM 的语言无关 IR 支持所有主流科学编程语言(C、C++、Fortran),实现跨优化分析。
实验结果
研究问题
- RQ1蒙特卡洛算术能否在 LLVM 中间表示层级被有效且透明地应用,以评估浮点数精度,且无需修改源代码?
- RQ2Verificarlo 基于 MCA 的方法在检测数值不稳定性与估算有效数字方面,与 CADNA 和基于 CIL 的 MCA 相比表现如何?
- RQ3编译器优化在多大程度上影响数值精度?能否通过基于编译器的 MCA 工具捕捉并量化这种影响?
- RQ4Verificarlo 是否能可靠检测出浮点结果本质上为数值噪声的情况,例如灾难性抵消或有效数字丧失?
主要发现
- Verificarlo 准确检测出交替计数器案例产生零有效数字的数值噪声,尽管 IEEE-754 返回非零值,仍正确识别结果不可靠。
- 在交替计数器案例中,Verificarlo 报告平均结果为 ¯c = −45.101042,标准差为 σ(c) = 44.33(基于 1000 次采样),得出 s′ = 0 个有效数字,表明无可靠信息。
- CADNA 在同一案例中高估有效数字,报告 s = 3,且错误估计均值为 −0.103 × 10⁶,远偏离真实期望值 −50。
- 对于线性方程组,Verificarlo 在单精度下估计为 8.3 个有效数字,在双精度下为 8.2 个,与预期精度高度一致。
- Verificarlo 的 MCA 结果显示出有效数字在迭代过程中稳定演化,而 CADNA 则表现出虚假且不一致的估计。
- Verificarlo 成功捕捉了编译器优化对数值精度的影响——这是 CIL 等源到源工具所不具备的关键优势,对真实 HPC 工作负载具有重要意义。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。