From b0dd47820b95b97fb5b4764d36efcc14797ea4fa Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 25 Jun 2024 09:42:13 -0700 Subject: [PATCH 1/3] regalloc: simplifies findOrSpillAllocatable Signed-off-by: Takeshi Yoneda --- internal/engine/wazevo/backend/regalloc/regalloc.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/internal/engine/wazevo/backend/regalloc/regalloc.go b/internal/engine/wazevo/backend/regalloc/regalloc.go index b30ffcec8e..d7839db913 100644 --- a/internal/engine/wazevo/backend/regalloc/regalloc.go +++ b/internal/engine/wazevo/backend/regalloc/regalloc.go @@ -267,13 +267,7 @@ func (a *Allocator[I, B]) findOrSpillAllocatable(s *state[I, B], allocatable []R r = RealRegInvalid // First, check if the preferredMask has any allocatable register. if preferred != RealRegInvalid && !forbiddenMask.has(preferred) && !s.regsInUse.has(preferred) { - for _, candidateReal := range allocatable { - // TODO: we should ensure the preferred register is in the allocatable set in the first place, - // but right now, just in case, we check it here. - if candidateReal == preferred { - return preferred - } - } + return preferred } var lastUseAt programCounter @@ -300,7 +294,7 @@ func (a *Allocator[I, B]) findOrSpillAllocatable(s *state[I, B], allocatable []R isPreferred := candidateReal == preferred // last == -1 means the value won't be used anymore. - if last := using.lastUse; r == RealRegInvalid || isPreferred || last == -1 || (lastUseAt != -1 && last > lastUseAt) { + if last := using.lastUse; r == RealRegInvalid || isPreferred || last == -1 || last > lastUseAt { lastUseAt = last r = candidateReal spillVReg = using.v From 9dc59267305c39d20646a5efed5c7724fffdc034 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 25 Jun 2024 09:49:25 -0700 Subject: [PATCH 2/3] fix Signed-off-by: Takeshi Yoneda --- internal/engine/wazevo/backend/regalloc/regalloc.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/engine/wazevo/backend/regalloc/regalloc.go b/internal/engine/wazevo/backend/regalloc/regalloc.go index d7839db913..b30ffcec8e 100644 --- a/internal/engine/wazevo/backend/regalloc/regalloc.go +++ b/internal/engine/wazevo/backend/regalloc/regalloc.go @@ -267,7 +267,13 @@ func (a *Allocator[I, B]) findOrSpillAllocatable(s *state[I, B], allocatable []R r = RealRegInvalid // First, check if the preferredMask has any allocatable register. if preferred != RealRegInvalid && !forbiddenMask.has(preferred) && !s.regsInUse.has(preferred) { - return preferred + for _, candidateReal := range allocatable { + // TODO: we should ensure the preferred register is in the allocatable set in the first place, + // but right now, just in case, we check it here. + if candidateReal == preferred { + return preferred + } + } } var lastUseAt programCounter @@ -294,7 +300,7 @@ func (a *Allocator[I, B]) findOrSpillAllocatable(s *state[I, B], allocatable []R isPreferred := candidateReal == preferred // last == -1 means the value won't be used anymore. - if last := using.lastUse; r == RealRegInvalid || isPreferred || last == -1 || last > lastUseAt { + if last := using.lastUse; r == RealRegInvalid || isPreferred || last == -1 || (lastUseAt != -1 && last > lastUseAt) { lastUseAt = last r = candidateReal spillVReg = using.v From 355b90d09b01a68b17bf8a3dfe338f0799ac951b Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 25 Jun 2024 09:49:40 -0700 Subject: [PATCH 3/3] fix Signed-off-by: Takeshi Yoneda --- internal/engine/wazevo/backend/regalloc/regalloc.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/internal/engine/wazevo/backend/regalloc/regalloc.go b/internal/engine/wazevo/backend/regalloc/regalloc.go index b30ffcec8e..bdb5eebccc 100644 --- a/internal/engine/wazevo/backend/regalloc/regalloc.go +++ b/internal/engine/wazevo/backend/regalloc/regalloc.go @@ -267,13 +267,7 @@ func (a *Allocator[I, B]) findOrSpillAllocatable(s *state[I, B], allocatable []R r = RealRegInvalid // First, check if the preferredMask has any allocatable register. if preferred != RealRegInvalid && !forbiddenMask.has(preferred) && !s.regsInUse.has(preferred) { - for _, candidateReal := range allocatable { - // TODO: we should ensure the preferred register is in the allocatable set in the first place, - // but right now, just in case, we check it here. - if candidateReal == preferred { - return preferred - } - } + return preferred } var lastUseAt programCounter