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

Add abs and make Oliver's example work #702

Merged
merged 11 commits into from
Jan 21, 2025
Merged

Add abs and make Oliver's example work #702

merged 11 commits into from
Jan 21, 2025

Conversation

ajpal
Copy link
Collaborator

@ajpal ajpal commented Jan 9, 2025

// ARGS: 20
fn unrelated_fn(input: i64, res: i64) -> i64 {
    return input / (res / 3);
}

fn other_unrelated_fn(input: i64, res: i64) -> i64 {
    return res / (input / 5);
}

fn main(input: i64) {
    let mut res: i64 = abs(input) * 2;

    if (input > 0) {
        res = res + abs(input) - input;
        res = unrelated_fn(input, res);
    } else {
        res = res + abs(input) + input;
        res = other_unrelated_fn(input, res);
    }

    println!("{}", res);
}

Output bril:

# ARGS: 20
@unrelated_fn(v0: int, v1: int): int {
.b2_:
  c3_: int = const 3;
  v4_: int = div v1 c3_;
  v5_: int = div v0 v4_;
  ret v5_;
}
@other_unrelated_fn(v0: int, v1: int): int {
.b2_:
  c3_: int = const 5;
  v4_: int = div v0 c3_;
  v5_: int = div v1 v4_;
  ret v5_;
}
@main(v0: int) {
.b1_:
  c2_: int = const 0;
  v3_: bool = gt v0 c2_;
  v4_: int = abs v0;
  c5_: int = const 2;
  v6_: int = mul c5_ v4_;
  br v3_ .b7_ .b8_;
.b7_:
  c9_: int = const 3;
  v10_: int = div v6_ c9_;
  v11_: int = div v0 v10_;
  v12_: int = id v11_;
  print v12_;
  ret;
  jmp .b13_;
.b8_:
  c14_: int = const 5;
  v15_: int = div v0 c14_;
  v16_: int = div v6_ v15_;
  v12_: int = id v16_;
  print v12_;
  ret;
.b13_:
}

SVGs
Before
image

After
image

@ajpal ajpal requested a review from oflatt January 9, 2025 21:37
Copy link
Member

@oflatt oflatt left a comment

Choose a reason for hiding this comment

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

Good but one of your rules seems unsound

@@ -214,6 +247,35 @@
((set (hi-bound lhs) (BoolB (bool-< la hb))))
:ruleset interval-analysis)

; Abs

; absolute value is non-negative
Copy link
Member

Choose a reason for hiding this comment

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

it's not true 😭

Copy link
Member

@oflatt oflatt left a comment

Choose a reason for hiding this comment

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

Looks good, you just have to fix CI
Look over the snapshots for our example carefully!

@ajpal ajpal merged commit 4569fbd into main Jan 21, 2025
4 checks passed
@ajpal ajpal deleted the ajpal-abs-ex branch January 21, 2025 17:58
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.

2 participants