Writing an LLVM Pass(question about .ll file )

Hello Expert
In Writing an LLVM Pass,(Writing an LLVM Pass — LLVM 18.0.0git documentation) I checked [Running a pass with opt].
In Eaxmple,

cat /tmp/a.ll
define i32 @foo() {
a = add i32 2, 3
ret i32 %a

define void @bar() {
ret void
.ll file is only that.

“I created a .c file, then used clang -emit-llvm -S test.c to generate a .ll file. After that, I tried build/bin/opt -disable-output /tmp/a.ll -passes=helloworld using the basic example from helloworld.cpp, but nothing was printed, and neither function names like ‘foo’ nor ‘main’ were displayed. Could you explain why this is happening? It seems that it might be because the .ll file contains more than just function definitions, such as DataLayout or FileName. In fact, when I have a .ll file that contains only function definitions with ‘define …,’ the function names are displayed correctly. Can you clarify this?”

I hope this helps clarify your question, and I’m here to assist with any further information or explanations you may need.

The functions in your .ll file might have optnone attribute attached to them. According to LLVM Lang Ref : “This function attribute indicates that most optimization passes will skip this function, with the exception of interprocedural optimization passes”

Generate .ll file using -disable-O0-optnone flag, for example:

clang -S -O0 -Xclang -disable-O0-optnone -emit-llvm foo.c -o foo.ll
1 Like

You are my benefactor. Thank you:)

If you already have a .ll/.bc file containing functions with the optnone attribute and don’t want to rerun the frontend with additional flags, you can also do the following with opt:

opt -passes=forceattrs -force-remove-attribute=optnone

This will remove the optnone attribute from all functions that have it within the module.