Skip to content

Commit 36dbb21

Browse files
committed
Tablegen simple
1 parent e41187a commit 36dbb21

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,5 @@ def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32
8989
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
9090
def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
9191

92-
def int_dx_memory_barrier : DefaultAttrsIntrinsic<[], [llvm_i32_ty], []>;
9392
def int_dx_group_memory_barrier_with_group_sync : DefaultAttrsIntrinsic<[], [], []>;
9493
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ class Attributes<Version ver = DXIL1_0, list<DXILAttribute> attrs> {
293293
list<DXILAttribute> op_attrs = attrs;
294294
}
295295

296+
class IntrinsicSelect<Intrinsic intr, list<string> extra_args> {
297+
Intrinsic Intr = intr;
298+
list<string> ExtraArgs = extra_args;
299+
}
300+
296301
// Abstraction DXIL Operation
297302
class DXILOp<int opcode, DXILOpClass opclass> {
298303
// A short description of the operation
@@ -307,6 +312,8 @@ class DXILOp<int opcode, DXILOpClass opclass> {
307312
// LLVM Intrinsic DXIL Operation maps to
308313
Intrinsic LLVMIntrinsic = ?;
309314

315+
list<IntrinsicSelect> intrinsic_selects = [];
316+
310317
// Result type of the op
311318
DXILOpParamType result;
312319

@@ -813,7 +820,12 @@ def WaveGetLaneIndex : DXILOp<111, waveGetLaneIndex> {
813820

814821
def Barrier : DXILOp<80, barrier> {
815822
let Doc = "inserts a memory barrier in the shader";
816-
let LLVMIntrinsic = int_dx_memory_barrier;
823+
let intrinsic_selects = [
824+
IntrinsicSelect<
825+
int_dx_group_memory_barrier_with_group_sync,
826+
[ "OpBuilder.getIRB().getInt32((unsigned)BarrierMode::SyncThreadGroup | (unsigned)BarrierMode::TGSMFence)" ]>,
827+
];
828+
817829
let arguments = [Int32Ty];
818830
let result = VoidTy;
819831
let stages = [Stages<DXIL1_0, [compute, library]>];

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "DXILIntrinsicExpansion.h"
14-
#include "DXILConstants.h"
1514
#include "DirectX.h"
1615
#include "llvm/ADT/STLExtras.h"
1716
#include "llvm/ADT/SmallVector.h"
@@ -66,7 +65,6 @@ static bool isIntrinsicExpansion(Function &F) {
6665
case Intrinsic::dx_sign:
6766
case Intrinsic::dx_step:
6867
case Intrinsic::dx_radians:
69-
case Intrinsic::dx_group_memory_barrier_with_group_sync:
7068
return true;
7169
}
7270
return false;
@@ -453,27 +451,6 @@ static Value *expandRadiansIntrinsic(CallInst *Orig) {
453451
return Builder.CreateFMul(X, PiOver180);
454452
}
455453

456-
static Value *expandMemoryBarrier(CallInst *Orig, Intrinsic::ID IntrinsicId) {
457-
assert(IntrinsicId == Intrinsic::dx_group_memory_barrier_with_group_sync);
458-
unsigned BarrierMode = 0;
459-
switch (IntrinsicId) {
460-
case Intrinsic::dx_group_memory_barrier_with_group_sync:
461-
BarrierMode = (unsigned)dxil::BarrierMode::TGSMFence |
462-
(unsigned)dxil::BarrierMode::SyncThreadGroup;
463-
break;
464-
default:
465-
report_fatal_error(Twine("Unexpected memory barrier intrinsic."),
466-
/* gen_crash_diag=*/false);
467-
break;
468-
}
469-
470-
IRBuilder<> Builder(Orig);
471-
return Builder.CreateIntrinsic(
472-
Builder.getVoidTy(), Intrinsic::dx_memory_barrier,
473-
ArrayRef<Value *>{Builder.getInt32(BarrierMode)}, nullptr,
474-
Orig->getName());
475-
}
476-
477454
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
478455
Intrinsic::ID ClampIntrinsic) {
479456
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -597,9 +574,6 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
597574
case Intrinsic::dx_radians:
598575
Result = expandRadiansIntrinsic(Orig);
599576
break;
600-
case Intrinsic::dx_group_memory_barrier_with_group_sync:
601-
Result = expandMemoryBarrier(Orig, IntrinsicId);
602-
break;
603577
}
604578
if (Result) {
605579
Orig->replaceAllUsesWith(Result);

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,21 @@ class OpLowerer {
105105
return false;
106106
}
107107

108-
[[nodiscard]]
109-
bool replaceFunctionWithOp(Function &F, dxil::OpCode DXILOp) {
108+
[[nodiscard]] bool replaceFunctionWithOp(Function &F, dxil::OpCode DXILOp,
109+
ArrayRef<Value *> ExtraArgs) {
110110
bool IsVectorArgExpansion = isVectorArgExpansion(F);
111111
return replaceFunction(F, [&](CallInst *CI) -> Error {
112-
SmallVector<Value *> Args;
113-
OpBuilder.getIRB().SetInsertPoint(CI);
112+
SmallVector<Value *> NewArgs;
114113
if (IsVectorArgExpansion) {
115-
SmallVector<Value *> NewArgs = argVectorFlatten(CI, OpBuilder.getIRB());
116-
Args.append(NewArgs.begin(), NewArgs.end());
114+
NewArgs = argVectorFlatten(CI, OpBuilder.getIRB());
117115
} else
118-
Args.append(CI->arg_begin(), CI->arg_end());
116+
NewArgs.append(CI->arg_begin(), CI->arg_end());
119117

120-
Expected<CallInst *> OpCall =
121-
OpBuilder.tryCreateOp(DXILOp, Args, CI->getName(), F.getReturnType());
118+
NewArgs.append(ExtraArgs.begin(), ExtraArgs.end());
119+
120+
OpBuilder.getIRB().SetInsertPoint(CI);
121+
Expected<CallInst *> OpCall = OpBuilder.tryCreateOp(
122+
DXILOp, NewArgs, CI->getName(), F.getReturnType());
122123
if (Error E = OpCall.takeError())
123124
return E;
124125

@@ -472,9 +473,9 @@ class OpLowerer {
472473
switch (ID) {
473474
default:
474475
continue;
475-
#define DXIL_OP_INTRINSIC(OpCode, Intrin) \
476-
case Intrin: \
477-
HasErrors |= replaceFunctionWithOp(F, OpCode); \
476+
#define DXIL_OP_INTRINSIC(OpCode, Intrin, ExtraArgs) \
477+
case Intrin: \
478+
HasErrors |= replaceFunctionWithOp(F, OpCode, ExtraArgs); \
478479
break;
479480
#include "DXILOperation.inc"
480481
case Intrinsic::dx_handle_fromBinding:

llvm/test/CodeGen/DirectX/group_memory_barrier_with_group_sync.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S -dxil-intrinsic-expansion -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library < %s | FileCheck %s --check-prefix=CHECK
1+
; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library < %s | FileCheck %s --check-prefix=CHECK
22

33
define void @test_group_memory_barrier_with_group_sync() {
44
entry:

llvm/utils/TableGen/DXILEmitter.cpp

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ using namespace llvm::dxil;
3333

3434
namespace {
3535

36+
struct DXILIntrinsicSelect {
37+
StringRef Intrinsic;
38+
SmallVector<StringRef, 4> ExtraArgs;
39+
};
40+
3641
struct DXILOperationDesc {
3742
std::string OpName; // name of DXIL operation
3843
int OpCode; // ID of DXIL operation
@@ -43,8 +48,7 @@ struct DXILOperationDesc {
4348
SmallVector<const Record *> OverloadRecs;
4449
SmallVector<const Record *> StageRecs;
4550
SmallVector<const Record *> AttrRecs;
46-
StringRef Intrinsic; // The llvm intrinsic map to OpName. Default is "" which
47-
// means no map exists
51+
SmallVector<DXILIntrinsicSelect> IntrinsicSelects;
4852
SmallVector<StringRef, 4>
4953
ShaderStages; // shader stages to which this applies, empty for all.
5054
int OverloadParamIndex; // Index of parameter with overload type.
@@ -158,14 +162,43 @@ DXILOperationDesc::DXILOperationDesc(const Record *R) {
158162
OpName);
159163
}
160164

161-
const RecordVal *RV = R->getValue("LLVMIntrinsic");
162-
if (RV && RV->getValue()) {
163-
if (DefInit *DI = dyn_cast<DefInit>(RV->getValue())) {
164-
auto *IntrinsicDef = DI->getDef();
165-
auto DefName = IntrinsicDef->getName();
166-
assert(DefName.starts_with("int_") && "invalid intrinsic name");
167-
// Remove the int_ from intrinsic name.
168-
Intrinsic = DefName.substr(4);
165+
auto GetIntrinsicName = [](const RecordVal *RV) -> StringRef {
166+
if (RV && RV->getValue()) {
167+
if (DefInit *DI = dyn_cast<DefInit>(RV->getValue())) {
168+
auto *IntrinsicDef = DI->getDef();
169+
auto DefName = IntrinsicDef->getName();
170+
assert(DefName.starts_with("int_") && "invalid intrinsic name");
171+
// Remove the int_ from intrinsic name.
172+
return DefName.substr(4);
173+
}
174+
}
175+
return "";
176+
};
177+
178+
{
179+
DXILIntrinsicSelect IntrSelect;
180+
IntrSelect.Intrinsic = GetIntrinsicName(R->getValue("LLVMIntrinsic"));
181+
if (IntrSelect.Intrinsic.size())
182+
IntrinsicSelects.emplace_back(std::move(IntrSelect));
183+
}
184+
185+
Recs = R->getValueAsListOfDefs("intrinsic_selects");
186+
if (Recs.size()) {
187+
if (IntrinsicSelects.size()) {
188+
PrintFatalError(R,
189+
Twine("LLVMIntrinsic and intrinsic_match cannot be both "
190+
"defined for DXIL operation - ") +
191+
OpName);
192+
} else {
193+
for (const Record *R : Recs) {
194+
DXILIntrinsicSelect IntrSelect;
195+
IntrSelect.Intrinsic = GetIntrinsicName(R->getValue("Intr"));
196+
auto ExtraArgs = R->getValueAsListOfStrings("ExtraArgs");
197+
for (StringRef Arg : ExtraArgs) {
198+
IntrSelect.ExtraArgs.push_back(Arg);
199+
}
200+
IntrinsicSelects.emplace_back(std::move(IntrSelect));
201+
}
169202
}
170203
}
171204
}
@@ -378,10 +411,17 @@ static void emitDXILIntrinsicMap(ArrayRef<DXILOperationDesc> Ops,
378411
OS << "#ifdef DXIL_OP_INTRINSIC\n";
379412
OS << "\n";
380413
for (const auto &Op : Ops) {
381-
if (Op.Intrinsic.empty())
414+
if (Op.IntrinsicSelects.empty()) {
382415
continue;
383-
OS << "DXIL_OP_INTRINSIC(dxil::OpCode::" << Op.OpName
384-
<< ", Intrinsic::" << Op.Intrinsic << ")\n";
416+
}
417+
for (const DXILIntrinsicSelect &MappedIntr : Op.IntrinsicSelects) {
418+
OS << "DXIL_OP_INTRINSIC(dxil::OpCode::" << Op.OpName
419+
<< ", Intrinsic::" << MappedIntr.Intrinsic << ", (ArrayRef<Value *> {";
420+
for (const StringRef &Arg : MappedIntr.ExtraArgs) {
421+
OS << Arg << ", ";
422+
}
423+
OS << "}))\n";
424+
}
385425
}
386426
OS << "\n";
387427
OS << "#undef DXIL_OP_INTRINSIC\n";

0 commit comments

Comments
 (0)