Skip to content

Commit e5c71ca

Browse files
committed
Added another intermediate memory barrier intrinsic that maps directly to the barrier dxil op
1 parent 8bd228c commit e5c71ca

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
@@ -108,5 +108,6 @@ def int_dx_discard : DefaultAttrsIntrinsic<[], [llvm_i1_ty], []>;
108108
def int_dx_firstbituhigh : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_anyint_ty], [IntrNoMem]>;
109109
def int_dx_firstbitshigh : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_anyint_ty], [IntrNoMem]>;
110110

111+
def int_dx_memory_barrier : DefaultAttrsIntrinsic<[], [llvm_i32_ty], []>;
111112
def int_dx_group_memory_barrier_with_group_sync : DefaultAttrsIntrinsic<[], [], []>;
112113
}

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ def WaveAllBitCount : DXILOp<135, waveAllOp> {
916916

917917
def Barrier : DXILOp<80, barrier> {
918918
let Doc = "inserts a memory barrier in the shader";
919-
let arguments = [];
919+
let LLVMIntrinsic = int_dx_memory_barrier;
920+
let arguments = [Int32Ty];
920921
let result = VoidTy;
921922
let stages = [Stages<DXIL1_0, [compute, library]>];
922923
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"
@@ -67,6 +68,7 @@ static bool isIntrinsicExpansion(Function &F) {
6768
case Intrinsic::dx_sign:
6869
case Intrinsic::dx_step:
6970
case Intrinsic::dx_radians:
71+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
7072
return true;
7173
}
7274
return false;
@@ -453,6 +455,27 @@ static Value *expandRadiansIntrinsic(CallInst *Orig) {
453455
return Builder.CreateFMul(X, PiOver180);
454456
}
455457

458+
static Value *expandMemoryBarrier(CallInst *Orig, Intrinsic::ID IntrinsicId) {
459+
assert(IntrinsicId == Intrinsic::dx_group_memory_barrier_with_group_sync);
460+
unsigned BarrierMode = 0;
461+
switch (IntrinsicId) {
462+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
463+
BarrierMode = (unsigned)dxil::BarrierMode::TGSMFence |
464+
(unsigned)dxil::BarrierMode::SyncThreadGroup;
465+
break;
466+
default:
467+
report_fatal_error(Twine("Unexpected memory barrier intrinsic."),
468+
/* gen_crash_diag=*/false);
469+
break;
470+
}
471+
472+
IRBuilder<> Builder(Orig);
473+
return Builder.CreateIntrinsic(
474+
Builder.getVoidTy(), Intrinsic::dx_memory_barrier,
475+
ArrayRef<Value *>{Builder.getInt32(BarrierMode)}, nullptr,
476+
Orig->getName());
477+
}
478+
456479
static Intrinsic::ID getMaxForClamp(Intrinsic::ID ClampIntrinsic) {
457480
if (ClampIntrinsic == Intrinsic::dx_uclamp)
458481
return Intrinsic::umax;
@@ -580,6 +603,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
580603
case Intrinsic::dx_radians:
581604
Result = expandRadiansIntrinsic(Orig);
582605
break;
606+
case Intrinsic::dx_group_memory_barrier_with_group_sync:
607+
Result = expandMemoryBarrier(Orig, IntrinsicId);
608+
break;
583609
}
584610
if (Result) {
585611
Orig->replaceAllUsesWith(Result);

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,6 @@ class OpLowerer {
260260
});
261261
}
262262

263-
[[nodiscard]] bool lowerBarrier(Function &F, Intrinsic::ID IntrId,
264-
ArrayRef<dxil::BarrierMode> BarrierModes) {
265-
unsigned BarrierMode = 0;
266-
for (const dxil::BarrierMode B : BarrierModes) {
267-
BarrierMode |= (unsigned)B;
268-
}
269-
IRBuilder<> &IRB = OpBuilder.getIRB();
270-
return replaceFunction(F, [&](CallInst *CI) -> Error {
271-
std::array<Value *, 1> Args{IRB.getInt32(BarrierMode)};
272-
273-
IRB.SetInsertPoint(CI);
274-
Expected<CallInst *> OpCall =
275-
OpBuilder.tryCreateOp(OpCode::Barrier, Args, CI->getName());
276-
if (Error E = OpCall.takeError())
277-
return E;
278-
279-
CI->replaceAllUsesWith(OpCall.get());
280-
CI->eraseFromParent();
281-
282-
return Error::success();
283-
});
284-
}
285-
286263
[[nodiscard]] bool lowerToBindAndAnnotateHandle(Function &F) {
287264
IRBuilder<> &IRB = OpBuilder.getIRB();
288265
Type *Int32Ty = IRB.getInt32Ty();
@@ -669,11 +646,6 @@ class OpLowerer {
669646
HasErrors |= replaceFunctionWithOp(F, OpCode); \
670647
break;
671648
#include "DXILOperation.inc"
672-
case Intrinsic::dx_group_memory_barrier_with_group_sync:
673-
HasErrors |= lowerBarrier(
674-
F, ID,
675-
{dxil::BarrierMode::TGSMFence, dxil::BarrierMode::SyncThreadGroup});
676-
break;
677649
case Intrinsic::dx_handle_fromBinding:
678650
HasErrors |= lowerHandleFromBinding(F);
679651
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)