-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Remove implicit floating-point FMAs #64323
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
Jake Bailey (jakebailey)
merged 4 commits into
microsoft:main
from
jakebailey:ban-implicit-fma
Sep 22, 2026
+3
−1
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package customlint | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
| "go/types" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
| "golang.org/x/tools/go/analysis/passes/buildssa" | ||
| "golang.org/x/tools/go/analysis/passes/inspect" | ||
| "golang.org/x/tools/go/ast/inspector" | ||
| "golang.org/x/tools/go/ssa" | ||
| ) | ||
|
|
||
| var implicitFMAAnalyzer = &analysis.Analyzer{ | ||
| Name: "implicitfma", | ||
| Doc: "finds floating-point additions and subtractions that may use implicit FMA", | ||
| Requires: []*analysis.Analyzer{ | ||
| buildssa.Analyzer, | ||
| inspect.Analyzer, | ||
| }, | ||
| Run: func(pass *analysis.Pass) (any, error) { | ||
| return (&implicitFMAPass{pass: pass}).run() | ||
| }, | ||
| } | ||
|
|
||
| type implicitFMAPass struct { | ||
| pass *analysis.Pass | ||
| expressionsByOpPos map[token.Pos]*ast.BinaryExpr | ||
| explicitlyRoundedMultiply map[token.Pos]bool | ||
| } | ||
|
|
||
| func (f *implicitFMAPass) run() (any, error) { | ||
| in := f.pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
| f.expressionsByOpPos = make(map[token.Pos]*ast.BinaryExpr) | ||
| f.explicitlyRoundedMultiply = make(map[token.Pos]bool) | ||
|
|
||
| for cursor := range in.Root().Preorder((*ast.BinaryExpr)(nil)) { | ||
| expr := cursor.Node().(*ast.BinaryExpr) | ||
| f.expressionsByOpPos[expr.OpPos] = expr | ||
| if expr.Op == token.MUL { | ||
| typeAndValue := f.pass.TypesInfo.Types[expr] | ||
| if typeAndValue.Value == nil && | ||
| isFloatingPointType(typeAndValue.Type) && | ||
| f.hasExplicitRoundingConversion(cursor) { | ||
| // buildssa removes representation-preserving conversions, even though an | ||
| // explicit floating-point conversion forces rounding under the Go spec. | ||
| f.explicitlyRoundedMultiply[expr.OpPos] = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ssaResult := f.pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) | ||
| reported := make(map[token.Pos]bool) | ||
| for _, function := range ssaResult.SrcFuncs { | ||
| for _, block := range function.Blocks { | ||
| for _, instruction := range block.Instrs { | ||
| binOp, ok := instruction.(*ssa.BinOp) | ||
| if !ok || binOp.Op != token.ADD && binOp.Op != token.SUB || !isFloatingPointType(binOp.Type()) { | ||
| continue | ||
| } | ||
| if !f.reachedByUnroundedMultiplication(binOp.X, make(map[ssa.Value]bool)) && | ||
| !f.reachedByUnroundedMultiplication(binOp.Y, make(map[ssa.Value]bool)) { | ||
| continue | ||
| } | ||
| if reported[binOp.Pos()] { | ||
| continue | ||
| } | ||
| reported[binOp.Pos()] = true | ||
|
|
||
| pos := binOp.Pos() | ||
| end := pos + 1 | ||
| if expr := f.expressionsByOpPos[pos]; expr != nil { | ||
| pos = expr.Pos() | ||
| end = expr.End() | ||
| } | ||
| f.pass.Report(analysis.Diagnostic{ | ||
| Pos: pos, | ||
| End: end, | ||
| Message: "explicitly round the floating-point multiplication result to prevent implicit FMA", | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| func (f *implicitFMAPass) reachedByUnroundedMultiplication(value ssa.Value, seen map[ssa.Value]bool) bool { | ||
| if seen[value] { | ||
| return false | ||
| } | ||
| seen[value] = true | ||
|
|
||
| switch value := value.(type) { | ||
| case *ssa.BinOp: | ||
| return value.Op == token.MUL && !f.explicitlyRoundedMultiply[value.Pos()] | ||
| case *ssa.ChangeInterface: | ||
| return f.reachedByUnroundedMultiplication(value.X, seen) | ||
| case *ssa.ChangeType: | ||
| return f.reachedByUnroundedMultiplication(value.X, seen) | ||
| case *ssa.Phi: | ||
| for _, edge := range value.Edges { | ||
| if f.reachedByUnroundedMultiplication(edge, seen) { | ||
| return true | ||
| } | ||
| } | ||
| case *ssa.UnOp: | ||
| if value.Op == token.ADD || value.Op == token.SUB { | ||
| return f.reachedByUnroundedMultiplication(value.X, seen) | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func (f *implicitFMAPass) hasExplicitRoundingConversion(cursor inspector.Cursor) bool { | ||
| for { | ||
| parent := cursor.Parent() | ||
| switch node := parent.Node().(type) { | ||
| case *ast.ParenExpr: | ||
| cursor = parent | ||
| case *ast.CallExpr: | ||
| if len(node.Args) != 1 || node.Args[0] != cursor.Node() { | ||
| return false | ||
| } | ||
| funTypeAndValue, ok := f.pass.TypesInfo.Types[node.Fun] | ||
| return ok && funTypeAndValue.IsType() && isFloatingPointType(f.pass.TypesInfo.TypeOf(node)) | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func isFloatingPointType(t types.Type) bool { | ||
| t = types.Unalias(t) | ||
| if t == nil { | ||
| return false | ||
| } | ||
|
|
||
| switch t := t.Underlying().(type) { | ||
| case *types.Basic: | ||
| return t.Info()&types.IsFloat != 0 | ||
| case *types.Interface: | ||
| for embedded := range t.EmbeddedTypes() { | ||
| if isFloatingPointType(embedded) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| case *types.Union: | ||
| for term := range t.Terms() { | ||
| if isFloatingPointType(term.Type()) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package implicitfma | ||
|
|
||
| type namedFloat64 float64 | ||
|
|
||
| func badFloat64(x, y, z float64) float64 { | ||
| return x*y + z | ||
| } | ||
|
|
||
| func badFloat32(x, y, z float32) float32 { | ||
| product := x * y | ||
| return z + product | ||
| } | ||
|
|
||
| func badNamed(x, y, z namedFloat64) namedFloat64 { | ||
| return x*y - z*x | ||
| } | ||
|
|
||
| func badGeneric[T ~float32 | ~float64](x, y, z T) T { | ||
| product := x * y | ||
| product += z | ||
| return product | ||
| } | ||
|
|
||
| func badConversionAroundSum(x, y, z float64) float64 { | ||
| return float64(x*y + z) | ||
| } | ||
|
|
||
| func goodStandalone(x, y float64) float64 { | ||
| return x * y | ||
| } | ||
|
|
||
| func goodFloat64(x, y, z float64) float64 { | ||
| return float64(x*y) + z | ||
| } | ||
|
|
||
| func goodBothProducts(x, y, z float64) float64 { | ||
| return float64(x*y) - float64(z*x) | ||
| } | ||
|
|
||
| func goodFloatToInt(x, y float64) int { | ||
| return int(x * y) | ||
| } | ||
|
|
||
| func goodReturnRounded(x, y float64) float64 { | ||
| return float64(x * y) | ||
| } | ||
|
|
||
| func goodParenthesized(x, y float64) float64 { | ||
| return float64((x * y)) | ||
| } | ||
|
|
||
| func goodNamed(x, y namedFloat64) namedFloat64 { | ||
| return namedFloat64(x * y) | ||
| } | ||
|
|
||
| func goodConversionToNamed(x, y, z float64) namedFloat64 { | ||
| return namedFloat64(x*y) + namedFloat64(z) | ||
| } | ||
|
|
||
| func goodConstant() float64 { | ||
| return 1.5 * 2.5 | ||
| } | ||
|
|
||
| func goodInteger(x, y int) int { | ||
| return x * y | ||
| } |
77 changes: 77 additions & 0 deletions
77
tools/customlint/testdata/implicitfma/implicitfma.go.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package implicitfma | ||
|
|
||
| type namedFloat64 float64 | ||
|
|
||
| func badFloat64(x, y, z float64) float64 { | ||
| return x*y + z | ||
| ~~~~~~~ | ||
| !!! implicitfma: explicitly round the floating-point multiplication result to prevent implicit FMA | ||
| } | ||
|
|
||
| func badFloat32(x, y, z float32) float32 { | ||
| product := x * y | ||
| return z + product | ||
| ~~~~~~~~~~~ | ||
| !!! implicitfma: explicitly round the floating-point multiplication result to prevent implicit FMA | ||
| } | ||
|
|
||
| func badNamed(x, y, z namedFloat64) namedFloat64 { | ||
| return x*y - z*x | ||
| ~~~~~~~~~ | ||
| !!! implicitfma: explicitly round the floating-point multiplication result to prevent implicit FMA | ||
| } | ||
|
|
||
| func badGeneric[T ~float32 | ~float64](x, y, z T) T { | ||
| product := x * y | ||
| product += z | ||
| ~ | ||
| !!! implicitfma: explicitly round the floating-point multiplication result to prevent implicit FMA | ||
| return product | ||
| } | ||
|
|
||
| func badConversionAroundSum(x, y, z float64) float64 { | ||
| return float64(x*y + z) | ||
| ~~~~~~~ | ||
| !!! implicitfma: explicitly round the floating-point multiplication result to prevent implicit FMA | ||
| } | ||
|
|
||
| func goodStandalone(x, y float64) float64 { | ||
| return x * y | ||
| } | ||
|
|
||
| func goodFloat64(x, y, z float64) float64 { | ||
| return float64(x*y) + z | ||
| } | ||
|
|
||
| func goodBothProducts(x, y, z float64) float64 { | ||
| return float64(x*y) - float64(z*x) | ||
| } | ||
|
|
||
| func goodFloatToInt(x, y float64) int { | ||
| return int(x * y) | ||
| } | ||
|
|
||
| func goodReturnRounded(x, y float64) float64 { | ||
| return float64(x * y) | ||
| } | ||
|
|
||
| func goodParenthesized(x, y float64) float64 { | ||
| return float64((x * y)) | ||
| } | ||
|
|
||
| func goodNamed(x, y namedFloat64) namedFloat64 { | ||
| return namedFloat64(x * y) | ||
| } | ||
|
|
||
| func goodConversionToNamed(x, y, z float64) namedFloat64 { | ||
| return namedFloat64(x*y) + namedFloat64(z) | ||
| } | ||
|
|
||
| func goodConstant() float64 { | ||
| return 1.5 * 2.5 | ||
| } | ||
|
|
||
| func goodInteger(x, y int) int { | ||
| return x * y | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.