Skip to content

[SYCL] Mark calls to barrier and work-item functions as convergent #1333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions llvm/lib/SYCLLowerIR/LowerWGScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,15 @@ Value *spirv::genLinearLocalID(Instruction &Before, const Triple &TT) {
IRBuilder<> Bld(Ctx);
Bld.SetInsertPoint(&Before);

AttributeList Attr;
Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
Attribute::Convergent);

#define CREATE_CALLEE(NAME, FN_NAME) \
FunctionCallee FnCallee##NAME = M.getOrInsertFunction(FN_NAME, RetTy); \
FunctionCallee FnCallee##NAME = M.getOrInsertFunction(FN_NAME, Attr, RetTy); \
assert(FnCallee##NAME && "spirv intrinsic creation failed"); \
auto NAME = Bld.CreateCall(FnCallee##NAME, {});
auto NAME = Bld.CreateCall(FnCallee##NAME, {}); \
NAME->addAttribute(AttributeList::FunctionIndex, Attribute::Convergent);

CREATE_CALLEE(LocalInvocationId_X, "_Z27__spirv_LocalInvocationId_xv");
CREATE_CALLEE(LocalInvocationId_Y, "_Z27__spirv_LocalInvocationId_yv");
Expand Down Expand Up @@ -937,8 +942,11 @@ Instruction *spirv::genWGBarrier(Instruction &Before, const Triple &TT) {
Type *SemanticsTy = Type::getInt32Ty(Ctx);
Type *RetTy = Type::getVoidTy(Ctx);

AttributeList Attr;
Attr = Attr.addAttribute(Ctx, AttributeList::FunctionIndex,
Attribute::Convergent);
FunctionCallee FC =
M.getOrInsertFunction(Name, RetTy, ScopeTy, ScopeTy, SemanticsTy);
M.getOrInsertFunction(Name, Attr, RetTy, ScopeTy, ScopeTy, SemanticsTy);
assert(FC.getCallee() && "spirv intrinsic creation failed");

IRBuilder<> Bld(Ctx);
Expand All @@ -948,5 +956,8 @@ Instruction *spirv::genWGBarrier(Instruction &Before, const Triple &TT) {
auto ArgSema = ConstantInt::get(
ScopeTy, asUInt(spirv::MemorySemantics::SequentiallyConsistent) |
asUInt(spirv::MemorySemantics::WorkgroupMemory));
return Bld.CreateCall(FC, {ArgExec, ArgMem, ArgSema});
auto BarrierCall = Bld.CreateCall(FC, {ArgExec, ArgMem, ArgSema});
BarrierCall->addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::Convergent);
return BarrierCall;
}
43 changes: 43 additions & 0 deletions llvm/test/SYCLLowerIR/convergent.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; RUN: opt < %s -LowerWGScope -S | FileCheck %s
; RUN: opt < %s -LowerWGScope --mtriple=nvptx -S | FileCheck %s -check-prefix=CHECK-PTX


%struct.baz = type { i64 }

define internal spir_func void @wibble(%struct.baz* byval(%struct.baz) %arg1) !work_group_scope !0 {
; CHECK-PTX: %1 = call i64 @_Z27__spirv_LocalInvocationId_xv() #0
; CHECK-PTX: %2 = call i64 @_Z27__spirv_LocalInvocationId_yv() #0
; CHECK-PTX: %3 = call i64 @_Z27__spirv_LocalInvocationId_zv() #0
; CHECK-PTX: %4 = call i64 @_Z23__spirv_WorkgroupSize_yv() #0
; CHECK-PTX: %5 = call i64 @_Z23__spirv_WorkgroupSize_zv() #0
; CHECK-PTX: call void @_Z22__spirv_ControlBarrierN5__spv5ScopeES0_j(i32 2, i32 2, i32 272) #0
; CHECK: call void @__spirv_ControlBarrier(i32 2, i32 2, i32 272) #1
ret void
}

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare i64 @_Z27__spirv_LocalInvocationId_xv() #0

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare i64 @_Z27__spirv_LocalInvocationId_yv() #0

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare i64 @_Z27__spirv_LocalInvocationId_zv() #0

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare i64 @_Z23__spirv_WorkgroupSize_yv() #0

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare i64 @_Z23__spirv_WorkgroupSize_zv() #0

; CHECK-PTX: ; Function Attrs: convergent
; CHECK-PTX: declare void @_Z22__spirv_ControlBarrierN5__spv5ScopeES0_j(i32, i32, i32) #0

; CHECK-PTX: attributes #0 = { convergent }

; CHECK: ; Function Attrs: convergent
; CHECK: declare void @__spirv_ControlBarrier(i32, i32, i32) #1

; CHECK: attributes #1 = { convergent }

!0 = !{}