solang icon indicating copy to clipboard operation
solang copied to clipboard

Evaluate Comparison Expressions with Constant Operands during Compilation

Open Nirhar opened this issue 1 year ago • 3 comments

This patch supports evaluation of comparison expressions with constants as operands during compile time.

Fixes #755

Nirhar avatar Jun 07 '24 07:06 Nirhar

As a first time contributor to this project, I'd like to ask some questions:

  1. How do I add tests for my patch?
  2. Is there any formatting rules that I have to follow, or do we use any tools like clang-format?

I am new to rust, so if you find anything that goes against conventional programming style in rust, please let me know!

Nirhar avatar Jun 07 '24 07:06 Nirhar

  1. Is there any formatting rules that I have to follow, or do we use any tools like clang-format?

Have you checked our contributing guide https://solang.readthedocs.io/en/latest/contributing.html?

LucasSte avatar Jun 10 '24 11:06 LucasSte

One way to write a test for this is to add test to tests/codegen_tests/.. which does something like the following:

contract C {
     function foo() public returns (bool) {
           int a = 100;
           
           return a < 200;
     }
     
     function bar() public returns (bool r) {
           int a = 100;
           int b = 200;
           
           r = a < b;
     }
}

Make sure the test ensures that the code generation constant folds the expressions and generates false for both functions.

I've added a similar test case as you've suggested, and when I try to execute it as:

<path-to-solang> compile --target polkadot --emit cfg

I find ty:bool %r = (signed less int256 100 < int256 200) which suggests to me that my optimization did not kick in. I'm not sure if cfg is the right place to investigate the results of optimizations. I also tried to emit llvm-ir to inspect (with --emit llvm-ir), however, nothing was printed to the console when I do so. Can you please suggest the right IR format where I should be able to see the results of my code changes?

Nirhar avatar Jun 30 '24 06:06 Nirhar

PR has not been changed for as long time, closing.

seanyoung avatar Feb 03 '25 11:02 seanyoung