Skip to content

Commit d4807ba

Browse files
authored
[SYCL] Fixup SYCL metadata when dead argument is optimized and enable DAE for ESIMD (#10644)
This change enables DAE for ESIMD kernels and adds code to fixup metadata inserted by the SYCL FE when an argument is removed. I found the metadata problem when running our E2E tests after enabling DAE for ESIMD but it does not seem like an ESIMD-specific issue. This change is tested by existing E2E tests. The metadata fixup is required because downstream code in llvm-spirv assumes the number of operands for the metadata will match the actual number of arguments for the kernel. Another option would be to fix the downstream code, but making the metadata be accurate to the actual kernel the downstream code will see seemed to be a better root cause fix to me. Let me know if you disagree with the above, or if you have a way to prevent explicitly listing all of the metadata to fix, which I don't love. --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 0289d2a commit d4807ba

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,10 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
575575

576576
// We can't modify arguments if the function is not local
577577
// but we can do so for SYCL kernel functions.
578-
// DAE is not currently supported for ESIMD kernels.
579-
bool FuncIsSyclNonEsimdKernel =
578+
bool FuncIsSyclKernel =
580579
CheckSYCLKernels &&
581-
(F.getCallingConv() == CallingConv::SPIR_KERNEL || IsNVPTXKernel(&F)) &&
582-
!F.getMetadata("sycl_explicit_simd");
583-
bool FuncIsLive = !F.hasLocalLinkage() && !FuncIsSyclNonEsimdKernel;
580+
(F.getCallingConv() == CallingConv::SPIR_KERNEL || IsNVPTXKernel(&F));
581+
bool FuncIsLive = !F.hasLocalLinkage() && !FuncIsSyclKernel;
584582
if (FuncIsLive && (!ShouldHackArguments || F.isIntrinsic())) {
585583
markLive(F);
586584
return;
@@ -820,6 +818,37 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
820818
MDOmitArgs.push_back(AliveArg ? MDOmitArgFalse : MDOmitArgTrue);
821819
F->setMetadata("sycl_kernel_omit_args",
822820
llvm::MDNode::get(F->getContext(), MDOmitArgs));
821+
822+
// Update metadata inserted by the SYCL FE to match the new kernel
823+
// signature.
824+
auto FixupMetadata = [&](StringRef MDName) {
825+
auto MDToFixup = F->getMetadata(MDName);
826+
if (MDToFixup) {
827+
assert(MDToFixup->getNumOperands() == MDOmitArgs.size() &&
828+
"Unexpected metadata operands");
829+
SmallVector<Metadata *, 10> NewMDOps;
830+
for (unsigned int i = 0; i < MDToFixup->getNumOperands(); i++) {
831+
const auto *MDConst = cast<ConstantAsMetadata>(MDOmitArgs[i]);
832+
bool ArgWasRemoved =
833+
static_cast<bool>(cast<ConstantInt>(MDConst->getValue())
834+
->getValue()
835+
.getZExtValue());
836+
if (!ArgWasRemoved)
837+
NewMDOps.push_back(MDToFixup->getOperand(i));
838+
}
839+
F->setMetadata(MDName, llvm::MDNode::get(F->getContext(), NewMDOps));
840+
}
841+
};
842+
FixupMetadata("kernel_arg_buffer_location");
843+
FixupMetadata("kernel_arg_runtime_aligned");
844+
FixupMetadata("kernel_arg_exclusive_ptr");
845+
FixupMetadata("kernel_arg_addr_space");
846+
FixupMetadata("kernel_arg_access_qual");
847+
FixupMetadata("kernel_arg_type");
848+
FixupMetadata("kernel_arg_base_type");
849+
FixupMetadata("kernel_arg_type_qual");
850+
FixupMetadata("kernel_arg_accessor_ptr");
851+
FixupMetadata("kernel_arg_name");
823852
}
824853

825854
// Find out the new return value.

sycl/test/check_device_code/buffer_location_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clangxx -fsycl -c -fsycl-device-only -S -emit-llvm %s -o - | FileCheck %s
22

3-
// CHECK: define {{.*}}spir_kernel void @_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E15kernel_function{{.*}} !kernel_arg_buffer_location ![[MDBL:[0-9]+]]
4-
// CHECK: ![[MDBL]] = !{i32 3, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 2, i32 -1, i32 -1, i32 -1, i32 2, i32 -1, i32 -1, i32 -1, i32 -1}
3+
// CHECK: define {{.*}}spir_kernel void @_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E15kernel_function(){{.*}} !kernel_arg_buffer_location ![[MDBL:[0-9]+]]
4+
// CHECK: ![[MDBL]] = !{}
55

66
#include <sycl/sycl.hpp>
77

sycl/test/esimd/dae.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clangxx -fsycl-device-only -Xclang -fsycl-is-device -emit-llvm %s -S -o %t.ll -I %sycl_include
2+
// RUN: FileCheck %s --input-file %t.ll
3+
4+
// Check SYCL FE metadata is updated when dead argument elimination removes an
5+
// argument
6+
7+
#include <sycl/ext/intel/esimd.hpp>
8+
#include <sycl/sycl.hpp>
9+
using namespace sycl;
10+
11+
template <typename name, typename Func>
12+
__attribute__((sycl_kernel)) void my_kernel(Func kernelFunc) {
13+
kernelFunc();
14+
}
15+
16+
SYCL_EXTERNAL SYCL_ESIMD_FUNCTION ESIMD_NOINLINE void callee(int x) {}
17+
18+
// CHECK: define dso_local spir_kernel {{.*}} !kernel_arg_addr_space ![[#MD:]]
19+
// CHECK: !kernel_arg_access_qual ![[#MD]] !kernel_arg_type ![[#MD]] !kernel_arg_base_type ![[#MD]] !kernel_arg_type_qual ![[#MD]] !kernel_arg_accessor_ptr ![[#MD]]
20+
SYCL_EXTERNAL void __attribute__((noinline)) caller(int x) {
21+
my_kernel<class kernel_abc>([=]() SYCL_ESIMD_KERNEL { callee(x); });
22+
}
23+
24+
//CHECK: [[#MD]] = !{}

sycl/test/esimd/genx_func_attr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SYCL_ESIMD_FUNCTION SYCL_EXTERNAL ESIMD_NOINLINE void callee(int x) {
2424
// inherits SLMSize and NBarrierCount from callee
2525
void caller_abc(int x) {
2626
kernel<class kernel_abc>([=]() SYCL_ESIMD_KERNEL { callee(x); });
27-
// CHECK: define dso_local spir_kernel void @_ZTSZ10caller_abciE10kernel_abc(i32 noundef "VCArgumentIOKind"="0" %{{.*}}) local_unnamed_addr #2
27+
// CHECK: define dso_local spir_kernel void @_ZTSZ10caller_abciE10kernel_abc() local_unnamed_addr #2
2828
}
2929

3030
// inherits only NBarrierCount from callee
@@ -33,7 +33,7 @@ void caller_xyz(int x) {
3333
slm_init(1235); // also works in non-O0
3434
callee(x);
3535
});
36-
// CHECK: define dso_local spir_kernel void @_ZTSZ10caller_xyziE10kernel_xyz(i32 noundef "VCArgumentIOKind"="0" %{{.*}}) local_unnamed_addr #2
36+
// CHECK: define dso_local spir_kernel void @_ZTSZ10caller_xyziE10kernel_xyz() local_unnamed_addr #2
3737
}
3838

