Skip to content

Commit f9c8c82

Browse files
allightcopybara-github
authored andcommitted
Make trace_fmt! include commas (,) in arrays
Fixes: #1428 PiperOrigin-RevId: 636691989
1 parent 94a12f8 commit f9c8c82

File tree

7 files changed

+192
-33
lines changed

7 files changed

+192
-33
lines changed

xls/dslx/interpreter_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ def test_trace_fmt_array_of_enum(self):
350350
)
351351
self.assertIn('a: [MyEnum::ONE, MyEnum::TWO]', stderr)
352352

353+
def test_trace_fmt_array_of_ints(self):
354+
"""Tests we can trace-format an array of u8 values."""
355+
program = """
356+
fn main() {
357+
let a = u8[2]:[u8:1, u8:2];
358+
trace_fmt!("a: {:#x}", a);
359+
}
360+
361+
#[test]
362+
fn hello_test() {
363+
main()
364+
}
365+
"""
366+
stderr = self._parse_and_test(
367+
program, want_error=False, alsologtostderr=True
368+
)
369+
self.assertIn('a: [0x1, 0x2]', stderr)
370+
353371
def test_trace_fmt_tuple_of_enum(self):
354372
"""Tests that we can trace format a tuple that includes enum values."""
355373
program = """

xls/dslx/ir_convert/convert_format_macro.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ absl::Status FlattenArray(const ArrayFormatDescriptor& sfd, const BValue& v,
9292
for (int64_t i = 0; i < sfd.size(); ++i) {
9393
BValue index = ctx.fn_builder.Literal(UBits(i, /*bit_count=*/32));
9494
BValue elem = ctx.fn_builder.ArrayIndex(v, {index});
95+
if (i != 0) {
96+
ctx.fmt_steps.push_back(", ");
97+
}
9598
XLS_RETURN_IF_ERROR(Flatten(sfd.element_format(), elem, ctx));
9699
}
97100
ctx.fmt_steps.push_back("]");

xls/dslx/ir_convert/ir_converter_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,14 +1962,14 @@ TEST(IrConverterTest, EmptyArray) {
19621962

19631963
TEST(IrConverterTest, TraceFmt) {
19641964
constexpr std::string_view kProgram = R"(
1965-
fn trace_and_add(x: u32) -> u32 {
1966-
trace_fmt!("x = {}", x);
1967-
x + u32:1
1965+
fn trace_and_add(x: u32, y: u32[u32:2]) -> u32 {
1966+
trace_fmt!("x = {}, y = {}", x, y);
1967+
x + y[u8:1] + y[u8:0]
19681968
}
19691969
19701970
fn assert_trace_and_add(x: u32) -> u32 {
19711971
if x == u32:5 { fail!("x_is_now_5", u32:0) } else { u32:0 };
1972-
trace_and_add(x)
1972+
trace_and_add(x, [u32:4, u32:6])
19731973
}
19741974
19751975
proc main {

xls/dslx/ir_convert/testdata/ir_converter_test_TraceFmt.ir

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,50 @@ file_number 0 "test_module.x"
44

55
chan test_module__input_c(bits[32], id=0, kind=streaming, ops=send_only, flow_control=ready_valid, strictness=proven_mutually_exclusive, metadata="""""")
66

7-
fn __itok__test_module__trace_and_add(__token: token, __activated: bits[1], x: bits[32]) -> (token, bits[32]) {
8-
trace.4: token = trace(__token, __activated, format="x = {}", data_operands=[x], id=4)
9-
literal.5: bits[32] = literal(value=1, id=5)
10-
after_all.7: token = after_all(trace.4, id=7)
11-
add.6: bits[32] = add(x, literal.5, id=6)
12-
ret tuple.8: (token, bits[32]) = tuple(after_all.7, add.6, id=8)
7+
fn __itok__test_module__trace_and_add(__token: token, __activated: bits[1], x: bits[32], y: bits[32][2]) -> (token, bits[32]) {
8+
literal.5: bits[32] = literal(value=0, id=5)
9+
literal.7: bits[32] = literal(value=1, id=7)
10+
literal.10: bits[8] = literal(value=1, id=10)
11+
array_index.6: bits[32] = array_index(y, indices=[literal.5], id=6)
12+
array_index.8: bits[32] = array_index(y, indices=[literal.7], id=8)
13+
array_index.11: bits[32] = array_index(y, indices=[literal.10], id=11)
14+
literal.13: bits[8] = literal(value=0, id=13)
15+
trace.9: token = trace(__token, __activated, format="x = {}, y = [{}, {}]", data_operands=[x, array_index.6, array_index.8], id=9)
16+
add.12: bits[32] = add(x, array_index.11, id=12)
17+
array_index.14: bits[32] = array_index(y, indices=[literal.13], id=14)
18+
after_all.16: token = after_all(trace.9, id=16)
19+
add.15: bits[32] = add(add.12, array_index.14, id=15)
20+
ret tuple.17: (token, bits[32]) = tuple(after_all.16, add.15, id=17)
1321
}
1422

1523
fn __itok__test_module__assert_trace_and_add(__token: token, __activated: bits[1], x: bits[32]) -> (token, bits[32]) {
16-
literal.12: bits[32] = literal(value=5, id=12)
17-
eq.13: bits[1] = eq(x, literal.12, id=13)
18-
and.16: bits[1] = and(__activated, eq.13, id=16)
19-
not.17: bits[1] = not(and.16, id=17)
20-
invoke.22: (token, bits[32]) = invoke(__token, __activated, x, to_apply=__itok__test_module__trace_and_add, id=22)
21-
literal.15: bits[32] = literal(value=0, id=15)
22-
assert.18: token = assert(__token, not.17, message="Assertion failure via fail! @ test_module.x:8:28-8:49", label="x_is_now_5", id=18)
23-
tuple_index.23: token = tuple_index(invoke.22, index=0, id=23)
24-
literal.20: bits[32] = literal(value=0, id=20)
25-
identity.19: bits[32] = identity(literal.15, id=19)
26-
after_all.25: token = after_all(assert.18, tuple_index.23, id=25)
27-
tuple_index.24: bits[32] = tuple_index(invoke.22, index=1, id=24)
28-
literal.14: bits[8][10] = literal(value=[120, 95, 105, 115, 95, 110, 111, 119, 95, 53], id=14)
29-
sel.21: bits[32] = sel(eq.13, cases=[literal.20, identity.19], id=21)
30-
ret tuple.26: (token, bits[32]) = tuple(after_all.25, tuple_index.24, id=26)
24+
literal.21: bits[32] = literal(value=5, id=21)
25+
eq.22: bits[1] = eq(x, literal.21, id=22)
26+
literal.31: bits[32] = literal(value=4, id=31)
27+
literal.32: bits[32] = literal(value=6, id=32)
28+
and.25: bits[1] = and(__activated, eq.22, id=25)
29+
array.33: bits[32][2] = array(literal.31, literal.32, id=33)
30+
not.26: bits[1] = not(and.25, id=26)
31+
invoke.34: (token, bits[32]) = invoke(__token, __activated, x, array.33, to_apply=__itok__test_module__trace_and_add, id=34)
32+
literal.24: bits[32] = literal(value=0, id=24)
33+
assert.27: token = assert(__token, not.26, message="Assertion failure via fail! @ test_module.x:8:28-8:49", label="x_is_now_5", id=27)
34+
tuple_index.35: token = tuple_index(invoke.34, index=0, id=35)
35+
literal.29: bits[32] = literal(value=0, id=29)
36+
identity.28: bits[32] = identity(literal.24, id=28)
37+
after_all.37: token = after_all(assert.27, tuple_index.35, id=37)
38+
tuple_index.36: bits[32] = tuple_index(invoke.34, index=1, id=36)
39+
literal.23: bits[8][10] = literal(value=[120, 95, 105, 115, 95, 110, 111, 119, 95, 53], id=23)
40+
sel.30: bits[32] = sel(eq.22, cases=[literal.29, identity.28], id=30)
41+
ret tuple.38: (token, bits[32]) = tuple(after_all.37, tuple_index.36, id=38)
3142
}
3243

3344
top proc __test_module__main_0_next(__state: bits[32], init={0}) {
34-
__token: token = literal(value=token, id=27)
35-
literal.29: bits[1] = literal(value=1, id=29)
36-
after_all.30: token = after_all(id=30)
37-
invoke.32: (token, bits[32]) = invoke(__token, literal.29, __state, to_apply=__itok__test_module__assert_trace_and_add, id=32)
38-
tok: token = send(after_all.30, __state, channel=test_module__input_c, id=31)
39-
tuple_index.33: token = tuple_index(invoke.32, index=0, id=33)
40-
tuple_index.34: bits[32] = tuple_index(invoke.32, index=1, id=34)
41-
next (tuple_index.34)
45+
__token: token = literal(value=token, id=39)
46+
literal.41: bits[1] = literal(value=1, id=41)
47+
after_all.42: token = after_all(id=42)
48+
invoke.44: (token, bits[32]) = invoke(__token, literal.41, __state, to_apply=__itok__test_module__assert_trace_and_add, id=44)
49+
tok: token = send(after_all.42, __state, channel=test_module__input_c, id=43)
50+
tuple_index.45: token = tuple_index(invoke.44, index=0, id=45)
51+
tuple_index.46: bits[32] = tuple_index(invoke.44, index=1, id=46)
52+
next (tuple_index.46)
4253
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2024 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+
load(
16+
"//xls/build_rules:xls_build_defs.bzl",
17+
"cc_xls_ir_jit_wrapper",
18+
"xls_dslx_ir",
19+
"xls_dslx_library",
20+
)
21+
22+
package(
23+
default_applicable_licenses = ["//:license"],
24+
default_visibility = ["//xls:xls_internal"],
25+
licenses = ["notice"], # Apache 2.0
26+
)
27+
28+
xls_dslx_library(
29+
name = "test_target_library",
30+
srcs = ["test_target.x"],
31+
)
32+
33+
xls_dslx_ir(
34+
name = "test_target",
35+
dslx_top = "traceit",
36+
library = ":test_target_library",
37+
)
38+
39+
cc_xls_ir_jit_wrapper(
40+
name = "test_target_wrapper",
41+
src = ":test_target.ir",
42+
jit_wrapper_args = {
43+
"class_name": "TraceIt",
44+
"namespace": "xls::wrapped",
45+
},
46+
)
47+
48+
cc_test(
49+
name = "trace_fmt_array_test",
50+
srcs = ["trace_fmt_array_test.cc"],
51+
deps = [
52+
":test_target_wrapper",
53+
"//xls/common:xls_gunit_main",
54+
"//xls/common/status:matchers",
55+
"//xls/ir:bits",
56+
"//xls/ir:events",
57+
"//xls/ir:value",
58+
"//xls/ir:value_builder",
59+
"@com_google_googletest//:gtest",
60+
],
61+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 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 traceit(v: u8[8]) -> u8 {
16+
trace_fmt!("The array is {}", v);
17+
v[0]
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 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+
#include "gmock/gmock.h"
16+
#include "gtest/gtest.h"
17+
#include "xls/common/status/matchers.h"
18+
#include "xls/dslx/tests/trace_fmt_array/test_target_wrapper.h"
19+
#include "xls/ir/bits.h"
20+
#include "xls/ir/events.h"
21+
#include "xls/ir/value.h"
22+
#include "xls/ir/value_builder.h"
23+
24+
namespace xls {
25+
namespace {
26+
using ::testing::ElementsAre;
27+
using ::testing::IsEmpty;
28+
29+
MATCHER_P(TraceMessage, m, "") {
30+
return testing::ExplainMatchResult(m, arg.message, result_listener);
31+
}
32+
TEST(TraceFmtArray, TraceHasCommas) {
33+
XLS_ASSERT_OK_AND_ASSIGN(auto trace, wrapped::TraceIt::Create());
34+
XLS_ASSERT_OK_AND_ASSIGN(
35+
auto input,
36+
ValueBuilder::UBitsArray({1, 2, 3, 4, 5, 6, 7, 8}, 8).Build());
37+
XLS_ASSERT_OK_AND_ASSIGN(InterpreterResult<Value> res,
38+
trace->jit()->Run({input}));
39+
40+
EXPECT_EQ(res.value.bits(), UBits(1, 8));
41+
EXPECT_THAT(res.events.assert_msgs, IsEmpty());
42+
EXPECT_THAT(
43+
res.events.trace_msgs,
44+
ElementsAre(TraceMessage("The array is [1, 2, 3, 4, 5, 6, 7, 8]")));
45+
}
46+
47+
} // namespace
48+
} // namespace xls

0 commit comments

Comments
 (0)