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

StrengthReduction ignores side-effects in function calls #2221

Closed
fruffy opened this issue Mar 2, 2020 · 5 comments
Closed

StrengthReduction ignores side-effects in function calls #2221

fruffy opened this issue Mar 2, 2020 · 5 comments
Assignees
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.

Comments

@fruffy
Copy link
Collaborator

fruffy commented Mar 2, 2020

StrengthReduction (or PassRepeated in the latest version) transforms the following program:

bit<16> function_with_side_effect(inout bit<16> eth_type) {
    eth_type = 16w0x806;
    return 16w2;
}
control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
    apply {
        bit<16> dummy_var;
        dummy_var = 16w0 & function_with_side_effect(h.eth_hdr.eth_type);
        dummy_var = 16w0 * function_with_side_effect(h.eth_hdr.eth_type);
        dummy_var = 16w0 >> function_with_side_effect(h.eth_hdr.eth_type);
    }
}

into

bit<16> function_with_side_effect(inout bit<16> eth_type) {
    eth_type = 16w0x806;
    return 16w2;
}
control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
    apply {
        bit<16> dummy_var;
        dummy_var = 16w0;
        dummy_var = 16w0;
        dummy_var = 16w0;
    }
}

which ignores the side effects in the method-call expression.

zero_op_with_side_effects.p4.txt
zero_op_with_side_effects.stf.txt

@fruffy fruffy changed the title StrengthReduction ignores side-effects StrengthReduction ignores side-effects in function calls Mar 2, 2020
@mihaibudiu mihaibudiu added the bug This behavior is unintended and should be fixed. label Mar 2, 2020
@mihaibudiu mihaibudiu self-assigned this Mar 2, 2020
@mihaibudiu
Copy link
Contributor

@fruffy : is this stf file the expected output?
It looks wrong to me.

@fruffy
Copy link
Collaborator Author

fruffy commented Mar 20, 2020

Fixed. There was some bug with the tool I was using to generate these.

zero_op_with_side_effects.stf.txt

@mihaibudiu
Copy link
Contributor

you should probably write a stronger test; this one will still work if one of the calls is eliminated.
you can for example count the number of calls made.

mihaibudiu pushed a commit to mihaibudiu/p4c-clone that referenced this issue Mar 20, 2020
@mihaibudiu mihaibudiu added the fixed This topic is considered to be fixed. label Mar 20, 2020
@fruffy
Copy link
Collaborator Author

fruffy commented Mar 20, 2020

Is that list of optimizations even exhaustive? What other expressions could lead to this?
2221.p4.txt
2221.stf.txt

@mihaibudiu
Copy link
Contributor

Feel free to review my PR for fixing this. I hope I have covered all cases, but who knows?
In general, there may be an error if you look at one operand of a binary expression and remove the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants