Skip to content

Commit 1ad58f3

Browse files
committed
Added another intermediate memory barrier intrinsic that maps directly to the barrier dxil op
1 parent 88b493e commit 1ad58f3

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ def int_dx_splitdouble : DefaultAttrsIntrinsic<[llvm_anyint_ty, LLVMMatchType<0>
9393
[LLVMScalarOrSameVectorWidth<0, llvm_double_ty>], [IntrNoMem]>;
9494
def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
9595

96+
def int_dx_memory_barrier : DefaultAttrsIntrinsic<[], [llvm_i32_ty], []>;
9697
def int_dx_group_memory_barrier_with_group_sync : DefaultAttrsIntrinsic<[], [], []>;
9798
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,8 @@ def WaveGetLaneIndex : DXILOp<111, waveGetLaneIndex> {
823823

824824
def Barrier : DXILOp<80, barrier> {
825825
let Doc = "inserts a memory barrier in the shader";
826-
let arguments = [];
826+
let LLVMIntrinsic = int_dx_memory_barrier;
827+
let arguments = [Int32Ty];
827828
let result = VoidTy;
828829
let stages = [Stages<DXIL1_0, [compute, library]>];
829830
let attributes = [Attributes<DXIL1_0, []>];

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// opcodes in DirectX Intermediate Language (DXIL).
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "DXILConstants.h"
1314
#include "DXILIntrinsicExpansion.h"
1415
#include "DirectX.h"
1516
#include "llvm/ADT/STLExtras.h"
@@ -66,6 +67,7 @@ static bool isIntrinsicExpansion(Function &F) {
6667
case Intrinsic::dx_sign:
6768
case Intrinsic::dx_step:
6869
case Intrinsic::dx_radians:
70+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
6971
return true;
7072
}
7173
return false;
@@ -452,6 +454,27 @@ static Value *expandRadiansIntrinsic(CallInst *Orig) {
452454
return Builder.CreateFMul(X, PiOver180);
453455
}
454456

457+
static Value *expandMemoryBarrier(CallInst *Orig, Intrinsic::ID IntrinsicId) {
458+
assert(IntrinsicId == Intrinsic::dx_group_memory_barrier_with_group_sync);
459+
unsigned BarrierMode = 0;
460+
switch (IntrinsicId) {
461+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
462+
BarrierMode = (unsigned)dxil::BarrierMode::TGSMFence |
463+
(unsigned)dxil::BarrierMode::SyncThreadGroup;
464+
break;
465+
default:
466+
report_fatal_error(Twine("Unexpected memory barrier intrinsic."),
467+
/* gen_crash_diag=*/false);
468+
break;
469+
}
470+
471+
IRBuilder<> Builder(Orig);
472+
return Builder.CreateIntrinsic(
473+
Builder.getVoidTy(), Intrinsic::dx_memory_barrier,
474+
ArrayRef<Value *>{Builder.getInt32(BarrierMode)}, nullptr,
475+
Orig->getName());
476+
}
477+
455478
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
456479
Intrinsic::ID ClampIntrinsic) {
457480
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -586,6 +609,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
586609
case Intrinsic::dx_radians:
587610
Result = expandRadiansIntrinsic(Orig);
588611
break;
612+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
613+
Result = expandMemoryBarrier(Orig, IntrinsicId);
614+
break;
589615
}
590616
if (Result) {
591617
Orig->replaceAllUsesWith(Result);

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,6 @@ class OpLowerer {
209209
});
210210
}
211211

212-
[[nodiscard]] bool lowerBarrier(Function &F, Intrinsic::ID IntrId,
213-
ArrayRef<dxil::BarrierMode> BarrierModes) {
214-
unsigned BarrierMode = 0;
215-
for (const dxil::BarrierMode B : BarrierModes) {
216-
BarrierMode |= (unsigned)B;
217-
}
218-
IRBuilder<> &IRB = OpBuilder.getIRB();
219-
return replaceFunction(F, [&](CallInst *CI) -> Error {
220-
std::array<Value *, 1> Args{IRB.getInt32(BarrierMode)};
221-
222-
IRB.SetInsertPoint(CI);
223-
Expected<CallInst *> OpCall =
224-
OpBuilder.tryCreateOp(OpCode::Barrier, Args, CI->getName());
225-
if (Error E = OpCall.takeError())
226-
return E;
227-
228-
CI->replaceAllUsesWith(OpCall.get());
229-
CI->eraseFromParent();
230-
231-
return Error::success();
232-
});
233-
}
234-
235212
[[nodiscard]] bool lowerToBindAndAnnotateHandle(Function &F) {
236213
IRBuilder<> &IRB = OpBuilder.getIRB();
237214

@@ -499,11 +476,6 @@ class OpLowerer {
499476
HasErrors |= replaceFunctionWithOp(F, OpCode); \
500477
break;
501478
#include "DXILOperation.inc"
502-
case Intrinsic::dx_group_memory_barrier_with_group_sync:
503-
HasErrors |= lowerBarrier(
504-
F, ID,
505-
{dxil::BarrierMode::TGSMFence, dxil::BarrierMode::SyncThreadGroup});
506-
break;
507479
case Intrinsic::dx_handle_fromBinding:
508480
HasErrors |= lowerHandleFromBinding(F);
509481
break;

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-op-lower -mtriple=dxil-pc-shadermodel6.3-library < %s | FileCheck %s --check-prefix=CHECK
1+
; RUN: opt -S -dxil-intrinsic-expansion -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:

0 commit comments

Comments
 (0)