@@ -42,6 +42,7 @@ type object struct {
4242 globalName string // name, if not yet created (not guaranteed to be the final name)
4343 buffer value // buffer with value as given by interp, nil if external
4444 size uint32 // must match buffer.len(), if available
45+ align int // alignment of the object (may be 0 if unknown)
4546 constant bool // true if this is a constant global
4647 marked uint8 // 0 means unmarked, 1 means external read, 2 means external write
4748}
@@ -593,6 +594,12 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
593594 // runtime.alloc.
594595 // First allocate a new global for this object.
595596 obj := mem .get (v .index ())
597+ alignment := obj .align
598+ if alignment == 0 {
599+ // Unknown alignment, perhaps from a direct call to runtime.alloc in
600+ // the runtime. Use a conservative default instead.
601+ alignment = mem .r .maxAlign
602+ }
596603 if obj .llvmType .IsNil () && obj .llvmLayoutType .IsNil () {
597604 // Create an initializer without knowing the global type.
598605 // This is probably the result of a runtime.alloc call.
@@ -603,7 +610,7 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
603610 globalType := initializer .Type ()
604611 llvmValue = llvm .AddGlobal (mem .r .mod , globalType , obj .globalName )
605612 llvmValue .SetInitializer (initializer )
606- llvmValue .SetAlignment (mem . r . maxAlign )
613+ llvmValue .SetAlignment (alignment )
607614 obj .llvmGlobal = llvmValue
608615 mem .put (v .index (), obj )
609616 } else {
@@ -642,11 +649,7 @@ func (v pointerValue) toLLVMValue(llvmType llvm.Type, mem *memoryView) (llvm.Val
642649 return llvm.Value {}, errors .New ("interp: allocated value does not match allocated type" )
643650 }
644651 llvmValue .SetInitializer (initializer )
645- if obj .llvmType .IsNil () {
646- // The exact type isn't known (only the layout), so use the
647- // alignment that would normally be expected from runtime.alloc.
648- llvmValue .SetAlignment (mem .r .maxAlign )
649- }
652+ llvmValue .SetAlignment (alignment )
650653 }
651654
652655 // It should be included in r.globals because otherwise markExternal
0 commit comments