From 7323187f902149bba190c126c4ec88c5e770724e Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 18 Sep 2025 21:06:30 +0800 Subject: [PATCH] ssa: impl builtin alignof offsetof --- cl/_testrt/tpunsafe/in.go | 38 +++++++ cl/_testrt/tpunsafe/out.ll | 220 +++++++++++++++++++++++++++++++++++++ ssa/expr.go | 16 +++ 3 files changed, 274 insertions(+) create mode 100644 cl/_testrt/tpunsafe/in.go create mode 100644 cl/_testrt/tpunsafe/out.ll diff --git a/cl/_testrt/tpunsafe/in.go b/cl/_testrt/tpunsafe/in.go new file mode 100644 index 00000000..3907cbe1 --- /dev/null +++ b/cl/_testrt/tpunsafe/in.go @@ -0,0 +1,38 @@ +package main + +import ( + "unsafe" +) + +type N[T any] struct { + n1 T + n2 T +} + +type M[T any] struct { + m0 T + m1 int32 + m2 N[T] +} + +func (m *M[T]) check(align, offset1, offset2 uintptr) { + if v := unsafe.Alignof(m.m2); v != align { + println("have", v, "want", align) + panic("unsafe.Alignof error") + } + if v := unsafe.Offsetof(m.m2); v != offset1 { + println("have", v, "want", offset1) + panic("unsafe.Offsetof error") + } + if v := unsafe.Offsetof(m.m2.n2); v != offset2 { + println("have", v, "want", offset2) + panic("unsafe.Offsetof error") + } +} + +func main() { + m1 := M[bool]{} + m1.check(1, 8, 1) + m2 := M[int64]{} + m2.check(8, 16, 8) +} diff --git a/cl/_testrt/tpunsafe/out.ll b/cl/_testrt/tpunsafe/out.ll new file mode 100644 index 00000000..367dd158 --- /dev/null +++ b/cl/_testrt/tpunsafe/out.ll @@ -0,0 +1,220 @@ +; ModuleID = 'github.com/goplus/llgo/cl/_testrt/tpunsafe' +source_filename = "github.com/goplus/llgo/cl/_testrt/tpunsafe" + +%"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[bool]" = type { i1, i32, %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[bool]" } +%"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[bool]" = type { i1, i1 } +%"github.com/goplus/llgo/runtime/internal/runtime.String" = type { ptr, i64 } +%"github.com/goplus/llgo/runtime/internal/runtime.eface" = type { ptr, ptr } +%"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[int64]" = type { i64, i32, %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[int64]" } +%"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[int64]" = type { i64, i64 } + +@"github.com/goplus/llgo/cl/_testrt/tpunsafe.init$guard" = global i1 false, align 1 +@0 = private unnamed_addr constant [4 x i8] c"have", align 1 +@1 = private unnamed_addr constant [4 x i8] c"want", align 1 +@2 = private unnamed_addr constant [20 x i8] c"unsafe.Alignof error", align 1 +@_llgo_string = linkonce global ptr null, align 8 +@3 = private unnamed_addr constant [21 x i8] c"unsafe.Offsetof error", align 1 + +define void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.init"() { +_llgo_0: + %0 = load i1, ptr @"github.com/goplus/llgo/cl/_testrt/tpunsafe.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"github.com/goplus/llgo/cl/_testrt/tpunsafe.init$guard", align 1 + call void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.init$after"() + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.main"() { +_llgo_0: + %0 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocZ"(i64 12) + call void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.(*M[bool]).check"(ptr %0, i64 1, i64 8, i64 1) + %1 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocZ"(i64 32) + call void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.(*M[int64]).check"(ptr %1, i64 8, i64 16, i64 8) + ret void +} + +declare ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocZ"(i64) + +define linkonce void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.(*M[bool]).check"(ptr %0, i64 %1, i64 %2, i64 %3) { +_llgo_0: + %4 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[bool]", ptr %0, i32 0, i32 2 + %5 = load %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[bool]", ptr %4, align 1 + %6 = icmp ne i64 1, %1 + br i1 %6, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 1) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %1) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %7 = load ptr, ptr @_llgo_string, align 8 + %8 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @2, i64 20 }, ptr %8, align 8 + %9 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %7, 0 + %10 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %9, ptr %8, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %10) + unreachable + +_llgo_2: ; preds = %_llgo_0 + %11 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[bool]", ptr %0, i32 0, i32 2 + %12 = load %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[bool]", ptr %11, align 1 + %13 = icmp ne i64 8, %2 + br i1 %13, label %_llgo_3, label %_llgo_4 + +_llgo_3: ; preds = %_llgo_2 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 8) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %2) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %14 = load ptr, ptr @_llgo_string, align 8 + %15 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @3, i64 21 }, ptr %15, align 8 + %16 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %14, 0 + %17 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %16, ptr %15, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %17) + unreachable + +_llgo_4: ; preds = %_llgo_2 + %18 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[bool]", ptr %0, i32 0, i32 2 + %19 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[bool]", ptr %18, i32 0, i32 1 + %20 = load i1, ptr %19, align 1 + %21 = icmp ne i64 1, %3 + br i1 %21, label %_llgo_5, label %_llgo_6 + +_llgo_5: ; preds = %_llgo_4 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 1) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %3) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %22 = load ptr, ptr @_llgo_string, align 8 + %23 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @3, i64 21 }, ptr %23, align 8 + %24 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %22, 0 + %25 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %24, ptr %23, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %25) + unreachable + +_llgo_6: ; preds = %_llgo_4 + ret void +} + +define linkonce void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.(*M[int64]).check"(ptr %0, i64 %1, i64 %2, i64 %3) { +_llgo_0: + %4 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[int64]", ptr %0, i32 0, i32 2 + %5 = load %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[int64]", ptr %4, align 4 + %6 = icmp ne i64 8, %1 + br i1 %6, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 8) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %1) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %7 = load ptr, ptr @_llgo_string, align 8 + %8 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @2, i64 20 }, ptr %8, align 8 + %9 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %7, 0 + %10 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %9, ptr %8, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %10) + unreachable + +_llgo_2: ; preds = %_llgo_0 + %11 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[int64]", ptr %0, i32 0, i32 2 + %12 = load %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[int64]", ptr %11, align 4 + %13 = icmp ne i64 16, %2 + br i1 %13, label %_llgo_3, label %_llgo_4 + +_llgo_3: ; preds = %_llgo_2 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 16) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %2) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %14 = load ptr, ptr @_llgo_string, align 8 + %15 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @3, i64 21 }, ptr %15, align 8 + %16 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %14, 0 + %17 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %16, ptr %15, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %17) + unreachable + +_llgo_4: ; preds = %_llgo_2 + %18 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.M[int64]", ptr %0, i32 0, i32 2 + %19 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testrt/tpunsafe.N[int64]", ptr %18, i32 0, i32 1 + %20 = load i64, ptr %19, align 4 + %21 = icmp ne i64 8, %3 + br i1 %21, label %_llgo_5, label %_llgo_6 + +_llgo_5: ; preds = %_llgo_4 + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64 8) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 4 }) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64 %3) + call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10) + %22 = load ptr, ptr @_llgo_string, align 8 + %23 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64 16) + store %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @3, i64 21 }, ptr %23, align 8 + %24 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" undef, ptr %22, 0 + %25 = insertvalue %"github.com/goplus/llgo/runtime/internal/runtime.eface" %24, ptr %23, 1 + call void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface" %25) + unreachable + +_llgo_6: ; preds = %_llgo_4 + ret void +} + +declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String") + +declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8) + +declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintInt"(i64) + +declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintUint"(i64) + +define void @"github.com/goplus/llgo/cl/_testrt/tpunsafe.init$after"() { +_llgo_0: + %0 = load ptr, ptr @_llgo_string, align 8 + %1 = icmp eq ptr %0, null + br i1 %1, label %_llgo_1, label %_llgo_2 + +_llgo_1: ; preds = %_llgo_0 + %2 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.Basic"(i64 24) + store ptr %2, ptr @_llgo_string, align 8 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +declare ptr @"github.com/goplus/llgo/runtime/internal/runtime.Basic"(i64) + +declare ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocU"(i64) + +declare void @"github.com/goplus/llgo/runtime/internal/runtime.Panic"(%"github.com/goplus/llgo/runtime/internal/runtime.eface") diff --git a/ssa/expr.go b/ssa/expr.go index 91235b9c..346b5b3e 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -1257,7 +1257,23 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) { case "Add": return b.Advance(args[0], args[1]) case "Sizeof": + // instance of generic function return b.Prog.Val(int(b.Prog.SizeOf(args[0].Type))) + case "Alignof": + // instance of generic function + return b.Prog.Val(int(b.Prog.td.ABITypeAlignment(args[0].ll))) + case "Offsetof": + // instance of generic function + if load := args[0].impl.IsALoadInst(); !load.IsNil() { + if gep := load.Operand(0).IsAGetElementPtrInst(); !gep.IsNil() { + typ := gep.GEPSourceElementType() + offset := gep.Operand(2).IsAConstantInt() + if typ.TypeKind() == llvm.StructTypeKind && !offset.IsNil() { + return b.Prog.Val(int(b.Prog.td.ElementOffset(typ, int(offset.SExtValue())))) + } + } + } + panic("invalid argument for unsafe.Offsetof: must be a selector expression") } panic("todo: " + fn) }