Skip to content

Commit a84f589

Browse files
authored
Fix a crash when alias metadata is attached to a call to lifetime inrinsic (#1059)
* Fix a crash when alias metadata is attached to a call to lifetime intrinsic The correct translation of this case will be done later, once the spec is updated. Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent c4c9b0c commit a84f589

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ static SPIRVMemoryModelKind getMemoryModel(Module &M) {
122122
return SPIRVMemoryModelKind::MemoryModelMax;
123123
}
124124

125+
static bool shouldTryToAddMemAliasingDecoration(Instruction *Inst) {
126+
// Limit translation of aliasing metadata with only this set of instructions
127+
// gracefully considering others as compilation mistakes and ignoring them
128+
if (!Inst->mayReadOrWriteMemory())
129+
return false;
130+
// Loads and Stores are handled during memory access mask addition
131+
if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
132+
return false;
133+
CallInst *CI = dyn_cast<CallInst>(Inst);
134+
if (!CI)
135+
return true;
136+
// Calls to intrinsics are skipped. At some point lifetime start/end will be
137+
// handled separately, but specification isn't ready.
138+
if (Function *Fun = CI->getCalledFunction())
139+
if (Fun->isIntrinsic())
140+
return false;
141+
return true;
142+
}
143+
125144
LLVMToSPIRVBase::LLVMToSPIRVBase(SPIRVModule *SMod)
126145
: M(nullptr), Ctx(nullptr), BM(SMod), SrcLang(0), SrcLangVer(0) {
127146
DbgTran = std::make_unique<LLVMToSPIRVDbgTran>(nullptr, SMod, this);
@@ -1956,7 +1975,9 @@ bool LLVMToSPIRVBase::transDecoration(Value *V, SPIRVValue *BV) {
19561975
BV->setFPFastMathMode(M);
19571976
}
19581977
}
1959-
transMemAliasingINTELDecorations(V, BV);
1978+
if (Instruction *Inst = dyn_cast<Instruction>(V))
1979+
if (shouldTryToAddMemAliasingDecoration(Inst))
1980+
transMemAliasingINTELDecorations(Inst, BV);
19601981

19611982
if (auto *CI = dyn_cast<CallInst>(V)) {
19621983
auto OC = BV->getOpCode();
@@ -1984,19 +2005,11 @@ bool LLVMToSPIRVBase::transAlign(Value *V, SPIRVValue *BV) {
19842005

19852006
// Apply aliasing decorations to instructions annotated with aliasing metadata.
19862007
// Do it for any instruction but loads and stores.
1987-
void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Value *V,
2008+
void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Instruction *Inst,
19882009
SPIRVValue *BV) {
19892010
if (!BM->isAllowedToUseExtension(
19902011
ExtensionID::SPV_INTEL_memory_access_aliasing))
19912012
return;
1992-
// Loads and Stores are handled during memory access mask addition
1993-
if (isa<StoreInst>(V) || isa<LoadInst>(V))
1994-
return;
1995-
1996-
Instruction *Inst = dyn_cast<Instruction>(V);
1997-
if (!Inst)
1998-
return;
1999-
20002013
if (MDNode *AliasingListMD =
20012014
Inst->getMetadata(LLVMContext::MD_alias_scope)) {
20022015
auto *MemAliasList =

lib/SPIRV/SPIRVWriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class LLVMToSPIRVBase {
102102
SPIRVValue *transAsmINTEL(InlineAsm *Asm);
103103
SPIRVValue *transAsmCallINTEL(CallInst *Call, SPIRVBasicBlock *BB);
104104
bool transDecoration(Value *V, SPIRVValue *BV);
105-
void transMemAliasingINTELDecorations(Value *V, SPIRVValue *BV);
105+
void transMemAliasingINTELDecorations(Instruction *V, SPIRVValue *BV);
106106
SPIRVWord transFunctionControlMask(Function *);
107107
SPIRVFunction *transFunctionDecl(Function *F);
108108
void transVectorComputeMetadata(Function *F);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; The test checks if the translator won't crash
2+
; TODO need to figure out how to translate the alias.scope/noalias metadata
3+
; in a case when it attached to a call to lifetime intrinsic. Now just skip it.
4+
5+
; RUN: llvm-as %s -o %t.bc
6+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_memory_access_aliasing -o %t.spv
7+
8+
; ModuleID = 'main'
9+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
10+
target triple = "spir64-unknown-unknown"
11+
12+
%class.anon = type { i8 }
13+
14+
; Function Attrs: nounwind
15+
define spir_kernel void @lifetime_simple()
16+
{
17+
%1 = alloca i32
18+
%2 = bitcast i32* %1 to i8*
19+
call void @llvm.lifetime.start.p0i8(i64 -1, i8* %2), !noalias !1
20+
ret void
21+
}
22+
23+
; Function Attrs: nounwind
24+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #0
25+
26+
attributes #0 = { nounwind }
27+
28+
!1 = !{!2}
29+
!2 = distinct !{!2, !3}
30+
!3 = distinct !{!3}

0 commit comments

Comments
 (0)