github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/transform/testdata/allocs.ll (about) 1 target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" 2 target triple = "armv7m-none-eabi" 3 4 @runtime.zeroSizedAlloc = internal global i8 0, align 1 5 6 declare nonnull ptr @runtime.alloc(i32, ptr) 7 8 ; Test allocating a single int (i32) that should be allocated on the stack. 9 define void @testInt() { 10 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 11 store i32 5, ptr %alloc 12 ret void 13 } 14 15 ; Test allocating an array of 3 i16 values that should be allocated on the 16 ; stack. 17 define i16 @testArray() { 18 %alloc = call ptr @runtime.alloc(i32 6, ptr null) 19 %alloc.1 = getelementptr i16, ptr %alloc, i32 1 20 store i16 5, ptr %alloc.1 21 %alloc.2 = getelementptr i16, ptr %alloc, i32 2 22 %val = load i16, ptr %alloc.2 23 ret i16 %val 24 } 25 26 ; Call a function that will let the pointer escape, so the heap-to-stack 27 ; transform shouldn't be applied. 28 define void @testEscapingCall() { 29 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 30 %val = call ptr @escapeIntPtr(ptr %alloc) 31 ret void 32 } 33 34 define void @testEscapingCall2() { 35 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 36 %val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc) 37 ret void 38 } 39 40 ; Call a function that doesn't let the pointer escape. 41 define void @testNonEscapingCall() { 42 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 43 %val = call ptr @noescapeIntPtr(ptr %alloc) 44 ret void 45 } 46 47 ; Return the allocated value, which lets it escape. 48 define ptr @testEscapingReturn() { 49 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 50 ret ptr %alloc 51 } 52 53 ; Do a non-escaping allocation in a loop. 54 define void @testNonEscapingLoop() { 55 entry: 56 br label %loop 57 loop: 58 %alloc = call ptr @runtime.alloc(i32 4, ptr null) 59 %ptr = call ptr @noescapeIntPtr(ptr %alloc) 60 %result = icmp eq ptr null, %ptr 61 br i1 %result, label %loop, label %end 62 end: 63 ret void 64 } 65 66 ; Test a zero-sized allocation. 67 define void @testZeroSizedAlloc() { 68 %alloc = call ptr @runtime.alloc(i32 0, ptr null) 69 %ptr = call ptr @noescapeIntPtr(ptr %alloc) 70 ret void 71 } 72 73 declare ptr @escapeIntPtr(ptr) 74 75 declare ptr @noescapeIntPtr(ptr nocapture) 76 77 declare ptr @escapeIntPtrSometimes(ptr nocapture, ptr)