Skip to content

Commit 6924e03

Browse files
committed
Combining multiple object files to one
1 parent bd24b51 commit 6924e03

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

python/iron/jit.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import numpy as np
1313
import pyxrt as xrt
1414
import shutil
15-
import sys
16-
import traceback
1715

1816
from aie.extras.context import mlir_mod_ctx
1917
from ..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

python/iron/kernel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def resolve(
7777

7878
class ExternalFunction(BaseKernel):
7979
_instances = set()
80+
_bin_name = str()
8081

8182
def __init__(
8283
self,
@@ -108,7 +109,6 @@ def __init__(
108109
self._object_file_name = object_file_name
109110
else:
110111
self._object_file_name = f"{self._name}.o"
111-
self._compiled = False
112112

113113
# Track this instance for JIT compilation
114114
ExternalFunction._instances.add(self)
@@ -132,9 +132,9 @@ def __exit__(self, exc_type, exc_value, traceback):
132132
"""Exit the context."""
133133
pass
134134

135-
@property
136-
def bin_name(self) -> str:
137-
return self._object_file_name
135+
@classmethod
136+
def bin_name(cls) -> str:
137+
return ExternalFunction._bin_name
138138

139139
def tile_size(self, arg_index: int = 0) -> int:
140140
"""Get the tile size from the specified array argument type.

python/iron/worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def do_nothing_core_fun(*args) -> None:
8686

8787
# Check arguments to the core. Some information is saved for resolution.
8888
for arg in self.fn_args:
89-
if isinstance(arg, (Kernel, ExternalFunction)):
89+
if isinstance(arg, ExternalFunction):
90+
bin_names.add(ExternalFunction._bin_name)
91+
if isinstance(arg, Kernel):
9092
bin_names.add(arg.bin_name)
9193
elif isinstance(arg, ObjectFifoHandle):
9294
arg.endpoint = self

0 commit comments

Comments
 (0)