8000 Added a test to the IR Fuzzer that verifies the result of IR before and after an optimization pass. by copybara-service[bot] · Pull Request #2465 · google/xls · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added a test to the IR Fuzzer that verifies the result of IR before and after an optimization pass. #2465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions xls/fuzzer/ir_fuzzer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ package(

cc_library(
name = "ir_fuzz_domain",
testonly = 1,
testonly = True,
srcs = ["ir_fuzz_domain.cc"],
hdrs = ["ir_fuzz_domain.h"],
deps = [
":fuzz_program_cc_proto",
":ir_fuzz_builder",
":ir_fuzz_helpers",
"//xls/common/fuzzing:fuzztest",
"//xls/common/status:ret_check",
"//xls/ir",
"//xls/ir:bits",
"//xls/ir:function_builder",
"//xls/ir:ir_test_base",
"//xls/ir:value",
"//xls/ir:verifier",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:span",
],
)

Expand All @@ -49,6 +54,7 @@ cc_test(
"//xls/common/fuzzing:fuzztest",
"//xls/common/status:matchers",
"//xls/ir",
"//xls/ir:function_builder",
"//xls/ir:ir_test_base",
"//xls/ir:verifier",
"@com_google_absl//absl/log",
Expand Down Expand Up @@ -105,6 +111,27 @@ cc_library(
],
)

cc_test(
name = "ir_fuzz_builder_test",
srcs = ["ir_fuzz_builder_test.cc"],
deps = [
":fuzz_program_cc_proto",
":ir_fuzz_builder",
"//xls/common:xls_gunit_main",
"//xls/common/fuzzing:fuzztest",
"//xls/common/status:matchers",
"//xls/ir",
"//xls/ir:bits",
"//xls/ir:function_builder",
"//xls/ir:ir_matcher",
"//xls/ir:ir_test_base",
"//xls/ir:verifier",
"@com_google_absl//absl/strings:str_format",
"@com_google_protobuf//:protobuf",
"@googletest//:gtest",
],
)

cc_library(
name = "ir_fuzz_visitor",
srcs = ["ir_fuzz_visitor.cc"],
Expand Down Expand Up @@ -137,23 +164,29 @@ cc_test(
],
)

cc_test(
name = "ir_fuzz_builder_test",
srcs = ["ir_fuzz_builder_test.cc"],
cc_library(
name = "ir_fuzz_test_library",
testonly = True,
srcs = ["ir_fuzz_test_library.cc"],
hdrs = ["ir_fuzz_test_library.h"],
deps = [
":fuzz_program_cc_proto",
":ir_fuzz_builder",
"//xls/common:xls_gunit_main",
":ir_fuzz_domain",
"//xls/common/fuzzing:fuzztest",
"//xls/common/status:matchers",
"//xls/common/status:status_macros",
"//xls/interpreter:ir_interpreter",
"//xls/ir",
"//xls/ir:bits",
"//xls/ir:events",
"//xls/ir:function_builder",
"//xls/ir:ir_matcher",
"//xls/ir:ir_test_base",
"//xls/ir:value",
"//xls/ir:verifier",
"@com_google_absl//absl/strings:str_format",
"@com_google_protobuf//:protobuf",
"@googletest//:gtest",
"//xls/passes:optimization_pass",
"//xls/passes:pass_base",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:span",
],
)
3 changes: 2 additions & 1 deletion xls/fuzzer/ir_fuzzer/ir_fuzz_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
namespace m = ::xls::op_matchers;

// Performs tests on the IrFuzzBuilder by manually creating a FuzzProgramProto,
// instantiating into its IR version, and manually verifying the IR is correct.
// instantiating it into its IR version, and manually verifying the IR is
// correct.

namespace xls {
namespace {
Expand Down
85 changes: 85 additions & 0 deletions xls/fuzzer/ir_fuzzer/ir_fuzz_domain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
#include "xls/fuzzer/ir_fuzzer/ir_fuzz_domain.h"

#include <cstdint>
#include <sstream>
#include <utility>

#include "xls/common/fuzzing/fuzztest.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "xls/common/status/ret_check.h"
#include "xls/fuzzer/ir_fuzzer/fuzz_program.pb.h"
#include "xls/fuzzer/ir_fuzzer/ir_fuzz_builder.h"
#include "xls/fuzzer/ir_fuzzer/ir_fuzz_helpers.h"
#include "xls/ir/bits.h"
#include "xls/ir/function_builder.h"
#include "xls/ir/ir_test_base.h"
#include "xls/ir/package.h"
#include "xls/ir/value.h"
#include "xls/ir/verifier.h"

namespace xls {
Expand All @@ -44,6 +50,50 @@ absl::StatusOr<std::shared_ptr<Package>> BuildPackage(
return std::move(p);
}

// A domain that expands upon the IrFuzzDomain by returning a vector of bytes
// parameters along with the Package. The strings in the vector correlate to a
// parameter input into the Function. A fuzztest::FlatMap is used in order to
// create the IrFuzzDomain first, then determine the number of parameters in the
// Function in order to randomly generate a vector of strings.
fuzztest::Domain<
std::pair<std::shared_ptr<Package>, std::vector<std::vector<std::string>>>>
IrFuzzDomainWithBytesParams(int64_t param_set_count) {
return fuzztest::FlatMap(
[param_set_count](std::shared_ptr<Package> p) {
Function* f = p->GetFunction(IrTestBase::TestName()).value();
return fuzztest::PairOf(
// fuzztest::Just does not like to deal with move-only types so we
// are using a shared_ptr for now.
fuzztest::Just(std::move(p)),
// A string represents a byte array. The byte array can be
// interpreted as a large integer.
fuzztest::VectorOf(
fuzztest::VectorOf(fuzztest::Arbitrary<std::string>())
// Retrieve the number of parameters in the Function.
.WithSize(f->params().size()))
// Generate param_set_count number of param sets.
.WithSize(param_set_count));
},
IrFuzzDomain());
}

// Returns a human-readable string representation of the param sets.
std::string StringifyParamSets(
absl::Span<const std::vector<Value>> param_sets) {
std::stringstream ss;
ss << "[";
// Iterate over the number of param sets.
for (int64_t i = 0; i < param_sets.size(); i += 1) {
// Iterate over the param set elements.
for (int64_t j = 0; j < param_sets[i].size(); j += 1) {
ss << (param_sets[i][j].ToHumanString())
<< (j != param_sets[i].size() - 1 ? ", " : "");
}
ss << (i != param_sets.size() - 1 ? "], [" : "]");
}
return ss.str();
}

} // namespace

// Returns a fuzztest domain, which is a range of possible values that an object
Expand Down Expand Up @@ -89,4 +139,39 @@ fuzztest::Domain<std::shared_ptr<Package>> IrFuzzDomain() {
.WithMinSize(1)));
}

// A domain that expands upon the IrFuzzDomainWithBytesParams by converting the
// string parameters into a vector of Value objects. This vector can be plugged
// into the Function directly for interpreting the IR. The byte arrays are
// truncated to the bit width of the parameters. The param_set_count parameter
// specifies the number of param sets to generate in case you need multiple sets
// of inputs for testing.
// TODO: Consider using randomly generated parameter values with something like
// absl::BitGen instead of FuzzTest because FuzzTest does not have much of an
// advantage over the simplicity of absl::BitGen in this case.
fuzztest::Domain<PackageAndTestParams> IrFuzzDomainWithParams(
int64_t param_set_count) {
return fuzztest::Map(
[](std::pair<std::shared_ptr<Package>,
std::vector<std::vector<std::string>>>
bytes_paramaterized_package) {
auto [p, bytes_param_sets] = bytes_paramaterized_package;
Function* f = p->GetFunction(IrTestBase::TestName()).value();
std::vector<std::vector<Value>> param_sets(bytes_param_sets.size());
// Iterate over the number of param sets.
for (int64_t i = 0; i < bytes_param_sets.size(); i += 1) {
// Iterate over the actual function parameters.
for (int64_t j = 0; j < bytes_param_sets[i].size(); j += 1) {
// Truncate the byte arrays to the bit width of the parameters.
int64_t bit_width = f->param(j)->BitCountOrDie();
Bits value_bits =
ChangeBytesBitWidth(bytes_param_sets[i][j], bit_width);
param_sets[i].push_back(Value(value_bits));
}
}
VLOG(3) << "2. Param Sets: " << StringifyParamSets(param_sets);
return PackageAndTestParams(p, param_sets);
},
IrFuzzDomainWithBytesParams(param_set_count));
}

} // namespace xls
17 changes: 16 additions & 1 deletion xls/fuzzer/ir_fuzzer/ir_fuzz_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,31 @@
#ifndef XLS_FUZZER_IR_FUZZER_IR_FUZZ_DOMAIN_H_
#define XLS_FUZZER_IR_FUZZER_IR_FUZZ_DOMAIN_H_

