Skip to content
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

For-loop and for-in-loop support for P4Smith #4772

Merged
merged 41 commits into from
Jul 5, 2024

Conversation

zzmic
Copy link
Contributor

@zzmic zzmic commented Jul 1, 2024

This is a draft PR that aims to support for-loop and for-in-loop statement generations for P4Smith.

The approach I took to generate for-loop and for-in-loop statements is to intentionally replace auto *applyBlock = statementGenerator().genBlockStatement(false) with auto *applyBlock = new IR::BlockStatement(statOrDecls), where statOrDecls contains the generated for-loop and for-in-loop statements (that are pushed into statOrDecls before constructing the block statement) in backends/p4tools/modules/smith/targets/generic/target.cpp (https://github.com/zzmic/p4c/blame/8deba9128cd8ba9ce035909824e96e9c8cad0b3c/backends/p4tools/modules/smith/targets/generic/target.cpp#L114-L125). An issue with my current implementation is that although the for-loops and for-in-loops contain block statements, the block statements do not explicitly interact with the loop-control variables.

Currently, an abundant amount of TODOs (labeled as TODO(zzmic)s) is in several files under the backends/p4tools/modules/smith directory. Most are opinion-based questions that consider what standards should be adopted for each case, such as what the upper bounds of the for-loop/for-in-loop condition should be.

The time I tested the build based on the changes I've made (make check -j$(nproc)), it failed 512 cases, comprising 10% of the overall test cases. Most failing cases were located at testgen-p4c-bmv2-ptf with additional bmv2-ptf/testdata/p4_16_samples/ternary2-bmv2.p4 (Timeout) and smith-compile-core (Failed).

I've attached the (abridged) testing output (which includes the indications of all the failing tests), a sample P4Smith-generated program (with for-loop and for-in-loop statements), and the specific testing output of the case smith-compile-core (that I believe my changes directly account for the failure) to this draft PR.

failure_on_smith-compile-core.txt
make_check_abridged_output.txt
sample_p4smith_gen_prog_with_for_and_for_in_loops.txt

@fruffy fruffy added the p4tools Topics related to the P4Tools back end label Jul 1, 2024
@zzmic zzmic force-pushed the for-loop-support-for-smith branch from 3b9f4b8 to d4e6e20 Compare July 1, 2024 19:20
@zzmic
Copy link
Contributor Author

zzmic commented Jul 2, 2024

std::string generateLoopControlVariable() {
    // Generate a random string of length 1 (i.e., a single character variable name,
    // such as i, j, k, etc.).
    const std::string lowerCaseAlphabet = "abcdefghijklmnopqrstuvwxyz";
    const char randomChar = lowerCaseAlphabet[Utils::getRandInt(lowerCaseAlphabet.size() - 1)];
    return std::string(1, randomChar);
}

I know this might a weird question, but I was wondering why the code snippet above could trigger the following error?
p4smith: /usr/include/boost/random/uniform_int_distribution.hpp:337: boost::random::uniform_int_distribution<IntType>::uniform_int_distribution(IntType, IntType) [with IntType = long int]: Assertion `min_arg <= max_arg' failed.

Because when I backtraced using GDB, it gives me the following, which is directly related to Utils::getRandInt(lowerCaseAlphabet.size() - 1).

... in P4Tools::Utils::getRandInt (min=1, max=0)
    at /home/zzmic/p4c/backends/p4tools/common/lib/util.cpp:67

The error happened when I executed smith-test-for-loop-support-build/p4smith --target generic --arch core smith-test-for-loop-support-build_gen_prog1.p4 --seed 30.

@zzmic zzmic force-pushed the for-loop-support-for-smith branch from d4e6e20 to 1bf77eb Compare July 2, 2024 01:35
@fruffy
Copy link
Collaborator

fruffy commented Jul 2, 2024

I know this might a weird question, but I was wondering why the code snippet above could trigger the following error? ...

It looks like lowerCaseAlphabet is empty. What's the problem with getRandomString?

Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that tests were added! Do they work?

One initial round of comments.

backends/p4tools/modules/smith/common/probabilities.h Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/common/statements.cpp Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/common/statements.cpp Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/common/statements.cpp Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/common/statements.cpp Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/targets/generic/target.cpp Outdated Show resolved Hide resolved
backends/p4tools/modules/smith/util/util.cpp Outdated Show resolved Hide resolved
test/gtest/smith_for_in_loop_test.cpp Outdated Show resolved Hide resolved
test/gtest/smith_for_loop_test.cpp Outdated Show resolved Hide resolved
@zzmic zzmic force-pushed the for-loop-support-for-smith branch 2 times, most recently from 57798c9 to 09098e0 Compare July 2, 2024 18:02
@zzmic
Copy link
Contributor Author

zzmic commented Jul 2, 2024

I've been stumbling on how I could properly construct a P4Tools::P4Smith::StatementGenerator instance, which leads me to the obstacle of feeding a proper SmithTarget as the argument of the constructor (of P4Tools::P4Smith::StatementGenerator).

@fruffy
Copy link
Collaborator

fruffy commented Jul 2, 2024

I've been stumbling on how I could properly construct a P4Tools::P4Smith::StatementGenerator instance, which leads me to the obstacle of feeding a proper SmithTarget as the argument of the constructor (of P4Tools::P4Smith::StatementGenerator).

The first problem is that you are trying to build these in the top-level gtest directory. You need to put this in the smith-local test directory. Take a look at the testgen code to see how local gtests are instantiated.

Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! This is great progress.

zzmic added 18 commits July 4, 2024 17:11
…ools/modules/smith/common/statements.cpp and backends/p4tools/modules/smith/common/statements.h
…4tools/modules/smith/util/util.h to support more generic for-loop generations
…s in backends/p4tools/modules/smith/common/statements.cpp and backends/p4tools/modules/smith/common/statements.h
…bility should be further investigated and determined
…option) in backends/p4tools/modules/smith/util/util.cpp and backends/p4tools/modules/smith/util/util.h
…tatementOrDeclaration function in declarations.cpp, and add probability for for-loop statement(s) or declaration(s) (albeit the (precise) probability has yet to be determined)
…ns in backends/p4tools/modules/smith/util/util.cpp
…generations in backends/p4tools/modules/smith/common/statements.h and backends/p4tools/modules/smith/common/statements.cpp and ...

Delete (and add annotations to consider deleting more) the support I previously added for for-loop generations in backends/p4tools/modules/smith/common/statements.h and backends/p4tools/modules/smith/common/statements.h since they seem to be redundant and useless
…e the probability for assignments or method calls (STATEMENT_ASSIGNMENTORMETHODCALL)
…genForInLoopStatement in backends/p4tools/modules/smith/common/statements.h and backends/p4tools/modules/smith/common/statements.cpp, and ...

Add unit tests for for-loop statements in test/gtest/for_loop_test.cpp
… test/gtest/smith_for_loop_test.cpp, and add unit tests for for-in-loops in test/gtest/smith_for_in_loop_test.cpp
…n/statements.cpp to make for-loop and for-in-loop statement generations possible, and ...

Remove the changes I previously made in backends/p4tools/modules/smith/common/declarations.cpp since they are irrelevant
zzmic added 22 commits July 4, 2024 17:11
…ENT_FOR_in generations in backends/p4tools/modules/smith/common/probabilities.h (this modification should be reverted)
…ns in backends/p4tools/modules/smith/common/probabilities.h (this modification should be reverted)
…odules/smith/util/util.cpp, and intentionally insert for-loop and for-in-loop statements within the genBlockStatement function in backends/p4tools/modules/smith/common/statements.cpp (for testing purpose)
…false) with auto *applyBlock = new IR::BlockStatement(statOrDecls), where statOrDecls contains the generated for-loop and for-in-loop statements (that are pushed into statOrDecls before contrusting the block statement) in backends/p4tools/modules/smith/targets/generic/target.cpp

2. Fine-tune the local variables in the genForLoopStatement and genForInLoopStatement functions and temporarily add debugging statements to print out the randomly generated values of those variables (in backends/p4tools/modules/smith/common/statements.cpp)

3. Comment out the stale code in backends/p4tools/modules/smith/util/util.h and backends/p4tools/modules/smith/util/util.cpp
…ement functions in backends/p4tools/modules/smith/common/statements.cpp to 100 to prevent buffer overflow (further caring should be implemented in the near future)
… and backends/p4tools/modules/smith/util/util.h
…b.com>

I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 18b5c4b
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 2b75a1a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 191492c
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: f0113ed
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b2e51b7
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 3ea729c
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: aa13f35
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: c54a7a9
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: fb447a3
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: ca8f080
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 9027e6e
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 10bb6e0
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e1b682a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b8a00d3
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 0cbf8ce
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: f846b9b
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a62ac85
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 2a897ee
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 91da15e
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 4a270ee
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a298b0a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 7bb07bd
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 00aa376
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 49cbc61
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b201b92
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: ab75268
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 1bf77eb

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…ssBlock

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…n defintion in the main branch

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…b.com>

I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 230a1e5
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 9669bf3
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 52b1794
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 2fb9111
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 56aa342
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 4c05810
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 96fd439
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: fffb38a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e2eed93
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 6abf21b
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: fc34177
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 4608659
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e1b9f97
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 804fc1c
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 5ab308a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a608ea7
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b42b0a5
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 2964fdb
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e585383
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e5f4b52
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 9cbfc11
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: ee4bc61
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 7aa57a9
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: ca9feb1
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 15681dd
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: bcfbaa3
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 908d0c7

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…smith_for_in_loop_test.cpp and test/gtest/smith_for_loop_test.cpp

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…test.cpp and test/gtest/smith_for_loop_test.cpp account for

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…_loop_test.cpp from the top-level gtest directory

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…p to backends/p4tools/modules/smith/targets/generic/test/ but still need to investigate its correctness

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
…p4tools/modules/smith/targets/generic/test/ by referencing testgen

2. Add a comma in backends/p4tools/modules/smith/common/statements.cpp to fix the formatting

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
….cpp

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
@zzmic zzmic force-pushed the for-loop-support-for-smith branch from 2d48c85 to 76993d5 Compare July 4, 2024 21:11
…b.com>

I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 846ccc3
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b80b761
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b004a88
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: e78b3ff
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 0fb60a7
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 05039ce
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: d016970
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: c1501c6
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: bc7f396
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 057d347
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 2798935
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 6aa831d
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: c0d8cf0
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 15f735a
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 7e2f53d
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 5d9b79f
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 69497ab
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 32425f0
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 31b3c44
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a41ffdd
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 54eb56d
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: fa8da96
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: c09186e
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 867d3ce
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 8e1d827
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: cb92632
I, zzmic <100326374+zzmic@users.noreply.github.com>, hereby add my Signed-off-by to this commit: b6723f3

Signed-off-by: zzmic <100326374+zzmic@users.noreply.github.com>
@fruffy
Copy link
Collaborator

fruffy commented Jul 5, 2024

Great! We can merge this version now.

@fruffy fruffy added this pull request to the merge queue Jul 5, 2024
Merged via the queue into p4lang:main with commit a305732 Jul 5, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4tools Topics related to the P4Tools back end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants