-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto name mangling provider #207
Comments
Good that you open a proposal for how to handle name mangling @dannypsnl. Do you have a specific use case that is blocked without having name mangling being part of the core library? I would consider the That is, if two C++ functions with different function types were compiled to LLVM IR, the corresponding code may look as follows: Input float add(float x, float y) {
return x + y;
} clang -S -emit-llvm -o a.ll a.cpp
opt -S --mem2reg -o a_opt.ll a.ll Output define float @_Z3addff(float %0, float %1) {
%3 = fadd float %0, %1
ret float %3
} Input int add(int x, int y) {
return x + y;
} Output define i32 @_Z3addii(i32 %0, i32 %1) {
%3 = add i32 %0, %1
ret i32 %3
} From above, the As such, a compiler written to use package main
import (
"fmt"
"github.com/llir/irutil"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/types"
)
func main() {
m := ir.NewModule()
// generate `float add(float x, float y)` function.
genAddFloats(m)
// generate `in add(in x, in y)` function.
genAddInts(m)
// print LLVM IR module to stdout.
fmt.Println(m)
}
func genAddFloats(m *ir.Module) *ir.Func {
// create function.
f32 := types.Float
x := ir.NewParam("x", f32)
y := ir.NewParam("y", f32)
const funcName = "add"
f := m.NewFunc(funcName, f32, x, y)
// mangle function name.
f.SetName(irutil.MangleFuncName(funcName, f.Sig))
// generate basic block with add and ret instructions.
entry := f.NewBlock("entry")
result := entry.NewFAdd(x, y)
entry.NewRet(result)
return f
}
func genAddInts(m *ir.Module) *ir.Func {
// create function.
i32 := types.I32
x := ir.NewParam("x", i32)
y := ir.NewParam("y", i32)
const funcName = "add"
f := m.NewFunc(funcName, i32, x, y)
// mangle function name.
f.SetName(irutil.MangleFuncName(funcName, f.Sig))
// generate basic block with add and ret instructions.
entry := f.NewBlock("entry")
result := entry.NewAdd(x, y)
entry.NewRet(result)
return f
} Added to e.g. package irutil
import (
"fmt"
"strings"
"github.com/llir/llvm/ir/types"
)
// MangleFuncName results the name mangled representation of the given function name,
// based on the specified function signature.
func MangleFuncName(funcName string, funcType *types.FuncType) string {
buf := &strings.Builder{}
const manglePrefix = "_Z"
funcNameLen := len(funcName)
fmt.Fprintf(buf, "%s%d%s", manglePrefix, funcNameLen, funcName)
for _, paramType := range funcType.Params {
paramTypeName := MangleParamType(paramType)
buf.WriteString(paramTypeName)
}
return buf.String()
}
// MangleParamType results the name mangled representation of the given parameter type.
func MangleParamType(typ types.Type) string {
switch typ := typ.(type) {
case *types.IntType:
switch typ.BitSize {
case 32:
return "i"
// etc...
}
case *types.FloatType:
switch typ.Kind {
//case types.FloatKind... etc
case types.FloatKindFloat:
return "f"
}
// etc...
}
panic(fmt.Errorf("support for mangling parameter type %v not yet implemented", typ))
} This would generate the following IR: define float @_Z3addff(float %x, float %y) {
entry:
%0 = fadd float %x, %y
ret float %0
}
define i32 @_Z3addii(i32 %x, i32 %y) {
entry:
%0 = add i32 %x, %y
ret i32 %0
} This is just a first draft to explore different design options. Cheers, |
As I remember, The problem is
|
Do you mean this is already implemented in The following example Go source file: package main
import (
"fmt"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/types"
)
func main() {
m := ir.NewModule()
a32 := m.NewGlobal("a", types.I32)
a64 := m.NewGlobal("a", types.I64)
_ = a32
_ = a64
fmt.Println(m)
} Generates the following LLVM IR: @a = global i32
@a = global i64 In other words, the name of global variables is set as the user specifies it in
This is what I meant with an IR generation library. The idea is that users would not use the low-level Note, for instance that in the above example, name mangling is not enough to avoid name collisions, since e.g. C++ name mangling does not mangle the names of global variables (ref). |
I think this is able to done by extension, for example type MangleModule struct {
*ir.Module
mangle func(name string) string
}
func (m *MangleModule) SetMangle(f func(string) string) {
m.mangle = f
}
func (m *MangleModule) NewFunc(name string, retT types.Type, params ...*ir.Param) *ir.Func {
return m.Module.NewFunc(m.mangle(name), retT, params...)
}
func (m *MangleModule) NewGlobal(name string, t types.Type) *ir.Global {
return m.Module.NewGlobal(m.mangle(name), t)
}
func (m *MangleModule) NewGlobalDef(name string, init constant.Constant) *ir.Global {
return m.Module.NewGlobalDef(m.mangle(name), init)
}
func (m *MangleModule) NewIFunc(name string, resolver constant.Constant) *ir.IFunc {
return m.Module.NewIFunc(m.mangle(name), resolver)
}
func (m *MangleModule) New(name string, typ types.Type) types.Type {
return m.Module.NewTypeDef(m.mangle(name), typ)
} so close. |
The idea is we put a mangling provider(maybe a function) in the module like
Then every function applied with this provider.
The text was updated successfully, but these errors were encountered: