Skip to content

Commit af6999e

Browse files
limeidanabner-chenc
authored andcommitted
cmd/compile: implement jump table on loong64
Following CL 357330, use jump tables on Loong64. goos: linux goarch: loong64 pkg: cmd/compile/internal/test cpu: Loongson-3A6000-HV @ 2500.00MHz │ old │ new │ │ sec/op │ sec/op vs base │ Switch8Predictable 2.352n ± 0% 2.101n ± 0% -10.65% (p=0.000 n=10) Switch8Unpredictable 11.99n ± 0% 10.25n ± 0% -14.51% (p=0.000 n=10) Switch32Predictable 3.153n ± 0% 1.887n ± 1% -40.14% (p=0.000 n=10) Switch32Unpredictable 12.47n ± 0% 10.22n ± 0% -18.00% (p=0.000 n=10) SwitchStringPredictable 3.162n ± 0% 3.352n ± 0% +6.01% (p=0.000 n=10) SwitchStringUnpredictable 14.70n ± 0% 13.31n ± 0% -9.46% (p=0.000 n=10) SwitchTypePredictable 3.702n ± 0% 2.201n ± 0% -40.55% (p=0.000 n=10) SwitchTypeUnpredictable 16.18n ± 0% 14.48n ± 0% -10.51% (p=0.000 n=10) SwitchInterfaceTypePredictable 7.654n ± 0% 9.680n ± 0% +26.47% (p=0.000 n=10) SwitchInterfaceTypeUnpredictable 22.04n ± 0% 22.44n ± 0% +1.81% (p=0.000 n=10) geomean 7.441n 6.469n -13.07% Change-Id: Id6f30fa73349c60fac17670084daee56973a955f Reviewed-on: https://go-review.googlesource.com/c/go/+/705396 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Junyang Shao <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: abner chenc <[email protected]>
1 parent 63cd912 commit af6999e

File tree

8 files changed

+72
-14
lines changed

8 files changed

+72
-14
lines changed

src/cmd/compile/internal/loong64/ssa.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,29 @@ func ssaGenBlock(s *ssagen.State, b, next *ssa.Block) {
12661266
p.From.Reg = b.Controls[0].Reg()
12671267
}
12681268
}
1269+
case ssa.BlockLOONG64JUMPTABLE:
1270+
// ALSLV $3, Rarg0, Rarg1, REGTMP
1271+
// MOVV (REGTMP), REGTMP
1272+
// JMP (REGTMP)
1273+
p := s.Prog(loong64.AALSLV)
1274+
p.From.Type = obj.TYPE_CONST
1275+
p.From.Offset = 3 // idx*8
1276+
p.Reg = b.Controls[0].Reg()
1277+
p.AddRestSourceReg(b.Controls[1].Reg())
1278+
p.To.Type = obj.TYPE_REG
1279+
p.To.Reg = loong64.REGTMP
1280+
p1 := s.Prog(loong64.AMOVV)
1281+
p1.From.Type = obj.TYPE_MEM
1282+
p1.From.Reg = loong64.REGTMP
1283+
p1.From.Offset = 0
1284+
p1.To.Type = obj.TYPE_REG
1285+
p1.To.Reg = loong64.REGTMP
1286+
p2 := s.Prog(obj.AJMP)
1287+
p2.To.Type = obj.TYPE_MEM
1288+
p2.To.Reg = loong64.REGTMP
1289+
// Save jump tables for later resolution of the target blocks.
1290+
s.JumpTables = append(s.JumpTables, b)
1291+
12691292
default:
12701293
b.Fatalf("branch not implemented: %s", b.LongString())
12711294
}

src/cmd/compile/internal/ssa/_gen/LOONG64.rules

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@
504504
(MOVBUreg x:((SGT|SGTU) _ _)) => x
505505
(MOVBUreg x:(XOR (MOVVconst [1]) ((SGT|SGTU) _ _))) => x
506506

507+
(JumpTable idx) => (JUMPTABLE {makeJumpTableSym(b)} idx (MOVVaddr <typ.Uintptr> {makeJumpTableSym(b)} (SB)))
508+
507509
// Write barrier.
508510
(WB ...) => (LoweredWB ...)
509511

src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ func init() {
577577
{name: "BLT", controls: 2}, // controls[0] < controls[1]
578578
{name: "BGEU", controls: 2}, // controls[0] >= controls[1], unsigned
579579
{name: "BLTU", controls: 2}, // controls[0] < controls[1], unsigned
580+
581+
// JUMPTABLE implements jump tables.
582+
// Aux is the symbol (an *obj.LSym) for the jump table.
583+
// control[0] is the index into the jump table.
584+
// control[1] is the address of the jump table (the address of the symbol stored in Aux).
585+
{name: "JUMPTABLE", controls: 2, aux: "Sym"},
580586
}
581587

582588
archs = append(archs, arch{

src/cmd/compile/internal/ssa/opGen.go

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/rewriteLOONG64.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/internal/obj/loong64/asm.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,15 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
707707
// so instruction sequences that use REGTMP are unsafe to
708708
// preempt asynchronously.
709709
obj.MarkUnsafePoints(c.ctxt, c.cursym.Func().Text, c.newprog, c.isUnsafePoint, c.isRestartable)
710+
711+
// Now that we know byte offsets, we can generate jump table entries.
712+
for _, jt := range cursym.Func().JumpTables {
713+
for i, p := range jt.Targets {
714+
// The ith jumptable entry points to the p.Pc'th
715+
// byte in the function symbol s.
716+
jt.Sym.WriteAddr(ctxt, int64(i)*8, 8, cursym, p.Pc)
717+
}
718+
}
710719
}
711720

712721
// isUnsafePoint returns whether p is an unsafe point.

src/cmd/internal/sys/arch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ var ArchLoong64 = &Arch{
145145
MinLC: 4,
146146
Alignment: 8, // Unaligned accesses are not guaranteed to be fast
147147
CanMergeLoads: true,
148+
CanJumpTable: true,
148149
HasLR: true,
149150
FixedFrameSize: 8, // LR
150151
}

test/codegen/switch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func f(x string) int {
2525
func square(x int) int {
2626
// amd64:`JMP\s\(.*\)\(.*\)$`
2727
// arm64:`MOVD\s\(R.*\)\(R.*<<3\)`,`JMP\s\(R.*\)$`
28+
// loong64: `ALSLV`,`MOVV`,`JMP`
2829
switch x {
2930
case 1:
3031
return 1
@@ -51,6 +52,7 @@ func square(x int) int {
5152
func length(x string) int {
5253
// amd64:`JMP\s\(.*\)\(.*\)$`
5354
// arm64:`MOVD\s\(R.*\)\(R.*<<3\)`,`JMP\s\(R.*\)$`
55+
// loong64:`ALSLV`,`MOVV`,`JMP`
5456
switch x {
5557
case "a":
5658
return 1

0 commit comments

Comments
 (0)