Skip to content

cmd/compile: rewrite rulegen to not generate plaintext Go code directly #30810

Closed
@mvdan

Description

@mvdan

cmd/compile/internal/ssa/gen/rulegen.go is a program that generates the code for all the SSA rewrite rules.

The way it currently works, the code is almost always generated directly. This keeps the code simple, but also limits it quite a bit. For example, it's hard to tell if a variable will be used or not, so declarations like typ := &config.Types tend to be followed by _ = typ.

It also means we tend to write repetitive and verbose code. For example, these two if statements could be collapsed like if X || Y { break }:

if z1.Op != OpAMD64SHRLconst {
        break
}
if z1.AuxInt != 31 {
        break
}

@josharian tried to do this last bit with string handling in https://go-review.googlesource.com/c/go/+/167438, but that turns into spaghetti code that noone can maintain.

There are other ways we could post-process the code to simplify it, too. For example, we could move declarations closer to their uses, and we could remove unused variable declarations instead of using so many _ = foo safeguards.

We could use go/ast for this, but that's probably too complex for our needs. The generated code only uses a tiny subset of Go, and we could have nodes for higher-level concepts like "rewrite this value".

The last step of the program would be to stringify the node tree, and pass it through go/format as usual.

The purpose of this issue is first to gather feedback. If it sounds like a good idea, I'd like to begin work on this soon, hopefully before the freeze is here again. The first incremental step would probably be to rewrite rulegen to use the intermediate node tree, without changing the generated code at all.

/cc @josharian @mundaym @cherrymui @randall77 @dr2chase

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.early-in-cycleA change that should be done early in the 3 month dev cycle.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions