Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/bytecode/encodings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1519,14 +1519,16 @@ function encode_XOrIOp!(cb::CodeBuilder, result_type::TypeId, lhs::Value, rhs::V
end

"""
encode_ShLIOp!(cb, result_type, lhs, rhs) -> Value
encode_ShLIOp!(cb, result_type, lhs, rhs; overflow) -> Value

Shift left.
Opcode: 96
"""
function encode_ShLIOp!(cb::CodeBuilder, result_type::TypeId, lhs::Value, rhs::Value)
function encode_ShLIOp!(cb::CodeBuilder, result_type::TypeId, lhs::Value, rhs::Value;
overflow::IntegerOverflow=OverflowNone)
encode_varint!(cb.buf, Opcode.ShLIOp)
encode_typeid!(cb.buf, result_type)
encode_enum!(cb.buf, overflow)
encode_operand!(cb.buf, lhs)
encode_operand!(cb.buf, rhs)
return new_op!(cb)
Expand Down
39 changes: 3 additions & 36 deletions src/compiler/intrinsics/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,7 @@ function tfunc(𝕃, ::typeof(Intrinsics.andi), @nospecialize(x), @nospecialize(
return CC.widenconst(x)
end
function emit_intrinsic!(ctx::CGCtx, ::typeof(Intrinsics.andi), args)
cb = ctx.cb
tt = ctx.tt

lhs = @something emit_value!(ctx, args[1]) throw(IRError("andi: cannot resolve lhs"))
rhs = @something emit_value!(ctx, args[2]) throw(IRError("andi: cannot resolve rhs"))

lhs_type = CC.widenconst(lhs.jltype)
dtype = julia_to_tile_dtype!(tt, eltype(lhs_type))
result_type_id = tile_type!(tt, dtype, lhs.shape)

result = encode_AndIOp!(cb, result_type_id, lhs.v, rhs.v)
CGVal(result, result_type_id, lhs.jltype, lhs.shape)
emit_binop!(ctx, args, encode_AndIOp!)
end

# cuda_tile.ori
Expand All @@ -361,35 +350,13 @@ function tfunc(𝕃, ::typeof(Intrinsics.ori), @nospecialize(x), @nospecialize(y
return CC.widenconst(x)
end
function emit_intrinsic!(ctx::CGCtx, ::typeof(Intrinsics.ori), args)
cb = ctx.cb
tt = ctx.tt

lhs = @something emit_value!(ctx, args[1]) throw(IRError("ori: cannot resolve lhs"))
rhs = @something emit_value!(ctx, args[2]) throw(IRError("ori: cannot resolve rhs"))

lhs_type = CC.widenconst(lhs.jltype)
dtype = julia_to_tile_dtype!(tt, eltype(lhs_type))
result_type_id = tile_type!(tt, dtype, lhs.shape)

result = encode_OrIOp!(cb, result_type_id, lhs.v, rhs.v)
CGVal(result, result_type_id, lhs.jltype, lhs.shape)
emit_binop!(ctx, args, encode_OrIOp!)
end

# cuda_tile.xori
@intrinsic xori(x::T, y::T) where {T<:Integer}
@intrinsic xori(a::Tile{T}, b::Tile{T}) where {T<:Integer}
tfunc(𝕃, ::typeof(Intrinsics.xori), @nospecialize(x), @nospecialize(y)) = CC.widenconst(x)
function emit_intrinsic!(ctx::CGCtx, ::typeof(Intrinsics.xori), args)
cb = ctx.cb
tt = ctx.tt

lhs = @something emit_value!(ctx, args[1]) throw(IRError("xori: cannot resolve lhs"))
rhs = @something emit_value!(ctx, args[2]) throw(IRError("xori: cannot resolve rhs"))

lhs_type = CC.widenconst(lhs.jltype)
dtype = julia_to_tile_dtype!(tt, eltype(lhs_type))
result_type_id = tile_type!(tt, dtype, lhs.shape)

result = encode_XOrIOp!(cb, result_type_id, lhs.v, rhs.v)
CGVal(result, result_type_id, lhs.jltype, lhs.shape)
emit_binop!(ctx, args, encode_XOrIOp!)
end
27 changes: 26 additions & 1 deletion src/compiler/intrinsics/julia.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Julia intrinsics

# Handle Julia Core.Intrinsics that IRStructurizer uses for control flow transformations.
# These are: add_int (loop increments), slt_int, sle_int, ult_int (loop bounds).
# These are: add_int / sub_int (loop increments), slt_int / sle_int / ult_int (loop bounds),
# and not_int (bitwise NOT, used by `for` loop iteration).
function emit_intrinsic!(ctx::CGCtx, func::Core.IntrinsicFunction, args)
if func === Core.Intrinsics.add_int
emit_intrinsic!(ctx, Intrinsics.addi, args)
Expand All @@ -13,11 +14,35 @@ function emit_intrinsic!(ctx::CGCtx, func::Core.IntrinsicFunction, args)
emit_intrinsic!(ctx, Intrinsics.cmpi, [args..., CmpLessThanOrEqual, SignednessSigned])
elseif func === Core.Intrinsics.ult_int
emit_intrinsic!(ctx, Intrinsics.cmpi, [args..., CmpLessThan, SignednessUnsigned])
elseif func === Core.Intrinsics.not_int
emit_not_int!(ctx, args)
else
throw(IRError("Unhandled Julia intrinsic: $func"))
end
end

# not_int(x) — bitwise NOT.
# Julia's `for` loops generate `not_int` to negate loop-exit conditions.
# Emitted as xori(x, allones) where allones is the bitwise complement identity:
# Bool → xori(x, true) (logical negation)
# Integer → xori(x, -1) (bitwise complement, all bits set)
function emit_not_int!(ctx::CGCtx, args)
cb = ctx.cb
tt = ctx.tt

operand = @something emit_value!(ctx, args[1]) throw(IRError("not_int: cannot resolve operand"))
jltype = CC.widenconst(operand.jltype)

# Build the all-ones constant for xori: true for Bool, -1 (all bits set) for integers
allones_val = jltype === Bool ? true : jltype(-1)
type_id = tile_type_for_julia!(ctx, jltype)
allones_bytes = reinterpret(UInt8, [allones_val])
allones_v = encode_ConstantOp!(cb, type_id, collect(allones_bytes))

result = encode_XOrIOp!(cb, type_id, operand.v, allones_v)
CGVal(result, type_id, operand.jltype, operand.shape)
end

# built-in: ===
function emit_intrinsic!(ctx::CGCtx, ::typeof(===), args)
cb = ctx.cb
Expand Down
49 changes: 48 additions & 1 deletion test/codegen/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,54 @@
#=========================================================================
8.9 Bitwise
=========================================================================#
# TODO: andi - bitwise AND
@testset "Bitwise" begin
spec_i32 = ct.ArraySpec{1}(16, true)

@testset "andi, ori, xori" begin
@test @filecheck begin
@check_label "entry"
code_tiled(Tuple{ct.TileArray{Int32,1,spec_i32}, ct.TileArray{Int32,1,spec_i32}}) do a, b
pid = ct.bid(1)
ta = ct.load(a, pid, (16,))
tb = ct.load(b, pid, (16,))
@check "andi"
Base.donotdelete(map(&, ta, tb))
@check "ori"
Base.donotdelete(map(|, ta, tb))
@check "xori"
Base.donotdelete(map(xor, ta, tb))
return
end
end
end

@testset "shli, shri" begin
@test @filecheck begin
@check_label "entry"
code_tiled(Tuple{ct.TileArray{Int32,1,spec_i32}}) do a
pid = ct.bid(1)
tile = ct.load(a, pid, (16,))
@check "shli"
Base.donotdelete(map(x -> x << Int32(4), tile))
@check "shri"
Base.donotdelete(map(x -> x >> Int32(8), tile))
return
end
end
end
@testset "bitwise NOT (~)" begin
@test @filecheck begin
@check_label "entry"
code_tiled(Tuple{ct.TileArray{Int32,1,spec_i32}}) do a
pid = ct.bid(1)
tile = ct.load(a, pid, (16,))
@check "xori"
Base.donotdelete(map(~, tile))
return
end
end
end
end

#=========================================================================
8.10 Atomics
Expand Down
Loading