Skip to content

[RFC] Formally supporting some suite of "-O*" type flags #19072

Open

Description

@qedawkins

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 of llvm::cl::hidden is, 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 -O0 codegen 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

  1. added
    enhancement ➕
    on Nov 8, 2024
  2. benvanik commented on Nov 8, 2024

    @benvanik
    Collaborator

    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 like IREE::Stream::TransformOptions would have an Option<OptimizationLevel> option, the default options could be set based on --iree-stream-opt=, and the compiler driver would pass in its --iree-opt flag if that was set. A shared OptimizationLevel only 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-distribution they 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).

  3. qedawkins commented on Nov 8, 2024

    @qedawkins
    ContributorAuthor

    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).

    Yes, this binary search is exactly what I want us to be able to start doing. Today we have two main issues

    1. Our compiler is essentially -O3 by default always
    2. Even if you wanted to binary search through the flags that are there, there's no organization that makes it possible for anyone outside of a developer to do (and honestly I don't think there are even any developers of IREE that could accurately navigate all of the flags we have)

    --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.

    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).

    --iree-codegen-llvmgpu-opt=

    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]PUPopulateTranslationPassPipeline take the OptimizationLevel and 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.

    (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).

    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.

  4. MaheshRavishankar commented on Nov 13, 2024

    @MaheshRavishankar
    Collaborator

    THis would be great! Though I think everyone always just wants to use -O1000

  5. MaheshRavishankar commented on Nov 13, 2024

    @MaheshRavishankar
    Collaborator

    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.

  6. IanWood1 commented on Jan 13, 2025

    @IanWood1
    Member

    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-matmul

    Then, 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):

    --iree-opt-strip-assertions
    --iree-opt-data-tiling
    --iree-opt-outer-dim-concat
    

    O2 (good optimization defaults):

    --iree-dispatch-creation-enable-aggressive-fusion=true 
    --iree-opt-generalize-matmul
    
    

    O3 (aggressive):

    --iree-opt-aggressively-propagate-transposes=true
    

    Other possible flags (needs to be categorized)

    
    --iree-hip-waves-per-eu=2
    --iree-llvmgpu-enable-prefetch
    --iree-codegen-gpu-native-math-precision=true
    
  7. benvanik commented on Jan 13, 2025

    @benvanik
    Collaborator

    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::OptimizationLevel for 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 of compiler/src/iree/compiler/Utils/OptionUtils.h that 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) under compiler/src/iree/compiler/Pipelines/Options.h. I suspect we'll need an applyOptimization(binder, level) on each options struct next to the existing bindOptions that used the binder to override flags not set - see compiler/src/iree/compiler/Utils/OptionUtils.cpp printArguments for how you can use isChanged to determine if an option has been overridden. If it hasn't been in the applyOptimization you 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.cpp and we can (whenever) expose it via the embedding API in compiler/bindings/c/iree/compiler/embedding_api.h. To start Session::setFlags in there could do the applyOptimization after setting the flags to whatever the user provides.

  8. self-assigned this
    on Feb 21, 2025
  9. IanWood1 commented on Mar 14, 2025

    @IanWood1
    Member

    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.

  10. hanhanW commented on May 16, 2025

    @hanhanW
    Contributor

    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.

  11. benvanik commented on May 16, 2025

    @benvanik
    Collaborator

    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):

      // Optimization level to be used by the LLVM optimizer (middle-end).
      llvm::OptimizationLevel optimizerOptLevel;
      // Optimization level to be used by the LLVM code generator (back-end).
      llvm::CodeGenOptLevel codeGenOptLevel;
    
  12. qedawkins commented on May 16, 2025

    @qedawkins
    ContributorAuthor

    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 -O flag 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 have LinalgOptimizationLevel and LLVMOptimizationLevel or 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.

  13. benvanik commented on May 16, 2025

    @benvanik
    Collaborator

    what Ian landed is great because in theory all you need to do is add init_at_opt in LLVMCPUTargetCLOptions::bindOptions(OptionsBinder &binder) (and other target backend options) and use it; e.g.:

      auto init_at_opt = binder.optimizationLevel(
          "iree-llvmcpu-opt-level", optLevel,
          llvm::cl::desc("Global optimization level for the LLVMCPU code generation pipeline."),
          llvm::cl::cat(category));
      binder.opt<bool>(
          "iree-llvmcpu-some-flag",
          someFlag,
          {init_at_opt(llvm::OptimizationLevel::O0, false),
           init_at_opt(llvm::OptimizationLevel::O3, true)},
           ...);

    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-level by default sets --iree-llvmcpu-llvm-codegen-opt-level, --iree-llvmcpu-llvm-optimizer-opt-level, --iree-llvmcpu-whatever-opt-level, etc).

  14. hanhanW commented on May 16, 2025

    @hanhanW
    Contributor

    what Ian landed is great because in theory all you need to do is add init_at_opt in LLVMCPUTargetCLOptions::bindOptions(OptionsBinder &binder) (and other target backend options) and use it

    Yes, I tracked the implementation a few weeks back, and I like how we set it up!

  15. self-assigned this
    on Jan 23, 2026
  16. hanhanW commented on Jan 23, 2026

    @hanhanW
    Contributor

    #23259 is the first step for CPU support. GPU and other backends can follow the PR to add the support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement ➕

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions