diff --git a/src/device/intrinsics/output.jl b/src/device/intrinsics/output.jl index 2b2ba00a..53ece0f9 100644 --- a/src/device/intrinsics/output.jl +++ b/src/device/intrinsics/output.jl @@ -75,10 +75,16 @@ end T_pint8 = LLVM.PointerType(LLVM.Int8Type()) T_pint8a2 = LLVM.PointerType(LLVM.Int8Type(), 2) + + # create functions param_types = LLVMType[convert(LLVMType, typ) for typ in arg_types] - llvm_f, llvm_ft = create_function(T_void, LLVMType[]; vararg=true) - mod = LLVM.parent(llvm_f) + wrapper_f, wrapper_ft = create_function(T_void, param_types) + mod = LLVM.parent(wrapper_f) + + llvm_ft = LLVM.FunctionType(T_void, LLVMType[]; vararg=true) + llvm_f = LLVM.Function(mod, "vararg", llvm_ft) + push!(function_attributes(llvm_f), EnumAttribute("alwaysinline", 0)) # generate IR @dispose builder=IRBuilder() begin @@ -86,6 +92,9 @@ end position!(builder, entry) str = globalstring_ptr!(builder, String(fmt), addrspace=2) + subsystem_str = globalstring_ptr!(builder, MTLLOG_SUBSYSTEM, addrspace=2) + category_str = globalstring_ptr!(builder, MTLLOG_CATEGRORY, addrspace=2) + log_type = LLVM.ConstantInt(T_int32, __METAL_OS_LOG_TYPE_DEBUG__) # compute argsize argtypes = LLVM.StructType(param_types) @@ -103,16 +112,11 @@ end va_start_fty = LLVM.FunctionType(T_void, [T_pint8]) va_start = LLVM.Function(mod, "llvm.va_start", va_start_fty) call!(builder, va_start_fty, va_start, [buffer]) - - # invoke @air.os_log and return - subsystem_str = globalstring_ptr!(builder, MTLLOG_SUBSYSTEM, addrspace=2) - category_str = globalstring_ptr!(builder, MTLLOG_CATEGRORY, addrspace=2) - log_type = LLVM.ConstantInt(T_int32, __METAL_OS_LOG_TYPE_DEBUG__) - os_log_fty = LLVM.FunctionType(T_void, [T_pint8a2, T_pint8a2, T_int32, T_pint8a2, T_pint8, T_int64]) - os_log = LLVM.Function(mod, "air.os_log", os_log_fty) arg_ptr = load!(builder, T_pint8, alloc) + os_log_fty = LLVM.FunctionType(T_void, [T_pint8a2, T_pint8a2, T_int32, T_pint8a2, T_pint8, T_int64]) + os_log = LLVM.Function(mod, "air.os_log", os_log_fty) call!(builder, os_log_fty, os_log, [subsystem_str, category_str, log_type, str, arg_ptr, arg_size]) va_end_fty = LLVM.FunctionType(T_void, [T_pint8]) @@ -126,19 +130,17 @@ end ret!(builder) end - wrapper_f, wrapper_ft = create_function(T_void, param_types) - - @dispose builder=IRBuilder() begin - entry = BasicBlock(wrapper_f, "entry") - position!(builder, entry) + # @dispose builder=IRBuilder() begin + # entry = BasicBlock(wrapper_f, "entry") + # position!(builder, entry) - # call!(builder, llvm_ft, llvm_f, collect(parameters(wrapper_f))) + # call!(builder, llvm_ft, llvm_f, collect(parameters(wrapper_f))) - ret!(builder) - end + # ret!(builder) + # end - call_function(wrapper_f, Nothing, Tuple{arg_types...}, arg_exprs...) + call_function(llvm_f, Nothing, Tuple{arg_types...}, arg_exprs...) end end