Skip to content

Add operator<< to the enum macro family - #5115

Open
CharlieL7 wants to merge 1 commit into
developfrom
enum_string_update
Open

Add operator<< to the enum macro family#5115
CharlieL7 wants to merge 1 commit into
developfrom
enum_string_update

Conversation

@CharlieL7

Copy link
Copy Markdown
Collaborator

Motivation

  • Add operator<<(stream, enum) to the enum macros such that it is automatically included.
  • We need it more whenever you would use MIGRAPHX_NESTED_ENUM(). For example, in an operation.

Technical Details

Generate std::ostream& operator<<(std::ostream&, name) alongside to_string for every MIGRAPHX_ENUM variant.
So a named enum streams as its enumerator name instead of its underlying integer.

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.

Generate std::ostream& operator<<(std::ostream&, name) alongside to_string for
every MIGRAPHX_ENUM variant, so a named enum streams as its enumerator name
instead of its underlying integer. The overload is an exact match, so it wins
over the conversion to the underlying type for unscoped enums, and it is
emitted after to_string so the nested (friend) variant reaches it by ADL.

The namespace-scope variants now take their linkage from
MIGRAPHX_DETAIL_ENUM_INLINE ([[maybe_unused]] inline). These enums belong in an
anonymous namespace in a .cpp, which gives the generated helpers internal
linkage, so any the file never calls would otherwise trip -Wunused-function.

Co-authored-by: Cursor <cursoragent@cursor.com>
@CharlieL7
CharlieL7 requested a review from causten as a code owner August 5, 2026 18:00
Copilot AI lite review requested due to automatic review settings August 5, 2026 18:00
@CharlieL7 CharlieL7 changed the title Add operator<< to the enum macro family Add operator<< to the enum macro family Aug 5, 2026

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 extends the MIGRAPHX_ENUM* macro family to also generate std::ostream& operator<<(std::ostream&, Enum) alongside the existing to_string/from_string support, so reflected enums stream as their enumerator names rather than underlying integers (including nested enums commonly used in ops).

Changes:

  • Generate operator<< for all MIGRAPHX_ENUM variants (unscoped, scoped, nested, nested scoped).
  • Add [[maybe_unused]] to namespace-scope helper linkage to avoid unused-function warnings in internal-linkage contexts.
  • Add unit tests validating streaming behavior (including nested enums, enums in migraphx namespace, and error-on-unknown).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/include/migraphx/enum.hpp Adds macro-generated operator<< (and related helper linkage tweak) to stream enums by name.
test/enum.cpp Adds test coverage for enum streaming and a stream_range integration scenario.

Comment on lines +179 to +182
// Linkage for the namespace-scope variants. These enums belong in an anonymous namespace in a .cpp,
// which gives the helpers internal linkage, so any the file never calls would trip
// -Wunused-function. The nested variants generate hidden friends and need no marking.
#define MIGRAPHX_DETAIL_ENUM_INLINE [[maybe_unused]] inline
@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Batch New Rate (4b5aa7) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 2,184.77 3,264.92 -33.08% 🔴
torchvision-resnet50_fp16 64 7,803.33 7,548.67 3.37%
torchvision-densenet121 32 2,491.05 2,483.99 0.28%
torchvision-densenet121_fp16 32 2,591.72 5,004.24 -48.21% 🔴
torchvision-inceptionv3 32 2,072.90 2,058.51 0.70%
torchvision-inceptionv3_fp16 32 4,487.09 4,416.99 1.59%
cadene-inceptionv4 16 821.20 820.61 0.07%
cadene-resnext64x4 16 782.73 782.78 -0.01%
slim-mobilenet 64 8,388.48 8,386.36 0.03%
slim-nasnetalarge 64 226.91 228.86 -0.85%
slim-resnet50v2 64 3,235.62 3,180.91 1.72%
bert-mrpc-onnx 8 1,167.14 1,168.84 -0.15%
bert-mrpc-tf 1 491.29 498.63 -1.47%
pytorch-examples-wlang-gru 1 478.05 473.35 0.99%
pytorch-examples-wlang-lstm 1 438.38 384.83 13.91% 🔆
torchvision-resnet50_1 1 381.66 1,046.63 -63.53% 🔴
cadene-dpn92_1 1 438.35 437.32 0.24%
cadene-resnext101_1 1 79.89 365.89 -78.17% 🔴
onnx-taau-downsample 1 381.91 844.09 -54.76% 🔴
dlrm-criteoterabyte 1 32.42 32.42 -0.02%
dlrm-criteoterabyte_fp16 1 49.52 51.80 -4.41%
agentmodel 1 9,534.07 9,209.12 3.53%
unet_fp16 2 58.75 58.80 -0.09%
resnet50v1_fp16 1 1,415.01 1,366.11 3.58%
resnet50v1_int8 1 1,706.76 1,883.96 -9.41% 🔴
bert_base_cased_fp16 64 1,097.85 1,098.16 -0.03%
bert_large_uncased_fp16 32 345.66 345.59 0.02%
bert_large_fp16 1 205.24 206.59 -0.65%
distilgpt2_fp16 16 2,090.68 2,092.89 -0.11%
yolov5s 1 559.53 558.33 0.21%
tinyllama 1 45.79 45.83 -0.10%
vicuna-fastchat 1 44.10 44.20 -0.22%
whisper-tiny-encoder 1 412.04 411.87 0.04%
whisper-tiny-decoder 1 405.14 408.48 -0.82%
llama2_7b 1 20.82 20.84 -0.10%
qwen1.5-7b 1 23.58 23.58 -0.01%
phi3-3.8b 1 26.72 26.72 0.03%
llama3-8b 1 21.75 21.80 -0.26%
whisper-large-encoder 1 10.17 10.18 -0.05%
whisper-large-decoder 1 107.76 105.30 2.33%
mistral-7b 1 23.76 23.78 -0.08%
FLUX.1-schnell 1 771.02 755.22 2.09%

Regressions detected 🔴

* No develop baseline was found for this PR's branch point; compared against the latest available develop run instead.

@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Status Result
bert-mrpc-onnx PASSED: MIGraphX meets tolerance
bert-mrpc-tf PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-gru PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-lstm PASSED: MIGraphX meets tolerance
dlrm-criteoterabyte PASSED: MIGraphX meets tolerance
agentmodel PASSED: MIGraphX meets tolerance
unet PASSED: MIGraphX meets tolerance
resnet50v1 PASSED: MIGraphX meets tolerance
bert_base_cased_fp16 PASSED: MIGraphX meets tolerance
bert_large_uncased_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
bert_large PASSED: MIGraphX meets tolerance
yolov5s PASSED: MIGraphX meets tolerance
tinyllama PASSED: MIGraphX meets tolerance
vicuna-fastchat PASSED: MIGraphX meets tolerance
whisper-tiny-encoder PASSED: MIGraphX meets tolerance
whisper-tiny-decoder PASSED: MIGraphX meets tolerance
distilgpt2_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
llama2_7b PASSED: MIGraphX meets tolerance
qwen1.5-7b PASSED: MIGraphX meets tolerance
phi3-3.8b PASSED: MIGraphX meets tolerance
llama3-8b PASSED: MIGraphX meets tolerance
whisper-large-decoder PASSED: MIGraphX meets tolerance
mistral-7b PASSED: MIGraphX meets tolerance
FLUX.1-schnell PASSED: MIGraphX meets tolerance

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