From 286c4dc776f45ffcebbd2214824318f6c8a9fd64 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Mon, 4 Nov 2024 15:45:27 -0800 Subject: [PATCH] ZAM fix for inlining empty function bodies as the sole elements of an if-else --- src/script_opt/Expr.cc | 11 +++++++++-- .../Baseline/opt.null-inline-branch/output | 2 ++ testing/btest/opt/null-inline-branch.zeek | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 testing/btest/Baseline/opt.null-inline-branch/output create mode 100644 testing/btest/opt/null-inline-branch.zeek diff --git a/src/script_opt/Expr.cc b/src/script_opt/Expr.cc index 5a04a3bef1..90e5419a0d 100644 --- a/src/script_opt/Expr.cc +++ b/src/script_opt/Expr.cc @@ -2613,9 +2613,16 @@ ExprPtr InlineExpr::Reduce(Reducer* c, StmtPtr& red_stmt) { body = body->Reduce(c); c->PopInlineBlock(); - auto catch_ret = with_location_of(make_intrusive(sf, body, ret_val), this); + StmtPtr repl_stmt; - red_stmt = MergeStmts(red_stmt, catch_ret); + if ( body->Tag() == STMT_NULL ) + // The inlined body reduced down to nothing, expose that fact + // rather than masking it with an empty catch-return. + repl_stmt = make_intrusive(); + else + repl_stmt = make_intrusive(sf, body, ret_val); + + red_stmt = MergeStmts(red_stmt, with_location_of(repl_stmt, this)); return ret_val ? ret_val->Duplicate() : nullptr; } diff --git a/testing/btest/Baseline/opt.null-inline-branch/output b/testing/btest/Baseline/opt.null-inline-branch/output new file mode 100644 index 0000000000..acd35f11eb --- /dev/null +++ b/testing/btest/Baseline/opt.null-inline-branch/output @@ -0,0 +1,2 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +got through the conditional diff --git a/testing/btest/opt/null-inline-branch.zeek b/testing/btest/opt/null-inline-branch.zeek new file mode 100644 index 0000000000..942b8509ac --- /dev/null +++ b/testing/btest/opt/null-inline-branch.zeek @@ -0,0 +1,19 @@ +# @TEST-DOC: Regression test for past ZAM issues with inlining empty functions in conditionals +# +# @TEST-EXEC: zeek -b -O ZAM %INPUT >output +# @TEST-EXEC: btest-diff output + +function empty_func() {} + +# Use a global to avoid constant propagation optimizing out the conditional. +global bar = F; + +event zeek_init() + { + if ( bar ) + empty_func(); + else + empty_func(); + + print "got through the conditional"; + }