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

Evaluate Comparison Expressions with Constant Operands during Compilation #1650

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Nirhar
Copy link

@Nirhar Nirhar commented Jun 7, 2024

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

Fixes #755

@Nirhar
Copy link
Author

Nirhar commented Jun 7, 2024

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 Nirhar force-pushed the main branch 2 times, most recently from 2ee062f to 881bb2e Compare June 7, 2024 07:50
src/sema/eval.rs Outdated Show resolved Hide resolved
src/sema/eval.rs Outdated Show resolved Hide resolved
src/sema/eval.rs Outdated Show resolved Hide resolved
src/sema/eval.rs Outdated Show resolved Hide resolved
src/sema/eval.rs Outdated Show resolved Hide resolved
src/sema/eval.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@seanyoung seanyoung left a comment

Choose a reason for hiding this comment

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

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.

src/sema/eval.rs Outdated Show resolved Hide resolved
@LucasSte
Copy link
Contributor

  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
Copy link
Author

Nirhar commented Jun 30, 2024

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?

…tion

This patch supports evaluation of comparison expressions with
constants as operands during compile time.
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.

Comparisons should support constants
3 participants