From 8bbb5fad1a022f2327147e23079651770e266a67 Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Thu, 12 Dec 2024 16:19:55 +0100 Subject: [PATCH 1/4] [SYCL] Move SYCLLowerWGLocalMemoryPass to OptimizerEarlyEPCallback The pass transforms __sycl_allocateLocalMemory call to access of global variable @WGLocalMem . Move the transform to beginning of the optimizer since the access could enable more optimization than the function call. In addition, intel gpu compiler has a pass to transform global variable in addrspace(3) to alloca, we must run SYCLLowerWGLocalMemoryPass before it. --- clang/lib/CodeGen/BackendUtil.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 51f7f640108cf..fb75559f34735 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1028,7 +1028,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline( // configure the pipeline. OptimizationLevel Level = mapToLevel(CodeGenOpts); - if (LangOpts.SYCLIsDevice) + if (LangOpts.SYCLIsDevice) { PB.registerPipelineStartEPCallback([&](ModulePassManager &MPM, OptimizationLevel Level) { MPM.addPass(SYCLVirtualFunctionsAnalysisPass()); @@ -1043,7 +1043,14 @@ void EmitAssemblyHelper::RunOptimizationPipeline( /*ExcludeAspects=*/{"fp64"})); MPM.addPass(SYCLPropagateJointMatrixUsagePass()); }); - else if (LangOpts.SYCLIsHost && !LangOpts.SYCLESIMDBuildHostCode) + PB.registerOptimizerEarlyEPCallback([](ModulePassManager &MPM, + OptimizationLevel Level, + ThinOrFullLTOPhase) { + // Allocate static local memory in SYCL kernel scope for each allocation + // call. + MPM.addPass(SYCLLowerWGLocalMemoryPass()); + }); + } else if (LangOpts.SYCLIsHost && !LangOpts.SYCLESIMDBuildHostCode) PB.registerPipelineStartEPCallback( [&](ModulePassManager &MPM, OptimizationLevel Level) { MPM.addPass(ESIMDRemoveHostCodePass()); @@ -1191,10 +1198,6 @@ void EmitAssemblyHelper::RunOptimizationPipeline( MPM.addPass(SPIRITTAnnotationsPass()); } - // Allocate static local memory in SYCL kernel scope for each allocation - // call. - MPM.addPass(SYCLLowerWGLocalMemoryPass()); - // Process properties and annotations MPM.addPass(CompileTimePropertiesPass()); From 5198fec04c203cdaa9489adb78642721f1fd824f Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Thu, 12 Dec 2024 13:22:46 -0500 Subject: [PATCH 2/4] Update clang/lib/CodeGen/BackendUtil.cpp Update comments. Co-authored-by: Victor Lomuller --- clang/lib/CodeGen/BackendUtil.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index fb75559f34735..b95490ca2a178 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1046,8 +1046,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline( PB.registerOptimizerEarlyEPCallback([](ModulePassManager &MPM, OptimizationLevel Level, ThinOrFullLTOPhase) { - // Allocate static local memory in SYCL kernel scope for each allocation - // call. + // Lowers __sycl_allocateLocalMemory and __sycl_dynamicLocalMemoryPlaceholder + // builtin calls. MPM.addPass(SYCLLowerWGLocalMemoryPass()); }); } else if (LangOpts.SYCLIsHost && !LangOpts.SYCLESIMDBuildHostCode) From 51958b87cd57438d8ecb5c78886eb01890ded23e Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Fri, 13 Dec 2024 01:12:53 +0100 Subject: [PATCH 3/4] clangformat --- clang/lib/CodeGen/BackendUtil.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index b95490ca2a178..54a398f196ef7 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1046,8 +1046,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline( PB.registerOptimizerEarlyEPCallback([](ModulePassManager &MPM, OptimizationLevel Level, ThinOrFullLTOPhase) { - // Lowers __sycl_allocateLocalMemory and __sycl_dynamicLocalMemoryPlaceholder - // builtin calls. + // Lowers __sycl_allocateLocalMemory and + // __sycl_dynamicLocalMemoryPlaceholder builtin calls. MPM.addPass(SYCLLowerWGLocalMemoryPass()); }); } else if (LangOpts.SYCLIsHost && !LangOpts.SYCLESIMDBuildHostCode) From dcf54f85d770ccea73ebc7d6d89e8ff9efde9609 Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Fri, 13 Dec 2024 01:23:33 +0100 Subject: [PATCH 4/4] Update comments --- clang/lib/CodeGen/BackendUtil.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 54a398f196ef7..5da80084fd1f9 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -1048,6 +1048,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline( ThinOrFullLTOPhase) { // Lowers __sycl_allocateLocalMemory and // __sycl_dynamicLocalMemoryPlaceholder builtin calls. + // It is required by design to run SYCLLowerWGLocalMemoryPass after + // AlwaysInliner. MPM.addPass(SYCLLowerWGLocalMemoryPass()); }); } else if (LangOpts.SYCLIsHost && !LangOpts.SYCLESIMDBuildHostCode)