Skip to content

Commit b715825

Browse files
committed
[DXIL] Define DXILAttribute
- switch to using DXILAttribute to only denote function attributes - attribute enums can't be or'd together as is currently implemented, so we switch to using a list of attributes in DXILOpBuilder.cpp and DXILEmitter.cpp
1 parent af20aff commit b715825

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,18 @@ def miss : DXILShaderStage;
266266
def all_stages : DXILShaderStage;
267267
// Denote support for DXIL Op to have been removed
268268
def removed : DXILShaderStage;
269+
269270
// DXIL Op attributes
270271

272+
// A function attribute denotes that there is a corresponding LLVM function
273+
// attribute that will be set when building the DXIL op. The mapping for
274+
// non-trivial cases is defined by setDXILAttribute in DXILOpBuilder.cpp
271275
class DXILAttribute;
272276

273-
def ReadOnly : DXILAttribute;
274277
def ReadNone : DXILAttribute;
275-
def IsDerivative : DXILAttribute;
276-
def IsGradient : DXILAttribute;
277-
def IsFeedback : DXILAttribute;
278-
def IsWave : DXILAttribute;
279-
def NeedsUniformInputs : DXILAttribute;
280-
def IsBarrier : DXILAttribute;
278+
def ReadOnly : DXILAttribute;
279+
def NoDuplicate : DXILAttribute;
280+
def NoReturn : DXILAttribute;
281281

282282
class Overloads<Version ver, list<DXILOpParamType> ols> {
283283
Version dxil_version = ver;
@@ -291,7 +291,7 @@ class Stages<Version ver, list<DXILShaderStage> st> {
291291

292292
class Attributes<Version ver = DXIL1_0, list<DXILAttribute> attrs> {
293293
Version dxil_version = ver;
294-
list<DXILAttribute> op_attrs = attrs;
294+
list<DXILAttribute> fn_attrs = attrs;
295295
}
296296

297297
defvar BarrierMode_DeviceMemoryBarrier = 2;

llvm/lib/Target/DirectX/DXILConstants.h

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

33+
enum class Attribute : unsigned {
34+
#define DXIL_ATTRIBUTE(Name) Name,
35+
#include "DXILOperation.inc"
36+
};
37+
3338
} // namespace dxil
3439
} // namespace llvm
3540

