Fix uninitialized variable in float_to_bf16_rtn_asm() causing incorrect rounding under -O3#3715
Open
zhyajie wants to merge 1 commit intoROCm:developfrom
Open
Fix uninitialized variable in float_to_bf16_rtn_asm() causing incorrect rounding under -O3#3715zhyajie wants to merge 1 commit intoROCm:developfrom
zhyajie wants to merge 1 commit intoROCm:developfrom
Conversation
…s under -O3 Initialize `tmp` to 0 in the inline assembly of `float_to_bf16_rtn_asm()`. Without initialization, the compiler under -O3 may alias the `tmp` operand (%1) with the ROUND_BIAS_FOR_BF16 input operand (%3) in the same VGPR, causing v_bfe_u32 to overwrite the 0x7fff bias before v_add3_u32 reads it. This produces incorrect BF16 rounding for ~50% of inputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix uninitialized
tmpvariable infloat_to_bf16_rtn_asm()that causes the compiler to incorrectly alias registers under-O3, producing wrong BF16 conversion results for ~50% of inputs.Problem
float_to_bf16_rtn_asm()inbfloat16.hpp(used whenCK_TILE_FLOAT_TO_BFLOAT16_DEFAULT=3/standard_asm) produces incorrect Round-to-Nearest-Even results when compiled with-O3.Environment: hipcc (AMD clang 19.0.0, ROCm 6.4.3),
-O3, gfx942Root Cause
The inline assembly declares
tmpwith a"+v"(read+write) constraint but never initializes it:Under
-O3, the compiler's register allocator treatstmp(%1) as having an undefined initial value and aggressively reuses registers, assigning%1(tmp) and%3(ROUND_BIAS_FOR_BF16 = 0x7fff) to the same VGPR.-O3 generated assembly (BROKEN):
%1and%3both mapped tov5-O0 generated assembly (CORRECT):
%1→v9,%3→v12The
v_bfe_u32instruction writes to%1, destroying the value of%3when they share the same register. This breaks thev_add3_u32rounding computation, causing ~50% of FP32 to BF16 conversions to be off by 1 ULP.