From 2c6156165c9c3e60e82174a7e4a751e53d462624 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 13 Apr 2021 19:18:52 +0300 Subject: [PATCH] Tolerate more inputs during alias.scope/noalias MD translation (#994) According to https://llvm.org/docs/LangRef.html aliasing metadata must have following layout: List { MD Node Scope1, (other scopes) ... } Scope1 { MD Node Scope1, MD Node Domain1, (optional MD String) } ... (other scopes) Domain1 { MD Node Domain1, (optional MD String) } ... (other domains) and this pattern is actually being checked in LLVM's ScopedNoAliasAA.cpp. But in a harsh reality LLVM opt have bugs, which can result in this pattern violation. So lets be more tolerant to the input IR module. Signed-off-by: Dmitry Sidorov --- lib/SPIRV/SPIRVWriter.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index f05d62d4bb..8b7a191fed 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -1222,14 +1222,16 @@ SPIRVValue *LLVMToSPIRVBase::transAtomicLoad(LoadInst *LD, // TODO: add support for an optional string operand. SPIRVEntry *addMemAliasingINTELInstructions(SPIRVModule *M, MDNode *AliasingListMD) { - assert(AliasingListMD->getNumOperands() > 0 && - "Aliasing list MD must have at least one operand"); + if (AliasingListMD->getNumOperands() == 0) + return nullptr; std::vector ListId; for (const MDOperand &MDListOp : AliasingListMD->operands()) { if (MDNode *ScopeMD = dyn_cast(MDListOp)) { - assert(ScopeMD->getNumOperands() > 1 && - "Aliasing scope MD must have at least two operands"); - MDNode *DomainMD = cast(ScopeMD->getOperand(1)); + if (ScopeMD->getNumOperands() < 2) + return nullptr; + MDNode *DomainMD = dyn_cast(ScopeMD->getOperand(1)); + if (!DomainMD) + return nullptr; auto *Domain = M->getOrAddAliasDomainDeclINTELInst(std::vector(), DomainMD); auto *Scope = @@ -1251,6 +1253,8 @@ void transAliasingMemAccess(SPIRVModule *BM, MDNode *AliasingListMD, return; MemoryAccess[0] |= MemAccessMask; auto *MemAliasList = addMemAliasingINTELInstructions(BM, AliasingListMD); + if (!MemAliasList) + return; MemoryAccess.push_back(MemAliasList->getId()); } @@ -1869,12 +1873,16 @@ void LLVMToSPIRVBase::transMemAliasingINTELDecorations(Value *V, Inst->getMetadata(LLVMContext::MD_alias_scope)) { auto *MemAliasList = addMemAliasingINTELInstructions(BM, AliasingListMD); + if (!MemAliasList) + return; BV->addDecorate(new SPIRVDecorateId( internal::DecorationAliasScopeINTEL, BV, MemAliasList->getId())); } else if (MDNode *AliasingListMD = Inst->getMetadata(LLVMContext::MD_noalias)) { auto *MemAliasList = addMemAliasingINTELInstructions(BM, AliasingListMD); + if (!MemAliasList) + return; BV->addDecorate(new SPIRVDecorateId( internal::DecorationNoAliasINTEL, BV, MemAliasList->getId())); }