cairo icon indicating copy to clipboard operation
cairo copied to clipboard

feat: Add option to disable optimizations

Open smonicas opened this issue 1 year ago • 4 comments
trafficstars

Feature Request

Describe the Feature Request My primary use case is for Caracal a static analysis tool that works with the sierra representation. The current problem is that optimized sierra makes analysis harder. For example if a function is not used it is removed while it could have been that the developer forgot to use it, another one is functions may seem unused while function inlining inlined them. Other possible use cases that comes to my mind are checking how much an optimization saves and fuzzing of optimized vs unoptimized code to check for possible issues during the optimization phase.

Describe Preferred Solution Have a flag to turn off all the optimizations or if possible allows to choose which optimizations to run. It should be available by CLI option and also by using the compiler as a libray. Something like replace_ids in CompilerConfig.

If the feature request is approved, would you be willing to submit a PR? (Help can be provided if you need assistance submitting a PR)

  • [x] Yes
  • [ ] No

smonicas avatar Mar 22 '24 15:03 smonicas

Any particular reason for the tool to run on sierra? That sounds like a clippy style tool that should be running as an analyzer.

orizi avatar Mar 22 '24 15:03 orizi

I'm not sure what you mean with "running as an analyzer". It builds a CFG where each basic block contains sierra instructions then it can do data-flow analysis and data dependency, The actual rules are implemented by analyzing the sierra instructions and if needed using the aforementioned analyses. It provides the possibility to write rules that would not be possible or more difficult with regexes or AST traversals. The tool is more security oriented than a linter.

smonicas avatar Mar 22 '24 17:03 smonicas

The scarb team is working on an ability for semantic analysis base diagnostics of cairo code. Sierra already loses quite a lot of information about the high level code (by design of course) - so analysis should happen on higher level code. Even if the too is security oriented - working on high level code makes more sense.

Specifically - the concept of "not adding function" is not an optimization phase at all - the entire process is based on getting the set of required functions, and adding only them and their dependencies.

orizi avatar Mar 22 '24 18:03 orizi

The scarb team is working on an ability for semantic analysis base diagnostics of cairo code.

What we’re working on will in no way be as powerful as it would be needed for this use case :(

We have some ideas of making a clippy-like linter but this is out of our capacity for several next quarters.

I will repeat Ori’s stance though - turning optimisations off won’t fix your problems. Seems like we have an XY problem here. You already are aware of a limitation that you cannot pinpoint source code that is failing the lint.

My suggestion would be for you to use Cairo semantic model from the compiler as a crate, and perhaps use scarb-eject to help you set compiler’s database.

mkaput avatar Mar 22 '24 20:03 mkaput

Closing this issue for now - as this doesn't seem to be the actual required feature. Feel free to reopen if there are specific suggestions for an action item.

orizi avatar May 08 '24 08:05 orizi

FWIW our tool Aegis also parses the generated Sierra text as an input for formal verification, and it is generally the right stage of the compilation process to grab it, not only because the syntax is so easy compared to high-level Cairo, but also because the generated Sierra is already in SSA form. We get around the "function not included" issue by adding fake call sites, but we'd also heavily profit from compiler flags that turn off some optimizations, notably also an option that would add #[inline(never)] globally to every function.

Would it be thinkable that a PR adding flags like these could be accepted?

javra avatar Jun 25 '24 15:06 javra