{"record":{"id":"db53512c3814a48d","repo":"jax-ml/jax","slug":"tmem-addr-ref-must-be-in-shared-memory-got-addr","errorCode":null,"errorMessage":"tmem_addr_ref must be in shared memory, got: {addr_ref_ty}","messagePattern":"tmem_addr_ref must be in shared memory, got: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/mosaic/gpu/tcgen05.py","lineNumber":1223,"sourceCode":"\n  def __post_init__(self):\n    self.layout.check_type(self.shape, utils.bitwidth(self.dtype))\n\n  @classmethod\n  def from_alloc(\n      cls,\n      tmem_addr_ref: ir.Value,\n      shape: tuple[int, int],\n      dtype,\n      collective: bool | None = None,\n      layout: TMEMLayout | None = None,\n  ) -> TMEMRef:\n    i32 = ir.IntegerType.get_signless(32)\n    if not isinstance(tmem_addr_ref.type, ir.MemRefType):\n      raise ValueError(f\"tmem_addr_ref must be a memref or a pointer, got: {tmem_addr_ref.type}\")\n    addr_ref_ty = ir.MemRefType(tmem_addr_ref.type)\n    if not utils.is_smem_ref(addr_ref_ty):\n      raise ValueError(f\"tmem_addr_ref must be in shared memory, got: {addr_ref_ty}\")\n    if addr_ref_ty.element_type != i32:\n      raise ValueError(f\"tmem_addr_ref must be an i32 memref, got: {addr_ref_ty}\")\n    if math.prod(addr_ref_ty.shape) != 1:\n      raise ValueError(f\"tmem_addr_ref must contain a single element, got: {addr_ref_ty}\")\n    i0 = arith.ConstantOp.create_index(0)\n    tmem_addr = memref.load(tmem_addr_ref, [i0] * addr_ref_ty.rank)\n    if shape[0] < 32:\n      raise ValueError(f\"TMEM refs must have at least 32 rows, got: {shape[0]}\")\n    if layout is None:\n      if collective is None:\n        raise ValueError(\n            \"collective argument must be provided when TMEM layout is inferred\"\n        )\n      layout = _infer_tmem_layout(shape, collective, packing=1)\n    # TODO: Do we have to do this??\n    # warp_idx = utils.warp_idx(sync=False)\n    # tmem_addr = arith.ori(tmem_addr, arith.shli(warp_idx, utils.c(21, i32)))\n    return cls(tmem_addr, shape, dtype, layout)","sourceCodeStart":1205,"sourceCodeEnd":1241,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/mosaic/gpu/tcgen05.py#L1205-L1241","documentation":"TMEM allocation addresses are communicated through a single-element i32 memref that must live in shared memory (address space 3) because the tcgen05.alloc result is written by the hardware/CTA into smem. from_alloc checks utils.is_smem_ref and rejects global or generic address-space memrefs.","triggerScenarios":"Passing a global-memory memref or a memref without the shared-memory address space attribute as tmem_addr_ref to from_alloc.","commonSituations":"Creating the address memref manually with default (global) address space instead of via smem helpers; refactoring that stripped the #gpu.address_space<workgroup> attribute.","solutions":["Allocate tmem_addr_ref in shared memory (use the Mosaic smem allocation utilities)","Verify the memref type string contains the workgroup/shared address space attribute","Reproduce the pattern from jax mosaic gpu tests/examples for tmem.alloc"],"exampleFix":"# before\naddr_ref = memref.alloca(..., memref_type_in_global_space)\n# after\naddr_ref = smem_alloca(i32, [])  # shared-memory 0-d memref, then pass to from_alloc","handlingStrategy":"type-guard","validationCode":"from jax._src.experimental.mosaic.gpu import utils\nassert utils.is_smem_ref(ir.MemRefType(tmem_addr_ref.type)), 'need smem address space'","typeGuard":"def addr_ref_ok(ref) -> bool:\n    return (isinstance(ref.type, ir.MemRefType)\n            and utils.is_smem_ref(ir.MemRefType(ref.type)))","tryCatchPattern":null,"preventionTips":["Allocate the address slot with the library's smem helpers, not plain memref.alloca","Memorize the pattern: tmem alloc -> 0-d i32 smem memref -> from_alloc"],"tags":["jax","mosaic","tmem","shared-memory","address-space"],"backgroundTag":"invalid-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}