llvm/lib/Target/DirectX/DXILOpBuilder.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct OpStage {
5454

5555
struct OpAttribute {
5656
Version DXILVersion;
57-
uint32_t ValidAttrs;
57+
llvm::SmallVector<dxil::Attribute> ValidAttrs;
5858
};
5959

6060
static const char *getOverloadTypeName(OverloadKind Kind) {
@@ -367,6 +367,20 @@ static std::optional<size_t> getPropIndex(ArrayRef<T> PropList,
367367
return std::nullopt;
368368
}
369369

370+
static void setDXILAttribute(CallInst *CI, dxil::Attribute Attr) {
371+
switch (Attr) {
372+
case dxil::Attribute::ReadNone:
373+
return CI->setDoesNotAccessMemory();
374+
case dxil::Attribute::ReadOnly:
375+
return CI->setOnlyReadsMemory();
376+
case dxil::Attribute::NoReturn:
377+
return CI->setDoesNotReturn();
378+
case dxil::Attribute::NoDuplicate:
379+
return CI->setCannotDuplicate();
380+
}
381+
llvm_unreachable("Invalid function attribute specified for DXIL operation");
382+
}
383+
370384
namespace llvm {
371385
namespace dxil {
372386

@@ -461,7 +475,17 @@ Expected<CallInst *> DXILOpBuilder::tryCreateOp(dxil::OpCode OpCode,
461475
OpArgs.push_back(IRB.getInt32(llvm::to_underlying(OpCode)));
462476
OpArgs.append(Args.begin(), Args.end());
463477

464-
return IRB.CreateCall(DXILFn, OpArgs, Name);
478+
// Create the function call instruction
479+
CallInst *CI = IRB.CreateCall(DXILFn, OpArgs, Name);
480+
481+
// We then need to attach available function attributes
482+
for (auto OpAttr : Prop->Attributes)
483+
if (VersionTuple(OpAttr.DXILVersion.Major, OpAttr.DXILVersion.Minor) <=
484+
DXILVersion)
485+
for (auto Attr : OpAttr.ValidAttrs)
486+
setDXILAttribute(CI, Attr);
487+
488+
return CI;
465489
}
466490

467491
CallInst *DXILOpBuilder::createOp(dxil::OpCode OpCode, ArrayRef<Value *> Args,

llvm/utils/TableGen/DXILEmitter.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -315,41 +315,37 @@ static std::string getStageMaskString(ArrayRef<const Record *> Recs) {
315315
// by input records
316316
//
317317
/// \param Recs A vector of records of TableGen Attribute records
318-
/// \return std::string string representation of stages mask string
318+
/// \return std::string string representation of attributes list string
319319
/// predicated by DXIL Version. E.g.,
320-
// {{{1, 0}, Mask1}, {{1, 2}, Mask2}, ...}
321-
static std::string getAttributeMaskString(ArrayRef<const Record *> Recs) {
322-
std::string MaskString = "";
320+
// {{{1, 0}, {Attr1, ...}}, {{1, 2}, {Attr2, ...}}, ...}
321+
static std::string getAttributeListString(ArrayRef<const Record *> Recs) {
322+
std::string ListString = "";
323323
std::string Prefix = "";
324-
MaskString.append("{");
324+
ListString.append("{");
325325

326326
for (const auto *Rec : Recs) {
327327
unsigned Major = Rec->getValueAsDef("dxil_version")->getValueAsInt("Major");
328328
unsigned Minor = Rec->getValueAsDef("dxil_version")->getValueAsInt("Minor");
329-
MaskString.append(Prefix)
329+
ListString.append(Prefix)
330330
.append("{{")
331331
.append(std::to_string(Major))
332332
.append(", ")
333-
.append(std::to_string(Minor).append("}, "));
334-
335-
std::string PipePrefix = "";
336-
auto Attrs = Rec->getValueAsListOfDefs("op_attrs");
337-
if (Attrs.empty()) {
338-
MaskString.append("Attribute::None");
339-
} else {
340-
for (const auto *Attr : Attrs) {
341-
MaskString.append(PipePrefix)
342-
.append("Attribute::")
343-
.append(Attr->getName());
344-
PipePrefix = " | ";
345-
}
333+
.append(std::to_string(Minor).append("}, {"));
334+
335+
std::string CommaPrefix = "";
336+
auto Attrs = Rec->getValueAsListOfDefs("fn_attrs");
337+
for (const auto *Attr : Attrs) {
338+
ListString.append(CommaPrefix)
339+
.append("dxil::Attribute::")
340+
.append(Attr->getName());
341+
CommaPrefix = ", ";
346342
}
347-
348-
MaskString.append("}");
343+
ListString.append("}"); // End of Attrs
344+
ListString.append("}"); // End of Rec
349345
Prefix = ", ";
350346
}
351-
MaskString.append("}");
352-
return MaskString;
347+
ListString.append("}"); // End of List
348+
return ListString;
353349
}
354350

355351
/// Emit a mapping of DXIL opcode to opname
@@ -381,6 +377,15 @@ static void emitDXILOpParamTypes(const RecordKeeper &Records, raw_ostream &OS) {
381377
OS << "#endif\n\n";
382378
}
383379

380+
/// Emit a list of DXIL op function attributes
381+
static void emitDXILAttributes(const RecordKeeper &Records, raw_ostream &OS) {
382+
OS << "#ifdef DXIL_ATTRIBUTE\n";
383+
for (const Record *Attr : Records.getAllDerivedDefinitions("DXILAttribute"))
384+
OS << "DXIL_ATTRIBUTE(" << Attr->getName() << ")\n";
385+
OS << "#undef DXIL_ATTRIBUTE\n";
386+
OS << "#endif\n\n";
387+
}
388+
384389
/// Emit a list of DXIL op function types
385390
static void emitDXILOpFunctionTypes(ArrayRef<DXILOperationDesc> Ops,
386391
raw_ostream &OS) {
@@ -477,7 +482,7 @@ static void emitDXILOperationTable(ArrayRef<DXILOperationDesc> Ops,
477482
<< OpClassStrings.get(Op.OpClass.data()) << ", "
478483
<< getOverloadMaskString(Op.OverloadRecs) << ", "
479484
<< getStageMaskString(Op.StageRecs) << ", "
480-
<< getAttributeMaskString(Op.AttrRecs) << ", " << Op.OverloadParamIndex
485+
<< getAttributeListString(Op.AttrRecs) << ", " << Op.OverloadParamIndex
481486
<< " }";
482487
Prefix = ",\n";
483488
}
@@ -582,6 +587,7 @@ static void emitDxilOperation(const RecordKeeper &Records, raw_ostream &OS) {
582587
emitDXILOpCodes(DXILOps, OS);
583588
emitDXILOpClasses(Records, OS);
584589
emitDXILOpParamTypes(Records, OS);
590+
emitDXILAttributes(Records, OS);
585591
emitDXILOpFunctionTypes(DXILOps, OS);
586592
emitDXILIntrinsicArgSelectTypes(Records, OS);
587593
emitDXILIntrinsicMap(DXILOps, OS);

0 commit comments

Comments
 (0)