Skip to content

Commit 94da6bf

Browse files
authored
[DirectX] Simplify DXIL_OP_INTRINSIC_MAP tablegen'ed code
Rather than generate a map with a known name, just define the mapping so the code that uses it can do what it needs. Pull Request: #101248
1 parent ee1040b commit 94da6bf

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

llvm/lib/Target/DirectX/DXILConstants.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace dxil {
1717

1818
#define DXIL_OP_ENUM
1919
#include "DXILOperation.inc"
20-
#undef DXIL_OP_ENUM
2120

2221
} // namespace dxil
2322
} // namespace llvm

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,19 @@ static void lowerIntrinsic(dxil::OpCode DXILOp, Function &F, Module &M) {
104104
static bool lowerIntrinsics(Module &M) {
105105
bool Updated = false;
106106

107-
#define DXIL_OP_INTRINSIC_MAP
108-
#include "DXILOperation.inc"
109-
#undef DXIL_OP_INTRINSIC_MAP
110-
111107
for (Function &F : make_early_inc_range(M.functions())) {
112108
if (!F.isDeclaration())
113109
continue;
114110
Intrinsic::ID ID = F.getIntrinsicID();
115-
if (ID == Intrinsic::not_intrinsic)
111+
switch (ID) {
112+
default:
116113
continue;
117-
auto LowerIt = LowerMap.find(ID);
118-
if (LowerIt == LowerMap.end())
119-
continue;
120-
lowerIntrinsic(LowerIt->second, F, M);
114+
#define DXIL_OP_INTRINSIC(OpCode, Intrin) \
115+
case Intrin: \
116+
lowerIntrinsic(OpCode, F, M); \
117+
break;
118+
#include "DXILOperation.inc"
119+
}
121120
Updated = true;
122121
}
123122
return Updated;

llvm/utils/TableGen/DXILEmitter.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,7 @@ static std::string getAttributeMaskString(const SmallVector<Record *> Recs) {
434434
/// \param Output stream
435435
static void emitDXILEnums(std::vector<DXILOperationDesc> &Ops,
436436
raw_ostream &OS) {
437-
// Sort by OpCode
438-
llvm::sort(Ops, [](DXILOperationDesc &A, DXILOperationDesc &B) {
439-
return A.OpCode < B.OpCode;
440-
});
441-
442-
OS << "#ifdef DXIL_OP_ENUM\n";
437+
OS << "#ifdef DXIL_OP_ENUM\n\n";
443438
OS << "// Enumeration for operations specified by DXIL\n";
444439
OS << "enum class OpCode : unsigned {\n";
445440

@@ -461,40 +456,33 @@ static void emitDXILEnums(std::vector<DXILOperationDesc> &Ops,
461456
OS << C << ",\n";
462457
}
463458
OS << "\n};\n\n";
464-
OS << "#endif // DXIL_OP_ENUM\n";
459+
OS << "#undef DXIL_OP_ENUM\n";
460+
OS << "#endif\n\n";
465461
}
466462

467463
/// Emit map of DXIL operation to LLVM or DirectX intrinsic
468464
/// \param A vector of DXIL Ops
469465
/// \param Output stream
470466
static void emitDXILIntrinsicMap(std::vector<DXILOperationDesc> &Ops,
471467
raw_ostream &OS) {
472-
OS << "\n#ifdef DXIL_OP_INTRINSIC_MAP\n";
473-
474-
// FIXME: use array instead of SmallDenseMap.
475-
OS << "static const SmallDenseMap<Intrinsic::ID, dxil::OpCode> LowerMap = "
476-
"{\n";
477-
for (auto &Op : Ops) {
468+
OS << "#ifdef DXIL_OP_INTRINSIC\n";
469+
OS << "\n";
470+
for (const auto &Op : Ops) {
478471
if (Op.Intrinsic.empty())
479472
continue;
480-
// {Intrinsic::sin, dxil::OpCode::Sin},
481-
OS << " { Intrinsic::" << Op.Intrinsic << ", dxil::OpCode::" << Op.OpName
482-
<< "},\n";
473+
OS << "DXIL_OP_INTRINSIC(dxil::OpCode::" << Op.OpName
474+
<< ", Intrinsic::" << Op.Intrinsic << ")\n";
483475
}
484-
OS << "};\n\n";
485-
OS << "#endif // DXIL_OP_INTRINSIC_MAP\n";
476+
OS << "\n";
477+
OS << "#undef DXIL_OP_INTRINSIC\n";
478+
OS << "#endif\n\n";
486479
}
487480

488481
/// Emit DXIL operation table
489482
/// \param A vector of DXIL Ops
490483
/// \param Output stream
491484
static void emitDXILOperationTable(std::vector<DXILOperationDesc> &Ops,
492485
raw_ostream &OS) {
493-
// Sort by OpCode.
494-
llvm::sort(Ops, [](DXILOperationDesc &A, DXILOperationDesc &B) {
495-
return A.OpCode < B.OpCode;
496-
});
497-
498486
// Collect Names.
499487
SequenceToOffsetTable<std::string> OpClassStrings;
500488
SequenceToOffsetTable<std::string> OpStrings;
@@ -601,7 +589,7 @@ static void emitDXILOperationTable(std::vector<DXILOperationDesc> &Ops,
601589
OS << " };\n\n";
602590
OS << " unsigned Index = Prop.ParameterTableOffset;\n";
603591
OS << " return DXILOpParameterKindTable + Index;\n";
604-
OS << "}\n ";
592+
OS << "}\n\n";
605593
}
606594

607595
static void emitDXILOperationTableDataStructs(RecordKeeper &Records,
@@ -653,12 +641,24 @@ static void EmitDXILOperation(RecordKeeper &Records, raw_ostream &OS) {
653641
for (auto *Record : OpIntrProps) {
654642
DXILOps.emplace_back(DXILOperationDesc(Record));
655643
}
644+
// Sort by opcode.
645+
llvm::sort(DXILOps, [](DXILOperationDesc &A, DXILOperationDesc &B) {
646+
return A.OpCode < B.OpCode;
647+
});
648+
int PrevOp = -1;
649+
for (DXILOperationDesc &Desc : DXILOps) {
650+
if (Desc.OpCode == PrevOp)
651+
PrintFatalError(Twine("Duplicate opcode: ") + Twine(Desc.OpCode));
652+
PrevOp = Desc.OpCode;
653+
}
654+
656655
emitDXILEnums(DXILOps, OS);
657656
emitDXILIntrinsicMap(DXILOps, OS);
658-
OS << "#ifdef DXIL_OP_OPERATION_TABLE\n";
657+
OS << "#ifdef DXIL_OP_OPERATION_TABLE\n\n";
659658
emitDXILOperationTableDataStructs(Records, OS);
660659
emitDXILOperationTable(DXILOps, OS);
661-
OS << "#endif // DXIL_OP_OPERATION_TABLE\n";
660+
OS << "#undef DXIL_OP_OPERATION_TABLE\n";
661+
OS << "#endif\n\n";
662662
}
663663

664664
static TableGen::Emitter::Opt X("gen-dxil-operation", EmitDXILOperation,

0 commit comments

Comments
 (0)