Skip to content

Commit

Permalink
Add abseil formatters for:
Browse files Browse the repository at this point in the history
  - cstring
  - IR::Node
  - IR::ID

This improves the code itself as we no longer need to do explicit
.toString() / .string_view() calls. Also, this prints directly to
sink, providing some performance improvements.

Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Oct 28, 2024
1 parent 2605767 commit 40d0a66
Show file tree
Hide file tree
Showing 58 changed files with 534 additions and 541 deletions.
18 changes: 9 additions & 9 deletions backends/dpdk/control-plane/bfruntime_arch_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> {
}
auto *externType = p4info->add_externs();
externType->set_extern_type_id(static_cast<p4rt_id_t>(typeId));
externType->set_extern_type_name(typeName);
externType->set_extern_type_name(typeName.string_view());
return externType;
}

Expand All @@ -161,8 +161,8 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> {
auto *externInstance = externType->add_instances();
auto *pre = externInstance->mutable_preamble();
pre->set_id(symbols.getId(typeId, name));
pre->set_name(prefix(pipeName, name));
pre->set_alias(symbols.getAlias(name));
pre->set_name(prefix(pipeName, name).string_view());
pre->set_alias(symbols.getAlias(name).string_view());
Helpers::addAnnotations(pre, annotations);
Helpers::addDocumentation(pre, annotations);
externInstance->mutable_info()->PackFrom(message);
Expand Down Expand Up @@ -237,7 +237,7 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> {
auto pipeName = getBlockNamePrefix(tableBlock);
auto *pre = table->mutable_preamble();
if (pre->name() == tableDeclaration->controlPlaneName())
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
}

void addExternInstance(const P4RuntimeSymbolTableIface &symbols, p4configv1::P4Info *p4info,
Expand All @@ -261,39 +261,39 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> {
for (auto &extType : *p4info->mutable_action_profiles()) {
auto *pre = extType.mutable_preamble();
if (pre->name() == decl->controlPlaneName()) {
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
break;
}
}
} else if (externBlock->type->name == "ActionProfile") {
for (auto &extType : *p4info->mutable_action_profiles()) {
auto *pre = extType.mutable_preamble();
if (pre->name() == decl->controlPlaneName()) {
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
break;
}
}
} else if (externBlock->type->name == "Meter") {
for (auto &extType : *p4info->mutable_meters()) {
auto *pre = extType.mutable_preamble();
if (pre->name() == decl->controlPlaneName()) {
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
break;
}
}
} else if (externBlock->type->name == "Counter") {
for (auto &extType : *p4info->mutable_counters()) {
auto *pre = extType.mutable_preamble();
if (pre->name() == decl->controlPlaneName()) {
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
break;
}
}
} else if (externBlock->type->name == "Register") {
for (auto &extType : *p4info->mutable_registers()) {
auto *pre = extType.mutable_preamble();
if (pre->name() == decl->controlPlaneName()) {
pre->set_name(prefix(pipeName, pre->name()));
pre->set_name(prefix(pipeName, pre->name()).string_view());
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions backends/ebpf/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ bool CodeGenInspector::preorder(const IR::Constant *expression) {
cstring str = EBPFInitializerUtils::genHexStr(expression->value, width, expression);
builder->append("{ ");
for (size_t i = 0; i < str.size() / 2; ++i)
builder->appendFormat("0x%s, ", str.substr(2 * i, 2));
builder->appendFormat("0x%v, ", str.substr(2 * i, 2));
builder->append("}");

return false;
}

bool CodeGenInspector::preorder(const IR::StringLiteral *expression) {
builder->appendFormat("\"%s\"", expression->toString());
builder->appendFormat("\"%v\"", expression->toString());
return true;
}

Expand Down Expand Up @@ -491,7 +491,7 @@ void CodeGenInspector::emitAndConvertByteOrder(const IR::Expression *expr, cstri
loadSize = 64;
}
unsigned shift = loadSize - widthToEmit;
builder->appendFormat("%s(", emit);
builder->appendFormat("%v(", emit);
visit(expr);
if (shift != 0 && byte_order == "HOST") builder->appendFormat(" << %d", shift);
builder->append(")");
Expand Down
14 changes: 6 additions & 8 deletions backends/ebpf/ebpfControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ bool ControlBodyTranslator::preorder(const IR::MethodCallExpression *expression)
// Action arguments have been eliminated by the mid-end.
BUG_CHECK(expression->arguments->size() == 0, "%1%: unexpected arguments for action call",
expression);
cstring msg =
absl::StrFormat("Control: explicit calling action %s()", ac->action->name.name);
cstring msg = absl::StrFormat("Control: explicit calling action %v()", ac->action->name);
builder->target->emitTraceMessage(builder, msg.c_str());
visit(ac->action->body);
return false;
Expand All @@ -165,9 +164,9 @@ void ControlBodyTranslator::compileEmitField(const IR::Expression *expr, cstring
if (!swap.isNullOrEmpty()) {
builder->emitIndent();
visit(expr);
builder->appendFormat(".%s = %s(", field.c_str(), swap);
builder->appendFormat(".%v = %v(", field, swap);
visit(expr);
builder->appendFormat(".%s)", field.c_str());
builder->appendFormat(".%v)", field);
builder->endOfStatement(true);
}

Expand Down Expand Up @@ -304,7 +303,7 @@ void ControlBodyTranslator::processApply(const P4::ApplyMethod *method) {
auto table = control->getTable(method->object->getName().name);
BUG_CHECK(table != nullptr, "No table for %1%", method->expr);

msgStr = absl::StrFormat("Control: applying %s", method->object->getName().name);
msgStr = absl::StrFormat("Control: applying %v", method->object->getName());
builder->target->emitTraceMessage(builder, msgStr.c_str());

builder->emitIndent();
Expand Down Expand Up @@ -393,7 +392,7 @@ void ControlBodyTranslator::processApply(const P4::ApplyMethod *method) {
builder->blockEnd(true);
builder->blockEnd(true);

msgStr = absl::StrFormat("Control: %s applied", method->object->getName().name);
msgStr = absl::StrFormat("Control: %v applied", method->object->getName());
builder->target->emitTraceMessage(builder, msgStr.c_str());
}

Expand Down Expand Up @@ -595,8 +594,7 @@ void EBPFControl::emitDeclaration(CodeBuilder *builder, const IR::Declaration *d
// Therefore, this piece of code zero-initialize structures
// that might be used as keys.
builder->emitIndent();
builder->appendFormat("__builtin_memset((void *) &%s, 0, sizeof(",
vd->name.name);
builder->appendFormat("__builtin_memset((void *) &%v, 0, sizeof(", vd->name);
etype->declare(builder, cstring::empty, false);
builder->append("))");
builder->endOfStatement(true);
Expand Down
16 changes: 8 additions & 8 deletions backends/ebpf/ebpfDeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ void DeparserHdrEmitTranslator::emitField(CodeBuilder *builder, cstring field,
visit(hdrExpr);
builder->appendFormat(".%s", field.c_str());
builder->endOfStatement(true);
msgStr = absl::StrFormat("Deparser: emitting field %s=0x%%llx (%u bits)", field,
msgStr = absl::StrFormat("Deparser: emitting field %v=0x%%llx (%u bits)", field,
widthToEmit);
builder->target->emitTraceMessage(builder, msgStr.c_str(), 1, "tmp");
builder->blockEnd(true);
}
} else {
msgStr = absl::StrFormat("Deparser: emitting field %s (%u bits)", field, widthToEmit);
msgStr = absl::StrFormat("Deparser: emitting field %v (%u bits)", field, widthToEmit);
builder->target->emitTraceMessage(builder, msgStr.c_str());
}

Expand All @@ -229,9 +229,9 @@ void DeparserHdrEmitTranslator::emitField(CodeBuilder *builder, cstring field,
if (!swap.isNullOrEmpty()) {
builder->emitIndent();
visit(hdrExpr);
builder->appendFormat(".%s = %s(", field, swap);
builder->appendFormat(".%v = %v(", field, swap);
visit(hdrExpr);
builder->appendFormat(".%s", field);
builder->appendFormat(".%v", field);
if (shift != 0) builder->appendFormat(" << %d", shift);
builder->append(")");
builder->endOfStatement(true);
Expand All @@ -244,7 +244,7 @@ void DeparserHdrEmitTranslator::emitField(CodeBuilder *builder, cstring field,
builder->emitIndent();
builder->appendFormat("%s = ((char*)(&", program->byteVar.c_str());
visit(hdrExpr);
builder->appendFormat(".%s))[%d]", field, i);
builder->appendFormat(".%v))[%d]", field, i);
builder->endOfStatement(true);
unsigned freeBits = alignment != 0 ? (8 - alignment) : 8;
bitsInCurrentByte = left >= 8 ? 8 : left;
Expand Down Expand Up @@ -371,14 +371,14 @@ void EBPFDeparser::emit(CodeBuilder *builder) {
emitBufferAdjusts(builder);

builder->emitIndent();
builder->appendFormat("%s = %s;", program->packetStartVar,
builder->appendFormat("%v = %v;", program->packetStartVar,
builder->target->dataOffset(program->model.CPacketName.toString()));
builder->newline();
builder->emitIndent();
builder->appendFormat("%s = %s;", program->headerStartVar, program->packetStartVar);
builder->appendFormat("%v = %v;", program->headerStartVar, program->packetStartVar);
builder->newline();
builder->emitIndent();
builder->appendFormat("%s = %s;", program->packetEndVar,
builder->appendFormat("%v = %v;", program->packetEndVar,
builder->target->dataEnd(program->model.CPacketName.toString()));
builder->newline();

Expand Down
56 changes: 27 additions & 29 deletions backends/ebpf/ebpfParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ limitations under the License.
namespace P4::EBPF {

void StateTranslationVisitor::compileLookahead(const IR::Expression *destination) {
cstring msgStr = absl::StrFormat("Parser: lookahead for %s %s",
state->parser->typeMap->getType(destination)->toString(),
destination->toString());
cstring msgStr = absl::StrFormat("Parser: lookahead for %v %v",
state->parser->typeMap->getType(destination), destination);
builder->target->emitTraceMessage(builder, msgStr.c_str());

builder->emitIndent();
builder->blockStart();
builder->emitIndent();
builder->appendFormat("u8* %s_save = %s", state->parser->program->headerStartVar.c_str(),
state->parser->program->headerStartVar.c_str());
builder->appendFormat("u8* %v_save = %v", state->parser->program->headerStartVar,
state->parser->program->headerStartVar);
builder->endOfStatement(true);
compileExtract(destination);
builder->emitIndent();
builder->appendFormat("%s = %s_save", state->parser->program->headerStartVar.c_str(),
state->parser->program->headerStartVar.c_str());
builder->appendFormat("%v = %v_save", state->parser->program->headerStartVar,
state->parser->program->headerStartVar);
builder->endOfStatement(true);
builder->blockEnd(true);
}
Expand All @@ -49,7 +48,7 @@ void StateTranslationVisitor::compileAdvance(const P4::ExternMethod *extMethod)
if (cnst) {
cstring argStr = cstring::to_cstring(cnst->asUnsigned());
cstring offsetStr =
absl::StrFormat("%s - (u8*)%s + BYTES(%s)", state->parser->program->headerStartVar,
absl::StrFormat("%v - (u8*)%v + BYTES(%v)", state->parser->program->headerStartVar,
state->parser->program->packetStartVar, argStr);
builder->target->emitTraceMessage(builder,
"Parser (advance): check pkt_len=%%d < "
Expand All @@ -70,14 +69,14 @@ void StateTranslationVisitor::compileAdvance(const P4::ExternMethod *extMethod)
}

builder->emitIndent();
builder->appendFormat("%s += BYTES(", state->parser->program->headerStartVar.c_str());
builder->appendFormat("%v += BYTES(", state->parser->program->headerStartVar);
visit(argExpr);
builder->append(")");
builder->endOfStatement(true);

builder->emitIndent();
builder->appendFormat("if ((u8*)%s < %s) ", state->parser->program->packetEndVar.c_str(),
state->parser->program->headerStartVar.c_str());
builder->appendFormat("if ((u8*)%v < %v) ", state->parser->program->packetEndVar,
state->parser->program->headerStartVar);
builder->blockStart();

builder->target->emitTraceMessage(builder, "Parser: invalid packet (packet too short)");
Expand Down Expand Up @@ -111,12 +110,11 @@ void StateTranslationVisitor::compileVerify(const IR::MethodCallExpression *expr
builder->blockStart();

builder->emitIndent();
builder->appendFormat("%s = %s", state->parser->program->errorVar.c_str(),
errorMember->member.name);
builder->appendFormat("%v = %v", state->parser->program->errorVar, errorMember->member);
builder->endOfStatement(true);

cstring msg = absl::StrFormat("Verify: condition failed, parser_error=%%u (%s)",
errorMember->member.name);
cstring msg =
absl::StrFormat("Verify: condition failed, parser_error=%%u (%v)", errorMember->member);
builder->target->emitTraceMessage(builder, msg.c_str(), 1,
state->parser->program->errorVar.c_str());

Expand Down Expand Up @@ -156,8 +154,8 @@ bool StateTranslationVisitor::preorder(const IR::ParserState *parserState) {
builder->spc();
builder->blockStart();

cstring msgStr = absl::StrFormat("Parser: state %s (curr_offset=%%d)", parserState->name.name);
cstring offsetStr = absl::StrFormat("%s - (u8*)%s", state->parser->program->headerStartVar,
cstring msgStr = absl::StrFormat("Parser: state %v (curr_offset=%%d)", parserState->name);
cstring offsetStr = absl::StrFormat("%v - (u8*)%v", state->parser->program->headerStartVar,
state->parser->program->packetStartVar);
builder->target->emitTraceMessage(builder, msgStr.c_str(), 1, offsetStr.c_str());

Expand Down Expand Up @@ -235,7 +233,7 @@ bool StateTranslationVisitor::preorder(const IR::SelectCase *selectCase) {
builder->append(" != NULL)");
} else if (auto mask = selectCase->keyset->to<IR::Mask>()) {
if (scalar) {
builder->appendFormat("if ((%s", selectValue);
builder->appendFormat("if ((%v", selectValue);
builder->append(" & ");
visit(mask->right);
builder->append(") == (");
Expand All @@ -255,15 +253,15 @@ bool StateTranslationVisitor::preorder(const IR::SelectCase *selectCase) {
if (!first) {
builder->append(" && ");
}
builder->appendFormat("((%s[%u] & 0x%s)", selectValue, i, hex.substr(2 * i, 2));
builder->appendFormat("((%v[%u] & 0x%v)", selectValue, i, hex.substr(2 * i, 2));
builder->append(" == ");
builder->appendFormat("(%s & %s))", value.substr(2 * i, 2), hex.substr(2 * i, 2));
builder->appendFormat("(%v & %v))", value.substr(2 * i, 2), hex.substr(2 * i, 2));
first = false;
}
builder->append(") ");
}
} else {
builder->appendFormat("if (%s", selectValue);
builder->appendFormat("if (%v", selectValue);
builder->append(" == ");
visit(selectCase->keyset);
builder->append(")");
Expand All @@ -283,7 +281,7 @@ void StateTranslationVisitor::compileExtractField(const IR::Expression *expr,
cstring msgStr;
cstring fieldName = field->name.name;

msgStr = absl::StrFormat("Parser: extracting field %s", fieldName);
msgStr = absl::StrFormat("Parser: extracting field %v", fieldName);
builder->target->emitTraceMessage(builder, msgStr.c_str());

if (widthToExtract <= 64) {
Expand Down Expand Up @@ -391,13 +389,13 @@ void StateTranslationVisitor::compileExtractField(const IR::Expression *expr,
expr->to<IR::Member>()->expr->to<IR::PathExpression>()->path->name.name)) {
exprStr = exprStr.replace(".", "->");
}
cstring tmp = absl::StrFormat("(unsigned long long) %s.%s", exprStr, fieldName);
auto tmp = absl::StrFormat("(unsigned long long) %v.%v", exprStr, fieldName);

msgStr =
absl::StrFormat("Parser: extracted %s=0x%%llx (%u bits)", fieldName, widthToExtract);
absl::StrFormat("Parser: extracted %v=0x%%llx (%u bits)", fieldName, widthToExtract);
builder->target->emitTraceMessage(builder, msgStr.c_str(), 1, tmp.c_str());
} else {
msgStr = absl::StrFormat("Parser: extracted %s (%u bits)", fieldName, widthToExtract);
msgStr = absl::StrFormat("Parser: extracted %v (%u bits)", fieldName, widthToExtract);
builder->target->emitTraceMessage(builder, msgStr.c_str());
}
}
Expand All @@ -422,8 +420,8 @@ void StateTranslationVisitor::compileExtract(const IR::Expression *destination)

auto program = state->parser->program;

cstring offsetStr = absl::StrFormat("(%s - (u8*)%s) + BYTES(%s)", program->headerStartVar,
program->packetStartVar, cstring::to_cstring(width));
auto offsetStr = absl::StrFormat("(%v - (u8*)%v) + BYTES(%d)", program->headerStartVar,
program->packetStartVar, width);

builder->target->emitTraceMessage(builder, "Parser: check pkt_len=%d >= last_read_byte=%d", 2,
program->lengthVar.c_str(), offsetStr.c_str());
Expand Down Expand Up @@ -466,7 +464,7 @@ void StateTranslationVisitor::compileExtract(const IR::Expression *destination)
builder->newline();
builder->blockEnd(true);

msgStr = absl::StrFormat("Parser: extracting header %s", destination->toString());
msgStr = absl::StrFormat("Parser: extracting header %v", destination);
builder->target->emitTraceMessage(builder, msgStr.c_str());
builder->newline();

Expand Down Expand Up @@ -496,7 +494,7 @@ void StateTranslationVisitor::compileExtract(const IR::Expression *destination)
builder->appendFormat("%s += BYTES(%u);", program->headerStartVar.c_str(), width);
builder->newline();

msgStr = absl::StrFormat("Parser: extracted %s", destination->toString());
msgStr = absl::StrFormat("Parser: extracted %v", destination);
builder->target->emitTraceMessage(builder, msgStr.c_str());

builder->newline();
Expand Down
Loading

0 comments on commit 40d0a66

Please sign in to comment.