Skip to content

Revert "Switch builtin strings to use string tables" #119638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 34 additions & 169 deletions clang/include/clang/Basic/Builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ struct HeaderDesc {
#undef HEADER
} ID;

constexpr HeaderDesc() : ID() {}
constexpr HeaderDesc(HeaderID ID) : ID(ID) {}

const char *getName() const;
Expand All @@ -69,152 +68,23 @@ enum ID {
FirstTSBuiltin
};

// The info used to represent each builtin.
struct Info {
// Rather than store pointers to the string literals describing these four
// aspects of builtins, we store offsets into a common string table.
struct StrOffsets {
int Name;
int Type;
int Attributes;
int Features;
} Offsets;

llvm::StringLiteral Name;
const char *Type, *Attributes;
const char *Features;
HeaderDesc Header;
LanguageID Langs;
};

// The storage for `N` builtins. This contains a single pointer to the string
// table used for these builtins and an array of metadata for each builtin.
template <size_t N> struct Storage {
const char *StringTable;

std::array<Info, N> Infos;

// A constexpr function to construct the storage for a a given string table in
// the first argument and an array in the second argument. This is *only*
// expected to be used at compile time, we should mark it `consteval` when
// available.
//
// The `Infos` array is particularly special. This function expects an array
// of `Info` structs, where the string offsets of each entry refer to the
// *sizes* of those strings rather than their offsets, and for the target
// string to be in the provided string table at an offset the sum of all
// previous string sizes. This function walks the `Infos` array computing the
// running sum and replacing the sizes with the actual offsets in the string
// table that should be used. This arrangement is designed to make it easy to
// expand `.def` and `.inc` files with X-macros to construct both the string
// table and the `Info` structs in the arguments to this function.
static constexpr Storage<N> Make(const char *Strings,
std::array<Info, N> Infos) {
// Translate lengths to offsets.
int Offset = 0;
for (auto &I : Infos) {
Info::StrOffsets NewOffsets = {};
NewOffsets.Name = Offset;
Offset += I.Offsets.Name;
NewOffsets.Type = Offset;
Offset += I.Offsets.Type;
NewOffsets.Attributes = Offset;
Offset += I.Offsets.Attributes;
NewOffsets.Features = Offset;
Offset += I.Offsets.Features;
I.Offsets = NewOffsets;
}
return {Strings, Infos};
}
};

// A detail macro used below to emit a string literal that, after string literal
// concatenation, ends up triggering the `-Woverlength-strings` warning. While
// the warning is useful in general to catch accidentally excessive strings,
// here we are creating them intentionally.
//
// This relies on a subtle aspect of `_Pragma`: that the *diagnostic* ones don't
// turn into actual tokens that would disrupt string literal concatenation.
#ifdef __clang__
#define CLANG_BUILTIN_DETAIL_STR_TABLE(S) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Woverlength-strings\"") \
S _Pragma("clang diagnostic pop")
#else
#define CLANG_BUILTIN_DETAIL_STR_TABLE(S) S
#endif

// A macro that can be used with `Builtins.def` and similar files as an X-macro
// to add the string arguments to a builtin string table. This is typically the
// target for the `BUILTIN`, `LANGBUILTIN`, or `LIBBUILTIN` macros in those
// files.
#define CLANG_BUILTIN_STR_TABLE(ID, TYPE, ATTRS) \
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" /*FEATURE*/ "\0")

// A macro that can be used with target builtin `.def` and `.inc` files as an
// X-macro to add the string arguments to a builtin string table. this is
// typically the target for the `TARGET_BUILTIN` macro.
#define CLANG_TARGET_BUILTIN_STR_TABLE(ID, TYPE, ATTRS, FEATURE) \
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" FEATURE "\0")

