[PATCH] D102649: [GlobalOpt] Introduce and prevent elimination of implementedby metadata
William Moses via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon May 17 13:18:38 PDT 2021
wsmoses created this revision.
wsmoses added a reviewer: jdoerfert.
Herald added subscribers: dexonsmith, hiraditya.
wsmoses requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Introduce implementedby metadata, which will be used to represent a function whose implementation
will be replaced by the corresponding implementation. In addition to introducing this metadata, this patch
modifies globalopt to prevent removal of the requisite implementation and user.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D102649
Files:
llvm/include/llvm/IR/FixedMetadataKinds.def
llvm/lib/Transforms/IPO/GlobalOpt.cpp
llvm/test/Transforms/GlobalOpt/implemented_by.ll
Index: llvm/test/Transforms/GlobalOpt/implemented_by.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/GlobalOpt/implemented_by.ll
@@ -0,0 +1,26 @@
+; RUN: opt < %s -globalopt -S | FileCheck %s
+
+declare !implementedby !0 double @llvm.sin.f64(double)
+; CHECK: declare !implementedby [[sinimpl:.+]] double @llvm.sin.f64
+declare double @sin(double)
+
+define internal double @mycos(double %x) {
+ ret double %x
+}
+; Calling convention is not changed
+; CHECK: define internal double @mycos(double %x)
+
+declare !implementedby !1 double @llvm.cos.f64(double)
+; CHECK: declare !implementedby [[cosimpl:.+]] double @llvm.cos.f64
+
+define double @cos(double %x) !implementedby !1 {
+ ret double %x
+}
+; CHECK: define double @cos{{.+}} !implementedby [[cosimpl]] {
+
+!0 = !{double(double)* @sin}
+!1 = !{double(double)* @mycos}
+
+; CHECK: [[sinimpl]] = !{double (double)* @sin}
+; CHECK: [[cosimpl]] = !{double (double)* @mycos}
+
Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
===================================================================
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -45,6 +45,7 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
@@ -1809,8 +1810,6 @@
Dead = GV.use_empty();
if (!Dead)
return false;
-
- LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
GV.eraseFromParent();
++NumDeleted;
return true;
@@ -2395,17 +2394,33 @@
bool Changed = false;
+ SmallPtrSet<Function *, 4> ImplementedByUses;
std::vector<Function *> AllCallsCold;
for (Module::iterator FI = M.begin(), E = M.end(); FI != E;) {
Function *F = &*FI++;
if (hasOnlyColdCalls(*F, GetBFI))
AllCallsCold.push_back(F);
+
+ auto MD = F->getMetadata(LLVMContext::MD_implementedby);
+ if (MD) {
+ ImplementedByUses.insert(F);
+ auto Impl =
+ cast<ValueAsMetadata>(cast<MDTuple>(MD)->getOperand(0))->getValue();
+ if (auto F2 = dyn_cast<Function>(Impl))
+ ImplementedByUses.insert(F2);
+ }
}
// Optimize functions.
for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
Function *F = &*FI++;
+ // Don't perform global opt on functions used within implemented by
+ // we neither want theses functions deleted, nor with a different
+ // calling convention.
+ if (ImplementedByUses.count(F))
+ continue;
+
// Don't perform global opt pass on naked functions; we don't want fast
// calling conventions for naked functions.
if (F->hasFnAttribute(Attribute::Naked))
Index: llvm/include/llvm/IR/FixedMetadataKinds.def
===================================================================
--- llvm/include/llvm/IR/FixedMetadataKinds.def
+++ llvm/include/llvm/IR/FixedMetadataKinds.def
@@ -42,3 +42,4 @@
LLVM_FIXED_MD_KIND(MD_vcall_visibility, "vcall_visibility", 28)
LLVM_FIXED_MD_KIND(MD_noundef, "noundef", 29)
LLVM_FIXED_MD_KIND(MD_annotation, "annotation", 30)
+LLVM_FIXED_MD_KIND(MD_implementedby, "implementedby", 31)
\ No newline at end of file
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102649.345983.patch
Type: text/x-patch
Size: 3263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210517/a246bba9/attachment-0001.bin>
More information about the llvm-commits
mailing list