Skip to content

ssa: simplifies basicBlock.params #2212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions internal/engine/wazevo/ssa/basic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ type (
basicBlock struct {
id BasicBlockID
rootInstr, currentInstr *Instruction
params []blockParam
predIter int
preds []basicBlockPredecessorInfo
success []*basicBlock
// params are Values that represent parameters to a basicBlock.
// Each parameter can be considered as an output of PHI instruction in traditional SSA.
params []Value
predIter int
preds []basicBlockPredecessorInfo
success []*basicBlock
// singlePred is the alias to preds[0] for fast lookup, and only set after Seal is called.
singlePred *basicBlock
// lastDefinitions maps Variable to its last definition in this block.
Expand Down Expand Up @@ -128,15 +130,6 @@ type (
// BasicBlockID is the unique ID of a basicBlock.
BasicBlockID uint32

// blockParam implements Value and represents a parameter to a basicBlock.
blockParam struct {
// value is the Value that corresponds to the parameter in this block,
// and can be considered as an output of PHI instruction in traditional SSA.
value Value
// typ is the type of the parameter.
typ Type
}

unknownValue struct {
// variable is the variable that this unknownValue represents.
variable Variable
Expand Down Expand Up @@ -190,13 +183,13 @@ func (bb *basicBlock) ReturnBlock() bool {
// AddParam implements BasicBlock.AddParam.
func (bb *basicBlock) AddParam(b Builder, typ Type) Value {
paramValue := b.allocateValue(typ)
bb.params = append(bb.params, blockParam{typ: typ, value: paramValue})
bb.params = append(bb.params, paramValue)
return paramValue
}

// addParamOn adds a parameter to this block whose value is already allocated.
func (bb *basicBlock) addParamOn(typ Type, value Value) {
bb.params = append(bb.params, blockParam{typ: typ, value: value})
func (bb *basicBlock) addParamOn(value Value) {
bb.params = append(bb.params, value)
}

// Params implements BasicBlock.Params.
Expand All @@ -206,8 +199,7 @@ func (bb *basicBlock) Params() int {

// Param implements BasicBlock.Param.
func (bb *basicBlock) Param(i int) Value {
p := &bb.params[i]
return p.value
return bb.params[i]
}

// Valid implements BasicBlock.Valid.
Expand Down Expand Up @@ -339,7 +331,7 @@ func (bb *basicBlock) addPred(blk BasicBlock, branch *Instruction) {
func (bb *basicBlock) FormatHeader(b Builder) string {
ps := make([]string, len(bb.params))
for i, p := range bb.params {
ps[i] = p.value.formatWithType(b)
ps[i] = p.formatWithType(b)
}

if len(bb.preds) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/wazevo/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func (b *builder) Seal(raw BasicBlock) {
for _, v := range blk.unknownValues {
variable, phiValue := v.variable, v.value
typ := b.definedVariableType(variable)
blk.addParamOn(typ, phiValue)
blk.addParamOn(phiValue)
for i := range blk.preds {
pred := &blk.preds[i]
predValue := b.findValue(typ, variable, pred.blk)
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/wazevo/ssa/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func passRedundantPhiEliminationOpt(b *builder) {
paramNum := len(blk.params)

for paramIndex := 0; paramIndex < paramNum; paramIndex++ {
phiValue := blk.params[paramIndex].value
phiValue := blk.params[paramIndex]
redundant := true

nonSelfReferencingValue := ValueInvalid
Expand Down Expand Up @@ -184,7 +184,7 @@ func passRedundantPhiEliminationOpt(b *builder) {

// Still need to have the definition of the value of the PHI (previously as the parameter).
for _, redundantParamIndex := range redundantParameterIndexes {
phiValue := blk.params[redundantParamIndex].value
phiValue := blk.params[redundantParamIndex]
onlyValue := b.redundantParameterIndexToValue[redundantParamIndex]
// Create an alias in this block from the only phi argument to the phi value.
b.alias(phiValue, onlyValue)
Expand Down
Loading