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:
@ -65,6 +65,7 @@ impl IfLoweringDryRunner {
|
||||
func,
|
||||
*block_id,
|
||||
self.debug_level >= 3,
|
||||
None, // Phase 61-1: Pure If(dry-runは常にPure If)
|
||||
) {
|
||||
Some(join_inst) => {
|
||||
lowered_count += 1;
|
||||
|
||||
@ -18,8 +18,13 @@ use crate::mir::join_ir::{JoinInst, MergePair};
|
||||
use crate::mir::{BasicBlockId, MirFunction, MirInstruction, ValueId};
|
||||
use std::collections::HashSet;
|
||||
|
||||
// Phase 61-1: If-in-loop context support
|
||||
use super::if_phi_context::IfPhiContext;
|
||||
|
||||
pub struct IfMergeLowerer {
|
||||
debug_level: u8,
|
||||
// Phase 61-1: If-in-loop context (None = Pure If)
|
||||
context: Option<IfPhiContext>,
|
||||
}
|
||||
|
||||
/// 検出された IfMerge パターン情報
|
||||
@ -39,13 +44,37 @@ struct IfBranch {
|
||||
|
||||
impl IfMergeLowerer {
|
||||
pub fn new(debug_level: u8) -> Self {
|
||||
Self { debug_level }
|
||||
Self {
|
||||
debug_level,
|
||||
context: None, // Phase 61-1: デフォルトは Pure If
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 33-8: debug-level backward compat wrapper
|
||||
pub fn with_debug(debug: bool) -> Self {
|
||||
Self {
|
||||
debug_level: if debug { 1 } else { 0 },
|
||||
context: None, // Phase 61-1: デフォルトは Pure If
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 61-1: If-in-loop 用コンストラクタ
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `debug_level` - デバッグログレベル (0-3)
|
||||
/// * `context` - If-in-loop コンテキスト(carrier_names 情報を含む)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
/// let lowerer = IfMergeLowerer::with_context(debug_level, context);
|
||||
/// ```
|
||||
pub fn with_context(debug_level: u8, context: IfPhiContext) -> Self {
|
||||
Self {
|
||||
debug_level,
|
||||
context: Some(context),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@ -13,8 +13,13 @@
|
||||
use crate::mir::join_ir::JoinInst;
|
||||
use crate::mir::{BasicBlockId, MirFunction, MirInstruction, ValueId};
|
||||
|
||||
// Phase 61-1: If-in-loop context support
|
||||
use super::if_phi_context::IfPhiContext;
|
||||
|
||||
pub struct IfSelectLowerer {
|
||||
debug_level: u8,
|
||||
// Phase 61-1: If-in-loop context (None = Pure If)
|
||||
context: Option<IfPhiContext>,
|
||||
}
|
||||
|
||||
/// If/Else パターンの分類
|
||||
@ -46,13 +51,37 @@ struct IfBranch {
|
||||
|
||||
impl IfSelectLowerer {
|
||||
pub fn new(debug_level: u8) -> Self {
|
||||
Self { debug_level }
|
||||
Self {
|
||||
debug_level,
|
||||
context: None, // Phase 61-1: デフォルトは Pure If
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 33-8: debug-level backward compat wrapper
|
||||
pub fn with_debug(debug: bool) -> Self {
|
||||
Self {
|
||||
debug_level: if debug { 1 } else { 0 },
|
||||
context: None, // Phase 61-1: デフォルトは Pure If
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 61-1: If-in-loop 用コンストラクタ
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `debug_level` - デバッグログレベル (0-3)
|
||||
/// * `context` - If-in-loop コンテキスト(carrier_names 情報を含む)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let context = IfPhiContext::for_loop_body(carrier_names);
|
||||
/// let lowerer = IfSelectLowerer::with_context(debug_level, context);
|
||||
/// ```
|
||||
pub fn with_context(debug_level: u8, context: IfPhiContext) -> Self {
|
||||
Self {
|
||||
debug_level,
|
||||
context: Some(context),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ pub mod funcscanner_trim;
|
||||
pub mod generic_case_a;
|
||||
pub mod if_dry_runner; // Phase 33-10.0
|
||||
pub mod if_merge; // Phase 33-7
|
||||
pub mod if_phi_context; // Phase 61-1
|
||||
pub mod if_select; // Phase 33
|
||||
pub mod loop_form_intake;
|
||||
pub mod loop_scope_shape;
|
||||
@ -92,11 +93,16 @@ pub(crate) fn is_loop_lowered_function(name: &str) -> bool {
|
||||
/// - 1 variable → Select
|
||||
/// - 2+ variables → IfMerge
|
||||
///
|
||||
/// Phase 61-1: If-in-loop support
|
||||
/// - `context` parameter: If-in-loop context (carrier_names for loop variables)
|
||||
/// - None = Pure If, Some(_) = If-in-loop
|
||||
///
|
||||
/// Returns Some(JoinInst::Select) or Some(JoinInst::IfMerge) if pattern matched, None otherwise.
|
||||
pub fn try_lower_if_to_joinir(
|
||||
func: &MirFunction,
|
||||
block_id: BasicBlockId,
|
||||
debug: bool,
|
||||
context: Option<&if_phi_context::IfPhiContext>, // Phase 61-1: If-in-loop context
|
||||
) -> Option<JoinInst> {
|
||||
// 1. dev トグルチェック
|
||||
if !crate::config::env::joinir_if_select_enabled() {
|
||||
@ -151,7 +157,12 @@ pub fn try_lower_if_to_joinir(
|
||||
|
||||
// 3. Phase 33-7: IfMerge を優先的に試行(複数変数パターン)
|
||||
// IfMerge が成功すればそれを返す、失敗したら Select を試行
|
||||
let if_merge_lowerer = if_merge::IfMergeLowerer::new(debug_level);
|
||||
// Phase 61-1: context がある場合は with_context() を使用
|
||||
let if_merge_lowerer = if let Some(ctx) = context {
|
||||
if_merge::IfMergeLowerer::with_context(debug_level, ctx.clone())
|
||||
} else {
|
||||
if_merge::IfMergeLowerer::new(debug_level)
|
||||
};
|
||||
|
||||
if if_merge_lowerer.can_lower_to_if_merge(func, block_id) {
|
||||
if let Some(result) = if_merge_lowerer.lower_if_to_if_merge(func, block_id) {
|
||||
@ -166,7 +177,12 @@ pub fn try_lower_if_to_joinir(
|
||||
}
|
||||
|
||||
// 4. IfMerge が失敗したら Select を試行(単一変数パターン)
|
||||
let if_select_lowerer = if_select::IfSelectLowerer::new(debug_level);
|
||||
// Phase 61-1: context がある場合は with_context() を使用
|
||||
let if_select_lowerer = if let Some(ctx) = context {
|
||||
if_select::IfSelectLowerer::with_context(debug_level, ctx.clone())
|
||||
} else {
|
||||
if_select::IfSelectLowerer::new(debug_level)
|
||||
};
|
||||
|
||||
if !if_select_lowerer.can_lower_to_select(func, block_id) {
|
||||
if debug_level >= 1 {
|
||||
|
||||
Reference in New Issue
Block a user