hyperledger / solang

Solidity Compiler for Solana and Polkadot
https://solang.readthedocs.io/
Apache License 2.0
1.22k stars 207 forks source link

Evaluate Comparison Expressions with Constant Operands during Compilation #1650

Open Nirhar opened 1 month ago

Nirhar commented 1 month ago

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

Fixes #755

Nirhar commented 1 month ago

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!

LucasSte commented 4 weeks ago
  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?

Nirhar commented 1 week ago

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?