1212import numpy as np
1313import pyxrt as xrt
1414import shutil
15- import sys
16- import traceback
1715
1816from aie .extras .context import mlir_mod_ctx
1917from ..utils .xrt import read_insts_binary
@@ -147,6 +145,7 @@ def decorator(*args, **kwargs):
147145
148146 # Clear any instances from previous runs to make sure if the user provided any broken code we don't try to recompile it
149147 ExternalFunction ._instances .clear ()
148+ ExternalFunction ._bin_name = function .__name__ + ".o"
150149
151150 # Find ExternalFunction instances in arguments and kwargs
152151 external_kernels = []
@@ -216,8 +215,25 @@ def decorator(*args, **kwargs):
216215 print (mlir_module , file = f )
217216
218217 # Compile ExternalFunctions from inside the JIT compilation directory
218+ object_files = []
219219 for func in external_kernels :
220- compile_external_kernel (func , kernel_dir , target_arch )
220+ compile_external_kernel (
221+ func , kernel_dir , target_arch , func ._object_file_name
222+ )
223+ object_files .append (
224+ os .path .join (kernel_dir , func ._object_file_name )
225+ )
226+
227+ # Combine all object files in a single object file
228+ if object_files :
229+ from .compile .link import merge_object_files
230+
231+ merged_object_file = os .path .join (
232+ kernel_dir , ExternalFunction ._bin_name
233+ )
234+ merge_object_files (
235+ object_paths = object_files , output_path = merged_object_file
236+ )
221237
222238 # Compile the MLIR module
223239 compile_mlir_module (
@@ -243,7 +259,7 @@ def decorator(*args, **kwargs):
243259 return decorator
244260
245261
246- def compile_external_kernel (func , kernel_dir , target_arch ):
262+ def compile_external_kernel (func , kernel_dir , target_arch , output_file ):
247263 """
248264 Compile an ExternalFunction to an object file in the kernel directory.
249265
@@ -252,12 +268,6 @@ def compile_external_kernel(func, kernel_dir, target_arch):
252268 kernel_dir: Directory to place the compiled object file
253269 target_arch: Target architecture (e.g., "aie2" or "aie2p")
254270 """
255-
256- # Check if object file already exists in kernel directory
257- output_file = os .path .join (kernel_dir , func ._object_file_name )
258- if os .path .exists (output_file ):
259- return
260-
261271 # Create source file in kernel directory
262272 source_file = os .path .join (kernel_dir , f"{ func ._name } .cc" )
263273
0 commit comments