Latest / CyberSecurity Summary / LLVM Techniques, Tips, and Best Practices Clang and Middle-End Libraries: Design powerful and reliable compilers using the latest libraries
Transcript
- 0:00Speaker 1: Okay, let's unpack LLVM. It's incredibly powerful, right, underpins so
- 0:04Speaker 1: much stuff, apples, x code, game engines, you name it.
- 0:07Speaker 2: Absolutely it's everywhere.
- 0:09Speaker 1: But you know, for developers already deep in the ecosystem
- 0:12Speaker 1: or maybe looking to get deeper into compiler engineering, tackle
- 0:15Speaker 1: the docs, well, it can feel less like learning and
- 0:19Speaker 1: more like drinking from a fire hose.
- 0:21Speaker 2: That's a perfect description. It's famously scattered, right.
- 0:24Speaker 1: So our source today, this book LLVM Techniques, tips and
- 0:28Speaker 1: bust practices, it promises to sort of rain that beast in.
- 0:32Speaker 1: So what's our big mission for you today? What are
- 0:34Speaker 1: we trying to achieve here?
- 0:35Speaker 2: Well, our mission is really to give you this streamlined,
- 0:39Speaker 2: comprehensive overview to cut through all that documentation sprawl. We're
- 0:44Speaker 2: going to unearth some key techniques, maybe some surprising insights,
- 0:48Speaker 2: to help you build, tests, optimize, and importantly debug your
- 0:51Speaker 2: LLVM projects much more efficiently. Fewer headaches.
- 0:55Speaker 1: Fewer headaches are always good.
- 0:57Speaker 2: Definitely think of this deep dive as like your shortcut,
- 1:01Speaker 2: making LLVM development faster, more reliable, and well more insightful.
- 1:05Speaker 2: We're aiming straight at those common pain points, those really
- 1:08Speaker 2: long build times, complex testing opaque debugging, that kind of thing.
- 1:13Speaker 1: Yeah, long build times. That's off for the first mountain
- 1:15Speaker 1: you hit, isn't it. Especially with a huge project like LLVM,
- 1:19Speaker 1: defaults can take hours, huge productivity killer.
- 1:22Speaker 2: Oh absolutely, it's a major drag.
- 1:24Speaker 1: So what's the secret? How do we cut down this
- 1:25Speaker 1: build time? Beast?
- 1:27Speaker 2: Okay, so the book immediately points to replacing slower, older tools.
- 1:31Speaker 2: Makes sense, right, right? Take build systems Ninja. It just
- 1:33Speaker 2: runs significantly faster than say, g and you make on
- 1:37Speaker 2: big code bases. LLVM is a perfect example.
- 1:40Speaker 1: Why is Ninja faster? What's the magic there?
- 1:42Speaker 2: The key is it's build dot ninjascript. It's it's almost
- 1:45Speaker 2: like assembly language for builds. It gets generated by higher
- 1:48Speaker 2: level systems like Seamake, and because it's so low level,
- 1:51Speaker 2: it allows for loads of optimizations under the hood. Plus
- 1:54Speaker 2: it handles dependencies much better. You just tell Sea make
- 1:56Speaker 2: desh g ninja as simple as that.
- 1:58Speaker 1: So just one command line flag can make a big difference.
- 2:01Speaker 1: We're basically upgrading the engine. But it's not just the
- 2:04Speaker 1: build system, is it. The linker is often a problem too.
- 2:07Speaker 2: Precisely, the default linker BFD. It's mature, sure, but it
- 2:11Speaker 2: wasn't really built for modern speed or memory needs.
- 2:14Speaker 1: How bad can it get?
- 2:15Speaker 2: It can show up that this up to twenty gigabytes
- 2:18Speaker 2: of memory building LLVM.
- 2:20Speaker 1: Wow. Okay, that's a bottleneck.
- 2:21Speaker 2: Definitely a performance hurdle. But thankfully there are much better
- 2:25Speaker 2: alternatives g and you gold, Google developed that one, and
- 2:28Speaker 2: LVM's own linker ld LED Yeah. Ld is often even faster,
- 2:33Speaker 2: and it's got experimental parallel linking too, which is pretty cool.
- 2:36Speaker 2: And again easy sea make flags ds DLVM muslinker Gold
- 2:41Speaker 2: or dz dlv musle ankored. Okay, the speed up from
- 2:44Speaker 2: just those two changes Ninja and a faster linker, it's substantial,
- 2:47Speaker 2: really noticeable.
- 2:48Speaker 1: That's huge just swapping out a couple of underlying tools.
- 2:51Speaker 1: But we can also tweak c make itself right, fine
- 2:53Speaker 1: tune the build arguments.
- 2:54Speaker 2: Absolutely. Tweaking CE make arguments is crucial for efficiency, like
- 2:58Speaker 2: choosing the right build type. A roll with deb info
- 3:01Speaker 2: is off in the sweet spot. Why that one, Well,
- 3:03Speaker 2: it gives you optimized code so it's fast, but it
- 3:05Speaker 2: keeps the debug information so you get a great balance
- 3:07Speaker 2: between space speed and you know, being able to actually debug.
- 3:12Speaker 1: It makes sense.
- 3:13Speaker 2: You generally want to avoid a full debug build unless
- 3:15Speaker 2: you absolutely have to. It just creates so much unnecessary
- 3:18Speaker 2: storage waste, huge binaries and targets.
- 3:21Speaker 1: LVM supports what nearly two dozen hardware targets. Most people
- 3:26Speaker 1: don't need all of those really exactly.
- 3:28Speaker 2: That's another massive time sink. If you build them all.
- 3:30Speaker 2: You can save a ton of time by just building
- 3:32Speaker 2: the ones you actually need. Use DLLLVM target.
- 3:36Speaker 1: Still build How does that look?
- 3:37Speaker 2: Something like Della's DLVM targets to build X eighty six
- 3:41Speaker 2: R sixty four. Just list the ones you care about.
- 3:43Speaker 1: And there's a catch with shells you mentioned.
- 3:45Speaker 2: Ah yeah, good point. In some shells like BSh, you
- 3:49Speaker 2: have to remember the double quotes around the list, otherwise
- 3:51Speaker 2: the command gets cut off part way through. Little gotcha?
- 3:53Speaker 1: Good tip? What else? Shared libraries?
- 3:56Speaker 2: Yes, another great strategy, especially during development. Build LVM components
- 4:01Speaker 2: as shared libraries. Use LVM components as shared libraries.
- 4:05Speaker 1: Use aad build shared libs.
- 4:07Speaker 2: On Why is that better? Because LVM is so modular,
- 4:10Speaker 2: Building shared library saves a significant amount of storage space
- 4:14Speaker 2: and really speeds up the linking part of the build
- 4:16Speaker 2: process compared to static libraries. Much faster iteration.
- 4:20Speaker 1: Okay, and what do LVM dash tubulliging. That one comes
- 4:23Speaker 1: up a lot as being slow.
- 4:24Speaker 2: It does. It can really impact build times. But there's
- 4:27Speaker 2: a trick. You can build an optimized version of just
- 4:30Speaker 2: lavmt bulgein itself even if the rest of your build
- 4:33Speaker 2: is in debug mode. Use h dlll V optimized stable gen.
- 4:38Speaker 1: Eldve one nice. So optimizing the tool that helps build the.
- 4:41Speaker 2: Tools exactly, it shaves off more time.
- 4:44Speaker 1: So it really feels like we're swapping out a rusty
- 4:48Speaker 1: old tractor for a soup up racing machine just by
- 4:50Speaker 1: changing a few settings and tools. Speaking of alternatives, the
- 4:53Speaker 1: source mentioned another build system gn it.
- 4:56Speaker 2: Does generate Ninja or gn used a lot by Google projects.
- 5:00Speaker 2: It's like Chromium.
- 5:00Speaker 1: What's its advantage.
- 5:01Speaker 2: It's known for really fast configuration time and reliable argument management.
- 5:06Speaker 2: The book says it's especially useful if your developments make
- 5:08Speaker 2: changes to build files, or if you're constantly trying out
- 5:11Speaker 2: different build options. Much quicker reconfiguration, so.
- 5:15Speaker 1: Good for rapid iteration on the build itself exactly.
- 5:17Speaker 2: It's more of an alternative for those specific scenarios. Maybe
- 5:20Speaker 2: not a full replacement for everyone, but very handy when
- 5:23Speaker 2: you're tweaking build files a lot.
- 5:24Speaker 1: Okay, it makes sense. So once you've got your compiler
- 5:26Speaker 1: built fast, the next big hurdle is reliability testing. How
- 5:30Speaker 1: do you make sure it's actually, you know, correct?
- 5:33Speaker 2: Yeah, testing is critical, and LVM provides its own framework
- 5:37Speaker 2: for this, LVM LIT like LLVM Integrated Tester. The book
- 5:43Speaker 2: calls it an easy to use yet general framework, and importantly,
- 5:47Speaker 2: while it started for LVM's own tests, it's actually a
- 5:50Speaker 2: generic testing framework. You can use it outside LVM for
- 5:53Speaker 2: other projects too.
- 5:54Speaker 1: Very versatile and inside RIT there's this utility file check
- 5:59Speaker 1: that sounds key for compiler testing. What's special about it?
- 6:02Speaker 2: File check is really powerful. It does advance pattern checking
- 6:05Speaker 2: on output files. It goes way beyond just diffing text,
- 6:08Speaker 2: so you embed directives right in your test files. Gacheck
- 6:11Speaker 2: is basic rajex matching, simple enough, but then you get
- 6:15Speaker 2: directives like check next t that makes sure a pattern
- 6:18Speaker 2: is found on the very next line after the previous match.
- 6:21Speaker 2: Super useful for checking sequential.
- 6:23Speaker 1: Output ah right, controlling the order.
- 6:25Speaker 2: Exactly and check same that matches patterns that must be
- 6:30Speaker 2: on the exact same line. Brilliant for avoiding really long
- 6:34Speaker 2: messy check lines when you need multiple things on one
- 6:36Speaker 2: line keeps tests readable.
- 6:38Speaker 1: Yeah, I can see that. Verbos ir needs concise checks.
- 6:42Speaker 1: What if you want to ensure something isn't there or
- 6:44Speaker 1: if the order doesn't matter?
- 6:46Speaker 2: Good questions. For negative checks, there's check not. It asserts
- 6:50Speaker 2: a pattern does not exist. Really handy for saying Okay,
- 6:53Speaker 2: I expect why, but I definitely don't want to see X.
- 6:56Speaker 1: Makes sense asserting the absence of something.
- 6:58Speaker 2: And for when the order might change, maybe due to optimizations.
- 7:01Speaker 2: Shuffling code around you use check DAG. That stands for
- 7:05Speaker 2: a directed ecyclic graph, but here it means it allows
- 7:07Speaker 2: matching texts and arbitrary orders. Super flexible for testing nondeterministic output.
- 7:13Speaker 1: Wow, check DAG. That's really flexible. It seems like you
- 7:16Speaker 1: can test the intent behind the code changes, not just
- 7:18Speaker 1: the literal output strengths.
- 7:20Speaker 2: That's exactly the point. It's about semantic checking, not just
- 7:22Speaker 2: textual matching.
- 7:23Speaker 1: So, speaking of describing intent and structure, compilers deal with
- 7:27Speaker 1: incredibly complex structured data instruction sets, optimization rules. How does
- 7:33Speaker 1: LLVM handle describing that efficiently? Is that table gen You've
- 7:37Speaker 1: nailed it.
- 7:38Speaker 2: Tablegen is the answer. There, it's a domain specific language
- 7:41Speaker 2: a DSL ESL. Yeah. It originally started within LVM for
- 7:46Speaker 2: describing things like process or instruction sets, the ISA, and
- 7:49Speaker 2: other hardware details, but its use has just exploded.
- 7:52Speaker 1: How so what else does it use for?
- 7:54Speaker 2: Oh? Everything, managing Clang's command line options, defining complex optimization
- 7:59Speaker 2: rules like the inst combine people optimizations. The book says
- 8:02Speaker 2: it's basically for any tasks that involve non trivial static
- 8:06Speaker 2: and structural data.
- 8:07Speaker 1: So much broader than just hardware. Now it's a general
- 8:10Speaker 1: tool for this kind of static data. Can you give
- 8:12Speaker 1: us a quick feel for the syntax? How does it work?
- 8:14Speaker 2: Sure? At its core, you define a class. Think of
- 8:17Speaker 2: it like a C plus plus struct It defines a layout,
- 8:20Speaker 2: fields and types. Then you use def to create an
- 8:22Speaker 2: instance of that class called a record.
- 8:24Speaker 1: Like creating an object from a class blueprint exactly.
- 8:27Speaker 2: And you can override specific fields in that instance using
- 8:31Speaker 2: the let keyword.
- 8:32Speaker 1: Okay, and what about these bang operators I've heard about,
- 8:35Speaker 1: dot AD, dot mole Ah, Yes.
- 8:39Speaker 2: Those aren't run time functions. They're more like macros that
- 8:41Speaker 2: get evaluated during build time buil tag. Yeah, so you
- 8:44Speaker 2: can do simple computations right in the table gen file itself.
- 8:48Speaker 2: The example given is dot mole kilogram one thousand to
- 8:51Speaker 2: maybe convert units. It happens when table gen runs, not
- 8:55Speaker 2: when the compiler runs.
- 8:56Speaker 1: Later clever build time computation, or if you have lots
- 8:59Speaker 1: of similar records, is there a shortcut?
- 9:01Speaker 2: Yes, that's where multi class comes in. It's a way
- 9:03Speaker 2: to define multiple records at once by factoring out common
- 9:06Speaker 2: parameters like a template sort of. Yeah. The book uses
- 9:09Speaker 2: an autopart and car example. You define a multi class
- 9:12Speaker 2: for parts, then use defen to instantiate multiple cars, and
- 9:15Speaker 2: it automatically generates all the individual part records like car one,
- 9:18Speaker 2: fuel tank, carto, engine, et cetera from one definition.
- 9:21Speaker 1: Very concise, nice as boilerplate right and complex relationships.
- 9:25Speaker 2: Yeah, graphs for that. Tablegen has a specific DAG data
- 9:29Speaker 2: type that lets you define directed cyclic graph instances explicitly,
- 9:33Speaker 2: super important for things like instruction selection patterns or optimization
- 9:37Speaker 2: rules where you have dependencies. You can even use tags
- 9:40Speaker 2: like the upper term dollars to give parts of the
- 9:42Speaker 2: daglogical names.
- 9:43Speaker 1: A DAG type built in. Yeah, that's powerful, and the
- 9:46Speaker 1: source uses this amazing analogy to make a concrete right
- 9:51Speaker 1: a donut recipe it does.
- 9:52Speaker 2: It's a brilliant example. The book uses a delicious doughnut
- 9:56Speaker 2: recipe to show tablegen's power.
- 9:58Speaker 1: How does that work? A doughnut recipe and a piler book.
- 10:00Speaker 2: It defines unit classes like gramunit peb's peanut, then ingredient
- 10:04Speaker 2: base records, and finally step records. These step records form
- 10:07Speaker 2: a DAG representing the cooking actions. Makes this add that
- 10:11Speaker 2: complete with ingredients and amounts. Wow, it's a perfect analogy
- 10:14Speaker 2: because it takes this abstract idea of describing structured data
- 10:17Speaker 2: and makes it totally tangible. You immediately see how it
- 10:20Speaker 2: parallels describing instruction patterns or optimization steps.
- 10:23Speaker 1: A donut recipe in compiler engineering. That's definitely an aha moment,
- 10:27Speaker 1: makes total sense. So, okay, you've described your donut recipe
- 10:30Speaker 1: or your instruction set in table gen. How do you
- 10:34Speaker 1: actually use that data like print the recipe?
- 10:36Speaker 2: Right? For that, you need a custom table gen back end?
- 10:39Speaker 2: Now important distinction. This isn't an LVM back end like
- 10:43Speaker 2: for generating machine code.
- 10:45Speaker 1: Different kind of back end, totally different.
- 10:47Speaker 2: A table gen back end is a piece of code,
- 10:49Speaker 2: usually C plus A that convert or transpiles table gen
- 10:54Speaker 2: files into an arbitrary, textual.
- 10:57Speaker 1: Content arbitrary, so anything pretty much.
- 10:59Speaker 2: It could generate a C plus plus header file documentation
- 11:03Speaker 2: or in the donut example, just plain text for the
- 11:05Speaker 2: recipe you use C plus plus APIs provided by tablegen
- 11:09Speaker 2: like recordkeeper dot get all the rive definitions to get
- 11:12Speaker 2: all the defined steps and record dot get value restring
- 11:15Speaker 2: to pull out specific values like ingredient names or amounts.
- 11:18Speaker 1: So you turn the table genstructure into usable code or data.
- 11:21Speaker 2: Exactly, you transform the structured description into whatever format you
- 11:25Speaker 2: need downstream.
- 11:26Speaker 1: That ability to generate code is huge. Yeah, it feels
- 11:29Speaker 1: like that opens the door to extending client itself, maybe
- 11:33Speaker 1: injecting custom logic into the frontend.
- 11:35Speaker 2: It absolutely does. The front end is a prime place
- 11:38Speaker 2: for customization. Think about the preprocessor, the very first stage
- 11:42Speaker 2: handling macros includes you can customize that. Oh yeah, you
- 11:46Speaker 2: can write custom Pragma handler extensions, so you can invent
- 11:51Speaker 2: your own hashtag pragma directives. The book shows an example
- 11:55Speaker 2: hashtag pragma macro or guard.
- 11:57Speaker 1: What would that do?
- 11:58Speaker 2: Well? When the preprocessor sees your prag your handler code runs.
- 12:01Speaker 2: It can parse the Pragma arguments and even register something
- 12:04Speaker 2: called PEP callbacks. Callbacks, yeah, pp callbats let you hook
- 12:08Speaker 2: into various preprocessor events, so you can insert custom logic
- 12:12Speaker 2: whenever a preprocessor event happens. In the example, a macroguard
- 12:15Speaker 2: validator uses the macro defined callback to automatically check if
- 12:19Speaker 2: arguments in certain macros are properly wrapped in parentheses.
- 12:22Speaker 1: Wow, that's fine grain control. Right at the start. What
- 12:24Speaker 1: of the driver, the thing that orchestrates GCC or Clang?
- 12:27Speaker 1: Can you customize that too?
- 12:28Speaker 2: You can? The driver is basically the dispatcher, right, it
- 12:32Speaker 2: passes flags and manages the different compilation phases. And guess
- 12:35Speaker 2: what Clang uses to define its driver flags.
- 12:39Speaker 1: Let me guess tablechen.
- 12:40Speaker 2: You got it, tablegen again. You can declare custom flags,
- 12:45Speaker 2: even paired flags like tay flag and NAM flag, using
- 12:49Speaker 2: things like the booleion f flag, multi class and table gen.
- 12:52Speaker 1: So you define your flag in table gen and Klang
- 12:55Speaker 1: understands it exactly.
- 12:56Speaker 2: The source gives an example of a custom fuse simple
- 12:59Speaker 2: log flag. Defining this in tablegen allows the driver to
- 13:02Speaker 2: recognize it, and then your custom logic can make it
- 13:05Speaker 2: implicitly include a specific header simplelog dot H and maybe
- 13:09Speaker 2: define macros to control log levels all driven by that
- 13:12Speaker 2: one flag.
- 13:13Speaker 1: That's really neat centralized control via custom flag. But can
- 13:16Speaker 1: you go even deeper, like fundamentally change how Clang interacts
- 13:19Speaker 1: with the system's tools, make it output something totally different.
- 13:22Speaker 2: You absolutely can using custom toolchains. The toolchain normally adapts
- 13:26Speaker 2: Clang for different platforms like different ozes or architectures, but
- 13:29Speaker 2: you can make it do completely customed things like what
- 13:32Speaker 2: The book has this fantastic, almost wild example called the
- 13:35Speaker 2: zipline toolchain. It's a demo obviously, but it shows the
- 13:38Speaker 2: power zipline.
- 13:39Speaker 1: What does it do?
- 13:40Speaker 2: Instead of normal compilation, it uses Clang, but then it
- 13:44Speaker 2: encodes the generated assembly code using base sixty four during
- 13:47Speaker 2: the assembling phase.
- 13:49Speaker 1: Base sixty four why, just to show it can.
- 13:51Speaker 2: And then during the linking phase it packages those base
- 13:55Speaker 2: sixty four files into a ZP archive.
- 13:58Speaker 1: Okay, that is wild? How does it that? In?
- 14:00Speaker 2: Through the tool chain definition, you override methods like ad
- 14:04Speaker 2: Clang system include ARGs can add custom include paths. Build
- 14:07Speaker 2: assembler gets overridden to call open cell base sixty four
- 14:10Speaker 2: instead of the normal assembler, and build linker gets overridden
- 14:13Speaker 2: to call zip or tar instead of the linker.
- 14:16Speaker 1: Wow. So you're completely replacing standard build steps with custom commands.
- 14:20Speaker 2: Exactly. It perfectly illustrates how deeply you can customize the
- 14:23Speaker 2: entire pipeline if you need to.
- 14:24Speaker 1: So if you thought compilers where a black box, definitely
- 14:27Speaker 1: think again. We're not just peeking inside. We're fundamentally changing
- 14:30Speaker 1: how they work, how they talk to the OS. That
- 14:32Speaker 1: level of control it must open up amazing possibilities for
- 14:36Speaker 1: optimization and analysis. Right.
- 14:38Speaker 2: Absolutely, that's where the real power of LLVM shines. Sophisticated
- 14:43Speaker 2: optimizations need deep program understanding, and this happens primarily in llvm.
- 14:48Speaker 1: IR, right, the intermediate representation.
- 14:50Speaker 2: Exactly, it's the target independent intermediate representation. It's the core
- 14:54Speaker 2: of the entire LLVM framework where most analysis and transformation happens.
- 14:59Speaker 1: And the mechan is and for doing these transformations is passes. Right,
- 15:02Speaker 1: what's a pass and what's this new pass manager deal?
- 15:06Speaker 2: Think of an LLVM pass as a module, a basic
- 15:10Speaker 2: unit that performs certain actions against LLVMI are like one
- 15:14Speaker 2: step on a factory assembly.
- 15:15Speaker 1: Line, Okay, a modular step, right.
- 15:17Speaker 2: And the new pass manager is a significant redesign compared
- 15:20Speaker 2: to the older system. The book highlights it runs faster
- 15:23Speaker 2: and generates results with better quality, partly due to a
- 15:26Speaker 2: cleaner interface.
- 15:27Speaker 1: Can you give an example of a simple pass.
- 15:28Speaker 2: Sure the source shows a strict up pass. Its goal
- 15:32Speaker 2: is simple, add the nolias attribute to function arguments that
- 15:35Speaker 2: are pointers no alias.
- 15:36Speaker 1: What does that tell the compiler?
- 15:38Speaker 2: It's a powerful hint. It guarantees that pointer does an alias,
- 15:42Speaker 2: meaning it doesn't point to the same memory location as
- 15:45Speaker 2: any other pointer accessible in that scope. This lets the
- 15:48Speaker 2: optimizer be much more aggressive, assuming less potential overlap, which
- 15:52Speaker 2: can unlock significant speed ups.
- 15:54Speaker 1: How does the pass know what other passes have done?
- 15:56Speaker 2: Ah, that's key to the new manager. When you write
- 15:59Speaker 2: a pass, you have to clear what analysis it preserves.
- 16:02Speaker 2: You use preserved analyzes, so if your pass adds no alias,
- 16:06Speaker 2: it might invalidate alias analysis results. You tell a manager,
- 16:10Speaker 2: maybe AA manager results are no longer valid.
- 16:13Speaker 1: So you explicitly state what your pass.
- 16:16Speaker 2: Breaks, well, rather what it doesn't break. By default, it
- 16:18Speaker 2: assumes you break everything you specify what's preserved. This avoids
- 16:22Speaker 2: costly recomputation of analyzes that are still perfectly valid. It's
- 16:26Speaker 2: like a librarian keeping track much more efficient.
- 16:28Speaker 1: Makes sense. So passes are the workers, but they need
- 16:31Speaker 1: information to do complex jobs, they need a brain, right
- 16:35Speaker 1: is that the analysis manager?
- 16:36Speaker 2: Precisely, you nailed it. Modern compiler optimizations can be complex.
- 16:40Speaker 2: They require lots of information and often getting that information
- 16:43Speaker 2: is expensive to evaluate.
- 16:45Speaker 1: So the analysis manager helps with that.
- 16:46Speaker 2: Yes, it handles all tasks related to program analysis. It
- 16:50Speaker 2: runs the analysis passes, and crucially caches their results so
- 16:54Speaker 2: they don't have to be rerun constantly.
- 16:57Speaker 1: Can you give an example of an analysis?
- 16:59Speaker 2: It might manage the source mentions a hal tantalizer project.
- 17:03Speaker 2: Its goal is to find code that's unreachable because a
- 17:05Speaker 2: special function like my halt gets called earlier.
- 17:08Speaker 1: Okay, dead god detection.
- 17:10Speaker 2: Sort of yeah. And to do this it relies on
- 17:13Speaker 2: one of the fundamental analyzes. LVM provides the dominator tree.
- 17:18Speaker 1: Or DT dominator tree. How does that help find unreachable
- 17:21Speaker 1: code after my halt?
- 17:22Speaker 2: Okay? So the dominator tree tells you control flow relationships.
- 17:25Speaker 2: If basic block A dominates basic block B, it means
- 17:29Speaker 2: every possible path to B must go through A first.
- 17:32Speaker 1: Ah.
- 17:33Speaker 2: I see, So if the block containing my halt dominates
- 17:35Speaker 2: another block and my halt stops execution, then that dominated
- 17:38Speaker 2: block is definitely unreachable. Dominator tree analysis computes this tree structure,
- 17:42Speaker 2: and halt tantalizer just needs to query it.
- 17:45Speaker 1: That's really clever. Leveraging fundamental graph analysis.
- 17:48Speaker 2: Exactly and understanding these core analyzes like dominator trees is
- 17:52Speaker 2: what lets you build much smarter, much more effective custom
- 17:56Speaker 2: optimization or analysis passes. You're building on solid theory foundations.
- 18:01Speaker 1: That's a powerful concept. But okay, even with great optimizations,
- 18:04Speaker 1: things go wrong. You need to debug, diagnose issues, check
- 18:08Speaker 1: run time behavior. What tools does LVM offer there?
- 18:11Speaker 2: Right, optimization isn't everything. LVM has some essential support utilities
- 18:16Speaker 2: for debugging your passes themselves. There's lvmd bug.
- 18:19Speaker 1: How does that work?
- 18:20Speaker 2: You sprinkle LLVMD e bug DDGSS calls in your passcode.
- 18:24Speaker 2: These messages only get printed if you run the optimizer
- 18:27Speaker 2: tool with the ededbug or dbug only your past name
- 18:30Speaker 2: flag keeps your production builds clean, but gives you detailed
- 18:34Speaker 2: logs when you need them.
- 18:35Speaker 1: Nice conditional logging. What about tracking numbers like how many
- 18:38Speaker 1: times an optimization fired?
- 18:40Speaker 2: For that, you use the statistic macro. You just declare
- 18:42Speaker 2: statistic counter name description and then increment counter name in
- 18:45Speaker 2: your code. LVM automatically collects these organizes them and can
- 18:49Speaker 2: print them out even in formats like.
- 18:51Speaker 1: Chason Jason output. That's useful for automation.
- 18:54Speaker 2: Very useful turns ad hoc counting into structured data for analysis.
- 18:59Speaker 2: Let's you see if you're pass is actually doing what
- 19:01Speaker 2: you thought or hitting unexpected bottlenecks.
- 19:04Speaker 1: Okay, what if an optimization tries to do something but fails,
- 19:09Speaker 1: like it wants to vectorize a loop but can't, how
- 19:11Speaker 1: do you find out why?
- 19:13Speaker 2: That's exactly what optimization remarks are for you use optimization
- 19:17Speaker 2: remark emitter in your past to report why something happened
- 19:20Speaker 2: or didn't.
- 19:21Speaker 1: Happen, So notes from the optimizer pretty much.
- 19:24Speaker 2: The book uses the example of loop invariant code motion LICM.
- 19:28Speaker 2: If it fails to hoist an instruction out of a loop,
- 19:31Speaker 2: it can emit a remark saying why maybe there's a
- 19:33Speaker 2: potential side effect it couldn't ignore.
- 19:35Speaker 1: You can see these remarks yes.
- 19:37Speaker 2: And even better, there's a tool Optviewer dot kei that
- 19:40Speaker 2: takes these remarks and generates a webpage. It highlights the
- 19:43Speaker 2: relevant source code lines and shows the remarks right next
- 19:45Speaker 2: to them. It's like a visual debugger for your optimizations.
- 19:48Speaker 1: Wow from pinpointing buffer overflows, which we'll get to to
- 19:51Speaker 1: visualizing optimization decisions. Yea LVM really does feel like it
- 19:55Speaker 1: gives you X ray vision into your code.
- 19:57Speaker 2: It's a good way to put it. And beyond these
- 19:59Speaker 2: utilities there's the whole world of instrumentation.
- 20:02Speaker 1: Instrumentation, you mean adding code to collect data at runtime.
- 20:05Speaker 2: Exactly, inserting some probes into the code we are compiling
- 20:08Speaker 2: in order to collect runtime information. Two massive areas here
- 20:12Speaker 2: are sanitizers and profile guided optimization or PGO.
- 20:17Speaker 1: Sanitizers sound like they're about fixing problems or finding them.
- 20:19Speaker 1: Give us a dramatic example.
- 20:21Speaker 2: Okay, address sanitizer classic example, buffer overflow dot C. Normally
- 20:26Speaker 2: it might just crash cryptically or worse, seem to work
- 20:29Speaker 2: but corrupt memory right hard to debug. But compile it
- 20:32Speaker 2: with Clang dash sanitize address. Now when you run it
- 20:35Speaker 2: and hit the overflow asan doesn't just crash. It prints
- 20:38Speaker 2: a detailed report that points out the problematic area with
- 20:41Speaker 2: high accuracy, right away tells you the exact line the
- 20:44Speaker 2: variable everything.
- 20:46Speaker 1: How does it do that?
- 20:47Speaker 2: It works by having the compiler inserting a boundary check
- 20:50Speaker 2: into the array index access in the LVMR. It adds
- 20:53Speaker 2: runtime guards. I remember this one bug. We spent days
- 20:56Speaker 2: stepping through GDB asan found it instantly on the Total
- 21:01Speaker 2: game Changer.
- 21:02Speaker 1: That's incredible. Are there other sanitizers custom ones?
- 21:05Speaker 2: Yes? Many built in ones, and the source describes building
- 21:08Speaker 2: a custom one loop counter sanitizer or LPC SAND.
- 21:12Speaker 1: What does that do?
- 21:13Speaker 2: It's designed to collect the exact trip count of every
- 21:16Speaker 2: loop in a module. It does this by using an
- 21:19Speaker 2: LVM pass to insert function calls like LPC sandst loopstart
- 21:23Speaker 2: and LPC senate loop in into the IR around loops.
- 21:26Speaker 2: These functions part of the compiler RT runtime library, then
- 21:31Speaker 2: record the counts when the instrumented code runs.
- 21:33Speaker 1: You build your own runtime analysis tools using the same infrastructure.
- 21:37Speaker 1: Very cool. And the other big area.
- 21:38Speaker 2: Was PGO profile guided optimization. The idea here is to
- 21:42Speaker 2: use runtime information not just for debugging, but to enable
- 21:45Speaker 2: more aggressive compiler optimizations.
- 21:48Speaker 1: How does runtime info help optimize?
- 21:50Speaker 2: It tells the compiler how the code is actually used,
- 21:52Speaker 2: which paths are hot, which are cold, which branches are
- 21:55Speaker 2: almost always taken. This lets it make smarter decisions about
- 21:58Speaker 2: things like function in lining, block lege out register allocation
- 22:02Speaker 2: optimizations that are risky without knowing typical behavior.
- 22:05Speaker 1: So it learns from real usage. How do you get
- 22:07Speaker 1: that usage data?
- 22:08Speaker 2: Two main ways. Instrumentation based PGO adds counters directly into
- 22:12Speaker 2: the code. It's very precise, but adds overhead during the
- 22:15Speaker 2: profiling run. Sampling based PGO uses external tools like Linux
- 22:20Speaker 2: perf to statistically sample the program counter during execution. Lower
- 22:24Speaker 2: overhead but less precise data.
- 22:26Speaker 1: Okay, so you gather the data than what.
- 22:29Speaker 2: First, you compile with a flag like clang def profile
- 22:32Speaker 2: generat for instrumentation. This creates a profiled data file when
- 22:36Speaker 2: you run the program. Then you can use llvm def
- 22:39Speaker 2: prof data to merge and maybe inspect this data. It
- 22:42Speaker 2: can show you things like the execution frequency of all
- 22:45Speaker 2: the enclosing basic.
- 22:46Speaker 1: Blocks to see the hotspots exactly.
- 22:49Speaker 2: Finally, you recompile your program, but this time with clang
- 22:52Speaker 2: def profile use, feeding that profile data back into the.
- 22:55Speaker 1: Compiler and the compiler uses it. Yes.
- 22:57Speaker 2: Yeah, the LVMIR actually gets annotated with metadat. You'll see
- 23:00Speaker 2: things like dot prop point seven to one or dot
- 23:03Speaker 2: prof point seven to two attached to instructions or branches.
- 23:06Speaker 2: These represent the collected frequencies and probabilities directly guiding the
- 23:09Speaker 2: optimization passes, the compiler literally learns from the profile that.
- 23:13Speaker 1: Closes the loop nicely, compiler learns from runtime. Okay, wow,
- 23:18Speaker 1: we have covered a ton of ground today.
- 23:20Speaker 2: We really have.
- 23:21Speaker 1: From you know, speeding up those massive builds, mastering testing
- 23:24Speaker 1: with lat and file check's crazy pattern matching.
- 23:27Speaker 2: To crafting custom dsls using table gen even from doughnut
- 23:31Speaker 2: recipes right.
- 23:32Speaker 1: And extending Klang's front end building whole custom tool chains,
- 23:36Speaker 1: then diving deep into LLVMR, the new pass manager analysis
- 23:40Speaker 1: like dominator trees, and.
- 23:42Speaker 2: Finally using runtime techniques like sanitizers for correctness and PGO
- 23:46Speaker 2: for performance, plus all those handy debug utilities.
- 23:49Speaker 1: It's been a whirlwind tour. But the key takeaway, I
- 23:52Speaker 1: think is that this deep dive using a guide like
- 23:55Speaker 1: the source book really offers you a shortcut. Doesn't it
- 23:58Speaker 1: a way to get a handle on LVM's huge, huge capabilities.
- 24:01Speaker 2: Absolutely, it turns these conmplex compiler engineering problems into challenges
- 24:05Speaker 2: you can actually tackle. It gives you that mental framework,
- 24:07Speaker 2: the specific tools, the techniques to really engage with LVM effectively.
- 24:12Speaker 1: We've seen how LVM lets you dissect code, rebuild it,
- 24:15Speaker 1: get incredible control, even less the compiler learn from how
- 24:18Speaker 1: programs run in the real world. With PGO, it's quite powerful,
- 24:21Speaker 1: which leads to a final thought. If the compilation process
- 24:24Speaker 1: itself can learn and adapt based on actual usage, what
- 24:28Speaker 1: fundamental assumptions that we currently make about software design and
- 24:31Speaker 1: development might we need to reconsider next? Something to chew on.
- 24:35Speaker 2: Definitely something to think about and if you want to
- 24:37Speaker 2: dive deeper. The LVM community is very active. Check out
- 24:40Speaker 2: the mailing lists on lists at LVM dot org, or
- 24:43Speaker 2: the discourse forums at LVM dot discourse dot group, or.
- 24:47Speaker 1: Even attended LVM Developers Meeting LVM dot org DDVMTG. Lots
- 24:52Speaker 1: of ways to keep learning and engage.
- 24:53Speaker 2: The resources out there once you know where to look,
- 24:55Speaker 2: are invaluable