Skip to content

Commit b5c4b14

Browse files
MrSidimsvmaksimo
authored andcommitted
Fix a crash when alias metadata is attached to a call to lifetime inrinsic
The correct translation of this case will be done later, once the spec is updated. Signed-off-by: Dmitry Sidorov <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@a84f589
1 parent 63110f3 commit b5c4b14

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 24 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);
@@ -1973,7 +1992,9 @@ bool LLVMToSPIRVBase::transDecoration(Value *V, SPIRVValue *BV) {
19731992
BV->setFPFastMathMode(M);
19741993
}
19751994
}
1976-
transMemAliasingINTELDecorations(V, BV);
1995+
if (Instruction *Inst = dyn_cast<Instruction>(V))
1996+
if (shouldTryToAddMemAliasingDecoration(Inst))
1997+
transMemAliasingINTELDecorations(Inst, BV);
19771998

19781999
if (auto *CI = dyn_cast<CallInst>(V)) {
19792000
auto OC = BV->getOpCode();
@@ -2001,18 +2022,11 @@ bool LLVMToSPIRVBase::transAlign(Value *V, SPIRVValue *BV) {
20012022

20022023
// Apply aliasing decorations to instructions annotated with aliasing metadata.
20032024
// Do it for any instruction but loads and stores.
2004-
void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Value *V, SPIRVValue *BV) {
2025+
void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Instruction *Inst,
2026+
SPIRVValue *BV) {
20052027
if (!BM->isAllowedToUseExtension(
20062028
ExtensionID::SPV_INTEL_memory_access_aliasing))
20072029
return;
2008-
// Loads and Stores are handled during memory access mask addition
2009-
if (isa<StoreInst>(V) || isa<LoadInst>(V))
2010-
return;
2011-
2012-
Instruction *Inst = dyn_cast<Instruction>(V);
2013-
if (!Inst)
2014-
return;
2015-
20162030
if (MDNode *AliasingListMD =
20172031
Inst->getMetadata(LLVMContext::MD_alias_scope)) {
20182032
auto *MemAliasList =

llvm-spirv/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)