From 8a285f2ed3c2126b588396f502b64273920281c9 Mon Sep 17 00:00:00 2001 From: Yuan Feng Date: Sun, 15 Jun 2025 12:43:29 -0700 Subject: [PATCH] add support of neura.phi in interpreter --- test/neura/interpreter/phiOp_interpret.mlir | 20 +++++++++++++++++++ tools/neura-interpreter/neura-interpreter.cpp | 11 ++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/neura/interpreter/phiOp_interpret.mlir diff --git a/test/neura/interpreter/phiOp_interpret.mlir b/test/neura/interpreter/phiOp_interpret.mlir new file mode 100644 index 00000000..b6f50513 --- /dev/null +++ b/test/neura/interpreter/phiOp_interpret.mlir @@ -0,0 +1,20 @@ +// RUN: neura-interpreter %s | FileCheck %s + +module { + func.func @test_phi() -> f32 { + %true_path_val = "neura.constant"() <{ + value = 10.0 : f32, + predicate = true + }> : () -> f32 + + %false_path_val = "neura.constant"() <{ + value = 20.0 : f32, + predicate = false + }> : () -> f32 + + %result = "neura.phi"(%true_path_val, %false_path_val) : (f32, f32) -> f32 + + return %result : f32 + // CHECK: [neura-interpreter] Output: 10.000000 + } +} \ No newline at end of file diff --git a/tools/neura-interpreter/neura-interpreter.cpp b/tools/neura-interpreter/neura-interpreter.cpp index 53cf0eb8..383782d9 100644 --- a/tools/neura-interpreter/neura-interpreter.cpp +++ b/tools/neura-interpreter/neura-interpreter.cpp @@ -95,6 +95,17 @@ int main(int argc, char **argv) { valueMap[constOp.getResult()] = val; + } else if (auto phiOp = dyn_cast(op)) { + PredicatedData result{0.0f, false}; // Default to a false predicate. + // Find the one operand with a true predicate. + for (Value operand : phiOp.getOperands()) { + auto incoming = valueMap[operand]; + if (incoming.predicate) { + result = incoming; + break; // Found the active value. + } + } + valueMap[phiOp.getResult()] = result; } else if (auto movOp = dyn_cast(op)) { valueMap[movOp.getResult()] = valueMap[movOp.getOperand()];