[PATCH] D123091: MemoryBuiltins: accept non-TLI funcs with attribs as allocator funcs

Augie Fackler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 7 07:36:44 PDT 2022


durin42 updated this revision to Diff 421213.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123091/new/

https://reviews.llvm.org/D123091

Files:
  llvm/lib/Analysis/MemoryBuiltins.cpp
  llvm/test/Transforms/InstCombine/out-of-tree-allocator-optimizes-away.ll


Index: llvm/test/Transforms/InstCombine/out-of-tree-allocator-optimizes-away.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/out-of-tree-allocator-optimizes-away.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=instcombine %s | FileCheck %s
+
+; Function Attrs: nonlazybind uwtable
+define void @alloc_elides_test(i32 %data){
+; CHECK-LABEL: @alloc_elides_test(
+; CHECK-NEXT:  start:
+; CHECK-NEXT:    ret void
+;
+start:
+  %0 = tail call i8* @__rust_alloc(i64 4, i64 32)
+  tail call void @__rust_dealloc(i8* nonnull %0, i64 4, i64 32)
+  ret void
+}
+
+declare noalias i8* @__rust_alloc(i64, i64 allocalign) unnamed_addr nounwind nonlazybind allocsize(0) uwtable allockind("new,uninitialized,aligned") "alloc-family"="__rust_alloc"
+
+declare void @__rust_dealloc(i8* allocptr, i64, i64) unnamed_addr nounwind nonlazybind uwtable allockind("free") "alloc-family"="__rust_alloc"
Index: llvm/lib/Analysis/MemoryBuiltins.cpp
===================================================================
--- llvm/lib/Analysis/MemoryBuiltins.cpp
+++ llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -525,14 +525,17 @@
   if (Callee == nullptr || IsNoBuiltin)
     return None;
   LibFunc TLIFn;
-  if (!TLI || !TLI->getLibFunc(*Callee, TLIFn) || !TLI->has(TLIFn))
-    return None;
-  const auto AllocData = getAllocationDataForFunction(Callee, AnyAlloc, TLI);
-  if (AllocData.hasValue())
-    return mangledNameForMallocFamily(AllocData.getValue().Family);
-  const auto FreeData = getFreeFunctionDataForFunction(Callee, TLIFn);
-  if (FreeData.hasValue())
-    return mangledNameForMallocFamily(FreeData.getValue().Family);
+
+  if (TLI && TLI->getLibFunc(*Callee, TLIFn) && TLI->has(TLIFn)) {
+    // Callee is some known library function.
+    const auto AllocData = getAllocationDataForFunction(Callee, AnyAlloc, TLI);
+    if (AllocData.hasValue())
+      return mangledNameForMallocFamily(AllocData.getValue().Family);
+    const auto FreeData = getFreeFunctionDataForFunction(Callee, TLIFn);
+    if (FreeData.hasValue())
+      return mangledNameForMallocFamily(FreeData.getValue().Family);
+  }
+  // Callee isn't a known library function, still check attributes.
   if (checkFnAllocKind(I, AllocFnKind::Free) ||
       checkFnAllocKind(I, AllocFnKind::New) ||
       checkFnAllocKind(I, AllocFnKind::Resize))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123091.421213.patch
Type: text/x-patch
Size: 2444 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220407/1a450fef/attachment.bin>


More information about the llvm-commits mailing list