Giter Club home page Giter Club logo

Comments (4)

dannypsnl avatar dannypsnl commented on September 25, 2024 1

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.

from llvm.

mewmew avatar mewmew commented on September 25, 2024

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

from llvm.

dannypsnl avatar dannypsnl commented on September 25, 2024

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

from llvm.

mewmew avatar mewmew commented on September 25, 2024

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

from llvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.