mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 22:43:40 +00:00
compiler: add "target-cpu" and "target-features" attributes
This matches Clang, and with that, it adds support for inlining between
Go and C because LLVM only allows inlining if the "target-cpu" and
"target-features" string attributes match.
For example, take a look at the following code:
// int add(int a, int b) {
// return a + b;
// }
import "C"
func main() {
println(C.add(3, 5))
}
The 'add' function is not inlined into the main function before this
commit, but after it, it can be inlined and trivially be optimized to
`println(8)`.
This commit is contained in:
committed by
Ron Evans
parent
78fec3719f
commit
cf640290a3
@@ -21,11 +21,18 @@ import (
|
||||
// attributes to a function. For example, it adds optsize when requested from
|
||||
// the -opt= compiler flag.
|
||||
func AddStandardAttributes(fn llvm.Value, config *compileopts.Config) {
|
||||
ctx := fn.Type().Context()
|
||||
_, sizeLevel, _ := config.OptLevels()
|
||||
if sizeLevel >= 1 {
|
||||
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
fn.AddFunctionAttr(ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
}
|
||||
if sizeLevel >= 2 {
|
||||
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0))
|
||||
fn.AddFunctionAttr(ctx.CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0))
|
||||
}
|
||||
if config.CPU() != "" {
|
||||
fn.AddFunctionAttr(ctx.CreateStringAttribute("target-cpu", config.CPU()))
|
||||
}
|
||||
if config.Features() != "" {
|
||||
fn.AddFunctionAttr(ctx.CreateStringAttribute("target-features", config.Features()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user