[llvm] 5913d77 - [Globals] Treat nobuiltin fns as maybe-derefined.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 23 05:45:26 PDT 2022
Author: Florian Hahn
Date: 2022-08-23T13:45:10+01:00
New Revision: 5913d7705618a7ccacf8207086c2bb0fc0d032a8
URL: https://github.com/llvm/llvm-project/commit/5913d7705618a7ccacf8207086c2bb0fc0d032a8
DIFF: https://github.com/llvm/llvm-project/commit/5913d7705618a7ccacf8207086c2bb0fc0d032a8.diff
LOG: [Globals] Treat nobuiltin fns as maybe-derefined.
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.
Fixes #57075
Fixes #48366
Reviewed By: ychen
Differential Revision: https://reviews.llvm.org/D97735
Added:
Modified:
llvm/include/llvm/IR/GlobalValue.h
llvm/lib/IR/Globals.cpp
llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
index 06702d3cdf6b9..63c5d1b5f5c79 100644
--- a/llvm/include/llvm/IR/GlobalValue.h
+++ b/llvm/include/llvm/IR/GlobalValue.h
@@ -145,12 +145,20 @@ class GlobalValue : public Constant {
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).
///
diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 51a22897babdb..684cff831068c 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -261,6 +261,13 @@ void GlobalObject::setSection(StringRef S) {
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))
diff --git a/llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll b/llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
index 45a4b56d7c761..258b4cddb5dab 100644
--- a/llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
+++ b/llvm/test/Transforms/DeadArgElim/pr49022-builtin-nobuiltin.ll
@@ -13,7 +13,7 @@ entry:
define dso_local i32 @test_fn(ptr %ptr) {
; CHECK-LABEL: @test_fn(
; CHECK-NEXT: entry:
-; CHECK-NEXT: call void @_ZdlPv(ptr poison) #[[ATTR1:[0-9]+]]
+; CHECK-NEXT: call void @_ZdlPv(ptr [[PTR:%.*]]) #[[ATTR1:[0-9]+]]
; CHECK-NEXT: ret i32 1
;
entry:
More information about the llvm-commits
mailing list