|
1 | 1 | import triton.language as tl |
2 | 2 | from triton.language.core import builtin, constexpr as tl_constexpr, tensor |
3 | 3 | from triton.experimental.tle.language.gpu import buffered_tensor |
| 4 | +import importlib.util |
| 5 | + |
| 6 | + |
| 7 | +def _pointer_type_hash(self): |
| 8 | + return hash((self.name, self.element_ty, "tt_ptr")) |
| 9 | + |
| 10 | + |
| 11 | +def patch_hash_method_for_pointer_type(): |
| 12 | + elem_dtype_list = tl.core.dtype.SINT_TYPES + tl.core.dtype.UINT_TYPES + tl.core.dtype.FP_TYPES + tl.core.dtype.OTHER_TYPES |
| 13 | + for elem_dtype in elem_dtype_list: |
| 14 | + ptr_ty = type(tl.core.pointer_type(tl.core.dtype(elem_dtype))) |
| 15 | + ptr_ty.__hash__ = _pointer_type_hash |
| 16 | + |
| 17 | + |
| 18 | +def import_from_path(file_path): |
| 19 | + module_name = f"_imported_{abs(hash(file_path))}" |
| 20 | + spec = importlib.util.spec_from_file_location(module_name, file_path) |
| 21 | + module = importlib.util.module_from_spec(spec) |
| 22 | + spec.loader.exec_module(module) |
| 23 | + return module |
4 | 24 |
|
5 | 25 |
|
6 | 26 | def _resolve_alias_indices(func, llvm, handles, output_indices, _semantic): |
@@ -42,14 +62,22 @@ def _normalize_hint(hint): |
42 | 62 |
|
43 | 63 | def _tle_raw_call(func, args, *, output_indices, hint, smem, _semantic): |
44 | 64 | hint = _normalize_hint(hint) |
45 | | - handles = [arg.handle for arg in args] |
46 | 65 | if getattr(func, "deferred", False): |
| 66 | + handles = [arg.handle for arg in args] |
47 | 67 | if output_indices is None: |
48 | 68 | raise RuntimeError("deferred tle_raw.call requires explicit output_indices=") |
49 | 69 | alias_indices = output_indices |
50 | 70 | source_id = func.register_pending_source(hint=hint) |
51 | 71 | dsl_region_op = func.create_region_deferred(_semantic.builder, source_id, handles, alias_indices, hint) |
52 | 72 | else: |
| 73 | + if func.compiler.lower() == "nvcc" or (func.compiler.lower() == "clang" and func.target.lower() == "bc"): |
| 74 | + patch_hash_method_for_pointer_type() |
| 75 | + module = import_from_path(func.extern_file) |
| 76 | + target_fn = getattr(module, func.extern_func_name) |
| 77 | + ret = target_fn(*args, _semantic=_semantic) |
| 78 | + return ret |
| 79 | + |
| 80 | + handles = [arg.handle for arg in args] |
53 | 81 | context = _semantic.builder.get_context() |
54 | 82 | llvm = func.make_llvm(context) |
55 | 83 | alias_indices = _resolve_alias_indices(func, llvm, handles, output_indices, _semantic) |
|
0 commit comments