// SPDX-License-Identifier: BSD-3-Clause // Copyright (c) 2026, The OpenROAD Authors #include #include #include "flow/constant_fold.h" #include "gtest/gtest.h" #include "syn/ir/Bundle.h" #include "syn/ir/Const.h" #include "syn/ir/ControlNet.h" #include "syn/ir/Graph.h" #include "syn/ir/Instance.h" #include "syn/ir/Net.h" #include "syn/ir/TritModel.h" // ABC (pulled in transitively via //src/syn/src/ir → TritModel) ships its // own main() that wins over gtest_main. Provide our own to force the gtest // entry point. int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } namespace syn { TEST(OptTest, AndZero) { Graph g; Bundle a = g.add("a", 1); Bundle z = Bundle::zero(1); Bundle c = g.add(a, z); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, AndOne) { Graph g; Bundle a = g.add("a", 1); Bundle one = Bundle::ones(1); Bundle c = g.add(a, one); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, OrOne) { Graph g; Bundle a = g.add("a", 1); Bundle one = Bundle::ones(1); Bundle c = g.add(a, one); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, OrZero) { Graph g; Bundle a = g.add("a", 1); Bundle z = Bundle::zero(1); Bundle c = g.add(a, z); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, MuxConstSel) { Graph g; Bundle a = g.add("a", 1); Bundle b = g.add("b", 1); Bundle m = g.add(Net::one(), a, b); g.add("out", m); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, MuxSameArms) { Graph g; Bundle a = g.add("a", 1); Bundle sel = g.add("sel", 1); Bundle m = g.add(sel[0], a, a); g.add("out", m); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, NotConst) { Graph g; Bundle z = Bundle::zero(1); Bundle n = g.add(z); g.add("out", n); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, XorZero) { Graph g; Bundle a = g.add("a", 1); Bundle z = Bundle::zero(1); Bundle c = g.add(a, z); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, XorSelf) { Graph g; Bundle a = g.add("a", 1); Bundle c = g.add(a, a); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); } TEST(OptTest, Chained) { // and(1, or(0, input)) should fold to just input. Graph g; Bundle a = g.add("a", 1); Bundle z = Bundle::zero(1); Bundle one = Bundle::ones(1); Bundle o = g.add(z, a); Bundle c = g.add(one, o); g.add("out", c); constantFold(g); g.normalize(); g.assertNone(); g.assertNone(); } TEST(OptTest, EqConst) { // eq(0101, 0101) = 1 std::istringstream is( "%3:4 = input \"a\"\n" "%7:1 = eq 0101 0101\n" "%8:0 = output \"out\" %7\n"); std::unique_ptr g = Graph::parse(is); constantFold(*g); g->normalize(); g->assertNone(); } TEST(OptTest, PreservesDff) { Graph g; Bundle a = g.add("a", 4); Bundle clk = g.add("clk", 1); Bundle output = g.add(a, ControlNet::pos(clk[0]), ControlNet::zero(), ControlNet::zero(), ControlNet::zero(), Const::zero(4), Const::zero(4), Const::zero(4)); g.add("q", output); constantFold(g); g.findOne(); } } // namespace syn