Skip to content

Commit 66989ec

Browse files
committed
review comments: remove attribute string generation
- convert to using a struct to store the active attributes to let us use a table of bools instead of a string list of attributes
1 parent 007fc09 commit 66989ec

File tree

3 files changed

+99
-62
lines changed

3 files changed

+99
-62
lines changed

llvm/lib/Target/DirectX/DXILConstants.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,24 @@ enum class OpParamType : unsigned {
3030
#include "DXILOperation.inc"
3131
};
3232

33-
enum class Attribute : unsigned {
34-
#define DXIL_ATTRIBUTE(Name) Name,
33+
struct Attributes {
34+
#define DXIL_ATTRIBUTE(Name) bool Name = false;
3535
#include "DXILOperation.inc"
3636
};
3737

38+
inline Attributes operator|(Attributes a, Attributes b) {
39+
Attributes c;
40+
#define DXIL_ATTRIBUTE(Name) c.Name = a.Name | b.Name;
41+
#include "DXILOperation.inc"
42+
return c;
43+
}
44+
45+
inline Attributes &operator|=(Attributes &a, Attributes &b) {
46+
a = a | b;
47+
return a;
48+
}
49+
50+
3851
enum class Property : unsigned {
3952
#define DXIL_PROPERTY(Name) Name,
4053
#include "DXILOperation.inc"

llvm/lib/Target/DirectX/DXILOpBuilder.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ struct OpStage {
5252
uint32_t ValidStages;
5353
};
5454

55-
struct OpAttribute {
56-
Version DXILVersion;
57-
llvm::SmallVector<dxil::Attribute> ValidAttrs;
58-
};
59-
6055
static const char *getOverloadTypeName(OverloadKind Kind) {
6156
switch (Kind) {
6257
case OverloadKind::HALF:
@@ -158,7 +153,6 @@ struct OpCodeProperty {
158153
unsigned OpCodeClassNameOffset;
159154
llvm::SmallVector<OpOverload> Overloads;
160155
llvm::SmallVector<OpStage> Stages;
161-
llvm::SmallVector<OpAttribute> Attributes;
162156
llvm::SmallVector<dxil::Property> Properties;
163157
int OverloadParamIndex; // parameter index which control the overload.
164158
// When < 0, should be only 1 overload type.
@@ -368,18 +362,40 @@ static std::optional<size_t> getPropIndex(ArrayRef<T> PropList,
368362
return std::nullopt;
369363
}
370364

371-
static void setDXILAttribute(CallInst *CI, dxil::Attribute Attr) {
372-
switch (Attr) {
373-
case dxil::Attribute::ReadNone:
374-
return CI->setDoesNotAccessMemory();
375-
case dxil::Attribute::ReadOnly:
376-
return CI->setOnlyReadsMemory();
377-
case dxil::Attribute::NoReturn:
378-
return CI->setDoesNotReturn();
379-
case dxil::Attribute::NoDuplicate:
380-
return CI->setCannotDuplicate();
365+
constexpr static uint64_t computeSwitchEnum(dxil::OpCode OpCode, uint16_t VersionMajor, uint16_t VersionMinor) {
366+
uint64_t OpCodePack = (uint64_t)OpCode;
367+
return (OpCodePack << 32) | (VersionMajor << 16) | VersionMinor;
368+
}
369+
370+
static dxil::Attributes getDXILAttributes(dxil::OpCode OpCode, VersionTuple DXILVersion) {
371+
SmallVector<Version> Versions = {
372+
#define DXIL_VERSION(MAJOR, MINOR) {MAJOR, MINOR},
373+
#include "DXILOperation.inc"
374+
};
375+
376+
dxil::Attributes Attributes;
377+
for (auto Version : Versions) {
378+
if (DXILVersion < VersionTuple(Version.Major, Version.Minor)) continue;
379+
switch (computeSwitchEnum(OpCode, Version.Major, Version.Minor)) {
380+
#define DXIL_OP_ATTRIBUTES(OpCode, VersionMajor, VersionMinor, ...) \
381+
case computeSwitchEnum(OpCode, VersionMajor, VersionMinor): { \
382+
auto Other = dxil::Attributes{__VA_ARGS__}; \
383+
Attributes |= Other; \
384+
break; \
385+
};
386+
#include "DXILOperation.inc"
387+
}
381388
}
382-
llvm_unreachable("Invalid function attribute specified for DXIL operation");
389+
return Attributes;
390+
}
391+
392+
static void setDXILAttributes(CallInst *CI, dxil::OpCode OpCode, VersionTuple DXILVersion) {
393+
dxil::Attributes Attributes = getDXILAttributes(OpCode, DXILVersion);
394+
if (Attributes.ReadNone) CI->setDoesNotAccessMemory();
395+
if (Attributes.ReadOnly) CI->setOnlyReadsMemory();
396+
if (Attributes.NoReturn) CI->setDoesNotReturn();
397+
if (Attributes.NoDuplicate) CI->setCannotDuplicate();
398+
return;
383399
}
384400

385401
namespace llvm {
@@ -480,11 +496,7 @@ Expected<CallInst *> DXILOpBuilder::tryCreateOp(dxil::OpCode OpCode,
480496
CallInst *CI = IRB.CreateCall(DXILFn, OpArgs, Name);
481497

482498
// We then need to attach available function attributes
483-
for (auto OpAttr : Prop->Attributes)
484-
if (VersionTuple(OpAttr.DXILVersion.Major, OpAttr.DXILVersion.Minor) <=
485-
DXILVersion)
486-
for (auto Attr : OpAttr.ValidAttrs)
487-
setDXILAttribute(CI, Attr);
499+
setDXILAttributes(CI, OpCode, DXILVersion);
488500

489501
return CI;
490502
}

llvm/utils/TableGen/DXILEmitter.cpp

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -319,43 +319,6 @@ static std::string getStageMaskString(ArrayRef<const Record *> Recs) {
319319
return MaskString;
320320
}
321321

322-
/// Return a string representation of valid attribute information denoted
323-
// by input records
324-
//
325-
/// \param Recs A vector of records of TableGen Attribute records
326-
/// \return std::string string representation of attributes list string
327-
/// predicated by DXIL Version. E.g.,
328-
// {{{1, 0}, {Attr1, ...}}, {{1, 2}, {Attr2, ...}}, ...}
329-
static std::string getAttributeListString(ArrayRef<const Record *> Recs) {
330-
std::string ListString = "";
331-
std::string Prefix = "";
332-
ListString.append("{");
333-
334-
for (const auto *Rec : Recs) {
335-
unsigned Major = Rec->getValueAsDef("dxil_version")->getValueAsInt("Major");
336-
unsigned Minor = Rec->getValueAsDef("dxil_version")->getValueAsInt("Minor");
337-
ListString.append(Prefix)
338-
.append("{{")
339-
.append(std::to_string(Major))
340-
.append(", ")
341-
.append(std::to_string(Minor).append("}, {"));
342-
343-
std::string CommaPrefix = "";
344-
auto Attrs = Rec->getValueAsListOfDefs("fn_attrs");
345-
for (const auto *Attr : Attrs) {
346-
ListString.append(CommaPrefix)
347-
.append("dxil::Attribute::")
348-
.append(Attr->getName());
349-
CommaPrefix = ", ";
350-
}
351-
ListString.append("}"); // End of Attrs
352-
ListString.append("}"); // End of Rec
353-
Prefix = ", ";
354-
}
355-
ListString.append("}"); // End of List
356-
return ListString;
357-
}
358-
359322
/// Return a string representation of valid property information denoted
360323
// by input records
361324
//
@@ -378,6 +341,20 @@ static std::string getPropertyListString(ArrayRef<const Record *> Recs) {
378341
return ListString;
379342
}
380343

344+
/// Emit a list valid DXIL Version records
345+
static void emitDXILVersions(const RecordKeeper &Records, raw_ostream &OS) {
346+
OS << "#ifdef DXIL_VERSION\n";
347+
for (const Record *Version : Records.getAllDerivedDefinitions("Version")) {
348+
unsigned Major = Version->getValueAsInt("Major");
349+
unsigned Minor = Version->getValueAsInt("Minor");
350+
OS << "DXIL_VERSION(";
351+
OS << std::to_string(Major) << ", " << std::to_string(Minor);
352+
OS << ")\n";
353+
}
354+
OS << "#undef DXIL_VERSION\n";
355+
OS << "#endif\n\n";
356+
}
357+
381358
/// Emit a mapping of DXIL opcode to opname
382359
static void emitDXILOpCodes(ArrayRef<DXILOperationDesc> Ops, raw_ostream &OS) {
383360
OS << "#ifdef DXIL_OPCODE\n";
@@ -416,6 +393,40 @@ static void emitDXILAttributes(const RecordKeeper &Records, raw_ostream &OS) {
416393
OS << "#endif\n\n";
417394
}
418395

396+
// Determine which function attributes are set for a dxil version
397+
static bool attrIsDefined(std::vector<const Record *> Attrs, const Record *Attr) {
398+
for (auto CurAttr : Attrs)
399+
if (CurAttr->getName() == Attr->getName())
400+
return true;
401+
return false;
402+
}
403+
404+
/// Emit a table of bools denoting a DXIL op's function attributes
405+
static void emitDXILOpAttributes(const RecordKeeper &Records,
406+
ArrayRef<DXILOperationDesc> Ops,
407+
raw_ostream &OS) {
408+
OS << "#ifdef DXIL_OP_ATTRIBUTES\n";
409+
for (const auto &Op : Ops) {
410+
for (const auto *Rec : Op.AttrRecs) {
411+
unsigned Major = Rec->getValueAsDef("dxil_version")->getValueAsInt("Major");
412+
unsigned Minor = Rec->getValueAsDef("dxil_version")->getValueAsInt("Minor");
413+
OS << "DXIL_OP_ATTRIBUTES(dxil::OpCode::" << Op.OpName << ", ";
414+
OS << std::to_string(Major) << ", " << std::to_string(Minor);
415+
auto Attrs = Rec->getValueAsListOfDefs("fn_attrs");
416+
for (const Record *Attr : Records.getAllDerivedDefinitions("DXILAttribute")) {
417+
std::string HasAttr = ", false";
418+
if (attrIsDefined(Attrs, Attr))
419+
HasAttr = ", true";
420+
OS << HasAttr;
421+
}
422+
OS << ")\n";
423+
}
424+
425+
}
426+
OS << "#undef DXIL_OP_ATTRIBUTES\n";
427+
OS << "#endif\n\n";
428+
}
429+
419430
/// Emit a list of DXIL op properties and their query functions
420431
static void emitDXILProperties(const RecordKeeper &Records, raw_ostream &OS) {
421432
// Generate their definitions
@@ -544,7 +555,6 @@ static void emitDXILOperationTable(ArrayRef<DXILOperationDesc> Ops,
544555
<< OpClassStrings.get(Op.OpClass.data()) << ", "
545556
<< getOverloadMaskString(Op.OverloadRecs) << ", "
546557
<< getStageMaskString(Op.StageRecs) << ", "
547-
<< getAttributeListString(Op.AttrRecs) << ", "
548558
<< getPropertyListString(Op.PropRecs) << ", " << Op.OverloadParamIndex
549559
<< " }";
550560
Prefix = ",\n";
@@ -647,10 +657,12 @@ static void emitDxilOperation(const RecordKeeper &Records, raw_ostream &OS) {
647657
PrevOp = Desc.OpCode;
648658
}
649659

660+
emitDXILVersions(Records, OS);
650661
emitDXILOpCodes(DXILOps, OS);
651662
emitDXILOpClasses(Records, OS);
652663
emitDXILOpParamTypes(Records, OS);
653664
emitDXILAttributes(Records, OS);
665+
emitDXILOpAttributes(Records, DXILOps, OS);
654666
emitDXILProperties(Records, OS);
655667
emitDXILOpFunctionTypes(DXILOps, OS);
656668
emitDXILIntrinsicArgSelectTypes(Records, OS);

0 commit comments

Comments
 (0)