# Design: go dialect ops for GC pointers (/docs/experiments/backend/design-gcptr-ops)

## Problem [#problem]

With MARMOT_GCPTR=1 the front end gives every Go pointer value address space 1 (!llvm.ptr&lt;1>), runtime call signatures included. The statepoint lowering then finds GC pointers by address space. The go dialect ops still require address space 0. About 17 operands and results in GoOps.td are LLVM_DefaultPointer. The string, slice and interface type predicates compare against structs of address space 0 pointers. Inferred result types (type_assert, map_lookup, chan_recv) build address space 0 types. The lowering (GoToLLVM.cpp) creates its runtime calls, GEPs and null values with one address space 0 pointer type. -release builds of jsondec fail to verify. The runtime measured why this matters: dead pointer locals kept by the conservative stack scan make jsondec's peak RSS 2.16x go's.

## Change [#change]

1. GoOps.td: LLVM_DefaultPointer becomes LLVM_AnyPointer for every operand and result that holds a Go value. These are new, make_closure, box, nil_check, index, slice, make_iface data, defer and go fn, make_map, map ops, make_chan and chan ops. Verifiers check that the pointers of one op agree.
2. GoDialect.cpp: ifaceType, stringType and sliceType take an address space (default 0). IsIfaceType, isStringType and isSliceType accept 0 and 1 with the same layout. Inferred results follow the address space of the op's pointer operand.
3. GoToLLVM.cpp: the pattern's ptrTy becomes the Go value pointer type of the module. That is address space 1 in a GCPTR module, else 0. The front end marks a GCPTR module with the module attribute marmot.gcptr. Runtime functions the lowering declares use it. Addresses that are not Go values (addressof of descriptors and itabs, allocas) stay in address space 0. They get an addrspacecast where they become a Go value: stored in an interface, or passed to the runtime as a Go pointer.
4. No casts back to address space 0 around safe points. A GC pointer held only as an address space 0 value across a call would be missing from the stack map.

## Textual syntax [#textual-syntax]

The ops print no pointer types today (go.new @T, go.nil_check %p): the parser knows they are !llvm.ptr. With LLVM_AnyPointer the type must come from the text. So each such operand and result gets a custom directive: an optional `: !llvm.ptr<1>` after the op. Address space 0 has none. The text the frontend emits today stays valid and prints the same, and the GCPTR text adds the type. The tier0 MLIR reader parses the same text and needs the same optional type.

## Scope found in the code [#scope-found-in-the-code]

- 17 operands and results in GoOps.td use LLVM_DefaultPointer. None of the ops are built from C++ (no ::create calls), and the tier0 reader does not parse go ops. So only the text form and the lowering change.
- One custom directive, custom&lt;GcSpace>(type($a), ...), parses an optional `gc` keyword and gives every listed pointer type address space 1 (else 0). The printer writes `gc` only for address space 1, so today's text is unchanged.
- new, index and box infer their result type today. With the directive they take it from the text, and their verifier checks that all pointers of the op agree. make_slice and slice infer a slice struct: the struct takes the address space of the directive. type_assert infers from its interface operand's address space.
- The string, slice and interface predicates accept both layouts.
- More ops than the 17 pointer sites carry implicit types. The interface, string and slice types are buildable constraints with address space 0. These ops are make_iface, iface_call, type_assert, panic, recover, str_concat, str_to_bytes and bytes_to_str. About 25 ops get the `gc` form. The directive takes every implicit type of the op (variadic ones included).

## Properties [#properties]

- Without MARMOT_GCPTR nothing changes. ptrTy is address space 0 and the types are the same, so objects are byte-identical (checked with cmp on the perfcmp programs).
- With it, -release and -O 2 builds verify. The stack maps then cover pointer locals in every function the statepoint lowering handles.

## Tests [#tests]

- Dialect tests with ptr&lt;1> operands for each family, and an address space mismatch that the verifier rejects.
- jsondec -release with MARMOT_GCPTR=1 builds and runs. the runtime's live object count after runtime.GC() in the loop, against go (713) and today (272,851).
- conform O2 and release with MARMOT_GCPTR=1, gcstress included.

## Cost and risk [#cost-and-risk]

About a day and a half for the dialect (about 25 ops) and the lowering. More if the statepoint pass itself needs work for the new values (aggregates, see the parked aggregate design). Risk: missing a cast where a static address becomes a Go value (a crash under gcstress shows it). Performance of statepoint code is measured separately by perfcmp before any default changes.