feat: add J/K loop index words and nested control flow tests#19
feat: add J/K loop index words and nested control flow tests#19tetsuo-cpp merged 2 commits intocanonfrom
Conversation
tetsuo-cpp
left a comment
There was a problem hiding this comment.
PR #19 Review: Add J/K Loop Index Words and Nested Control Flow Tests
Overview
Clean, focused PR that extends forth.loop_index to support accessing outer loop induction variables via J (depth=1) and K (depth=2), matching standard Forth semantics. Adds a depth attribute to the op, validates nesting depth at parse time, and walks up enclosing scf.for ops during conversion. Comprehensive tests at all three levels (translation, conversion, pipeline).
Code Quality
Well done:
- Minimal diff — changes are focused and avoid unnecessary refactoring
doLoopDepthtracking in the parser correctly validates nesting before emitting the op, with good error messages- The conversion loop walking up
scf.forops is correct and straightforward - Test coverage is thorough: translation tests cover nested IF, IF-in-DO, nested DO with J, triple-nested DO with K, and mixed control flow; conversion tests verify the lowered IR; pipeline tests verify end-to-end through GPU
DefaultValuedAttr<I64Attr, "0">means existingdepth=0(I) cases don't need the attribute printed, preserving backward compatibility in IR
Suggestions
-
Op class change from
Forth_StackOpBasetoForth_Op(ForthOps.td:532): This is becauseForth_StackOpBasedoesn't support extra attributes. Fine for now — if more ops eventually need optional attributes, a parameterized variant of the base class could help. No action needed. -
Consider a negative test for
J/Koutside sufficient nesting: The translation tests exercise the happy path. Anot+FileChecktest verifying the error diagnostic would be a nice addition:\ RUN: not %warpforth-translate --forth-to-mlir %s 2>&1 | FileCheck %s \ CHECK: 'J' requires 2 nested DO/LOOP(s) 0 DO J LOOP -
Pipeline test specificity (
test/Pipeline/nested-control-flow.forth): TheMIDchecks are fairly loose (cf.br,cf.cond_br,gpu.return) — they mostly verify the intermediate doesn't crash rather than checking structure. Pragmatic for a pipeline test, just flagging it.
Potential Issues
None found. The getParentOfType walk is the standard MLIR pattern. Parser validation ensures depth is always valid by the time we reach conversion. DefaultValuedAttr makes this fully backward-compatible.
Verdict
Looks good to merge. The one suggestion worth acting on is adding a negative test for J/K at insufficient nesting depth.
Summary
depthattribute toForth_LoopIndexOpto supportJ(depth=1) andK(depth=2) for accessing outer loop indices in nested DO/LOOPscf.forops to find the target induction variableRelated to #16
Test plan
3 0 DO 4 0 DO J I + LOOP LOOPthrough translate and convert