[BugFix][Metal] Preserve pointer address spaces for byte offsets - #20101
Open
GY-Bai wants to merge 1 commit into
Open
[BugFix][Metal] Preserve pointer address spaces for byte offsets#20101GY-Bai wants to merge 1 commit into
GY-Bai wants to merge 1 commit into
Conversation
tqchen
approved these changes
Aug 6, 2026
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
A Metal kernel that derives typed and untyped aliases from shared memory can reach the MSL compiler with pointer declarations and byte-offset casts that have no address-space qualifier.
Metal rejects those generic pointers before kernel execution with a diagnostic such as:
The source allocation is
threadgroup, but the alias declarations, result casts, and intermediatechar*casts do not carry that address space.Root cause
The TIR result types of
tirx.ptr_byte_offsetandtirx.handle_add_byte_offsetretain the pointer storage scope.CodeGenMetalpreviously inherited the genericCodeGenChandling for both the pointer-valuedBindnodes and these byte-offset operations. That path emits ordinary C pointer casts and does not express Metal address spaces.The loss occurs at the TIR-to-MSL codegen boundary, before Metal runtime compilation or GPU execution.
What changed
Handle pointer-valued
Bindnodes inCodeGenMetaland emit the storage scope recorded in theirPointerType.Lower typed and untyped byte-offset operations with the same address space on both the result pointer and the intermediate
char*cast.Keep the existing generic C behavior for pointers without an explicit storage scope.
The corrected source now has the equivalent form:
threadgroup half* typed_alias = (threadgroup half*)((threadgroup half*)(((threadgroup char*)shared) + 4)); threadgroup void* void_alias = (threadgroup void*)((threadgroup void*)(((threadgroup char*)shared) + 8));Validation
Added a source-generation regression covering both typed and untyped aliases derived from a
float16shared allocation.Added an end-to-end Metal test using the standard compilation pipeline. The generated source contains the expected
threadgroupallocation, typed alias, void alias, and byte-offset casts.Ran the end-to-end test on a MacBook Air (
Mac14,2) with an Apple M2 GPU, macOS 15.6.1 (24G90), Metal 3, Command Line Tools, and the macOS 15.5 SDK. TVM compiled MSL 2.3 through the Metal runtime without the offlinemetalormetallibtools. The kernel returned[10.0, 11.0, 12.0], matching the expected output.The focused tests pass:
Scope
This change only affects Metal pointer code generation when the TIR pointer type carries an explicit storage scope.
The issue is independent of the pointee dtype: the source-generation regression uses
float16, while the end-to-end Metal test exercises the same path withfloat32.It does not change compilation pipelines, Metal runtime implementation, public APIs, or serialized formats.
Hardware validation is limited to the Apple M2 configuration above.