Skip to content

Instantly share code, notes, and snippets.

@nazavode
Last active June 8, 2023 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazavode/d64107432f2cf6e8be20a2430203846b to your computer and use it in GitHub Desktop.
Save nazavode/d64107432f2cf6e8be20a2430203846b to your computer and use it in GitHub Desktop.
mlir-func-snitch-runtime
#include "printf.h"
#include "snrt.h"
#include <stdint.h>
void echo_from_mlir() {
uint32_t core_idx = snrt_global_core_idx();
uint32_t core_num = snrt_global_core_num();
uint32_t hart = snrt_hartid();
printf("hart %d (core %d/%d): hello from xdsl-opt -> mlir-opt -> llvm -> snitch\n", hart, core_idx, core_num);
}
; ModuleID = 'main.ll'
source_filename = "main.ll"
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-unknown-unknown-elf"
declare i32 @snrt_global_core_idx()
declare i32 @snrt_global_core_num()
declare void @echo_from_mlir()
define dso_local i32 @main() #0 {
call void @echo_from_mlir()
%1 = call i32 @snrt_global_core_idx()
%2 = call i32 @snrt_global_core_num()
%3 = add i32 %1, %2
ret i32 %3
}
!llvm.module.flags = !{!3, !4, !5, !6, !7, !8}
!3 = !{i32 7, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}
!6 = !{i32 1, !"target-abi", !"ilp32d"}
!7 = !{i32 1, !"Code Model", i32 3}
!8 = !{i32 1, !"SmallDataLimit", i32 8}
attributes #0 = { nounwind "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-
vector-width"="0" "no-builtin-printf" "no-infs-fp-math"="true" "no-jump-tables"="false" "no-nans-fp-math"="true" "no-si
gned-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="snitch" "target-fe
atures"="+a,+d,+experimental-zfh,+f,+m,+xdma,+xfalthalf,+xfaltquarter,+xfauxalthalf,+xfauxaltquarter,+xfauxhalf,+xfauxq
uarter,+xfauxvecalthalf,+xfauxvecaltquarter,+xfauxvechalf,+xfauxvecquarter,+xfauxvecsingle,+xfexpauxvecalthalf,+xfexpau
xvecaltquarter,+xfexpauxvechalf,+xfexpauxvecquarter,+xfquarter,+xfrep,+xfvecalthalf,+xfvecaltquarter,+xfvechalf,+xfvecq
uarter,+xfvecsingle,+xssr,-64bit,-relax,-save-restore" "unsafe-fp-math"="true" "use-soft-float"="false" }
attributes #1 = { "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-builtin-printf"
"no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-pro
tector-buffer-size"="8" "target-cpu"="snitch" "target-features"="+a,+d,+experimental-zfh,+f,+m,+xdma,+xfalthalf,+xfaltq
uarter,+xfauxalthalf,+xfauxaltquarter,+xfauxhalf,+xfauxquarter,+xfauxvecalthalf,+xfauxvecaltquarter,+xfauxvechalf,+xfau
xvecquarter,+xfauxvecsingle,+xfexpauxvecalthalf,+xfexpauxvecaltquarter,+xfexpauxvechalf,+xfexpauxvecquarter,+xfquarter,
+xfrep,+xfvecalthalf,+xfvecaltquarter,+xfvechalf,+xfvecquarter,+xfvecsingle,+xssr,-64bit,-relax,-save-restore" "unsafe-
fp-math"="true" "use-soft-float"="false" }
# Snitch runtime
SNRT_BANSHEE = /root/src/mlir/snRuntime-banshee
SNRT_VERILATOR = /root/src/mlir/snRuntime-cluster
CC1 = /tools/riscv-llvm/bin/clang-12 -cc1
LD = /tools/riscv-llvm/bin/riscv32-unknown-elf-clang
CC1FLAGS =
CC1FLAGS += -triple riscv32-unknown-unknown-elf
CC1FLAGS += -emit-llvm-bc
CC1FLAGS += -flto=thin
CC1FLAGS += -flto-unit
CC1FLAGS += -disable-free
CC1FLAGS += -disable-llvm-verifier
CC1FLAGS += -fno-builtin-printf
CC1FLAGS += -fno-signed-char
CC1FLAGS += -static-define
CC1FLAGS += -mrelocation-model static
CC1FLAGS += -mframe-pointer=none
CC1FLAGS += -menable-no-infs
CC1FLAGS += -menable-no-nans
CC1FLAGS += -menable-unsafe-fp-math
CC1FLAGS += -fno-signed-zeros
CC1FLAGS += -mreassociate
CC1FLAGS += -freciprocal-math
CC1FLAGS += -ffp-contract=fast
CC1FLAGS += -fno-rounding-math
CC1FLAGS += -ffast-math
CC1FLAGS += -ffinite-math-only
CC1FLAGS += -nostdsysteminc
CC1FLAGS += -mcmodel=medium
CC1FLAGS += -target-cpu snitch
CC1FLAGS += -target-feature +m
CC1FLAGS += -target-feature +a
CC1FLAGS += -target-feature +f
CC1FLAGS += -target-feature +d
CC1FLAGS += -target-feature +experimental-zfh
CC1FLAGS += -target-feature +xfrep
CC1FLAGS += -target-feature +xssr
CC1FLAGS += -target-feature +xdma
CC1FLAGS += -target-feature +xfalthalf
CC1FLAGS += -target-feature +xfquarter
CC1FLAGS += -target-feature +xfaltquarter
CC1FLAGS += -target-feature +xfvecsingle
CC1FLAGS += -target-feature +xfvechalf
CC1FLAGS += -target-feature +xfvecalthalf
CC1FLAGS += -target-feature +xfvecquarter
CC1FLAGS += -target-feature +xfvecaltquarter
CC1FLAGS += -target-feature +xfauxhalf
CC1FLAGS += -target-feature +xfauxalthalf
CC1FLAGS += -target-feature +xfauxquarter
CC1FLAGS += -target-feature +xfauxaltquarter
CC1FLAGS += -target-feature +xfauxvecsingle
CC1FLAGS += -target-feature +xfauxvechalf
CC1FLAGS += -target-feature +xfauxvecalthalf
CC1FLAGS += -target-feature +xfauxvecquarter
CC1FLAGS += -target-feature +xfauxvecaltquarter
CC1FLAGS += -target-feature +xfexpauxvechalf
CC1FLAGS += -target-feature +xfexpauxvecalthalf
CC1FLAGS += -target-feature +xfexpauxvecquarter
CC1FLAGS += -target-feature +xfexpauxvecaltquarter
CC1FLAGS += -target-feature -64bit
CC1FLAGS += -target-feature -relax
CC1FLAGS += -target-feature -save-restore
CC1FLAGS += -target-abi ilp32d
CC1FLAGS += -msmall-data-limit 8
CC1FLAGS += -ffunction-sections
CC1FLAGS += -O3
CC1FLAGS += -Wextra
CC1FLAGS += -vectorize-loops
CC1FLAGS += -vectorize-slp
CC1FLAGS += -mllvm
CC1FLAGS += -enable-misched=false
CC1FLAGS += -faddrsig
LDFLAGS =
LDFLAGS += -flto=thin
LDFLAGS += -mcpu=snitch
LDFLAGS += -nostartfiles
LDFLAGS += -fuse-ld=lld
LDFLAGS += -Wl,--image-base=0x80000000
LDFLAGS += -static
LDFLAGS += -Wl,-z,norelro
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -Wl,--no-relax
LDFLAGS += -lm
LDFLAGS_BANSHEE = $(LDFLAGS)
LDFLAGS_BANSHEE += -T $(SNRT_BANSHEE)/common.ld
LDFLAGS_BANSHEE += -lm $(SNRT_BANSHEE)/libsnRuntime-banshee.a
LDFLAGS_VERILATOR = $(LDFLAGS)
LDFLAGS_VERILATOR += -T $(SNRT_VERILATOR)/common.ld
LDFLAGS_VERILATOR += -lm $(SNRT_VERILATOR)/libsnRuntime-cluster.a
# MLIR
OPT = mlir-opt
TRANSLATE = mlir-translate
OPTFLAGS =
OPTFLAGS += --eliminate-empty-tensors
OPTFLAGS += --empty-tensor-to-alloc-tensor
OPTFLAGS += --one-shot-bufferize="bufferize-function-boundaries allow-return-allocs"
# Current lowering of all_to_all_shortest needs to save tensors
# coming from previous iterations: let's hoist allocs out of
# loop nests:
OPTFLAGS += --buffer-loop-hoisting
# Pretty standard lowering strategy down to llvm:
# taken straight from the 'test-lower-to-llvm' pass,
# the sequence below should be identical to:
# OPTFLAGS += --test-lower-to-llvm
# Blanket-convert any remaining high-level vector ops to loops if any remain.
OPTFLAGS += --convert-vector-to-scf
# Blanket-convert any remaining linalg ops to loops if any remain.
OPTFLAGS += --convert-linalg-to-affine-loops
# Blanket-convert any remaining affine ops if any remain.
OPTFLAGS += --lower-affine
# Convert SCF to CF (always needed).
OPTFLAGS += --convert-scf-to-cf
# Sprinkle some cleanups.
OPTFLAGS += --canonicalize
OPTFLAGS += --cse
# Blanket-convert any remaining linalg ops to LLVM if any remain.
OPTFLAGS += --convert-linalg-to-llvm
# Convert vector to LLVM (always needed).
OPTFLAGS += --convert-vector-to-llvm=reassociate-fp-reductions
# Convert Math to LLVM (always needed).
OPTFLAGS += --convert-math-to-llvm
# Expand complicated MemRef operations before lowering them.
OPTFLAGS += --expand-strided-metadata
# The expansion may create affine expressions. Get rid of them.
OPTFLAGS += --lower-affine
# Convert MemRef to LLVM (always needed).
OPTFLAGS += --convert-memref-to-llvm
# Convert Func to LLVM (always needed).
OPTFLAGS += --convert-func-to-llvm
# Convert Index to LLVM (always needed).
OPTFLAGS += --convert-index-to-llvm
# Convert remaining unrealized_casts (always needed).
OPTFLAGS += --reconcile-unrealized-casts
# Rules
.PRECIOUS: %.llvm.mlir
%.llvm.mlir: %.mlir
$(OPT) $(OPTFLAGS) -o $@ $<
%.ll: %.llvm.mlir
$(TRANSLATE) --mlir-to-llvmir -o $@ $<
%.c.o: %.c
$(CC1) $(CC1FLAGS) -o $@ $<
%.ll.o: %.ll
$(CC1) $(CC1FLAGS) -o $@ $<
echo.banshee.x: func.o main.o
$(LD) $(LDFLAGS_BANSHEE) -o $@ $<
echo.verilator.x: func.o main.o
$(LD) $(LDFLAGS_VERILATOR) -o $@ $<
#!/usr/bin/env bash
set -xeuo pipefail
target="$1"
name="$(basename $1)"
trace="${PWD}/${name}.trace"
banshee \
--no-opt-llvm \
--no-opt-jit \
--num-cores=9 \
--configuration /root/src/sw/banshee/config/snitch_cluster.yaml \
--trace \
"${target}" > "${trace}"
# cat /root/src/sw/examples/build/hello_world.trace | spike-dasm > /root/src/sw/examples/build/hello_world.trace.txt
#!/usr/bin/env bash
set -xeuo pipefail
target="$1"
~/src/hw/system/snitch_cluster/bin/snitch_cluster.vlt "${target}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment