Skip to content

Commit 91c4d35

Browse files
dplassgitcopybara-github
authored andcommitted
[DSLX Fuzz testing] Add a FuzzTestFunction AST node in addition to the
`#[fuzz_test]` attribute. This is needed to unlock parsing and type- checking the fuzz test domains as a tuple. PiperOrigin-RevId: 896502488
1 parent 2742cfa commit 91c4d35

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

xls/dslx/frontend/ast.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ std::string_view AstNodeKindToString(AstNodeKind kind) {
311311
return "for";
312312
case AstNodeKind::kFunctionRef:
313313
return "function-ref";
314+
case AstNodeKind::kFuzzTestFunction:
315+
return "fuzz test function";
314316
case AstNodeKind::kStatementBlock:
315317
return "statement-block";
316318
case AstNodeKind::kTrait:
@@ -2419,6 +2421,10 @@ Function::LambdaReturnTypeParametrics() const {
24192421

24202422
TestFunction::~TestFunction() = default;
24212423

2424+
// -- class FuzzTestFunction
2425+
2426+
FuzzTestFunction::~FuzzTestFunction() = default;
2427+
24222428
// -- class Lambda
24232429

24242430
Lambda::Lambda(Module* owner, Span span, Function* function, bool in_parens)

xls/dslx/frontend/ast.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
X(ConstantDef) \
9595
X(EnumDef) \
9696
X(Function) \
97+
X(FuzzTestFunction) \
9798
X(Impl) \
9899
X(Import) \
99100
X(Let) \
@@ -3843,6 +3844,8 @@ class Range : public Expr {
38433844
// #[test]
38443845
// fn test_foo() { ... }
38453846
// ```
3847+
// TODO: davidplass - Add an optional parameter Expr to allow re-using as a
3848+
// FuzzTestFunction or QuickCheck.
38463849
class TestFunction : public AstNode {
38473850
public:
38483851
static std::string_view GetDebugTypeName() { return "test function"; }
@@ -3879,6 +3882,73 @@ class TestFunction : public AstNode {
38793882
Function& fn_;
38803883
};
38813884

3885+
// Represents a fuzz test construct.
3886+
//
3887+
// These are specified with an annotation as follows:
3888+
//
3889+
// ```dslx
3890+
// #[fuzz_test]
3891+
// fn test_foo() { ... }
3892+
// ```
3893+
//
3894+
// or with a domains argument:
3895+
//
3896+
// ```dslx
3897+
// #[fuzz_test(domains=`...`)]
3898+
// fn test_foo() { ... }
3899+
// ```
3900+
// TODO: davidplass - Migrate this into TestFunction, after adding an optional
3901+
// parameter to TestFunction to capture the domain tuple.
3902+
// TODO: davidplass - make this a ModuleMember.
3903+
class FuzzTestFunction : public AstNode {
3904+
public:
3905+
static std::string_view GetDebugTypeName() { return "fuzz test function"; }
3906+
3907+
FuzzTestFunction(Module* owner, Span span, Function& fn,
3908+
std::optional<Expr*> domains)
3909+
: AstNode(owner),
3910+
span_(std::move(span)),
3911+
fn_(fn),
3912+
domains_(std::move(domains)) {}
3913+
3914+
~FuzzTestFunction() override;
3915+
3916+
AstNodeKind kind() const override { return AstNodeKind::kFuzzTestFunction; }
3917+
NameDef* name_def() const { return fn_.name_def(); }
3918+
3919+
absl::Status Accept(AstNodeVisitor* v) const override {
3920+
return v->HandleFuzzTestFunction(this);
3921+
}
3922+
3923+
std::vector<AstNode*> GetChildren(bool want_types) const override {
3924+
return {&fn_};
3925+
}
3926+
3927+
std::string_view GetNodeTypeName() const override {
3928+
return "FuzzTestFunction";
3929+
}
3930+
std::string ToString() const override {
3931+
if (domains_.has_value()) {
3932+
return absl::StrFormat("#[fuzz_test(domains=`%s`)]\n%s",
3933+
(*domains_)->ToString(), fn_.ToString());
3934+
}
3935+
return absl::StrFormat("#[fuzz_test]\n%s", fn_.ToString());
3936+
}
3937+
3938+
Function& fn() const { return fn_; }
3939+
std::optional<Span> GetSpan() const override { return span(); }
3940+
const Span& span() const { return span_; }
3941+
3942+
const std::string& identifier() const { return fn_.name_def()->identifier(); }
3943+
3944+
const std::optional<Expr*>& domains() const { return domains_; }
3945+
3946+
private:
3947+
const Span span_;
3948+
Function& fn_;
3949+
const std::optional<Expr*> domains_;
3950+
};
3951+
38823952
enum class QuickCheckTestCasesTag {
38833953
kExhaustive,
38843954
kCounted,
@@ -3914,6 +3984,8 @@ class QuickCheckTestCases {
39143984
};
39153985

39163986
// Represents a function to be quick-check'd.
3987+
// TODO: davidplass - Migrate this into TestFunction, since QuickCheck is an
3988+
// attribute which can capture the quick check test cases.
39173989
class QuickCheck : public AstNode {
39183990
public:
39193991
static std::string_view GetDebugTypeName() { return "quickcheck"; }

xls/dslx/frontend/ast_cloner.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,17 @@ class AstCloner : public AstNodeVisitor {
10471047
return absl::OkStatus();
10481048
}
10491049

1050+
absl::Status HandleFuzzTestFunction(const FuzzTestFunction* n) override {
1051+
XLS_RETURN_IF_ERROR(VisitChildren(n));
1052+
1053+
XLS_RETURN_IF_ERROR(ReplaceOrVisit(&n->fn()));
1054+
XLS_ASSIGN_OR_RETURN(Function * new_fn, CastIfNotVerbatim<Function*>(
1055+
old_to_new_.at(&n->fn())));
1056+
old_to_new_[n] =
1057+
module(n)->Make<FuzzTestFunction>(n->span(), *new_fn, n->domains());
1058+
return absl::OkStatus();
1059+
}
1060+
10501061
absl::Status HandleTestProc(const TestProc* n) override {
10511062
XLS_RETURN_IF_ERROR(VisitChildren(n));
10521063

xls/dslx/frontend/ast_node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum class AstNodeKind : uint8_t {
5252
kFormatMacro,
5353
kFunction,
5454
kFunctionRef,
55+
kFuzzTestFunction,
5556
kImpl,
5657
kImport,
5758
kIndex,

xls/dslx/ir_convert/function_converter.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ class FunctionConverterVisitor : public AstNodeVisitor {
399399
// keep-sorted start
400400
INVALID(Attribute)
401401
INVALID(FunctionRef)
402+
INVALID(FuzzTestFunction)
402403
INVALID(MatchArm)
403404
INVALID(NameDef)
404405
INVALID(NameDefTree)

xls/dslx/type_system/type_info.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ enum AstNodeKindProto {
9797
AST_NODE_KIND_PROC_ALIAS = 72;
9898
AST_NODE_KIND_TRAIT = 73;
9999
AST_NODE_KIND_ATTRIBUTE = 74;
100+
AST_NODE_KIND_FUZZ_TEST_FUNCTION = 75;
100101
}
101102

102103
message BitsValueProto {

xls/dslx/type_system/type_info_to_proto.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <string_view>
2323
#include <tuple>
2424
#include <utility>
25-
#include <variant>
2625
#include <vector>
2726

2827
#include "absl/base/optimization.h"
@@ -33,7 +32,6 @@
3332
#include "absl/strings/str_format.h"
3433
#include "absl/strings/str_join.h"
3534
#include "absl/types/span.h"
36-
#include "xls/common/proto_adaptor_utils.h"
3735
#include "xls/common/status/status_macros.h"
3836
#include "xls/dslx/channel_direction.h"
3937
#include "xls/dslx/frontend/ast.h"
@@ -124,6 +122,8 @@ AstNodeKindProto ToProto(AstNodeKind kind) {
124122
return AST_NODE_KIND_SEND_IF;
125123
case AstNodeKind::kTestFunction:
126124
return AST_NODE_KIND_TEST_FUNCTION;
125+
case AstNodeKind::kFuzzTestFunction:
126+
return AST_NODE_KIND_FUZZ_TEST_FUNCTION;
127127
case AstNodeKind::kTestProc:
128128
return AST_NODE_KIND_TEST_PROC;
129129
case AstNodeKind::kWildcardPattern:
@@ -738,6 +738,8 @@ absl::StatusOr<AstNodeKind> FromProto(AstNodeKindProto p) {
738738
return AstNodeKind::kSendIf;
739739
case AST_NODE_KIND_TEST_FUNCTION:
740740
return AstNodeKind::kTestFunction;
741+
case AST_NODE_KIND_FUZZ_TEST_FUNCTION:
742+
return AstNodeKind::kFuzzTestFunction;
741743
case AST_NODE_KIND_TEST_PROC:
742744
return AstNodeKind::kTestProc;
743745
case AST_NODE_KIND_WILDCARD_PATTERN:

0 commit comments

Comments
 (0)