| 29 | } |
| 30 | |
| 31 | func runTest(t *testing.T, pathPrefix string) { |
| 32 | // Read the input IR. |
| 33 | ctx := llvm.NewContext() |
| 34 | defer ctx.Dispose() |
| 35 | buf, err := llvm.NewMemoryBufferFromFile(pathPrefix + ".ll") |
| 36 | os.Stat(pathPrefix + ".ll") // make sure this file is tracked by `go test` caching |
| 37 | if err != nil { |
| 38 | t.Fatalf("could not read file %s: %v", pathPrefix+".ll", err) |
| 39 | } |
| 40 | mod, err := ctx.ParseIR(buf) |
| 41 | if err != nil { |
| 42 | t.Fatalf("could not load module:\n%v", err) |
| 43 | } |
| 44 | defer mod.Dispose() |
| 45 | |
| 46 | // Perform the transform. |
| 47 | err = Run(mod, 10*time.Minute, false) |
| 48 | if err != nil { |
| 49 | if err, match := err.(*Error); match { |
| 50 | println(err.Error()) |
| 51 | if len(err.Inst) != 0 { |
| 52 | println(err.Inst) |
| 53 | } |
| 54 | if len(err.Traceback) > 0 { |
| 55 | println("\ntraceback:") |
| 56 | for _, line := range err.Traceback { |
| 57 | println(line.Pos.String() + ":") |
| 58 | println(line.Inst) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | |
| 65 | // To be sure, verify that the module is still valid. |
| 66 | if llvm.VerifyModule(mod, llvm.PrintMessageAction) != nil { |
| 67 | t.FailNow() |
| 68 | } |
| 69 | |
| 70 | // Run some cleanup passes to get easy-to-read outputs. |
| 71 | to := llvm.NewPassBuilderOptions() |
| 72 | defer to.Dispose() |
| 73 | mod.RunPasses("globalopt,dse,adce", llvm.TargetMachine{}, to) |
| 74 | |
| 75 | // Read the expected output IR. |
| 76 | out, err := os.ReadFile(pathPrefix + ".out.ll") |
| 77 | if err != nil { |
| 78 | t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err) |
| 79 | } |
| 80 | |
| 81 | // See whether the transform output matches with the expected output IR. |
| 82 | expected := string(out) |
| 83 | actual := mod.String() |
| 84 | if !fuzzyEqualIR(expected, actual) { |
| 85 | t.Logf("output does not match expected output:\n%s", actual) |
| 86 | t.Fail() |
| 87 | } |
| 88 | } |