|
| 1 | +mutable struct HloInstruction |
| 2 | + ptr::Ptr{Cvoid} |
| 3 | + |
| 4 | + function HloInstruction(ptr::Ptr{Cvoid}) |
| 5 | + @assert ptr != C_NULL |
| 6 | + return new(ptr) |
| 7 | + end |
| 8 | +end |
| 9 | + |
| 10 | +function free_hlo_instruction(hlo_instruction) |
| 11 | + @ccall MLIR.API.mlir_c.freeHloInstruction(hlo_instruction.ptr::Ptr{Cvoid})::Cvoid |
| 12 | +end |
| 13 | + |
| 14 | +function Base.show(io::IO, hlo_instruction::HloInstruction) |
| 15 | + GC.@preserve hlo_instruction begin |
| 16 | + str = @ccall MLIR.API.mlir_c.hloInstructionToString( |
| 17 | + hlo_instruction.ptr::Ptr{Cvoid}, _iobuffer_to_hlo_print_options(io)::Int32 |
| 18 | + )::Cstring |
| 19 | + end |
| 20 | + print(io, unsafe_string_and_free(str)) |
| 21 | + return nothing |
| 22 | +end |
| 23 | + |
| 24 | +function Base.getproperty(hlo_instruction::HloInstruction, sym::Symbol) |
| 25 | + if sym === :opcode |
| 26 | + return HloOpcode( |
| 27 | + @ccall MLIR.API.mlir_c.hloInstructionGetOpcode( |
| 28 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 29 | + )::UInt8 |
| 30 | + ) |
| 31 | + end |
| 32 | + if sym === :to_apply |
| 33 | + @assert has_to_apply(hlo_instruction) |
| 34 | + return HloComputation( |
| 35 | + @ccall MLIR.API.mlir_c.hloInstructionGetToApply( |
| 36 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 37 | + )::Ptr{Cvoid} |
| 38 | + ) |
| 39 | + end |
| 40 | + if sym in (:fusion_kind, :fused_instructions_computation) |
| 41 | + @assert is_fusion_instruction(hlo_instruction) |
| 42 | + if sym === :fusion_kind |
| 43 | + return HloFusionKind( |
| 44 | + @ccall MLIR.API.mlir_c.hloInstructionGetFusionKind( |
| 45 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 46 | + )::UInt8 |
| 47 | + ) |
| 48 | + else |
| 49 | + return HloComputation( |
| 50 | + @ccall MLIR.API.mlir_c.hloInstructionFusedInstructionsComputation( |
| 51 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 52 | + )::Ptr{Cvoid} |
| 53 | + ) |
| 54 | + end |
| 55 | + end |
| 56 | + return getfield(hlo_instruction, sym) |
| 57 | +end |
| 58 | + |
| 59 | +function has_to_apply(hlo_instruction::HloInstruction) |
| 60 | + has_to_apply = @ccall MLIR.API.mlir_c.hloInstructionHasToApply( |
| 61 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 62 | + )::UInt8 |
| 63 | + return has_to_apply == 1 |
| 64 | +end |
| 65 | + |
| 66 | +function is_fusion_instruction(hlo_instruction::HloInstruction) |
| 67 | + is_fusion = @ccall MLIR.API.mlir_c.hloInstructionIsFusion( |
| 68 | + hlo_instruction.ptr::Ptr{Cvoid} |
| 69 | + )::UInt8 |
| 70 | + return is_fusion == 1 |
| 71 | +end |
| 72 | + |
| 73 | +struct HloOpcode |
| 74 | + opcode::UInt8 |
| 75 | +end |
| 76 | + |
| 77 | +function Base.show(io::IO, hlo_opcode::HloOpcode) |
| 78 | + print( |
| 79 | + io, |
| 80 | + unsafe_string_and_free( |
| 81 | + @ccall MLIR.API.mlir_c.hloOpcodeToString(hlo_opcode.opcode::UInt8)::Cstring |
| 82 | + ), |
| 83 | + ) |
| 84 | + return nothing |
| 85 | +end |
| 86 | + |
| 87 | +struct HloFusionKind |
| 88 | + kind::UInt8 |
| 89 | +end |
| 90 | + |
| 91 | +function Base.show(io::IO, fusion_kind::HloFusionKind) |
| 92 | + print( |
| 93 | + io, |
| 94 | + unsafe_string_and_free( |
| 95 | + @ccall MLIR.API.mlir_c.hloFusionKindToString(fusion_kind.kind::UInt8)::Cstring |
| 96 | + ), |
| 97 | + ) |
| 98 | + return nothing |
| 99 | +end |
0 commit comments