Skip to content

Commit 73e957f

Browse files
[SYCL][ESIMD] Fixed compiler crash in LowerESIMDVecArg pass (#2556)
This fixes potential compiler crash in LowerESIMDVecArg pass, which I encountered while writing a small test. Just to be clear, this doesn't happen in a real test, but potentially could happen. The problem arises when Global is used in simple instruction, not directly in ConstantExpr, e.g.: ``` @GlobalGRF_data = dso_local global %"class._ZTSN2cl4sycl5INTEL3gpu4simdIiLi2512EEE.cl::sycl::INTEL::gpu::simd" undef, align 16384 define void @no_crash(<2512 x i32> %simd_val) { %cast = addrspacecast %"class._ZTSN2cl4sycl5INTEL3gpu4simdIiLi2512EEE.cl::sycl::INTEL::gpu::simd"* @GlobalGRF_data to %"class._ZTSN2cl4sycl5INTEL3gpu4simdIiLi2512EEE.cl::sycl::INTEL::gpu::simd" addrspace(4)* ... } ``` It crashed in `ESIMDLowerVecArgPass::createNewConstantExpr`.
1 parent bb78d2c commit 73e957f

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

llvm/lib/SYCLLowerIR/LowerESIMDVecArg.cpp

+5-42
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ class ESIMDLowerVecArgPass {
8989
Function *rewriteFunc(Function &F);
9090
Type *getSimdArgPtrTyOrNull(Value *arg);
9191
void fixGlobals(Module &M);
92-
void replaceConstExprWithGlobals(Module &M);
93-
ConstantExpr *createNewConstantExpr(GlobalVariable *newGlobalVar,
94-
Type *oldGlobalType, Value *old);
9592
void removeOldGlobals();
9693
};
9794

@@ -229,41 +226,6 @@ Function *ESIMDLowerVecArgPass::rewriteFunc(Function &F) {
229226
return NF;
230227
}
231228

232-
// Replace ConstantExpr if it contains old global variable.
233-
ConstantExpr *
234-
ESIMDLowerVecArgPass::createNewConstantExpr(GlobalVariable *NewGlobalVar,
235-
Type *OldGlobalType, Value *Old) {
236-
ConstantExpr *NewConstantExpr = nullptr;
237-
238-
if (isa<GlobalVariable>(Old)) {
239-
NewConstantExpr = cast<ConstantExpr>(
240-
ConstantExpr::getBitCast(NewGlobalVar, OldGlobalType));
241-
return NewConstantExpr;
242-
}
243-
244-
auto InnerMost = createNewConstantExpr(
245-
NewGlobalVar, OldGlobalType, cast<ConstantExpr>(Old)->getOperand(0));
246-
247-
NewConstantExpr = cast<ConstantExpr>(
248-
cast<ConstantExpr>(Old)->getWithOperandReplaced(0, InnerMost));
249-
250-
return NewConstantExpr;
251-
}
252-
253-
// Globals are part of ConstantExpr. This loop iterates over
254-
// all such instances and replaces them with a new ConstantExpr
255-
// consisting of new global vector* variable.
256-
void ESIMDLowerVecArgPass::replaceConstExprWithGlobals(Module &M) {
257-
for (auto &GlobalVars : OldNewGlobal) {
258-
auto &G = *GlobalVars.first;
259-
for (auto UseOfG : G.users()) {
260-
auto NewGlobal = GlobalVars.second;
261-
auto NewConstExpr = createNewConstantExpr(NewGlobal, G.getType(), UseOfG);
262-
UseOfG->replaceAllUsesWith(NewConstExpr);
263-
}
264-
}
265-
}
266-
267229
// This function creates new global variables of type vector* type
268230
// when old one is of simd* type.
269231
void ESIMDLowerVecArgPass::fixGlobals(Module &M) {
@@ -288,16 +250,17 @@ void ESIMDLowerVecArgPass::fixGlobals(Module &M) {
288250
}
289251
}
290252

291-
replaceConstExprWithGlobals(M);
292-
293253
removeOldGlobals();
294254
}
295255

296256
// Remove old global variables from the program.
297257
void ESIMDLowerVecArgPass::removeOldGlobals() {
298258
for (auto &G : OldNewGlobal) {
299-
G.first->removeDeadConstantUsers();
300-
G.first->eraseFromParent();
259+
auto OldGlob = G.first;
260+
auto NewGlobal = G.second;
261+
OldGlob->replaceAllUsesWith(
262+
ConstantExpr::getBitCast(NewGlobal, OldGlob->getType()));
263+
OldGlob->eraseFromParent();
301264
}
302265
}
303266

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -ESIMDLowerVecArg -S | FileCheck %s
3+
4+
; This test checks that there is no compiler crash when a Global
5+
; is used in simple instruction, not directly in ConstantExpr.
6+
7+
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"
8+
target triple = "spir64-unknown-unknown-sycldevice"
9+
10+
%"class.cl::sycl::INTEL::gpu::simd" = type { <2512 x i32> }
11+
12+
; CHECK: @Global = dso_local global <2512 x i32> undef, align 16384
13+
@Global = dso_local global %"class.cl::sycl::INTEL::gpu::simd" undef, align 16384
14+
15+
define void @no_crash(<2512 x i32> %simd_val) {
16+
; CHECK-LABEL: @no_crash(
17+
; CHECK-NEXT: [[CAST:%.*]] = addrspacecast %"class.cl::sycl::INTEL::gpu::simd"* bitcast (<2512 x i32>* @Global to %"class.cl::sycl::INTEL::gpu::simd"*) to %"class.cl::sycl::INTEL::gpu::simd" addrspace(4)*
18+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr %"class.cl::sycl::INTEL::gpu::simd", %"class.cl::sycl::INTEL::gpu::simd" addrspace(4)* [[CAST]], i64 0, i32 0
19+
; CHECK-NEXT: store <2512 x i32> [[SIMD_VAL:%.*]], <2512 x i32> addrspace(4)* [[GEP]], align 16384
20+
; CHECK-NEXT: ret void
21+
;
22+
%cast = addrspacecast %"class.cl::sycl::INTEL::gpu::simd"* @Global to %"class.cl::sycl::INTEL::gpu::simd" addrspace(4)*
23+
%gep = getelementptr %"class.cl::sycl::INTEL::gpu::simd", %"class.cl::sycl::INTEL::gpu::simd" addrspace(4)* %cast, i64 0, i32 0
24+
store <2512 x i32> %simd_val, <2512 x i32> addrspace(4)* %gep, align 16384
25+
ret void
26+
}

0 commit comments

Comments
 (0)