Skip to content

Commit 27a3990

Browse files
fineg74sarnex
andauthored
[SYCL][ESIMD] Refactor lowering of slm allocation into a separate pass (#13221)
Co-authored-by: Nick Sarnie <[email protected]>
1 parent 3ea29b2 commit 27a3990

File tree

9 files changed

+49
-10
lines changed

9 files changed

+49
-10
lines changed

llvm/include/llvm/SYCLLowerIR/ESIMD/LowerESIMD.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ class ESIMDOptimizeVecArgCallConvPass
6666
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
6767
};
6868

69-
// Lowers calls __esimd_slm_alloc, __esimd_slm_free and __esimd_slm_init APIs.
70-
// See more details in the .cpp file.
71-
size_t lowerSLMReservationCalls(Module &M);
72-
7369
// Lowers calls to __esimd_set_kernel_properties
7470
class SYCLLowerESIMDKernelPropsPass
7571
: public PassInfoMixin<SYCLLowerESIMDKernelPropsPass> {
@@ -95,6 +91,14 @@ class ESIMDRemoveOptnoneNoinlinePass
9591
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
9692
};
9793

94+
// Lowers calls __esimd_slm_alloc, __esimd_slm_free and __esimd_slm_init APIs.
95+
// See more details in the .cpp file.
96+
class ESIMDLowerSLMReservationCalls
97+
: public PassInfoMixin<ESIMDLowerSLMReservationCalls> {
98+
public:
99+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
100+
};
101+
98102
} // namespace llvm
99103

