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

Constant-fold isValid calls when possible #3794

Merged
merged 1 commit into from
Dec 22, 2022

Conversation

mihaibudiu
Copy link
Contributor

@mihaibudiu mihaibudiu commented Dec 19, 2022

Signed-off-by: Mihai Budiu mbudiu@vmware.com
Fixes #3779

@mihaibudiu mihaibudiu requested a review from rst0git December 19, 2022 22:12
auto ne = si->components.getDeclaration<IR::NamedExpression>(e->member.name);
BUG_CHECK(ne != nullptr, "Could not find field %1% in initializer %2%", e->member, si);
return CloneConstants::clone(ne->expression, this);
} else if (expr->is<IR::InvalidHeader>() && e->member.name == IR::Type_Header::isValid) {
return e;
Copy link
Member

Choose a reason for hiding this comment

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

The "else if" conditions are unnecessary because return would prevent the execution of subsequent statements. Perhaps we can simplify this method to something like:

const IR::Node* DoConstantFolding::postorder(IR::Member* e) {
    if (!typesKnown) return e;
    auto orig = getOriginal<IR::Member>();
    auto type = typeMap->getType(orig->expr, true);
    auto origtype = typeMap->getType(orig);

    if (type->is<IR::Type_Stack>() && e->member == IR::Type_Stack::arraySize) {
        auto st = type->to<IR::Type_Stack>();
        auto size = st->getSize();
        return new IR::Constant(st->size->srcInfo, origtype, size);
    }

    auto expr = getConstant(e->expr);
    if (expr == nullptr) return e;

    auto structType = type->to<IR::Type_StructLike>();
    if (structType == nullptr) BUG("Expected a struct type, got %1%", type);

    if (auto list = expr->to<IR::ListExpression>()) {
        bool found = false;
        int index = 0;
        for (auto f : structType->fields) {
            if (f->name.name == e->member.name) {
                found = true;
                break;
            }
            index++;
        }

        if (!found) BUG("Could not find field %1% in type %2%", e->member, type);
        return CloneConstants::clone(list->components.at(index), this);
    }

    if (auto si = expr->to<IR::StructExpression>()) {
        if (type->is<IR::Type_Header>() && e->member.name == IR::Type_Header::isValid) return e;
        auto ne = si->components.getDeclaration<IR::NamedExpression>(e->member.name);
        BUG_CHECK(ne != nullptr, "Could not find field %1% in initializer %2%", e->member, si);
        return CloneConstants::clone(ne->expression, this);
    }

    if (expr->is<IR::InvalidHeader>() && e->member.name == IR::Type_Header::isValid) {
        return e;
    }

    BUG("Unexpected initializer: %1%", expr);
}

Copy link
Contributor

@jafingerhut jafingerhut left a comment

Choose a reason for hiding this comment

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

LGTM. As usual I am only reviewing the test program and its expected outputs, not the code changes within p4c.

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
@mihaibudiu
Copy link
Contributor Author

Thank you @rst0git, I will leave such improvements for a future PR.

@mihaibudiu mihaibudiu merged commit 5b3c7ff into p4lang:main Dec 22, 2022
@mihaibudiu mihaibudiu deleted the issue3779 branch December 22, 2022 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler bug when calling isValid on header as a list
3 participants