Skip to content

Custom pass - #17

Merged
YWHyuk merged 2 commits into
developfrom
custom_pass
Jan 26, 2024
Merged

YWHyuk merged 2 commits into
developfrom
custom_pass

Conversation

@YWHyuk

@YWHyuk YWHyuk commented Jan 17, 2024

Copy link
Copy Markdown
Collaborator

To lowering the matrix intrinsics to gemmini style instruction,
custom llvm pass was implemented. This code was originated from
LLVM source tree's LowerMatrixIntrinsics.cpp

To lowering the matrix intrinsics to gemmini style instruction,
custom llvm pass was implemented. This code was originated from
LLVM source tree's LowerMatrixIntrinsics.cpp

Signed-off-by: wonhyuk yang <wonhyuk@postech.ac.kr>
@YWHyuk
YWHyuk requested review from YunseonShin and wok1909 and removed request for YunseonShin January 17, 2024 05:12
To generate LLVM IR iniline assembly, "gemmini_extended_mv"
function was implemented.

Currently, DRAM address and SRAM address are same. This should be
handled in future

Signed-off-by: wonhyuk yang <wonhyuk@postech.ac.kr>
@YWHyuk

YWHyuk commented Jan 17, 2024 •

Copy link
Copy Markdown
Collaborator Author

Vector Add Example of this Gemmini lower pass

$ clang -S -emit-llvm -march=rv64gc -target riscv64 -O2 -fenable-matrix -mllvm -print-before=lower-matrix-intrinsics -mllvm -print-module-scope  matric.c
typedef float m4x4_t __attribute__((matrix_type(4, 4)));

void f(float *pa, float *pb) {
    m4x4_t a = __builtin_matrix_column_major_load(pa, 4, 4, 4);
    m4x4_t b = __builtin_matrix_column_major_load(pb, 4, 4, 10);
    m4x4_t r = a+b;
    __builtin_matrix_column_major_store(r, pa, 4);
}
$ opt --load-pass-plugin=./libLowerGemminiPass.so --passes=LowerGemminiPass  --print-after-all -disable-output /home/workspace/rvv-intrinsics/matric2.ll
; ModuleID = 'matric.c'
source_filename = "matric.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-unknown"

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite)
define dso_local void @f(ptr nocapture noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = tail call <16 x float> @llvm.matrix.column.major.load.v16f32.i64(ptr align 4 %0, i64 4, i1 false, i32 4, i32 4)
  %4 = tail call <16 x float> @llvm.matrix.column.major.load.v16f32.i64(ptr align 4 %1, i64 10, i1 false, i32 4, i32 4)
  %5 = fadd <16 x float> %3, %4
  tail call void @llvm.matrix.column.major.store.v16f32.i64(<16 x float> %5, ptr align 4 %0, i64 4, i1 false, i32 4, i32 4)
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare <16 x float> @llvm.matrix.column.major.load.v16f32.i64(ptr nocapture, i64, i1 immarg, i32 immarg, i32 immarg) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.matrix.column.major.store.v16f32.i64(<16 x float>, ptr nocapture writeonly, i64, i1 immarg, i32 immarg, i32 immarg) #2

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic-rv64" "target-features"="+64bit,+a,+c,+d,+f,+m,+relax,-e,-experimental-zawrs,-experimental-zca,-experimental-zcd,-experimental-zcf,-experimental-zihintntl,-experimental-ztso,-experimental-zvfh,-h,-save-restore,-svinval,-svnapot,-svpbmt,-v,-xtheadvdot,-xventanacondops,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zdinx,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zihintpause,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: read) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"target-abi", !"lp64d"}
!2 = !{i32 1, !"SmallDataLimit", i32 8}
!3 = !{!"clang version 16.0.4 (https://github.com/llvm/llvm-project ae42196bc493ffe877a7e3dff8be32035dea4d07)"}
; ModuleID = '/home/workspace/rvv-intrinsics/matric2.ll'
source_filename = "matric.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @f(ptr nocapture noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  %4 = or i64 %3, 1125917086711808
  call void asm sideeffect ".insn r 47, 2, 4, x0, $0, $1", "r,r,~{dirflag},~{fpsr},~{flags}"(i64 %3, i64 %4)
  %col.load = load <4 x float>, ptr %0, align 4
  %vec.gep = getelementptr float, ptr %0, i64 4
  %col.load1 = load <4 x float>, ptr %vec.gep, align 4
  %vec.gep2 = getelementptr float, ptr %0, i64 8
  %col.load3 = load <4 x float>, ptr %vec.gep2, align 4
  %vec.gep4 = getelementptr float, ptr %0, i64 12
  %col.load5 = load <4 x float>, ptr %vec.gep4, align 4
  %5 = ptrtoint ptr %1 to i64
  %6 = or i64 %5, 1125917086711808
  call void asm sideeffect ".insn r 47, 2, 4, x0, $0, $1", "r,r,~{dirflag},~{fpsr},~{flags}"(i64 %5, i64 %6)
  %col.load6 = load <4 x float>, ptr %1, align 4
  %vec.gep7 = getelementptr float, ptr %1, i64 10
  %col.load8 = load <4 x float>, ptr %vec.gep7, align 4
  %vec.gep9 = getelementptr float, ptr %1, i64 20
  %col.load10 = load <4 x float>, ptr %vec.gep9, align 4
  %vec.gep11 = getelementptr float, ptr %1, i64 30
  %col.load12 = load <4 x float>, ptr %vec.gep11, align 4
  %7 = fadd <4 x float> %col.load, %col.load6
  %8 = fadd <4 x float> %col.load1, %col.load8
  %9 = fadd <4 x float> %col.load3, %col.load10
  %10 = fadd <4 x float> %col.load5, %col.load12
  %11 = ptrtoint ptr %0 to i64
  store <4 x float> %7, ptr %0, align 4
  %vec.gep13 = getelementptr float, ptr %0, i64 4
  store <4 x float> %8, ptr %vec.gep13, align 4
  %vec.gep14 = getelementptr float, ptr %0, i64 8
  store <4 x float> %9, ptr %vec.gep14, align 4
  %vec.gep15 = getelementptr float, ptr %0, i64 12
  store <4 x float> %10, ptr %vec.gep15, align 4
  %12 = or i64 %11, 1125917086711808
  call void asm sideeffect ".insn r 47, 3, 4, x0, $0, $1", "r,r,~{dirflag},~{fpsr},~{flags}"(i64 %11, i64 %12)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare <16 x float> @llvm.matrix.column.major.load.v16f32.i64(ptr nocapture, i64, i1 immarg, i32 immarg, i32 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: write)
