banach-space / llvm-tutor

A collection of out-of-tree LLVM passes for teaching and learning

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dynamic-cc instrumented prints nothing

YuuuuuuuuY opened this issue · comments

I'm using llvm 13.0.1
After being instrumented by dynamic-cc, running the program in lli prints nothing.
But when I run the same program in lli 15.0.0, it prints as expected.

Anybody experienced same issue?

Hey, thanks for posting this!

Could you copy and paste all your commands so that I can reproduce this?

Thanks,
-Andrzej

Thanks for replying!
Here are the commands I used:

$ clang-13 -O1 -emit-llvm -c ../../inputs/input_for_cc.c -o input_for_cc.bc
$ opt-13 -enable-new-pm=0 -load ./libDynamicCallCounter.so -legacy-dynamic-cc input_for_cc.bc -o instrumented_bin

# running it on lli-13 prints nothing
$ lli-13 ./instrumented_bin

# running it on lli 15.0.0, which is built from source code, prints the following:
$ LLVM_PROJECT_DIR/build/bin/lli ./instrumented_bin
=================================================
LLVM-TUTOR: dynamic analysis results
=================================================
NAME                 #N DIRECT CALLS
-------------------------------------------------
foo                  0         
bar                  0         
fez                  0         
main                 1

Thanks for sending this! Please try this for your first step (replace -O1 with -O0 -Xclang -disable-O0-optnone):

$ clang-13 -O0 -Xclang -disable-O0-optnone -emit-llvm -c ../../inputs/input_for_cc.c -o input_for_cc.bc

You should get a good idea of what is happening by comparing the generate file before and after the change (you will want to add -S too to get a textual form of the test file, too).

That's my bad - I need to update the README file. Sorry about it - I really appreciate you reporting this, thank you!

-Andrzej

I tried the change and it only makes lli 15 to print something different:

=================================================
LLVM-TUTOR: dynamic analysis results
=================================================
NAME                 #N DIRECT CALLS
-------------------------------------------------
foo                  13        
bar                  2         
fez                  1         
main                 1

But lli 13 still prints nothing.

This seems to be caused by JIT. According to the release note, the default JIT engine is changed from -jit-kind=mcjit to -jit-kind=orc.

Using -jit-kind=mcjit on lli 13 prints as expected, but -jit-kind=orc prints nothing. On lli 15, both options work well. So, this is probably a bug in the ORC engine in llvm 13 which is fixed in later releases.

Ah, now I see! Great find :) Annoyingly, I did encounter this myself when bumping to LLVM 13: b2f4d7b.

So, the README needs updating :) Would you fancy creating a PR? And perhaps an end-to-end test?

Thanks for digging into this and seeing it through - that's greatly appreciated!

-Andrzej

Oh, that's right! I didn't even think about checking the tests.

I created a PR updating the README. But DynamicCallCounterTest1.ll does the testing already. Is that the end-to-end test you're talking about?

Thanks for creating that PR!

By "end-to-end" I meant a test that consumes a C file (instead of an LLVM file), so that it needs to run clang first and only then runs a pass on the generated output. Something similar to MergeBB_exec.ll). Such test would verify both:

  • clang invocation
  • opt invocation

This would be "end-to-end" in the sense that it wouldn't skip the Clang invocation. Perhaps not the best name!

I guess that you could just update DynamicCallCounterTest1.ll to contain this run line:

; RUN: %clang -S -emit-llvm %S/../inputs/input_for_cc.c -o - \
; RUN:   | opt -load-pass-plugin %shlibdir/libDynamicCallCounter%shlibext -passes="dynamic-cc,verify" -o %t.bin
; RUN: lli --jit-kind=mcjit %t.bin | FileCheck %s

That should just work ™️ 🤞🏻 :)

I'm suggesting this as next time we will be able to use this test as a reference for people (i.e. something that definitely works as it's regularly being tested in the CI).

-Andrzej

Thanks for the detailed explanation! I've updated the test.

Also, thanks for creating this awesome tutorial!

-Yu