【问题标题】:The LLVM HelloWorld pass from the tutorial does not run if the IR is produced by clang如果 IR 由 clang 生成,则教程中的 LLVM HelloWorld 通道不会运行
【发布时间】:2021-05-12 14:29:59
【问题描述】:

我对 LLVM/clang 非常陌生,正在尝试使用新的 pass manager 编写我的自定义 LLVM pass。

我的第一步是使用来自 official documentation 的 HelloWorld 通行证。

当我使用文档提供的文件 a.ll 和命令 ./bin/opt a.ll -passes=helloworld -S 时,它工作正常

foo
bar 
; ModuleID = 'a.ll'
source_filename = "a.ll"

define i32 @foo() {
 %a = add i32 2, 3
 ret i32 %a
}
define void @bar() {
 ret void
}

现在我创建了一个 C 文件 a2.c:

void test(){
}

并使用./bin/clang -S -emit-llvm a2.c生成IR

在 a2.ll 上运行前面的 opt 命令给出

; ModuleID = 'a2.ll'
source_filename = "a2.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @test() #0 {
  entry:
  ret void
}

attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 13.0.0 (https://github.com/llvm/llvm-project.git 8e7df996e3054cc174b91bc103057747c8349c06)"}

我在文件开头看不到预期的“测试”,因此我的 pass 不是由 PassManager 运行的。

关于我的通行证有什么问题有什么想法吗? 感谢您的帮助。

[编辑]

使用 clang 标志 -O1 或更多可以解决问题,但我不明白为什么。

【问题讨论】:

    标签: clang llvm llvm-ir


    【解决方案1】:

    带有 -O0 的clang 添加了optnone 属性,该属性禁止通过转换传递对IR 进行任何进一步处理。

    【讨论】:

    • 为了获得我想要的行为,我需要在 clang 编译行中添加 -Xclang -disable-O0-optnone。谢谢 Anton,您的回答帮助我找到了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多