Skip to content

Commit 8f963ba

Browse files
committed
make SSA sanity checks opt-in for builds
1 parent 8a04264 commit 8f963ba

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

internal/build/build.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
384384
})
385385
preCollectRuntimeLinknames(prog, altPkgs)
386386

387-
buildMode := ssaBuildMode
387+
buildMode := ssaBuildMode()
388388
cabiOptimize := true
389389
passOpt := true
390390
if IsDbgEnabled() || mode == ModeGen {
@@ -571,9 +571,17 @@ func (p Package) isNeedRuntimeOrPyInit() (needRuntime, needPyInit bool) {
571571
return
572572
}
573573

574-
const (
575-
ssaBuildMode = ssa.SanityCheckFunctions | ssa.InstantiateGenerics
576-
)
574+
func ssaBuildMode() ssa.BuilderMode {
575+
mode := ssa.InstantiateGenerics
576+
if ssaSanityEnabled() {
577+
mode |= ssa.SanityCheckFunctions
578+
}
579+
return mode
580+
}
581+
582+
func ssaSanityEnabled() bool {
583+
return isEnvOn(llgoSSASanity, false)
584+
}
577585

578586
type context struct {
579587
env *llvm.Env
@@ -1767,9 +1775,11 @@ func fixUntypedShiftTypes(p *packages.Package) {
17671775
}
17681776

17691777
func applyPatches(ctx *context, p *packages.Package, verbose bool) {
1770-
// Fix untyped shift types before SSA build
1778+
// Fix untyped shift types before SSA sanity checking.
17711779
// See: https://github.com/golang/go/issues/77067
1772-
fixUntypedShiftTypes(p)
1780+
if ssaSanityEnabled() {
1781+
fixUntypedShiftTypes(p)
1782+
}
17731783

17741784
// fix instance patch
17751785
for id, inst := range p.TypesInfo.Instances {
@@ -1839,6 +1849,7 @@ const llgoWasiThreads = "LLGO_WASI_THREADS"
18391849
const llgoStdioNobuf = "LLGO_STDIO_NOBUF"
18401850
const llgoFullRpath = "LLGO_FULL_RPATH"
18411851
const llgoBuildCache = "LLGO_BUILD_CACHE"
1852+
const llgoSSASanity = "LLGO_SSA_SANITY"
18421853

18431854
// for Plan9 asm translation debug
18441855
const llgoPlan9ASMPkgs = "LLGO_PLAN9ASM_PKGS"

internal/build/build_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/goplus/llgo/internal/mockable"
2121
"github.com/goplus/llgo/internal/packages"
2222
llssa "github.com/goplus/llgo/ssa"
23+
gossa "golang.org/x/tools/go/ssa"
2324
)
2425

2526
func TestMain(m *testing.M) {
@@ -52,6 +53,27 @@ func TestPrependEnvPath(t *testing.T) {
5253
}
5354
}
5455

56+
func TestSSABuildModeSanityOptIn(t *testing.T) {
57+
t.Setenv(llgoSSASanity, "")
58+
if ssaSanityEnabled() {
59+
t.Fatal("SSA sanity should default to disabled")
60+
}
61+
if got := ssaBuildMode(); got&gossa.SanityCheckFunctions != 0 {
62+
t.Fatalf("default SSA build mode enables sanity checks: %v", got)
63+
}
64+
if got := ssaBuildMode(); got&gossa.InstantiateGenerics == 0 {
65+
t.Fatalf("default SSA build mode does not instantiate generics: %v", got)
66+
}
67+
68+
t.Setenv(llgoSSASanity, "1")
69+
if !ssaSanityEnabled() {
70+
t.Fatal("SSA sanity should be enabled by LLGO_SSA_SANITY=1")
71+
}
72+
if got := ssaBuildMode(); got&gossa.SanityCheckFunctions == 0 {
73+
t.Fatalf("opt-in SSA build mode does not enable sanity checks: %v", got)
74+
}
75+
}
76+
5577
func TestNeedsLinuxNoPIE(t *testing.T) {
5678
ctx := &context{buildConf: &Config{Goos: "linux"}}
5779
if !needsLinuxNoPIE(ctx, nil) {

internal/build/collect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (c *context) envInputs() envSection {
100100
llgoWasiThreads,
101101
llgoStdioNobuf,
102102
llgoFullRpath,
103+
llgoSSASanity,
103104
}
104105
for _, envVar := range envVars {
105106
if v := os.Getenv(envVar); v != "" {

0 commit comments

Comments
 (0)