Skip to content

Commit 3780b8b

Browse files
code format
1 parent 65d9d93 commit 3780b8b

21 files changed

Lines changed: 875 additions & 716 deletions

third_party/tsingmicro/backend/compiler.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def _get_libc_root() -> str:
3232
raise Exception("LIB_C_ROOT is not set.")
3333
return path
3434

35+
3536
def _get_core_dialects_to_mk_pass_arg() -> str:
3637
value = os.getenv("PRECISION_MODE", "0").strip()
3738
if value not in ("0", "1", "2"):
@@ -50,7 +51,8 @@ def compile_accelerator(src, metadata, o_path):
5051
if cache_path is None:
5152
with tempfile.TemporaryDirectory() as tmpdir:
5253
dst_path = os.path.join(tmpdir, f"{name}.so")
53-
xuantie_dir=txda_tools.get_tx8_deps_path("rcs1fw-rtt/tool/rcsfw-xuantie-sdk/Xuantie-900-gcc-elf-newlib-x86_64-V2.8.0")
54+
xuantie_dir = txda_tools.get_tx8_deps_path(
55+
"rcs1fw-rtt/tool/rcsfw-xuantie-sdk/Xuantie-900-gcc-elf-newlib-x86_64-V2.8.0")
5456
gcc_path = os.path.join(xuantie_dir, "bin", "riscv64-unknown-elf-gcc")
5557
libc_lib = os.path.join(xuantie_dir, "riscv64-unknown-elf", "lib", "rv64imfdc", "lp64d")
5658
libgcc_lib = os.path.join(xuantie_dir, "lib", "gcc", "riscv64-unknown-elf", "10.4.0", "rv64imfdc", "lp64d")
@@ -118,20 +120,11 @@ def _ttir_to_coreir(mod, num_stages=2):
118120
pipeline_flag = f"--mk-pipeline=num-stages={num_stages}"
119121

120122
args = [
121-
triton_opt_path,
122-
src_path,
123-
"--triton-to-core-dialects",
124-
"--tle-to-mk",
125-
"--dsa-memory-to-core",
126-
"--linalg-tiling",
127-
f"{coreir_to_mk_mode}",
128-
"--linalg-fusion",
129-
"--legalize-tensor-form-loops",
130-
"--one-shot-bufferize",
131-
"--convert-bufferization-to-memref",
132-
"--materialize-strided-linalg-inputs",
133-
"--cse",
134-
"--canonicalize"]
123+
triton_opt_path, src_path, "--triton-to-core-dialects", "--tle-to-mk", "--dsa-memory-to-core",
124+
"--linalg-tiling", f"{coreir_to_mk_mode}", "--linalg-fusion", "--legalize-tensor-form-loops",
125+
"--one-shot-bufferize", "--convert-bufferization-to-memref", "--materialize-strided-linalg-inputs", "--cse",
126+
"--canonicalize"
127+
]
135128

136129
if os.getenv("TRITON_PIPELINE", "1") == "1":
137130
args.append(pipeline_flag)
@@ -191,7 +184,9 @@ def _coreir_to_txir(mod):
191184
triton_opt_path = txda_tools.get_tsm_opt_path()
192185
txda_tools.dump_ir_if_needed([src_path])
193186

