Skip to content

Commit

Permalink
Modify percentage allocations in backends/p4tools/modules/smith/commo…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
zzmic committed Jul 4, 2024
1 parent 69497ab commit 32425f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
32 changes: 7 additions & 25 deletions backends/p4tools/modules/smith/common/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,14 @@ IR::StatOrDecl *DeclarationGenerator::generateRandomStatementOrDeclaration(bool
return stmt;
}
auto genStmt = target().statementGenerator();
if (val == 2) {
auto *stmt = genStmt.genAssignmentOrMethodCallStatement(is_in_func);
if (stmt == nullptr) {
// it can happen that no statement can be generated
// for example in functions without writable values
// so declare a variable instead
return target().declarationGenerator().genVariableDeclaration();
}
return stmt;
}
// Add an option to generate a for-loop statement.
// TODO(zzmic): Verify whether this support is needed in this function (or more broadly, in this declaraion file).
// As option 2 (`val == 2`) is used for generating assignment or method call statements, I boldly assume that this support may be needed
// since we can't use it for generating for-loop statements.
if (val == 3) {
auto *stmt = genStmt.genForLoopStatement(is_in_func);
if (stmt == nullptr) {
// it can happen that no statement can be generated
// for example in functions without writable values
// so declare a variable instead
return target().declarationGenerator().genVariableDeclaration();
}
return stmt;
auto *stmt = genStmt.genAssignmentOrMethodCallStatement(is_in_func);
if (stmt == nullptr) {
// it can happen that no statement can be generated
// for example in functions without writable values
// so declare a variable instead
return target().declarationGenerator().genVariableDeclaration();
}
// Fallback.
return target().declarationGenerator().genVariableDeclaration();
return stmt;
}

IR::Annotations *DeclarationGenerator::genAnnotation() {
Expand Down
22 changes: 18 additions & 4 deletions backends/p4tools/modules/smith/common/statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ IR::Statement *StatementGenerator::genStatement(bool is_in_func) {
PCT.STATEMENT_IF,
PCT.STATEMENT_RETURN,
pctExit,
PCT.STATEMENT_BLOCK};
PCT.STATEMENT_BLOCK,
// Add the for-loop statement and the for-in-loop statement generation percentages.
PCT.STATEMENT_FOR,
PCT.STATEMENT_FOR_IN};
IR::Statement *stmt = nullptr;
bool useDefaultStmt = false;

Expand Down Expand Up @@ -68,11 +71,17 @@ IR::Statement *StatementGenerator::genStatement(bool is_in_func) {
// Add a new case for the for-loop statement generation.
case 6: {
stmt = genForLoopStatement(is_in_func);
if (stmt == nullptr) {
useDefaultStmt = true;
}
break;
}
// Add a new case for the for-in-loop statement generation.
case 7: {
stmt = genForInLoopStatement(is_in_func);
if (stmt == nullptr) {
useDefaultStmt = true;
}
break;
}
}
Expand Down Expand Up @@ -401,13 +410,15 @@ IR::ReturnStatement *StatementGenerator::genReturnStatement(const IR::Type *tp)
/// Generate a for-loop statement.
IR::ForStatement *StatementGenerator::genForLoopStatement(bool is_in_func) {
std::string loopVar = generateLoopControlVariable();
// TODO(zzmic): Determine the exact range of the bit field width and the upper bound.
int bitFieldWidth = Utils::getRandInt(1, 64);
int upperBound = Utils::getRandInt(1, 100);

// Create the IR nodes for the for-loop components
// Create the IR nodes for the for-loop components.
auto *initExpr = new IR::Declaration_Variable(
loopVar,
IR::ID(loopVar),
IR::Type_Bits::get(bitFieldWidth),
// TODO(zzmic): Perhaps randomize the initial value.
new IR::Constant(IR::Type_Bits::get(bitFieldWidth), 0)
);
auto *condExpr = new IR::Lss(
Expand All @@ -420,6 +431,7 @@ IR::ForStatement *StatementGenerator::genForLoopStatement(bool is_in_func) {
new IR::Add(
IR::Type_Bits::get(bitFieldWidth),
new IR::PathExpression(loopVar),
// TODO(zzmic): Perhaps randomize the increment value.
new IR::Constant(IR::Type_Bits::get(bitFieldWidth), 1)
)
);
Expand All @@ -446,14 +458,16 @@ IR::ForStatement *StatementGenerator::genForLoopStatement(bool is_in_func) {
/// Generate a for-in-loop statement.
IR::ForInStatement *StatementGenerator::genForInLoopStatement(bool is_in_func) {
std::string loopVar = generateLoopControlVariable();
// TODO(zzmic): Determine the exact range of the bit field width, the lower bound, and the upper bound.
int bitFieldWidth = Utils::getRandInt(1, 64);
int lowerBound = Utils::getRandInt(0, 50);
int upperBound = Utils::getRandInt(50, 100);

// Create the IR nodes for the for-in-loop component expressions.
auto declVar = new IR::Declaration_Variable(
loopVar,
IR::ID(loopVar),
IR::Type_Bits::get(bitFieldWidth),
// TODO(zzmic): Perhaps randomize the initial value.
new IR::Constant(IR::Type_Bits::get(bitFieldWidth), 0)
);
auto collectionExpr = new IR::Range(
Expand Down

0 comments on commit 32425f0

Please sign in to comment.