首页›Matt Godbolt›文章

Loop-Invariant Code Motion

Matt Godbolt · 2025年12月13日

AI 摘要 AI 生成,可能有误

本文是《2025 年编译器优化降临历》(Advent of Compiler Optimisations 2025)系列文章的一部分,介绍了循环不变代码外提(LICM,Loop-Invariant Code Motion)这一优化技术:编译器将迭代之间不会发生变化的计算——例如 vec.size() 或纯函数 get_range() 的调用——提升(hoist)到循环之外。作者通过汇编代码展示了 clang 如何借助巧妙的免分支技巧成功完成这一变换,但他惊讶地发现 GCC 在该示例中却未能做到,原因似乎是返回的 std::pair 未经过公共子表达式消除。核心要点:要信任编译器,但也要懂得如何借助 Compiler Explorer 之类的工具来验证它的输出。

Written by me, proof-read by an LLM.
Details at end.

Look back at our simple loop example - there's an optimisation I completely glossed over. Let me show you what I mean:

On every loop iteration we are calling vec.size() to compare the index value, and to check if the index has reached the end of the vector. However, looking in the assembly, the compiler has pulled the size calculation1 out of the loop entirely - the sar that divides the "end - start" value by the size of an int is only performed before the loop! The compiler quietly rewrote our code to be:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Matt Godbolt

Advent of Microarchitecture 2026

AI 摘要The author announces the Advent of Microarchitecture 2026, a YouTube series in lieu of the previous Advent of Compiler Optimisations, which was too time-consuming (about 10 hours per episode) to produce again. The new series consists of low-effort, ad-libbed videos recorded straight to camera with minimal editing, some help from an LLM, and as much automation as possible. The introduction video is already available, and daily videos will be released each morning throughout December.

Recreating a 2010 Experiment

AI 摘要The author recounts recreating a fun 2010 experiment from their Google days, when they hooked a BBC Master up as a dumb terminal over serial to browse the web, jokingly claiming to be the only person who googled Google on a BBC Micro from inside Google. Since Google no longer supports non-JavaScript browsers like lynx, they used DuckDuckGo's lite mode at home, working around cabling mistakes, the inadequate built-in *TERMINAL program, and baud rate limits to get the setup running with the Acornsoft Termulator ROM. Along the way they also improved jsbeeb's audio and video emulation using feedback from their PALindrome PAL decoder, making it more authentic even if it sounds and looks objectively worse.

How Compiler Explorer Runs on AWS in 2026

AI 摘要The article describes how Compiler Explorer runs on AWS in 2026: browser traffic goes through CloudFront and WAF to a single Application Load Balancer that routes to blue/green auto-scaling fleets of mostly cheap spot instances, while its roughly 6,000 never-deleted compiler binaries across 93 languages are stored on EFS using a custom content-addressed image system (CEFS) that cut startup times dramatically. Nightly compiler builds run on self-hosted GitHub Actions EC2 runners, a partially deployed 'CE Router' will eventually decouple HTTP handling from compilation, and services like DynamoDB, S3, and Lambda handle short links, caching, and odds and ends. The site handled 78.7 million compilations over the past year at around $3,600 per month (about $0.0007 per compilation), costs largely covered by AWS open source credits and sponsors, though traffic has been sliding for reasons the author can't fully explain since the site deliberately doesn't track users.

Walking the dog with Claude

AI 摘要The author, Matt, recounts interviewing himself with Claude (dictating via voice-to-text) during a dog walk about the gap between accurate-but-impenetrable technical content and polished-but-slightly-wrong material. He discusses his explanation philosophy—including his 'tell my mum' test, childhood mental models from Usborne books, and the real costs in hours or conceptual distillation that explain his infrequent video output—and admits his motivations range from showing off to the joy of someone finally understanding. He concludes that Compiler Explorer shares the same impulse as his videos and talks, and notes that the LLM-as-interviewer experiment sent him home with a new thought, which is more than most dog walks achieve.

2025 in Review

AI 摘要In his 2025 year-in-review, Matt Godbolt recounts a busy year featuring numerous conference talks and keynotes, appearances in Computerphile videos, and Compiler Explorer milestones such as solving the CEFS mounting problem and launching the experimental Claude Explain feature. He also kept up his monthly Two's Complement podcast, appeared in a Tom7 video, and produced a 27-video Advent of Code Optimisation YouTube series. The year ended with him starting a new job at Hudson River Trading, where he is delighted with the technology, interesting problems, and kind colleagues.