Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/src/man/julia_differences.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ one becomes a compile-time diagnostic. `try`/`catch` is not supported. Use
`ct.@assert` for explicit runtime checks.


## Per-element branches run both arms

A scalar function broadcast or mapped over a tile (`f.(tile)`) runs once for
the whole tile, so a branch on an element's value (`x < 0 ? a : b`, or the
special cases in Base's `asin` and `hypot`) cannot jump. Instead, both arms are
computed for every element and each element selects its own result. This
requires both arms to be free of side effects such as memory accesses or
printing. A `throw` in an arm becomes an assertion that no element takes it.
Loops whose trip count depends on the element's value are not supported.


## Some operations are non-throwing

Float-to-integer conversions such as `Int32(x)`, `trunc(Int32, x)`, and
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/codegen/control_flow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ function emit_if_op!(ctx::CGCtx, op::IfOp, @nospecialize(parent_result_type), ss
# Get condition value
cond_tv = emit_value!(ctx, op.condition)
cond_tv === nothing && throw(IRError("Cannot resolve condition for IfOp"))
isempty(cond_tv.shape) || throw(IRError(
"Cannot branch on a per-element condition (a Bool tile of size " *
"$(Tuple(ColMajorShape(cond_tv.shape).dims))); a branch in a function lifted over " *
"a tile must be free of loops and side effects in both arms, or throw in one"))

# Determine result types from parent_result_type (token_order_pass! already
# updated the type to include any token carries via inst[:type] = …).
Expand Down Expand Up @@ -295,6 +299,10 @@ function emit_while_op!(ctx::CGCtx, op::WhileOp, @nospecialize(parent_result_typ

cond_tv = emit_value!(ctx, cond_op.condition)
(cond_tv === nothing || cond_tv.v === nothing) && throw(IRError("Cannot resolve WhileOp condition"))
isempty(cond_tv.shape) || throw(IRError(
"Cannot loop on a per-element condition (a Bool tile of size " *
"$(Tuple(ColMajorShape(cond_tv.shape).dims))); a loop in a function lifted over " *
"a tile cannot depend on the element's value"))

# Emit conditional break: if (cond) { yield } else { break }
# This keeps nested loops in "after" at LoopOp body level
Expand Down
9 changes: 8 additions & 1 deletion src/compiler/interpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ function cuTileInterpreter(cache::CacheView; always_inline::Bool=true)
else
inf_cache = Vector{CC.InferenceResult}()
end
inf_params = CC.InferenceParams()
# Before 1.12, calls in a block that ends in `throw` are left uninferred and
# uninlined by default. Kernels need them inferred: an exception
# constructor typed `Any` cannot be removed, and its message is lost.
inf_params = @static if VERSION >= v"1.12.0-DEV.1017"
CC.InferenceParams()
else
CC.InferenceParams(; unoptimize_throw_blocks=false)
end
opt_params = if always_inline
CC.OptimizationParams(; inline_cost_threshold=typemax(Int))
else
Expand Down
147 changes: 99 additions & 48 deletions src/compiler/transform/canonicalize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const INTRINSIC_RULES = RewriteRule[
Intrinsics.cmpi(~x, ~y, $(ComparisonPredicate.LessThanOrEqual), $(Signedness.Signed))
@rewrite Core.Intrinsics.ult_int(~x, ~y) =>
Intrinsics.cmpi(~x, ~y, $(ComparisonPredicate.LessThan), $(Signedness.Unsigned))
@rewrite Core.Intrinsics.ule_int(~x, ~y) =>
Intrinsics.cmpi(~x, ~y, $(ComparisonPredicate.LessThanOrEqual), $(Signedness.Unsigned))

# Bitwise
@rewrite Core.Intrinsics.and_int(~x, ~y) => Intrinsics.andi(~x, ~y)
Expand All @@ -52,6 +54,8 @@ const INTRINSIC_RULES = RewriteRule[
# `muladd` may be computed fused (rounded once) or as a multiply and an add;
# Tile IR has a native `fma`, so always fuse.
@rewrite Core.Intrinsics.muladd_float(~x, ~y, ~z) => Intrinsics.fma(~x, ~y, ~z)
# Base's `asin` and `acos` call `sqrt_llvm` directly.
@rewrite Core.Intrinsics.sqrt_llvm(~x) => Intrinsics.sqrt(~x)

# Float comparison
@rewrite Core.Intrinsics.lt_float(~x, ~y) =>
Expand Down Expand Up @@ -87,44 +91,78 @@ lower_intr_pass!(sci::StructuredIRCode) = rewrite_patterns!(sci, INTRINSIC_RULES
#
# This eliminates the scalar/tile duality that complicates codegen and pattern
# matching. Scalars now only can exist as literal values embedded in the IR.
#
# A scalar function lifted over a tile (`f.(tile)`, `map(f, tile)`) becomes
# scalar code operating on `to_scalar` results. Its control flow ends up in
# nested regions, so shapes are propagated over the whole tree in program
# order. A branch on a shaped condition is later flattened by `if_convert!`.

function scalar_elim_pass!(sci::StructuredIRCode)
scalar_elim_block!(sci.entry)
# Phase 1: Eliminate to_scalar (forward tile-typed operand). Uses may be
# in nested regions, so this must happen before any shape propagation.
eliminate_calls!(sci, Intrinsics.to_scalar)

# Phase 2: Propagate tile types from operands to results.
propagate_tile_types!(sci.entry)

# Phase 3: Eliminate from_scalar (operand now has correct tile type)
eliminate_calls!(sci, Intrinsics.from_scalar)

for block in eachblock(sci)
promote_scalar_types!(block)
end
end

function scalar_elim_block!(block::Block)
# Recurse into nested control flow first
for inst in instructions(block)
s = inst[:stmt]
if s isa ForOp
scalar_elim_block!(s.body)
elseif s isa IfOp
scalar_elim_block!(s.then_region)
scalar_elim_block!(s.else_region)
elseif s isa WhileOp
scalar_elim_block!(s.before)
scalar_elim_block!(s.after)
elseif s isa LoopOp
scalar_elim_block!(s.body)
# Replace every call to `func` with its first operand.
function eliminate_calls!(sci::StructuredIRCode, func)
for block in eachblock(sci)
to_delete = Instruction[]
for inst in instructions(block)
call = resolve_call(block, inst[:stmt])
call === nothing && continue
call[1] === func || continue
replace_uses!(block, ssavalue(inst), call[2][1])
push!(to_delete, inst)
end
for inst in to_delete
delete!(block, inst)
end
end
end

# Phase 1: Eliminate to_scalar (forward tile-typed operand)
to_delete = Instruction[]
for inst in instructions(block)
call = resolve_call(block, inst[:stmt])
call === nothing && continue
func, ops = call
func === Intrinsics.to_scalar || continue
replace_uses!(block, ssavalue(inst), ops[1])
push!(to_delete, inst)
# Broadcast shape of the shaped tiles among `types`, or `nothing` when no type
# is a tile. Incompatible shapes keep the first one; codegen reports those.
function broadcast_tile_shape(types)
shape = nothing
for T in types
T isa DataType && T <: Tile || continue
s = size(T)
shape = shape === nothing ? s :
try Base.Broadcast.broadcast_shape(shape, s) catch; shape end
end
return shape
end

# Phase 2: Propagate tile types from operands to results.
# Instructions that consumed to_scalar results still have scalar type
# annotations but now receive tile-typed operands — inherit their shape.
# Type of an operand, or `nothing` for tokens and unknown types.
function tile_operand_type(block::Block, @nospecialize(op))
T = value_type(block, op)
(T === nothing || is_token_type(T)) ? nothing : CC.widenconst(T)
end

# Instructions that consumed to_scalar results still have scalar type
# annotations but now receive tile-typed operands; give them the broadcast
# shape of those operands. Regions are visited in program order, so values
# defined outside a region are typed before the region uses them.
function propagate_tile_types!(block::Block)
for inst in instructions(block)
call = resolve_call(block, inst[:stmt])
stmt = inst[:stmt]
if stmt isa ControlFlowOp
foreach(propagate_tile_types!, IRStructurizer.blocks(stmt))
stmt isa IfOp && propagate_if_type!(block, inst, stmt)
continue
end

call = resolve_call(block, stmt)
call === nothing && continue
func, ops = call

Expand All @@ -140,32 +178,45 @@ function scalar_elim_block!(block::Block)
T <: Tile && continue # already tile-typed
T <: Number || continue # only promote scalar number types

for op in ops
op_type = value_type(block, op)
op_type === nothing && continue
is_token_type(op_type) && continue
OT = CC.widenconst(op_type)
OT <: Tile || continue
S = OT.parameters[2]
inst[:type] = Tile{T, S}
break
# A field of a control-flow result, which carries its own shape.
if func === getfield && length(ops) >= 2 && ops[2] isa Int
PT = tile_operand_type(block, ops[1])
if PT isa DataType && PT <: Tuple && 1 <= ops[2] <= length(PT.parameters)
FT = CC.widenconst(PT.parameters[ops[2]])
FT <: Tile{T} && (inst[:type] = FT)
continue
end
end
end

# Phase 3: Eliminate from_scalar (operand now has correct tile type)
for inst in instructions(block)
call = resolve_call(block, inst[:stmt])
call === nothing && continue
func, ops = call
func === Intrinsics.from_scalar || continue
replace_uses!(block, ssavalue(inst), ops[1])
push!(to_delete, inst)
shape = broadcast_tile_shape(tile_operand_type(block, op) for op in ops)
shape === nothing && continue
inst[:type] = Tile{T, Tuple{shape...}}
end
end

for inst in to_delete
delete!(block, inst)
# Give each scalar result of an `IfOp` the broadcast shape of the values its
# arms yield. A shaped condition will be flattened into selects, whose result
# also has the condition's shape.
function propagate_if_type!(block::Block, inst::Instruction, op::IfOp)
RT = CC.widenconst(inst[:type])
RT <: Tuple || return
cond_type = tile_operand_type(block, op.condition)
params = map(enumerate(RT.parameters)) do (i, P)
P = CC.widenconst(P)
P <: Number && !(P <: Tile) || return P
types = Any[cond_type]
for region in (op.then_region, op.else_region)
term = terminator(region)
term isa YieldOp && i <= length(term.values) &&
push!(types, tile_operand_type(region, term.values[i]))
end
shape = broadcast_tile_shape(types)
shape === nothing ? P : Tile{P, Tuple{shape...}}
end
inst[:type] = Tuple{params...}
end

function promote_scalar_types!(block::Block)
# Phase 4: Promote any remaining scalar Number types to 0D tiles.
# This catches intrinsics that natively return scalars (get_tile_block_id,
# item, etc.) and any scalar arithmetic on them (addi of two block IDs).
Expand Down
Loading
Loading