blob: b58d6aab8857c2a5a28cd2ae46d4d401188dfc9d [file] [edit]
; RUN: opt -S -dxil-resource-type -dxil-resource-access -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
; This tests that we can resolve the case when cycling through a dynamic
; resource, this might be generated by something like:
;
; RWBuffer<float> buf[3] : register(u0);
; cbuffer C { uint N; }
; ...
; RWBuffer<float> src = buf[0];
; RWBuffer<float> dst = buf[1];
; RWBuffer<float> mid = buf[2];
; for (uint i = 0; i < N; ++i) {
; dst[tid] = src[tid] * 0.5f;
; RWBuffer<float> tmp = src; src = mid; mid = dst; dst = tmp;
; }
; CHECK-LABEL: define void @main_same_binding
; CHECK: for.cond:
; CHECK-NEXT: %[[SRC:.*]] = phi i32 [ 0, %entry ], [ %[[MID:.*]], %for.body ]
; CHECK-NEXT: %[[DST:.*]] = phi i32 [ 1, %entry ], [ %[[SRC]], %for.body ]
; CHECK-NEXT: %[[MID]] = phi i32 [ 2, %entry ], [ %[[DST]], %for.body ]
; CHECK-NOT: phi target(
; CHECK: for.body:
; CHECK-NEXT: %[[SRCH:.*]] = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 3, i32 %[[SRC]], ptr nonnull @.str.buf)
; CHECK-NEXT: %{{.*}} = call { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %[[SRCH]], i32 %tid)
; CHECK: %[[DSTH:.*]] = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 3, i32 %[[DST]], ptr nonnull @.str.buf)
; CHECK-NEXT: call void @llvm.dx.resource.store.typedbuffer.tdx.TypedBuffer_f32_1_0_0t.f32(target("dx.TypedBuffer", float, 1, 0, 0) %[[DSTH]], i32 %tid, float %mul)
@.str.buf = private unnamed_addr constant [4 x i8] c"buf\00", align 1
@In.str = private unnamed_addr constant [3 x i8] c"In\00", align 1
@Out.str = private unnamed_addr constant [4 x i8] c"Out\00", align 1
define void @main_same_binding(i32 %tid, i32 %n) {
entry:
%0 = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 3, i32 0, ptr nonnull @.str.buf)
%1 = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 3, i32 1, ptr nonnull @.str.buf)
%2 = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 3, i32 2, ptr nonnull @.str.buf)
br label %for.cond
for.cond:
%src = phi target("dx.TypedBuffer", float, 1, 0, 0) [ %0, %entry ], [ %mid, %for.body ]
%dst = phi target("dx.TypedBuffer", float, 1, 0, 0) [ %1, %entry ], [ %src, %for.body ]
%mid = phi target("dx.TypedBuffer", float, 1, 0, 0) [ %2, %entry ], [ %dst, %for.body ]
%i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
%exit = icmp eq i32 %i, %n
br i1 %exit, label %end, label %for.body
for.body:
%srcptr = call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %src, i32 %tid)
%v = load float, ptr %srcptr, align 4
%mul = fmul float %v, 5.000000e-01
%dstptr = call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %dst, i32 %tid)
store float %mul, ptr %dstptr, align 4
%inc = add nuw i32 %i, 1
br label %for.cond
end:
ret void
}
; This tests that we can resolve the case when there is a phi choice of dynamic
; resources obfuscated by a phi loop, this might be generated by something like:
;
; RWBuffer<float> In[2] : register(u0);
; RWBuffer<float> Out : register(u2);
; ...
; RWBuffer<float> R = In[0];
; for (uint i = 0; i < id.y; ++i)
; if (i & 1) R = In[1];
; Out[id.x] = R[id.x];
; CHECK-LABEL: define void @main_conditional_update
; CHECK: entry:
; CHECK-NEXT: %[[OUT:.*]] = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 2, i32 1, i32 0, ptr nonnull @Out.str)
; CHECK-NEXT: br label %for.cond
; CHECK: for.cond:
; CHECK-NEXT: %[[R:.*]] = phi i32 [ 0, %entry ], [ %[[RNEXT:.*]], %for.inc ]
; CHECK-NOT: phi target(
; CHECK: for.inc:
; CHECK-NEXT: %[[RNEXT]] = phi i32 [ 1, %if.then ], [ %[[R]], %for.body ]
; CHECK: for.end:
; CHECK-NEXT: %[[INH:.*]] = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 2, i32 %[[R]], ptr nonnull @In.str)
; CHECK-NEXT: %[[LOAD:.*]] = call { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %[[INH]], i32 %x)
; CHECK: call void @llvm.dx.resource.store.typedbuffer.tdx.TypedBuffer_f32_1_0_0t.f32(target("dx.TypedBuffer", float, 1, 0, 0) %[[OUT]], i32 %x, float %{{.*}})
define void @main_conditional_update(i32 %x, i32 %y) {
entry:
%in0 = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 2, i32 0, ptr nonnull @In.str)
%in1 = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 0, i32 2, i32 1, ptr nonnull @In.str)
%out = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0_0t(i32 0, i32 2, i32 1, i32 0, ptr nonnull @Out.str)
br label %for.cond
for.cond:
%r = phi target("dx.TypedBuffer", float, 1, 0, 0) [ %in0, %entry ], [ %r.next, %for.inc ]
%i = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
%cmp = icmp ult i32 %i, %y
br i1 %cmp, label %for.body, label %for.end
for.body:
%and = and i32 %i, 1
%odd = icmp ne i32 %and, 0
br i1 %odd, label %if.then, label %for.inc
if.then:
br label %for.inc
for.inc:
%r.next = phi target("dx.TypedBuffer", float, 1, 0, 0) [ %in1, %if.then ], [ %r, %for.body ]
%inc = add nuw i32 %i, 1
br label %for.cond
for.end:
%rptr = call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %r, i32 %x)
%v = load float, ptr %rptr, align 4
%outptr = call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.TypedBuffer_f32_1_0_0t(target("dx.TypedBuffer", float, 1, 0, 0) %out, i32 %x)
store float %v, ptr %outptr, align 4
ret void
}