feat(joinir): Phase 47-A-LOWERING - P3 Normalized→MIR(direct)

Implement full Pattern3 (if-sum) Normalized→MIR(direct) support, completing
the P3 minimal implementation.

Key changes:

1. P3 Shape Detection (shape_guard.rs):
   - Implemented is_pattern3_if_sum_minimal() with structure-based detection
   - Detects P3 characteristics:
     - Has Compare instruction (loop condition)
     - Has Select instruction (conditional carrier update)
     - Has tail call (Call with k_next: None)
     - Reasonable param count (2-4 for i, sum carriers)
   - Handles both JoinInst::Select and Compute(MirLikeInst::Select)
   - Added unit test: test_detect_pattern3_if_sum_minimal_shape 

2. P3 Normalization Function (normalized.rs):
   - Implemented normalize_pattern3_if_sum_minimal()
   - Guards: Validates Structured JoinIR + P3 shape detection
   - Phase 47-A minimal: Delegates to P2 normalization (works for simple cases)
   - Updated normalized_dev_roundtrip_structured() integration
   - Returns Result<NormalizedModule, String> for proper error handling

3. Bridge Integration (bridge.rs):
   - Updated normalize_for_shape() to call P3 normalization
   - No changes needed to direct.rs (already handles P3 instructions)

4. Integration Tests (normalized_joinir_min.rs):
   - Added test_phase47a_pattern3_if_sum_minimal_normalization
     - Tests Structured→Normalized transformation
     - Verifies shape detection + normalization succeeds
     - Validates function count and phase correctness
   - Added test_phase47a_pattern3_if_sum_minimal_runner
     - Basic smoke test for P3 fixture validity
     - Verifies 3-function structure and entry point

Benefits:
- P3 now uses Normalized→MIR(direct) pipeline (same as P1/P2)
- Structure-based detection (no name-based heuristics)
- Minimal implementation (delegates to P2 for simplicity)
- Pure additive (no P1/P2 behavioral changes)

Tests: 938/938 PASS (lib), shape_guard P3 test PASS
- test_detect_pattern3_if_sum_minimal_shape 
- test_phase47a_pattern3_if_sum_minimal_normalization 
- test_phase47a_pattern3_if_sum_minimal_runner 

Next phase: Phase 47-B (proper P3-specific normalization, array_filter, multi-carrier)

Design note: This minimal implementation reuses P2 normalization for simplicity.
Proper P3-specific normalization (IfCond, ThenUpdates, ElseUpdates step sequence)
will be implemented in Phase 47-B when needed for more complex P3 patterns.
This commit is contained in:
nyash-codex
2025-12-12 05:53:23 +09:00
parent c3a26e705e
commit 99bdf93dfe
4 changed files with 146 additions and 13 deletions

View File

@ -528,6 +528,32 @@ pub fn normalize_pattern2_minimal(structured: &JoinModule) -> NormalizedModule {
norm
}
/// Phase 47-A: Normalize Pattern3 if-sum minimal to Normalized JoinIR
#[cfg(feature = "normalized_dev")]
pub fn normalize_pattern3_if_sum_minimal(
structured: &JoinModule,
) -> Result<NormalizedModule, String> {
// Guard: Must be Structured and match Pattern3IfSumMinimal shape
if !structured.is_structured() {
return Err("[normalize_p3] Not structured JoinIR".to_string());
}
// Use shape detection to verify P3 shape
let shapes = shape_guard::supported_shapes(&structured);
if !shapes.contains(&NormalizedDevShape::Pattern3IfSumMinimal) {
return Err("[normalize_p3] Not Pattern3IfSumMinimal shape".to_string());
}
// Phase 47-A minimal: Reuse P2 normalization temporarily
// TODO: Implement proper P3-specific normalization with:
// - EnvLayout for i, sum carriers
// - IfCond → ThenUpdates / ElseUpdates step sequence
// - Proper JpInst::If for conditional carrier updates
// For now, delegate to P2 normalization (works for simple cases)
Ok(normalize_pattern2_minimal(structured))
}
/// Pattern2 専用: Normalized → Structured への簡易逆変換。
pub fn normalized_pattern2_to_structured(norm: &NormalizedModule) -> JoinModule {
if let Some(backup) = norm.to_structured() {
@ -905,9 +931,10 @@ pub(crate) fn normalized_dev_roundtrip_structured(
let norm = normalize_pattern2_minimal(module);
normalized_pattern2_to_structured(&norm)
})),
// Phase 47-A: P3 minimal uses P2 normalization for now
// Phase 47-A: P3 minimal (delegates to P2 for now, but uses proper guard)
NormalizedDevShape::Pattern3IfSumMinimal => catch_unwind(AssertUnwindSafe(|| {
let norm = normalize_pattern2_minimal(module);
let norm = normalize_pattern3_if_sum_minimal(module)
.expect("P3 normalization failed");
normalized_pattern2_to_structured(&norm)
})),
};