100104
#endif // LLVM_SYCLLOWERIR_LOWERESIMD_H

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ MODULE_PASS("sycl-propagate-joint-matrix-usage", SYCLPropagateJointMatrixUsagePa
157157
MODULE_PASS("sycl-add-opt-level-attribute", SYCLAddOptLevelAttributePass())
158158
MODULE_PASS("compile-time-properties", CompileTimePropertiesPass())
159159
MODULE_PASS("cleanup-sycl-metadata", CleanupSYCLMetadataPass())
160+
MODULE_PASS("lower-slm-reservation-calls", ESIMDLowerSLMReservationCalls())
160161
#undef MODULE_PASS
161162

162163
#ifndef MODULE_PASS_WITH_PARAMS

llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ PreservedAnalyses SYCLLowerESIMDPass::run(Module &M,
20132013
generateKernelMetadata(M);
20142014
// This function needs to run after generateKernelMetadata, as it
20152015
// uses the generated metadata:
2016-
size_t AmountOfESIMDIntrCalls = lowerSLMReservationCalls(M);
2016+
size_t AmountOfESIMDIntrCalls = 0;
20172017
SmallPtrSet<Type *, 4> GVTS = collectGenXVolatileTypes(M);
20182018
lowerGlobalStores(M, GVTS);
20192019
lowerGlobalsToVector(M);
@@ -2082,6 +2082,13 @@ size_t SYCLLowerESIMDPass::runOnFunction(Function &F,
20822082
// process ESIMD builtins that go through special handling instead of
20832083
// the translation procedure
20842084

2085+
// SLM allocation API will be lowered in LowerESIMDSlmReservationCalls
2086+
// pass
2087+
if (Name.starts_with("__esimd_slm_alloc") ||
2088+
Name.starts_with("__esimd_slm_free")) {
2089+
continue;
2090+
}
2091+
20852092
if (Name.starts_with("__esimd_svm_block_ld") ||
20862093
Name.starts_with("__esimd_slm_block_ld")) {
20872094
translateBlockLoad(*CI, Name.starts_with("__esimd_slm_block_ld"));

llvm/lib/SYCLLowerIR/ESIMD/LowerESIMDSlmReservation.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "llvm/IR/Function.h"
6868
#include "llvm/IR/Instruction.h"
6969
#include "llvm/IR/Module.h"
70+
#include "llvm/Pass.h"
7071

7172
#include <set>
7273
#include <unordered_map>
@@ -81,11 +82,19 @@ namespace {
8182
constexpr int DebugLevel = 0;
8283
#endif
8384

85+
bool isGenXSLMInit(const Function &F) {
86+
constexpr char SLM_INIT_PREFIX[] = "llvm.genx.slm.init";
87+
return F.getName().starts_with(SLM_INIT_PREFIX);
88+
}
89+
8490
bool isSlmInitCall(const CallInst *CI) {
8591
if (!CI)
8692
return false;
8793
Function *F = CI->getCalledFunction();
88-
return F && esimd::isSlmInit(*F);
94+
if (!F)
95+
return false;
96+
assert(!esimd::isSlmInit(*F) && "Should have been translated already");
97+
return isGenXSLMInit(*F);
8998
}
9099

91100
bool isSlmAllocCall(const CallInst *CI) {
@@ -543,7 +552,8 @@ TraversalResult findMaxSLMUsageAlongAllPaths(const ScopedCallGraph::Node *Cur,
543552
return Res;
544553
}
545554

546-
size_t lowerSLMReservationCalls(Module &M) {
555+
PreservedAnalyses
556+
ESIMDLowerSLMReservationCalls::run(Module &M, ModuleAnalysisManager &MAM) {
547557
// Create a detailed "scoped" call graph. Scope start/end is marked with
548558
// x = __esimd_slm_alloc / __esimd_slm_free(x)
549559
//
@@ -567,7 +577,7 @@ size_t lowerSLMReservationCalls(Module &M) {
567577
#endif
568578
if (SCG.getNumSLMScopes() == 0) {
569579
// Early bail out if nothing to analyze.
570-
return 0;
580+
return PreservedAnalyses::none();
571581
}
572582
// Use the detailed call graph nodes to calculate maximum possible SLM usage
573583
// at any "scope start" node, and record this info in the result map.
@@ -669,7 +679,8 @@ size_t lowerSLMReservationCalls(Module &M) {
669679
#endif
670680
}
671681
// Return the number of API calls transformed.
672-
return SLMAllocCallCnt;
682+
return SLMAllocCallCnt == 0 ? PreservedAnalyses::none()
683+
: PreservedAnalyses::all();
673684
}
674685

675686
} // namespace llvm

llvm/test/SYCLLowerIR/ESIMD/slm_init_same_bb.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; This test confirms we don't assert for a single slm_init call
22
; in a basic block with two predecessors.
33
;
4-
; RUN: opt < %s -passes=LowerESIMD -S | FileCheck %s
4+
; RUN: opt < %s -passes=LowerESIMD,lower-slm-reservation-calls -S | FileCheck %s
55

66
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
77
target triple = "spir64-unknown-unknown"

llvm/tools/sycl-post-link/sycl-post-link.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ bool lowerEsimdConstructs(module_split::ModuleDesc &MD) {
669669
MainFPM.addPass(InstCombinePass{});
670670
MainFPM.addPass(DCEPass{});
671671
}
672+
MPM.addPass(ESIMDLowerSLMReservationCalls{});
672673
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
673674
MPM.addPass(GenXSPIRVWriterAdaptor(/*RewriteTypes=*/true,
674675
/*RewriteSingleElementVectorsIn*/ false));

sycl/test-e2e/ESIMD/slm_alloc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
//
2+
// REQUIRES-INTEL-DRIVER: lin: 28454, win: 101.5333
23
// RUN: %{build} -o %t.1.out
34
// RUN: %{run} %t.1.out
45
//
56
// Vary the test case by forcing inlining of the functions with slm_allocator:
67
// RUN: %{build} -DFORCE_INLINE -o %t.2.out
78
// RUN: %{run} %t.2.out
89

10+
// Check if the test sill passes with O0
11+
// RUN: %{build} -O0 -o %t.3.out
12+
// RUN: %{run} %t.3.out
13+
914
// This is end-to-end test for the slm_allocator API used together with the
1015
// slm_init. The call graph is:
1116
// Test1(kernel) - uses slm_init(SLM_IN_KERNEL)

sycl/test-e2e/ESIMD/slm_alloc_many_kernels_many_funcs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// Windows doesn't yet have full shutdown().
33
// UNSUPPORTED: ze_debug && windows
4+
// REQUIRES-INTEL-DRIVER: lin: 28454, win: 101.5333
45
//
56
// RUN: %{build} -o %t.1.out
67
// RUN: %{run} %t.1.out
@@ -9,6 +10,10 @@
910
// RUN: %{build} -DFORCE_INLINE -o %t.2.out
1011
// RUN: %{run} %t.2.out
1112

13+
// Check if the test sill passes with O0
14+
// RUN: %{build} -O0 -o %t.3.out
15+
// RUN: %{run} %t.3.out
16+
1217
// Checks validity of SLM frame offsets in case of complex call graph with two
1318
// kernels and 2 functions all using SLM, and one of the functions using two
1419
// slm_allocator objects with nested liveness ranges.

sycl/test-e2e/ESIMD/slm_alloc_many_kernels_one_func.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// Windows doesn't yet have full shutdown().
33
// UNSUPPORTED: ze_debug && windows
4+
// REQUIRES-INTEL-DRIVER: lin: 28454, win: 101.5333
45
//
56
// RUN: %{build} -o %t.1.out
67
// RUN: %{run} %t.1.out
@@ -9,6 +10,10 @@
910
// RUN: %{build} -DFORCE_INLINE -o %t.2.out
1011
// RUN: %{run} %t.2.out
1112

13+
// Check if the test sill passes with O0
14+
// RUN: %{build} -O0 -o %t.3.out
15+
// RUN: %{run} %t.3.out
16+
1217
// Check that SLM frame offset of a function foo called from two kernels Test1
1318
// and Test2 is the maximum of the SLM size used in both kernels.
1419

0 commit comments

Comments
 (0)