Skip to content

[asm] Allow RawOp to reserve registers from regalloc#1156

Closed
Hardcode84 wants to merge 4 commits intoiree-org:mainfrom
Hardcode84:rawop-regread-annotations
Closed

[asm] Allow RawOp to reserve registers from regalloc#1156
Hardcode84 wants to merge 4 commits intoiree-org:mainfrom
Hardcode84:rawop-regread-annotations

Conversation

@Hardcode84
Copy link
Copy Markdown
Contributor

RawOp embeds physical register names as opaque assembly text, invisible to SSA-based liveness. When PrecoloredSRegOps are DCE'd (they have [Pure] and no SSA users), the allocator loses track of those registers and may assign them to loop iter_args, corrupting SRDs at runtime.

This PR adds optional sreg_uses/vreg_uses/areg_uses attributes to waveasm.raw so creation sites can declare which physical registers they reference. The linear-scan pass reserves registers from these attributes.

Changes

  • WaveASMOps.td: Add sreg_uses/vreg_uses/areg_uses as OptionalAttr<DenseI64ArrayAttr> to RawOp with a convenience ArrayRef<int64_t> builder (empty = omitted).
  • LinearScanPass.cpp: Walk RawOps and reserve declared registers in the corresponding reservedSGPRs/reservedVGPRs/reservedAGPRs sets before allocation.
  • TranslateFromMLIR.cpp: Annotate RawOps in emitSRDPrologue and emitSRDBaseAdjustment where physical register dependencies exist. Deliberately omit annotations on GFX950 preload-path RawOps where reservation would conflict with S_LOAD_DWORDX2 allocation (documented with comments).
  • TranslateFromLLVMDialect.cpp: Annotate the s_and_b32 SRD word-1 fixup in handleMakeBufferRsrc.
  • AMDGPUHandlers.cpp: Annotate SRD copy/swizzle RawOps in handleFatRawBufferCast.

@Hardcode84 Hardcode84 force-pushed the rawop-regread-annotations branch from d01c315 to 7a488d2 Compare March 20, 2026 10:11
Hardcode84 and others added 4 commits March 20, 2026 19:53
RawOp embeds physical register names as opaque text, invisible to
SSA-based liveness. When PrecoloredSRegOps are DCE'd (they have [Pure]
and no SSA users), the allocator loses track of those registers and may
assign them to loop iter_args, corrupting SRDs at runtime.

Instead of parsing raw assembly text with regex in the allocator, add
sreg_reads/vreg_reads/areg_reads optional DenseI64ArrayAttr attributes
to RawOp so creation sites can declare which physical registers they
read. The linear-scan pass now reserves registers from these attributes
directly.

Made-with: Cursor
Signed-off-by: Ivan Butygin <ivan.butygin@gmail.com>
- Rename sreg_reads/vreg_reads/areg_reads to sreg_uses/vreg_uses/areg_uses
  for generality (covers both reads and writes of physical registers).
- Replace two OpBuilders with a single one taking ArrayRef<int64_t> with
  default = {} for all three register sets, eliminating DenseI64ArrayAttr
  boilerplate and sregAttr lambdas at call sites.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Ivan Butygin <ivan.butygin@gmail.com>
- Dialect roundtrip test: verifies sreg_uses, vreg_uses, areg_uses
  parse and print correctly, including combinations and omission.
- Regalloc test: verifies the linear-scan allocator reserves SGPRs
  declared in sreg_uses, forcing virtual registers to skip those
  positions. Includes baseline (no annotation) for comparison.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Ivan Butygin <ivan.butygin@gmail.com>
Remove the TODO and wire up vreg_uses/areg_uses reservation alongside
the existing sreg_uses handling. All three register-use attributes now
reserve their respective physical registers in the allocator.

Add vreg_uses regalloc test cases alongside existing sreg_uses ones.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Ivan Butygin <ivan.butygin@gmail.com>
@Hardcode84 Hardcode84 force-pushed the rawop-regread-annotations branch from 7a488d2 to 266d44c Compare March 20, 2026 19:01
@Hardcode84
Copy link
Copy Markdown
Contributor Author

Superseded by #1188

@Hardcode84 Hardcode84 closed this Mar 30, 2026
@Hardcode84 Hardcode84 deleted the rawop-regread-annotations branch March 30, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant