From 6e94ecb8b5da29e3a07d0117d27bb5fa58271678 Mon Sep 17 00:00:00 2001 From: Albert Magyar Date: Thu, 6 Feb 2020 04:05:16 -0800 Subject: Constant prop binary PrimOps with matching arguments * Add SimplifyBinaryOp trait * Add extra functionality to comparison folding * Add tests * Fix comments from review --- .../firrtlTests/ConstantPropagationTests.scala | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/test') diff --git a/src/test/scala/firrtlTests/ConstantPropagationTests.scala b/src/test/scala/firrtlTests/ConstantPropagationTests.scala index ef52507f..cc7a5e32 100644 --- a/src/test/scala/firrtlTests/ConstantPropagationTests.scala +++ b/src/test/scala/firrtlTests/ConstantPropagationTests.scala @@ -1393,6 +1393,50 @@ class ConstantPropagationIntegrationSpec extends LowTransformSpec { """.stripMargin execute(input, check, Seq.empty) } + + private def matchingArgs(op: String, iType: String, oType: String, result: String): Unit = { + val input = + s"""circuit Top : + | module Top : + | input i : ${iType} + | output o : ${oType} + | o <= ${op}(i, i) + """.stripMargin + val check = + s"""circuit Top : + | module Top : + | input i : ${iType} + | output o : ${oType} + | o <= ${result} + """.stripMargin + execute(input, check, Seq.empty) + } + + it should "optimize some binary operations when arguments match" in { + // Signedness matters + matchingArgs("sub", "UInt<8>", "UInt<8>", """ UInt<8>("h0") """ ) + matchingArgs("sub", "SInt<8>", "SInt<8>", """ SInt<8>("h0") """ ) + matchingArgs("div", "UInt<8>", "UInt<8>", """ UInt<8>("h1") """ ) + matchingArgs("div", "SInt<8>", "SInt<8>", """ SInt<8>("h1") """ ) + matchingArgs("rem", "UInt<8>", "UInt<8>", """ UInt<8>("h0") """ ) + matchingArgs("rem", "SInt<8>", "SInt<8>", """ SInt<8>("h0") """ ) + matchingArgs("and", "UInt<8>", "UInt<8>", """ i """ ) + matchingArgs("and", "SInt<8>", "UInt<8>", """ asUInt(i) """ ) + // Signedness doesn't matter + matchingArgs("or", "UInt<8>", "UInt<8>", """ i """ ) + matchingArgs("or", "SInt<8>", "UInt<8>", """ asUInt(i) """ ) + matchingArgs("xor", "UInt<8>", "UInt<8>", """ UInt<8>("h0") """ ) + matchingArgs("xor", "SInt<8>", "UInt<8>", """ UInt<8>("h0") """ ) + // Always true + matchingArgs("eq", "UInt<8>", "UInt<1>", """ UInt<1>("h1") """ ) + matchingArgs("leq", "UInt<8>", "UInt<1>", """ UInt<1>("h1") """ ) + matchingArgs("geq", "UInt<8>", "UInt<1>", """ UInt<1>("h1") """ ) + // Never true + matchingArgs("neq", "UInt<8>", "UInt<1>", """ UInt<1>("h0") """ ) + matchingArgs("lt", "UInt<8>", "UInt<1>", """ UInt<1>("h0") """ ) + matchingArgs("gt", "UInt<8>", "UInt<1>", """ UInt<1>("h0") """ ) + } + } -- cgit v1.2.3