[PATCH] D97735: [Globals] Treat nobuiltin fns as maybe-derefined.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 1 14:09:39 PST 2021


fhahn created this revision.
fhahn added reviewers: probinson, jdoerfert, rsmith, jeroen.dobbelaere.
Herald added subscribers: dexonsmith, hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

Fixes PR49022.

Callsites could be marked as `builtin` while calling `nobuiltin`
functions. This can lead to problems, if local optimizations apply
transformations based on the semantics of the builtin, but then IPO
treats the function as `nobuiltin` and applies a transform that breaks
builtin semantics (assumed earlier).

To avoid this, mark such functions as maybey-derefined, to avoid IPO
transforms on them that may break assumptions of earlier calls.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97735

Files:
  llvm/include/llvm/IR/GlobalValue.h
  llvm/lib/IR/Globals.cpp
  llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll


Index: llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
===================================================================
--- llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
+++ llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
@@ -12,7 +12,7 @@
 define dso_local i32 @test_fn(i8* %ptr) {
 ; CHECK-LABEL: @test_fn(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    call void @_ZdlPv(i8* undef)
+; CHECK-NEXT:    call void @_ZdlPv(i8* %ptr)
 ; CHECK-NEXT:    ret i32 1
 ;
 entry:
Index: llvm/lib/IR/Globals.cpp
===================================================================
--- llvm/lib/IR/Globals.cpp
+++ llvm/lib/IR/Globals.cpp
@@ -225,6 +225,13 @@
   setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
 }
 
+bool GlobalValue::isNobuiltinFnDef() const {
+  const Function *F = dyn_cast<Function>(this);
+  if (!F || F->empty())
+    return false;
+  return F->hasFnAttribute(Attribute::NoBuiltin);
+}
+
 bool GlobalValue::isDeclaration() const {
   // Globals are definitions if they have an initializer.
   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(this))
Index: llvm/include/llvm/IR/GlobalValue.h
===================================================================
--- llvm/include/llvm/IR/GlobalValue.h
+++ llvm/include/llvm/IR/GlobalValue.h
@@ -140,12 +140,20 @@
     case AppendingLinkage:
     case InternalLinkage:
     case PrivateLinkage:
-      return isInterposable();
+      // Optimizations may assume builtin semantics for functions defined as
+      // nobuiltin due to attributes at call-sites. To avoid applying IPO based
+      // on nobuiltin semantics, treat such function definitions as maybe
+      // derefined.
+      return isInterposable() || isNobuiltinFnDef();
     }
 
     llvm_unreachable("Fully covered switch above!");
   }
 
+  /// Returns true if the global is a function definition with the nobuiltin
+  /// attribute.
+  bool isNobuiltinFnDef() const;
+
 protected:
   /// The intrinsic ID for this subclass (which must be a Function).
   ///


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97735.327289.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210301/1fa13fe2/attachment.bin>


More information about the llvm-commits mailing list