|
7 | 7 | // places would be a big risk if only one of them is updated. |
8 | 8 | package llvmutil |
9 | 9 |
|
10 | | -import "tinygo.org/x/go-llvm" |
| 10 | +import ( |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "tinygo.org/x/go-llvm" |
| 15 | +) |
| 16 | + |
| 17 | +// Major returns the LLVM major version. |
| 18 | +func Major() int { |
| 19 | + llvmMajor, err := strconv.Atoi(strings.SplitN(llvm.Version, ".", 2)[0]) |
| 20 | + if err != nil { |
| 21 | + // sanity check, should be unreachable |
| 22 | + panic("could not parse LLVM version: " + err.Error()) |
| 23 | + } |
| 24 | + return llvmMajor |
| 25 | +} |
11 | 26 |
|
12 | 27 | // CreateEntryBlockAlloca creates a new alloca in the entry block, even though |
13 | 28 | // the IR builder is located elsewhere. It assumes that the insert point is |
@@ -78,25 +93,33 @@ func EmitLifetimeEnd(builder llvm.Builder, mod llvm.Module, ptr, size llvm.Value |
78 | 93 | // getLifetimeStartFunc returns the llvm.lifetime.start intrinsic and creates it |
79 | 94 | // first if it doesn't exist yet. |
80 | 95 | func getLifetimeStartFunc(mod llvm.Module) (llvm.Type, llvm.Value) { |
81 | | - fn := mod.NamedFunction("llvm.lifetime.start.p0i8") |
| 96 | + fnName := "llvm.lifetime.start.p0" |
| 97 | + if Major() < 15 { // compatibility with LLVM 14 |
| 98 | + fnName = "llvm.lifetime.start.p0i8" |
| 99 | + } |
| 100 | + fn := mod.NamedFunction(fnName) |
82 | 101 | ctx := mod.Context() |
83 | 102 | i8ptrType := llvm.PointerType(ctx.Int8Type(), 0) |
84 | 103 | fnType := llvm.FunctionType(ctx.VoidType(), []llvm.Type{ctx.Int64Type(), i8ptrType}, false) |
85 | 104 | if fn.IsNil() { |
86 | | - fn = llvm.AddFunction(mod, "llvm.lifetime.start.p0i8", fnType) |
| 105 | + fn = llvm.AddFunction(mod, fnName, fnType) |
87 | 106 | } |
88 | 107 | return fnType, fn |
89 | 108 | } |
90 | 109 |
|
91 | 110 | // getLifetimeEndFunc returns the llvm.lifetime.end intrinsic and creates it |
92 | 111 | // first if it doesn't exist yet. |
93 | 112 | func getLifetimeEndFunc(mod llvm.Module) (llvm.Type, llvm.Value) { |
94 | | - fn := mod.NamedFunction("llvm.lifetime.end.p0i8") |
| 113 | + fnName := "llvm.lifetime.end.p0" |
| 114 | + if Major() < 15 { |
| 115 | + fnName = "llvm.lifetime.end.p0i8" |
| 116 | + } |
| 117 | + fn := mod.NamedFunction(fnName) |
95 | 118 | ctx := mod.Context() |
96 | 119 | i8ptrType := llvm.PointerType(ctx.Int8Type(), 0) |
97 | 120 | fnType := llvm.FunctionType(ctx.VoidType(), []llvm.Type{ctx.Int64Type(), i8ptrType}, false) |
98 | 121 | if fn.IsNil() { |
99 | | - fn = llvm.AddFunction(mod, "llvm.lifetime.end.p0i8", fnType) |
| 122 | + fn = llvm.AddFunction(mod, fnName, fnType) |
100 | 123 | } |
101 | 124 | return fnType, fn |
102 | 125 | } |
|
0 commit comments