|
| 1 | +--- |
| 2 | +title: "Optimize Usage of Source Locations in Clang Modules" |
| 3 | +layout: post |
| 4 | +excerpt: "Reducing source-location memory pressure in modular C++ builds through allocation reuse and interval mapping." |
| 5 | +sitemap: false |
| 6 | +author: Ayokunle Amodu |
| 7 | +permalink: blogs/optimize_source_locations_clang_modules_Ayokunle_Amodu_blog/ |
| 8 | +thumbnail_image: /images/cppalliance-logo.svg |
| 9 | +date: 2026-05-20 |
| 10 | +tags: clang llvm modules source-locations cppalliance |
| 11 | +--- |
| 12 | + |
| 13 | +{% include dual-banner.html |
| 14 | +left_logo="/images/cppalliance-logo.svg" |
| 15 | +right_logo="/images/cr-logo.png" |
| 16 | +caption="" |
| 17 | +height="20vh" %} |
| 18 | + |
| 19 | +## Introduction |
| 20 | + |
| 21 | +Hi everyone! I am Ayokunle Amodu, joining the Compiler Research team as a CppAlliance |
| 22 | +Fellow for the 2026 cycle. I am an MLIR geek at heart. Most of my compiler work has |
| 23 | +lived in the middle end, progressive lowering. I love seeing how high-level structure |
| 24 | +survives a full lowering chain and still means something at the bottom. |
| 25 | + |
| 26 | +This project is front-end territory, not my typical residence, but being a comp arch |
| 27 | +kid who has spent time with RISC-V assembly means a 32-bit space running out of room |
| 28 | +is a very familiar kind of problem. Working under Vassil Vassilev with real upstream |
| 29 | +review as the target is exactly the kind of opportunity I wanted. |
| 30 | + |
| 31 | +## The Problem |
| 32 | + |
| 33 | +Clang tracks every position in source code as a 32-bit offset into a global address |
| 34 | +space managed by `SourceManager`. That space is finite, and in large modular builds it |
| 35 | +fills up faster than you would expect. The issue is not the size of the representation |
| 36 | +itself. It is that when multiple modules include the same headers, Clang has no memory |
| 37 | +of having already mapped those files and simply maps them again. The address space |
| 38 | +grows with the number of modules rather than the number of unique files, and once it |
| 39 | +runs out, the build fails. |
| 40 | + |
| 41 | +The community looked seriously at widening `SourceLocation` to 64 bits to buy more |
| 42 | +room, but the proposal did not land. The concern was legitimate: `SourceLocation` is |
| 43 | +embedded across a huge number of AST nodes, so making it bigger raises memory usage |
| 44 | +for every build, not just the ones actually hitting the limit. It is a global cost for |
| 45 | +a problem that has a much more local cause. |
| 46 | + |
| 47 | +## The Approach |
| 48 | + |
| 49 | +The core idea is to introduce deduplication at the point of allocation. Before |
| 50 | +assigning a new region for a module's input files, we check whether those files have |
| 51 | +already been allocated and reuse the existing range if they have. This keeps the |
| 52 | +address space from growing proportionally with module count and lets it scale with |
| 53 | +unique content instead. |
| 54 | + |
| 55 | +The work starts with a `DenseMap`-based prototype to validate the concept and confirm |
| 56 | +the numbers move in the right direction. From there it extends to an |
| 57 | +`llvm::IntervalMap` design that handles more complex scenarios, including cases where |
| 58 | +different modules serialize different amounts of location data for the same file. |
| 59 | +Alongside the implementation, the project covers diagnostic correctness, include-stack |
| 60 | +reconstruction, and upstream patch preparation for LLVM review. |
| 61 | + |
| 62 | +## What We Hope to Achieve |
| 63 | + |
| 64 | +If this works the way we expect, large modular builds that are currently approaching |
| 65 | +the limit will complete without exhaustion. Duplicated headers stop eating into the |
| 66 | +address space, diagnostics stay correct, and the fix is local enough that nothing |
| 67 | +else in the compiler has to change. The address space scales with unique content |
| 68 | +instead of module count. |
| 69 | + |
| 70 | +## Relevant Links |
| 71 | + |
| 72 | +- [LLVM Discourse: Revisiting 64-bit source locations](https://discourse.llvm.org/t/revisiting-64-bit-source-locations/86556) |
| 73 | +- [LLVM Discourse: RFC: An opt-in CMake option for 64-bit Source Location](https://discourse.llvm.org/t/rfc-an-opt-in-cmake-option-for-64-bit-source-location/87538) |
| 74 | + |
| 75 | +May the blessings of the Dragon be with us. |
0 commit comments