Repository navigation
Add umbrella optimization flags - #20047
Conversation
| void GlobalPipelineOptions::bindOptions(OptionsBinder &binder) { | ||
| static llvm::cl::OptionCategory category( | ||
| "IREE global pipeline options controlling the entire compilation flow."); | ||
|
|
||
| binder.opt<llvm::OptimizationLevel>( | ||
| "iree-opt-level", optLevel, | ||
| llvm::cl::desc("Global optimization level to apply to the entire " | ||
| "compilation flow."), | ||
| llvm::cl::cat(category)); | ||
| } |
There was a problem hiding this comment.
Just dropping a reminder here: we have some existing docs for "optimization options" that are due to a rework, especially after these changes land: https://iree.dev/reference/optimization-options/
We could even add a snippet that we include on each page like https://iree.dev/guides/deployment-configurations/cpu/#compile-and-run-a-program and https://iree.dev/guides/deployment-configurations/gpu-vulkan/#compile-and-run-a-program that explains what to expect out of the box and how to optimize / tune / configure from there (linking to the optimization options reference page).
There was a problem hiding this comment.
@ScottTodd I'm going to merge this so that others can add flags, but I'll open a followup PR to fix the docs
There was a problem hiding this comment.
Thanks. We do have a release coming up and we'll be choosing a release candidate around 1 week from now. If you can get this ready for use and documented in time then we can advertise it in the release notes (#19960).
| void GlobalOptimizationOptions::applyOptimization( | ||
| const OptionsBinder &binder, const GlobalPipelineOptions &globalLevel) { | ||
| binder.overrideDefault("iree-global-optimization-opt-level", optLevel, | ||
| globalLevel.optLevel); | ||
|
|
||
| if (optLevel != llvm::OptimizationLevel::O0) { | ||
| binder.overrideDefault("iree-opt-strip-assertions", stripAssertions, true); | ||
| llvm::dbgs() << "stripAssertions " << stripAssertions << '\n'; | ||
| } | ||
| }; | ||
|
|
||
| void GlobalOptimizationOptions::bindOptions(OptionsBinder &binder) { | ||
| static llvm::cl::OptionCategory category( | ||
| "IREE options for controlling global optimizations."); | ||
| binder.opt<llvm::OptimizationLevel>( | ||
| "iree-global-optimization-opt-level", optLevel, | ||
| llvm::cl::desc("Optimization level for the this pipeline"), | ||
| llvm::cl::cat(category)); |
There was a problem hiding this comment.
@benvanik I attempted to do similar to what you suggested but I've run into some problems.
As an example init_at_opt_level would be the function used to tell the binder to set stripAssertions to true when at O3 (and there wouldn't be a need for the manual applyOptimization):
binder.opt<bool>(
"iree-opt-strip-assertions",
stripAssertions,
init_at_opt_level(O3, true));The problem I'm having is that iree-opt-strip-assertions should be set based upon the value iree-global-optimization-opt-level and I don't have a good way to let the binder know that.
There is a secondary problem, too. As you mentioned, the options shouldn't be directly set but rather a copy should be made. This is needed because we don't want setting optimization levels to affect the other flags for a session. This makes it hard (impossible?) to to make it so that GlobalOptimizationOptions::stripAssertions should be set based on GlobalOptimizationOptions::optLevel from inside of of bindOptions because the GlobalOptimizationOptions object we use to bind won't be the one that has the optimizations applied.
There was a problem hiding this comment.
This is a real good start!
There's a few super magic ways we could do things, but for v0 I'd have your applyOptimization above walk through the options registered on a binder and if there are registered init_at_opt_level entries perform the override. It's a bit of boilerplate we could remove with template magic but copy/pasting for now is fine. something like:
applyOptimization:
binder.overrideDefault("...-opt-level", optLevel, ...);
binder.overrideOptimizationDefaults(optLevel);
// if there are nested binders, call applyOptimization on them
The fancier automatic way would be to have the pipeline opt level option be stashed by the binder - instead of binder.opt<OptimizationLevel>(...) could have binder.optimizationLevel("...-opt-level") to make it explicit what the primary scoped optimization level option is and then the applyOptimization default impl above can do everything itself: the top level gets called, it checks if it has a registered optimization level option, overrides it if needed, and then calls overrideOptimizationDefaults. Then we'd only need to override it if we had nested options (but even that could be improved, and for now we could ignore).
- Use llvm::OptimizationLevel - Fix `isChanged` callback and move previus impl to `isDefault` - Add tests for OptionUtils Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
- Uses the overall optimization level to set global opt's phase optimization level. Also, sets strip assertions accordingly - Adds a test to make sure that the session flags are not modified. Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
|
This is still a WIP, but I opened the PR and requested reviews to get some feedback on the general approach. There is definitely some cleanup needed + only 1 flag is setup to use the optimization levels. I would like to hear some thoughts on #20047 (comment) before flushing this out. |
| void GlobalOptimizationOptions::applyOptimization( | ||
| const OptionsBinder &binder, const GlobalPipelineOptions &globalLevel) { | ||
| binder.overrideDefault("iree-global-optimization-opt-level", optLevel, | ||
| globalLevel.optLevel); | ||
|
|
||
| if (optLevel != llvm::OptimizationLevel::O0) { | ||
| binder.overrideDefault("iree-opt-strip-assertions", stripAssertions, true); | ||
| llvm::dbgs() << "stripAssertions " << stripAssertions << '\n'; | ||
| } | ||
| }; | ||
|
|
||
| void GlobalOptimizationOptions::bindOptions(OptionsBinder &binder) { | ||
| static llvm::cl::OptionCategory category( | ||
| "IREE options for controlling global optimizations."); | ||
| binder.opt<llvm::OptimizationLevel>( | ||
| "iree-global-optimization-opt-level", optLevel, | ||
| llvm::cl::desc("Optimization level for the this pipeline"), | ||
| llvm::cl::cat(category)); |
There was a problem hiding this comment.
This is a real good start!
There's a few super magic ways we could do things, but for v0 I'd have your applyOptimization above walk through the options registered on a binder and if there are registered init_at_opt_level entries perform the override. It's a bit of boilerplate we could remove with template magic but copy/pasting for now is fine. something like:
applyOptimization:
binder.overrideDefault("...-opt-level", optLevel, ...);
binder.overrideOptimizationDefaults(optLevel);
// if there are nested binders, call applyOptimization on them
The fancier automatic way would be to have the pipeline opt level option be stashed by the binder - instead of binder.opt<OptimizationLevel>(...) could have binder.optimizationLevel("...-opt-level") to make it explicit what the primary scoped optimization level option is and then the applyOptimization default impl above can do everything itself: the top level gets called, it checks if it has a registered optimization level option, overrides it if needed, and then calls overrideOptimizationDefaults. Then we'd only need to override it if we had nested options (but even that could be improved, and for now we could ignore).
|
@benvanik replying to your comment above, I was trying to get this to work but I think there is a problem with having the binder automatically apply flag modifications specified with Maybe its a problem with how I'm trying to implement it, so I will share some details. I was thinking that For example: GlobalOptimizationOptions options0;
{
GlobalOptimizationOptions options1;
// binds options and stores reference to `options1.stripAssertions` to be updated
// when `applyOptimizations` is called.
options1.bindOptions(binder);
options0 = optioins1;
}
// set from flags...
// Problematic because binder is storing references to options1's members.
// So, `options0` never gets updated and this is UB
options0.applyOptimizations(binder); |
|
I didn't think of using callbacks and instead just storing the data as a field on the options info that is cloneable - SmallVector or something of the optimization level -> value. |
Okay, so this would still require something that passes this field to the binder? Something roughly similar to void applyOptimizations(...){
binder.overrideDefault("...-opt-level", optLevel, ...);
binder.overrideDefaultWith(optLevel, stripAssertions, stripAssertionsOptToDefaultMap);
}And the benefit would be printability of default levels at each optimization level because they are declared during binding? |
|
I don't think so. You'd have a subclass of A user should then be able to do something like binder.opt<bool>(
"iree-opt-strip-assertions",
stripAssertions,
init_at_opt_level(O2, true));(that make_unique'ing your optimization_level_opt, the init_at_opt_level calling the setInitialValue(O3, true) to add it to the opt vector, and your generic applyOptimizations walking over it to do the "if initial value then walk vector and find the max opt level value and use that" (e.g. the O2 option should be picked up above if -O3 was specified)) (rough idea, but it should work just as |
46d1505 to
1f8ae49
Compare
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
| "--iree-opt-strip-assertions=true", | ||
| "--iree-global-opt-propagate-transposes=true", | ||
| "--iree-dispatch-creation-enable-fuse-horizontal-contractions=true", | ||
| "--iree-dispatch-creation-enable-aggressive-fusion=true", | ||
| "--iree-opt-aggressively-propagate-transposes=true", | ||
| "--iree-opt-outer-dim-concat=true", | ||
| "--iree-opt-generalize-matmul=true", |
There was a problem hiding this comment.
Picked a few (hopefully uncontroversial flags) to move under the optimization defaults
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
|
|
||
| binder.opt<bool>( | ||
| "iree-opt-generalize-matmul", generalizeMatmul, | ||
| {init_at_opt(llvm::OptimizationLevel::O0, false), |
| auto globalBinder = OptionsBinder::global(); | ||
| auto &binder = | ||
| session.globalInit.usesCommandLine ? globalBinder : session.binder; | ||
| GlobalOptimizationOptions highLevelOptimizationOptions = | ||
| session.highLevelOptimizationOptions; | ||
|
|
||
| // Set optimization options on a copy of the sessions options. | ||
| binder.setApplyOptimizations(session.pipelineOptions.optLevel); | ||
| auto scope = llvm::make_scope_exit([&] { binder.unsetApplyOptimizations(); }); | ||
| highLevelOptimizationOptions.bindOptions(binder); |
There was a problem hiding this comment.
This unfortunately isn't going to work. We need to do this via reflection instead of explicitly doing it per type, and need to do it consistently for all options - including those in plugins. The OptionsBinder is the thing that has that information (plugins have createUninitializedSession that binds their local options, and all these are registered too).
Ideally we'd not have pointers stored in option info and instead be storing relative offsets into option structs - then we could apply bound options to any allocated options struct. With the current design, though, the absolute addresses being captured prevents that.
A workaround would be to have the option info in the binder be able to store a previous value for each option - instead of cloning the options and rebinding you would run through all options on the binder, stash their current value in the option info, apply the optimization overrides, and return - the code below consuming options would not change (again, important for plugins) - and then afterward you'd run back through the options on the binder and restore the value to the one that was stashed. This mutates the session/global options but does satisfy the requirement that all options are handled (including those out-of-tree/in plugins). Sessions are thread-compatible and there cannot be two invocations with the same session concurrently and when global options are used concurrent invocation is not supported so at least this would be safe.
Kind of bad choices all around due to the pointer capture, unfortunately. Having OptionsBinder::applyOptimizationDefaults/revertOptimizationDefaults be the only change needed in here at least contains the insanity, though.
There was a problem hiding this comment.
Okay, that makes sense. The problem I'm having with that is that the Session class makes a copy of the options if they are from the command line: highLevelOptimizationOptions = *globalInit.clGlobalOptimizationOptions;. Meaning that applyOptimizationDefaults/revertOptimizationDefaults would mutate globalInit.clGlobalOptimizationOptions but not highLevelOptimizationOptions.
This seems fairly straightforward to solve by using a reference/pointer to either the local option or the global option instead of copying. But I just want to make sure I'm understanding you correctly.
There was a problem hiding this comment.
Okay, this should now support plugins by storing pointers in the binder. However, the code has gotten a bit messier and it feels a bit prone to errors due to all the pointer manipulation.
I needed to add opt_scope to link pipeline optimization levels (iree-global-optimization-opt-level) to its nested overrides (iree-opt-strip-assertion). I had to add a tree-like structure to make sure that options are overridden in order.
In CompilerDriver.cpp I added a call to binder.applyOptimizationDefaults() with the global binder because isChanged will always be false when using the local binder + cl options.
Signed-off-by: Ian Wood <[email protected]>
benvanik
left a comment
There was a problem hiding this comment.
Nice accounting for plugins!
| static llvm::cl::OptionCategory category( | ||
| "IREE global pipeline options controlling the entire compilation flow."); | ||
|
|
||
| binder.topLevelOpt( |
There was a problem hiding this comment.
Having a method that should only ever be called once is odd - it indicates some spooky coupling and that this should either be an inherent part of the OptionsBinder (instead of having this separate GlobalPipelineOptions) or this shouldn't be a requirement of the API (no special topLevelOpt, can register things in any order). Basically, if it's required (for some spooky reason) that this is called one and called prior to any other binder usage then it must become part of the OptionsBinder or whatever requirement is making this need to be special should be fixed.
That you have the option name hardcoded into the binder also indicates that this should really be in there or that special handling needs to move out - having it in both places doesn't really add anything.
Preferably, though, you don't need a top level option to be registered in any particular order - instead, if there's only one top level option allowed then just store what you're storing on the OptionsBinder, treat this global opt level as a normal option, and when you call in to apply optimizations make it take an arg that is the global opt level as read from this normal option (which it looks like you already do in CompilerDriver, so that this is special here definitely is calling for it to be excised and moved back there and not treated as special).
There was a problem hiding this comment.
I allowed OptionInfos to be created before the option was registered. This let me append children in any order and also remove
static auto &_ = OptionsFromFlags<GlobalPipelineOptions>::get();I still need to determine root(s) to start applying optimizations from, so I kept topLevelOpt but now it just specifies a root (could be >1 root).
There was a problem hiding this comment.
Also, each of the stored OptionInfo may already exist when binding an option, so I need to set each field manually instead of assigning from a OptionInfo{...}.
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
d79d962 to
886bcf5
Compare
Signed-off-by: Ian Wood <[email protected]>
Signed-off-by: Ian Wood <[email protected]>
| void appendToDesc(std::string &desc) { | ||
| llvm::raw_string_ostream os(desc); | ||
| os << "\nAt optimization level " << optLevel << " the default is "; | ||
| prettyPrint(os, init); |
There was a problem hiding this comment.
Hi @IanWood1 I think prettyPrint is causing iree to have issues compiling on windows
https://github.com/iree-org/iree/actions/runs/13807906570/job/38665664265#step:9:6875
Does this look easily fixable? I haven't worked directly on this codebase for a while, so I'm not sure
There was a problem hiding this comment.
Also gcc, affecting:
- The x86 linux gcc build: https://github.com/iree-org/iree/actions/runs/13807869735/job/38622447033#step:4:10343
- aarch64 main-dist release builds: https://github.com/iree-org/iree/actions/runs/13804121155/job/38611734817#step:8:6530
Sample logs:
[7825/8403] Building CXX object compiler/src/iree/compiler/API/Internal/CMakeFiles/iree_compiler_API_Internal_IREEOptToolEntryPoint.objects.dir/IREEOptToolEntryPoint.cpp.o
FAILED: compiler/src/iree/compiler/API/Internal/CMakeFiles/iree_compiler_API_Internal_IREEOptToolEntryPoint.objects.dir/IREEOptToolEntryPoint.cpp.o
/usr/local/bin/ccache /usr/bin/g++-9 -DCPUINFO_SUPPORTED_PLATFORM=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_EXAMPLE=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_CUDA=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_LLVM_CPU=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_METAL_SPIRV=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_ROCM=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_VMVX=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_HAL_TARGET_VULKAN_SPIRV=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_INPUT_STABLEHLO=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_INPUT_TORCH=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_INPUT_TOSA=1 -DIREE_COMPILER_PLUGIN_HAVE_STATIC_SIMPLE_IO_SAMPLE=1 -DIREE_HAVE_C_OUTPUT_FORMAT -DIREE_HAVE_HAL_EXECUTABLE_EMBEDDED_ELF_PLUGIN=1 -DIREE_HAVE_HAL_EXECUTABLE_LOADER_EMBEDDED_ELF=1 -DIREE_HAVE_HAL_EXECUTABLE_LOADER_SYSTEM_LIBRARY=1 -DIREE_HAVE_HAL_EXECUTABLE_LOADER_VMVX_MODULE=1 -DIREE_HAVE_HAL_EXECUTABLE_SYSTEM_LIBRARY_PLUGIN=1 -DIREE_HAVE_HAL_LOCAL_TASK_DRIVER_MODULE=1 -DIREE_HAVE_VMVX_MODULE -I/__w/iree/iree -I/__w/iree/iree/build-gcc -I/__w/iree/iree/third_party/llvm-project/llvm/include -I/__w/iree/iree/build-gcc/llvm-project/include -I/__w/iree/iree/third_party/llvm-project/mlir/include -I/__w/iree/iree/build-gcc/llvm-project/tools/mlir/include -I/__w/iree/iree/third_party/llvm-project/lld/include -I/__w/iree/iree/build-gcc/llvm-project/tools/lld/include -I/__w/iree/iree/compiler/src -I/__w/iree/iree/build-gcc/compiler/src -I/__w/iree/iree/runtime/src -I/__w/iree/iree/build-gcc/runtime/src -I/__w/iree/iree/build-gcc/runtime/src/iree/base/internal/flatcc -I/__w/iree/iree/build-gcc/runtime/src/iree/schemas -I/__w/iree/iree/third_party/stablehlo -I/__w/iree/iree/build-gcc/llvm-external-projects/stablehlo -I/__w/iree/iree/llvm-external-projects/iree-dialects/include -I/__w/iree/iree/build-gcc/llvm-external-projects/mlir-iree-dialects/include -I/__w/iree/iree/compiler/plugins/input/Torch -I/__w/iree/iree/build-gcc/compiler/plugins/input/Torch -I/__w/iree/iree/third_party/torch-mlir/include -I/__w/iree/iree/compiler/bindings/c/. -I/__w/iree/iree/build-gcc/compiler/bindings/c/. -I/__w/iree/iree/build-gcc/runtime/src/iree/schemas/instruments -I/__w/iree/iree/build-gcc/build_tools/third_party/cuda -I/__w/iree/iree/third_party/spirv_cross -I/__w/iree/iree/samples/compiler_plugins/example/src -I/__w/iree/iree/build-gcc/compiler/plugins/samples/example/src -I/__w/iree/iree/samples/compiler_plugins/simple_io_sample/src -I/__w/iree/iree/build-gcc/compiler/plugins/samples/simple_io_sample/src -I/__w/iree/iree/third_party/cpuinfo/include -isystem /__w/iree/iree/third_party/flatcc/include -O3 -fPIC -fvisibility=hidden -fno-rtti -fno-exceptions -Wall -Werror -Wno-error=deprecated-declarations -Wno-address -Wno-address-of-packed-member -Wno-comment -Wno-format-zero-length -Wno-uninitialized -Wno-overloaded-virtual -Wno-invalid-offsetof -Wno-sign-compare -Wno-unused-function -Wno-unknown-pragmas -Wno-unused-but-set-variable -Wno-misleading-indentation -fmacro-prefix-map=/__w/iree/iree=iree -I/__w/iree/iree/third_party/flatcc/include/ -I/__w/iree/iree/third_party/flatcc/include/flatcc/reflection/ -std=gnu++17 -MD -MT compiler/src/iree/compiler/API/Internal/CMakeFiles/iree_compiler_API_Internal_IREEOptToolEntryPoint.objects.dir/IREEOptToolEntryPoint.cpp.o -MF compiler/src/iree/compiler/API/Internal/CMakeFiles/iree_compiler_API_Internal_IREEOptToolEntryPoint.objects.dir/IREEOptToolEntryPoint.cpp.o.d -o compiler/src/iree/compiler/API/Internal/CMakeFiles/iree_compiler_API_Internal_IREEOptToolEntryPoint.objects.dir/IREEOptToolEntryPoint.cpp.o -c /__w/iree/iree/compiler/src/iree/compiler/API/Internal/IREEOptToolEntryPoint.cpp
In file included from /__w/iree/iree/compiler/src/iree/compiler/Pipelines/Options.h:10,
from /__w/iree/iree/compiler/src/iree/compiler/PluginAPI/Client.h:14,
from /__w/iree/iree/compiler/src/iree/compiler/PluginAPI/PluginManager.h:14,
from /__w/iree/iree/compiler/src/iree/compiler/API/Internal/IREEOptToolEntryPoint.cpp:13:
/__w/iree/iree/compiler/src/iree/compiler/Utils/OptionUtils.h:58:13: error: explicit specialization in non-namespace scope 'struct mlir::iree_compiler::opt_initializer<Ty>'
58 | template <>
| ^
/__w/iree/iree/compiler/src/iree/compiler/Utils/OptionUtils.h:59:58: error: template-id 'prettyPrint<bool>' in declaration of primary template
59 | void prettyPrint<bool>(llvm::raw_ostream &os, bool &val) {
| ^
Shall we revert for now, or do we want to attempt a fix forward? As this broke the Windows release build, this will block releases and slow down updates across downstream projects like iree-turbine and shark-ai until resolved.
There was a problem hiding this comment.
Could try this: https://stackoverflow.com/a/13018943 or one of the other answers there
Just take the template specialization outside the class declaration. gcc doesn't allow inline template specialization.
As another option, just deleting line template<> seems to work for me.
There was a problem hiding this comment.
Deleting the template<> worked, opened PR #20231
This change adds support for optimization flags that can be used to broadly control compiler optimizations. `iree-opt-level` is the name of the flag that would control the overall optimization level for the compiler invocation and it would cascade down to more fine-grained flags (i.e. `iree-opt-strip-assertions`). This will give users an easy way to control the compilation process and reduce the number of flags required to get the desired optimizations. Progress towards #19072 --------- Signed-off-by: Ian Wood <[email protected]>
This change adds support for optimization flags that can be used to broadly control compiler optimizations. Currently,
iree-opt-levelis the name of the flag that would control the overall optimization level for the compiler invocation and it would cascade down to more fine-grained flags (i.e.iree-opt-strip-assertions). This will give users an easy way to control the compilation process and reduce the number of flags required to get the desired optimizations.Progress towards #19072