declare void @llvm.matrix.column.major.store.v16f32.i64(<16 x float>, ptr nocapture writeonly, i64, i1 immarg, i32 immarg, i32 immarg) #2

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(argmem: readwrite) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: write) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 16.0.4 (https://github.com/llvm/llvm-project ae42196bc493ffe877a7e3dff8be32035dea4d07)"}
$ llc -march=riscv64 -mattr=+m,+f,+d,+a,+c,+v -O3 matric2.ll
	.text
	.attribute	4, 16
	.attribute	5, "rv64i2p0_m2p0_a2p0_f2p0_d2p0_c2p0_v1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0"
	.file	"matric.c"
	.globl	f                               # -- Begin function f
	.p2align	1
	.type	f,@function
f:                                      # @f
# %bb.0:
	addi	a6, a1, 40
	addi	a7, a0, 16
	lui	a4, 65537
	slli	a4, a4, 22
	or	a5, a0, a4
	#APP
	.insn r 47, 2, 4, zero, a0, a5
	#NO_APP
	vsetivli	zero, 4, e32, m1, ta, ma
	vle32.v	v8, (a0)
	vle32.v	v9, (a7)
	addi	a2, a0, 32
	vle32.v	v10, (a2)
	addi	a3, a0, 48
	vle32.v	v11, (a3)
	or	a4, a4, a1
	#APP
	.insn r 47, 2, 4, zero, a1, a4
	#NO_APP
	vsetivli	zero, 4, e32, m1, ta, ma
	vle32.v	v12, (a1)
	vle32.v	v13, (a6)
	addi	a4, a1, 80
	vle32.v	v14, (a4)
	addi	a1, a1, 120
	vle32.v	v15, (a1)
	vfadd.vv	v8, v8, v12
	vfadd.vv	v9, v9, v13
	vfadd.vv	v10, v10, v14
	vfadd.vv	v11, v11, v15
	vse32.v	v8, (a0)
	vse32.v	v9, (a7)
	vse32.v	v10, (a2)
	vse32.v	v11, (a3)
	#APP
	.insn r 47, 3, 4, zero, a0, a5
	#NO_APP
	ret
.Lfunc_end0:
	.size	f, .Lfunc_end0-f
                                        # -- End function
	.ident	"clang version 16.0.4 (https://github.com/llvm/llvm-project ae42196bc493ffe877a7e3dff8be32035dea4d07)"
	.section	".note.GNU-stack","",@progbits

@wok1909 @YunSunny00
Check this example

@YWHyuk
YWHyuk merged commit 7d15666 into develop Jan 26, 2024
@YWHyuk
YWHyuk deleted the custom_pass branch January 26, 2024 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants