Repository: tetratelabs/wazero
Pull request #1692 (2023-09-08 02:29:26)

wazevo(arm64): fixes constant call argument inlining
#1496 


Patch:
diff --git a/internal/engine/wazevo/backend/isa/arm64/abi.go b/internal/engine/wazevo/backend/isa/arm64/abi.go
index 37a2a42b..db02d69d 100644
--- a/internal/engine/wazevo/backend/isa/arm64/abi.go
+++ b/internal/engine/wazevo/backend/isa/arm64/abi.go
@@ -262,8 +262,7 @@ func (a *abiImpl) callerGenVRegToFunctionArg(argIndex int, reg regalloc.VReg, de
 	if def != nil && def.IsFromInstr() {
 		// Constant instructions are inlined.
 		if inst := def.Instr; inst.Constant() {
-			a.m.InsertLoadConstant(inst, arg.Reg)
-			return
+			a.m.InsertLoadConstant(inst, reg)
 		}
 	}
 	if arg.Kind == backend.ABIArgKindReg {
diff --git a/internal/engine/wazevo/backend/regalloc/regalloc.go b/internal/engine/wazevo/backend/regalloc/regalloc.go
index d4fa30ee..af01d593 100644
--- a/internal/engine/wazevo/backend/regalloc/regalloc.go
+++ b/internal/engine/wazevo/backend/regalloc/regalloc.go
@@ -224,7 +224,7 @@ func (a *Allocator) livenessAnalysis(f Function) {
 		}
 
 		if wazevoapi.RegAllocLoggingEnabled {
-			fmt.Printf("constructed block info for block[%d]:\n%s\n\n", blk.ID(), info)
+			fmt.Printf("prepared block info for block[%d]:\n%s\n\n", blk.ID(), info.Format(a.regInfo))
 		}
 	}
 
@@ -263,7 +263,7 @@ func (a *Allocator) livenessAnalysis(f Function) {
 		}
 
 		if wazevoapi.RegAllocLoggingEnabled {
-			fmt.Printf("\nfinalized info for block[%d]:\n%s\n", blk.ID(), info)
+			fmt.Printf("\nfinalized info for block[%d]:\n%s\n", blk.ID(), info.Format(a.regInfo))
 		}
 	}
 }
@@ -610,7 +610,7 @@ func (i *blockInfo) addRealRegUsage(v VReg, pc programCounter) {
 }
 
 // String implements fmt.Stringer for debugging.
-func (i *blockInfo) String() string {
+func (i *blockInfo) Format(ri *RegisterInfo) string {
 	var buf strings.Builder
 	buf.WriteString("\tliveOuts: ")
 	for v := range i.liveOuts {
@@ -634,11 +634,11 @@ func (i *blockInfo) String() string {
 	}
 	buf.WriteString("\n\trealRegUses: ")
 	for v, pos := range i.realRegUses {
-		buf.WriteString(fmt.Sprintf("%v@%v ", v, pos))
+		buf.WriteString(fmt.Sprintf("%s@%v ", ri.RealRegName(v.RealReg()), pos))
 	}
 	buf.WriteString("\n\trealRegDefs: ")
 	for v, pos := range i.realRegDefs {
-		buf.WriteString(fmt.Sprintf("%v@%v ", v, pos))
+		buf.WriteString(fmt.Sprintf("%s@%v ", ri.RealRegName(v.RealReg()), pos))
 	}
 	return buf.String()
 }


Test patch:
diff --git a/internal/engine/wazevo/backend/backend_test.go b/internal/engine/wazevo/backend/backend_test.go
index 6fc287da..248e803a 100644
--- a/internal/engine/wazevo/backend/backend_test.go
+++ b/internal/engine/wazevo/backend/backend_test.go
@@ -568,7 +568,8 @@ L1 (SSA Block: blk0):
 	mov x0, x0?
 	mov x1, x1?
 	mov x2, x2?
-	movz w3, #0x5, lsl 0
+	movz w3?, #0x5, lsl 0
+	mov x3, x3?
 	bl f2
 	mov x4?, x0
 	str x1?, [x0?, #0x8]
diff --git a/internal/engine/wazevo/backend/isa/arm64/abi_test.go b/internal/engine/wazevo/backend/isa/arm64/abi_test.go
index 4f621317..f100ea77 100644
--- a/internal/engine/wazevo/backend/isa/arm64/abi_test.go
+++ b/internal/engine/wazevo/backend/isa/arm64/abi_test.go
@@ -259,3 +259,17 @@ add sp, sp, x27`, formatEmittedInstructionsInCurrentBlock(m))
 sub sp, sp, x27`, formatEmittedInstructionsInCurrentBlock(m))
 	})
 }
+
+func TestAbiImpl_callerGenVRegToFunctionArg_constant_inlining(t *testing.T) {
+	_, builder, m := newSetupWithMockContext()
+
+	i64 := builder.AllocateInstruction().AsIconst64(10).Insert(builder)
+	f64 := builder.AllocateInstruction().AsF64const(3.14).Insert(builder)
+	abi := m.getOrCreateABIImpl(&ssa.Signature{Params: []ssa.Type{ssa.TypeI64, ssa.TypeF64}})
+	abi.callerGenVRegToFunctionArg(0, regalloc.VReg(100).SetRegType(regalloc.RegTypeInt), &backend.SSAValueDefinition{Instr: i64, RefCount: 1})
+	abi.callerGenVRegToFunctionArg(1, regalloc.VReg(50).SetRegType(regalloc.RegTypeFloat), &backend.SSAValueDefinition{Instr: f64, RefCount: 1})
+	require.Equal(t, `movz x100?, #0xa, lsl 0
+mov x0, x100?
+ldr d50?, #8; b 16; data.f64 3.140000
+mov v0.8b, v50?.8b`, formatEmittedInstructionsInCurrentBlock(m))
+}