fix: implement ABS as a builtin - #1849
Open
ghaith wants to merge 1 commit into
Open
Conversation
Calling ABS with an unsigned argument bound to a monomorph the standard library does not provide and crashed at runtime. ABS is now a builtin: signed integers lower to llvm.abs (is_int_min_poison = false), floats to llvm.fabs, and unsigned arguments are returned unchanged and report the new warning E150 because the call has no effect. The builtin takes precedence over the generic ABS declaration that standard library includes still ship; Index::get_builtin_function now considers every POU registered under a name instead of only the first. The stdlib ABS tests moved to lit fixtures since ABS is no longer implemented by the library. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Build Artifacts🐧 Linux
From workflow run 🪟 Windows
From workflow run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Calling
ABSwith an unsigned argument (USINT/UINT/UDINT/ULINT) resolved to a monomorph the standard library does not provide. The compiler emitted a bare external declaration, the shared object carried an undefined symbol, and the call crashed at runtime.ABSis now a compiler builtin covering the wholeANY_NUMdomain:llvm.absintrinsic withis_int_min_poison = false, soABS(INT_MIN)wraps toINT_MINexactly like the previous library implementationllvm.fabsABScall is almost always a mistake, e.g. an argument expression that already underflowedNotes for review
ABSdeclaration that standard library includes still ship (same generic-overload exemptionADDalready relies on).Index::get_builtin_functionnow checks every POU registered under a name instead of only the first, so builtin codegen dispatch no longer depends on index insertion order.Llvm::get_intrinsic_declarationis extracted frombuild_lifetime_markerand reused by the ABS codegen.ABS__DINTetc. still resolve to the library monomorphs and link as before (covered by lit).tests/lit/single/builtin/abs_signed.st/abs_floats.stsince ABS is no longer implemented by the library;abs_unsigned.standabs_unsigned_warning.stcover the unsigned runtime behavior and the E150 diagnostics end-to-end.Release/1.0.x counterpart with a minimal patch-lane approach: #1850
Test plan
cargo test --workspace./scripts/build.sh --lit(default +-Onone)🤖 Generated with Claude Code