// A macro that can be used with target builtin `.def` and `.inc` files as an
// X-macro to add the string arguments to a builtin string table. this is
// typically the target for the `TARGET_HEADER_BUILTIN` macro. We can't delegate
// to `TARGET_BUILTIN` because the `FEATURE` string changes position.
#define CLANG_TARGET_HEADER_BUILTIN_STR_TABLE(ID, TYPE, ATTRS, HEADER, LANGS, \
FEATURE) \
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" FEATURE "\0")

// A detail macro used internally to compute the desired string table
// `StrOffsets` struct for arguments to `Storage::Make`.
#define CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS) \
Builtin::Info::StrOffsets { \
sizeof(#ID), sizeof(TYPE), sizeof(ATTRS), sizeof("") \
}

// A detail macro used internally to compute the desired string table
// `StrOffsets` struct for arguments to `Storage::Make`.
#define CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE) \
Builtin::Info::StrOffsets { \
sizeof(#ID), sizeof(TYPE), sizeof(ATTRS), sizeof(FEATURE) \
}

// A set of macros that can be used with builtin `.def' files as an X-macro to
// create an `Info` struct for a particular builtin. It both computes the
// `StrOffsets` value for the string table (the lengths here, translated to
// offsets by the Storage::Make function), and the other metadata for each
// builtin.
//
// There is a corresponding macro for each of `BUILTIN`, `LANGBUILTIN`,
// `LIBBUILTIN`, `TARGET_BUILTIN`, and `TARGET_HEADER_BUILTIN`.
#define CLANG_BUILTIN_ENTRY(ID, TYPE, ATTRS) \
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
HeaderDesc::NO_HEADER, ALL_LANGUAGES},
#define CLANG_LANGBUILTIN_ENTRY(ID, TYPE, ATTRS, LANG) \
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
HeaderDesc::NO_HEADER, LANG},
#define CLANG_LIBBUILTIN_ENTRY(ID, TYPE, ATTRS, HEADER, LANG) \
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
HeaderDesc::HEADER, LANG},
#define CLANG_TARGET_BUILTIN_ENTRY(ID, TYPE, ATTRS, FEATURE) \
Builtin::Info{ \
CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE), \
HeaderDesc::NO_HEADER, ALL_LANGUAGES},
#define CLANG_TARGET_HEADER_BUILTIN_ENTRY(ID, TYPE, ATTRS, HEADER, LANG, \
FEATURE) \
Builtin::Info{ \
CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE), \
HeaderDesc::HEADER, LANG},

/// 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.
class Context {
const char *TSStrTable = nullptr;
const char *AuxTSStrTable = nullptr;

llvm::ArrayRef<Info> TSInfos;
llvm::ArrayRef<Info> AuxTSInfos;
llvm::ArrayRef<Info> TSRecords;
llvm::ArrayRef<Info> AuxTSRecords;

public:
Context() = default;
Expand All @@ -230,13 +100,10 @@ class Context {

/// Return the identifier name for the specified builtin,
/// e.g. "__builtin_abs".
llvm::StringRef getName(unsigned ID) const;
llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }

/// Get the type descriptor string for the specified builtin.
const char *getTypeString(unsigned ID) const;

/// Get the attributes descriptor string for the specified builtin.
const char *getAttributesString(unsigned ID) const;
const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }

