Skip to content

intrinsics: Implement arbitrary-width integer intrinsics in C#61399

Merged
topolarity merged 3 commits intoJuliaLang:masterfrom
topolarity:ct/intrinsics-no-LLVM
Mar 31, 2026
Merged

intrinsics: Implement arbitrary-width integer intrinsics in C#61399
topolarity merged 3 commits intoJuliaLang:masterfrom
topolarity:ct/intrinsics-no-LLVM

Conversation

@topolarity
Copy link
Copy Markdown
Member

@topolarity topolarity commented Mar 24, 2026

@gbaraldi 's work in #61292 makes a big step towards removing our dependency on LLVMSupport in libjulia-internal (saving on size for --trim applications). This PR takes another step by removing our dependency on LLVM for our arbitrary-precision integer intrinsics.

The main performance trade-off versus the LLVM implementation is that this implements "schoolbook" multiplication / division, rather than accelerated algorithms (I believe LLVM provides Knuth's "Algorithm D"). These intrinsics are used only rarely as a runtime / interpreted fallback to native execution of these intrinsics via LLVM codegen, so I doubt these implementations are performance-sensitive in practice.

This PR is significantly generated by AI 🤖. Largely one-shot by Claude, although I had to guide it to merge overlapping implementations and consider sign bits (edit: and rounding) carefully in a couple of places. Tested locally with a combination of hand-written, AI-generated, and machine-generated tests (a subset of these will be added to the PR soon).

Resolves #61421, Resolves #61419, Resolves #61423

@topolarity topolarity added the needs tests Unit tests are required for this change label Mar 24, 2026
@topolarity topolarity changed the title intrinsics: Implement arbitrary-precision intrinsics in C intrinsics: Implement arbitrary-width integer intrinsics in C Mar 24, 2026
@topolarity topolarity force-pushed the ct/intrinsics-no-LLVM branch 4 times, most recently from 105f048 to be217e3 Compare March 27, 2026 20:48
@topolarity topolarity removed the needs tests Unit tests are required for this change label Mar 27, 2026
@topolarity topolarity force-pushed the ct/intrinsics-no-LLVM branch 2 times, most recently from 92b5f92 to 4d0195e Compare March 27, 2026 22:04
@topolarity
Copy link
Copy Markdown
Member Author

topolarity commented Mar 27, 2026

I did a barrage of testing today, primarily externally using Python and also internally by comparing to LLVM / codegen. This seems to be a net improvement in terms of bugs now. Certainly it is in terms of test coverage. I will probably add a few more tests on Monday, but this should be nearly ready to go.

@gbaraldi
Copy link
Copy Markdown
Member

How are we guaranteed to go through the intrinsics on your tests?

@topolarity
Copy link
Copy Markdown
Member Author

topolarity commented Mar 27, 2026

How are we guaranteed to go through the intrinsics on your tests?

The invokelatest means that this will get routed to the runtime intrinsic implementation instead of codegen, while the other case is compiled normally.

We do have a bit of dispatching in runtime_intrinsics.c that this doesn't work around, but that only applies to "standard" width types and means that those end up testing another implementation.

@topolarity
Copy link
Copy Markdown
Member Author

i686 failures due to #61436

@topolarity topolarity force-pushed the ct/intrinsics-no-LLVM branch from 4d0195e to e317048 Compare March 30, 2026 20:24
topolarity and others added 3 commits March 31, 2026 09:17
These are one of the remaining reasons that we have to link against
LLVMSupport in `libjulia-internal`, and it turns out that it's not that
difficult to provide a replacement implementation for this.

Largely one-shot by Claude, although I had to guide it to merge
overlapping implementations and consider sign bits carefully in a couple
of places.

The main performance trade-off versus the LLVM implementation is that
this implements "schoolbook" multiplication / division, rather than
accelerated algorithms (I believe LLVM provides Knuth's "Algorithm D").

However these intrinsics are used only rarely as a runtime / interpreted
fallback to native execution of these intrinsics via LLVM codegen, so I
doubt these are performance-sensitive functions in practice.

Also fix a bug in `jl_intrinsiclambda_checked` that caused the
"overflow" bit to be written to the wrong location for non-power-of-two
primitive types.

---------

Co-authored-by: Claude <claude@anthropic.com>
This adds a number of tests for our runtime and compile-time intrinsics
implementations.

This primarily checks the implementations against each other, rather
than testing them directly. Nonetheless, it should provide decent
confidence to catch regressions from the port to C. The various bugs
found and fixed today (by comparison to Python) are all caught by these
tests too.

---------

Co-authored-by: Claude <claude@anthropic.com>
@topolarity topolarity force-pushed the ct/intrinsics-no-LLVM branch from 59773a9 to 33f6904 Compare March 31, 2026 14:22
@topolarity
Copy link
Copy Markdown
Member Author

CI looks green, and I am happy with the test coverage.

I will merge this soon (in absence of any objections)

@topolarity topolarity added the merge me PR is reviewed. Merge when all tests are passing label Mar 31, 2026
Copy link
Copy Markdown
Member

@oscardssmith oscardssmith left a comment

Choose a reason for hiding this comment

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

Implimentation looks reasonable.

@topolarity topolarity merged commit a4bf186 into JuliaLang:master Mar 31, 2026
9 checks passed
@topolarity topolarity deleted the ct/intrinsics-no-LLVM branch March 31, 2026 18:11
@topolarity
Copy link
Copy Markdown
Member Author

Thanks for the review @oscardssmith

@oscardssmith oscardssmith added maths Mathematical functions and removed merge me PR is reviewed. Merge when all tests are passing labels Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maths Mathematical functions

Projects

None yet

3 participants