Skip to content

Commit

Permalink
Refine vsetvl[i] (for #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksco committed Apr 7, 2024
1 parent d5ab039 commit 54db55c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 93 deletions.
1 change: 1 addition & 0 deletions Makefrag
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ tests = \
vsetivli-0 \
vsetvl-0 \
vsetvli-0 \
vsetvli-1 \
vsext.vf2-0 \
vsext.vf2-1 \
vsext.vf4-0 \
Expand Down
1 change: 1 addition & 0 deletions configs/vsetvl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ format = "vsetvl"
[tests]
base = [
[0x0, 0x0, 0x0],
[0x0, 0x0, 0xa],
[0x0, 0x0, 0xd],
[0x1, 0x1, 0x2],
[0x3, 0x3, 0xf]
Expand Down
10 changes: 3 additions & 7 deletions generator/insn_vsetivli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ func (i *Insn) genCodevsetivli(pos int) []string {
res := make([]string, 0, len(combinations))
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("# ------combination test begin---------\n"))
builder.WriteString(c.comment())

cases := i.testCases(false, 8)
curvtype := int64(0)
curvl := int64(0)
for _, cs := range cases {
builder.WriteString(fmt.Sprintf("# ------case test begin---------\n"))
builder.WriteString("# -------------- TEST BEGIN --------------\n")
builder.WriteString(fmt.Sprintf("li t0, %d\n", cs[0]))
builder.WriteString(fmt.Sprintf("vsetivli t0, %d, %s,%s,%s,%s\n",
cs[1], c.SEW, c.LMUL, ta(c.vta), ma(c.vma)))

v := vtype{float32(c.LMUL), int(c.SEW), c.vta, c.vma}
t := v.vtypeImm(int(i.Option.XLEN), int(i.Option.VLEN), curvtype, curvl, int64(cs[0].(uint8)), int64(cs[1].(uint8)))
t := v.vtypeImm(int(i.Option.XLEN), int(i.Option.VLEN), curvtype, int64(cs[1].(uint8)))

builder.WriteString(fmt.Sprintf("csrr a4, vstart\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[0]))
Expand All @@ -46,11 +44,9 @@ func (i *Insn) genCodevsetivli(pos int) []string {
builder.WriteString(fmt.Sprintf("csrr a4, vl\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[2]))
ncase = ncase + 1

curvtype = t[1]
curvl = t[2]

builder.WriteString(fmt.Sprintf("# ------case test end---------\n"))
builder.WriteString("# -------------- TEST BEGIN --------------\n")
}
res = append(res, builder.String())
}
Expand Down
121 changes: 62 additions & 59 deletions generator/insn_vsetvl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,32 @@ func bits64(val int64, first int, last int) int64 {
return (val >> last) & (1<<nbits - 1)
}

func (v *vtype) vtypeRaw(XLEN int, VLEN int, newVtypeRaw int64, curVtypeRaw int64, curVl int64, rd int64, rs1 int64) []int64 {
func (v *vtype) vtypeRaw(XLEN int, VLEN int, newVtypeRaw int64, curVtypeRaw int64, vl int64) []int64 {
res := make([]int64, 0, 3)
// vstart
res = append(res, 0)
var vstart int64 = 0
res = append(res, vstart)

vlmax := int64(getVlmax(curVtypeRaw, VLEN))
VtypeRaw := newVtypeRaw
if VtypeRaw != curVtypeRaw {
vlmax = int64(getVlmax(VtypeRaw, VLEN))
newLmul := getLmul(VtypeRaw)
newVsew := getSew(VtypeRaw)
vtypeRaw := newVtypeRaw
if vtypeRaw != curVtypeRaw {
vlmax = int64(getVlmax(vtypeRaw, VLEN))
newLmul := getLmul(vtypeRaw)
newVsew := getSew(vtypeRaw)
// We assume ELEN is consistent with XLEN.
new_vill :=
!(newLmul >= 0.125 && newLmul <= 8) ||
float64(newVsew) > math.Min(float64(newLmul), float64(1))*float64(XLEN) ||
bits64(VtypeRaw, XLEN-2, 8) != 0
bits64(vtypeRaw, XLEN-2, 8) != 0
if new_vill {
vlmax = 0
VtypeRaw = 1 << (XLEN - 1)
vtypeRaw = 1 << (XLEN - 1)
}
}
// vtype
res = append(res, VtypeRaw)

vl := curVl
if vlmax == 0 {
vl = 0
} else if rd == 0 && rs1 == 0 {
if curVl > int64(vlmax) {
vl = curVl
} else {
vl = curVl
}
} else if rd != 0 && rs1 == 0 {
res = append(res, vtypeRaw)

if vl < 0 || vl > vlmax || vlmax == 0 {
vl = vlmax
} else if rs1 != 0 {
if rs1 > vlmax {
vl = vlmax
} else {
vl = rs1
}
}
// vl
res = append(res, vl)

return res
Expand All @@ -94,36 +77,56 @@ func (i *Insn) genCodevsetvl(pos int) []string {
curvtype := int64(0)
curvl := int64(0)
for _, cs := range cases {
builder.WriteString(fmt.Sprintf("# ------case test begin---------\n"))

builder.WriteString(fmt.Sprintf("li t0, %d\n", cs[0]))
builder.WriteString(fmt.Sprintf("li t1, %d\n", cs[1]))
builder.WriteString(fmt.Sprintf("li t2, %d\n", cs[2]))
builder.WriteString(fmt.Sprintf("vsetvl t0, t1, t2\n"))

var v vtype
t := v.vtypeRaw(int(i.Option.XLEN), int(i.Option.VLEN), int64(cs[2].(uint8)), curvtype, curvl, int64(cs[0].(uint8)), int64(cs[1].(uint8)))

builder.WriteString(fmt.Sprintf("csrr a4, vstart\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[0]))
ncase = ncase + 1

builder.WriteString(fmt.Sprintf("csrr a4, vtype\n"))
if t[1] == 1<<(i.Option.XLEN-1) {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, uint64(1<<(i.Option.XLEN-1))))
} else {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[1]))
for idx := 0; idx < 3; idx++ {
builder.WriteString("# -------------- TEST BEGIN --------------\n")

builder.WriteString(fmt.Sprintf("li t0, %d\n", cs[0]))
builder.WriteString(fmt.Sprintf("li t1, %d\n", cs[1]))
builder.WriteString(fmt.Sprintf("li t2, %d\n", cs[2]))

rd := "t0"
rs := "t1"
if idx > 0 {
rs = "zero"
}
if idx == 2 {
rd = "zero"
}

builder.WriteString(fmt.Sprintf("vsetvl %s, %s, t2\n", rd, rs))

var v vtype
t := v.vtypeRaw(int(i.Option.XLEN), int(i.Option.VLEN), int64(cs[2].(uint8)), curvtype, int64(cs[1].(uint8)))

builder.WriteString(fmt.Sprintf("csrr a4, vstart\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[0]))
ncase = ncase + 1

builder.WriteString(fmt.Sprintf("csrr a4, vtype\n"))
if t[1] == 1<<(i.Option.XLEN-1) {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, uint64(1<<(i.Option.XLEN-1))))
} else {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[1]))
}
ncase = ncase + 1

switch idx {
case 0:
curvl = t[2]
case 1:
if t[1] == 1 << (i.Option.XLEN - 1) { // vill
curvl = t[2]
} else {
curvl = int64(getVlmax(curvtype, int(i.Option.VLEN)))
}
}
curvtype = t[1]

builder.WriteString(fmt.Sprintf("csrr a4, vl\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, curvl))
ncase = ncase + 1
builder.WriteString("# -------------- TEST END --------------\n")
}
ncase = ncase + 1

builder.WriteString(fmt.Sprintf("csrr a4, vl\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[2]))
ncase = ncase + 1

curvtype = t[1]
curvl = t[2]

builder.WriteString(fmt.Sprintf("# ------case test end---------\n"))
}
res = append(res, builder.String())

Expand Down
71 changes: 45 additions & 26 deletions generator/insn_vsetvli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func (v *vtype) vtypeImm(XLEN int, VLEN int, curVtypeRaw int64, curVl int64, rd int64, rs1 int64) []int64 {
func (v *vtype) vtypeImm(XLEN int, VLEN int, curVtypeRaw int64, vl int64) []int64 {
t := int64(math.Ilogb(float64(v.lmul)) & 7)
t = t + int64(math.Ilogb(float64(v.sew/8))<<3)
if v.vta {
Expand All @@ -15,7 +15,7 @@ func (v *vtype) vtypeImm(XLEN int, VLEN int, curVtypeRaw int64, curVl int64, rd
if v.vma {
t = t + (1 << 7)
}
res := v.vtypeRaw(XLEN, VLEN, t, curVtypeRaw, curVl, rd, rs1)
res := v.vtypeRaw(XLEN, VLEN, t, curVtypeRaw, vl)
return res
}

Expand All @@ -30,42 +30,61 @@ func (i *Insn) genCodevsetvli(pos int) []string {
res := make([]string, 0, len(combinations))
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("# ------combination test begin---------\n"))
builder.WriteString(c.comment())

cases := i.testCases(false, 8)
curvtype := int64(0)
curvl := int64(0)
for _, cs := range cases {
builder.WriteString(fmt.Sprintf("# ------case test begin---------\n"))
builder.WriteString(fmt.Sprintf("li t0, %d\n", cs[0]))
builder.WriteString(fmt.Sprintf("li t1, %d\n", cs[1]))
builder.WriteString(fmt.Sprintf("vsetvli t0, t1, %s,%s,%s,%s\n",
c.SEW, c.LMUL, ta(c.vta), ma(c.vma)))
for idx := 0; idx < 3; idx++ {
builder.WriteString("# -------------- TEST BEGIN --------------\n")
builder.WriteString(fmt.Sprintf("li t0, %d\n", cs[0]))
builder.WriteString(fmt.Sprintf("li t1, %d\n", cs[1]))

v := vtype{float32(c.LMUL), int(c.SEW), c.vta, c.vma}
t := v.vtypeImm(int(i.Option.XLEN), int(i.Option.VLEN), curvtype, curvl, int64(cs[0].(uint8)), int64(cs[1].(uint8)))
rd := "t0"
rs := "t1"
if idx > 0 {
rs = "zero"
}
if idx == 2 {
rd = "zero"
}

builder.WriteString(fmt.Sprintf("csrr a4, vstart\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[0]))
ncase = ncase + 1
builder.WriteString(fmt.Sprintf("vsetvli %s, %s, %s,%s,%s,%s\n", rd, rs,
c.SEW, c.LMUL, ta(c.vta), ma(c.vma)))

builder.WriteString(fmt.Sprintf("csrr a4, vtype\n"))
if t[1] == 1<<(i.Option.XLEN-1) {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, uint64(1<<(i.Option.XLEN-1))))
} else {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[1]))
}
ncase = ncase + 1
v := vtype{float32(c.LMUL), int(c.SEW), c.vta, c.vma}
t := v.vtypeImm(int(i.Option.XLEN), int(i.Option.VLEN), curvtype, int64(cs[1].(uint8)))

builder.WriteString(fmt.Sprintf("csrr a4, vstart\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[0]))
ncase = ncase + 1

builder.WriteString(fmt.Sprintf("csrr a4, vl\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[2]))
ncase = ncase + 1
builder.WriteString(fmt.Sprintf("csrr a4, vtype\n"))
if t[1] == 1<<(i.Option.XLEN-1) {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, uint64(1<<(i.Option.XLEN-1))))
} else {
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, t[1]))
}
ncase = ncase + 1

curvtype = t[1]
curvl = t[2]
switch idx {
case 0:
curvl = t[2]
case 1:
if t[1] == 1<<(i.Option.XLEN-1) { // vill
curvl = t[2]
} else {
curvl = int64(getVlmax(curvtype, int(i.Option.VLEN)))
}
}
curvtype = t[1]

builder.WriteString(fmt.Sprintf("# ------case test end---------\n"))
builder.WriteString(fmt.Sprintf("csrr a4, vl\n"))
builder.WriteString(fmt.Sprintf("TEST_CASE(%d, a4, %d)\n", ncase, curvl))
ncase = ncase + 1
builder.WriteString("# -------------- TEST END --------------\n")
}
}
res = append(res, builder.String())
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func main() {
}
insn, err := generator.ReadInsnFromToml(contents, option)
fatalIf(err)
if (insn.Name != strings.Replace(file.Name(), ".toml", "", -1)) {
if insn.Name != strings.Replace(file.Name(), ".toml", "", -1) {
fatalIf(errors.New("filename and instruction name unmatched"))
}

Expand Down

0 comments on commit 54db55c

Please sign in to comment.