[llvm-dev] Looking for an out-of-source "Hello, world" LLVM/Clang pass example

213 views
Skip to first unread message

Scott Pakin via llvm-dev

unread,
Jul 10, 2019, 3:46:21 PM7/10/19
to llvm...@lists.llvm.org
I'm looking for a "Hello, world" pass example that ideally has all of
the following properties:

1) Complete. That is, it should have functional CMakeLists.txt,
etc. files.

2) Out-of-source. That is, it should build against a binary LLVM
installation.

3) Modern. That is, I don't want to use any deprecated APIs or
CMake macros.

4) Clang-compatible. That is, I'd like the pass to be able to run
directly from Clang instead of having to generate bitcode, run
opt on it, and then feed it back to Clang for linking.

Does such an example exist and if so, where can I find it?

Here's what I've considered to date:

* llvm/lib/Transforms/Hello, but this appears to be in-source only.

* http://llvm.org/docs/WritingAnLLVMPass.html, but this example
doesn't appear to be runnable directly from Clang.

* https://llvm.org/docs/CMake.html, but its proposed CMake line of
"add_llvm_library(LLVMPassname MODULE Pass.cpp)" blows up with

CMake Error at /usr/local/lib/cmake/llvm/AddLLVM.cmake:659 (install):
install TARGETS given no LIBRARY DESTINATION for module target
"Hello".
Call Stack (most recent call first):
Hello/CMakeLists.txt:3 (add_llvm_library)

(Perhaps this is fixable with additional CMake macro calls, but
that's what I list "Complete" as a desideratum.)

*
https://llvm.org/devmtg/2015-10/slides/GueltonGuinet-BuildingTestingDebuggingASimpleOutOfTreePass.pdf,
but this uses add_llvm_loadable_module in CMake, which I read in
some thread is likely to go away, and the legacy pass manager,
while is also likely to go away, to make the pass usable from
Clang.

See my predicament? What's a poor pass-writer to do?

Thanks in advance,
-- Scott
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Florian Hahn via llvm-dev

unread,
Jul 10, 2019, 4:18:10 PM7/10/19
to Scott Pakin, llvm...@lists.llvm.org
Hi,

> On Jul 10, 2019, at 20:45, Scott Pakin via llvm-dev <llvm...@lists.llvm.org> wrote:
>
> I'm looking for a "Hello, world" pass example that ideally has all of
> the following properties:
>
> 1) Complete. That is, it should have functional CMakeLists.txt,
> etc. files.
>
> 2) Out-of-source. That is, it should build against a binary LLVM
> installation.
>
> 3) Modern. That is, I don't want to use any deprecated APIs or
> CMake macros.
>
> 4) Clang-compatible. That is, I'd like the pass to be able to run
> directly from Clang instead of having to generate bitcode, run
> opt on it, and then feed it back to Clang for linking.
>
> Does such an example exist and if so, where can I find it?

http://www.cs.cornell.edu/~asampson/blog/llvm.html describes how to create an out-of-source pass (see 'Let’s Write a Pass’). I think it is for LLVM 3.8 though.

Cheers,
Florian

Scott Pakin via llvm-dev

unread,
Jul 10, 2019, 7:54:13 PM7/10/19
to Florian Hahn, llvm...@lists.llvm.org
Florian,

On 7/10/19 2:17 PM, Florian Hahn wrote:
> http://www.cs.cornell.edu/~asampson/blog/llvm.html describes how to
> create an out-of-source pass (see 'Let’s Write a Pass’). I think it
> is for LLVM 3.8 though.

Thanks for pointing that out. I had actually recently stumbled across
that page (and the GitHub repository it references,
https://github.com/sampsyo/llvm-pass-skeleton) while searching for an
answer to my questions but wasn't sure how up-to-date the information
is:

* Is add_library the currently recommended CMake macro call for
building an LLVM pass?
http://llvm.org/docs/WritingAnLLVMPass.html and
https://llvm.org/docs/CMake.html both point to add_llvm_library,
which I couldn't get to work.

* Is the legacy pass manager still the right way to make a pass
Clang-callable? There's a currently active thread on this list,
"Status of the New Pass Manager", that makes me think I have some
code-rewriting in my future.

-- Scott

Andrzej Warzynski via llvm-dev

unread,
Jul 14, 2019, 10:03:01 AM7/14/19
to Scott Pakin, Florian Hahn, llvm...@lists.llvm.org
Scott,

This is still very early stages, but you can be my guinea pig:

https://github.com/banach-space/llvm-tutor

It's a tutorial that I've been preparing recently and am hoping to
present somewhere at some point :-) I believe that it already covers 1),
2) and 3). I haven't had the time to work on 4). It's based on LLVM-8
and includes a working CI set-up, so that you can be confident that it
works. If it doesn't, ping me - I'm keen on improving it.