194-
args = [triton_opt_path, src_path,
187+
args = [
188+
triton_opt_path,
189+
src_path,
195190
"--spmd-allocate-shared-memory",
196191
"--expand-strided-metadata",
197192
"--lower-affine", # convert affine.load to memref.load, need exec before tx81-to-llvm since we will support spm offset to memref.load
@@ -231,10 +226,9 @@ def _txir_to_llir(mod, metadata):
231226
args = [
232227
triton_opt_path, src_path,
233228
# Use tx81-memref-to-llvm to replace "--finalize-memref-to-llvm".
234-
"--tx81-memref-to-llvm", "--addr-to-llvm", "--convert-scf-to-cf",
235-
"--expand-strided-metadata",
236-
"--convert-math-to-llvm",
237-
"--convert-math-to-libm", "--convert-cf-to-llvm", # need exec before "convert-func-to-llvm"
229+
"--tx81-memref-to-llvm", "--addr-to-llvm", "--convert-scf-to-cf", "--expand-strided-metadata",
230+
"--convert-math-to-llvm", "--convert-math-to-libm",
231+
"--convert-cf-to-llvm", # need exec before "convert-func-to-llvm"
238232
"--convert-func-to-llvm", # need exec before "kernel-arg-buffer", otherwise un-rank memref will translate to int(rank) + ptr
239233
# Other unconverted memref ops, eg: memref.global from scan op conversion
240234
"--finalize-memref-to-llvm"
@@ -257,8 +251,7 @@ def _txir_to_llir(mod, metadata):
257251
tx81_to_llvm = "--tx81-to-llvm=gather-scatter-async=true"
258252

259253
args += [
260-
tx81_to_llvm,
261-
"--convert-arith-to-llvm", # need exec last since arith.const conversion
254+
tx81_to_llvm, "--convert-arith-to-llvm", # need exec last since arith.const conversion
262255
# Remove all unrealized casts created
263256
"--reconcile-unrealized-casts", "--canonicalize", "--export-kernel-symbols", "-o", llvmir_path
264257
]
@@ -434,6 +427,7 @@ def hash(self):
434427

435428

436429
class TXDABackend(BaseBackend):
430+
437431
@staticmethod
438432
def supports_target(target: GPUTarget):
439433
return target.backend == 'txda'

third_party/tsingmicro/backend/driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,7 @@ def get_device_properties(self, *args):
11871187
_last_launch_res = None
11881188
_launch_counter = 0
11891189

1190+
11901191
# Launch cross compiled runtime program on controller
11911192
class TXDALauncher(object):
11921193

@@ -1211,8 +1212,7 @@ def __call__(self, gridX, gridY, gridZ, stream, function, *args, **kwargs):
12111212
logger.info(f"{self.func_name} launch card:{device_id} count:{_launch_counter} begin")
12121213
launchRes = self.launch(device_id, self.metadata.so_key, self.metadata.kernel_path, self.func_name,
12131214
txda_tools.is_dump_args_profile(), txda_tools.get_dump_dir(), log_level,
1214-
_launch_counter,
1215-
gridX, gridY, gridZ, stream, function, *args, **kwargs)
1215+
_launch_counter, gridX, gridY, gridZ, stream, function, *args, **kwargs)
12161216
logger.info(f"{self.func_name} launch card:{device_id} count:{_launch_counter} end")
12171217
if launchRes.res != 0:
12181218
logger.error(f"launch error code:{launchRes.res}")

third_party/tsingmicro/backend/include/tsm_prof_query.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) 2026 Tsingmicro CORPORATION
22
//
3-
// Query device kernel timing (AP / kcore) after txLaunch* + txStreamSynchronize.
4-
// Requires libtsm-api-log-tracing.so listed in ROCP_TOOL_LIBRARIES.
5-
// Resolve symbols via dlopen(ROCP_TOOL_LIBRARIES) + dlsym — not dlsym(RTLD_DEFAULT).
3+
// Query device kernel timing (AP / kcore) after txLaunch* +
4+
// txStreamSynchronize. Requires libtsm-api-log-tracing.so listed in
5+
// ROCP_TOOL_LIBRARIES. Resolve symbols via dlopen(ROCP_TOOL_LIBRARIES) + dlsym
6+
// — not dlsym(RTLD_DEFAULT).
67
//
78
#pragma once
89

@@ -12,7 +13,7 @@
1213
extern "C" {
1314
#endif
1415

15-
#define TSM_PROF_API_OP_MAX 64
16+
#define TSM_PROF_API_OP_MAX 64
1617
#define TSM_PROF_API_ARGS_MAX 512
1718

1819
typedef struct tsm_prof_timing_t {
@@ -25,23 +26,26 @@ typedef struct tsm_prof_timing_t {
2526
uint64_t ap_end;
2627
uint64_t kcore_start;
2728
uint64_t kcore_end;
28-
char api_op[TSM_PROF_API_OP_MAX];
29-
char api_args[TSM_PROF_API_ARGS_MAX];
30-
char api_result[64];
31-
int dev_id;
32-
int ready;
29+
char api_op[TSM_PROF_API_OP_MAX];
30+
char api_args[TSM_PROF_API_ARGS_MAX];
31+
char api_result[64];
32+
int dev_id;
33+
int ready;
3334
} tsm_prof_timing_t;
3435

35-
/* Start a profiling scope; call immediately before the target traced API. Returns 0 on failure. */
36+
/* Start a profiling scope; call immediately before the target traced API.
37+
* Returns 0 on failure. */
3638
uint64_t tsm_prof_begin(void);
3739

3840
/*
39-
* Query device timing for token. Call after txStreamSynchronize (same thread as tsm_prof_begin).
40-
* The next traced TSM_RUNTIME_API EXIT after begin binds token to that API's corid.
41-
* Returns 0 on success, -1 invalid args/unknown token, -2 corid not bound yet, -3 timeout.
42-
* timeout_ms: 0 = poll once, >0 = wait up to N ms for kernel notify.
41+
* Query device timing for token. Call after txStreamSynchronize (same thread as
42+
* tsm_prof_begin). The next traced TSM_RUNTIME_API EXIT after begin binds token
43+
* to that API's corid. Returns 0 on success, -1 invalid args/unknown token, -2
44+
* corid not bound yet, -3 timeout. timeout_ms: 0 = poll once, >0 = wait up to N
45+
* ms for kernel notify.
4346
*/
44-
int tsm_prof_get_timing(uint64_t token, tsm_prof_timing_t* out, uint32_t timeout_ms);
47+
int tsm_prof_get_timing(uint64_t token, tsm_prof_timing_t *out,
48+
uint32_t timeout_ms);
4549

4650
#ifdef __cplusplus
4751
}

third_party/tsingmicro/backend/include/tsm_profiler.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,42 @@
1414
#pragma once
1515

1616
#include "tsm_prof_query.h"
17-
#include <dlfcn.h>
1817
#include <cstdint>
1918
#include <cstdio>
2019
#include <cstdlib>
20+
#include <dlfcn.h>
2121
#include <string>
2222

23-
static void* g_prof_tool_handle = nullptr;
23+
static void *g_prof_tool_handle = nullptr;
2424
static uint64_t (*resolve_tsm_prof_begin)(void) = nullptr;
25-
static int (*resolve_tsm_prof_get_timing)(uint64_t, tsm_prof_timing_t*, uint32_t) = nullptr;
25+
static int (*resolve_tsm_prof_get_timing)(uint64_t, tsm_prof_timing_t *,
26+
uint32_t) = nullptr;
2627

27-
inline void* open_prof_tool_from_env() {
28+
inline void *open_prof_tool_from_env() {
2829
if (g_prof_tool_handle)
2930
return g_prof_tool_handle;
3031

31-
const char* env = std::getenv("ROCP_TOOL_LIBRARIES");
32+
const char *env = std::getenv("ROCP_TOOL_LIBRARIES");
3233
if (!env || env[0] == '\0')
3334
return nullptr;
3435

3536
std::string paths{env};
36-
size_t start = 0;
37+
size_t start = 0;
3738
while (start < paths.size()) {
3839
const size_t end = paths.find(':', start);
39-
const auto path =
40-
(end == std::string::npos) ? paths.substr(start) : paths.substr(start, end - start);
40+
const auto path = (end == std::string::npos)
41+
? paths.substr(start)
42+
: paths.substr(start, end - start);
4143
start = (end == std::string::npos) ? paths.size() : end + 1;
4244

4345
if (path.empty())
4446
continue;
4547

46-
void* handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_NOLOAD);
48+
void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_NOLOAD);
4749
if (!handle)
4850
handle = dlopen(path.c_str(), RTLD_LAZY);
4951
if (handle) {
50-
void* sym = dlsym(handle, "tsm_prof_begin");
52+
void *sym = dlsym(handle, "tsm_prof_begin");
5153
if (sym != nullptr) {
5254
g_prof_tool_handle = handle;
5355
return handle;
@@ -58,14 +60,16 @@ inline void* open_prof_tool_from_env() {
5860
}
5961

6062
inline bool resolve_prof_query_api() {
61-
void* handle = open_prof_tool_from_env();
63+
void *handle = open_prof_tool_from_env();
6264
if (!handle)
6365
return false;
6466

6567
if (!resolve_tsm_prof_begin)
66-
resolve_tsm_prof_begin = reinterpret_cast<uint64_t (*)(void)>(dlsym(handle, "tsm_prof_begin"));
68+
resolve_tsm_prof_begin =
69+
reinterpret_cast<uint64_t (*)(void)>(dlsym(handle, "tsm_prof_begin"));
6770
if (!resolve_tsm_prof_get_timing)
6871
resolve_tsm_prof_get_timing =
69-
reinterpret_cast<int (*)(uint64_t, tsm_prof_timing_t*, uint32_t)>(dlsym(handle, "tsm_prof_get_timing"));
72+
reinterpret_cast<int (*)(uint64_t, tsm_prof_timing_t *, uint32_t)>(
73+
dlsym(handle, "tsm_prof_get_timing"));
7074
return resolve_tsm_prof_begin && resolve_tsm_prof_get_timing;
7175
}

0 commit comments

Comments
 (0)