[PATCH] D130319: [Internalize] Support glob patterns for API lists

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 21 17:49:09 PDT 2022


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, tra, yaxunl, JonChesterfield, MaskRay, fhahn.
Herald added subscribers: ormris, StephenFan, hiraditya.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The internalize pass supports an option to provide a list of symbols
that should not be internalized. THis is useful retaining certain
defintions that should be kept alive. However, this interface is
somewhat difficult to use as it requires knowing every single symbol's
name and specifying it. Many APIs provide common prefixes for the
symbols exported by the library, so it would make sense to be able to
match these using a simple glob pattern. This patch changes the handling
from a simple string comparison to a glob pattern match.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130319

Files:
  llvm/lib/Transforms/IPO/Internalize.cpp
  llvm/test/Transforms/Internalize/globs.ll


Index: llvm/test/Transforms/Internalize/globs.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/Internalize/globs.ll
@@ -0,0 +1,22 @@
+; RUN: opt < %s -internalize -internalize-public-api-list bar?,_*,*_,[ab] -S | FileCheck %s
+
+; CHECK: @foo = internal global
+ at foo = global i32 0
+
+; CHECK: @bar_ = global
+ at bar_ = global i32 0
+
+; CHECK: @_foo = global
+ at _foo = global i32 0
+
+; CHECK: @foo_ = global
+ at foo_ = global i32 0
+
+; CHECK: @a = global
+ at a = global i32 0
+
+; CHECK: @b = global
+ at b = global i32 0
+
+; CHECK: @c = internal global
+ at c = global i32 0
Index: llvm/lib/Transforms/IPO/Internalize.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Internalize.cpp
+++ llvm/lib/Transforms/IPO/Internalize.cpp
@@ -28,6 +28,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/GlobPattern.h"
 #include "llvm/Support/LineIterator.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
@@ -59,29 +60,44 @@
   PreserveAPIList() {
     if (!APIFile.empty())
       LoadFile(APIFile);
-    ExternalNames.insert(APIList.begin(), APIList.end());
+    for (StringRef Pattern : APIList)
+      addGlob(Pattern);
   }
 
   bool operator()(const GlobalValue &GV) {
-    return ExternalNames.count(GV.getName());
+    return llvm::any_of(
+        ExternalNames, [&](GlobPattern &GP) { return GP.match(GV.getName()); });
   }
 
 private:
   // Contains the set of symbols loaded from file
-  StringSet<> ExternalNames;
+  SmallVector<GlobPattern> ExternalNames;
+
+  void addGlob(StringRef Pattern) {
+    auto GlobOrErr = GlobPattern::create(Pattern);
+    if (!GlobOrErr) {
+      errs() << "WARNING: when loading pattern: '"
+             << toString(GlobOrErr.takeError()) << "' ignoring";
+      return;
+    }
+    ExternalNames.emplace_back(std::move(*GlobOrErr));
+  }
 
   void LoadFile(StringRef Filename) {
     // Load the APIFile...
-    ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
+    ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
         MemoryBuffer::getFile(Filename);
-    if (!Buf) {
+    if (!BufOrErr) {
       errs() << "WARNING: Internalize couldn't load file '" << Filename
              << "'! Continuing as if it's empty.\n";
       return; // Just continue as if the file were empty
     }
-    for (line_iterator I(*Buf->get(), true), E; I != E; ++I)
-      ExternalNames.insert(*I);
+    Buf = std::move(*BufOrErr);
+    for (line_iterator I(*Buf, true), E; I != E; ++I)
+      addGlob(*I);
   }
+
+  std::shared_ptr<MemoryBuffer> Buf;
 };
 } // end anonymous namespace
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130319.446672.patch
Type: text/x-patch
Size: 2715 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220722/ee5b09d8/attachment.bin>


More information about the llvm-commits mailing list