He Yucheng
He Yucheng

Reputation: 31

about LLVM IR: No function definition in LLVM ir code

I am looking at a llvm ir file converted from a cpp file by clang. But I found there were several functions in llvm ir file only with declaration without definition. And all these functions are not the "build-in" functions like:

declare i32 @puts(i8* nocapture)

It's like:

declare void @_ZNK5Arrow7BaseRow9getColumnINS_11IpGenPrefixEEEvtRT_(%"class.Arrow::BaseRow"*, i16 zeroext, %"class.Arrow::IpGenPrefix"* dereferenceable(24)) #0

It seems like those functions are using some external definition? I am new to LLVM IR. And I was wondering is there a way that LLVM IR can do like cpp library, I can store the functions I will use in some LLVM IR libraries and use them in a .ll file by just do something like include ?

Thanks

Upvotes: 0

Views: 1630

Answers (1)

eush77
eush77

Reputation: 4088

It seems like those functions are using some external definition?

Exactly. declare keyword indicates a function declaration, as opposed to function definition, and function declarations can only be linked externally:

It is illegal for a function declaration to have any linkage type other than external or extern_weak.

The reason Clang generated declarations instead of definitions is (most likely) that these functions were not defined in the translation unit that was given to it.

The declarations are resolved during linking. To link several LLVM modules together, use llvm-link tool.

For example, suppose lib.cpp defines function foo() which is used in main.cpp.

$ clang++ -c -emit-llvm main.cpp lib.cpp

This command compiles these files to LLVM IR and creates two modules main.bc and lib.bc. main.bc contains just a declaration of foo() because this function is defined in a separate translation unit. The definition of foo() is in lib.bc.

$ llvm-link main.bc lib.bc -o all.bc

This command links main.bc and lib.bc into a single module, which now contains the definition of foo().

Upvotes: 1

Related Questions