如何收集LLVM IR中受特定优化影响的函数?

问题描述 投票:0回答:1

在具有多个功能的模块中,我想知道哪些功能已被特定的内置通道修改,而无需对其进行检测。例如,我可以比较原始模块和修改后的模块,看看哪些功能不同,但这太耗时了。

使用 OPT,是否有一种编程方式来确定哪些函数的内容已被 LLVM IR 中的特定优化过程修改?

可以

opt
转储此信息给我吗?

如果没有,

llvm::PassManager
上有什么东西可以用来创建这样的功能吗?

compiler-construction llvm llvm-ir
1个回答
0
投票

要确定哪些函数已被 LLVM IR 中的特定优化过程修改而无需对其进行检测,您可以使用带有

-analyze
选项的 opt 工具与
desired pass
相结合。不幸的是,opt 不直接提供内置选项来转储此信息,但您可以使用
custom passes
scripting
来实现。这是一般方法:

自定义通道:您可以编写自定义 LLVM 通道来分析 IR 并识别特定优化通道修改的函数。此遍可以检查优化遍之前和之后的 IR 并比较它们以识别变化。然后您可以编译此过程并使用

opt
运行它。

脚本:或者,您可以使用脚本来实现类似的功能。编写一个脚本,使用您的优化过程运行 opt 并捕获该过程之前和之后的 IR。然后,比较传递前后每个函数的 IR,以识别修改的函数。 llvm::PassManager:虽然 llvm::PassManager 中没有直接功能来创建此类功能

out-of-the-box
,但您可以通过创建分析和操作 IR 的自定义通道来扩展它,以实现您想要的结果。

以下是如何使用 Python 和 LLVM IR 编写脚本的高级示例:

import subprocess

def run_opt_pass(input_file, optimization_pass):
    # Run opt with the specified optimization pass
    command = ['opt', '-analyze', '-passes=' + optimization_pass, 
    input_file]
    result = subprocess.run(command, capture_output=True, text=True)
    return result.stdout

def main():
    input_file = 'input.ll'
    optimization_pass = 'your_optimization_pass_name'
    
    # Run the optimization pass and capture the IR changes
    modified_functions = run_opt_pass(input_file, optimization_pass)
    
    # Process modified functions
    # For example, you could parse the output to extract the modified function names
    
    print("Modified Functions:")
    print(modified_functions)

if __name__ == "__main__":
    main()

在此脚本中,

run_opt_pass
函数使用指定的优化过程执行 opt 并捕获输出。然后,您可以处理此输出以识别修改后的函数。将
your_optimization_pass_name
替换为您要分析的优化过程的名称。

© www.soinside.com 2019 - 2024. All rights reserved.