Skip to content

Lower one-argument atan, copysign, muladd and float === - #327

Merged
AntonOresten merged 4 commits into
mainfrom
ao/scalar-math-lowerings
Oct 15, 2026
Merged

AntonOresten merged 4 commits into
mainfrom
ao/scalar-math-lowerings

Conversation

@AntonOresten

@AntonOresten AntonOresten commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Several common scalar functions fail to compile on main when broadcast over a tile:

Function Error
muladd (any float type) muladd_float … has no Tile IR equivalent
copysign copysign_float … has no Tile IR equivalent
atan(x) the same, since Base's implementation calls copysign
isinf tileiras: 'cuda_tile.cmpi' op operand #0 must be tile of i1 or i8 or i16 or i32 or i64 values, but got '!cuda_tile.tile<256xf32>'

Changes:

  • muladd_float lowers to Tile IR's fma. muladd allows either a fused or an unfused computation, and Tile IR has a native fma.
  • copysign combines the magnitude bits of one operand with the sign bit of the other, as Tile IR has no copysign.
  • atan(x) maps to atan2(x, 1), as the two-argument form already does.
  • === was lowered to Intrinsics.cmpi for any operand type, so on floats it emitted a cmpi on float tiles. Base defines isinf(x::IEEEFloat) as abs(x) === oftype(x, Inf), so isinf hit this. === now lowers to a new Intrinsics.egal, which bitcasts float operands to unsigned integers of the same width (Base.uinttype) and then emits cmpi. This keeps ==='s bitwise meaning (-0.0 !== 0.0, NaN === NaN), and Intrinsics.cmpi stays integer-only, like Tile IR's cmpi. Integer operands emit the same cmpi as before.

Tests: device tests compare each function against the CPU, including isinf on ±Inf, NaN and floatmax, and muladd on Float64 values. A codegen test checks that float === emits bitcast followed by cmpi.

Assisted by: Opus 5.5

@AntonOresten
AntonOresten force-pushed the ao/scalar-math-lowerings branch from 7a0f1c9 to 5d712cc Compare October 15, 2026 08:40
Several common scalar functions failed to compile when broadcast over a
tile:

- `muladd` (any float type) had no lowering for `muladd_float`. It now
  lowers to Tile IR's `fma`: `muladd` allows either a fused or an unfused
  computation, and Tile IR has a native fma.
- `copysign` had no lowering for `copysign_float`. Tile IR has no
  copysign, so it combines the magnitude bits of one operand with the sign
  bit of the other.
- `atan(x)` reached `copysign` through Base's implementation. It now maps
  to Tile IR's `atan2(x, 1)`, as the two-argument form already does.
- `===` on floats was emitted as a `cmpi` on float tiles, which tileiras
  rejects. This broke `isinf`, which Base defines as
  `abs(x) === oftype(x, Inf)`. The operands are now bitcast to integers of
  the same width first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019SPsbjijQAL2CBmDmAYip5
@AntonOresten
AntonOresten force-pushed the ao/scalar-math-lowerings branch from 5d712cc to 633646b Compare October 15, 2026 08:41
Comment thread src/compiler/intrinsics/arithmetic.jl Resolved

@maleadt maleadt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small doc nit. LGTM otherwise.

AntonOresten and others added 3 commits October 15, 2026 13:32
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019SPsbjijQAL2CBmDmAYip5
Instead of letting `Intrinsics.cmpi` take float operands and bitcasting them
in its emitter, the `===` lowering reinterprets them as unsigned integers of
the same width (`Base.uinttype`): a correctly typed `Intrinsics.bitcast` for
values, and the reinterpreted bit pattern for literals. `cmpi` stays
integer-only, like Tile IR's `cmpi`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019SPsbjijQAL2CBmDmAYip5
Replace the imperative `===` rewrite with a one-line rule to a new
`Intrinsics.egal`. Its emitter bitcasts float operands to unsigned integers
of the same width (`Base.uinttype`) and emits `cmpi` through the helper it
shares with `Intrinsics.cmpi`, which stays integer-only. The generated PTX
is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019SPsbjijQAL2CBmDmAYip5
@AntonOresten

Copy link
Copy Markdown
Collaborator Author

Switched to a new egal intrinsic.

@AntonOresten
AntonOresten merged commit 921489a into main Oct 15, 2026
1 check passed
@AntonOresten
AntonOresten deleted the ao/scalar-math-lowerings branch October 15, 2026 16:21
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