3939
// CHECK: attributes #2 = { {{.*}} "VCNamedBarrierCount"="13" "VCSLMSize"="2469"

sycl/test/esimd/slm_init_specconst_size.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %clangxx -O2 -fsycl -fsycl-device-only -Xclang -no-opaque-pointers -emit-llvm %s -o %t
22
// RUN: sycl-post-link -split-esimd -lower-esimd -O2 -S %t -o %t.table
3-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-TYPED %s -input-file=%t_esimd_0.ll
3+
// RUN: FileCheck %s -input-file=%t_esimd_0.ll
44
// RUN: %clangxx -O2 -fsycl -fsycl-device-only -Xclang -opaque-pointers -emit-llvm %s -o %t
55
// RUN: sycl-post-link -split-esimd -lower-esimd -O2 -S %t -o %t.table
6-
// RUN: FileCheck --check-prefixes=CHECK,CHECK-OPAQUE %s -input-file=%t_esimd_0.ll
6+
// RUN: FileCheck %s -input-file=%t_esimd_0.ll
77
// Checks that we set 0 as VCSLMSize when slm_init is used with
88
// non-constant operand, like with specialization constant.
99

@@ -24,8 +24,7 @@ int main() {
2424
[=](sycl::kernel_handler kh) SYCL_ESIMD_KERNEL {
2525
slm_init(kh.get_specialization_constant<Size>());
2626
});
27-
// CHECK-TYPED: define weak_odr dso_local spir_kernel void @{{.*}}(i8 addrspace(1)* noundef align 1 "VCArgumentIOKind"="0" %{{.*}}) local_unnamed_addr #1
28-
// CHECK-OPAQUE: define weak_odr dso_local spir_kernel void @{{.*}}(ptr addrspace(1) noundef align 1 "VCArgumentIOKind"="0" %{{.*}}) local_unnamed_addr #1
27+
// CHECK: define weak_odr dso_local spir_kernel void @{{.*}}() local_unnamed_addr #1
2928
});
3029
}
3130

0 commit comments

Comments
 (0)