From 9dddbf97980006c9cc039436bba452396944b10d Mon Sep 17 00:00:00 2001 From: Paul Kirth <paulkirth@google.com> Date: Mon, 14 Apr 2025 11:15:51 -0700 Subject: [PATCH 1/2] [llvm][lto] Precommit test for libcalls --- .../test/LTO/Resolution/RISCV/bcmp-libcall.ll | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll diff --git a/llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll b/llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll new file mode 100644 index 0000000000000..4c6bebf69a074 --- /dev/null +++ b/llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll @@ -0,0 +1,37 @@ +;; This test comes from a real world scenario in LTO, where the +;; definition of bcmp is deleted because it has no uses, but later instcombine +;; re-introduces a call to bcmp() as part of SimplifyLibCalls. + +; RUN: opt %s -o %t -module-summary -mtriple riscv32-unknown-linux-musl +; RUN: llvm-lto2 run -o %t2 \ +; RUN: -r %t,foo,plx \ +; RUN: -r %t,memcmp,x \ +; RUN: -r %t,bcmp,pl %t -save-temps +; RUN: llvm-dis %t2.1.2.internalize.bc -o - \ +; RUN: | FileCheck %s --check-prefix=INTERNALIZE +; RUN: opt %t2.1.2.internalize.bc -passes=instcombine -o - -S \ +; RUN: | FileCheck %s --check-prefix=OPTIMIZE + +define i1 @foo(ptr %0, [2 x i32] %1) { + ; OPTIMIZE-LABEL: define{{.*}}i1 @foo + ; OPTIMIZE-NEXT: %size = extractvalue [2 x i32] %1, 1 + ; OPTIMIZE-NEXT: %bcmp = {{.*}}call i32 @bcmp + ; OPTIMIZE-NEXT: %eq = icmp eq i32 %bcmp, 0 + ; OPTIMIZE-NEXT: ret i1 %eq + + %size = extractvalue [2 x i32] %1, 1 + %cmp = call i32 @memcmp(ptr %0, ptr null, i32 %size) + %eq = icmp eq i32 %cmp, 0 + ret i1 %eq +} + +; INTERNALIZE: declare i32 @memcmp(ptr, ptr, i32) +declare i32 @memcmp(ptr, ptr, i32) + +;; Ensure bcmp is removed from module. Follow up patches can address this. +; INTERNALIZE-NOT: declare{{.*}}i32 @bcmp +; INTERNALIZE-NOT: define{{.*}}i32 @bcmp +define i32 @bcmp(ptr %0, ptr %1, i32 %2) { + ret i32 0 +} + From 34d1490e6f0d1e9125134e5e1c360246ef3ebe57 Mon Sep 17 00:00:00 2001 From: Paul Kirth <paulkirth@google.com> Date: Tue, 15 Apr 2025 09:09:14 -0700 Subject: [PATCH 2/2] Revise test checks, avoid instcombine, and use x86_64 --- .../Resolution/{RISCV => X86}/bcmp-libcall.ll | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) rename llvm/test/LTO/Resolution/{RISCV => X86}/bcmp-libcall.ll (55%) diff --git a/llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll b/llvm/test/LTO/Resolution/X86/bcmp-libcall.ll similarity index 55% rename from llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll rename to llvm/test/LTO/Resolution/X86/bcmp-libcall.ll index 4c6bebf69a074..b4e526f83e60f 100644 --- a/llvm/test/LTO/Resolution/RISCV/bcmp-libcall.ll +++ b/llvm/test/LTO/Resolution/X86/bcmp-libcall.ll @@ -2,22 +2,20 @@ ;; definition of bcmp is deleted because it has no uses, but later instcombine ;; re-introduces a call to bcmp() as part of SimplifyLibCalls. -; RUN: opt %s -o %t -module-summary -mtriple riscv32-unknown-linux-musl +; RUN: opt %s -o %t -module-summary -mtriple x86_64-unknown-linux-musl ; RUN: llvm-lto2 run -o %t2 \ ; RUN: -r %t,foo,plx \ ; RUN: -r %t,memcmp,x \ ; RUN: -r %t,bcmp,pl %t -save-temps ; RUN: llvm-dis %t2.1.2.internalize.bc -o - \ -; RUN: | FileCheck %s --check-prefix=INTERNALIZE -; RUN: opt %t2.1.2.internalize.bc -passes=instcombine -o - -S \ -; RUN: | FileCheck %s --check-prefix=OPTIMIZE +; RUN: | FileCheck %s define i1 @foo(ptr %0, [2 x i32] %1) { - ; OPTIMIZE-LABEL: define{{.*}}i1 @foo - ; OPTIMIZE-NEXT: %size = extractvalue [2 x i32] %1, 1 - ; OPTIMIZE-NEXT: %bcmp = {{.*}}call i32 @bcmp - ; OPTIMIZE-NEXT: %eq = icmp eq i32 %bcmp, 0 - ; OPTIMIZE-NEXT: ret i1 %eq + ; CHECK-LABEL: define{{.*}}i1 @foo + ; CHECK-NEXT: %size = extractvalue [2 x i32] %1, 1 + ; CHECK-NEXT: %cmp = {{.*}}call i32 @memcmp + ; CHECK-NEXT: %eq = icmp eq i32 %cmp, 0 + ; CHECK-NEXT: ret i1 %eq %size = extractvalue [2 x i32] %1, 1 %cmp = call i32 @memcmp(ptr %0, ptr null, i32 %size) @@ -25,12 +23,12 @@ define i1 @foo(ptr %0, [2 x i32] %1) { ret i1 %eq } -; INTERNALIZE: declare i32 @memcmp(ptr, ptr, i32) +; CHECK: declare i32 @memcmp(ptr, ptr, i32) declare i32 @memcmp(ptr, ptr, i32) ;; Ensure bcmp is removed from module. Follow up patches can address this. -; INTERNALIZE-NOT: declare{{.*}}i32 @bcmp -; INTERNALIZE-NOT: define{{.*}}i32 @bcmp +; CHECK-NOT: declare{{.*}}i32 @bcmp +; CHECK-NOT: define{{.*}}i32 @bcmp define i32 @bcmp(ptr %0, ptr %1, i32 %2) { ret i32 0 }