-- Andrzej
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Scott Pakin via llvm-dev

unread,
Jul 15, 2019, 6:01:18 PM7/15/19
to Andrzej Warzynski, llvm...@lists.llvm.org
Andrzej,

On 7/14/19 8:02 AM, Andrzej Warzynski wrote:
> This is still very early stages, but you can be my guinea pig:
>
> https://github.com/banach-space/llvm-tutor
>
> It's a tutorial that I've been preparing recently and am hoping to
> present somewhere at some point :-) I believe that it already covers 1),
> 2) and 3). I haven't had the time to work on 4). It's based on LLVM-8
> and includes a working CI set-up, so that you can be confident that it
> works. If it doesn't, ping me - I'm keen on improving it.

Cool, thanks. I just started reading over that now. I have a couple
of questions already, if you don't mind:

1) I see you're building the pass with

add_library(lt-cc-static STATIC StaticCallCounter.cpp
DynamicCallCounter.cpp)
add_library(lt-cc-shared MODULE StaticCallCounter.cpp
DynamicCallCounter.cpp)

What's the reason for building a STATIC version? Does any LLVM tool
make use of that?

Is add_library the preferred pass-building CMake macro these days,
as opposed to add_llvm_library or add_llvm_loadable_module?

2) How is the user supposed to install the generated files? Neither
"make install" nor "cmake --target=install" seem to work.

Serge Guelton via llvm-dev

unread,
Jul 17, 2019, 9:17:55 AM7/17/19
to Scott Pakin, llvm...@lists.llvm.org
This review https://reviews.llvm.org/D61446 tries hard to address your needs: it makes it possible to register a pass withing clang/opt/bugpoint with a dedicated cmake function call. The pass can then behave as a loadable plugin or be linked statically to LLVM (depending on cmake options), and the code can live out-of-tree, much like polly.

Andrzej Warzynski via llvm-dev

unread,
Jul 17, 2019, 9:21:57 AM7/17/19
to Scott Pakin, llvm...@lists.llvm.org
Hi Scott,

This is useful feedback for me, thanks! As you've noticed, that project
is Work In Progress, which has been on hold for a while. I'm about to
get back to it, and you've already given me some good hints as what to
improve.

My replies inline.

On 15/07/2019 23:01, Scott Pakin wrote:
> Andrzej,
>
> On 7/14/19 8:02 AM, Andrzej Warzynski wrote:
>> This is still very early stages, but you can be my guinea pig:
>>
>> https://github.com/banach-space/llvm-tutor
>>
>> It's a tutorial that I've been preparing recently and am hoping to
>> present somewhere at some point :-) I believe that it already covers 1),
>> 2) and 3). I haven't had the time to work on 4). It's based on LLVM-8
>> and includes a working CI set-up, so that you can be confident that it
>> works. If it doesn't, ping me - I'm keen on improving it.
>
> Cool, thanks. I just started reading over that now. I have a couple
> of questions already, if you don't mind:
>
> 1) I see you're building the pass with
>
> add_library(lt-cc-static STATIC StaticCallCounter.cpp
> DynamicCallCounter.cpp)
> add_library(lt-cc-shared MODULE StaticCallCounter.cpp
> DynamicCallCounter.cpp)
>
> What's the reason for building a STATIC version? Does any LLVM tool
> make use of that?

Good catch - I honestly don't remember. Let me check that and I'll get
back to you.

>
> Is add_library the preferred pass-building CMake macro these days,
> as opposed to add_llvm_library or add_llvm_loadable_module?

My motivation was to make it as standalone and basic as possible.
add_library was sufficient and that's what I used.

>
> 2) How is the user supposed to install the generated files? Neither
> "make install" nor "cmake --target=install" seem to work.

I have not considered that at all. Added to my TODO list.

>
> -- Scott
>

-- Andrzej
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Scott Pakin via llvm-dev

unread,
Jul 17, 2019, 8:50:53 PM7/17/19
to Serge Guelton, llvm...@lists.llvm.org
On 7/17/19 7:17 AM, Serge Guelton wrote:
> This review https://reviews.llvm.org/D61446 tries hard to address your
> needs: it makes it possible to register a pass withing
> clang/opt/bugpoint with a dedicated cmake function call. The pass can
> then behave as a loadable plugin or be linked statically to LLVM
> (depending on cmake options), and the code can live out-of-tree, much
> like polly.

Thanks. I had looked at Polly in the past. I guess I should take
another look now that I more clearly know what I'm looking for.

Reply all
Reply to author
Forward
0 new messages