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

[Draft] - Align key components to the nearest 8-bit size #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
236 changes: 191 additions & 45 deletions backends/ebpf/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ void CodeGenInspector::emitAssignStatement(const IR::Type *ltype, const IR::Expr
width = scalar->implementationWidthInBits();
memcpy = !EBPFScalarType::generatesScalar(width);
}

if (builder->target->name == "P4TC") {
emitTCAssignmentEndianessConversion(lexpr, rexpr, lpath, memcpy, scalar->bytesRequired());
return;
}
builder->emitIndent();
if (memcpy) {
builder->append("__builtin_memcpy(&");
Expand All @@ -373,11 +376,7 @@ void CodeGenInspector::emitAssignStatement(const IR::Type *ltype, const IR::Expr
builder->append(lpath);
}
builder->append(" = ");
if (builder->target->name == "P4TC") {
emitTCAssignmentEndianessConversion(lexpr, rexpr);
} else {
visit(rexpr);
}
visit(rexpr);
}
builder->endOfStatement();
}
Expand Down Expand Up @@ -492,7 +491,9 @@ void CodeGenInspector::emitAndConvertByteOrder(const IR::Expression *expr, cstri
}
unsigned shift = loadSize - widthToEmit;
builder->appendFormat("%s(", emit);
visit(expr);
bool primitive = isPrimitive(expr);
if (primitive) { visit(expr);
} else { getBitAlignment(expr); }
if (shift != 0 && byte_order == "HOST") builder->appendFormat(" << %d", shift);
builder->append(")");
}
Expand All @@ -512,7 +513,9 @@ void CodeGenInspector::emitTCBinaryOperation(const IR::Operation_Binary *b, bool
rByteOrder = tcTarget->getByteOrder(typeMap, action, rexpr);
}
if (lByteOrder == rByteOrder) {
visit(lexpr);
bool primitive = isPrimitive(lexpr);
if (primitive) { visit(lexpr);
} else { getBitAlignment(lexpr); }
if (isScalar) {
builder->spc();
builder->append(stringop);
Expand All @@ -521,7 +524,9 @@ void CodeGenInspector::emitTCBinaryOperation(const IR::Operation_Binary *b, bool
builder->append(", &");
}
if (!b->is<IR::Operation_Relation>()) expressionPrecedence = b->getPrecedence() + 1;
visit(rexpr);
primitive = isPrimitive(rexpr);
if (primitive) { visit(rexpr);
} else { getBitAlignment(rexpr); }
return;
}
if (lByteOrder == "NETWORK") {
Expand All @@ -542,14 +547,18 @@ void CodeGenInspector::emitTCBinaryOperation(const IR::Operation_Binary *b, bool
builder->append(", &");
}
if (!b->is<IR::Operation_Relation>()) expressionPrecedence = b->getPrecedence() + 1;
visit(rexpr);
bool primitive = isPrimitive(rexpr);
if (primitive) { visit(rexpr);
} else { getBitAlignment(rexpr); }
return;
} else if (rByteOrder == "NETWORK") {
// ConvertRight
auto ftype = typeMap->getType(rexpr);
auto et = EBPFTypeFactory::instance->create(ftype);
unsigned width = dynamic_cast<IHasWidth *>(et)->widthInBits();
visit(lexpr);
bool primitive = isPrimitive(lexpr);
if (primitive) { visit(lexpr);
} else { getBitAlignment(lexpr); }
if (isScalar) {
builder->spc();
builder->append(stringop);
Expand All @@ -568,46 +577,183 @@ void CodeGenInspector::emitTCBinaryOperation(const IR::Operation_Binary *b, bool
}

void CodeGenInspector::emitTCAssignmentEndianessConversion(const IR::Expression *lexpr,
const IR::Expression *rexpr) {
auto action = findContext<IR::P4Action>();
auto b = dynamic_cast<const P4TCTarget *>(builder->target);
cstring lByteOrder = "HOST"_cs, rByteOrder = "HOST"_cs;
if (lexpr) {
lByteOrder = b->getByteOrder(typeMap, action, lexpr);
const IR::Expression *rexpr,
cstring lpath, bool memcpy,
unsigned width) {
builder->emitIndent();
if (memcpy) {
builder->append("__builtin_memcpy(&");
if (lexpr != nullptr) {
visit(lexpr);
} else {
builder->append(lpath);
}
builder->append(", &");
if (rexpr->is<IR::Constant>()) {
builder->appendFormat("(u8[%u])", width);
}
visit(rexpr);
builder->appendFormat(", %d)", width);
} else {
if (lexpr != nullptr) {
bool primitive = isPrimitive(lexpr);
if (primitive) {
visit(lexpr);
} else {
storeBitAlignment(lexpr, rexpr);
return;
}
} else {
builder->append(lpath);
}
builder->append(" = ");
auto action = findContext<IR::P4Action>();
auto b = dynamic_cast<const P4TCTarget *>(builder->target);
cstring lByteOrder = "HOST"_cs, rByteOrder = "HOST"_cs;
if (lexpr) {
lByteOrder = b->getByteOrder(typeMap, action, lexpr);
}
if (rexpr) {
rByteOrder = b->getByteOrder(typeMap, action, rexpr);
}
auto ftype = typeMap->getType(rexpr);
auto et = EBPFTypeFactory::instance->create(ftype);
unsigned width = dynamic_cast<IHasWidth *>(et)->widthInBits();
// TODO:
if (lByteOrder == rByteOrder) {
bool primitive = isPrimitive(rexpr);
if (primitive) { visit(rexpr);
} else { getBitAlignment(rexpr); }
return;
}
if (width <= 8) {
visit(rexpr);
return;
}
if (rByteOrder == "NETWORK") {
// If left side of assignment is not annotated field i.e host endian and right expression
// is annotated field i.e network endian, we need to convert rexp to host order.
// Example -
// select_0 = hdr.ipv4.diffserv
// select_0 = bntoh(hdr.ipv4.diffserv)
//
emitAndConvertByteOrder(rexpr, "HOST"_cs);
}
if (lByteOrder == "NETWORK") {
// If left side of assignment is annotated field i.e network endian, we need to convert
// right expression to network order.
// Example -
// hdr.opv4.diffserv = 0x1;
// hdr.opv4.diffserv = bhton(0x1)
//
emitAndConvertByteOrder(rexpr, "NETWORK"_cs);
}
}
if (rexpr) {
rByteOrder = b->getByteOrder(typeMap, action, rexpr);
builder->endOfStatement();
return;
}

bool CodeGenInspector::isPrimitive(const IR::Expression* expression) {
if (!expression->is<IR::Member>()) { return true; }
auto ftype = typeMap->getType(expression);
if (!ftype) { return true; }
auto tcTarget = dynamic_cast<const P4TCTarget *>(builder->target);
auto ebpfType = EBPFTypeFactory::instance->create(ftype);
EBPFScalarType *scalar = nullptr;
if (ebpfType->is<EBPFScalarType>()) {
scalar = ebpfType->to<EBPFScalarType>();
bool isPrimitive = tcTarget->isPrimitiveByteAligned(scalar->implementationWidthInBits());
if (!isPrimitive) {
return false;
}
}
if (lByteOrder == rByteOrder) {
visit(rexpr);
return true;
}

void CodeGenInspector::getBitAlignment(const IR::Expression* expression) {
auto ftype = typeMap->getType(expression);
if (!ftype) { return; }
auto ebpfType = EBPFTypeFactory::instance->create(ftype);
EBPFScalarType *scalar = nullptr;
if (ebpfType->is<EBPFScalarType>()) {
scalar = ebpfType->to<EBPFScalarType>();
if (scalar->implementationWidthInBits() < 32) {
builder->append("getPrimitive32(");
visit(expression);
builder->append(", ");
} else {
builder->append("getPrimitive64(");
visit(expression);
builder->appendFormat(", %d", scalar->implementationWidthInBits());
}
builder->append(")");
return;
}
auto ftype = typeMap->getType(rexpr);
auto et = EBPFTypeFactory::instance->create(ftype);
unsigned width = dynamic_cast<IHasWidth *>(et)->widthInBits();
if (width <= 8) {
visit(rexpr);
return;
}

void CodeGenInspector::storeBitAlignment(const IR::Expression* lexpr, const IR::Expression* rexpr) {
auto tcTarget = dynamic_cast<const P4TCTarget *>(builder->target);
auto ftype = typeMap->getType(lexpr);
if (!ftype) { return;}
auto ebpfType = EBPFTypeFactory::instance->create(ftype);
EBPFScalarType *scalar = nullptr;
if (ebpfType->is<EBPFScalarType>()) {
scalar = ebpfType->to<EBPFScalarType>();
if (scalar->implementationWidthInBits() < 32) {
builder->append("storePrimitive32(&");
visit(lexpr);
builder->append(", ");
} else {
builder->append("storePrimitive64(&");
visit(lexpr);
builder->appendFormat(", %d, (" , scalar->implementationWidthInBits());
}
auto action = findContext<IR::P4Action>();
cstring lByteOrder = "HOST"_cs, rByteOrder = "HOST"_cs;
if (lexpr) {
lByteOrder = tcTarget->getByteOrder(typeMap, action, lexpr);
}
if (rexpr) {
rByteOrder = tcTarget->getByteOrder(typeMap, action, rexpr);
}
auto ftype = typeMap->getType(rexpr);
auto et = EBPFTypeFactory::instance->create(ftype);
unsigned width = dynamic_cast<IHasWidth *>(et)->widthInBits();
// TODO:
if (lByteOrder == rByteOrder) {
bool primitive = isPrimitive(rexpr);
if (primitive) { visit(rexpr);
} else { getBitAlignment(rexpr); }
builder->append("));");
return;
}
if (width <= 8) {
visit(rexpr);
builder->append("));");
return;
}
if (rByteOrder == "NETWORK") {
// If left side of assignment is not annotated field i.e host endian and right expression
// is annotated field i.e network endian, we need to convert rexp to host order.
// Example -
// select_0 = hdr.ipv4.diffserv
// select_0 = bntoh(hdr.ipv4.diffserv)
//
emitAndConvertByteOrder(rexpr, "HOST"_cs);
}
if (lByteOrder == "NETWORK") {
// If left side of assignment is annotated field i.e network endian, we need to convert
// right expression to network order.
// Example -
// hdr.opv4.diffserv = 0x1;
// hdr.opv4.diffserv = bhton(0x1)
//
emitAndConvertByteOrder(rexpr, "NETWORK"_cs);
}
builder->append("));");
return;
}
if (rByteOrder == "NETWORK") {
// If left side of assignment is not annotated field i.e host endian and right expression
// is annotated field i.e network endian, we need to convert rexp to host order.
// Example -
// select_0 = hdr.ipv4.diffserv
// select_0 = bntoh(hdr.ipv4.diffserv)
//
emitAndConvertByteOrder(rexpr, "HOST"_cs);
}
if (lByteOrder == "NETWORK") {
// If left side of assignment is annotated field i.e network endian, we need to convert
// right expression to network order.
// Example -
// hdr.opv4.diffserv = 0x1;
// hdr.opv4.diffserv = bhton(0x1)
//
emitAndConvertByteOrder(rexpr, "NETWORK"_cs);
}

return;
}

Expand Down
6 changes: 5 additions & 1 deletion backends/ebpf/codeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ class CodeGenInspector : public Inspector {
void emitAndConvertByteOrder(const IR::Expression *expr, cstring byte_order);
void emitTCBinaryOperation(const IR::Operation_Binary *b, bool isScalar);
void emitTCAssignmentEndianessConversion(const IR::Expression *lexpr,
const IR::Expression *rexpr);
const IR::Expression *rexpr,
cstring lpath, bool memcpy, unsigned width);
void getBitAlignment(const IR::Expression* expression);
bool isPrimitive(const IR::Expression* expression);
void storeBitAlignment(const IR::Expression* lexpr, const IR::Expression* rexpr);
};

class EBPFInitializerUtils {
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/ebpfBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void emitFilterModel(const EbpfOptions &options, Target *target, const IR::Tople
CodeBuilder c(target);
CodeBuilder h(target);

EBPFTypeFactory::createFactory(typeMap);
EBPFTypeFactory::createFactory(typeMap, false);
auto ebpfprog = new EBPFProgram(options, toplevel->getProgram(), refMap, typeMap, toplevel);
if (!ebpfprog->build()) return;

Expand Down
62 changes: 61 additions & 1 deletion backends/ebpf/ebpfType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
namespace P4::EBPF {

EBPFTypeFactory *EBPFTypeFactory::instance;
bool EBPFTypeFactory::isTC;

EBPFType *EBPFTypeFactory::create(const IR::Type *type) {
CHECK_NULL(type);
Expand All @@ -27,7 +28,11 @@ EBPFType *EBPFTypeFactory::create(const IR::Type *type) {
if (type->is<IR::Type_Boolean>()) {
result = new EBPFBoolType();
} else if (auto bt = type->to<IR::Type_Bits>()) {
result = new EBPFScalarType(bt);
if (EBPFTypeFactory::isTC) {
result = new EBPFScalarTypePNA(bt);
} else {
result = new EBPFScalarType(bt);
}
} else if (auto st = type->to<IR::Type_StructLike>()) {
result = new EBPFStructType(st);
} else if (auto tt = type->to<IR::Type_Typedef>()) {
Expand Down Expand Up @@ -384,4 +389,59 @@ void EBPFMethodDeclaration::emit(CodeBuilder *builder) {
builder->newline();
}

unsigned EBPFScalarTypePNA::alignment() const {
if (width <= 8)
return 1;
else if (width <= 16)
return 2;
else if (width <= 24)
return 1; // compiled as u8*
else if (width <= 32)
return 4;
else if (width <= 56)
return 1; // compiled as u8*
else if (width <= 64)
return 8;
else
// compiled as u8*
return 1;
}

void EBPFScalarTypePNA::declare(CodeBuilder *builder, cstring id, bool asPointer) {
if (generatesScalar(width) && isPrimitiveByteAligned == true) {
emit(builder);
if (asPointer) builder->append("*");
builder->spc();
builder->append(id);
} else {
if (asPointer)
builder->appendFormat("u8* %s", id.c_str());
else
builder->appendFormat("u8 %s[%d]", id.c_str(), bytesRequired());
}
}

void EBPFScalarTypePNA::declareInit(CodeBuilder *builder, cstring id, bool asPointer) {
if (generatesScalar(width) && isPrimitiveByteAligned == true ) {
emit(builder);
if (asPointer) builder->append("*");
builder->spc();
id = id + cstring(" = 0");
builder->append(id);
} else {
if (asPointer)
builder->appendFormat("u8* %s = NULL", id.c_str());
else
builder->appendFormat("u8 %s[%d] = {0}", id.c_str(), bytesRequired());
}
}

void EBPFScalarTypePNA::emitInitializer(CodeBuilder *builder) {
if (generatesScalar(width) && isPrimitiveByteAligned == true) {
builder->append("0");
} else {
builder->append("{ 0 }");
}
}

} // namespace P4::EBPF
Loading
Loading