[NFC][TableGen] Adopt CodeGenHelpers in IntrinsicEmitter (#179310)

- Adopt IfDefEmitter in IntrinsicEmitter.
- Remove #undef for various flags in Intrinsics.cpp/Intrinsics.h as the
TableGen generated code does that now.
This commit is contained in:
Rahul Joshi 2026-02-03 10:56:00 -08:00 committed by GitHub
parent 90f575ba81
commit f0c519d2c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 74 deletions

View File

@ -50,7 +50,6 @@ namespace Intrinsic {
// Get the intrinsic enums generated from Intrinsics.td
#define GET_INTRINSIC_ENUM_VALUES
#include "llvm/IR/IntrinsicEnums.inc"
#undef GET_INTRINSIC_ENUM_VALUES
};
/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
@ -196,7 +195,6 @@ namespace Intrinsic {
enum ArgKind {
#define GET_INTRINSIC_ARGKIND
#include "llvm/IR/IntrinsicEnums.inc"
#undef GET_INTRINSIC_ARGKIND
};
unsigned getArgumentNumber() const {

View File

@ -40,7 +40,6 @@ using namespace llvm;
/// Table of string intrinsic names indexed by enum value.
#define GET_INTRINSIC_NAME_TABLE
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_NAME_TABLE
StringRef Intrinsic::getBaseName(ID id) {
assert(id < num_intrinsics && "Invalid intrinsic ID!");
@ -196,7 +195,6 @@ std::string Intrinsic::getNameNoUnnamedTypes(ID Id, ArrayRef<Type *> Tys) {
enum IIT_Info {
#define GET_INTRINSIC_IITINFO
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_IITINFO
};
static void
@ -434,7 +432,6 @@ DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
#define GET_INTRINSIC_GENERATOR_GLOBAL
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_GENERATOR_GLOBAL
void Intrinsic::getIntrinsicInfoTableEntries(
ID id, SmallVectorImpl<IITDescriptor> &T) {
@ -609,19 +606,16 @@ FunctionType *Intrinsic::getType(LLVMContext &Context, ID id,
bool Intrinsic::isOverloaded(ID id) {
#define GET_INTRINSIC_OVERLOAD_TABLE
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_OVERLOAD_TABLE
}
bool Intrinsic::hasPrettyPrintedArgs(ID id){
#define GET_INTRINSIC_PRETTY_PRINT_TABLE
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_PRETTY_PRINT_TABLE
}
/// Table of per-target intrinsic name tables.
#define GET_INTRINSIC_TARGET_DATA
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_TARGET_DATA
bool Intrinsic::isTargetIntrinsic(Intrinsic::ID IID) {
return IID > TargetInfos[0].Count;
@ -735,7 +729,6 @@ Intrinsic::ID Intrinsic::lookupIntrinsicID(StringRef Name) {
/// This defines the "Intrinsic::getAttributes(ID id)" method.
#define GET_INTRINSIC_ATTRIBUTES
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_ATTRIBUTES
static Function *getOrInsertIntrinsicDeclarationImpl(Module *M,
Intrinsic::ID id,
@ -812,12 +805,10 @@ Function *Intrinsic::getDeclarationIfExists(Module *M, ID id,
// This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
#define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
bool Intrinsic::isConstrainedFPIntrinsic(ID QID) {
switch (QID) {
@ -1204,4 +1195,3 @@ Intrinsic::ID Intrinsic::getDeinterleaveIntrinsicID(unsigned Factor) {
#define GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
#include "llvm/IR/IntrinsicImpl.inc"
#undef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS

View File

@ -30,15 +30,16 @@ def int_my_fadd_f32 : DefaultAttrsIntrinsic<
ArgInfo<ArgIndex<2>, [ArgName<"rounding_mode">, ImmArgPrinter<"printRoundingMode">]>,
ArgInfo<ArgIndex<3>, [ArgName<"saturation_mode">]>]>;
// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK-NEXT: static constexpr uint8_t PPTable[] = {
// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK-NEXT: #undef GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK: static constexpr uint8_t PPTable[] = {
// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_TABLE
// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
// CHECK-NEXT: #undef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
// CHECK: void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
// CHECK: #ifdef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
// CHECK: void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
// CHECK: case dummy_foo_bar:
// CHECK: case dummy_foo_bar:
// CHECK-NEXT: switch (ArgIdx) {
// CHECK-NEXT: case 1:
@ -53,7 +54,7 @@ def int_my_fadd_f32 : DefaultAttrsIntrinsic<
// CHECK-NEXT: }
// CHECK-NEXT: break;
// CHECK: case my_fadd_f32:
// CHECK: case my_fadd_f32:
// CHECK-NEXT: switch (ArgIdx) {
// CHECK-NEXT: case 2:
@ -68,4 +69,4 @@ def int_my_fadd_f32 : DefaultAttrsIntrinsic<
// CHECK-NEXT: }
// CHECK-NEXT: break;
// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
// CHECK: #endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS

View File

@ -22,6 +22,7 @@
#include "llvm/Support/ModRef.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/CodeGenHelpers.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/StringToOffsetTable.h"
@ -157,13 +158,17 @@ void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
}
// Generate a complete header for target specific intrinsics.
std::optional<IfDefEmitter> IfDef;
std::optional<IncludeGuardEmitter> IncGuard;
std::optional<NamespaceEmitter> NS;
if (IntrinsicPrefix.empty()) {
OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n";
IfDef.emplace(OS, "GET_INTRINSIC_ENUM_VALUES");
} else {
std::string UpperPrefix = StringRef(IntrinsicPrefix).upper();
OS << formatv("#ifndef LLVM_IR_INTRINSIC_{}_ENUMS_H\n", UpperPrefix);
OS << formatv("#define LLVM_IR_INTRINSIC_{}_ENUMS_H\n", UpperPrefix);
OS << "namespace llvm::Intrinsic {\n";
IncGuard.emplace(
OS, formatv("LLVM_IR_INTRINSIC_{}_ENUMS_H", UpperPrefix).str());
NS.emplace(OS, "llvm::Intrinsic");
OS << formatv("enum {}Intrinsics : unsigned {{\n", UpperPrefix);
}
@ -188,34 +193,27 @@ void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints,
}
// Emit num_intrinsics into the target neutral enum.
if (IntrinsicPrefix.empty()) {
if (IntrinsicPrefix.empty())
OS << formatv(" num_intrinsics = {}\n", Ints.size() + 1);
OS << "#endif\n\n";
} else {
OS << R"(}; // enum
} // namespace llvm::Intrinsic
#endif
)";
}
else
OS << "}; // enum\n";
}
void IntrinsicEmitter::EmitArgKind(raw_ostream &OS) {
if (!IntrinsicPrefix.empty())
return;
IfDefEmitter IfDef(OS, "GET_INTRINSIC_ARGKIND");
OS << "// llvm::Intrinsic::IITDescriptor::ArgKind.\n";
OS << "#ifdef GET_INTRINSIC_ARGKIND\n";
if (const auto RecArgKind = Records.getDef("ArgKind")) {
for (const auto &RV : RecArgKind->getValues())
OS << " AK_" << RV.getName() << " = " << *RV.getValue() << ",\n";
} else {
OS << "#error \"ArgKind is not defined\"\n";
}
OS << "#endif\n\n";
}
void IntrinsicEmitter::EmitIITInfo(raw_ostream &OS) {
OS << "#ifdef GET_INTRINSIC_IITINFO\n";
IfDefEmitter IfDef(OS, "GET_INTRINSIC_IITINFO");
std::array<StringRef, 256> RecsByNumber;
auto IIT_Base = Records.getAllDerivedDefinitionsIfDefined("IIT_Base");
for (const Record *Rec : IIT_Base) {
@ -232,13 +230,12 @@ void IntrinsicEmitter::EmitIITInfo(raw_ostream &OS) {
} else {
OS << "#error \"class IIT_Base is not defined\"\n";
}
OS << "#endif\n\n";
}
void IntrinsicEmitter::EmitTargetInfo(const CodeGenIntrinsicTable &Ints,
raw_ostream &OS) {
IfDefEmitter IfDef(OS, "GET_INTRINSIC_TARGET_DATA");
OS << R"(// Target mapping.
#ifdef GET_INTRINSIC_TARGET_DATA
struct IntrinsicTargetInfo {
StringLiteral Name;
size_t Offset;
@ -248,10 +245,7 @@ static constexpr IntrinsicTargetInfo TargetInfos[] = {
)";
for (const auto [Name, Offset, Count] : Ints.getTargets())
OS << formatv(" {{\"{}\", {}, {}},\n", Name, Offset, Count);
OS << R"(};
#endif
)";
OS << "};\n";
}
/// Helper function to emit a bit table for intrinsic properties.
@ -260,8 +254,8 @@ void IntrinsicEmitter::EmitIntrinsicBitTable(
const CodeGenIntrinsicTable &Ints, raw_ostream &OS, StringRef Guard,
StringRef TableName, StringRef Comment,
function_ref<bool(const CodeGenIntrinsic &Int)> GetProperty) {
IfDefEmitter IfDef(OS, Guard);
OS << formatv("// {}\n", Comment);
OS << formatv("#ifdef {}\n", Guard);
OS << formatv("static constexpr uint8_t {}[] = {{\n", TableName);
OS << " 0\n ";
for (auto [I, Int] : enumerate(Ints)) {
@ -274,7 +268,6 @@ void IntrinsicEmitter::EmitIntrinsicBitTable(
}
OS << "\n};\n\n";
OS << formatv("return ({}[id/8] & (1 << (id%8))) != 0;\n", TableName);
OS << formatv("#endif // {}\n\n", Guard);
}
void IntrinsicEmitter::EmitIntrinsicToNameTable(
@ -286,8 +279,8 @@ void IntrinsicEmitter::EmitIntrinsicToNameTable(
for (const auto &Int : Ints)
Table.GetOrAddStringOffset(Int.Name);
IfDefEmitter IfDef(OS, "GET_INTRINSIC_NAME_TABLE");
OS << R"(// Intrinsic ID to name table.
#ifdef GET_INTRINSIC_NAME_TABLE
// Note that entry #0 is the invalid intrinsic!
)";
@ -303,12 +296,7 @@ static constexpr unsigned IntrinsicNameOffsetTable[] = {
for (const auto &Int : Ints)
OS << formatv(" {}, // {}\n", Table.GetStringOffset(Int.Name), Int.Name);
OS << R"(
}; // IntrinsicNameOffsetTable
#endif
)";
OS << "\n}; // IntrinsicNameOffsetTable\n";
}
void IntrinsicEmitter::EmitIntrinsicToOverloadTable(
@ -387,8 +375,8 @@ void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
LongEncodingTable.layout();
IfDefEmitter IfDef(OS, "GET_INTRINSIC_GENERATOR_GLOBAL");
OS << formatv(R"(// Global intrinsic function declaration type table.
#ifdef GET_INTRINSIC_GENERATOR_GLOBAL
using FixedEncodingTy = {};
static constexpr FixedEncodingTy IIT_Table[] = {{
)",
@ -426,7 +414,6 @@ static constexpr FixedEncodingTy IIT_Table[] = {{
LongEncodingTable.emit(
OS, [](raw_ostream &OS, unsigned char C) { OS << (unsigned)C; });
OS << " 255\n};\n";
OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
}
/// Returns the effective MemoryEffects for intrinsic \p Int.
@ -525,8 +512,8 @@ static StringRef getArgAttrEnumName(CodeGenIntrinsic::ArgAttrKind Kind) {
/// EmitAttributes - This emits the Intrinsic::getAttributes method.
void IntrinsicEmitter::EmitAttributes(const CodeGenIntrinsicTable &Ints,
raw_ostream &OS) {
IfDefEmitter IfDef(OS, "GET_INTRINSIC_ATTRIBUTES");
OS << R"(// Add parameter attributes that are not common to all intrinsics.
#ifdef GET_INTRINSIC_ATTRIBUTES
static AttributeSet getIntrinsicArgAttributeSet(LLVMContext &C, unsigned ID,
Type *ArgType) {
unsigned BitWidth = ArgType->getScalarSizeInBits();
@ -826,8 +813,6 @@ AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {{
return AttributeSet();
return getIntrinsicFnAttributeSet(C, FnAttrID);
}
#endif // GET_INTRINSIC_ATTRIBUTES
)",
UniqAttributesBitSize, MaxNumAttrs, NoFunctionAttrsID,
NoFunctionAttrsID);
@ -844,8 +829,8 @@ void IntrinsicEmitter::EmitIntrinsicToPrettyPrintTable(
void IntrinsicEmitter::EmitPrettyPrintArguments(
const CodeGenIntrinsicTable &Ints, raw_ostream &OS) {
IfDefEmitter IfDef(OS, "GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS");
OS << R"(
#ifdef GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal) {
using namespace Intrinsic;
switch (IID) {
@ -874,9 +859,7 @@ void Intrinsic::printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Cons
OS << R"( default:
break;
}
}
#endif // GET_INTRINSIC_PRETTY_PRINT_ARGUMENTS
)";
})";
}
void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
@ -931,25 +914,22 @@ void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(
}
}
IfDefEmitter IfDef(
OS,
formatv("GET_LLVM_INTRINSIC_FOR_{}_BUILTIN", UpperCompilerName).str());
OS << formatv(R"(
// Get the LLVM intrinsic that corresponds to a builtin. This is used by the
// C front-end. The builtin name is passed in as BuiltinName, and a target
// prefix (e.g. 'ppc') is passed in as TargetPrefix.
#ifdef GET_LLVM_INTRINSIC_FOR_{}_BUILTIN
Intrinsic::ID
Intrinsic::getIntrinsicFor{}Builtin(StringRef TargetPrefix,
StringRef BuiltinName) {{
using namespace Intrinsic;
)",
UpperCompilerName, CompilerName);
CompilerName);
if (BuiltinMap.empty()) {
OS << formatv(R"(
return not_intrinsic;
}
#endif // GET_LLVM_INTRINSIC_FOR_{}_BUILTIN
)",
UpperCompilerName);
OS << "return not_intrinsic;\n";
return;
}
@ -1036,7 +1016,7 @@ Intrinsic::getIntrinsicFor{}Builtin(StringRef TargetPrefix,
}
// If a target independent builtin was not found, lookup the target specific.
OS << formatv(R"(
OS << R"(
auto TI = lower_bound(TargetTable, TargetPrefix);
if (TI == std::end(TargetTable) || TI->TargetPrefix != TargetPrefix)
return not_intrinsic;
@ -1049,10 +1029,7 @@ Intrinsic::getIntrinsicFor{}Builtin(StringRef TargetPrefix,
return not_intrinsic;
return II->IntrinsicID;
}
#endif // GET_LLVM_INTRINSIC_FOR_{}_BUILTIN
)",
UpperCompilerName);
)";
}
static TableGen::Emitter::OptClass<IntrinsicEmitterOpt</*Enums=*/true>>