[go] cmd/compile: tweak rewriting to leave original exported declarations

1 view
Skip to first unread message

David Chase (Gerrit)

unread,
Jun 9, 2026, 5:23:11 PM (18 hours ago) Jun 9
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

David Chase has uploaded the change for review

Commit message

cmd/compile: tweak rewriting to leave original exported declarations

While testing unsafe.Sizeof, discovered problems with exported
functions and variables. Leaving the originals behind puts them
in the export data and allows the pre-rewrite parse to work correctly.
Change-Id: Icd53978ab0f448c2fdb126bcd4decd0403f564cc

Change diff

diff --git a/src/cmd/compile/internal/midway/analysis.go b/src/cmd/compile/internal/midway/analysis.go
index d977957..45c8ded 100644
--- a/src/cmd/compile/internal/midway/analysis.go
+++ b/src/cmd/compile/internal/midway/analysis.go
@@ -74,11 +74,9 @@
return false
}
// Walk the body and check identifiers
+ // This will also note any variable references that are dependent.
found := false
syntax.Inspect(fn.Body, func(n syntax.Node) bool {
- if found {
- return false
- }
if id, ok := n.(*syntax.Name); ok {
obj := a.info.Uses[id]
if obj == nil {
@@ -98,6 +96,10 @@
}
}
if a.isDependentType(obj.Type()) {
+ if obj, ok := obj.(*types2.Var); ok && !a.dependentObj[obj] {
+ // For some reason, variable references need this special treatment
+ a.dependentObj[obj] = true
+ }
found = true
return false
}
diff --git a/src/cmd/compile/internal/midway/rewrite.go b/src/cmd/compile/internal/midway/rewrite.go
index 34269e6..022b036 100644
--- a/src/cmd/compile/internal/midway/rewrite.go
+++ b/src/cmd/compile/internal/midway/rewrite.go
@@ -93,7 +93,12 @@
}

if r.analyzer.HasDependentSignature(sig) {
- // Drop dependent signatures entirely
+ if o := r.info.Defs[d.Name]; o != nil && !o.Exported() {
+ // Drop unexported dependent signatures entirely
+ continue
+ }
+ d.Body = r.blockOf(d.Pos(), r.panicStmt(d.Pos(), "Unexpected call of original function rewritten to specialized SIMD"))
+ newDecls = append(newDecls, d)
continue
}

@@ -110,7 +115,7 @@
break // Keep entire var decl if any name is clean, else drop
}
}
- if keep {
+ if keep || true {
newDecls = append(newDecls, d)
}
case *syntax.TypeDecl:
@@ -285,20 +290,33 @@
switchStmt.Body = append(switchStmt.Body, caseClause)
}

- fnName := "panic"
- fnIdent := pe(syntax.NewName(d.Pos(), fnName))
+ panicStmt := r.panicStmt(d.Pos(), "unsupported vector size in simd-rewritten code")
+ return r.blockOf(d.Pos(), switchStmt, panicStmt)
+}

+func (r *Rewriter) blockOf(p syntax.Pos, stmts ...syntax.Stmt) *syntax.BlockStmt {
+ for _, s := range stmts {
+ s.SetPos(p)
+ }
+ blockStmt := &syntax.BlockStmt{List: stmts}
+ blockStmt.SetPos(p)
+ return blockStmt
+}
+
+func (r *Rewriter) panicStmt(p syntax.Pos, unquotedMessage string) *syntax.ExprStmt {
+ pe := func(e syntax.Expr) syntax.Expr {
+ e.SetPos(p)
+ return e
+ }
+ fnName := "panic"
+ fnIdent := pe(syntax.NewName(p, fnName))
callExpr := pe(&syntax.CallExpr{
Fun: fnIdent,
- ArgList: []syntax.Expr{pe(&syntax.BasicLit{Value: "\"unsupported vector size in simd-rewritten code\"", Kind: syntax.StringLit})},
+ ArgList: []syntax.Expr{pe(&syntax.BasicLit{Value: `"` + unquotedMessage + `"`, Kind: syntax.StringLit})},
})
-
panicStmt := &syntax.ExprStmt{X: callExpr}
- blockStmt := &syntax.BlockStmt{List: []syntax.Stmt{ps(switchStmt), ps(panicStmt)}}
-
- blockStmt.SetPos(d.Pos())
-
- return blockStmt
+ panicStmt.SetPos(p)
+ return panicStmt
}

func (r *Rewriter) generateForSize(fileAST *syntax.File, k int, newDecls []syntax.Decl) []syntax.Decl {

Change information

Files:
  • M src/cmd/compile/internal/midway/analysis.go
  • M src/cmd/compile/internal/midway/rewrite.go
Change size: S
Delta: 2 files changed, 34 insertions(+), 14 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icd53978ab0f448c2fdb126bcd4decd0403f564cc
Gerrit-Change-Number: 788960
Gerrit-PatchSet: 1
Gerrit-Owner: David Chase <drc...@google.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages