Skip to content

Commit 21e5350

Browse files
committed
[llgo] Update to use the latest IR attribute bindings
A recent commit (r286087) to the LLVM Go bindings that changed things over to use the new attribute API broke llgo. This commit updates llgo accordingly. llvm-svn: 288769
1 parent 7695d89 commit 21e5350

File tree

7 files changed

+73
-38
lines changed

7 files changed

+73
-38
lines changed

llgo/cmd/gllgo/gllgo.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,21 @@ func (san *sanitizerOptions) addLibs(triple string, flags []string) []string {
154154
}
155155

156156
func (san *sanitizerOptions) getAttribute() llvm.Attribute {
157+
var attrKind uint
158+
157159
switch {
158160
case san.address:
159-
return llvm.SanitizeAddressAttribute
161+
attrKind = llvm.AttributeKindID("sanitize_address")
160162
case san.thread:
161-
return llvm.SanitizeThreadAttribute
163+
attrKind = llvm.AttributeKindID("sanitize_thread")
162164
case san.memory:
163-
return llvm.SanitizeMemoryAttribute
165+
attrKind = llvm.AttributeKindID("sanitize_memory")
164166
default:
165-
return 0
167+
attrKind = 0
166168
}
169+
170+
ctx := llvm.GlobalContext()
171+
return ctx.CreateEnumAttribute(attrKind, 0)
167172
}
168173

169174
type driverOptions struct {

llgo/irgen/attribute.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,27 @@ func parseLLVMAttribute(value string) llvmAttribute {
148148
for _, field := range strings.Fields(value) {
149149
switch strings.ToLower(field) {
150150
case "noreturn":
151-
result |= llvmAttribute(llvm.NoReturnAttribute)
152151
case "nounwind":
153-
result |= llvmAttribute(llvm.NoUnwindAttribute)
154152
case "noinline":
155-
result |= llvmAttribute(llvm.NoInlineAttribute)
156153
case "alwaysinline":
157-
result |= llvmAttribute(llvm.AlwaysInlineAttribute)
154+
kind := llvm.AttributeKindID(strings.ToLower(field))
155+
result.AttrKinds = append(result.AttrKinds, kind)
158156
}
159157
}
160158
return result
161159
}
162160

163-
type llvmAttribute llvm.Attribute
161+
type llvmAttribute struct {
162+
AttrKinds []uint
163+
}
164164

165165
func (a llvmAttribute) Apply(v llvm.Value) {
166+
ctx := v.GlobalParent().Context()
166167
if !v.IsAFunction().IsNil() {
167-
v.AddFunctionAttr(llvm.Attribute(a))
168-
} else {
169-
v.AddAttribute(llvm.Attribute(a))
168+
for _, kind := range a.AttrKinds {
169+
attr := ctx.CreateEnumAttribute(kind, 0)
170+
v.AddFunctionAttr(attr)
171+
}
170172
}
171173
}
172174

llgo/irgen/cabi.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ func (tm *llvmTypeMap) expandType(argTypes []llvm.Type, argAttrs []llvm.Attribut
249249

250250
switch bt := bt.(type) {
251251
case *structBType, *arrayBType:
252+
noneAttr := tm.ctx.CreateEnumAttribute(0, 0)
252253
bo := tm.getBackendOffsets(bt)
253254
sp := 0
254255
for sp != len(bo) && bo[sp].offset < 8 {
@@ -258,21 +259,23 @@ func (tm *llvmTypeMap) expandType(argTypes []llvm.Type, argAttrs []llvm.Attribut
258259
eb2 := bo[sp:]
259260
if len(eb2) > 0 {
260261
argTypes = append(argTypes, tm.classifyEightbyte(eb1, &numInt, &numSSE), tm.classifyEightbyte(eb2, &numInt, &numSSE))
261-
argAttrs = append(argAttrs, 0, 0)
262+
argAttrs = append(argAttrs, noneAttr, noneAttr)
262263
} else {
263264
argTypes = append(argTypes, tm.classifyEightbyte(eb1, &numInt, &numSSE))
264-
argAttrs = append(argAttrs, 0)
265+
argAttrs = append(argAttrs, noneAttr)
265266
}
266267

267268
return argTypes, argAttrs, numInt, numSSE
268269

269270
case *intBType:
270271
if bt.width < 4 {
272+
var argAttrKind uint
271273
if bt.signed {
272-
argAttr = llvm.SExtAttribute
274+
argAttrKind = llvm.AttributeKindID("signext")
273275
} else {
274-
argAttr = llvm.ZExtAttribute
276+
argAttrKind = llvm.AttributeKindID("zeroext")
275277
}
278+
argAttr = tm.ctx.CreateEnumAttribute(argAttrKind, 0)
276279
}
277280
}
278281

@@ -516,10 +519,12 @@ type functionTypeInfo struct {
516519

517520
func (fi *functionTypeInfo) declare(m llvm.Module, name string) llvm.Value {
518521
fn := llvm.AddFunction(m, name, fi.functionType)
519-
fn.AddFunctionAttr(fi.retAttr)
522+
if fi.retAttr.GetEnumKind() != 0 {
523+
fn.AddAttributeAtIndex(0, fi.retAttr)
524+
}
520525
for i, a := range fi.argAttrs {
521-
if a != 0 {
522-
fn.Param(i).AddAttribute(a)
526+
if a.GetEnumKind() != 0 {
527+
fn.AddAttributeAtIndex(i + 1, a)
523528
}
524529
}
525530
return fn
@@ -537,9 +542,13 @@ func (fi *functionTypeInfo) call(ctx llvm.Context, allocaBuilder llvm.Builder, b
537542
fi.retInf.prepare(ctx, allocaBuilder, callArgs)
538543
typedCallee := builder.CreateBitCast(callee, llvm.PointerType(fi.functionType, 0), "")
539544
call := builder.CreateCall(typedCallee, callArgs, "")
540-
call.AddInstrAttribute(0, fi.retAttr)
545+
if fi.retAttr.GetEnumKind() != 0 {
546+
call.AddCallSiteAttribute(0, fi.retAttr)
547+
}
541548
for i, a := range fi.argAttrs {
542-
call.AddInstrAttribute(i+1, a)
549+
if a.GetEnumKind() != 0 {
550+
call.AddCallSiteAttribute(i + 1, a)
551+
}
543552
}
544553
return fi.retInf.decode(ctx, allocaBuilder, builder, call)
545554
}
@@ -556,9 +565,13 @@ func (fi *functionTypeInfo) invoke(ctx llvm.Context, allocaBuilder llvm.Builder,
556565
fi.retInf.prepare(ctx, allocaBuilder, callArgs)
557566
typedCallee := builder.CreateBitCast(callee, llvm.PointerType(fi.functionType, 0), "")
558567
call := builder.CreateInvoke(typedCallee, callArgs, cont, lpad, "")
559-
call.AddInstrAttribute(0, fi.retAttr)
568+
if fi.retAttr.GetEnumKind() != 0 {
569+
call.AddCallSiteAttribute(0, fi.retAttr)
570+
}
560571
for i, a := range fi.argAttrs {
561-
call.AddInstrAttribute(i+1, a)
572+
if a.GetEnumKind() != 0 {
573+
call.AddCallSiteAttribute(i + 1, a)
574+
}
562575
}
563576
builder.SetInsertPointAtEnd(cont)
564577
return fi.retInf.decode(ctx, allocaBuilder, builder, call)
@@ -567,6 +580,7 @@ func (fi *functionTypeInfo) invoke(ctx llvm.Context, allocaBuilder llvm.Builder,
567580
func (tm *llvmTypeMap) getFunctionTypeInfo(args []types.Type, results []types.Type) (fi functionTypeInfo) {
568581
var returnType llvm.Type
569582
var argTypes []llvm.Type
583+
var argAttrKind uint
570584
if len(results) == 0 {
571585
returnType = llvm.VoidType()
572586
fi.retInf = &directRetInfo{}
@@ -609,15 +623,17 @@ func (tm *llvmTypeMap) getFunctionTypeInfo(args []types.Type, results []types.Ty
609623
case AIK_Indirect:
610624
returnType = llvm.VoidType()
611625
argTypes = []llvm.Type{llvm.PointerType(resultsType, 0)}
612-
fi.argAttrs = []llvm.Attribute{llvm.StructRetAttribute}
626+
argAttrKind = llvm.AttributeKindID("sret")
627+
fi.argAttrs = []llvm.Attribute{tm.ctx.CreateEnumAttribute(argAttrKind, 0)}
613628
fi.retInf = &indirectRetInfo{numResults: len(results), resultsType: resultsType}
614629
}
615630
}
616631

617632
// Allocate an argument for the call chain.
618633
fi.chainIndex = len(argTypes)
619634
argTypes = append(argTypes, llvm.PointerType(tm.ctx.Int8Type(), 0))
620-
fi.argAttrs = append(fi.argAttrs, llvm.NestAttribute)
635+
argAttrKind = llvm.AttributeKindID("nest")
636+
fi.argAttrs = append(fi.argAttrs, tm.ctx.CreateEnumAttribute(argAttrKind, 0))
621637

622638
// Keep track of the number of INTEGER/SSE class registers remaining.
623639
remainingInt := 6
@@ -651,7 +667,8 @@ func (tm *llvmTypeMap) getFunctionTypeInfo(args []types.Type, results []types.Ty
651667
if !isDirect {
652668
fi.argInfos = append(fi.argInfos, &indirectArgInfo{len(argTypes)})
653669
argTypes = append(argTypes, llvm.PointerType(tm.ToLLVM(arg), 0))
654-
fi.argAttrs = append(fi.argAttrs, llvm.ByValAttribute)
670+
argAttrKind = llvm.AttributeKindID("byval")
671+
fi.argAttrs = append(fi.argAttrs, tm.ctx.CreateEnumAttribute(argAttrKind, 0))
655672
}
656673
}
657674

llgo/irgen/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func (c *compiler) logf(format string, v ...interface{}) {
156156
func (c *compiler) addCommonFunctionAttrs(fn llvm.Value) {
157157
fn.AddTargetDependentFunctionAttr("disable-tail-calls", "true")
158158
fn.AddTargetDependentFunctionAttr("split-stack", "")
159-
if attr := c.SanitizerAttribute; attr != 0 {
160-
fn.AddFunctionAttr(attr)
159+
if c.SanitizerAttribute.GetEnumKind() != 0 {
160+
fn.AddFunctionAttr(c.SanitizerAttribute)
161161
}
162162
}
163163

llgo/irgen/maps.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ func (fr *frame) mapLookup(m, k *govalue) (v *govalue, ok *govalue) {
3939
pk := fr.allocaBuilder.CreateAlloca(llk.Type(), "")
4040
fr.builder.CreateStore(llk, pk)
4141
valptr := fr.runtime.mapIndex.call(fr, m.value, pk, boolLLVMValue(false))[0]
42-
valptr.AddInstrAttribute(2, llvm.NoCaptureAttribute)
43-
valptr.AddInstrAttribute(2, llvm.ReadOnlyAttribute)
42+
attrkind := llvm.AttributeKindID("nocapture")
43+
valptr.AddCallSiteAttribute(2, fr.types.ctx.CreateEnumAttribute(attrkind, 0))
44+
attrkind = llvm.AttributeKindID("readonly")
45+
valptr.AddCallSiteAttribute(2, fr.types.ctx.CreateEnumAttribute(attrkind, 0))
4446
okbit := fr.builder.CreateIsNotNull(valptr, "")
4547

4648
elemtyp := m.Type().Underlying().(*types.Map).Elem()
@@ -55,8 +57,10 @@ func (fr *frame) mapUpdate(m, k, v *govalue) {
5557
pk := fr.allocaBuilder.CreateAlloca(llk.Type(), "")
5658
fr.builder.CreateStore(llk, pk)
5759
valptr := fr.runtime.mapIndex.call(fr, m.value, pk, boolLLVMValue(true))[0]
58-
valptr.AddInstrAttribute(2, llvm.NoCaptureAttribute)
59-
valptr.AddInstrAttribute(2, llvm.ReadOnlyAttribute)
60+
attrkind := llvm.AttributeKindID("nocapture")
61+
valptr.AddCallSiteAttribute(2, fr.types.ctx.CreateEnumAttribute(attrkind, 0))
62+
attrkind = llvm.AttributeKindID("readonly")
63+
valptr.AddCallSiteAttribute(2, fr.types.ctx.CreateEnumAttribute(attrkind, 0))
6064

6165
elemtyp := m.Type().Underlying().(*types.Map).Elem()
6266
llelemtyp := fr.types.ToLLVM(elemtyp)

llgo/irgen/runtime.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
145145
ByteSlice := types.NewSlice(types.Typ[types.Byte])
146146
IntSlice := types.NewSlice(types.Typ[types.Int])
147147

148+
AttrKind := llvm.AttributeKindID("nounwind")
149+
NoUnwindAttr := module.Context().CreateEnumAttribute(AttrKind, 0)
150+
AttrKind = llvm.AttributeKindID("noreturn")
151+
NoReturnAttr := module.Context().CreateEnumAttribute(AttrKind, 0)
152+
148153
for _, rt := range [...]struct {
149154
name string
150155
rfi *runtimeFnInfo
@@ -168,7 +173,7 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
168173
rfi: &ri.byteArrayToString,
169174
args: []types.Type{UnsafePointer, Int},
170175
res: []types.Type{String},
171-
attrs: []llvm.Attribute{llvm.NoUnwindAttribute},
176+
attrs: []llvm.Attribute{NoUnwindAttr},
172177
},
173178
{
174179
name: "__go_can_recover",
@@ -314,7 +319,7 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
314319
rfi: &ri.New,
315320
args: []types.Type{UnsafePointer, Uintptr},
316321
res: []types.Type{UnsafePointer},
317-
attrs: []llvm.Attribute{llvm.NoUnwindAttribute},
322+
attrs: []llvm.Attribute{NoUnwindAttr},
318323
},
319324
{
320325
name: "__go_new_channel",
@@ -338,7 +343,7 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
338343
name: "__go_panic",
339344
rfi: &ri.panic,
340345
args: []types.Type{EmptyInterface},
341-
attrs: []llvm.Attribute{llvm.NoReturnAttribute},
346+
attrs: []llvm.Attribute{NoReturnAttr},
342347
},
343348
{
344349
name: "__go_print_bool",
@@ -417,7 +422,7 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
417422
name: "__go_runtime_error",
418423
rfi: &ri.runtimeError,
419424
args: []types.Type{Int32},
420-
attrs: []llvm.Attribute{llvm.NoReturnAttribute},
425+
attrs: []llvm.Attribute{NoReturnAttr},
421426
},
422427
{
423428
name: "runtime.selectdefault",
@@ -474,7 +479,7 @@ func newRuntimeInterface(module llvm.Module, tm *llvmTypeMap) (*runtimeInterface
474479
rfi: &ri.stringToByteArray,
475480
args: []types.Type{String},
476481
res: []types.Type{ByteSlice},
477-
attrs: []llvm.Attribute{llvm.NoUnwindAttribute},
482+
attrs: []llvm.Attribute{NoUnwindAttr},
478483
},
479484
{
480485
name: "__go_string_to_int_array",

llgo/irgen/ssa.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ func (fr *frame) emitInitPrologue() llvm.BasicBlock {
534534
func (fr *frame) bridgeRecoverFunc(llfn llvm.Value, fti functionTypeInfo) *frame {
535535
// The bridging function must not be inlined, or the return address
536536
// may not correspond to the source function.
537-
llfn.AddFunctionAttr(llvm.NoInlineAttribute)
537+
attrKind := llvm.AttributeKindID("noinline")
538+
noInlineAttr := fr.module.Context().CreateEnumAttribute(attrKind, 0)
539+
llfn.AddFunctionAttr(noInlineAttr)
538540

539541
// Call __go_can_recover, passing in the function's return address.
540542
entry := llvm.AddBasicBlock(llfn, "entry")

0 commit comments

Comments
 (0)