transform: do not lower zero-sized alloc to alloca

The LLVM CoroFrame pass appears to be tripping over this zero-sized
alloca. Therefore, do what the runtime would do: return a pointer to
runtime.zeroSizedAlloc. Or just don't deal with this case. But don't
emit a zero sized alloca to avoid this LLVM bug.

More information: https://bugs.llvm.org/show_bug.cgi?id=49916
This commit is contained in:
Ayke van Laethem
2021-04-11 00:51:48 +02:00
committed by Ron Evans
parent 2fd8f103ab
commit e7a05b6e74
3 changed files with 30 additions and 0 deletions
+12
View File
@@ -43,6 +43,18 @@ func OptimizeAllocs(mod llvm.Module) {
continue
}
if size == 0 {
// If the size is 0, the pointer is allowed to alias other
// zero-sized pointers. Use the pointer to the global that would
// also be returned by runtime.alloc.
zeroSizedAlloc := mod.NamedGlobal("runtime.zeroSizedAlloc")
if !zeroSizedAlloc.IsNil() {
heapalloc.ReplaceAllUsesWith(zeroSizedAlloc)
heapalloc.EraseFromParentAsInstruction()
}
continue
}
// In general the pattern is:
// %0 = call i8* @runtime.alloc(i32 %size)
// %1 = bitcast i8* %0 to type*
+10
View File
@@ -1,6 +1,8 @@
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "armv7m-none-eabi"
@runtime.zeroSizedAlloc = internal global i8 0, align 1
declare nonnull i8* @runtime.alloc(i32)
; Test allocating a single int (i32) that should be allocated on the stack.
@@ -68,6 +70,14 @@ end:
ret void
}
; Test a zero-sized allocation.
define void @testZeroSizedAlloc() {
%1 = call i8* @runtime.alloc(i32 0)
%2 = bitcast i8* %1 to i32*
%3 = call i32* @noescapeIntPtr(i32* %2)
ret void
}
declare i32* @escapeIntPtr(i32*)
declare i32* @noescapeIntPtr(i32* nocapture)
+8
View File
@@ -1,6 +1,8 @@
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "armv7m-none-eabi"
@runtime.zeroSizedAlloc = internal global i8 0, align 1
declare nonnull i8* @runtime.alloc(i32)
define void @testInt() {
@@ -66,6 +68,12 @@ end: ; preds = %loop
ret void
}
define void @testZeroSizedAlloc() {
%1 = bitcast i8* @runtime.zeroSizedAlloc to i32*
%2 = call i32* @noescapeIntPtr(i32* %1)
ret void
}
declare i32* @escapeIntPtr(i32*)
declare i32* @noescapeIntPtr(i32* nocapture)