LLVM 20 will be released. As usual, I maintain lld/ELF and have added some notes to https://github.com/llvm/llvm-project/blob/release/20.x/lld/docs/ReleaseNotes.rst. I've meticulously reviewed nearly all the patches that are not authored by me. I'll delve into some of the key changes.
Natural loops
A dominator tree can be used to compute natural loops.
- For every node
H
in a post-order traversal of the dominator tree (or the original CFG), find all predecessors that are dominated byH
. This identifies all back edges. - Each back edge
T->H
identifies a natural loop withH
as the header.- Perform a flood fill starting from
T
in the reversed dominator tree (from exiting block to header) - All visited nodes reachable from the root belong to the natural loop
associated with the back edge. These nodes are guaranteed to be
reachable from
H
due to the dominator property. - Visited nodes unreachable from the root should be ignored.
- Loops associated with visited nodes are considered subloops.
- Perform a flood fill starting from
Understanding and improving Clang -ftime-report
Clang provides a few options to generate timing report. Among them,
-ftime-report
and -ftime-trace
can be used to
analyze the performance of Clang's internal passes.
-fproc-stat-report
records time and memory on spawned processes (ld
, and gas if-fno-integrated-as
).-ftime-trace
, introduced in 2019, generates Clang timing information in the Chrome Trace Event format (JSON). The format supports nested events, providing a rich view of the front end.-ftime-report
: The option name is borrowed from GCC.
This post focuses on the traditional -ftime-report
,
which uses a line-based textual format.
Understanding
-ftime-report
output
The output consists of information about multiple timer groups. The last group spans the largest interval and encompasses timing data from other groups.
Up to Clang 19, the last group is called "Clang front-end time report". You would see something like the following.
2024年总结
一如既往,主要在工具链领域耕耘。
Blogging
I have been busy creating posts, authoring a total of 31 blog posts (including this one). 7 posts resonated on Hacker News, garnering over 50 points. (https://news.ycombinator.com/from?site=maskray.me).
I have also revised many posts initially written between 2020 and 2024.
Mastodon: https://hachyderm.io/@meowray
Skipping boring functions in debuggers
In debuggers, stepping into a function with arguments that involve function calls may step into the nested function calls, even if they are simple and uninteresting, such as those found in the C++ STL.
GDB
Consider the following example:
1 |
|
When GDB stops at the foo
call, the step
(s
) command will step into std::vector::back
and std::unique_ptr::operator*
. While you can execute
finish
(fin
) and then execute s
again, it's time-consuming and distracting, especially when dealing with
complex argument expressions.
Exporting Tweets
On https://x.com/settings/, click
More -> Settings and privacy -> Download an archive of your data
.
Wait for a message from x.com: "@XXX your X data is ready" Download the
archive.
1 | cp data/tweets.js tweets.ts |
Change the first line from window.YTD.tweets.part0 = [
to let part0 = [
, and append
1 | import { unescape } from "@std/html/entities"; |
Then run deno run --allow-write=. tweets.ts
1 | % cat 2022/index.md |
tweet0
tweet1
Simplifying disassembly with LLVM tools
Both compiler developers and security researchers have built disassemblers. They often prioritize different aspects. Compiler toolchains, benefiting from direct contributions from CPU vendors, tend to offer more accurate and robust decoding. Security-focused tools, on the other hand, often excel in user interface design.
For quick disassembly tasks, rizin provides a convenient command-line interface.
clang-format and single-line statements
The Google C++ Style is widely adopted by projects. It contains a brace omission guideline in Looping and branching statements:
For historical reasons, we allow one exception to the above rules: the curly braces for the controlled statement or the line breaks inside the curly braces may be omitted if as a result the entire statement appears on either a single line (in which case there is a space between the closing parenthesis and the controlled statement) or on two lines (in which case there is a line break after the closing parenthesis and there are no braces).
Removing global state from LLD
LLD, the LLVM linker, is a mature and fast linker supporting multiple binary formats (ELF, Mach-O, PE/COFF, WebAssembly). Designed as a standalone program, the code base relies heavily on global state, making it less than ideal for library integration. As outlined in RFC: Revisiting LLD-as-a-library design, two main hurdles exist:
- Fatal errors: they exit the process without returning control to the
caller. This was actually addressed for most scenarios in 2020 by
utilizing
llvm::sys::Process::Exit(val, /*NoCleanup=*/true)
andCrashRecoveryContext
(longjmp
under the hood). - Global variable conflicts: shared global variables do not allow two concurrent invocation.
I understand that calling a linker API could be convenient, especially when you want to avoid shipping another executable (which can be large when you link against LLVM statically). However, I believe that invoking LLD as a separate process remains the recommended approach. There are several advantages:
- Build system control: Build systems gain greater control over scheduling and resource allocation for LLD. In an edit-compile-link cycle, the link could need more resources and threading is more useful.
- Better parallelism management
- Global state isolation: LLVM's global state (primarily
cl::opt
andManagedStatic
) is isolated.
Keeping pace with LLVM: compatibility strategies
LLVM's C++ API doesn't offer a stability guarantee. This means function signatures can change or be removed between versions, forcing projects to adapt.
On the other hand, LLVM has an extensive API surface. When a library
like llvm/lib/Y
relies functionality from another library,
the API is often exported in header files under
llvm/include/llvm/X/
, even if it is not intended to be
user-facing.
To be compatible with multiple LLVM versions, many projects rely on
#if
directives based on the LLVM_VERSION_MAJOR
macro. This post explores the specific techniques used by ccls to ensure
compatibility with LLVM versions 7 to 19. For the latest release (ccls
0.20241108), support for LLVM versions 7 to 9 has been
discontinued.
Given the tight coupling between LLVM and Clang, the
LLVM_VERSION_MAJOR
macro can be used for both version
detection. There's no need to check
CLANG_VERSION_MAJOR
.