Skip to content

Commit

Permalink
Tolerate more inputs during alias.scope/noalias MD translation (Khron…
Browse files Browse the repository at this point in the history
…osGroup#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 <[email protected]>
  • Loading branch information
MrSidims authored Apr 13, 2021
1 parent f653af3 commit 2c61561
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SPIRVId> ListId;
for (const MDOperand &MDListOp : AliasingListMD->operands()) {
if (MDNode *ScopeMD = dyn_cast<MDNode>(MDListOp)) {
assert(ScopeMD->getNumOperands() > 1 &&
"Aliasing scope MD must have at least two operands");
MDNode *DomainMD = cast<MDNode>(ScopeMD->getOperand(1));
if (ScopeMD->getNumOperands() < 2)
return nullptr;
MDNode *DomainMD = dyn_cast<MDNode>(ScopeMD->getOperand(1));
if (!DomainMD)
return nullptr;
auto *Domain =
M->getOrAddAliasDomainDeclINTELInst(std::vector<SPIRVId>(), DomainMD);
auto *Scope =
Expand All @@ -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());
}

Expand Down Expand Up @@ -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()));
}
Expand Down

0 comments on commit 2c61561

Please sign in to comment.