Skip to content
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

Closed
dannypsnl opened this issue Dec 13, 2021 · 4 comments
Closed

auto name mangling provider #207

dannypsnl opened this issue Dec 13, 2021 · 4 comments

Comments

@dannypsnl
Copy link
Member

dannypsnl commented Dec 13, 2021

The idea is we put a mangling provider(maybe a function) in the module like

type Module struct {
    maglingProvider(name string) string
}

Then every function applied with this provider.

@dannypsnl dannypsnl changed the title auto name magling provider auto name mangling provider Dec 13, 2021
@mewmew
Copy link
Member

mewmew commented Dec 13, 2021

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 llir/llvm/ir package low-level in the sense that the names used for functions and global variables is already the mangled name.

That is, if two C++ functions with different function types were compiled to LLVM IR, the corresponding code may look as follows:

Input a.cpp source file:

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 a_opt.ll source file:

define float @_Z3addff(float %0, float %1) {
  %3 = fadd float %0, %1
  ret float %3
}

Input b.cpp source file:

int add(int x, int y) {
	return x + y;
}

Output b_opt.ll source file:

define i32 @_Z3addii(i32 %0, i32 %1) {
  %3 = add i32 %0, %1
  ret i32 %3
}

From above, the _Z3addff and _Z3addii function names were mangled in the LLVM IR.

As such, a compiler written to use llir/llvm/ir could handle name mangling e.g. as follows.

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. irutil/name_mangle.go:

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,
Robin

@mewmew mewmew added the util label Dec 13, 2021
@dannypsnl
Copy link
Member Author

dannypsnl commented Dec 13, 2021

As I remember, ir.Module let m to m0 if users added a second global/function called m again. For simple usage, this is enough since we can store references to IR instances in a static compiler.

The problem is

  1. sometimes we need a predictable mangling, for example, JIT compiler
  2. Doing mangling outside of the project is ok, but users have to call it again and again

@mewmew
Copy link
Member

mewmew commented Dec 13, 2021

As I remember, ir.Module let m to m0 if users added a second global/function called m again. For simple usage, this is enough since we can store references to IR instances in a static compiler.

Do you mean this is already implemented in llir/llvm/ir? I don't remember us implementing it.

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 llir/llvm/ir.

Doing mangling outside of the project is ok, but users have to call it again and again

This is what I meant with an IR generation library. The idea is that users would not use the low-level llir/llvm/ir package directly, but instead use the IR builder implemented by the high-level IR generation library. The irgen library could handle e.g. name mangling, avoid name collisions, etc.

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).

@dannypsnl
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants