Source file src/cmd/compile/internal/ssa/fmahash_test.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa_test
     6  
     7  import (
     8  	"internal/testenv"
     9  	"path/filepath"
    10  	"regexp"
    11  	"runtime"
    12  	"testing"
    13  )
    14  
    15  // TestFmaHash checks that the hash-test machinery works properly for a single case.
    16  // It also runs ssa/check and gccheck to be sure that those are checked at least a
    17  // little in each run.bash.  It does not check or run the generated code.
    18  // The test file is however a useful example of fused-vs-cascaded multiply-add.
    19  func TestFmaHash(t *testing.T) {
    20  	switch runtime.GOOS {
    21  	case "linux", "darwin":
    22  	default:
    23  		t.Skipf("Slow test, usually avoid it, os=%s not linux or darwin", runtime.GOOS)
    24  	}
    25  	switch runtime.GOARCH {
    26  	case "amd64", "arm64":
    27  	default:
    28  		t.Skipf("Slow test, usually avoid it, arch=%s not amd64 or arm64", runtime.GOARCH)
    29  	}
    30  
    31  	testenv.MustHaveGoBuild(t)
    32  	gocmd := testenv.GoToolPath(t)
    33  	tmpdir := t.TempDir()
    34  	source := filepath.Join("testdata", "fma.go")
    35  	output := filepath.Join(tmpdir, "fma.exe")
    36  	cmd := testenv.Command(t, gocmd, "build", "-o", output, source)
    37  	// The hash-dependence on file path name is dodged by specifying "all hashes ending in 1" plus "all hashes ending in 0"
    38  	// i.e., all hashes.  This will print all the FMAs; this test is only interested in one of them (that should appear near the end).
    39  	cmd.Env = append(cmd.Env, "GOCOMPILEDEBUG=fmahash=1/0", "GOOS=linux", "GOARCH=arm64", "HOME="+tmpdir)
    40  	t.Logf("%v", cmd)
    41  	t.Logf("%v", cmd.Env)
    42  	b, e := cmd.CombinedOutput()
    43  	if e != nil {
    44  		t.Errorf("build failed: %v\n%s", e, b)
    45  	}
    46  	s := string(b) // Looking for "GOFMAHASH triggered main.main:24"
    47  	re := "fmahash(0?) triggered .*fma.go:29:..;.*fma.go:18:.."
    48  	match := regexp.MustCompile(re)
    49  	if !match.MatchString(s) {
    50  		t.Errorf("Expected to match '%s' with \n-----\n%s-----", re, s)
    51  	}
    52  }
    53  

View as plain text