# 8.4.4 编译日志

编译器提供了参数来反馈编译过程中的各种优化：

> ```
> -json string
> version,file for JSON compiler/optimizer detail output
> -m	print optimization decisions
> ```

我们可以通过以上参数来查看编译器所做的各种优化，包括内联在内。将下列代码保存为 `main.go`:

```go
package main

func C() {
    println("C")
    D()
}

func D() {
    println("D")
    C()
}

func main() {
    C()
}
```

通过命令 `go tool compile -G=3 -m -json "0,file:///tmp/main.json" main.go` 进行编译，可以看到如下输出：

> ```
> main.go:3:6: can inline C
> main.go:8:6: can inline D
> main.go:10:3: inlining call to C
> main.go:13:6: can inline main
> main.go:14:3: inlining call to C
> main.go:14:3: inlining call to D
> ```

文件 `/tmp/main.json` 也会包含相关信息。其中 `-m` 对应的数字越大，日志就越详细，例如我们通过命令 `go tool compile -G=3 -m=2 -json "0,file:///tmp/main.json" main.go` 编译时，得到的结果如下：

> ```
> main.go:3:6: can inline C with cost 61 as: func() { println(string(“C”)); D() }
> main.go:8:6: can inline D with cost 65 as: func() { println(string(“D”)); C() }
> main.go:10:3: inlining call to C func() { println(string(“C”)); D() }
> main.go:13:6: can inline main with cost 63 as: func() { C() }
> main.go:14:3: inlining call to C func() { println(string(“C”)); D() }
> main.go:14:3: inlining call to D func() { println(string(“D”)); C() }
> main.go:14:3: cannot inline C into main: repeated recursive cycle
> ```

&#x20;其中 `-m -m` 与 `-m=2` 效果一样，当 `-m=4` 时，日志还会打印出内联前后的 IR Tree 结构，读者可以自己尝试。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gocompiler.shizhz.me/8.-golang-bian-yi-qi-inline/8.4.4-bian-yi-ri-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
