Comments (4)
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.
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.
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
- sometimes we need a predictable mangling, for example, JIT compiler
- Doing mangling outside of the project is ok, but users have to call it again and again
from llvm.
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)
- `SRet` struct location HOT 3
- How to use Function Pointers? HOT 3
- Wrong function pointer type HOT 1
- indirect br only takes constant addr HOT 1
- Returning void as a value HOT 3
- update llir/llvm to support 14.0 HOT 2
- update llir/llvm to support LLVM 15.0 HOT 7
- How to have two structs referencing eachother HOT 4
- How to convert to exe? HOT 3
- `module.NewTypeDef` has unexpected side effects HOT 17
- Provide working Hello, World example HOT 1
- support llvm 16.0.0 HOT 2
- incorrect cmpxchg signature HOT 4
- How to generate ir for embedded struct? HOT 6
- Convert to string HOT 1
- Hello 10 instead of Hello world. Passing a IntPtr to printf (go - LLIR/LLVM) HOT 3
- malloc and strcpy in LLVM HOT 1
- unable to generate LLVM: structure, external , noundef and ptr HOT 1
- No way to find underlying type of pointer or array HOT 1
- How to compile LLVM IR to machine code programatically? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from llvm.