Skip to content

Commit 2212e90

Browse files
committed
[ELF,LTO] Test calloc defined in a lazy bitcode file for (malloc+memset => calloc) libcall optimization
Similar to https://reviews.llvm.org/D50017: malloc+memset references can be combined to a calloc reference, which is not explicit in the referencer's IR symbol table. If calloc is defined in a lazy bitcode file, we should extract the archive member to satisfy possible references from LTO generated object files; otherwise (current status, which will be fixed by #72673), `calloc` as a LazyObject symbol will be resolved by compileBitcodeFiles generated Undefined, leading to an incorrectly-extracted Defined symbol without section, which will lower to an SHN_ABS symbol at address 0.
1 parent 9fa2d74 commit 2212e90

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
; REQUIRES: x86
2+
;; malloc+memset references can be combined to a calloc reference.
3+
;; Test that we extract calloc defined in a lazy bitcode file to satisfy
4+
;; possible references from LTO generated object files.
5+
6+
; RUN: rm -rf %t && split-file %s %t && cd %t
7+
; RUN: llvm-as a.ll -o a.bc
8+
; RUN: llvm-as calloc.ll -o calloc.bc
9+
; RUN: llvm-mc -filetype=obj -triple=x86_64 lib.s -o lib.o
10+
; RUN: ld.lld -u foo a.bc --start-lib calloc.bc lib.o --end-lib -o t --save-temps
11+
; RUN: llvm-dis < t.0.4.opt.bc | FileCheck %s
12+
; RUN: llvm-nm t | FileCheck %s --check-prefix=NM
13+
14+
; CHECK: declare noalias noundef ptr @calloc(i64 noundef, i64 noundef)
15+
16+
; NM-NOT: {{.}}
17+
; NM: {{.*}} T _start
18+
;; TODO: Currently the symbol is lazy, which lowers to a SHN_ABS symbol at address 0.
19+
; NM-NEXT: {{.*}} A calloc
20+
; NM-NEXT: {{.*}} T foo
21+
; NM-NEXT: {{.*}} T malloc
22+
; NM-NEXT: {{.*}} T memset
23+
; NM-NOT: {{.}}
24+
25+
;--- a.ll
26+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
27+
target triple = "x86_64-unknown-linux-gnu"
28+
29+
define ptr @foo() noinline {
30+
entry:
31+
%call = tail call noalias ptr @malloc(i64 400)
32+
tail call void @llvm.memset.p0.i64(ptr %call, i8 0, i64 400, i1 false)
33+
ret ptr %call
34+
}
35+
36+
define void @_start(ptr %a, ptr %b) {
37+
entry:
38+
call ptr @foo()
39+
ret void
40+
}
41+
42+
declare ptr @malloc(i64)
43+
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
44+
45+
;--- calloc.ll
46+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
47+
target triple = "x86_64-unknown-linux-gnu"
48+
49+
define void @calloc(i64, i64) {
50+
entry:
51+
ret void
52+
}
53+
54+
;--- lib.s
55+
.globl malloc, memset
56+
malloc:
57+
memset:

0 commit comments

Comments
 (0)