Skip to content

Commit

Permalink
Use llvm.abs for Int.absolute
Browse files Browse the repository at this point in the history
This removes the need for branching and should result in slightly more
efficient code. As part of this change, Int.absolute no longer panics
when used with int.MIN and instead returns int.MIN itself.

Changelog: performance
  • Loading branch information
yorickpeterse committed Aug 10, 2024
1 parent 448a6a8 commit 00a0d18
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler/src/llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ impl<'ctx> Builder<'ctx> {
self.inner.build_ptr_to_int(value, self.context.i64_type(), "").unwrap()
}

pub(crate) fn bool_literal(&self, value: bool) -> IntValue<'ctx> {
self.context.bool_type().const_int(value as u64, false)
}

pub(crate) fn u8_literal(&self, value: u8) -> IntValue<'ctx> {
self.context.i8_type().const_int(value as u64, false)
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/src/llvm/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,22 @@ impl<'shared, 'module, 'ctx> LowerMethod<'shared, 'module, 'ctx> {

self.builder.store(reg_var, res);
}
BuiltinFunction::IntAbsolute => {
let reg_var = self.variables[&ins.register];
let val_var = self.variables[&ins.arguments[0]];
let val = self.builder.load_int(val_var);
let fun = self.module.intrinsic(
"llvm.abs",
&[self.builder.context.i64_type().into()],
);
let no_poison = self.builder.bool_literal(false);
let res = self
.builder
.call(fun, &[val.into(), no_poison.into()])
.into_int_value();

self.builder.store(reg_var, res);
}
BuiltinFunction::Panic => {
let val_var = self.variables[&ins.arguments[0]];
let val = self.builder.load_untyped_pointer(val_var);
Expand Down
10 changes: 7 additions & 3 deletions std/src/std/int.inko
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,18 @@ class builtin Int {

# Returns the absolute value of `self`.
#
# If `self` is equal to `std.int.MIN`, then the returned value is also
# `std.int.MIN`.
#
# # Examples
#
# ```inko
# -4.absolute # => 4
# 4.absolute # => 4
# -4.absolute # => 4
# 4.absolute # => 4
# -9_223_372_036_854_775808.absolute # => -9_223_372_036_854_775808
# ```
fn pub absolute -> Int {
if self < 0 { 0 - self } else { clone }
_INKO.int_absolute(self)
}

# Returns a value with the opposite sign of `self`.
Expand Down
1 change: 1 addition & 0 deletions std/test/std/test_int.inko
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn pub tests(t: mut Tests) {
t.equal(1.absolute, 1)
t.equal(-1.absolute, 1)
t.equal(-9223372036854775807.absolute, 9223372036854775807)
t.equal(-9_223_372_036_854_775_808.absolute, -9_223_372_036_854_775_808)
})

t.test('Int.opposite', fn (t) {
Expand Down
4 changes: 4 additions & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,7 @@ pub enum BuiltinFunction {
IntCheckedMul,
IntCheckedSub,
IntSwapBytes,
IntAbsolute,
}

impl BuiltinFunction {
Expand Down Expand Up @@ -1995,6 +1996,7 @@ impl BuiltinFunction {
BuiltinFunction::FloatRound,
BuiltinFunction::FloatPowi,
BuiltinFunction::IntSwapBytes,
BuiltinFunction::IntAbsolute,
]
.into_iter()
.fold(HashMap::new(), |mut map, func| {
Expand Down Expand Up @@ -2051,6 +2053,7 @@ impl BuiltinFunction {
BuiltinFunction::FloatRound => "float_round",
BuiltinFunction::FloatPowi => "float_powi",
BuiltinFunction::IntSwapBytes => "int_swap_bytes",
BuiltinFunction::IntAbsolute => "int_absolute",
}
}

Expand Down Expand Up @@ -2110,6 +2113,7 @@ impl BuiltinFunction {
BuiltinFunction::FloatRound => TypeRef::float(),
BuiltinFunction::FloatPowi => TypeRef::float(),
BuiltinFunction::IntSwapBytes => TypeRef::int(),
BuiltinFunction::IntAbsolute => TypeRef::int(),
}
}
}
Expand Down

0 comments on commit 00a0d18

Please sign in to comment.