feat(joinir): Phase 61-1 If-in-loop JoinIR化インフラ整備完了
## 実装内容 ### 新規ファイル - `if_phi_context.rs`: If-in-loop用PHIコンテキスト構造体 (135行) - `IfPhiContext::for_loop_body()`: ループ内if用コンストラクタ - `is_carrier()`: ループキャリア変数判定 - 単体テスト2個完全動作 ### 既存ファイル拡張 - `if_select.rs`, `if_merge.rs`: context パラメータ追加 (+68行) - `with_context()` コンストラクタ実装 - Pure If との完全互換性維持 - `mod.rs`: `try_lower_if_to_joinir()` シグネチャ拡張 (+25行) - `context: Option<&IfPhiContext>` パラメータ追加 - 既存呼び出し箇所6箇所修正完了 - `loop_builder.rs`: JoinIR経路実装 (+43行) - `NYASH_JOINIR_IF_SELECT=1` で試行 - フォールバック設計(PhiBuilderBox経路保持) - デバッグログ完備 ## テスト結果 - ✅ loopform テスト 14/14 PASS(退行なし) - ✅ ビルド成功(エラー0件) - ✅ Borrow Checker 問題解決 ## コード変更量 - 新規: +135行 - 拡張: +136行 - 削除: -18行 - 純増: +253行(インフラ投資、Phase 61-3で-226行削減予定) ## 次のステップ Phase 61-2: join_inst dry-run実装で実際のPHI生成を行う 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
132
src/mir/join_ir/lowering/if_phi_context.rs
Normal file
132
src/mir/join_ir/lowering/if_phi_context.rs
Normal file
@ -0,0 +1,132 @@
|
||||
//! Phase 61-1: If-in-loop 用 PHI コンテキスト
|
||||
//!
|
||||
//! loop_builder.rs から carrier_names 情報を JoinIR に渡すための構造体
|
||||
//!
|
||||
//! ## 背景
|
||||
//!
|
||||
//! ループ内の if では、ループキャリア変数に対して「片腕 PHI」が必要になる。
|
||||
//! 従来は PhiBuilderBox::set_if_context() で carrier_names を渡していたが、
|
||||
//! Phase 61-1 で JoinIR 経路に統一するため、この情報を渡す手段が必要。
|
||||
//!
|
||||
//! ## 設計
|
||||
//!
|
||||
//! ```
|
||||
//! // loop_builder.rs
|
||||
//! let carrier_names = ...;
|
||||
//! let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
//! try_lower_if_to_joinir(func, block_id, debug, Some(&context));
|
||||
//! ```
|
||||
//!
|
||||
//! ## 責務
|
||||
//!
|
||||
//! - ループ内 if のコンテキスト情報を保持
|
||||
//! - carrier_names の判定ユーティリティ提供
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
/// If-in-loop 用 PHI コンテキスト
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfPhiContext {
|
||||
/// ループ内の if かどうか
|
||||
///
|
||||
/// true の場合、carrier_names に含まれる変数は「片腕 PHI」が必要
|
||||
pub in_loop_body: bool,
|
||||
|
||||
/// ループキャリア変数名リスト
|
||||
///
|
||||
/// ループ内 if で片腕 PHI が必要な変数を指定する。
|
||||
/// 例: `loop(i < 3) { if cond { x = 1 } }` の場合、
|
||||
/// x は carrier 変数として扱われ、else 側で pre_if 値を使用する PHI を生成する。
|
||||
pub carrier_names: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl IfPhiContext {
|
||||
/// ループ内 if 用コンテキスト作成
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `carrier_names` - ループキャリア変数名リスト(BTreeSet で決定的イテレーション保証)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let carrier_names = pre_if_var_map
|
||||
/// .keys()
|
||||
/// .filter(|name| !name.starts_with("__pin$"))
|
||||
/// .cloned()
|
||||
/// .collect();
|
||||
///
|
||||
/// let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
/// ```
|
||||
pub fn for_loop_body(carrier_names: BTreeSet<String>) -> Self {
|
||||
Self {
|
||||
in_loop_body: true,
|
||||
carrier_names,
|
||||
}
|
||||
}
|
||||
|
||||
/// 指定された変数がループキャリアかどうか判定
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `var_name` - 判定対象の変数名
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// - `true`: ループキャリア変数(片腕 PHI が必要)
|
||||
/// - `false`: 通常変数(純粋な if PHI)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// if context.is_carrier("x") {
|
||||
/// // 片腕 PHI 生成: phi [then_val, pre_if_val]
|
||||
/// } else {
|
||||
/// // 純粋 if PHI: phi [then_val, else_val]
|
||||
/// }
|
||||
/// ```
|
||||
pub fn is_carrier(&self, var_name: &str) -> bool {
|
||||
self.carrier_names.contains(var_name)
|
||||
}
|
||||
|
||||
/// ループキャリア変数の数を取得
|
||||
pub fn carrier_count(&self) -> usize {
|
||||
self.carrier_names.len()
|
||||
}
|
||||
|
||||
/// ループ内 if かどうか判定
|
||||
pub fn is_in_loop(&self) -> bool {
|
||||
self.in_loop_body
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_for_loop_body() {
|
||||
let mut carrier_names = BTreeSet::new();
|
||||
carrier_names.insert("x".to_string());
|
||||
carrier_names.insert("y".to_string());
|
||||
|
||||
let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
|
||||
assert!(context.is_in_loop());
|
||||
assert!(context.is_carrier("x"));
|
||||
assert!(context.is_carrier("y"));
|
||||
assert!(!context.is_carrier("z"));
|
||||
assert_eq!(context.carrier_count(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_carrier() {
|
||||
let mut carrier_names = BTreeSet::new();
|
||||
carrier_names.insert("loop_var".to_string());
|
||||
|
||||
let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
|
||||
assert!(context.is_carrier("loop_var"));
|
||||
assert!(!context.is_carrier("local_var"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user