/// Return true if this function is a target-specific builtin.
bool isTSBuiltin(unsigned ID) const {
Expand All @@ -245,40 +112,40 @@ class Context {

/// Return true if this function has no side effects.
bool isPure(unsigned ID) const {
return strchr(getAttributesString(ID), 'U') != nullptr;
return strchr(getRecord(ID).Attributes, 'U') != nullptr;
}

/// Return true if this function has no side effects and doesn't
/// read memory.
bool isConst(unsigned ID) const {
return strchr(getAttributesString(ID), 'c') != nullptr;
return strchr(getRecord(ID).Attributes, 'c') != nullptr;
}

/// Return true if we know this builtin never throws an exception.
bool isNoThrow(unsigned ID) const {
return strchr(getAttributesString(ID), 'n') != nullptr;
return strchr(getRecord(ID).Attributes, 'n') != nullptr;
}

/// Return true if we know this builtin never returns.
bool isNoReturn(unsigned ID) const {
return strchr(getAttributesString(ID), 'r') != nullptr;
return strchr(getRecord(ID).Attributes, 'r') != nullptr;
}

/// Return true if we know this builtin can return twice.
bool isReturnsTwice(unsigned ID) const {
return strchr(getAttributesString(ID), 'j') != nullptr;
return strchr(getRecord(ID).Attributes, 'j') != nullptr;
}

/// Returns true if this builtin does not perform the side-effects
/// of its arguments.
bool isUnevaluated(unsigned ID) const {
return strchr(getAttributesString(ID), 'u') != nullptr;
return strchr(getRecord(ID).Attributes, 'u') != nullptr;
}

/// Return true if this is a builtin for a libc/libm function,
/// with a "__builtin_" prefix (e.g. __builtin_abs).
bool isLibFunction(unsigned ID) const {
return strchr(getAttributesString(ID), 'F') != nullptr;
return strchr(getRecord(ID).Attributes, 'F') != nullptr;
}

/// Determines whether this builtin is a predefined libc/libm
Expand All @@ -289,29 +156,29 @@ class Context {
/// they do not, but they are recognized as builtins once we see
/// a declaration.
bool isPredefinedLibFunction(unsigned ID) const {
return strchr(getAttributesString(ID), 'f') != nullptr;
return strchr(getRecord(ID).Attributes, 'f') != nullptr;
}

/// Returns true if this builtin requires appropriate header in other
/// compilers. In Clang it will work even without including it, but we can emit
/// a warning about missing header.
bool isHeaderDependentFunction(unsigned ID) const {
return strchr(getAttributesString(ID), 'h') != nullptr;
return strchr(getRecord(ID).Attributes, 'h') != nullptr;
}

/// Determines whether this builtin is a predefined compiler-rt/libgcc
/// function, such as "__clear_cache", where we know the signature a
/// priori.
bool isPredefinedRuntimeFunction(unsigned ID) const {
return strchr(getAttributesString(ID), 'i') != nullptr;
return strchr(getRecord(ID).Attributes, 'i') != nullptr;
}

/// Determines whether this builtin is a C++ standard library function
/// that lives in (possibly-versioned) namespace std, possibly a template
/// specialization, where the signature is determined by the standard library
/// declaration.
bool isInStdNamespace(unsigned ID) const {
return strchr(getAttributesString(ID), 'z') != nullptr;
return strchr(getRecord(ID).Attributes, 'z') != nullptr;
}

/// Determines whether this builtin can have its address taken with no
Expand All @@ -325,33 +192,33 @@ class Context {

/// Determines whether this builtin has custom typechecking.
bool hasCustomTypechecking(unsigned ID) const {
return strchr(getAttributesString(ID), 't') != nullptr;
return strchr(getRecord(ID).Attributes, 't') != nullptr;
}

/// Determines whether a declaration of this builtin should be recognized
/// even if the type doesn't match the specified signature.
bool allowTypeMismatch(unsigned ID) const {
return strchr(getAttributesString(ID), 'T') != nullptr ||
return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
hasCustomTypechecking(ID);
}

/// Determines whether this builtin has a result or any arguments which
/// are pointer types.
bool hasPtrArgsOrResult(unsigned ID) const {
return strchr(getTypeString(ID), '*') != nullptr;
return strchr(getRecord(ID).Type, '*') != nullptr;
}

/// Return true if this builtin has a result or any arguments which are
/// reference types.
bool hasReferenceArgsOrResult(unsigned ID) const {
return strchr(getTypeString(ID), '&') != nullptr ||
strchr(getTypeString(ID), 'A') != nullptr;
return strchr(getRecord(ID).Type, '&') != nullptr ||
strchr(getRecord(ID).Type, 'A') != nullptr;
}

/// If this is a library function that comes from a specific
/// header, retrieve that header name.
const char *getHeaderName(unsigned ID) const {
return getInfo(ID).Header.getName();
return getRecord(ID).Header.getName();
}

/// Determine whether this builtin is like printf in its
Expand All @@ -376,25 +243,27 @@ class Context {
/// Such functions can be const when the MathErrno lang option and FP
/// exceptions are disabled.
bool isConstWithoutErrnoAndExceptions(unsigned ID) const {
return strchr(getAttributesString(ID), 'e') != nullptr;
return strchr(getRecord(ID).Attributes, 'e') != nullptr;
}

bool isConstWithoutExceptions(unsigned ID) const {
return strchr(getAttributesString(ID), 'g') != nullptr;
return strchr(getRecord(ID).Attributes, 'g') != nullptr;
}

const char *getRequiredFeatures(unsigned ID) const;
const char *getRequiredFeatures(unsigned ID) const {
return getRecord(ID).Features;
}

unsigned getRequiredVectorWidth(unsigned ID) const;

/// Return true if builtin ID belongs to AuxTarget.
bool isAuxBuiltinID(unsigned ID) const {
return ID >= (Builtin::FirstTSBuiltin + TSInfos.size());
return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
}

/// Return real builtin ID (i.e. ID it would have during compilation
/// for AuxTarget).
unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSInfos.size(); }
unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }

/// Returns true if this is a libc/libm function without the '__builtin_'
/// prefix.
Expand All @@ -406,20 +275,16 @@ class Context {

/// Return true if this function can be constant evaluated by Clang frontend.
bool isConstantEvaluated(unsigned ID) const {
return strchr(getAttributesString(ID), 'E') != nullptr;
return strchr(getRecord(ID).Attributes, 'E') != nullptr;
}

/// Returns true if this is an immediate (consteval) function
bool isImmediate(unsigned ID) const {
return strchr(getAttributesString(ID), 'G') != nullptr;
return strchr(getRecord(ID).Attributes, 'G') != nullptr;
}

private:
std::pair<const char *, const Info &> getStrTableAndInfo(unsigned ID) const;

const Info &getInfo(unsigned ID) const {
return getStrTableAndInfo(ID).second;
}
const Info &getRecord(unsigned ID) const;

/// Helper function for isPrintfLike and isScanfLike.
bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/Basic/BuiltinsPPC.def
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,5 @@ UNALIASED_CUSTOM_BUILTIN(mma_pmxvbf16ger2nn, "vW512*VVi15i15i3", true,
// FIXME: Obviously incomplete.

#undef BUILTIN
#undef TARGET_BUILTIN
#undef CUSTOM_BUILTIN
#undef UNALIASED_CUSTOM_BUILTIN
10 changes: 4 additions & 6 deletions clang/include/clang/Basic/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "clang/Basic/AddressSpaces.h"
#include "clang/Basic/BitmaskEnum.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/CFProtectionOptions.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/LLVM.h"
Expand Down Expand Up @@ -1010,11 +1009,10 @@ class TargetInfo : public TransferrableTargetInfo,
virtual void getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const = 0;

/// Return information about target-specific builtins for the current primary
/// target, and info about which builtins are non-portable across the current
/// set of primary and secondary targets.
virtual std::pair<const char *, ArrayRef<Builtin::Info>>
getTargetBuiltinStorage() const = 0;
/// Return information about target-specific builtins for
/// the current primary target, and info about which builtins are non-portable
/// across the current set of primary and secondary targets.
virtual ArrayRef<Builtin::Info> getTargetBuiltins() const = 0;

/// Returns target-specific min and max values VScale_Range.
virtual std::optional<std::pair<unsigned, unsigned>>
Expand Down
Loading
Loading