From efe4ea4590e4450e3a6673fd5ff7b7c5391c4e52 Mon Sep 17 00:00:00 2001 From: XLS Team Date: Fri, 19 Jun 2026 10:53:49 -0700 Subject: [PATCH] Fix xls codegen for extension ops Added checks in Op::kZeroExt and Op::kSignExt handling. If the input and output widths are the same (identity extension), the generator now returns the input expression directly. This avoids generating invalid 0'h0 literals (for zero-extension) or 0-replication (for sign-extension). PiperOrigin-RevId: 934986596 --- xls/codegen/node_expressions.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xls/codegen/node_expressions.cc b/xls/codegen/node_expressions.cc index 905413644a..c139a6e40b 100644 --- a/xls/codegen/node_expressions.cc +++ b/xls/codegen/node_expressions.cc @@ -634,6 +634,9 @@ absl::StatusOr NodeToExpression( case Op::kShrl: return EmitShift(node, inputs[0], inputs[1], file); case Op::kSignExt: { + if (node->BitCountOrDie() == node->operand(0)->BitCountOrDie()) { + return inputs[0]; + } if (node->operand(0)->BitCountOrDie() == 1) { // A sign extension of a single-bit value is just replication. return file->Concat( @@ -709,6 +712,9 @@ absl::StatusOr NodeToExpression( case Op::kXorReduce: return file->XorReduce(inputs[0], node->loc()); case Op::kZeroExt: { + if (node->BitCountOrDie() == node->operand(0)->BitCountOrDie()) { + return inputs[0]; + } int64_t bits_added = node->BitCountOrDie() - node->operand(0)->BitCountOrDie();