Repository navigation
[Codegen] Use attributes to define default tuning specs - #20979
Conversation
Currently default tuning specs are registered to a global embedded directory when the plugin for a backend is registered. Then the MaterializeTuningSpecs pass would try to interpret the target attribute as a gpu target attr to get a target arch to use. This is action at a distance that only works for gpu targets. This patch replaces the global directory with a simple lookup for "iree_codegen.default_tuning_spec" in the `hal.executable.target` configuration. The attribute stored there maintains a reference to a (typically static) module that can be retrieved by attribute interface, giving target backends freedom over the mechanism used to deliver the tuning spec + caching implementations. Today only the ROCMTarget implements a default tuning spec (for a single arch at that), so this PR adds a ROCMDialect to the ROCM plugin to implement the `rocm.builtin.tuning_module` attribute. Any builtin accessed through this attribute is cached on the dialect (same as the iree_codegen dialect). The same move to attribute based reference will be done for ukernels as a follow up (but with a different interface given that parsing is not needed).
| /// Helper for checking whether the requested builtin is present. | ||
| bool hasBuiltin(StringRef name); | ||
|
|
||
| /// Returns the loaded builtin module for the `path`. If the module has | ||
| /// already been loaded in the past, returns the memoized module without | ||
| /// parsing the file again. This function is thead-safe. | ||
| FailureOr<::mlir::ModuleOp> | ||
| getOrLoadBuiltinModule(StringRef path); | ||
|
|
||
| private: | ||
|
|
||
| /// Pseudo-directory for storing references to builtins. | ||
| EmbeddedDataDirectory builtins; | ||
|
|
||
| /// Map from "filename" to module, cached on load. Accesses to this map are | ||
| /// protected by a mutex. | ||
| ::llvm::StringMap<::mlir::OwningOpRef<::mlir::ModuleOp>> builtinModules; | ||
|
|
||
| /// Lock to control the updating of the builtin modules such that we only | ||
| /// load the module once and can reuse it across all invocations. | ||
| std::mutex builtinMutex; |
There was a problem hiding this comment.
for the next backend that implements this it'd probably be worth pulling this out to sit with EmbeddedDataDirectory somehow - e.g., EmbeddedModuleCache and then have it take the filetoc (or a list of them) in its constructor.
There was a problem hiding this comment.
hmm maybe not - maybe a DialectInterface with a default implementation - something less boilerplate-y if this grows. For now copy/paste is probably fine, but that tends to drift.
There was a problem hiding this comment.
I started with a dialect interface but wasn't sure exactly what to make it and did the copy paste because the implementation for the codegen dialect was sufficiently different. I think it'll be easier to figure out reuse with the second backend.
kuhar
left a comment
There was a problem hiding this comment.
Is this dropping support for sku-specific tuning specs? (I wouldn't necessarily object to this, just want to get some clarity on this)
Looks good otherwise, just some nits
Technically it was still supported because it was based on ROCMOptions.target (which will either be the target arch or the sku) but I realized that would mean if an sku is specified, then it will never fall back to the target arch which isn't great. Updated to handle that better now. |
Currently default tuning specs are registered to a global embedded directory when the plugin for a backend is registered. Then the MaterializeTuningSpecs pass would try to interpret the target attribute as a gpu target attr to get a target arch to use. This is action at a distance that only works for gpu targets. This patch replaces the global directory with a simple lookup for "iree_codegen.default_tuning_spec" in the `hal.executable.target` configuration. The attribute stored there maintains a reference to a (typically static) module that can be retrieved by attribute interface, giving target backends freedom over the mechanism used to deliver the tuning spec + caching implementations. Today only the ROCMTarget implements a default tuning spec (for a single arch at that), so this PR adds a ROCMDialect to the ROCM plugin to implement the `rocm.builtin.tuning_module` attribute. Any builtin accessed through this attribute is cached on the dialect (same as the iree_codegen dialect). The same move to attribute based reference will be done for ukernels as a follow up (but with a different interface given that parsing is not needed).
Currently default tuning specs are registered to a global embedded directory when the plugin for a backend is registered. Then the MaterializeTuningSpecs pass would try to interpret the target attribute as a gpu target attr to get a target arch to use. This is action at a distance that only works for gpu targets.
This patch replaces the global directory with a simple lookup for "iree_codegen.default_tuning_spec" in the
hal.executable.targetconfiguration. The attribute stored there maintains a reference to a (typically static) module that can be retrieved by attribute interface, giving target backends freedom over the mechanism used to deliver the tuning spec + caching implementations.Today only the ROCMTarget implements a default tuning spec (for a single arch at that), so this PR adds a ROCMDialect to the ROCM plugin to implement the
rocm.builtin.tuning_moduleattribute. Any builtin accessed through this attribute is cached on the dialect (same as the iree_codegen dialect).The same move to attribute based reference will be done for ukernels as a follow up (but with a different interface given that parsing is not needed).