Skip to content

Add promote_storage_type pass to compute storage-only types in float - #5138

Open
pfultz2 wants to merge 11 commits into
developfrom
promote-storage-type
Open

Add promote_storage_type pass to compute storage-only types in float#5138
pfultz2 wants to merge 11 commits into
developfrom
promote-storage-type

Conversation

@pfultz2

@pfultz2 pfultz2 commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Motivation

On the GPU, bf16 does not support any VALU computations. It only supports matrix ops. So the compiler does an implicit conversion to float and then back to bf16 every time making it much slower. This adds a pass to do this directly in the graph, so we dont generate all these intermediate conversions.

Constant propagation also needs to cooperate: folding a widening convert
would materialize the literal in the wider type, enlarging it and losing the
narrow storage type, so those converts must stay as runtime conversions.

Technical Details

  • Add a promote_storage_type pass that takes a list of storage-only types.
    For every pointwise or reduction instruction of such a type (detected via
    the pointwise/reduce attributes, excluding convert, bit_cast,
    layout, and identity, which carry the pointwise attribute but perform no
    computation), it converts the storage-typed inputs to float and converts the
    result back to the storage type. It then runs eliminate_convert, which
    cancels the storage/float convert pairs between adjacent promoted
    instructions, leaving the intermediate values in float.
  • Generalize quantize_module from truncate_float.cpp into a reusable
    replace_data_type(module, src_types, target_type, predicate) template in
    include/migraphx/replace_data_type.hpp. Both truncate_float_pass and
    promote_storage_type are now thin wrappers over it, so the tuple-output
    and module-input handling is shared instead of duplicated.
  • Update propagate_constant to skip folding converts to a wider type
    (mirroring the existing dequantizelinear handling), keeping literals in
    the narrow storage type with a runtime conversion. Narrowing converts still
    fold.
  • Tests: unit tests for the new pass (pointwise, reduce, adjacent
    pointwise+reduce with no intermediate converts, where keeping its bool
    condition, unlisted types unchanged, idempotency) and for the
    propagate_constant change (widening convert of a broadcast literal is
    kept, narrowing convert still folds). The literal_add quantization test is
    updated for the new semantics: quantize_fp16 with {"add"} const-folds
    the add in fp32 before truncation, so nothing remains to quantize and the
    folded literal stays float, while the hand-built half program now keeps its
    half literal with a runtime widening convert.

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

Follow the LLVM AI Tool Use Policy for contributions using AI.

Copilot AI lite review requested due to automatic review settings August 15, 2026 16:29
@pfultz2
pfultz2 requested a review from causten as a code owner August 15, 2026 16:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves GPU performance for storage-only data types (notably bf16) by introducing a compiler pass that promotes eligible computations to run in float, reducing repeated implicit bf16↔float conversions and preserving narrow storage where appropriate during constant folding.

Changes:

  • Added promote_storage_type pass to insert storage→float→storage conversions around pointwise/reduction computations and then cancel adjacent convert pairs via eliminate_convert.
  • Refactored truncation/quantization type-rewrite logic into a reusable replace_data_type(...) helper template and rewired truncate_float_pass to use it.
  • Updated constant propagation to avoid folding widening convert ops (to preserve narrow storage literals), and added/updated unit tests to match the new semantics.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/quantization.cpp Updates literal_add expectations to reflect fp32 const-folding behavior before truncation/quantization.
test/propagate_constant_test.cpp Adds regression coverage for skipping widening convert folding and still folding narrowing converts.
test/promote_storage_type_test.cpp Adds unit tests for the new promote_storage_type pass (pointwise, reduce, adjacency, idempotency, where-condition handling).
src/truncate_float.cpp Replaces the local quantization rewrite helper with the new shared replace_data_type(...) utility.
src/propagate_constant.cpp Prevents constant folding of widening convert to keep narrow storage literals and avoid literal size/type expansion.
src/promote_storage_type.cpp Implements the promote_storage_type module pass and runs eliminate_convert to remove redundant convert pairs.
src/include/migraphx/replace_data_type.hpp Introduces the shared type-rewrite helper template used by both truncation and promotion.
src/include/migraphx/promote_storage_type.hpp Declares the new promote_storage_type pass API.
src/CMakeLists.txt Adds promote_storage_type.cpp to the core library build.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5138   +/-   ##
========================================
  Coverage    93.13%   93.13%           
========================================
  Files          625      628    +3     
  Lines        33252    33270   +18     
========================================
+ Hits         30967    30985   +18     
  Misses        2285     2285           
Files with missing lines Coverage Δ
src/include/migraphx/promote_storage_type.hpp 100.00% <100.00%> (ø)
src/include/migraphx/replace_data_type.hpp 100.00% <100.00%> (ø)
src/promote_storage_type.cpp 100.00% <100.00%> (ø)
src/propagate_constant.cpp 84.48% <100.00%> (+0.55%) ⬆️
src/truncate_float.cpp 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdevorem bdevorem 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

@TedThemistokleous

Copy link
Copy Markdown
Collaborator

Add a changelog entry for this

@pfultz2
pfultz2 requested a review from a team as a code owner August 20, 2026 17:01
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.

4 participants