#include <memory>
#include <cstdint>

#include "xls/common/fuzzing/fuzztest.h"
#include "xls/ir/package.h"
#include "xls/ir/value.h"

// Contains functions that return IR fuzz test domains.

namespace xls {

// Stores a Package object which contains a function. param_sets contains
// multiple sets of parameter values for the function.
struct PackageAndTestParams {
std::shared_ptr<Package> p;
std::vector<std::vector<Value>> param_sets;
};

// These functions return a shared_ptr instead of a unique_ptr because the
// IrFuzzDomainWithBytesParams() function in the ir_fuzz_domain.cc file uses
// fuzztest::Just() which refuses to deal with move-only types.
// TODO: Implement a clone_ptr class that makes a deep copy of a
// unique_ptr to avoid returning a shared_ptr.
fuzztest::Domain<std::shared_ptr<Package>> IrFuzzDomain();
fuzztest::Domain<PackageAndTestParams> IrFuzzDomainWithParams(
int64_t param_set_count = 1);

} // namespace xls

Expand Down
6 changes: 5 additions & 1 deletion xls/fuzzer/ir_fuzzer/ir_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
#include "absl/log/log.h"
#include "xls/common/status/matchers.h"
#include "xls/fuzzer/ir_fuzzer/ir_fuzz_domain.h"
#include "xls/ir/function.h"
#include "xls/ir/function_builder.h"
#include "xls/ir/ir_test_base.h"
#include "xls/ir/node.h"
#include "xls/ir/nodes.h"
#include "xls/ir/package.h"
#include "xls/ir/verifier.h"

Expand All @@ -31,7 +35,7 @@ void VerifyIrFuzzPackage(std::shared_ptr<Package> p) {
XLS_ASSERT_OK(VerifyPackage(p.get()));
XLS_ASSERT_OK_AND_ASSIGN(Function * f,
p->GetFunction(IrTestBase::TestName()));
VLOG(3) << "3. IR:" << "\n" << f->DumpIr() << "\n";
VLOG(3) << "2. IR:" << "\n" << f->DumpIr() << "\n";
}
// Use of gtest FUZZ_TEST to randomly generate IR while being compatible with
// Google infrastructure. The IrFuzzTest function is called and represents the
Expand Down
Loading
0