Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement support for LLVMs code coverage instrumentation #34701
Comments
This comment has been minimized.
This comment has been minimized.
|
I know I'd love to see this work! Do you know what it would entail in terms of what the hoops a prototype would have to jump through? Also, does this work reliably on platforms like Windows? Or is it still best on Linux and "works mostly elsewhere"? |
This comment has been minimized.
This comment has been minimized.
|
A prototype would need to generate the LLVM IR for the instrumentation, for that it needs to generate a mapping between source ranges and performance counters (I would start with just functions). Then it needs to generate and embed the IR in the object files. It would be a good idea to look how clang then outputs this into a file to use the same format (which is documented), and test that we can read it with llvm-cov in linux and macos (don't know about windows support but since clang is gaining full windows support if its not there it will be there soon). I think that would be enough for a prototype, from there we can move on to generating instrumentation for conditionals (match/loop/jumps/ifs...) and blocks (how many iterations was a loop body executed). We could go down to expressions, but then it would be wise to offer users a way to control how much instrumentation is generated: functions, branches, and loops (which are branches) is typically enough. We should then support skipped regions (for conditional compilation), expansions (for macros, maybe plugins), and dealing with unreachable code (there is a performance counter that is always zero for that). The meat of the work is in the source=> performance counter mapping, generating the IR, and generating the conforming output.
It works on Mac and Linux, don't know about Windows. Even if it doesn't work everywhere, this is probably the way to make it work in as much platforms as possible anyways. Clang support on windows is pretty good already and it is only getting better. |
This comment has been minimized.
This comment has been minimized.
|
@sujayakar's done some awesome work to prototype this on OSX at least, discovering:
That should at least get code coverage working on OSX in some respect! THis is also a pretty strong case that it may not be too hard to actually get this working for all platforms if LLVM's standard tool suite "just works". |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton That's gcov coverage, which is completely different from what @gnzlbg meant in this issue. The correct pass name is |
This comment has been minimized.
This comment has been minimized.
|
@sanxiyn is correct. I just want to add that I don't think we should implement clang-like gcov coverage generation. LLVM format can be used for many more things (e.g. profile guided optimizations), and LLVM's llvm-cov tool can generate gcov files from it. |
This comment has been minimized.
This comment has been minimized.
|
afaik, there's a PR open for this: #38608 |
This comment has been minimized.
This comment has been minimized.
|
As far as I can tell, #38608 is still gcov coverage and not instrprof. |
Mark-Simulacrum
added
the
A-LLVM
label
Jun 22, 2017
Mark-Simulacrum
added
the
C-feature-request
label
Jul 25, 2017
kennytm
referenced this issue
Jul 28, 2017
Open
As of 1.19 it is not possible to accurately measure unit test coverage #43410
This comment has been minimized.
This comment has been minimized.
|
I believe this is now implemented as |
alexcrichton
closed this
Aug 25, 2017
This comment has been minimized.
This comment has been minimized.
|
This is not implemented yet. LLVM coverage is different from gcov coverage, which is also different from sanitizer coverage. LLVM implements three separate coverage mechanisms. |
sanxiyn
reopened this
Aug 28, 2017
This comment has been minimized.
This comment has been minimized.
|
Would LLVM coverage ( My project (a gameboy emulator) makes very heavy usage of generics and it'd be wonderful to get code coverage working on integration tests but I'm not really sure how. |
This comment has been minimized.
This comment has been minimized.
|
I think good code coverage support is the single most important piece of tooling missing in Rust. When I run This information does not mean much either (just because a code-path was exercised does not mean that it was exercised for all inputs), but it would mean more than nothing, and it would make writing tests and asserting the value of test way easier. IMO adding this kind of capability to There is only another part of tooling infrastructure that would come close to this in value, and that would be an undefined behavior detector for all rust code. This might sound like a rant, but I just want to raise awareness that this is a very important issue at least for me (and from the other issues being filled, for others as well) because it directly affects the correctness of rust programs (we don't want them to only be memory safe, but also to have correct logic). Maybe if If I don't have precise auto-completion information or my source code is not perfectly formatted, well, I can live with that. But if my program has undefined behavior and I am not catching it because I am only testing 40% of my code-paths then... I am screwed. Does this make sense? |
This comment has been minimized.
This comment has been minimized.
Hi, can anyone explain that what's the difference between "LLVM coverage" ( |
This comment has been minimized.
This comment has been minimized.
|
I found @kennytm's answer to my question. Let me put here in case someone like me gets lost in the overwhelmed references: #44673 (comment) |
This comment has been minimized.
This comment has been minimized.
|
I've done a bit of prototyping on this to see what would be involved in using So, here's a vague "plan of action" (with progress so far) to enable PGO and LLVM-assisted coverage:
Add Frontend-Guided InstrumentationThis is currently implemented in its most basic form as There may be other places that it's useful to add instrumentation (e.g. adding vtable-downcast counts to enable Enhance the profile-rt libraryWith the above work complete, the generated binaries will count (in-memory) each time an instrumented code path is hit. To extract this information, the profile-rt library should walk the various Enable profile-guided optimizationWith a Create a coverage mapLLVM's coverage tooling can use a "Coverage Map" to map between instrumentation counters and blocks of code. The format of the map is well documented at http://llvm.org/docs/CoverageMappingFormat.html and there's code in LLVM (http://llvm.org/doxygen/classllvm_1_1coverage_1_1CoverageMappingWriter.html) for creating the magic format. Note that this may require Rustc manually inserting instrumentation (rather than using Note that the LLVM coverage map format supports various useful features, not well handled in GCOV-style coverage:
Integrate coverage tooling into CargoCreating a Likely requires stabilising Unanswered QuestionsThere are some bits of Rust code for which coverage is non-obvious:
|
This comment has been minimized.
This comment has been minimized.
|
gnzlbg commentedJul 7, 2016
There are ways to more or less easily obtain code coverage information from rust binaries, some of which are in widespread use (e.g. travis-cargo + gcov + coveralls.io). However, these are either platform specific (gcov/kcov are linux only), or incomplete (coverage for documentation tests is not collected).
It would be better if rustc would be able to instrument rust binaries and tests using LLVM Coverage Instrumentation. This would allow code coverage information to work portably across the supported platforms, as well as produce reliable coverage information.
Ideally, such support would be integrated in an easy to use
cargo coveragesubcommand and the Rust Language Server, such that IDEs can use this information to guide the user while writing tests.