Conversation
ComputeLegalizer promoted the value of a Let expression or a Bind statement without visiting it, so a conversion from a BF16 buffer bound to a variable survived the pass. The paired storage pass then retyped the buffer to uint16 and the surviving cast became an integer-to-float conversion of the raw bits. Every other handler in the class visits its operands before promoting; Let and Bind now do the same. Tests cover a Bind statement (a typed assignment in script) and a Let expression whose value is a BF16 load cast to float32.
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.
Problem
BF16ComputeLegalizeleaves a BF16 conversion in place when it sits inside the value of aLetexpression or aBindstatement.BF16StorageLegalizethen retypes the BF16 buffer touint16, and the survivingCast("float32", A[i])becomes an integer-to-float conversion of the raw bits. A kernel that writesreads BF16
1/1024as14976.0. The same kernel with the cast written inline at the use site is legalized correctly.Root cause
ComputeLegalizer::VisitExpr_(const LetNode*)andVisitStmt_(const BindNode*)callPromoteToTarget(op->value)on the unvisited value. Every other handler in the class (Cast,Select,Broadcast,Shuffle,BufferStore, the binary operators) callsPromoteToTarget(this->VisitExpr(...)). Nothing in TileLang runs this pass on the CPU pipeline today, and the CUDA gate skips it, so the two handlers had not processed real IR.Change
src/tirx/transform/unsupported_dtype_legalize.cc: visit the bound value before promoting it, in both handlers.Tests
tests/python/tirx-transform/test_tir_transform_bf16_legalize.py:test_bf16_bind_value_will_legalize: a typed assignment in script (aBind) whose value casts a BF16 load; structural equality against the legalized form.test_bf16_let_value_will_legalize: aLetexpression built directly; asserts theLetsurvives and no cast to or frombfloat16remains.Validation
Apple M-series, macOS 15, built inside TileLang (
85fd8fc2) withUSE_METAL=ON USE_LLVM=ON(LLVM 20.1.8).With TileLang's CPU pipeline running
BF16ComputeLegalize(companion TileLang PR),testing/python/cpuplustesting/python/llvm: 120 passed, 0 failed. A downstream BF16 pointwise kernel that binds its casts computes correctly on the LLVM target with this change and reads raw bits without it.Related
[BugFix][CPU] Legalize BF16 arithmetic in the CPU pipelineaddsBF16ComputeLegalizetoCPUPassPipelineBody. It is independent of this fix and can merge in either order: it repairs the compile failures on its own, and this fix repairs the remaining case of a conversion bound to a variable, which TileLang's eager frontend emits for every assignment.3rdparty/tvm, and extendtesting/python/cpu/test_tilelang_cpu_bf16_legalize.pywith the bound-value kernel (a, b = A[i].astype("float32"), B[i].astype("float32")) in both the device-free lowering check and the LLVM execution check. Those cases fail against the current3rdparty/tvmand pass with this change; they were left out of the TileLang PR for that reason.