Repository navigation
Description
Motivation
In the process of trying to land #18474 I ran into a number of correctness issues related to LLVM backends where the only saving grace was having a reference that gave correct numerics to compare against. I often found myself hacking in adjustments to the LLVM optimization level for comparison, or string parsing on dispatch names for finer grain control over various pipelines.
Moving forward we still have a fairly large suite of models with correctness issues across multiple backends: https://github.com/nod-ai/e2eshark-reports/blob/main/2024-11-07/ci_reports_onnx/rocm/combined-reports/summary.md + https://github.com/nod-ai/e2eshark-reports/blob/main/2024-11-07/ci_reports_onnx/llvm-cpu/combined-reports/summary.md
We need to find a way to be much better about numerics if we want consistent users of the compiler. Focusing on a few of the models that we care about for only the most optimized paths is not going to help get us there.
Currently we have a set of flags scattered throughout the code base that control various optimizations, most of which are leftover from when the underlying feature was under development. To name a few:
--iree-opt-aggressively-propagate-transposes--iree-opt-outer-dim-concat--iree-opt-data-tiling--iree-opt-numeric-precision-reduction--iree-dispatch-creation-enable-aggressive-fusion--iree-scheduling-optimize-bindings--iree-llvmcpu-disable-distribution--iree-llvmcpu-disable-vector-peeling--iree-codegen-llvmgpu-use-vector-distribution--iree-codegen-llvmgpu-use-igemm
99% of this issue is focused at codegen (as that's where 99%+ of the numerics issues come from) but other phases (e.g. DispatchCreation) also includes a large space of optimization choices.
Proposal
What I'm proposing is:
- Limit the proliferation of "developer flags." This includes being clearer about the naming when it is for enabling an in-progress feature that is intended to be turned on by default (i.e.
-test-or-experimental-naming prefixes). I don't know what the opinion ofllvm::cl::hiddenis, but that is another option to avoid exposing every new/developer flag to the user. This cleanup will help with formalizing the compiler's APIs for controlling optimizations. - Expose [codegen] backend optimization level flags (mainly for LLVM based backends, I don't think there is any equivalent in SPIR-V) as compiler options (not floating flags)
- Add support for true
-O0codegen pipelines that prioritize correctness and can handle any input + expose flags to enable them (perhaps a shared flag) - Add a mechanism to control codegen "opt" levels per dispatch
- Do a coarse grouping of compiler flags that we can enable/disable all at once (similar to clang -O___). I don't know if our compiler is suited for global optimization levels, so this may make more sense per compilation phase.
I am not an expert in API design, this is just my intuition for the kind of reorganization that I think could help us make progress on the swath of correctness issues we have. Also there have been a number of improvements to the pass rate of the model suite listed above recently, and I believe almost all of those have been from frontend improvements that are papering over correctness issues that are still there.
Activity
benvanik commentedon Nov 8, 2024
Has been on the TODO list for awhile, would be nice to have.
In a multi-stage compiler we will always need a way to control each area (input/frontend level with asserts/etc, global tensor optimization, fusion, host codegen (stream/hal/vm/etc), device codegen) so having per-stage opt flags is the baseline. For most purposes like you're working through here a global flag across all stages will only help the first step of a binary search ("does literally anything in the compiler change anything anywhere") - useful, but rarer in workflows. The more we can also scope things the more effective the binary search is ("does anything in codegen change behavior yes/no, does anything in global opt change behavior yes/no" etc).
I'd like to see any top-level transformation pipeline have an opt flag and the compiler frontend propagate them consistently:
--iree-stream-opt=--iree-hal-opt=--iree-flow-opt=--iree-codegen-llvmgpu-opt=etc (named after the pipeline names, like--iree-hal-transformation-ipeline->-opt). Then we can have an--iree-opt=that sets those all. If those compiler driver flags just set the transform options for each pipeline we can still create reproducers and such that include the opt level. So concretely something likeIREE::Stream::TransformOptionswould have anOption<OptimizationLevel>option, the default options could be set based on--iree-stream-opt=, and the compiler driver would pass in its--iree-optflag if that was set. A sharedOptimizationLevelonly really works at those high-level pipelines (as you may want finer down below) but that's ok - if a user wants to still pass--iree-llvmcpu-disable-distributionthey can (though naming wise we should never include disable/enable in a flag name - a boolean flag true denotes enabled and false denotes disabled, and --foo-disable=true is weird).qedawkins commentedon Nov 8, 2024
Yes, this binary search is exactly what I want us to be able to start doing. Today we have two main issues
This sounds perfect to me. We can start by getting some of this structure in place and then figure out what each phase means in isolation (per the people who work on each one).
Currently AFAICT top level compiler options are inaccessible to codegen directly, only to the target backends, so I was assuming we'd make
LLVM[G|C]PUPopulateTranslationPassPipelinetake theOptimizationLeveland then we get--iree-llvmcpu-codegen-opt=/--iree-rocm-codegen-opt=/--iree-cuda-codegen-opt=. That might clash with LLVM opt levels though and I'd want to have separate control over what our codegen does and what LLVM does. Maybe those could be--iree-*-llvm-opt.I wasn't really going to go after naming, but makes sense. I am not in the mood right now to go rename all of the flags though :P.
MaheshRavishankar commentedon Nov 13, 2024
THis would be great! Though I think everyone always just wants to use
-O1000MaheshRavishankar commentedon Nov 13, 2024
But without being tongue in cheek, lets start with -O1. I think it is more to manage some features that are ready on some backend but not on other backends for the dispatch region formation stuff. I keep one flag there (
--iree-dispatch-creation-enable-aggressive-fusion=true). Do we need any of the RoCM flags? There are developer flags that can allow going down "conservative" compilation. But if these are flags that are just developer flags they dont qualify to be part of optimization flags. Basically anything you add to a list of "optimization level controlled flags" is a long-term feature support. If these flags dont fall into it, then they are purely developer flags.IanWood1 commentedon Jan 13, 2025
First, we need to move
---iree-preprocessing-pass-pipeline="builtin.module(util.func(iree-preprocessing-generalize-linalg-matmul-experimental))"out of preprocessing and have its own flag. E.g.iree-global-opt-generalize-linalg-matmulThen, introduce 3 global optimization levels (O1, O2, O3) and corresponding levels for each pipeline. For now, the global level directly sets each per-pipeline opt level. And each pipeline sets it's specific flags.
O0
O1 (backed support):
O2 (good optimization defaults):
O3 (aggressive):
Other possible flags (needs to be categorized)
benvanik commentedon Jan 13, 2025
don't forget 0!
Since this stuff is pretty tricky (interacting with plugins which may provide their own pipelines that need to pick up the flag, used via the API from python, etc) here's some notes from when I was looking at this awhile back:
We should use or fork
llvm::OptimizationLevelfor our global flags - it already handles everything we want (levels, size/speed tradeoff, etc). From there pipelines can define their own but always have a default constructor that takes the global one.An initial change would consist of an
llvm::cl::option that parses/prints the OptimizationLevel so that we can have global flags and MLIR pass/pipeline options embed it as normal MLIR options (reproducers/etc can then pick them up) - we have some already at the bottom ofcompiler/src/iree/compiler/Utils/OptionUtils.hthat can be used to see how it's done. It's important that plugins and users of the compiler via API have the same abilities the top-level iree-compile tool does and being just another option would let us do that.After that, adding that to each of the top-level option structs (
IREE::GlobalOptimizationOptions,IREE::SchedulingOptions, etc) undercompiler/src/iree/compiler/Pipelines/Options.h. I suspect we'll need anapplyOptimization(binder, level)on each options struct next to the existingbindOptionsthat used the binder to override flags not set - seecompiler/src/iree/compiler/Utils/OptionUtils.cppprintArgumentsfor how you can useisChangedto determine if an option has been overridden. If it hasn't been in theapplyOptimizationyou can force it - e.g.applyOptimization(...) { binder.overrideDefault("iree-scheduling-opt", level); }. As we go down the layers that can be specified to be more interesting (fan out to multiple flags, etc).Finally the global flag would live near
compiler/src/iree/compiler/API/Internal/CompilerDriver.cppand we can (whenever) expose it via the embedding API incompiler/bindings/c/iree/compiler/embedding_api.h. To startSession::setFlagsin there could do theapplyOptimizationafter setting the flags to whatever the user provides.Add `iree-opt-level` and per-pipeline optimization flags (#20047)
IanWood1 commentedon Mar 14, 2025
I just merged #20216 which updates the docs to reflect the recent addition of
iree-opt-level& pipeline flags.We still need to add support for codegen related flags.
hanhanW commentedon May 16, 2025
I'm coming from a recent issue (#20824), and I think we need to enable O0 feature for CPU backends. The rough idea is that all the dispatches go to CPUDefault that does not include any optimization (e.g., distribution, tiling, vectorization.) The codegen logic is dumb -- just lower them to scalar codes and basic loops. I think it could help the numerical debugging in the future.
We might need a few more flags for it.
benvanik commentedon May 16, 2025
O0 would be great to enable on the CPU path - there's LLVMTargetOptions that should have their defaults set by it too (the design allows for overriding if the user wants to O0 most of IREE and then O3 the CPU side, or the other way around - compile the model as if O3 was set for dispatch creation/etc but O0 just our/LLVM codegen):
qedawkins commentedon May 16, 2025
Yes, strongly agree with -O0. And what Ben says is key, we need different -O levels for different phases. If -O0 means we get different dispatches, often times that won't be very useful. Step 1 then is probably to plumb
-Oflag support through to codegen. Ian did a great job with the other phases but no one has had cycles to add it to the TargetBackend yet. We might want TargetBackends to be able to register their own optimization groups somehow (i.e. ROCMTarget can haveLinalgOptimizationLevelandLLVMOptimizationLevelor more).I was trying to come up with how to do O0 for GPU, and I think LLVMGPUTileAndFuse is mostly setup for it, but we might still need some basic workgroup level distribution even there. I haven't completely worked out what "bare minimum" means for GPU yet.
benvanik commentedon May 16, 2025
what Ian landed is great because in theory all you need to do is add
init_at_optinLLVMCPUTargetCLOptions::bindOptions(OptionsBinder &binder)(and other target backend options) and use it; e.g.:The
--iree-llvmcpu-opt-level=flag is by default set to whatever the global optimization flag is, but you can override it. You should be able to have as many as you want in a single bindOptions (auto init_at_opt_for_llvm = binder.optimizationLevel...,auto init_at_opt_for_mlir = binder.optimizationLevel..., etc). Then you just make your flags dependent on it. I don't know how deep it can nest automatically, but we can create some helpers for it if we wanted (--iree-llvmcpu-opt-levelby default sets--iree-llvmcpu-llvm-codegen-opt-level,--iree-llvmcpu-llvm-optimizer-opt-level,--iree-llvmcpu-whatever-opt-level, etc).hanhanW commentedon May 16, 2025
Yes, I tracked the implementation a few weeks back, and I like how we set it up!
Add `iree-opt-level` and per-pipeline optimization flags (#20047)
[Codegen] Introduce optimization level to Codegen.
[Codegen] Introduce optimization level to Codegen.
[Codegen] Introduce optimization level to Codegen.
hanhanW commentedon Jan 23, 2026
#23259 is the first step for CPU support. GPU and other backends can follow the PR to add the support.
[CPU] Introduce optimization level to CPU backend. (#23259)
[CPU] Introduce optimization level to CPU backend. (iree-org#23259)