Skip to content

Commit 9b91aeb

Browse files
committed
Fix IR conversion bug in for loops.
The implicit activation bit was improperly generated inside for loops in some cases. This resulted in spurious asserts or other side-effecting operations firing improperly.
1 parent e234b02 commit 9b91aeb

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

xls/dslx/ir_convert/function_converter.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,8 +1650,10 @@ absl::StatusOr<BValue> FunctionConverter::HandleForBody(
16501650

16511651
XLS_ASSIGN_OR_RETURN(BValue init, Use(node->init()));
16521652
if (implicit_token_data_.has_value()) {
1653-
BValue activated = trip_count == 0 ? function_builder_->Literal(UBits(0, 1))
1654-
: implicit_token_data_->activated;
1653+
BValue activated = trip_count == 0
1654+
? function_builder_->Literal(UBits(0, 1))
1655+
: implicit_token_data_->create_control_predicate();
1656+
16551657
init = function_builder_->Tuple(
16561658
{implicit_token_data_->entry_token, activated, init});
16571659
}

xls/dslx/tests/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,16 @@ dslx_lang_test(
911911
test_ir_equivalence = False,
912912
)
913913

914+
dslx_lang_test(
915+
name = "assert_in_for_under_inactive_conditional",
916+
compare = "interpreter",
917+
# Don't convert to IR because we want to compare the IR interpreter and
918+
# DSL interpreter. convert_to_ir=True disables comparison.
919+
convert_to_ir = False,
920+
# IR asserts are software-only; skip z3 equivalence.
921+
test_ir_equivalence = False,
922+
)
923+
914924
dslx_lang_test(
915925
name = "quickcheck_fn_with_fail",
916926
# Primarily a #[quickcheck] function.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 The XLS Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() -> () { () }
16+
17+
fn bar() -> () { assert!(false, "Should not happen."); }
18+
19+
fn foo(x: bool) -> () {
20+
if x {
21+
for (_, ()) in u32:0..u32:4 {
22+
bar()
23+
}(())
24+
}
25+
}
26+
27+
#[test]
28+
fn test_main_no_assert_when_false() { foo(false) }

0 commit comments

Comments
 (0)