[clang] [lldb] [clang] Register builtin identifiers lazily; share a name index (PR #243274)

Alexis Engelke via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 00:06:14 PST 2026


================
@@ -229,32 +230,97 @@ struct InfosShard {
       CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE),       \
       HeaderDesc::HEADER, LANG},
 
+/// The builtins of a target and an optional aux target: their shards, and a
+/// hash table from their names to their builtin IDs.
+///
+/// It contains every builtin of the targets, whether or not it's supported by
+/// some language options; \c Context checks that when looking a name up. It's
+/// immutable, so any number of contexts for the same targets can use it, also
+/// concurrently.
+class TargetBuiltins {
+  /// Open addressing with linear probing. A slot holds the hash of the name in
+  /// the upper 32 bits and the builtin ID in the lower 32 bits, or zero if
+  /// it's empty (zero is \c NotBuiltin).
+  std::unique_ptr<uint64_t[]> Slots;
+  size_t Mask = 0;
+
+  /// The target-independent shard, then the target's and the aux target's
+  /// shards: the order of their builtin IDs.
+  llvm::SmallVector<InfosShard, 16> Shards;
+  unsigned NumTargetShards = 0;
+
+  unsigned NumTargetBuiltins = 0;
+  unsigned NumAuxTargetBuiltins = 0;
+
+  /// Whether the builtin with \p ID is named \p Name.
+  bool hasName(unsigned ID, llvm::StringRef Name) const;
+
+public:
+  /// Collect the builtins of \p Target and \p AuxTarget, which may be null.
+  TargetBuiltins(const TargetInfo &Target, const TargetInfo *AuxTarget);
+
+  /// The shards of the target's builtins.
+  llvm::ArrayRef<InfosShard> getTargetShards() const {
+    return llvm::ArrayRef(Shards).slice(1, NumTargetShards);
+  }
+
+  /// The shards of the aux target's builtins.
+  llvm::ArrayRef<InfosShard> getAuxTargetShards() const {
+    return llvm::ArrayRef(Shards).drop_front(1 + NumTargetShards);
+  }
+
+  unsigned getNumTargetBuiltins() const { return NumTargetBuiltins; }
+
+  /// Return the shard of the builtin with \p ID and its info.
+  std::pair<const InfosShard &, const Info &>
+  getShardAndInfo(unsigned ID) const;
+
+  /// Return the IDs of all builtins named \p Name, in no particular order.
+  llvm::SmallVector<unsigned, 2> lookup(llvm::StringRef Name) const;
+};
+
 /// Holds information about both target-independent and
 /// target-specific builtins, allowing easy queries by clients.
 ///
-/// Builtins from an optional auxiliary target are stored in
-/// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
-/// be translated back with getAuxBuiltinID() before use.
+/// Builtins from an optional auxiliary target follow the target's builtins.
+/// Their IDs are shifted up by the number of target builtins and need to be
+/// translated back with getAuxBuiltinID() before use.
 class Context {
-  llvm::SmallVector<InfosShard> BuiltinShards;
-
-  llvm::SmallVector<InfosShard> TargetShards;
-  llvm::SmallVector<InfosShard> AuxTargetShards;
+  /// The builtins of the target and the aux target. Null for a context without
+  /// a target, which only has the target-independent builtins.
+  std::shared_ptr<const TargetBuiltins> Builtins;
----------------
aengelke wrote:

Why is this a shared_ptr? We avoid using them where possible as they are rather expensive (esp. atomic refcounts in multi-threaded environments, vtables). If shared ownership is truly required, I think the preferred way is IntrusiveRefCntPtr.

https://github.com/llvm/llvm-project/pull/243274


More information about the cfe-commits mailing list