The LLVM HelloWorld pass from the tutorial does not run if the IR is produced by clang

Viewed 377

I am very new to LLVM/clang and trying to write my custom LLVM pass using the new pass manager.

My first step was to use the HelloWorld pass from the official documentation.

It works fine when I am using the file a.ll provided by the documentation with the command ./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
}

Now I have created a C file a2.c with:

void test(){
}

And generate the IR with ./bin/clang -S -emit-llvm a2.c

Running the previous opt command on a2.ll gives

; 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)"}

I can not see the expected "test" at the beginning of the file, therefore my pass is not run by the PassManager.

Any ideas on what is wrong with my pass? Thanks for your help.

[EDIT]

Using clang flags -O1 or more solves the issue but I do not understand why.

1 Answers

clang with -O0 adds optnone attribute that disables any further processing of the IR by transformation passes.

Related