Skip to content

[Codegen] Use attributes to define default tuning specs - #20979

Merged
qedawkins merged 9 commits into
iree-org:mainfrom
qedawkins:default_tuning_spec_storage_attr
Jun 3, 2025
Merged

qedawkins merged 9 commits into
iree-org:mainfrom
qedawkins:default_tuning_spec_storage_attr

Conversation

@qedawkins

Copy link
Copy Markdown
Contributor

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.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).
Comment on lines +30 to +50
/// 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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kuhar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@qedawkins

Copy link
Copy Markdown
Contributor Author

Is this dropping support for sku-specific tuning specs?

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.

@qedawkins
qedawkins requested a review from kuhar June 3, 2025 11:51

@kuhar kuhar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qedawkins
qedawkins enabled auto-merge (squash) June 3, 2025 19:25
@qedawkins
qedawkins merged commit 54fb605 into iree-org:main Jun 3, 2025
@qedawkins
qedawkins deleted the default_tuning_spec_storage_attr branch June 3, 2025 19:57
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants