Inline snrt
inline_snrt
¶
SnrtConstants
dataclass
¶
Bases: ABC
Constants used when compiling the snitch runtime, depend on the exact snitch architecture target.
Source code in xdsl/transforms/inline_snrt.py
cluster_num: int = 4
class-attribute
instance-attribute
¶
Number of snitch clusters on the chip
cluster_core_num: int = 9
class-attribute
instance-attribute
¶
Number of cores per cluster (1 dma + 8 compute)
base_hartid: int = 0
class-attribute
instance-attribute
¶
SNRT expects the snitch cores to be numbered contiguously starting at base_hartid
cluster_dm_core_num: int = 1
class-attribute
instance-attribute
¶
Number of dm cores per cluster. Compute core num = cluster_core_num - cluster_dm_core_num
__init__(cluster_num: int = 4, cluster_core_num: int = 9, base_hartid: int = 0, cluster_dm_core_num: int = 1) -> None
¶
LowerClusterHWBarrier
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.ClusterHwBarrierOp, rewriter: PatternRewriter)
¶
Lowers to:
/// Synchronize cores in a cluster with a hardware barrier inline void snrt_cluster_hw_barrier() { asm volatile("csrr x0, 0x7C2" ::: "memory"); }
Source code in xdsl/transforms/inline_snrt.py
LowerSSRDisable
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.SsrDisableOp, rewriter: PatternRewriter)
¶
Lowers to:
/// Disable SSR.
inline void snrt_ssr_disable() {
#ifdef __TOOLCHAIN_LLVM__
__builtin_ssr_disable();
#else
asm volatile("csrci 0x7C0, 1
"); #endif }
P.S. This specific rewrite ignores the LLVM case and goes
straight to the generic one.
Source code in xdsl/transforms/inline_snrt.py
LowerDMAStart1D
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.DmaStart1DOp, rewriter: PatternRewriter)
¶
Lowers to:
/// Initiate an asynchronous 1D DMA transfer. inline snrt_dma_txid_t snrt_dma_start_1d(void *dst, const void *src, size_t size) { return snrt_dma_start_1d_wideptr((size_t)dst, (size_t)src, size); }
Source code in xdsl/transforms/inline_snrt.py
LowerDMAStart1DWidePtr
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
match_and_rewrite(op: snitch_runtime.DmaStart1DWideptrOp, rewriter: PatternRewriter)
¶
Lowers to:
/// Initiate an asynchronous 1D DMA transfer with wide 64-bit pointers.
inline snrt_dma_txid_t snrt_dma_start_1d_wideptr(uint64_t dst, uint64_t src,
size_t size) {
// Current DMA does not allow transfers with size == 0 (blocks)
// TODO(colluca) remove this check once new DMA is integrated
if (size > 0) {
register uint32_t reg_dst_low asm("a0") = dst >> 0; // 10
register uint32_t reg_dst_high asm("a1") = dst >> 32; // 11
register uint32_t reg_src_low asm("a2") = src >> 0; // 12
register uint32_t reg_src_high asm("a3") = src >> 32; // 13
register uint32_t reg_size asm("a4") = size; // 14
// dmsrc a2, a3
asm volatile(
".word (0b0000000 << 25) | ( (13) << 20) | ( (12) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" ::"r"(reg_src_high), "r"(reg_src_low));
// dmdst a0, a1
asm volatile(
".word (0b0000001 << 25) | ( (11) << 20) | ( (10) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" ::"r"(reg_dst_high), "r"(reg_dst_low));
// dmcpyi a0, a4, 0b00
register uint32_t reg_txid asm("a0"); // 10
asm volatile(
".word (0b0000010 << 25) | ( 0b00000 << 20) | ( (14) << 15) | ( 0b000 << 12) | ( (10) << 7) | (0b0101011 << 0)
" : "=r"(reg_txid) : "r"(reg_size));
return reg_txid;
} else {
return -1;
}
}
P.S. We only implement taking the top branch for now.
Source code in xdsl/transforms/inline_snrt.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
LowerDMAStart2DBase
¶
Bases: RewritePattern, ABC
Source code in xdsl/transforms/inline_snrt.py
any_reg = riscv.Registers.UNALLOCATED_INT
class-attribute
instance-attribute
¶
generate_dma_instructions(dst_low: SSAValue | Operation, dst_high: SSAValue | Operation, src_low: SSAValue | Operation, src_high: SSAValue | Operation, size: SSAValue | Operation, dst_stride: SSAValue | Operation, src_stride: SSAValue | Operation, repeat: SSAValue | Operation) -> tuple[Sequence[Operation], Sequence[SSAValue]]
¶
Common function to generate the following sequence of operations
dmsrc %src_low, %src_high dmdst %dst_low, %dst_high dmstr %src_stride, %dst_stride dmrep %repeat %tx_id = dmcpyi %size, 0b10 %tx_id_i32 unrealized_conversion_cast %tx_id to i32
Source code in xdsl/transforms/inline_snrt.py
cast_i32(input_val: SSAValue)
¶
cast_i64(input_val: SSAValue)
¶
LowerDMAStart2DWideptr
¶
Bases: LowerDMAStart2DBase
Source code in xdsl/transforms/inline_snrt.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
match_and_rewrite(op: snitch_runtime.DmaStart2DWideptrOp, rewriter: PatternRewriter)
¶
Lowers to the equivalent of the snitch_runtime implementation:
inline snrt_dma_txid_t snrt_dma_start_2d_wideptr(uint64_t dst, uint64_t src,
size_t size, size_t dst_stride,
size_t src_stride,
size_t repeat) {
// Current DMA does not allow transfers with size == 0 (blocks)
// TODO(colluca) remove this check once new DMA is integrated
if (size > 0) {
register uint32_t reg_dst_low asm("a0") = dst >> 0; // 10
register uint32_t reg_dst_high asm("a1") = dst >> 32; // 11
register uint32_t reg_src_low asm("a2") = src >> 0; // 12
register uint32_t reg_src_high asm("a3") = src >> 32; // 13
register uint32_t reg_size asm("a4") = size; // 14
register uint32_t reg_dst_stride asm("a5") = dst_stride; // 15
register uint32_t reg_src_stride asm("a6") = src_stride; // 16
register uint32_t reg_repeat asm("a7") = repeat; // 17
// dmsrc a0, a1
asm volatile(
".word (0b0000000 << 25) | ( (13) << 20) | ( (12) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" ::"r"(reg_src_high), "r"(reg_src_low));
// dmdst a0, a1
asm volatile(
".word (0b0000001 << 25) | ( (11) << 20) | ( (10) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" ::"r"(reg_dst_high), "r"(reg_dst_low));
// dmstr a5, a6
asm volatile(
".word (0b0000110 << 25) | ( (15) << 20) | ( (16) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" : : "r"(reg_dst_stride), "r"(reg_src_stride));
// dmrep a7
asm volatile(
".word (0b0000111 << 25) | ( (17) << 15) | ( 0b000 << 12) | (0b0101011 << 0)
" : : "r"(reg_repeat));
// dmcpyi a0, a4, 0b10
register uint32_t reg_txid asm("a0"); // 10
asm volatile(
".word (0b0000010 << 25) | ( 0b00010 << 20) | ( (14) << 15) | ( 0b000 << 12) | ( (10) << 7) | (0b0101011 << 0)
" : "=r"(reg_txid) : "r"(reg_size));
return reg_txid;
} else {
return -1;
}
}
Source code in xdsl/transforms/inline_snrt.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
LowerDMAStart2D
¶
Bases: LowerDMAStart2DBase
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.DmaStart2DOp, rewriter: PatternRewriter)
¶
Lower to the equivalent snitch_runtime implementation:
/// Initiate an asynchronous 2D DMA transfer. inline snrt_dma_txid_t snrt_dma_start_2d(void *dst, const void *src, size_t size, size_t dst_stride, size_t src_stride, size_t repeat) { return snrt_dma_start_2d_wideptr((size_t)dst, (size_t)src, size, dst_stride, src_stride, repeat); }
Source code in xdsl/transforms/inline_snrt.py
LowerGlobalCoreBaseHartid
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
LowerGlobalCoreNum
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
LowerClusterCoreNum
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
LowerClusterNum
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
LowerClusterDmCoreNum
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
LowerIsComputeCore
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.IsComputeCoreOp, rewriter: PatternRewriter)
¶
inline int attribute((const)) snrt_is_compute_core() { return snrt_cluster_core_idx() < snrt_cluster_compute_core_num(); }
Source code in xdsl/transforms/inline_snrt.py
LowerIsDmCore
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.IsDmCoreOp, rewriter: PatternRewriter)
¶
inline int attribute((const)) snrt_is_compute_core() { return snrt_cluster_core_idx() < snrt_cluster_compute_core_num(); }
inline int attribute((const)) snrt_is_dm_core() { return !snrt_is_compute_core(); }
Source code in xdsl/transforms/inline_snrt.py
LowerClusterCoreIdx
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.ClusterCoreIdxOp, rewriter: PatternRewriter)
¶
inline uint32_t attribute((const)) snrt_cluster_core_idx() { return snrt_global_core_idx() % snrt_cluster_core_num(); }
Source code in xdsl/transforms/inline_snrt.py
LowerClusterComputeCoreNum
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
constants: SnrtConstants
instance-attribute
¶
__init__(constants: SnrtConstants) -> None
¶
match_and_rewrite(op: snitch_runtime.ClusterComputeCoreNumOp, rewriter: PatternRewriter)
¶
Implementation:
inline uint32_t attribute((const)) snrt_cluster_compute_core_num() { return snrt_cluster_core_num() - snrt_cluster_dm_core_num(); }
inline uint32_t attribute((const)) snrt_cluster_core_num() { return SNRT_CLUSTER_CORE_NUM; }
inline uint32_t attribute((const)) snrt_cluster_dm_core_num() { return SNRT_CLUSTER_DM_CORE_NUM; }
Source code in xdsl/transforms/inline_snrt.py
LowerGlobalCoreIdx
dataclass
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
constants: SnrtConstants
instance-attribute
¶
__init__(constants: SnrtConstants) -> None
¶
match_and_rewrite(op: snitch_runtime.GlobalCoreIdxOp, rewriter: PatternRewriter)
¶
Implementation:
inline uint32_t attribute((const)) snrt_hartid() { uint32_t hartid; asm("csrr %0, mhartid" : "=r"(hartid)); return hartid; }
inline uint32_t attribute((const)) snrt_global_core_idx() { return snrt_hartid() - snrt_global_core_base_hartid(); }
Source code in xdsl/transforms/inline_snrt.py
LowerClusterIdx
¶
Bases: RewritePattern
Source code in xdsl/transforms/inline_snrt.py
match_and_rewrite(op: snitch_runtime.ClusterIdxOp, rewriter: PatternRewriter)
¶
Implementation:
inline uint32_t attribute((const)) snrt_cluster_idx() { return snrt_global_core_idx() / snrt_cluster_core_num(); }
Source code in xdsl/transforms/inline_snrt.py
InlineSnrtPass
dataclass
¶
Bases: SnrtConstants, ModulePass
Inline operations of the snrt dialect to their definitions.
Uses arith operations where possible, and emit riscv.csr* and riscv_snitch ops where required.