Skip to content

Commit

Permalink
define DXILProperty for queryable attributes
Browse files Browse the repository at this point in the history
- define these properties in DXIL.td and DXILConstants.h
- emit DXIL definitions as enumerations
- emit some helper functions to query OpCodeProp for each property class
  • Loading branch information
inbelic committed Nov 20, 2024
1 parent 9a27517 commit 084cdd0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions llvm/lib/Target/DirectX/DXIL.td
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ def ReadOnly : DXILAttribute;
def NoDuplicate : DXILAttribute;
def NoReturn : DXILAttribute;

// A property is simply used to mark a DXIL op belongs to a sub-group of
// DXIL ops, and it is used to query if a particular holds this property.
// This is used for static analysis of DXIL ops.
class DXILProperty;

def IsBarrier : DXILProperty;
def IsDerivative : DXILProperty;
def IsGradient : DXILProperty;
def IsFeedback : DXILProperty;
def IsWave : DXILProperty;
def RequiresUniformInputs : DXILProperty;

class Overloads<Version ver, list<DXILOpParamType> ols> {
Version dxil_version = ver;
list<DXILOpParamType> overload_types = ols;
Expand Down Expand Up @@ -322,6 +334,9 @@ class DXILOp<int opcode, DXILOpClass opclass> {

// Versioned attributes of operation
list<Attributes> attributes = [];

// List of properties. Default to no properties.
list<DXILProperty> properties = [];
}

// Concrete definitions of DXIL Operations
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/DirectX/DXILConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ enum class Attribute : unsigned {
#include "DXILOperation.inc"
};

enum class Property : unsigned {
#define DXIL_PROPERTY(Name) Name,
#include "DXILOperation.inc"
};

} // namespace dxil
} // namespace llvm

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/DirectX/DXILOpBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct OpCodeProperty {
llvm::SmallVector<OpOverload> Overloads;
llvm::SmallVector<OpStage> Stages;
llvm::SmallVector<OpAttribute> Attributes;
llvm::SmallVector<dxil::Property> Properties;
int OverloadParamIndex; // parameter index which control the overload.
// When < 0, should be only 1 overload type.
};
Expand Down
59 changes: 58 additions & 1 deletion llvm/utils/TableGen/DXILEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct DXILOperationDesc {
SmallVector<const Record *> OverloadRecs;
SmallVector<const Record *> StageRecs;
SmallVector<const Record *> AttrRecs;
SmallVector<const Record *> PropRecs;
StringRef Intrinsic; // The llvm intrinsic map to OpName. Default is "" which
// means no map exists
SmallVector<StringRef, 4>
Expand Down Expand Up @@ -149,6 +150,13 @@ DXILOperationDesc::DXILOperationDesc(const Record *R) {
AttrRecs.push_back(CR);
}

Recs = R->getValueAsListOfDefs("properties");

// Get property records
for (const Record *CR : Recs) {
PropRecs.push_back(CR);
}

// Get the operation class
OpClass = R->getValueAsDef("OpClass")->getName();

Expand Down Expand Up @@ -318,6 +326,28 @@ static std::string getAttributeListString(ArrayRef<const Record *> Recs) {
return ListString;
}

/// Return a string representation of valid property information denoted
// by input records
//
/// \param Recs A vector of records of TableGen Property records
/// \return std::string string representation of properties list string
// {Attr1, Attr2, ...}
static std::string getPropertyListString(ArrayRef<const Record *> Recs) {
std::string ListString = "";
std::string Prefix = "";
ListString.append("{");

std::string CommaPrefix = "";
for (const auto *Rec : Recs) {
ListString.append(CommaPrefix)
.append("dxil::Property::")
.append(Rec->getName());
CommaPrefix = ", ";
}
ListString.append("}");
return ListString;
}

/// Emit a mapping of DXIL opcode to opname
static void emitDXILOpCodes(ArrayRef<DXILOperationDesc> Ops, raw_ostream &OS) {
OS << "#ifdef DXIL_OPCODE\n";
Expand Down Expand Up @@ -356,6 +386,30 @@ static void emitDXILAttributes(const RecordKeeper &Records, raw_ostream &OS) {
OS << "#endif\n\n";
}

/// Emit a list of DXIL op properties and their query functions
static void emitDXILProperties(const RecordKeeper &Records, raw_ostream &OS) {
// Generate their definitions
OS << "#ifdef DXIL_PROPERTY\n";
for (const Record *Prop: Records.getAllDerivedDefinitions("DXILProperty"))
OS << "DXIL_PROPERTY(" << Prop->getName() << ")\n";
OS << "#undef DXIL_PROPERTY\n";
OS << "#endif\n\n";
}

static void emitDXILPropertyHelpers(const RecordKeeper &Records, raw_ostream &OS) {
// Generate their helper functions
for (const Record *Prop: Records.getAllDerivedDefinitions("DXILProperty")) {
OS << "[[maybe_unused]]\n";
OS << "static bool has" << Prop->getName() << "(dxil::OpCode Op) {\n";
OS << " auto *OpCodeProp = getOpCodeProperty(Op);\n";
OS << " for (auto Prop : OpCodeProp->Properties)\n";
OS << " if (Prop == dxil::Property::" << Prop->getName() << ")\n";
OS << " return true;\n";
OS << " return false;\n";
OS << "}\n\n";
}
}

/// Emit a list of DXIL op function types
static void emitDXILOpFunctionTypes(ArrayRef<DXILOperationDesc> Ops,
raw_ostream &OS) {
Expand Down Expand Up @@ -426,7 +480,8 @@ static void emitDXILOperationTable(ArrayRef<DXILOperationDesc> Ops,
<< OpClassStrings.get(Op.OpClass.data()) << ", "
<< getOverloadMaskString(Op.OverloadRecs) << ", "
<< getStageMaskString(Op.StageRecs) << ", "
<< getAttributeListString(Op.AttrRecs) << ", " << Op.OverloadParamIndex
<< getAttributeListString(Op.AttrRecs) << ", "
<< getPropertyListString(Op.PropRecs) << ", " << Op.OverloadParamIndex
<< " }";
Prefix = ",\n";
}
Expand Down Expand Up @@ -532,11 +587,13 @@ static void emitDxilOperation(const RecordKeeper &Records, raw_ostream &OS) {
emitDXILOpClasses(Records, OS);
emitDXILOpParamTypes(Records, OS);
emitDXILAttributes(Records, OS);
emitDXILProperties(Records, OS);
emitDXILOpFunctionTypes(DXILOps, OS);
emitDXILIntrinsicMap(DXILOps, OS);
OS << "#ifdef DXIL_OP_OPERATION_TABLE\n\n";
emitDXILOperationTableDataStructs(Records, OS);
emitDXILOperationTable(DXILOps, OS);
emitDXILPropertyHelpers(Records, OS);
OS << "#undef DXIL_OP_OPERATION_TABLE\n";
OS << "#endif\n\n";
}
Expand Down

0 comments on commit 084cdd0

Please sign in to comment.