Skip to content

Commit 2db70ef

Browse files
dplassgitcopybara-github
authored andcommitted
[DSLX fuzz testing] In preparation for propagating DSLX fuzztest domains
from the .x file to the IR, refactor Attribute kind and arguments to a shared place. This will be re-used in the IR eventually. PiperOrigin-RevId: 885171285
1 parent ce5dd34 commit 2db70ef

17 files changed

Lines changed: 207 additions & 117 deletions

File tree

xls/common/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ package(
3434
licenses = ["notice"], # Apache 2.0
3535
)
3636

37+
cc_library(
38+
name = "attribute_data",
39+
srcs = ["attribute_data.cc"],
40+
hdrs = ["attribute_data.h"],
41+
)
42+
3743
cc_library(
3844
name = "timeout_support",
3945
srcs = ["timeout_support.cc"],

xls/common/attribute_data.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2026 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 "xls/common/attribute_data.h"
16+
17+
#include <string>
18+
19+
namespace xls {
20+
21+
std::string AttributeKindToString(AttributeKind kind) {
22+
switch (kind) {
23+
case AttributeKind::kCfg:
24+
return "cfg";
25+
case AttributeKind::kDerive:
26+
return "derive";
27+
case AttributeKind::kDslxFormatDisable:
28+
return "dslx_format_disable";
29+
case AttributeKind::kExternVerilog:
30+
return "extern_verilog";
31+
case AttributeKind::kSvType:
32+
return "sv_type";
33+
case AttributeKind::kTest:
34+
return "test";
35+
case AttributeKind::kTestProc:
36+
return "test_proc";
37+
case AttributeKind::kQuickcheck:
38+
return "quickcheck";
39+
case AttributeKind::kChannelStrictness:
40+
return "channel_strictness";
41+
}
42+
}
43+
44+
} // namespace xls

xls/common/attribute_data.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2026 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+
#ifndef XLS_COMMON_ATTRIBUTE_DATA_H_
16+
#define XLS_COMMON_ATTRIBUTE_DATA_H_
17+
18+
#include <cstdint>
19+
#include <string>
20+
#include <utility>
21+
#include <variant>
22+
#include <vector>
23+
24+
namespace xls {
25+
26+
enum class AttributeKind : uint8_t {
27+
kCfg,
28+
kDerive,
29+
kDslxFormatDisable,
30+
kExternVerilog,
31+
kSvType,
32+
kTest,
33+
kTestProc,
34+
kQuickcheck,
35+
kChannelStrictness
36+
};
37+
38+
std::string AttributeKindToString(AttributeKind kind);
39+
40+
class AttributeData {
41+
public:
42+
using StringKeyValueArgument = std::pair<std::string, std::string>;
43+
using IntKeyValueArgument = std::pair<std::string, int64_t>;
44+
45+
// Represents a quoted string argument as opposed to a bare identifier-like
46+
// string.
47+
struct StringLiteralArgument {
48+
std::string text;
49+
};
50+
51+
// Note that a std::string argument is the simplest kind and looks like an
52+
// unquoted identifier, like `test` in `#[cfg(test)]`.
53+
using Argument = std::variant<std::string, StringLiteralArgument,
54+
StringKeyValueArgument, IntKeyValueArgument>;
55+
56+
AttributeData(AttributeKind kind, std::vector<Argument> args)
57+
: kind_(kind), args_(std::move(args)) {}
58+
59+
AttributeData(const AttributeData& other) = default;
60+
61+
AttributeKind kind() const { return kind_; }
62+
const std::vector<Argument>& args() const { return args_; }
63+
64+
private:
65+
const AttributeKind kind_;
66+
const std::vector<Argument> args_;
67+
};
68+
69+
} // namespace xls
70+
71+
#endif // XLS_COMMON_ATTRIBUTE_DATA_H_

xls/dslx/fmt/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ cc_library(
6565
":comments",
6666
":format_disabler",
6767
":pretty_print",
68+
"//xls/common:attribute_data",
6869
"//xls/common:visitor",
6970
"//xls/common/status:ret_check",
7071
"//xls/common/status:status_macros",

xls/dslx/fmt/ast_fmt.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "absl/types/span.h"
4141
#include "absl/types/variant.h"
4242
#include "cppitertools/enumerate.hpp"
43+
#include "xls/common/attribute_data.h"
4344
#include "xls/common/status/ret_check.h"
4445
#include "xls/common/status/status_macros.h"
4546
#include "xls/common/visitor.h"
@@ -465,19 +466,19 @@ DocRef FmtAttribute(const Attribute& attribute, DocArena& arena) {
465466

466467
if (!attribute.args().empty()) {
467468
pieces.push_back(arena.oparen());
468-
for (const Attribute::Argument& arg : attribute.args()) {
469+
for (const AttributeData::Argument& arg : attribute.args()) {
469470
absl::visit(
470471
Visitor{
471472
[&](std::string str) { pieces.push_back(arena.MakeText(str)); },
472-
[&](Attribute::StringLiteralArgument arg) {
473+
[&](AttributeData::StringLiteralArgument arg) {
473474
pieces.push_back(
474475
arena.MakeText(absl::Substitute("\"$0\"", arg.text)));
475476
},
476-
[&](Attribute::IntKeyValueArgument arg) {
477+
[&](AttributeData::IntKeyValueArgument arg) {
477478
pieces.push_back(arena.MakeText(
478479
absl::Substitute("$0=$1", arg.first, arg.second)));
479480
},
480-
[&](Attribute::StringKeyValueArgument arg) {
481+
[&](AttributeData::StringKeyValueArgument arg) {
481482
pieces.push_back(arena.MakeText(
482483
absl::Substitute("$0=$1", arg.first, arg.second)));
483484
},

xls/dslx/frontend/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ cc_library(
3939
":module",
4040
":pos",
4141
":proc",
42+
"//xls/common:attribute_data",
4243
"//xls/common:visitor",
4344
"//xls/common/status:ret_check",
4445
"//xls/common/status:status_macros",
@@ -164,6 +165,7 @@ cc_library(
164165
":scanner",
165166
":token",
166167
":token_parser",
168+
"//xls/common:attribute_data",
167169
"//xls/common:strong_int",
168170
"//xls/common:visitor",
169171
"//xls/common/status:ret_check",
@@ -235,6 +237,7 @@ cc_library(
235237
":module",
236238
":pos",
237239
":token_utils",
240+
"//xls/common:attribute_data",
238241
"//xls/common:visitor",
239242
"//xls/common/status:ret_check",
240243
"//xls/common/status:status_macros",
@@ -486,6 +489,7 @@ cc_library(
486489
":ast_node",
487490
":pos",
488491
":token_utils",
492+
"//xls/common:attribute_data",
489493
"//xls/common:indent",
490494
"//xls/common:visitor",
491495
"//xls/common/status:ret_check",

xls/dslx/frontend/ast.cc

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "absl/strings/substitute.h"
4444
#include "absl/types/span.h"
4545
#include "absl/types/variant.h"
46+
#include "xls/common/attribute_data.h"
4647
#include "xls/common/indent.h"
4748
#include "xls/common/status/ret_check.h"
4849
#include "xls/common/status/status_macros.h"
@@ -639,51 +640,31 @@ std::string BinopKindToString(BinopKind kind) {
639640
return absl::StrFormat("<invalid BinopKind(%d)>", static_cast<int>(kind));
640641
}
641642

642-
std::string AttributeKindToString(AttributeKind kind) {
643-
switch (kind) {
644-
case AttributeKind::kCfg:
645-
return "cfg";
646-
case AttributeKind::kDerive:
647-
return "derive";
648-
case AttributeKind::kDslxFormatDisable:
649-
return "dslx_format_disable";
650-
case AttributeKind::kExternVerilog:
651-
return "extern_verilog";
652-
case AttributeKind::kSvType:
653-
return "sv_type";
654-
case AttributeKind::kTest:
655-
return "test";
656-
case AttributeKind::kTestProc:
657-
return "test_proc";
658-
case AttributeKind::kQuickcheck:
659-
return "quickcheck";
660-
case AttributeKind::kChannelStrictness:
661-
return "channel_strictness";
662-
}
663-
}
664-
665643
// -- class Attribute
666644

667645
std::string Attribute::ToString() const {
668646
std::string args;
669-
if (!args_.empty()) {
647+
if (!attribute_data_.args().empty()) {
670648
absl::StrAppend(&args, "(");
671-
for (Argument next : args_) {
649+
for (const AttributeData::Argument& next : attribute_data_.args()) {
672650
absl::StrAppend(
673651
&args,
674652
absl::visit(Visitor{
675653
[](auto arg) {
676654
return absl::Substitute("$0 = $1", arg.first,
677655
arg.second);
678656
},
679-
[](StringLiteralArgument arg) { return arg.text; },
680-
[](std::string arg) { return arg; },
657+
[](const AttributeData::StringLiteralArgument& arg) {
658+
return arg.text;
659+
},
660+
[](const std::string& arg) { return arg; },
681661
},
682662
next));
683663
}
684664
absl::StrAppend(&args, ")");
685665
}
686-
return absl::Substitute("#[$0$1]", AttributeKindToString(kind_), args);
666+
return absl::Substitute("#[$0$1]",
667+
AttributeKindToString(attribute_data_.kind()), args);
687668
}
688669

689670
// -- class NameDef

xls/dslx/frontend/ast.h

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "absl/strings/str_format.h"
3939
#include "absl/types/span.h"
4040
#include "absl/types/variant.h"
41+
#include "xls/common/attribute_data.h" // IWYU pragma: keep
4142
#include "xls/common/status/status_macros.h"
4243
#include "xls/dslx/channel_direction.h"
4344
#include "xls/dslx/frontend/ast_node.h" // IWYU pragma: export
@@ -321,47 +322,18 @@ enum class TypeAnnotationKind : uint8_t {
321322
kTypeVariable
322323
};
323324

324-
enum class AttributeKind : uint8_t {
325-
kCfg,
326-
kDerive,
327-
kDslxFormatDisable,
328-
kExternVerilog,
329-
kSvType,
330-
kTest,
331-
kTestProc,
332-
kQuickcheck,
333-
kChannelStrictness
334-
};
335-
336-
std::string AttributeKindToString(AttributeKind kind);
337-
338325
// One of the `#[foo]`-like nodes preceding some entity.
339326
class Attribute : public AstNode {
340327
public:
341-
using StringKeyValueArgument = std::pair<std::string, std::string>;
342-
using IntKeyValueArgument = std::pair<std::string, int64_t>;
343-
344-
// Represents a quoted string argument as opposed to a bare identifier-like
345-
// string.
346-
struct StringLiteralArgument {
347-
std::string text;
348-
};
349-
350-
// Note that a std::string argument is the simplest kind and looks like an
351-
// unquoted identifier, like `test` in `#[cfg(test)]`.
352-
using Argument = std::variant<std::string, StringLiteralArgument,
353-
StringKeyValueArgument, IntKeyValueArgument>;
354-
355328
Attribute(Module* owner, Span span, std::optional<Span> arg_span,
356-
AttributeKind kind, std::vector<Argument> args)
329+
AttributeData attribute_data)
357330
: AstNode(owner),
358331
span_(span),
359332
arg_span_(arg_span),
360-
kind_(kind),
361-
args_(std::move(args)) {}
333+
attribute_data_(std::move(attribute_data)) {}
362334

363335
AstNodeKind kind() const override { return AstNodeKind::kAttribute; }
364-
AttributeKind attribute_kind() const { return kind_; }
336+
AttributeKind attribute_kind() const { return attribute_data_.kind(); }
365337

366338
std::optional<Span> GetSpan() const override { return span_; }
367339
std::optional<Span> GetArgSpan() const { return arg_span_; }
@@ -371,7 +343,10 @@ class Attribute : public AstNode {
371343
}
372344

373345
std::string_view GetNodeTypeName() const override { return "Attribute"; }
374-
const std::vector<Argument>& args() const { return args_; }
346+
const AttributeData& attribute_data() const { return attribute_data_; }
347+
const std::vector<AttributeData::Argument>& args() const {
348+
return attribute_data_.args();
349+
}
375350

376351
std::vector<AstNode*> GetChildren(bool want_types) const override {
377352
return {};
@@ -382,8 +357,7 @@ class Attribute : public AstNode {
382357
private:
383358
const Span span_;
384359
const std::optional<Span> arg_span_;
385-
const AttributeKind kind_;
386-
const std::vector<Argument> args_;
360+
const AttributeData attribute_data_;
387361
};
388362

389363
// Abstract base class for type annotations.

xls/dslx/frontend/ast_cloner.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "absl/strings/str_format.h"
3232
#include "absl/types/span.h"
3333
#include "absl/types/variant.h"
34+
#include "xls/common/attribute_data.h"
3435
#include "xls/common/status/ret_check.h"
3536
#include "xls/common/status/status_macros.h"
3637
#include "xls/common/visitor.h"
@@ -87,7 +88,7 @@ class AstCloner : public AstNodeVisitor {
8788
XLS_RETURN_IF_ERROR(VisitChildren(n));
8889

8990
old_to_new_[n] = module(n)->Make<Attribute>(*n->GetSpan(), n->GetArgSpan(),
90-
n->attribute_kind(), n->args());
91+
n->attribute_data());
9192
return absl::OkStatus();
9293
}
9394

xls/dslx/frontend/ast_utils.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "absl/status/statusor.h"
3535
#include "absl/strings/str_format.h"
3636
#include "absl/types/variant.h"
37+
#include "xls/common/attribute_data.h"
3738
#include "xls/common/status/ret_check.h"
3839
#include "xls/common/status/status_macros.h"
3940
#include "xls/common/visitor.h"

0 commit comments

Comments
 (0)